blob: c44892e6046af2856958ff65100984cfb98ae088 [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker7a7c78f2009-01-04 18:15:48 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_XTEA_C)
Paul Bakker7a7c78f2009-01-04 18:15:48 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/xtea.h"
Paul Bakker7a7c78f2009-01-04 18:15:48 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_SELF_TEST)
36#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010038#else
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#define mbedtls_printf printf
41#endif /* MBEDTLS_PLATFORM_C */
42#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if !defined(MBEDTLS_XTEA_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020045
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020046/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020048 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
49}
50
Paul Bakker7a7c78f2009-01-04 18:15:48 +000051/*
52 * 32-bit integer manipulation macros (big endian)
53 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000054#ifndef GET_UINT32_BE
55#define GET_UINT32_BE(n,b,i) \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000056{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000057 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
58 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
59 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
60 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000061}
62#endif
63
Paul Bakker5c2364c2012-10-01 14:41:15 +000064#ifndef PUT_UINT32_BE
65#define PUT_UINT32_BE(n,b,i) \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000066{ \
67 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
68 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
69 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
70 (b)[(i) + 3] = (unsigned char) ( (n) ); \
71}
72#endif
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074void mbedtls_xtea_init( mbedtls_xtea_context *ctx )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 memset( ctx, 0, sizeof( mbedtls_xtea_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020077}
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079void mbedtls_xtea_free( mbedtls_xtea_context *ctx )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020080{
81 if( ctx == NULL )
82 return;
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_zeroize( ctx, sizeof( mbedtls_xtea_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020085}
86
Paul Bakker7a7c78f2009-01-04 18:15:48 +000087/*
88 * XTEA key schedule
89 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] )
Paul Bakker7a7c78f2009-01-04 18:15:48 +000091{
92 int i;
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 memset( ctx, 0, sizeof(mbedtls_xtea_context) );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000095
96 for( i = 0; i < 4; i++ )
97 {
Paul Bakker5c2364c2012-10-01 14:41:15 +000098 GET_UINT32_BE( ctx->k[i], key, i << 2 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000099 }
100}
101
102/*
103 * XTEA encrypt function
104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, int mode,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200106 const unsigned char input[8], unsigned char output[8])
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000107{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +0000108 uint32_t *k, v0, v1, i;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000109
110 k = ctx->k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200111
Paul Bakker5c2364c2012-10-01 14:41:15 +0000112 GET_UINT32_BE( v0, input, 0 );
113 GET_UINT32_BE( v1, input, 4 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 if( mode == MBEDTLS_XTEA_ENCRYPT )
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000116 {
Paul Bakker23986e52011-04-24 08:57:21 +0000117 uint32_t sum = 0, delta = 0x9E3779B9;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000118
Paul Bakker23986e52011-04-24 08:57:21 +0000119 for( i = 0; i < 32; i++ )
120 {
121 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
122 sum += delta;
123 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
124 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000125 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 else /* MBEDTLS_XTEA_DECRYPT */
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000127 {
Paul Bakker23986e52011-04-24 08:57:21 +0000128 uint32_t delta = 0x9E3779B9, sum = delta * 32;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000129
Paul Bakker23986e52011-04-24 08:57:21 +0000130 for( i = 0; i < 32; i++ )
131 {
132 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
133 sum -= delta;
134 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
135 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000136 }
137
Paul Bakker5c2364c2012-10-01 14:41:15 +0000138 PUT_UINT32_BE( v0, output, 0 );
139 PUT_UINT32_BE( v1, output, 4 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000140
141 return( 0 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000142}
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000145/*
146 * XTEA-CBC buffer encryption/decryption
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, int mode, size_t length,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200149 unsigned char iv[8], const unsigned char *input,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000150 unsigned char *output)
151{
152 int i;
153 unsigned char temp[8];
154
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200155 if( length % 8 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 return( MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH );
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 if( mode == MBEDTLS_XTEA_DECRYPT )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000159 {
160 while( length > 0 )
161 {
162 memcpy( temp, input, 8 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_xtea_crypt_ecb( ctx, mode, input, output );
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000164
Paul Bakker66d5d072014-06-17 16:39:18 +0200165 for( i = 0; i < 8; i++ )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000166 output[i] = (unsigned char)( output[i] ^ iv[i] );
167
168 memcpy( iv, temp, 8 );
169
170 input += 8;
171 output += 8;
172 length -= 8;
173 }
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200174 }
175 else
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000176 {
177 while( length > 0 )
178 {
179 for( i = 0; i < 8; i++ )
180 output[i] = (unsigned char)( input[i] ^ iv[i] );
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_xtea_crypt_ecb( ctx, mode, output, output );
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000183 memcpy( iv, output, 8 );
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200184
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000185 input += 8;
186 output += 8;
187 length -= 8;
188 }
189 }
190
191 return( 0 );
192}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#endif /* MBEDTLS_CIPHER_MODE_CBC */
194#endif /* !MBEDTLS_XTEA_ALT */
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_SELF_TEST)
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000197
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000198/*
199 * XTEA tests vectors (non-official)
200 */
201
202static const unsigned char xtea_test_key[6][16] =
203{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +0000204 { 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, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
209 0x0c, 0x0d, 0x0e, 0x0f },
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 },
214 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
215 0x00, 0x00, 0x00, 0x00 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000216};
217
218static const unsigned char xtea_test_pt[6][8] =
219{
220 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
221 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
222 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
223 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
224 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
225 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
226};
227
228static const unsigned char xtea_test_ct[6][8] =
229{
230 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
231 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
232 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
233 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
234 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
235 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
236};
237
238/*
239 * Checkup routine
240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241int mbedtls_xtea_self_test( int verbose )
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000242{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200243 int i, ret = 0;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000244 unsigned char buf[8];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_xtea_context ctx;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_xtea_init( &ctx );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000248 for( i = 0; i < 6; i++ )
249 {
250 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_printf( " XTEA test #%d: ", i + 1 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000252
253 memcpy( buf, xtea_test_pt[i], 8 );
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_xtea_setup( &ctx, xtea_test_key[i] );
256 mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000257
258 if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
259 {
260 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_printf( "failed\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000262
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200263 ret = 1;
264 goto exit;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000265 }
266
267 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_printf( "passed\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000269 }
270
271 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_printf( "\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000273
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200274exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_xtea_free( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200276
277 return( ret );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000278}
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_XTEA_C */