Mercurial > dropbear
comparison src/ciphers/xtea.c @ 192:9cc34777b479 libtomcrypt
propagate from branch 'au.asn.ucc.matt.ltc-orig' (head 9ba8f01f44320e9cb9f19881105ae84f84a43ea9)
to branch 'au.asn.ucc.matt.dropbear.ltc' (head dbf51c569bc34956ad948e4cc87a0eeb2170b768)
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 08 May 2005 06:36:47 +0000 |
parents | 1c15b283127b |
children | 39d5d58461d6 |
comparison
equal
deleted
inserted
replaced
164:cd1143579f00 | 192:9cc34777b479 |
---|---|
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 /** | |
13 @file xtea.c | |
14 Implementation of XTEA, Tom St Denis | |
15 */ | |
16 #include "tomcrypt.h" | |
17 | |
18 #ifdef XTEA | |
19 | |
20 const struct ltc_cipher_descriptor xtea_desc = | |
21 { | |
22 "xtea", | |
23 1, | |
24 16, 16, 8, 32, | |
25 &xtea_setup, | |
26 &xtea_ecb_encrypt, | |
27 &xtea_ecb_decrypt, | |
28 &xtea_test, | |
29 &xtea_done, | |
30 &xtea_keysize, | |
31 NULL, NULL, NULL, NULL, NULL, NULL, NULL | |
32 }; | |
33 | |
34 int xtea_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) | |
35 { | |
36 unsigned long x, sum, K[4]; | |
37 | |
38 LTC_ARGCHK(key != NULL); | |
39 LTC_ARGCHK(skey != NULL); | |
40 | |
41 /* check arguments */ | |
42 if (keylen != 16) { | |
43 return CRYPT_INVALID_KEYSIZE; | |
44 } | |
45 | |
46 if (num_rounds != 0 && num_rounds != 32) { | |
47 return CRYPT_INVALID_ROUNDS; | |
48 } | |
49 | |
50 /* load key */ | |
51 LOAD32L(K[0], key+0); | |
52 LOAD32L(K[1], key+4); | |
53 LOAD32L(K[2], key+8); | |
54 LOAD32L(K[3], key+12); | |
55 | |
56 for (x = sum = 0; x < 32; x++) { | |
57 skey->xtea.A[x] = (sum + K[sum&3]) & 0xFFFFFFFFUL; | |
58 sum = (sum + 0x9E3779B9UL) & 0xFFFFFFFFUL; | |
59 skey->xtea.B[x] = (sum + K[(sum>>11)&3]) & 0xFFFFFFFFUL; | |
60 } | |
61 | |
62 #ifdef LTC_CLEAN_STACK | |
63 zeromem(&K, sizeof(K)); | |
64 #endif | |
65 | |
66 return CRYPT_OK; | |
67 } | |
68 | |
69 /** | |
70 Encrypts a block of text with XTEA | |
71 @param pt The input plaintext (8 bytes) | |
72 @param ct The output ciphertext (8 bytes) | |
73 @param skey The key as scheduled | |
74 */ | |
75 void xtea_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) | |
76 { | |
77 unsigned long y, z; | |
78 int r; | |
79 | |
80 LTC_ARGCHK(pt != NULL); | |
81 LTC_ARGCHK(ct != NULL); | |
82 LTC_ARGCHK(skey != NULL); | |
83 | |
84 LOAD32L(y, &pt[0]); | |
85 LOAD32L(z, &pt[4]); | |
86 for (r = 0; r < 32; r += 4) { | |
87 y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r])) & 0xFFFFFFFFUL; | |
88 z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r])) & 0xFFFFFFFFUL; | |
89 | |
90 y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r+1])) & 0xFFFFFFFFUL; | |
91 z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r+1])) & 0xFFFFFFFFUL; | |
92 | |
93 y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r+2])) & 0xFFFFFFFFUL; | |
94 z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r+2])) & 0xFFFFFFFFUL; | |
95 | |
96 y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r+3])) & 0xFFFFFFFFUL; | |
97 z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r+3])) & 0xFFFFFFFFUL; | |
98 } | |
99 STORE32L(y, &ct[0]); | |
100 STORE32L(z, &ct[4]); | |
101 } | |
102 | |
103 /** | |
104 Decrypts a block of text with XTEA | |
105 @param ct The input ciphertext (8 bytes) | |
106 @param pt The output plaintext (8 bytes) | |
107 @param skey The key as scheduled | |
108 */ | |
109 void xtea_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) | |
110 { | |
111 unsigned long y, z; | |
112 int r; | |
113 | |
114 LTC_ARGCHK(pt != NULL); | |
115 LTC_ARGCHK(ct != NULL); | |
116 LTC_ARGCHK(skey != NULL); | |
117 | |
118 LOAD32L(y, &ct[0]); | |
119 LOAD32L(z, &ct[4]); | |
120 for (r = 31; r >= 0; r -= 4) { | |
121 z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r])) & 0xFFFFFFFFUL; | |
122 y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r])) & 0xFFFFFFFFUL; | |
123 | |
124 z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-1])) & 0xFFFFFFFFUL; | |
125 y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-1])) & 0xFFFFFFFFUL; | |
126 | |
127 z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-2])) & 0xFFFFFFFFUL; | |
128 y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-2])) & 0xFFFFFFFFUL; | |
129 | |
130 z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-3])) & 0xFFFFFFFFUL; | |
131 y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-3])) & 0xFFFFFFFFUL; | |
132 } | |
133 STORE32L(y, &pt[0]); | |
134 STORE32L(z, &pt[4]); | |
135 } | |
136 | |
137 /** | |
138 Performs a self-test of the XTEA block cipher | |
139 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled | |
140 */ | |
141 int xtea_test(void) | |
142 { | |
143 #ifndef LTC_TEST | |
144 return CRYPT_NOP; | |
145 #else | |
146 static const unsigned char key[16] = | |
147 { 0x78, 0x56, 0x34, 0x12, 0xf0, 0xcd, 0xcb, 0x9a, | |
148 0x48, 0x37, 0x26, 0x15, 0xc0, 0xbf, 0xae, 0x9d }; | |
149 static const unsigned char pt[8] = | |
150 { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; | |
151 static const unsigned char ct[8] = | |
152 { 0x75, 0xd7, 0xc5, 0xbf, 0xcf, 0x58, 0xc9, 0x3f }; | |
153 unsigned char tmp[2][8]; | |
154 symmetric_key skey; | |
155 int err, y; | |
156 | |
157 if ((err = xtea_setup(key, 16, 0, &skey)) != CRYPT_OK) { | |
158 return err; | |
159 } | |
160 xtea_ecb_encrypt(pt, tmp[0], &skey); | |
161 xtea_ecb_decrypt(tmp[0], tmp[1], &skey); | |
162 | |
163 if (memcmp(tmp[0], ct, 8) != 0 || memcmp(tmp[1], pt, 8) != 0) { | |
164 return CRYPT_FAIL_TESTVECTOR; | |
165 } | |
166 | |
167 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ | |
168 for (y = 0; y < 8; y++) tmp[0][y] = 0; | |
169 for (y = 0; y < 1000; y++) xtea_ecb_encrypt(tmp[0], tmp[0], &skey); | |
170 for (y = 0; y < 1000; y++) xtea_ecb_decrypt(tmp[0], tmp[0], &skey); | |
171 for (y = 0; y < 8; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR; | |
172 | |
173 return CRYPT_OK; | |
174 #endif | |
175 } | |
176 | |
177 /** Terminate the context | |
178 @param skey The scheduled key | |
179 */ | |
180 void xtea_done(symmetric_key *skey) | |
181 { | |
182 } | |
183 | |
184 /** | |
185 Gets suitable key size | |
186 @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable. | |
187 @return CRYPT_OK if the input key size is acceptable. | |
188 */ | |
189 int xtea_keysize(int *keysize) | |
190 { | |
191 LTC_ARGCHK(keysize != NULL); | |
192 if (*keysize < 16) { | |
193 return CRYPT_INVALID_KEYSIZE; | |
194 } | |
195 *keysize = 16; | |
196 return CRYPT_OK; | |
197 } | |
198 | |
199 | |
200 #endif | |
201 | |
202 | |
203 |