blob: 1bb6f29dd5a4045e37a063f856b38c64c6f9042b [file] [log] [blame]
Paul Bakker7a7c78f2009-01-04 18:15:48 +00001/*
2 * An 32-bit implementation of the XTEA algorithm
3 *
Paul Bakker90995b52013-06-24 19:20:35 +02004 * Copyright (C) 2006-2013, 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
26#include "polarssl/config.h"
27
28#if defined(POLARSSL_XTEA_C)
29
30#include "polarssl/xtea.h"
31
Paul Bakker7dc4c442014-02-01 22:50:26 +010032#if defined(POLARSSL_PLATFORM_C)
33#include "polarssl/platform.h"
34#else
35#define polarssl_printf printf
36#endif
37
Paul Bakker90995b52013-06-24 19:20:35 +020038#if !defined(POLARSSL_XTEA_ALT)
39
Paul Bakker7a7c78f2009-01-04 18:15:48 +000040/*
41 * 32-bit integer manipulation macros (big endian)
42 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000043#ifndef GET_UINT32_BE
44#define GET_UINT32_BE(n,b,i) \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000045{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000046 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
47 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
48 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
49 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000050}
51#endif
52
Paul Bakker5c2364c2012-10-01 14:41:15 +000053#ifndef PUT_UINT32_BE
54#define PUT_UINT32_BE(n,b,i) \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000055{ \
56 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
57 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
58 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
59 (b)[(i) + 3] = (unsigned char) ( (n) ); \
60}
61#endif
62
63/*
64 * XTEA key schedule
65 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020066void xtea_setup( xtea_context *ctx, const unsigned char key[16] )
Paul Bakker7a7c78f2009-01-04 18:15:48 +000067{
68 int i;
69
70 memset(ctx, 0, sizeof(xtea_context));
71
72 for( i = 0; i < 4; i++ )
73 {
Paul Bakker5c2364c2012-10-01 14:41:15 +000074 GET_UINT32_BE( ctx->k[i], key, i << 2 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000075 }
76}
77
78/*
79 * XTEA encrypt function
80 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020081int xtea_crypt_ecb( xtea_context *ctx, int mode,
82 const unsigned char input[8], unsigned char output[8])
Paul Bakker7a7c78f2009-01-04 18:15:48 +000083{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +000084 uint32_t *k, v0, v1, i;
Paul Bakker7a7c78f2009-01-04 18:15:48 +000085
86 k = ctx->k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020087
Paul Bakker5c2364c2012-10-01 14:41:15 +000088 GET_UINT32_BE( v0, input, 0 );
89 GET_UINT32_BE( v1, input, 4 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000090
91 if( mode == XTEA_ENCRYPT )
92 {
Paul Bakker23986e52011-04-24 08:57:21 +000093 uint32_t sum = 0, delta = 0x9E3779B9;
Paul Bakker7a7c78f2009-01-04 18:15:48 +000094
Paul Bakker23986e52011-04-24 08:57:21 +000095 for( i = 0; i < 32; i++ )
96 {
97 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
98 sum += delta;
99 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
100 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000101 }
102 else /* XTEA_DECRYPT */
103 {
Paul Bakker23986e52011-04-24 08:57:21 +0000104 uint32_t delta = 0x9E3779B9, sum = delta * 32;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000105
Paul Bakker23986e52011-04-24 08:57:21 +0000106 for( i = 0; i < 32; i++ )
107 {
108 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
109 sum -= delta;
110 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
111 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000112 }
113
Paul Bakker5c2364c2012-10-01 14:41:15 +0000114 PUT_UINT32_BE( v0, output, 0 );
115 PUT_UINT32_BE( v1, output, 4 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000116
117 return( 0 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000118}
119
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200120#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000121/*
122 * XTEA-CBC buffer encryption/decryption
123 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200124int xtea_crypt_cbc( xtea_context *ctx, int mode, size_t length,
125 unsigned char iv[8], const unsigned char *input,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000126 unsigned char *output)
127{
128 int i;
129 unsigned char temp[8];
130
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200131 if( length % 8 )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000132 return( POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH );
133
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200134 if( mode == XTEA_DECRYPT )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000135 {
136 while( length > 0 )
137 {
138 memcpy( temp, input, 8 );
139 xtea_crypt_ecb( ctx, mode, input, output );
140
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200141 for(i = 0; i < 8; i++)
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000142 output[i] = (unsigned char)( output[i] ^ iv[i] );
143
144 memcpy( iv, temp, 8 );
145
146 input += 8;
147 output += 8;
148 length -= 8;
149 }
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200150 }
151 else
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000152 {
153 while( length > 0 )
154 {
155 for( i = 0; i < 8; i++ )
156 output[i] = (unsigned char)( input[i] ^ iv[i] );
157
158 xtea_crypt_ecb( ctx, mode, output, output );
159 memcpy( iv, output, 8 );
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200160
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000161 input += 8;
162 output += 8;
163 length -= 8;
164 }
165 }
166
167 return( 0 );
168}
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200169#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker90995b52013-06-24 19:20:35 +0200170#endif /* !POLARSSL_XTEA_ALT */
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000171
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000172#if defined(POLARSSL_SELF_TEST)
173
174#include <string.h>
175#include <stdio.h>
176
177/*
178 * XTEA tests vectors (non-official)
179 */
180
181static const unsigned char xtea_test_key[6][16] =
182{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +0000183 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
184 0x0c, 0x0d, 0x0e, 0x0f },
185 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
186 0x0c, 0x0d, 0x0e, 0x0f },
187 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
188 0x0c, 0x0d, 0x0e, 0x0f },
189 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
190 0x00, 0x00, 0x00, 0x00 },
191 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
192 0x00, 0x00, 0x00, 0x00 },
193 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
194 0x00, 0x00, 0x00, 0x00 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000195};
196
197static const unsigned char xtea_test_pt[6][8] =
198{
199 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
200 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
201 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
202 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
203 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
204 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
205};
206
207static const unsigned char xtea_test_ct[6][8] =
208{
209 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
210 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
211 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
212 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
213 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
214 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
215};
216
217/*
218 * Checkup routine
219 */
220int xtea_self_test( int verbose )
221{
222 int i;
223 unsigned char buf[8];
224 xtea_context ctx;
225
226 for( i = 0; i < 6; i++ )
227 {
228 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100229 polarssl_printf( " XTEA test #%d: ", i + 1 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000230
231 memcpy( buf, xtea_test_pt[i], 8 );
232
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200233 xtea_setup( &ctx, xtea_test_key[i] );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000234 xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, buf, buf );
235
236 if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
237 {
238 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100239 polarssl_printf( "failed\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000240
241 return( 1 );
242 }
243
244 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100245 polarssl_printf( "passed\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000246 }
247
248 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100249 polarssl_printf( "\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000250
251 return( 0 );
252}
253
254#endif
255
256#endif