26 #if !defined(POLARSSL_CONFIG_FILE) 29 #include POLARSSL_CONFIG_FILE 32 #if defined(POLARSSL_XTEA_C) 36 #if defined(POLARSSL_PLATFORM_C) 39 #define polarssl_printf printf 42 #if !defined(POLARSSL_XTEA_ALT) 45 static void polarssl_zeroize(
void *v,
size_t n ) {
46 volatile unsigned char *p = v;
while( n-- ) *p++ = 0;
53 #define GET_UINT32_BE(n,b,i) \ 55 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ 56 | ( (uint32_t) (b)[(i) + 1] << 16 ) \ 57 | ( (uint32_t) (b)[(i) + 2] << 8 ) \ 58 | ( (uint32_t) (b)[(i) + 3] ); \ 63 #define PUT_UINT32_BE(n,b,i) \ 65 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ 66 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ 67 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ 68 (b)[(i) + 3] = (unsigned char) ( (n) ); \ 94 for( i = 0; i < 4; i++ )
104 const unsigned char input[8],
unsigned char output[8])
106 uint32_t *k, v0, v1, i;
115 uint32_t sum = 0, delta = 0x9E3779B9;
117 for( i = 0; i < 32; i++ )
119 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
121 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
126 uint32_t delta = 0x9E3779B9, sum = delta * 32;
128 for( i = 0; i < 32; i++ )
130 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
132 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
142 #if defined(POLARSSL_CIPHER_MODE_CBC) 147 unsigned char iv[8],
const unsigned char *input,
148 unsigned char *output)
151 unsigned char temp[8];
160 memcpy( temp, input, 8 );
163 for( i = 0; i < 8; i++ )
164 output[i] = (
unsigned char)( output[i] ^ iv[i] );
166 memcpy( iv, temp, 8 );
177 for( i = 0; i < 8; i++ )
178 output[i] = (
unsigned char)( input[i] ^ iv[i] );
181 memcpy( iv, output, 8 );
194 #if defined(POLARSSL_SELF_TEST) 203 static const unsigned char xtea_test_key[6][16] =
205 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
206 0x0c, 0x0d, 0x0e, 0x0f },
207 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
208 0x0c, 0x0d, 0x0e, 0x0f },
209 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
210 0x0c, 0x0d, 0x0e, 0x0f },
211 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
212 0x00, 0x00, 0x00, 0x00 },
213 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
214 0x00, 0x00, 0x00, 0x00 },
215 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
216 0x00, 0x00, 0x00, 0x00 }
219 static const unsigned char xtea_test_pt[6][8] =
221 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
222 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
223 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
224 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
225 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
226 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
229 static const unsigned char xtea_test_ct[6][8] =
231 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
232 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
233 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
234 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
235 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
236 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
245 unsigned char buf[8];
249 for( i = 0; i < 6; i++ )
254 memcpy( buf, xtea_test_pt[i], 8 );
259 if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
#define POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH
The data input has an invalid length.
#define PUT_UINT32_BE(n, b, i)
void xtea_free(xtea_context *ctx)
Clear XTEA context.
Configuration options (set of defines)
void xtea_init(xtea_context *ctx)
Initialize XTEA context.
int xtea_crypt_ecb(xtea_context *ctx, int mode, const unsigned char input[8], unsigned char output[8])
XTEA cipher function.
void xtea_setup(xtea_context *ctx, const unsigned char key[16])
XTEA key schedule.
XTEA block cipher (32-bit)
#define GET_UINT32_BE(n, b, i)
int xtea_crypt_cbc(xtea_context *ctx, int mode, size_t length, unsigned char iv[8], const unsigned char *input, unsigned char *output)
XTEA CBC cipher function.
int xtea_self_test(int verbose)
Checkup routine.