]> git.cryptolib.org Git - arm-crypto-lib.git/blob - seed/seed_c.c
added arcfour/rc4
[arm-crypto-lib.git] / seed / seed_c.c
1 /* seed_c.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2010  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19  /**
20  * \file        seed_c.c
21  * \author      Daniel Otte 
22  * \date        2007-06-1
23  * \brief       SEED parts in C for ARM
24  * \par License 
25  * GPL
26  * 
27  */
28 #include <stdint.h>
29 #include <string.h>
30 #include "seed.h"
31 #include "seed_sbox.h"
32
33
34 static
35 uint32_t g_function(uint32_t x);
36 /******************************************************************************/
37
38 static
39 void changeendian32(uint32_t * a){
40         *a = (*a & 0x000000FF) << 24 |
41                  (*a & 0x0000FF00) <<  8 |
42                  (*a & 0x00FF0000) >>  8 |
43                  (*a & 0xFF000000) >> 24;
44 }
45
46 /******************************************************************************/
47 static
48 uint32_t bigendian_sum32(uint32_t a, uint32_t b){
49         changeendian32(&a);
50         changeendian32(&b);
51         a += b;
52         changeendian32(&a);
53         return a;
54 }
55
56 /******************************************************************************/
57 static 
58 uint32_t bigendian_sub32(uint32_t a, uint32_t b){
59         changeendian32(&a);
60         changeendian32(&b);
61         a -= b;
62         changeendian32(&a);
63         return a;
64 }
65
66 /******************************************************************************/
67 static inline
68 uint64_t bigendian_rotl8_64(uint64_t a){
69         /*
70         changeendian64(&a);
71         a = (a<<8) | (a>>(64-8));
72         changeendian64(&a);
73         */
74         a = (a>>8) | (a<<(64-8));
75         return a;
76 }
77
78 /******************************************************************************/
79 static inline
80 uint64_t bigendian_rotr8_64(uint64_t a){
81         /*
82         changeendian64(&a);
83         a = (a>>8) | (a<<(64-8));
84         changeendian64(&a);
85         */
86         a = (a<<8) | (a>>(64-8));
87         return a;
88 }
89
90 /******************************************************************************/
91 static
92 uint64_t f_function(const uint64_t* a, uint32_t k0, uint32_t k1){
93         uint32_t c,d;
94
95         c = *a & 0x00000000FFFFFFFFLL;
96         d = (*a>>32) & 0x00000000FFFFFFFFLL;
97         
98         c ^= k0; d ^= k1;
99         d ^= c;
100         d = g_function(d);
101         c = bigendian_sum32(c,d);
102         c = g_function(c);
103         d = bigendian_sum32(c,d);
104         d = g_function(d);
105         c = bigendian_sum32(c,d);
106         return ((uint64_t)d << 32) | c;
107 }
108
109 /******************************************************************************/
110 #define M0 0xfc
111 #define M1 0xf3
112 #define M2 0xcf
113 #define M3 0x3f
114
115 #define X3 (((uint8_t*)(&x))[0])
116 #define X2 (((uint8_t*)(&x))[1])
117 #define X1 (((uint8_t*)(&x))[2])
118 #define X0 (((uint8_t*)(&x))[3])
119
120 #define Z3 (((uint8_t*)(&z))[0])
121 #define Z2 (((uint8_t*)(&z))[1])
122 #define Z1 (((uint8_t*)(&z))[2])
123 #define Z0 (((uint8_t*)(&z))[3])
124
125 static
126 uint32_t g_function(uint32_t x){
127         uint32_t z;
128         /* sbox substitution */
129         X3 = seed_sbox2[X3];
130         X2 = seed_sbox1[X2];
131         X1 = seed_sbox2[X1];
132         X0 = seed_sbox1[X0];
133         /* now the permutation */
134         Z0 = (X0 & M0) ^ (X1 & M1) ^ (X2 & M2) ^ (X3 & M3);
135         Z1 = (X0 & M1) ^ (X1 & M2) ^ (X2 & M3) ^ (X3 & M0);
136         Z2 = (X0 & M2) ^ (X1 & M3) ^ (X2 & M0) ^ (X3 & M1);
137         Z3 = (X0 & M3) ^ (X1 & M0) ^ (X2 & M1) ^ (X3 & M2);
138         return z;
139 }
140 /******************************************************************************/
141 typedef struct {
142         uint32_t k0, k1;
143 } keypair_t;
144
145 keypair_t getnextkeys(uint32_t *keystate, uint8_t curround){
146         keypair_t ret;
147         if (curround>15){
148                 /* ERROR */
149                 ret.k0 = ret.k1 = 0;
150         } else {
151         /*      ret.k0 = g_function(keystate[0] + keystate[2] - pgm_read_dword(&(seed_kc[curround])));
152                 ret.k1 = g_function(keystate[1] - keystate[3] + pgm_read_dword(&(seed_kc[curround]))); */
153                 ret.k0 = bigendian_sum32(keystate[0], keystate[2]);
154                 ret.k0 = bigendian_sub32(ret.k0, seed_kc[curround]);
155                 ret.k0 = g_function(ret.k0);
156                 ret.k1 = bigendian_sub32(keystate[1], keystate[3]);
157                 ret.k1 = bigendian_sum32(ret.k1, seed_kc[curround]);
158                 ret.k1 = g_function(ret.k1);
159                 
160                 if (curround & 1){
161                         /* odd round (1,3,5, ...) */
162                         ((uint64_t*)keystate)[1] = bigendian_rotl8_64( ((uint64_t*)keystate)[1] );
163                 } else {
164                         /* even round (0,2,4, ...) */
165                         ((uint64_t*)keystate)[0] = bigendian_rotr8_64(((uint64_t*)keystate)[0]);
166                 }
167         }
168         return ret;
169 }
170
171
172 /******************************************************************************/
173
174 keypair_t getprevkeys(uint32_t *keystate, uint8_t curround){
175         keypair_t ret;
176         if (curround>15){
177                 /* ERROR */
178                 ret.k0 = ret.k1 = 0;
179         } else {
180                 if (curround & 1){
181                         /* odd round (1,3,5, ..., 15) */
182                         ((uint64_t*)keystate)[1] = bigendian_rotr8_64( ((uint64_t*)keystate)[1] );
183                 } else {
184                         /* even round (0,2,4, ..., 14) */
185                         ((uint64_t*)keystate)[0] = bigendian_rotl8_64(((uint64_t*)keystate)[0]);
186                 }
187         /*      ret.k0 = g_function(keystate[0] + keystate[2] - pgm_read_dword(&(seed_kc[curround])));
188                 ret.k1 = g_function(keystate[1] - keystate[3] + pgm_read_dword(&(seed_kc[curround]))); */
189                 ret.k0 = bigendian_sum32(keystate[0], keystate[2]);
190                 ret.k0 = bigendian_sub32(ret.k0, seed_kc[curround]);
191                 ret.k0 = g_function(ret.k0);
192                 ret.k1 = bigendian_sub32(keystate[1], keystate[3]);
193                 ret.k1 = bigendian_sum32(ret.k1, seed_kc[curround]);
194                 ret.k1 = g_function(ret.k1);
195                 }
196         return ret;
197 }
198
199 /******************************************************************************/
200
201 void seed_init(const void * key, seed_ctx_t * ctx){
202         memcpy(ctx->k, key, 128/8);
203 }
204
205 /******************************************************************************/
206
207 #define L (((uint64_t*)buffer)[0])
208 #define R (((uint64_t*)buffer)[1])
209
210 void seed_enc(void * buffer, const seed_ctx_t * ctx){
211         uint8_t r;
212         keypair_t k;
213         for(r=0; r<8; ++r){
214                         k = getnextkeys(((seed_ctx_t*)ctx)->k, 2*r);
215 /*
216         DEBUG_S("\r\n\tDBG ka,0: "); cli_hexdump(&k.k0, 4);
217         DEBUG_S("\r\n\tDBG ka,1: "); cli_hexdump(&k.k1, 4);
218         DEBUG_S("\r\n\t DBG L: "); cli_hexdump((uint8_t*)buffer+0, 8);
219         DEBUG_S("\r\n\t DBG R: "); cli_hexdump((uint8_t*)buffer+8, 8);
220 */
221                         L ^= f_function(&R,k.k0,k.k1);
222                         
223                         k = getnextkeys(((seed_ctx_t*)ctx)->k, 2*r+1);
224 /*
225         DEBUG_S("\r\n\tDBG kb,0: "); cli_hexdump(&k.k0, 4);
226         DEBUG_S("\r\n\tDBG kb,1: "); cli_hexdump(&k.k1, 4);
227         DEBUG_S("\r\n\t DBG L: "); cli_hexdump((uint8_t*)buffer+8, 8);
228         DEBUG_S("\r\n\t DBG R: "); cli_hexdump((uint8_t*)buffer+0, 8);
229 */
230                         R ^= f_function(&L,k.k0,k.k1);
231         }
232         /* just an exchange without temp. variable */
233         L ^= R;
234         R ^= L;
235         L ^= R;
236 }
237
238 /******************************************************************************/
239
240 #define L (((uint64_t*)buffer)[0])
241 #define R (((uint64_t*)buffer)[1])
242
243 void seed_dec(void * buffer, const seed_ctx_t * ctx){
244         int8_t r;
245         keypair_t k;
246         for(r=7; r>=0; --r){
247                         k = getprevkeys(((seed_ctx_t*)ctx)->k, 2*r+1);
248 /*
249         DEBUG_S("\r\n\tDBG ka,0: "); cli_hexdump(&k.k0, 4);
250         DEBUG_S("\r\n\tDBG ka,1: "); cli_hexdump(&k.k1, 4);
251         DEBUG_S("\r\n\t DBG L: "); cli_hexdump((uint8_t*)buffer+0, 8);
252         DEBUG_S("\r\n\t DBG R: "); cli_hexdump((uint8_t*)buffer+8, 8);
253 */
254                         L ^= f_function(&R,k.k0,k.k1);
255                         
256                         k = getprevkeys(((seed_ctx_t*)ctx)->k, 2*r+0);
257 /*
258         DEBUG_S("\r\n\tDBG kb,0: "); cli_hexdump(&k.k0, 4);
259         DEBUG_S("\r\n\tDBG kb,1: "); cli_hexdump(&k.k1, 4);
260         DEBUG_S("\r\n\t DBG L: "); cli_hexdump((uint8_t*)buffer+8, 8);
261         DEBUG_S("\r\n\t DBG R: "); cli_hexdump((uint8_t*)buffer+0, 8);
262 */
263                         R ^= f_function(&L,k.k0,k.k1);
264         }
265         /* just an exchange without temp. variable */
266         L ^= R;
267         R ^= L;
268         L ^= R;
269 }
270
271
272
273
274
275
276
277
278
279
280