blob: f8ab014f9a63fc0a386c785d8f5b68e59b88e704 [file] [log] [blame]
Paul Bakker7a7c78f2009-01-04 18:15:48 +00001/*
2 * An 32-bit implementation of the XTEA algorithm
3 *
Paul Bakker4087c472013-06-12 16:49:10 +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 Bakker4087c472013-06-12 16:49:10 +020032#if !defined(POLARSSL_XTEA_ALT)
33
Paul Bakker7a7c78f2009-01-04 18:15:48 +000034/*
35 * 32-bit integer manipulation macros (big endian)
36 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000037#ifndef GET_UINT32_BE
38#define GET_UINT32_BE(n,b,i) \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000039{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000040 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
41 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
42 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
43 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000044}
45#endif
46
Paul Bakker5c2364c2012-10-01 14:41:15 +000047#ifndef PUT_UINT32_BE
48#define PUT_UINT32_BE(n,b,i) \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000049{ \
50 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
51 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
52 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
53 (b)[(i) + 3] = (unsigned char) ( (n) ); \
54}
55#endif
56
57/*
58 * XTEA key schedule
59 */
60void xtea_setup( xtea_context *ctx, unsigned char key[16] )
61{
62 int i;
63
64 memset(ctx, 0, sizeof(xtea_context));
65
66 for( i = 0; i < 4; i++ )
67 {
Paul Bakker5c2364c2012-10-01 14:41:15 +000068 GET_UINT32_BE( ctx->k[i], key, i << 2 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000069 }
70}
71
72/*
73 * XTEA encrypt function
74 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000075int xtea_crypt_ecb( xtea_context *ctx, int mode, unsigned char input[8],
Paul Bakker0fdf3ca2009-05-03 12:54:07 +000076 unsigned char output[8])
Paul Bakker7a7c78f2009-01-04 18:15:48 +000077{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +000078 uint32_t *k, v0, v1, i;
Paul Bakker7a7c78f2009-01-04 18:15:48 +000079
80 k = ctx->k;
81
Paul Bakker5c2364c2012-10-01 14:41:15 +000082 GET_UINT32_BE( v0, input, 0 );
83 GET_UINT32_BE( v1, input, 4 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000084
85 if( mode == XTEA_ENCRYPT )
86 {
Paul Bakker23986e52011-04-24 08:57:21 +000087 uint32_t sum = 0, delta = 0x9E3779B9;
Paul Bakker7a7c78f2009-01-04 18:15:48 +000088
Paul Bakker23986e52011-04-24 08:57:21 +000089 for( i = 0; i < 32; i++ )
90 {
91 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
92 sum += delta;
93 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
94 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +000095 }
96 else /* XTEA_DECRYPT */
97 {
Paul Bakker23986e52011-04-24 08:57:21 +000098 uint32_t delta = 0x9E3779B9, sum = delta * 32;
Paul Bakker7a7c78f2009-01-04 18:15:48 +000099
Paul Bakker23986e52011-04-24 08:57:21 +0000100 for( i = 0; i < 32; i++ )
101 {
102 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
103 sum -= delta;
104 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
105 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000106 }
107
Paul Bakker5c2364c2012-10-01 14:41:15 +0000108 PUT_UINT32_BE( v0, output, 0 );
109 PUT_UINT32_BE( v1, output, 4 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000110
111 return( 0 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000112}
113
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000114/*
115 * XTEA-CBC buffer encryption/decryption
116 */
117int xtea_crypt_cbc( xtea_context *ctx,
118 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000119 size_t length,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000120 unsigned char iv[8],
121 unsigned char *input,
122 unsigned char *output)
123{
124 int i;
125 unsigned char temp[8];
126
127 if(length % 8)
128 return( POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH );
129
130 if( mode == XTEA_DECRYPT )
131 {
132 while( length > 0 )
133 {
134 memcpy( temp, input, 8 );
135 xtea_crypt_ecb( ctx, mode, input, output );
136
137 for(i = 0; i < 8; i++)
138 output[i] = (unsigned char)( output[i] ^ iv[i] );
139
140 memcpy( iv, temp, 8 );
141
142 input += 8;
143 output += 8;
144 length -= 8;
145 }
146 }
147 else
148 {
149 while( length > 0 )
150 {
151 for( i = 0; i < 8; i++ )
152 output[i] = (unsigned char)( input[i] ^ iv[i] );
153
154 xtea_crypt_ecb( ctx, mode, output, output );
155 memcpy( iv, output, 8 );
156
157 input += 8;
158 output += 8;
159 length -= 8;
160 }
161 }
162
163 return( 0 );
164}
Paul Bakker4087c472013-06-12 16:49:10 +0200165#endif /* !POLARSSL_XTEA_ALT */
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000166
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000167#if defined(POLARSSL_SELF_TEST)
168
169#include <string.h>
170#include <stdio.h>
171
172/*
173 * XTEA tests vectors (non-official)
174 */
175
176static const unsigned char xtea_test_key[6][16] =
177{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +0000178 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
179 0x0c, 0x0d, 0x0e, 0x0f },
180 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
181 0x0c, 0x0d, 0x0e, 0x0f },
182 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
183 0x0c, 0x0d, 0x0e, 0x0f },
184 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
185 0x00, 0x00, 0x00, 0x00 },
186 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
187 0x00, 0x00, 0x00, 0x00 },
188 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
189 0x00, 0x00, 0x00, 0x00 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000190};
191
192static const unsigned char xtea_test_pt[6][8] =
193{
194 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
195 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
196 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
197 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
198 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
199 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
200};
201
202static const unsigned char xtea_test_ct[6][8] =
203{
204 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
205 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
206 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
207 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
208 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
209 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
210};
211
212/*
213 * Checkup routine
214 */
215int xtea_self_test( int verbose )
216{
217 int i;
218 unsigned char buf[8];
219 xtea_context ctx;
220
221 for( i = 0; i < 6; i++ )
222 {
223 if( verbose != 0 )
224 printf( " XTEA test #%d: ", i + 1 );
225
226 memcpy( buf, xtea_test_pt[i], 8 );
227
228 xtea_setup( &ctx, (unsigned char *) xtea_test_key[i] );
229 xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, buf, buf );
230
231 if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
232 {
233 if( verbose != 0 )
234 printf( "failed\n" );
235
236 return( 1 );
237 }
238
239 if( verbose != 0 )
240 printf( "passed\n" );
241 }
242
243 if( verbose != 0 )
244 printf( "\n" );
245
246 return( 0 );
247}
248
249#endif
250
251#endif