blob: cea9ff82f49749bdbfd243e04664e53243ec6a1a [file] [log] [blame]
Paul Bakker7a7c78f2009-01-04 18:15:48 +00001/*
2 * An 32-bit implementation of the XTEA algorithm
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker7a7c78f2009-01-04 18:15:48 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7a7c78f2009-01-04 18:15:48 +000024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker7a7c78f2009-01-04 18:15:48 +000028
29#if defined(POLARSSL_XTEA_C)
30
31#include "polarssl/xtea.h"
32
Paul Bakker7dc4c442014-02-01 22:50:26 +010033#if defined(POLARSSL_PLATFORM_C)
34#include "polarssl/platform.h"
35#else
36#define polarssl_printf printf
37#endif
38
Paul Bakker90995b52013-06-24 19:20:35 +020039#if !defined(POLARSSL_XTEA_ALT)
40
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020041/* Implementation that should never be optimized out by the compiler */
42static void polarssl_zeroize( void *v, size_t n ) {
43 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
44}
45
Paul Bakker7a7c78f2009-01-04 18:15:48 +000046/*
47 * 32-bit integer manipulation macros (big endian)
48 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000049#ifndef GET_UINT32_BE
50#define GET_UINT32_BE(n,b,i) \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000051{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000052 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
53 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
54 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
55 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000056}
57#endif
58
Paul Bakker5c2364c2012-10-01 14:41:15 +000059#ifndef PUT_UINT32_BE
60#define PUT_UINT32_BE(n,b,i) \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000061{ \
62 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
63 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
64 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
65 (b)[(i) + 3] = (unsigned char) ( (n) ); \
66}
67#endif
68
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020069void xtea_init( xtea_context *ctx )
70{
71 memset( ctx, 0, sizeof( xtea_context ) );
72}
73
74void xtea_free( xtea_context *ctx )
75{
76 if( ctx == NULL )
77 return;
78
79 polarssl_zeroize( ctx, sizeof( xtea_context ) );
80}
81
Paul Bakker7a7c78f2009-01-04 18:15:48 +000082/*
83 * XTEA key schedule
84 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020085void xtea_setup( xtea_context *ctx, const unsigned char key[16] )
Paul Bakker7a7c78f2009-01-04 18:15:48 +000086{
87 int i;
88
Paul Bakker66d5d072014-06-17 16:39:18 +020089 memset( ctx, 0, sizeof(xtea_context) );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000090
91 for( i = 0; i < 4; i++ )
92 {
Paul Bakker5c2364c2012-10-01 14:41:15 +000093 GET_UINT32_BE( ctx->k[i], key, i << 2 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000094 }
95}
96
97/*
98 * XTEA encrypt function
99 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200100int xtea_crypt_ecb( xtea_context *ctx, int mode,
101 const unsigned char input[8], unsigned char output[8])
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000102{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +0000103 uint32_t *k, v0, v1, i;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000104
105 k = ctx->k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200106
Paul Bakker5c2364c2012-10-01 14:41:15 +0000107 GET_UINT32_BE( v0, input, 0 );
108 GET_UINT32_BE( v1, input, 4 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000109
110 if( mode == XTEA_ENCRYPT )
111 {
Paul Bakker23986e52011-04-24 08:57:21 +0000112 uint32_t sum = 0, delta = 0x9E3779B9;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000113
Paul Bakker23986e52011-04-24 08:57:21 +0000114 for( i = 0; i < 32; i++ )
115 {
116 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
117 sum += delta;
118 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
119 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000120 }
121 else /* XTEA_DECRYPT */
122 {
Paul Bakker23986e52011-04-24 08:57:21 +0000123 uint32_t delta = 0x9E3779B9, sum = delta * 32;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000124
Paul Bakker23986e52011-04-24 08:57:21 +0000125 for( i = 0; i < 32; i++ )
126 {
127 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
128 sum -= delta;
129 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
130 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000131 }
132
Paul Bakker5c2364c2012-10-01 14:41:15 +0000133 PUT_UINT32_BE( v0, output, 0 );
134 PUT_UINT32_BE( v1, output, 4 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000135
136 return( 0 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000137}
138
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200139#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000140/*
141 * XTEA-CBC buffer encryption/decryption
142 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200143int xtea_crypt_cbc( xtea_context *ctx, int mode, size_t length,
144 unsigned char iv[8], const unsigned char *input,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000145 unsigned char *output)
146{
147 int i;
148 unsigned char temp[8];
149
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200150 if( length % 8 )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000151 return( POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH );
152
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200153 if( mode == XTEA_DECRYPT )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000154 {
155 while( length > 0 )
156 {
157 memcpy( temp, input, 8 );
158 xtea_crypt_ecb( ctx, mode, input, output );
159
Paul Bakker66d5d072014-06-17 16:39:18 +0200160 for( i = 0; i < 8; i++ )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000161 output[i] = (unsigned char)( output[i] ^ iv[i] );
162
163 memcpy( iv, temp, 8 );
164
165 input += 8;
166 output += 8;
167 length -= 8;
168 }
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200169 }
170 else
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000171 {
172 while( length > 0 )
173 {
174 for( i = 0; i < 8; i++ )
175 output[i] = (unsigned char)( input[i] ^ iv[i] );
176
177 xtea_crypt_ecb( ctx, mode, output, output );
178 memcpy( iv, output, 8 );
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200179
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000180 input += 8;
181 output += 8;
182 length -= 8;
183 }
184 }
185
186 return( 0 );
187}
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200188#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker90995b52013-06-24 19:20:35 +0200189#endif /* !POLARSSL_XTEA_ALT */
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000190
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000191#if defined(POLARSSL_SELF_TEST)
192
193#include <string.h>
194#include <stdio.h>
195
196/*
197 * XTEA tests vectors (non-official)
198 */
199
200static const unsigned char xtea_test_key[6][16] =
201{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +0000202 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
203 0x0c, 0x0d, 0x0e, 0x0f },
204 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
205 0x0c, 0x0d, 0x0e, 0x0f },
206 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
207 0x0c, 0x0d, 0x0e, 0x0f },
208 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
209 0x00, 0x00, 0x00, 0x00 },
210 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
211 0x00, 0x00, 0x00, 0x00 },
212 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
213 0x00, 0x00, 0x00, 0x00 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000214};
215
216static const unsigned char xtea_test_pt[6][8] =
217{
218 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
219 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
220 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
221 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
222 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
223 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
224};
225
226static const unsigned char xtea_test_ct[6][8] =
227{
228 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
229 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
230 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
231 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
232 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
233 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
234};
235
236/*
237 * Checkup routine
238 */
239int xtea_self_test( int verbose )
240{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200241 int i, ret = 0;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000242 unsigned char buf[8];
243 xtea_context ctx;
244
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200245 xtea_init( &ctx );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000246 for( i = 0; i < 6; i++ )
247 {
248 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100249 polarssl_printf( " XTEA test #%d: ", i + 1 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000250
251 memcpy( buf, xtea_test_pt[i], 8 );
252
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200253 xtea_setup( &ctx, xtea_test_key[i] );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000254 xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, buf, buf );
255
256 if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
257 {
258 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100259 polarssl_printf( "failed\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000260
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200261 ret = 1;
262 goto exit;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000263 }
264
265 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100266 polarssl_printf( "passed\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000267 }
268
269 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100270 polarssl_printf( "\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000271
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200272exit:
273 xtea_free( &ctx );
274
275 return( ret );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000276}
277
Paul Bakker9af723c2014-05-01 13:03:14 +0200278#endif /* POLARSSL_SELF_TEST */
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000279
Paul Bakker9af723c2014-05-01 13:03:14 +0200280#endif /* POLARSSL_XTEA_C */