1 // SPDX-License-Identifier: BSD-2-Clause 2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis 3 * 4 * LibTomCrypt is a library that provides various cryptographic 5 * algorithms in a highly modular and flexible manner. 6 * 7 * The library is free for all purposes without any express 8 * guarantee it works. 9 */ 10 11 /** 12 @file xtea.c 13 Implementation of LTC_XTEA, Tom St Denis 14 */ 15 #include "tomcrypt_private.h" 16 17 #ifdef LTC_XTEA 18 19 const struct ltc_cipher_descriptor xtea_desc = 20 { 21 "xtea", 22 1, 23 16, 16, 8, 32, 24 &xtea_setup, 25 &xtea_ecb_encrypt, 26 &xtea_ecb_decrypt, 27 &xtea_test, 28 &xtea_done, 29 &xtea_keysize, 30 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 31 }; 32 33 int xtea_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) 34 { 35 ulong32 x, sum, K[4]; 36 37 LTC_ARGCHK(key != NULL); 38 LTC_ARGCHK(skey != NULL); 39 40 /* check arguments */ 41 if (keylen != 16) { 42 return CRYPT_INVALID_KEYSIZE; 43 } 44 45 if (num_rounds != 0 && num_rounds != 32) { 46 return CRYPT_INVALID_ROUNDS; 47 } 48 49 /* load key */ 50 LOAD32H(K[0], key+0); 51 LOAD32H(K[1], key+4); 52 LOAD32H(K[2], key+8); 53 LOAD32H(K[3], key+12); 54 55 for (x = sum = 0; x < 32; x++) { 56 skey->xtea.A[x] = (sum + K[sum&3]) & 0xFFFFFFFFUL; 57 sum = (sum + 0x9E3779B9UL) & 0xFFFFFFFFUL; 58 skey->xtea.B[x] = (sum + K[(sum>>11)&3]) & 0xFFFFFFFFUL; 59 } 60 61 #ifdef LTC_CLEAN_STACK 62 zeromem(&K, sizeof(K)); 63 #endif 64 65 return CRYPT_OK; 66 } 67 68 /** 69 Encrypts a block of text with LTC_XTEA 70 @param pt The input plaintext (8 bytes) 71 @param ct The output ciphertext (8 bytes) 72 @param skey The key as scheduled 73 @return CRYPT_OK if successful 74 */ 75 int xtea_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey) 76 { 77 ulong32 y, z; 78 int r; 79 80 LTC_ARGCHK(pt != NULL); 81 LTC_ARGCHK(ct != NULL); 82 LTC_ARGCHK(skey != NULL); 83 84 LOAD32H(y, &pt[0]); 85 LOAD32H(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 STORE32H(y, &ct[0]); 100 STORE32H(z, &ct[4]); 101 return CRYPT_OK; 102 } 103 104 /** 105 Decrypts a block of text with LTC_XTEA 106 @param ct The input ciphertext (8 bytes) 107 @param pt The output plaintext (8 bytes) 108 @param skey The key as scheduled 109 @return CRYPT_OK if successful 110 */ 111 int xtea_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey) 112 { 113 ulong32 y, z; 114 int r; 115 116 LTC_ARGCHK(pt != NULL); 117 LTC_ARGCHK(ct != NULL); 118 LTC_ARGCHK(skey != NULL); 119 120 LOAD32H(y, &ct[0]); 121 LOAD32H(z, &ct[4]); 122 for (r = 31; r >= 0; r -= 4) { 123 z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r])) & 0xFFFFFFFFUL; 124 y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r])) & 0xFFFFFFFFUL; 125 126 z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-1])) & 0xFFFFFFFFUL; 127 y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-1])) & 0xFFFFFFFFUL; 128 129 z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-2])) & 0xFFFFFFFFUL; 130 y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-2])) & 0xFFFFFFFFUL; 131 132 z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-3])) & 0xFFFFFFFFUL; 133 y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-3])) & 0xFFFFFFFFUL; 134 } 135 STORE32H(y, &pt[0]); 136 STORE32H(z, &pt[4]); 137 return CRYPT_OK; 138 } 139 140 /** 141 Performs a self-test of the LTC_XTEA block cipher 142 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled 143 */ 144 int xtea_test(void) 145 { 146 #ifndef LTC_TEST 147 return CRYPT_NOP; 148 #else 149 static const struct { 150 unsigned char key[16], pt[8], ct[8]; 151 } tests[] = { 152 { 153 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 154 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 155 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 156 { 0xde, 0xe9, 0xd4, 0xd8, 0xf7, 0x13, 0x1e, 0xd9 } 157 }, { 158 { 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 159 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04 }, 160 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 161 { 0xa5, 0x97, 0xab, 0x41, 0x76, 0x01, 0x4d, 0x72 } 162 }, { 163 { 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 164 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06 }, 165 { 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02 }, 166 { 0xb1, 0xfd, 0x5d, 0xa9, 0xcc, 0x6d, 0xc9, 0xdc } 167 }, { 168 { 0x78, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f, 169 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87 }, 170 { 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87 }, 171 { 0x70, 0x4b, 0x31, 0x34, 0x47, 0x44, 0xdf, 0xab } 172 }, { 173 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 174 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, 175 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, 176 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 } 177 }, { 178 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 179 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, 180 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, 181 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 } 182 }, { 183 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 184 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, 185 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f }, 186 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 } 187 }, { 188 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 189 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 190 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, 191 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 } 192 }, { 193 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 194 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 195 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, 196 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d } 197 }, { 198 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 199 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 200 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }, 201 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 } 202 } 203 }; 204 unsigned char tmp[2][8]; 205 symmetric_key skey; 206 int i, err, y; 207 for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { 208 zeromem(&skey, sizeof(skey)); 209 if ((err = xtea_setup(tests[i].key, 16, 0, &skey)) != CRYPT_OK) { 210 return err; 211 } 212 xtea_ecb_encrypt(tests[i].pt, tmp[0], &skey); 213 xtea_ecb_decrypt(tmp[0], tmp[1], &skey); 214 215 if (compare_testvector(tmp[0], 8, tests[i].ct, 8, "XTEA Encrypt", i) != 0 || 216 compare_testvector(tmp[1], 8, tests[i].pt, 8, "XTEA Decrypt", i) != 0) { 217 return CRYPT_FAIL_TESTVECTOR; 218 } 219 220 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ 221 for (y = 0; y < 8; y++) tmp[0][y] = 0; 222 for (y = 0; y < 1000; y++) xtea_ecb_encrypt(tmp[0], tmp[0], &skey); 223 for (y = 0; y < 1000; y++) xtea_ecb_decrypt(tmp[0], tmp[0], &skey); 224 for (y = 0; y < 8; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR; 225 } /* for */ 226 227 return CRYPT_OK; 228 #endif 229 } 230 231 /** Terminate the context 232 @param skey The scheduled key 233 */ 234 void xtea_done(symmetric_key *skey) 235 { 236 LTC_UNUSED_PARAM(skey); 237 } 238 239 /** 240 Gets suitable key size 241 @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable. 242 @return CRYPT_OK if the input key size is acceptable. 243 */ 244 int xtea_keysize(int *keysize) 245 { 246 LTC_ARGCHK(keysize != NULL); 247 if (*keysize < 16) { 248 return CRYPT_INVALID_KEYSIZE; 249 } 250 *keysize = 16; 251 return CRYPT_OK; 252 } 253 254 255 #endif 256 257 258 259 260 /* ref: $Format:%D$ */ 261 /* git commit: $Format:%H$ */ 262 /* commit time: $Format:%ai$ */ 263