blob: 01478ac932080cc22a28a56b309bb721a1e3798c [file] [log] [blame]
Paul Bakker7a7c78f2009-01-04 18:15:48 +00001/*
Tom Cosgrove5205c972022-07-28 06:12:08 +01002 * A 32-bit implementation of the XTEA algorithm
Paul Bakker7a7c78f2009-01-04 18:15:48 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 Bakker7a7c78f2009-01-04 18:15:48 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker7a7c78f2009-01-04 18:15:48 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_XTEA_C)
Paul Bakker7a7c78f2009-01-04 18:15:48 +000023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/xtea.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050025#include "mbedtls/platform_util.h"
Paul Bakker7a7c78f2009-01-04 18:15:48 +000026
Rich Evans00ab4702015-02-06 13:43:58 +000027#include <string.h>
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_SELF_TEST)
30#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010032#else
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#define mbedtls_printf printf
35#endif /* MBEDTLS_PLATFORM_C */
36#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if !defined(MBEDTLS_XTEA_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040void mbedtls_xtea_init( mbedtls_xtea_context *ctx )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020041{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 memset( ctx, 0, sizeof( mbedtls_xtea_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020043}
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045void mbedtls_xtea_free( mbedtls_xtea_context *ctx )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020046{
47 if( ctx == NULL )
48 return;
49
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050050 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_xtea_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020051}
52
Paul Bakker7a7c78f2009-01-04 18:15:48 +000053/*
54 * XTEA key schedule
55 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] )
Paul Bakker7a7c78f2009-01-04 18:15:48 +000057{
58 int i;
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 memset( ctx, 0, sizeof(mbedtls_xtea_context) );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000061
62 for( i = 0; i < 4; i++ )
63 {
Joe Subbianica8a7cf2021-08-03 16:42:42 +010064 ctx->k[i] = MBEDTLS_GET_UINT32_BE( key, i << 2 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000065 }
66}
67
68/*
69 * XTEA encrypt function
70 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, int mode,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020072 const unsigned char input[8], unsigned char output[8])
Paul Bakker7a7c78f2009-01-04 18:15:48 +000073{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +000074 uint32_t *k, v0, v1, i;
Paul Bakker7a7c78f2009-01-04 18:15:48 +000075
76 k = ctx->k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020077
Joe Subbianica8a7cf2021-08-03 16:42:42 +010078 v0 = MBEDTLS_GET_UINT32_BE( input, 0 );
79 v1 = MBEDTLS_GET_UINT32_BE( input, 4 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 if( mode == MBEDTLS_XTEA_ENCRYPT )
Paul Bakker7a7c78f2009-01-04 18:15:48 +000082 {
Paul Bakker23986e52011-04-24 08:57:21 +000083 uint32_t sum = 0, delta = 0x9E3779B9;
Paul Bakker7a7c78f2009-01-04 18:15:48 +000084
Paul Bakker23986e52011-04-24 08:57:21 +000085 for( i = 0; i < 32; i++ )
86 {
87 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
88 sum += delta;
89 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
90 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +000091 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 else /* MBEDTLS_XTEA_DECRYPT */
Paul Bakker7a7c78f2009-01-04 18:15:48 +000093 {
Paul Bakker23986e52011-04-24 08:57:21 +000094 uint32_t delta = 0x9E3779B9, sum = delta * 32;
Paul Bakker7a7c78f2009-01-04 18:15:48 +000095
Paul Bakker23986e52011-04-24 08:57:21 +000096 for( i = 0; i < 32; i++ )
97 {
98 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
99 sum -= delta;
100 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
101 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000102 }
103
Joe Subbianica8a7cf2021-08-03 16:42:42 +0100104 MBEDTLS_PUT_UINT32_BE( v0, output, 0 );
105 MBEDTLS_PUT_UINT32_BE( v1, output, 4 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000106
107 return( 0 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000108}
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000111/*
112 * XTEA-CBC buffer encryption/decryption
113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, int mode, size_t length,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200115 unsigned char iv[8], const unsigned char *input,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000116 unsigned char *output)
117{
118 int i;
119 unsigned char temp[8];
120
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200121 if( length % 8 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 return( MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH );
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 if( mode == MBEDTLS_XTEA_DECRYPT )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000125 {
126 while( length > 0 )
127 {
128 memcpy( temp, input, 8 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_xtea_crypt_ecb( ctx, mode, input, output );
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000130
Paul Bakker66d5d072014-06-17 16:39:18 +0200131 for( i = 0; i < 8; i++ )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000132 output[i] = (unsigned char)( output[i] ^ iv[i] );
133
134 memcpy( iv, temp, 8 );
135
136 input += 8;
137 output += 8;
138 length -= 8;
139 }
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200140 }
141 else
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000142 {
143 while( length > 0 )
144 {
145 for( i = 0; i < 8; i++ )
146 output[i] = (unsigned char)( input[i] ^ iv[i] );
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_xtea_crypt_ecb( ctx, mode, output, output );
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000149 memcpy( iv, output, 8 );
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200150
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000151 input += 8;
152 output += 8;
153 length -= 8;
154 }
155 }
156
157 return( 0 );
158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#endif /* MBEDTLS_CIPHER_MODE_CBC */
160#endif /* !MBEDTLS_XTEA_ALT */
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_SELF_TEST)
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000163
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000164/*
165 * XTEA tests vectors (non-official)
166 */
167
168static const unsigned char xtea_test_key[6][16] =
169{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +0000170 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
171 0x0c, 0x0d, 0x0e, 0x0f },
172 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
173 0x0c, 0x0d, 0x0e, 0x0f },
174 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
175 0x0c, 0x0d, 0x0e, 0x0f },
176 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
177 0x00, 0x00, 0x00, 0x00 },
178 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
179 0x00, 0x00, 0x00, 0x00 },
180 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
181 0x00, 0x00, 0x00, 0x00 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000182};
183
184static const unsigned char xtea_test_pt[6][8] =
185{
186 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
187 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
188 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
189 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
190 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
191 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
192};
193
194static const unsigned char xtea_test_ct[6][8] =
195{
196 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
197 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
198 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
199 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
200 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
201 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
202};
203
204/*
205 * Checkup routine
206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207int mbedtls_xtea_self_test( int verbose )
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000208{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200209 int i, ret = 0;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000210 unsigned char buf[8];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_xtea_context ctx;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_xtea_init( &ctx );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000214 for( i = 0; i < 6; i++ )
215 {
216 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_printf( " XTEA test #%d: ", i + 1 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000218
219 memcpy( buf, xtea_test_pt[i], 8 );
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_xtea_setup( &ctx, xtea_test_key[i] );
222 mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000223
224 if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
225 {
226 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 mbedtls_printf( "failed\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000228
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200229 ret = 1;
230 goto exit;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000231 }
232
233 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_printf( "passed\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000235 }
236
237 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_printf( "\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000239
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200240exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_xtea_free( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200242
243 return( ret );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000244}
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#endif /* MBEDTLS_XTEA_C */