blob: dab6cd3ee6fe4737d5c86dd752bb1e9f9ac6f7df [file] [log] [blame]
Paul Bakker7a7c78f2009-01-04 18:15:48 +00001/*
2 * An 32-bit implementation of the XTEA algorithm
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7a7c78f2009-01-04 18:15:48 +000020 */
21
Gilles Peskinedb09ef62020-06-03 01:43:33 +020022#include "common.h"
Paul Bakker7a7c78f2009-01-04 18:15:48 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_XTEA_C)
Paul Bakker7a7c78f2009-01-04 18:15:48 +000025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/xtea.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Paul Bakker7a7c78f2009-01-04 18:15:48 +000028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_SELF_TEST)
32#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010034#else
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define mbedtls_printf printf
37#endif /* MBEDTLS_PLATFORM_C */
38#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if !defined(MBEDTLS_XTEA_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020041
Paul Bakker7a7c78f2009-01-04 18:15:48 +000042/*
43 * 32-bit integer manipulation macros (big endian)
44 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000045#ifndef GET_UINT32_BE
46#define GET_UINT32_BE(n,b,i) \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000047{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000048 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
49 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
50 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
51 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000052}
53#endif
54
Paul Bakker5c2364c2012-10-01 14:41:15 +000055#ifndef PUT_UINT32_BE
56#define PUT_UINT32_BE(n,b,i) \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000057{ \
58 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
59 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
60 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
61 (b)[(i) + 3] = (unsigned char) ( (n) ); \
62}
63#endif
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065void mbedtls_xtea_init( mbedtls_xtea_context *ctx )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 memset( ctx, 0, sizeof( mbedtls_xtea_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020068}
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070void mbedtls_xtea_free( mbedtls_xtea_context *ctx )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020071{
72 if( ctx == NULL )
73 return;
74
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050075 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_xtea_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020076}
77
Paul Bakker7a7c78f2009-01-04 18:15:48 +000078/*
79 * XTEA key schedule
80 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] )
Paul Bakker7a7c78f2009-01-04 18:15:48 +000082{
83 int i;
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 memset( ctx, 0, sizeof(mbedtls_xtea_context) );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000086
87 for( i = 0; i < 4; i++ )
88 {
Paul Bakker5c2364c2012-10-01 14:41:15 +000089 GET_UINT32_BE( ctx->k[i], key, i << 2 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000090 }
91}
92
93/*
94 * XTEA encrypt function
95 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, int mode,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020097 const unsigned char input[8], unsigned char output[8])
Paul Bakker7a7c78f2009-01-04 18:15:48 +000098{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +000099 uint32_t *k, v0, v1, i;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000100
101 k = ctx->k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200102
Paul Bakker5c2364c2012-10-01 14:41:15 +0000103 GET_UINT32_BE( v0, input, 0 );
104 GET_UINT32_BE( v1, input, 4 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 if( mode == MBEDTLS_XTEA_ENCRYPT )
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000107 {
Paul Bakker23986e52011-04-24 08:57:21 +0000108 uint32_t sum = 0, delta = 0x9E3779B9;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000109
Paul Bakker23986e52011-04-24 08:57:21 +0000110 for( i = 0; i < 32; i++ )
111 {
112 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
113 sum += delta;
114 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
115 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000116 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 else /* MBEDTLS_XTEA_DECRYPT */
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000118 {
Paul Bakker23986e52011-04-24 08:57:21 +0000119 uint32_t delta = 0x9E3779B9, sum = delta * 32;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000120
Paul Bakker23986e52011-04-24 08:57:21 +0000121 for( i = 0; i < 32; i++ )
122 {
123 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
124 sum -= delta;
125 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
126 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000127 }
128
Paul Bakker5c2364c2012-10-01 14:41:15 +0000129 PUT_UINT32_BE( v0, output, 0 );
130 PUT_UINT32_BE( v1, output, 4 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000131
132 return( 0 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000133}
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000136/*
137 * XTEA-CBC buffer encryption/decryption
138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, int mode, size_t length,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200140 unsigned char iv[8], const unsigned char *input,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000141 unsigned char *output)
142{
143 int i;
144 unsigned char temp[8];
145
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200146 if( length % 8 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 return( MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH );
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 if( mode == MBEDTLS_XTEA_DECRYPT )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000150 {
151 while( length > 0 )
152 {
153 memcpy( temp, input, 8 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_xtea_crypt_ecb( ctx, mode, input, output );
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000155
Paul Bakker66d5d072014-06-17 16:39:18 +0200156 for( i = 0; i < 8; i++ )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000157 output[i] = (unsigned char)( output[i] ^ iv[i] );
158
159 memcpy( iv, temp, 8 );
160
161 input += 8;
162 output += 8;
163 length -= 8;
164 }
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200165 }
166 else
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000167 {
168 while( length > 0 )
169 {
170 for( i = 0; i < 8; i++ )
171 output[i] = (unsigned char)( input[i] ^ iv[i] );
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_xtea_crypt_ecb( ctx, mode, output, output );
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000174 memcpy( iv, output, 8 );
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200175
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000176 input += 8;
177 output += 8;
178 length -= 8;
179 }
180 }
181
182 return( 0 );
183}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#endif /* MBEDTLS_CIPHER_MODE_CBC */
185#endif /* !MBEDTLS_XTEA_ALT */
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_SELF_TEST)
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000188
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000189/*
190 * XTEA tests vectors (non-official)
191 */
192
193static const unsigned char xtea_test_key[6][16] =
194{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +0000195 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
196 0x0c, 0x0d, 0x0e, 0x0f },
197 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
198 0x0c, 0x0d, 0x0e, 0x0f },
199 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
200 0x0c, 0x0d, 0x0e, 0x0f },
201 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
202 0x00, 0x00, 0x00, 0x00 },
203 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
204 0x00, 0x00, 0x00, 0x00 },
205 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
206 0x00, 0x00, 0x00, 0x00 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000207};
208
209static const unsigned char xtea_test_pt[6][8] =
210{
211 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
212 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
213 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
214 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
215 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
216 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
217};
218
219static const unsigned char xtea_test_ct[6][8] =
220{
221 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
222 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
223 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
224 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
225 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
226 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
227};
228
229/*
230 * Checkup routine
231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232int mbedtls_xtea_self_test( int verbose )
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000233{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200234 int i, ret = 0;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000235 unsigned char buf[8];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_xtea_context ctx;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_xtea_init( &ctx );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000239 for( i = 0; i < 6; i++ )
240 {
241 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_printf( " XTEA test #%d: ", i + 1 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000243
244 memcpy( buf, xtea_test_pt[i], 8 );
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_xtea_setup( &ctx, xtea_test_key[i] );
247 mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000248
249 if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
250 {
251 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_printf( "failed\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000253
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200254 ret = 1;
255 goto exit;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000256 }
257
258 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_printf( "passed\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000260 }
261
262 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_printf( "\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000264
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200265exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_xtea_free( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200267
268 return( ret );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000269}
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#endif /* MBEDTLS_XTEA_C */