3
|
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.org |
|
10 */ |
|
11 |
|
12 #include "mycrypt.h" |
|
13 |
|
14 #ifdef XTEA |
|
15 |
|
16 const struct _cipher_descriptor xtea_desc = |
|
17 { |
|
18 "xtea", |
|
19 1, |
|
20 16, 16, 8, 32, |
|
21 &xtea_setup, |
|
22 &xtea_ecb_encrypt, |
|
23 &xtea_ecb_decrypt, |
|
24 &xtea_test, |
|
25 &xtea_keysize |
|
26 }; |
|
27 |
|
28 int xtea_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) |
|
29 { |
|
30 unsigned long x, sum, K[4]; |
|
31 |
|
32 _ARGCHK(key != NULL); |
|
33 _ARGCHK(skey != NULL); |
|
34 |
|
35 /* check arguments */ |
|
36 if (keylen != 16) { |
|
37 return CRYPT_INVALID_KEYSIZE; |
|
38 } |
|
39 |
|
40 if (num_rounds != 0 && num_rounds != 32) { |
|
41 return CRYPT_INVALID_ROUNDS; |
|
42 } |
|
43 |
|
44 /* load key */ |
|
45 LOAD32L(K[0], key+0); |
|
46 LOAD32L(K[1], key+4); |
|
47 LOAD32L(K[2], key+8); |
|
48 LOAD32L(K[3], key+12); |
|
49 |
|
50 for (x = sum = 0; x < 32; x++) { |
|
51 skey->xtea.A[x] = (sum + K[sum&3]) & 0xFFFFFFFFUL; |
|
52 sum = (sum + 0x9E3779B9UL) & 0xFFFFFFFFUL; |
|
53 skey->xtea.B[x] = (sum + K[(sum>>11)&3]) & 0xFFFFFFFFUL; |
|
54 } |
|
55 |
|
56 #ifdef CLEAN_STACK |
|
57 zeromem(&K, sizeof(K)); |
|
58 #endif |
|
59 |
|
60 return CRYPT_OK; |
|
61 } |
|
62 |
|
63 void xtea_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key) |
|
64 { |
|
65 unsigned long y, z; |
|
66 int r; |
|
67 |
|
68 _ARGCHK(pt != NULL); |
|
69 _ARGCHK(ct != NULL); |
|
70 _ARGCHK(key != NULL); |
|
71 |
|
72 LOAD32L(y, &pt[0]); |
|
73 LOAD32L(z, &pt[4]); |
|
74 for (r = 0; r < 32; r += 4) { |
|
75 y = (y + ((((z<<4)^(z>>5)) + z) ^ key->xtea.A[r])) & 0xFFFFFFFFUL; |
|
76 z = (z + ((((y<<4)^(y>>5)) + y) ^ key->xtea.B[r])) & 0xFFFFFFFFUL; |
|
77 |
|
78 y = (y + ((((z<<4)^(z>>5)) + z) ^ key->xtea.A[r+1])) & 0xFFFFFFFFUL; |
|
79 z = (z + ((((y<<4)^(y>>5)) + y) ^ key->xtea.B[r+1])) & 0xFFFFFFFFUL; |
|
80 |
|
81 y = (y + ((((z<<4)^(z>>5)) + z) ^ key->xtea.A[r+2])) & 0xFFFFFFFFUL; |
|
82 z = (z + ((((y<<4)^(y>>5)) + y) ^ key->xtea.B[r+2])) & 0xFFFFFFFFUL; |
|
83 |
|
84 y = (y + ((((z<<4)^(z>>5)) + z) ^ key->xtea.A[r+3])) & 0xFFFFFFFFUL; |
|
85 z = (z + ((((y<<4)^(y>>5)) + y) ^ key->xtea.B[r+3])) & 0xFFFFFFFFUL; |
|
86 } |
|
87 STORE32L(y, &ct[0]); |
|
88 STORE32L(z, &ct[4]); |
|
89 } |
|
90 |
|
91 void xtea_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key) |
|
92 { |
|
93 unsigned long y, z; |
|
94 int r; |
|
95 |
|
96 _ARGCHK(pt != NULL); |
|
97 _ARGCHK(ct != NULL); |
|
98 _ARGCHK(key != NULL); |
|
99 |
|
100 LOAD32L(y, &ct[0]); |
|
101 LOAD32L(z, &ct[4]); |
|
102 for (r = 31; r >= 0; r -= 4) { |
|
103 z = (z - ((((y<<4)^(y>>5)) + y) ^ key->xtea.B[r])) & 0xFFFFFFFFUL; |
|
104 y = (y - ((((z<<4)^(z>>5)) + z) ^ key->xtea.A[r])) & 0xFFFFFFFFUL; |
|
105 |
|
106 z = (z - ((((y<<4)^(y>>5)) + y) ^ key->xtea.B[r-1])) & 0xFFFFFFFFUL; |
|
107 y = (y - ((((z<<4)^(z>>5)) + z) ^ key->xtea.A[r-1])) & 0xFFFFFFFFUL; |
|
108 |
|
109 z = (z - ((((y<<4)^(y>>5)) + y) ^ key->xtea.B[r-2])) & 0xFFFFFFFFUL; |
|
110 y = (y - ((((z<<4)^(z>>5)) + z) ^ key->xtea.A[r-2])) & 0xFFFFFFFFUL; |
|
111 |
|
112 z = (z - ((((y<<4)^(y>>5)) + y) ^ key->xtea.B[r-3])) & 0xFFFFFFFFUL; |
|
113 y = (y - ((((z<<4)^(z>>5)) + z) ^ key->xtea.A[r-3])) & 0xFFFFFFFFUL; |
|
114 } |
|
115 STORE32L(y, &pt[0]); |
|
116 STORE32L(z, &pt[4]); |
|
117 } |
|
118 |
|
119 int xtea_test(void) |
|
120 { |
|
121 #ifndef LTC_TEST |
|
122 return CRYPT_NOP; |
|
123 #else |
|
124 static const unsigned char key[16] = |
|
125 { 0x78, 0x56, 0x34, 0x12, 0xf0, 0xcd, 0xcb, 0x9a, |
|
126 0x48, 0x37, 0x26, 0x15, 0xc0, 0xbf, 0xae, 0x9d }; |
|
127 static const unsigned char pt[8] = |
|
128 { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; |
|
129 static const unsigned char ct[8] = |
|
130 { 0x75, 0xd7, 0xc5, 0xbf, 0xcf, 0x58, 0xc9, 0x3f }; |
|
131 unsigned char tmp[2][8]; |
|
132 symmetric_key skey; |
|
133 int err, y; |
|
134 |
|
135 if ((err = xtea_setup(key, 16, 0, &skey)) != CRYPT_OK) { |
|
136 return err; |
|
137 } |
|
138 xtea_ecb_encrypt(pt, tmp[0], &skey); |
|
139 xtea_ecb_decrypt(tmp[0], tmp[1], &skey); |
|
140 |
|
141 if (memcmp(tmp[0], ct, 8) != 0 || memcmp(tmp[1], pt, 8) != 0) { |
|
142 return CRYPT_FAIL_TESTVECTOR; |
|
143 } |
|
144 |
|
145 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ |
|
146 for (y = 0; y < 8; y++) tmp[0][y] = 0; |
|
147 for (y = 0; y < 1000; y++) xtea_ecb_encrypt(tmp[0], tmp[0], &skey); |
|
148 for (y = 0; y < 1000; y++) xtea_ecb_decrypt(tmp[0], tmp[0], &skey); |
|
149 for (y = 0; y < 8; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR; |
|
150 |
|
151 return CRYPT_OK; |
|
152 #endif |
|
153 } |
|
154 |
|
155 int xtea_keysize(int *desired_keysize) |
|
156 { |
|
157 _ARGCHK(desired_keysize != NULL); |
|
158 if (*desired_keysize < 16) { |
|
159 return CRYPT_INVALID_KEYSIZE; |
|
160 } |
|
161 *desired_keysize = 16; |
|
162 return CRYPT_OK; |
|
163 } |
|
164 |
|
165 |
|
166 #endif |
|
167 |
|
168 |
|
169 |