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)
48 #define GET_UINT32_BE(n,b,i) \
50 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
51 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
52 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
53 | ( (uint32_t) (b)[(i) + 3] ); \
58 #define PUT_UINT32_BE(n,b,i) \
60 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
61 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
62 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
63 (b)[(i) + 3] = (unsigned char) ( (n) ); \
76 for( i = 0; i < 4; i++ )
78 GET_UINT32_BE( ctx->
k[i], key, i << 2 );
86 const unsigned char input[8],
unsigned char output[8])
88 uint32_t *k, v0, v1, i;
92 GET_UINT32_BE( v0, input, 0 );
93 GET_UINT32_BE( v1, input, 4 );
97 uint32_t sum = 0, delta = 0x9E3779B9;
99 for( i = 0; i < 32; i++ )
101 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
103 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
108 uint32_t delta = 0x9E3779B9, sum = delta * 32;
110 for( i = 0; i < 32; i++ )
112 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
114 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
118 PUT_UINT32_BE( v0, output, 0 );
119 PUT_UINT32_BE( v1, output, 4 );
124 #if defined(POLARSSL_CIPHER_MODE_CBC)
129 unsigned char iv[8],
const unsigned char *input,
130 unsigned char *output)
133 unsigned char temp[8];
142 memcpy( temp, input, 8 );
145 for(i = 0; i < 8; i++)
146 output[i] = (
unsigned char)( output[i] ^ iv[i] );
148 memcpy( iv, temp, 8 );
159 for( i = 0; i < 8; i++ )
160 output[i] = (
unsigned char)( input[i] ^ iv[i] );
163 memcpy( iv, output, 8 );
176 #if defined(POLARSSL_SELF_TEST)
185 static const unsigned char xtea_test_key[6][16] =
187 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
188 0x0c, 0x0d, 0x0e, 0x0f },
189 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
190 0x0c, 0x0d, 0x0e, 0x0f },
191 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
192 0x0c, 0x0d, 0x0e, 0x0f },
193 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
194 0x00, 0x00, 0x00, 0x00 },
195 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
196 0x00, 0x00, 0x00, 0x00 },
197 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
198 0x00, 0x00, 0x00, 0x00 }
201 static const unsigned char xtea_test_pt[6][8] =
203 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
204 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
205 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
206 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
207 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
208 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
211 static const unsigned char xtea_test_ct[6][8] =
213 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
214 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
215 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
216 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
217 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
218 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
227 unsigned char buf[8];
230 for( i = 0; i < 6; i++ )
235 memcpy( buf, xtea_test_pt[i], 8 );
240 if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
#define POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH
The data input has an invalid length.
Configuration options (set of defines)
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)
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.