comparison libtomcrypt/src/ciphers/kasumi.c @ 382:0cbe8f6dbf9e

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f) to branch 'au.asn.ucc.matt.dropbear' (head 02c413252c90e9de8e03d91e9939dde3029f5c0a)
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 02:41:05 +0000
parents
children f849a5ca2efc
comparison
equal deleted inserted replaced
379:b66a00272a90 382:0cbe8f6dbf9e
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.com
10 */
11
12 /**
13 @file kasumi.c
14 Implementation of the 3GPP Kasumi block cipher
15 Derived from the 3GPP standard source code
16 */
17
18 #include "tomcrypt.h"
19
20 #ifdef LTC_KASUMI
21
22 typedef unsigned u16;
23
24 #define ROL16(x, y) ((((x)<<(y)) | ((x)>>(16-(y)))) & 0xFFFF)
25
26 const struct ltc_cipher_descriptor kasumi_desc = {
27 "kasumi",
28 21,
29 16, 16, 8, 8,
30 &kasumi_setup,
31 &kasumi_ecb_encrypt,
32 &kasumi_ecb_decrypt,
33 &kasumi_test,
34 &kasumi_done,
35 &kasumi_keysize,
36 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
37 };
38
39 static u16 FI( u16 in, u16 subkey )
40 {
41 u16 nine, seven;
42 static const u16 S7[128] = {
43 54, 50, 62, 56, 22, 34, 94, 96, 38, 6, 63, 93, 2, 18,123, 33,
44 55,113, 39,114, 21, 67, 65, 12, 47, 73, 46, 27, 25,111,124, 81,
45 53, 9,121, 79, 52, 60, 58, 48,101,127, 40,120,104, 70, 71, 43,
46 20,122, 72, 61, 23,109, 13,100, 77, 1, 16, 7, 82, 10,105, 98,
47 117,116, 76, 11, 89,106, 0,125,118, 99, 86, 69, 30, 57,126, 87,
48 112, 51, 17, 5, 95, 14, 90, 84, 91, 8, 35,103, 32, 97, 28, 66,
49 102, 31, 26, 45, 75, 4, 85, 92, 37, 74, 80, 49, 68, 29,115, 44,
50 64,107,108, 24,110, 83, 36, 78, 42, 19, 15, 41, 88,119, 59, 3 };
51 static const u16 S9[512] = {
52 167,239,161,379,391,334, 9,338, 38,226, 48,358,452,385, 90,397,
53 183,253,147,331,415,340, 51,362,306,500,262, 82,216,159,356,177,
54 175,241,489, 37,206, 17, 0,333, 44,254,378, 58,143,220, 81,400,
55 95, 3,315,245, 54,235,218,405,472,264,172,494,371,290,399, 76,
56 165,197,395,121,257,480,423,212,240, 28,462,176,406,507,288,223,
57 501,407,249,265, 89,186,221,428,164, 74,440,196,458,421,350,163,
58 232,158,134,354, 13,250,491,142,191, 69,193,425,152,227,366,135,
59 344,300,276,242,437,320,113,278, 11,243, 87,317, 36, 93,496, 27,
60 487,446,482, 41, 68,156,457,131,326,403,339, 20, 39,115,442,124,
61 475,384,508, 53,112,170,479,151,126,169, 73,268,279,321,168,364,
62 363,292, 46,499,393,327,324, 24,456,267,157,460,488,426,309,229,
63 439,506,208,271,349,401,434,236, 16,209,359, 52, 56,120,199,277,
64 465,416,252,287,246, 6, 83,305,420,345,153,502, 65, 61,244,282,
65 173,222,418, 67,386,368,261,101,476,291,195,430, 49, 79,166,330,
66 280,383,373,128,382,408,155,495,367,388,274,107,459,417, 62,454,
67 132,225,203,316,234, 14,301, 91,503,286,424,211,347,307,140,374,
68 35,103,125,427, 19,214,453,146,498,314,444,230,256,329,198,285,
69 50,116, 78,410, 10,205,510,171,231, 45,139,467, 29, 86,505, 32,
70 72, 26,342,150,313,490,431,238,411,325,149,473, 40,119,174,355,
71 185,233,389, 71,448,273,372, 55,110,178,322, 12,469,392,369,190,
72 1,109,375,137,181, 88, 75,308,260,484, 98,272,370,275,412,111,
73 336,318, 4,504,492,259,304, 77,337,435, 21,357,303,332,483, 18,
74 47, 85, 25,497,474,289,100,269,296,478,270,106, 31,104,433, 84,
75 414,486,394, 96, 99,154,511,148,413,361,409,255,162,215,302,201,
76 266,351,343,144,441,365,108,298,251, 34,182,509,138,210,335,133,
77 311,352,328,141,396,346,123,319,450,281,429,228,443,481, 92,404,
78 485,422,248,297, 23,213,130,466, 22,217,283, 70,294,360,419,127,
79 312,377, 7,468,194, 2,117,295,463,258,224,447,247,187, 80,398,
80 284,353,105,390,299,471,470,184, 57,200,348, 63,204,188, 33,451,
81 97, 30,310,219, 94,160,129,493, 64,179,263,102,189,207,114,402,
82 438,477,387,122,192, 42,381, 5,145,118,180,449,293,323,136,380,
83 43, 66, 60,455,341,445,202,432, 8,237, 15,376,436,464, 59,461};
84
85 /* The sixteen bit input is split into two unequal halves, *
86 * nine bits and seven bits - as is the subkey */
87
88 nine = (u16)(in>>7)&0x1FF;
89 seven = (u16)(in&0x7F);
90
91 /* Now run the various operations */
92 nine = (u16)(S9[nine] ^ seven);
93 seven = (u16)(S7[seven] ^ (nine & 0x7F));
94 seven ^= (subkey>>9);
95 nine ^= (subkey&0x1FF);
96 nine = (u16)(S9[nine] ^ seven);
97 seven = (u16)(S7[seven] ^ (nine & 0x7F));
98 return (u16)(seven<<9) + nine;
99 }
100
101 static ulong32 FO( ulong32 in, int round_no, symmetric_key *key)
102 {
103 u16 left, right;
104
105 /* Split the input into two 16-bit words */
106 left = (u16)(in>>16);
107 right = (u16) in&0xFFFF;
108
109 /* Now apply the same basic transformation three times */
110 left ^= key->kasumi.KOi1[round_no];
111 left = FI( left, key->kasumi.KIi1[round_no] );
112 left ^= right;
113
114 right ^= key->kasumi.KOi2[round_no];
115 right = FI( right, key->kasumi.KIi2[round_no] );
116 right ^= left;
117
118 left ^= key->kasumi.KOi3[round_no];
119 left = FI( left, key->kasumi.KIi3[round_no] );
120 left ^= right;
121
122 return (((ulong32)right)<<16)+left;
123 }
124
125 static ulong32 FL( ulong32 in, int round_no, symmetric_key *key )
126 {
127 u16 l, r, a, b;
128 /* split out the left and right halves */
129 l = (u16)(in>>16);
130 r = (u16)(in)&0xFFFF;
131 /* do the FL() operations */
132 a = (u16) (l & key->kasumi.KLi1[round_no]);
133 r ^= ROL16(a,1);
134 b = (u16)(r | key->kasumi.KLi2[round_no]);
135 l ^= ROL16(b,1);
136 /* put the two halves back together */
137
138 return (((ulong32)l)<<16) + r;
139 }
140
141 int kasumi_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
142 {
143 ulong32 left, right, temp;
144 int n;
145
146 LTC_ARGCHK(pt != NULL);
147 LTC_ARGCHK(ct != NULL);
148 LTC_ARGCHK(skey != NULL);
149
150 LOAD32H(left, pt);
151 LOAD32H(right, pt+4);
152
153 for (n = 0; n <= 7; ) {
154 temp = FL(left, n, skey);
155 temp = FO(temp, n++, skey);
156 right ^= temp;
157 temp = FO(right, n, skey);
158 temp = FL(temp, n++, skey);
159 left ^= temp;
160 }
161
162 STORE32H(left, ct);
163 STORE32H(right, ct+4);
164
165 return CRYPT_OK;
166 }
167
168 int kasumi_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
169 {
170 ulong32 left, right, temp;
171 int n;
172
173 LTC_ARGCHK(pt != NULL);
174 LTC_ARGCHK(ct != NULL);
175 LTC_ARGCHK(skey != NULL);
176
177 LOAD32H(left, ct);
178 LOAD32H(right, ct+4);
179
180 for (n = 7; n >= 0; ) {
181 temp = FO(right, n, skey);
182 temp = FL(temp, n--, skey);
183 left ^= temp;
184 temp = FL(left, n, skey);
185 temp = FO(temp, n--, skey);
186 right ^= temp;
187 }
188
189 STORE32H(left, pt);
190 STORE32H(right, pt+4);
191
192 return CRYPT_OK;
193 }
194
195 int kasumi_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
196 {
197 static const u16 C[8] = { 0x0123,0x4567,0x89AB,0xCDEF, 0xFEDC,0xBA98,0x7654,0x3210 };
198 u16 ukey[8], Kprime[8];
199 int n;
200
201 LTC_ARGCHK(key != NULL);
202 LTC_ARGCHK(skey != NULL);
203
204 if (keylen != 16) {
205 return CRYPT_INVALID_KEYSIZE;
206 }
207
208 if (num_rounds != 0 && num_rounds != 8) {
209 return CRYPT_INVALID_ROUNDS;
210 }
211
212 /* Start by ensuring the subkeys are endian correct on a 16-bit basis */
213 for (n = 0; n < 8; n++ ) {
214 ukey[n] = (((u16)key[2*n]) << 8) | key[2*n+1];
215 }
216
217 /* Now build the K'[] keys */
218 for (n = 0; n < 8; n++) {
219 Kprime[n] = ukey[n] ^ C[n];
220 }
221
222 /* Finally construct the various sub keys */
223 for(n = 0; n < 8; n++) {
224 skey->kasumi.KLi1[n] = ROL16(ukey[n],1);
225 skey->kasumi.KLi2[n] = Kprime[(n+2)&0x7];
226 skey->kasumi.KOi1[n] = ROL16(ukey[(n+1)&0x7],5);
227 skey->kasumi.KOi2[n] = ROL16(ukey[(n+5)&0x7],8);
228 skey->kasumi.KOi3[n] = ROL16(ukey[(n+6)&0x7],13);
229 skey->kasumi.KIi1[n] = Kprime[(n+4)&0x7];
230 skey->kasumi.KIi2[n] = Kprime[(n+3)&0x7];
231 skey->kasumi.KIi3[n] = Kprime[(n+7)&0x7];
232 }
233
234 return CRYPT_OK;
235 }
236
237 void kasumi_done(symmetric_key *skey)
238 {
239 }
240
241 int kasumi_keysize(int *keysize)
242 {
243 LTC_ARGCHK(keysize != NULL);
244 if (*keysize >= 16) {
245 *keysize = 16;
246 return CRYPT_OK;
247 } else {
248 return CRYPT_INVALID_KEYSIZE;
249 }
250 }
251
252 int kasumi_test(void)
253 {
254 #ifndef LTC_TEST
255 return CRYPT_NOP;
256 #else
257 static const struct {
258 unsigned char key[16], pt[8], ct[8];
259 } tests[] = {
260
261 {
262 { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
263 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
264 { 0x4B, 0x58, 0xA7, 0x71, 0xAF, 0xC7, 0xE5, 0xE8 }
265 },
266
267 {
268 { 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
269 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
270 { 0x7E, 0xEF, 0x11, 0x3C, 0x95, 0xBB, 0x5A, 0x77 }
271 },
272
273 {
274 { 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
275 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
276 { 0x5F, 0x14, 0x06, 0x86, 0xD7, 0xAD, 0x5A, 0x39 },
277 },
278
279 {
280 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
281 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
282 { 0x2E, 0x14, 0x91, 0xCF, 0x70, 0xAA, 0x46, 0x5D }
283 },
284
285 {
286 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 },
287 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
288 { 0xB5, 0x45, 0x86, 0xF4, 0xAB, 0x9A, 0xE5, 0x46 }
289 },
290
291 };
292 unsigned char buf[2][8];
293 symmetric_key key;
294 int err, x;
295
296 for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
297 if ((err = kasumi_setup(tests[x].key, 16, 0, &key)) != CRYPT_OK) {
298 return err;
299 }
300 if ((err = kasumi_ecb_encrypt(tests[x].pt, buf[0], &key)) != CRYPT_OK) {
301 return err;
302 }
303 if ((err = kasumi_ecb_decrypt(tests[x].ct, buf[1], &key)) != CRYPT_OK) {
304 return err;
305 }
306 if (XMEMCMP(tests[x].pt, buf[1], 8) || XMEMCMP(tests[x].ct, buf[0], 8)) {
307 return CRYPT_FAIL_TESTVECTOR;
308 }
309 }
310 return CRYPT_OK;
311 #endif
312 }
313
314 #endif
315
316 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/kasumi.c,v $ */
317 /* $Revision: 1.7 $ */
318 /* $Date: 2006/11/09 03:05:44 $ */