1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
12 Implementation of LTC_XTEA, Tom St Denis
18 const struct ltc_cipher_descriptor xtea_desc =
29 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
32 int xtea_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
36 LTC_ARGCHK(key != NULL);
37 LTC_ARGCHK(skey != NULL);
41 return CRYPT_INVALID_KEYSIZE;
44 if (num_rounds != 0 && num_rounds != 32) {
45 return CRYPT_INVALID_ROUNDS;
52 LOAD32H(K[3], key+12);
54 for (x = sum = 0; x < 32; x++) {
55 skey->xtea.A[x] = (sum + K[sum&3]) & 0xFFFFFFFFUL;
56 sum = (sum + 0x9E3779B9UL) & 0xFFFFFFFFUL;
57 skey->xtea.B[x] = (sum + K[(sum>>11)&3]) & 0xFFFFFFFFUL;
60 #ifdef LTC_CLEAN_STACK
61 zeromem(&K, sizeof(K));
68 Encrypts a block of text with LTC_XTEA
69 @param pt The input plaintext (8 bytes)
70 @param ct The output ciphertext (8 bytes)
71 @param skey The key as scheduled
72 @return CRYPT_OK if successful
74 int xtea_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
79 LTC_ARGCHK(pt != NULL);
80 LTC_ARGCHK(ct != NULL);
81 LTC_ARGCHK(skey != NULL);
85 for (r = 0; r < 32; r += 4) {
86 y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r])) & 0xFFFFFFFFUL;
87 z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r])) & 0xFFFFFFFFUL;
89 y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r+1])) & 0xFFFFFFFFUL;
90 z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r+1])) & 0xFFFFFFFFUL;
92 y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r+2])) & 0xFFFFFFFFUL;
93 z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r+2])) & 0xFFFFFFFFUL;
95 y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r+3])) & 0xFFFFFFFFUL;
96 z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r+3])) & 0xFFFFFFFFUL;
104 Decrypts a block of text with LTC_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 @return CRYPT_OK if successful
110 int xtea_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
115 LTC_ARGCHK(pt != NULL);
116 LTC_ARGCHK(ct != NULL);
117 LTC_ARGCHK(skey != NULL);
121 for (r = 31; r >= 0; r -= 4) {
122 z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r])) & 0xFFFFFFFFUL;
123 y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r])) & 0xFFFFFFFFUL;
125 z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-1])) & 0xFFFFFFFFUL;
126 y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-1])) & 0xFFFFFFFFUL;
128 z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-2])) & 0xFFFFFFFFUL;
129 y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-2])) & 0xFFFFFFFFUL;
131 z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-3])) & 0xFFFFFFFFUL;
132 y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-3])) & 0xFFFFFFFFUL;
140 Performs a self-test of the LTC_XTEA block cipher
141 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled
148 static const struct {
149 unsigned char key[16], pt[8], ct[8];
152 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
154 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
155 { 0xde, 0xe9, 0xd4, 0xd8, 0xf7, 0x13, 0x1e, 0xd9 }
157 { 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
158 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04 },
159 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
160 { 0xa5, 0x97, 0xab, 0x41, 0x76, 0x01, 0x4d, 0x72 }
162 { 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04,
163 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06 },
164 { 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02 },
165 { 0xb1, 0xfd, 0x5d, 0xa9, 0xcc, 0x6d, 0xc9, 0xdc }
167 { 0x78, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f,
168 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87 },
169 { 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87 },
170 { 0x70, 0x4b, 0x31, 0x34, 0x47, 0x44, 0xdf, 0xab }
172 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
173 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
174 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
175 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 }
177 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
178 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
179 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
180 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 }
182 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
183 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
184 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
185 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
187 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
188 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
189 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
190 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 }
192 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
193 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
194 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
195 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d }
197 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
198 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
199 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 },
200 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
203 unsigned char tmp[2][8];
206 for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
207 zeromem(&skey, sizeof(skey));
208 if ((err = xtea_setup(tests[i].key, 16, 0, &skey)) != CRYPT_OK) {
211 xtea_ecb_encrypt(tests[i].pt, tmp[0], &skey);
212 xtea_ecb_decrypt(tmp[0], tmp[1], &skey);
214 if (compare_testvector(tmp[0], 8, tests[i].ct, 8, "XTEA Encrypt", i) != 0 ||
215 compare_testvector(tmp[1], 8, tests[i].pt, 8, "XTEA Decrypt", i) != 0) {
216 return CRYPT_FAIL_TESTVECTOR;
219 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
220 for (y = 0; y < 8; y++) tmp[0][y] = 0;
221 for (y = 0; y < 1000; y++) xtea_ecb_encrypt(tmp[0], tmp[0], &skey);
222 for (y = 0; y < 1000; y++) xtea_ecb_decrypt(tmp[0], tmp[0], &skey);
223 for (y = 0; y < 8; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
230 /** Terminate the context
231 @param skey The scheduled key
233 void xtea_done(symmetric_key *skey)
235 LTC_UNUSED_PARAM(skey);
239 Gets suitable key size
240 @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable.
241 @return CRYPT_OK if the input key size is acceptable.
243 int xtea_keysize(int *keysize)
245 LTC_ARGCHK(keysize != NULL);
247 return CRYPT_INVALID_KEYSIZE;
259 /* ref: $Format:%D$ */
260 /* git commit: $Format:%H$ */
261 /* commit time: $Format:%ai$ */