blob: 75215c50af8add7da27582b56d8bab77116a0cca [file] [log] [blame]
Paul Bakker7a7c78f2009-01-04 18:15:48 +00001/*
2 * An 32-bit implementation of the XTEA algorithm
3 *
Paul Bakker9af723c2014-05-01 13:03:14 +02004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakker7a7c78f2009-01-04 18:15:48 +000010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7a7c78f2009-01-04 18:15:48 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker7a7c78f2009-01-04 18:15:48 +000031
32#if defined(POLARSSL_XTEA_C)
33
34#include "polarssl/xtea.h"
35
Paul Bakker7dc4c442014-02-01 22:50:26 +010036#if defined(POLARSSL_PLATFORM_C)
37#include "polarssl/platform.h"
38#else
39#define polarssl_printf printf
40#endif
41
Paul Bakker90995b52013-06-24 19:20:35 +020042#if !defined(POLARSSL_XTEA_ALT)
43
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020044/* Implementation that should never be optimized out by the compiler */
45static void polarssl_zeroize( void *v, size_t n ) {
46 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
47}
48
Paul Bakker7a7c78f2009-01-04 18:15:48 +000049/*
50 * 32-bit integer manipulation macros (big endian)
51 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000052#ifndef GET_UINT32_BE
53#define GET_UINT32_BE(n,b,i) \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000054{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000055 (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] ); \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000059}
60#endif
61
Paul Bakker5c2364c2012-10-01 14:41:15 +000062#ifndef PUT_UINT32_BE
63#define PUT_UINT32_BE(n,b,i) \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000064{ \
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) ); \
69}
70#endif
71
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020072void xtea_init( xtea_context *ctx )
73{
74 memset( ctx, 0, sizeof( xtea_context ) );
75}
76
77void xtea_free( xtea_context *ctx )
78{
79 if( ctx == NULL )
80 return;
81
82 polarssl_zeroize( ctx, sizeof( xtea_context ) );
83}
84
Paul Bakker7a7c78f2009-01-04 18:15:48 +000085/*
86 * XTEA key schedule
87 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020088void xtea_setup( xtea_context *ctx, const unsigned char key[16] )
Paul Bakker7a7c78f2009-01-04 18:15:48 +000089{
90 int i;
91
Paul Bakker66d5d072014-06-17 16:39:18 +020092 memset( ctx, 0, sizeof(xtea_context) );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000093
94 for( i = 0; i < 4; i++ )
95 {
Paul Bakker5c2364c2012-10-01 14:41:15 +000096 GET_UINT32_BE( ctx->k[i], key, i << 2 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000097 }
98}
99
100/*
101 * XTEA encrypt function
102 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200103int xtea_crypt_ecb( xtea_context *ctx, int mode,
104 const unsigned char input[8], unsigned char output[8])
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000105{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +0000106 uint32_t *k, v0, v1, i;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000107
108 k = ctx->k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200109
Paul Bakker5c2364c2012-10-01 14:41:15 +0000110 GET_UINT32_BE( v0, input, 0 );
111 GET_UINT32_BE( v1, input, 4 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000112
113 if( mode == XTEA_ENCRYPT )
114 {
Paul Bakker23986e52011-04-24 08:57:21 +0000115 uint32_t sum = 0, delta = 0x9E3779B9;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000116
Paul Bakker23986e52011-04-24 08:57:21 +0000117 for( i = 0; i < 32; i++ )
118 {
119 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
120 sum += delta;
121 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
122 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000123 }
124 else /* XTEA_DECRYPT */
125 {
Paul Bakker23986e52011-04-24 08:57:21 +0000126 uint32_t delta = 0x9E3779B9, sum = delta * 32;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000127
Paul Bakker23986e52011-04-24 08:57:21 +0000128 for( i = 0; i < 32; i++ )
129 {
130 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
131 sum -= delta;
132 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
133 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000134 }
135
Paul Bakker5c2364c2012-10-01 14:41:15 +0000136 PUT_UINT32_BE( v0, output, 0 );
137 PUT_UINT32_BE( v1, output, 4 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000138
139 return( 0 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000140}
141
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200142#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000143/*
144 * XTEA-CBC buffer encryption/decryption
145 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200146int xtea_crypt_cbc( xtea_context *ctx, int mode, size_t length,
147 unsigned char iv[8], const unsigned char *input,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000148 unsigned char *output)
149{
150 int i;
151 unsigned char temp[8];
152
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200153 if( length % 8 )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000154 return( POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH );
155
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200156 if( mode == XTEA_DECRYPT )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000157 {
158 while( length > 0 )
159 {
160 memcpy( temp, input, 8 );
161 xtea_crypt_ecb( ctx, mode, input, output );
162
Paul Bakker66d5d072014-06-17 16:39:18 +0200163 for( i = 0; i < 8; i++ )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000164 output[i] = (unsigned char)( output[i] ^ iv[i] );
165
166 memcpy( iv, temp, 8 );
167
168 input += 8;
169 output += 8;
170 length -= 8;
171 }
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200172 }
173 else
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000174 {
175 while( length > 0 )
176 {
177 for( i = 0; i < 8; i++ )
178 output[i] = (unsigned char)( input[i] ^ iv[i] );
179
180 xtea_crypt_ecb( ctx, mode, output, output );
181 memcpy( iv, output, 8 );
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200182
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000183 input += 8;
184 output += 8;
185 length -= 8;
186 }
187 }
188
189 return( 0 );
190}
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200191#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker90995b52013-06-24 19:20:35 +0200192#endif /* !POLARSSL_XTEA_ALT */
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000193
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000194#if defined(POLARSSL_SELF_TEST)
195
196#include <string.h>
197#include <stdio.h>
198
199/*
200 * XTEA tests vectors (non-official)
201 */
202
203static const unsigned char xtea_test_key[6][16] =
204{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +0000205 { 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 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000217};
218
219static const unsigned char xtea_test_pt[6][8] =
220{
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 }
227};
228
229static const unsigned char xtea_test_ct[6][8] =
230{
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 }
237};
238
239/*
240 * Checkup routine
241 */
242int xtea_self_test( int verbose )
243{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200244 int i, ret = 0;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000245 unsigned char buf[8];
246 xtea_context ctx;
247
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200248 xtea_init( &ctx );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000249 for( i = 0; i < 6; i++ )
250 {
251 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100252 polarssl_printf( " XTEA test #%d: ", i + 1 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000253
254 memcpy( buf, xtea_test_pt[i], 8 );
255
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200256 xtea_setup( &ctx, xtea_test_key[i] );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000257 xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, buf, buf );
258
259 if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
260 {
261 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100262 polarssl_printf( "failed\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000263
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200264 ret = 1;
265 goto exit;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000266 }
267
268 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100269 polarssl_printf( "passed\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000270 }
271
272 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100273 polarssl_printf( "\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000274
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200275exit:
276 xtea_free( &ctx );
277
278 return( ret );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000279}
280
Paul Bakker9af723c2014-05-01 13:03:14 +0200281#endif /* POLARSSL_SELF_TEST */
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000282
Paul Bakker9af723c2014-05-01 13:03:14 +0200283#endif /* POLARSSL_XTEA_C */