blob: 26ec5de5a9d4562e9235bddfcc8eb8cf50f4e975 [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
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7a7c78f2009-01-04 18:15:48 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker7a7c78f2009-01-04 18:15:48 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_XTEA_C)
Paul Bakker7a7c78f2009-01-04 18:15:48 +000056
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/xtea.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050058#include "mbedtls/platform_util.h"
Paul Bakker7a7c78f2009-01-04 18:15:48 +000059
Rich Evans00ab4702015-02-06 13:43:58 +000060#include <string.h>
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_SELF_TEST)
63#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010065#else
Rich Evans00ab4702015-02-06 13:43:58 +000066#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#define mbedtls_printf printf
68#endif /* MBEDTLS_PLATFORM_C */
69#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if !defined(MBEDTLS_XTEA_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020072
Paul Bakker7a7c78f2009-01-04 18:15:48 +000073/*
74 * 32-bit integer manipulation macros (big endian)
75 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000076#ifndef GET_UINT32_BE
77#define GET_UINT32_BE(n,b,i) \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000078{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000079 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
80 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
81 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
82 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000083}
84#endif
85
Paul Bakker5c2364c2012-10-01 14:41:15 +000086#ifndef PUT_UINT32_BE
87#define PUT_UINT32_BE(n,b,i) \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000088{ \
89 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
90 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
91 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
92 (b)[(i) + 3] = (unsigned char) ( (n) ); \
93}
94#endif
95
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_xtea_init( mbedtls_xtea_context *ctx )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 memset( ctx, 0, sizeof( mbedtls_xtea_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020099}
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101void mbedtls_xtea_free( mbedtls_xtea_context *ctx )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200102{
103 if( ctx == NULL )
104 return;
105
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500106 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_xtea_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200107}
108
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000109/*
110 * XTEA key schedule
111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] )
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000113{
114 int i;
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 memset( ctx, 0, sizeof(mbedtls_xtea_context) );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000117
118 for( i = 0; i < 4; i++ )
119 {
Paul Bakker5c2364c2012-10-01 14:41:15 +0000120 GET_UINT32_BE( ctx->k[i], key, i << 2 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000121 }
122}
123
124/*
125 * XTEA encrypt function
126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, int mode,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200128 const unsigned char input[8], unsigned char output[8])
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000129{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +0000130 uint32_t *k, v0, v1, i;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000131
132 k = ctx->k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200133
Paul Bakker5c2364c2012-10-01 14:41:15 +0000134 GET_UINT32_BE( v0, input, 0 );
135 GET_UINT32_BE( v1, input, 4 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 if( mode == MBEDTLS_XTEA_ENCRYPT )
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000138 {
Paul Bakker23986e52011-04-24 08:57:21 +0000139 uint32_t sum = 0, delta = 0x9E3779B9;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000140
Paul Bakker23986e52011-04-24 08:57:21 +0000141 for( i = 0; i < 32; i++ )
142 {
143 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
144 sum += delta;
145 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
146 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000147 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 else /* MBEDTLS_XTEA_DECRYPT */
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000149 {
Paul Bakker23986e52011-04-24 08:57:21 +0000150 uint32_t delta = 0x9E3779B9, sum = delta * 32;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000151
Paul Bakker23986e52011-04-24 08:57:21 +0000152 for( i = 0; i < 32; i++ )
153 {
154 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
155 sum -= delta;
156 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
157 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000158 }
159
Paul Bakker5c2364c2012-10-01 14:41:15 +0000160 PUT_UINT32_BE( v0, output, 0 );
161 PUT_UINT32_BE( v1, output, 4 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000162
163 return( 0 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000164}
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000167/*
168 * XTEA-CBC buffer encryption/decryption
169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, int mode, size_t length,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200171 unsigned char iv[8], const unsigned char *input,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000172 unsigned char *output)
173{
174 int i;
175 unsigned char temp[8];
176
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200177 if( length % 8 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 return( MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH );
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 if( mode == MBEDTLS_XTEA_DECRYPT )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000181 {
182 while( length > 0 )
183 {
184 memcpy( temp, input, 8 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_xtea_crypt_ecb( ctx, mode, input, output );
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000186
Paul Bakker66d5d072014-06-17 16:39:18 +0200187 for( i = 0; i < 8; i++ )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000188 output[i] = (unsigned char)( output[i] ^ iv[i] );
189
190 memcpy( iv, temp, 8 );
191
192 input += 8;
193 output += 8;
194 length -= 8;
195 }
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200196 }
197 else
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000198 {
199 while( length > 0 )
200 {
201 for( i = 0; i < 8; i++ )
202 output[i] = (unsigned char)( input[i] ^ iv[i] );
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_xtea_crypt_ecb( ctx, mode, output, output );
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000205 memcpy( iv, output, 8 );
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200206
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000207 input += 8;
208 output += 8;
209 length -= 8;
210 }
211 }
212
213 return( 0 );
214}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#endif /* MBEDTLS_CIPHER_MODE_CBC */
216#endif /* !MBEDTLS_XTEA_ALT */
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#if defined(MBEDTLS_SELF_TEST)
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000219
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000220/*
221 * XTEA tests vectors (non-official)
222 */
223
224static const unsigned char xtea_test_key[6][16] =
225{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +0000226 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
227 0x0c, 0x0d, 0x0e, 0x0f },
228 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
229 0x0c, 0x0d, 0x0e, 0x0f },
230 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
231 0x0c, 0x0d, 0x0e, 0x0f },
232 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
233 0x00, 0x00, 0x00, 0x00 },
234 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
235 0x00, 0x00, 0x00, 0x00 },
236 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
237 0x00, 0x00, 0x00, 0x00 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000238};
239
240static const unsigned char xtea_test_pt[6][8] =
241{
242 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
243 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
244 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
245 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
246 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
247 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
248};
249
250static const unsigned char xtea_test_ct[6][8] =
251{
252 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
253 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
254 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
255 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
256 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
257 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
258};
259
260/*
261 * Checkup routine
262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263int mbedtls_xtea_self_test( int verbose )
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000264{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200265 int i, ret = 0;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000266 unsigned char buf[8];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_xtea_context ctx;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_xtea_init( &ctx );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000270 for( i = 0; i < 6; i++ )
271 {
272 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_printf( " XTEA test #%d: ", i + 1 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000274
275 memcpy( buf, xtea_test_pt[i], 8 );
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_xtea_setup( &ctx, xtea_test_key[i] );
278 mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000279
280 if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
281 {
282 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_printf( "failed\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000284
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200285 ret = 1;
286 goto exit;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000287 }
288
289 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 mbedtls_printf( "passed\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000291 }
292
293 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_printf( "\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000295
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200296exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_xtea_free( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200298
299 return( ret );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000300}
301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#endif /* MBEDTLS_XTEA_C */