blob: fe0a3509f6f77072bbe46a332dd5cc4dfb49f72d [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker7a7c78f2009-01-04 18:15:48 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_XTEA_C)
Paul Bakker7a7c78f2009-01-04 18:15:48 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/xtea.h"
Paul Bakker7a7c78f2009-01-04 18:15:48 +000031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <string.h>
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_SELF_TEST)
35#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010037#else
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#define mbedtls_printf printf
40#endif /* MBEDTLS_PLATFORM_C */
41#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if !defined(MBEDTLS_XTEA_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020044
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020045/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020047 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
48}
49
Paul Bakker7a7c78f2009-01-04 18:15:48 +000050/*
51 * 32-bit integer manipulation macros (big endian)
52 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000053#ifndef GET_UINT32_BE
54#define GET_UINT32_BE(n,b,i) \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000055{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000056 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
57 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
58 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
59 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000060}
61#endif
62
Paul Bakker5c2364c2012-10-01 14:41:15 +000063#ifndef PUT_UINT32_BE
64#define PUT_UINT32_BE(n,b,i) \
Paul Bakker7a7c78f2009-01-04 18:15:48 +000065{ \
66 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
67 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
68 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
69 (b)[(i) + 3] = (unsigned char) ( (n) ); \
70}
71#endif
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073void mbedtls_xtea_init( mbedtls_xtea_context *ctx )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020074{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 memset( ctx, 0, sizeof( mbedtls_xtea_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020076}
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078void mbedtls_xtea_free( mbedtls_xtea_context *ctx )
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020079{
80 if( ctx == NULL )
81 return;
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_zeroize( ctx, sizeof( mbedtls_xtea_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020084}
85
Paul Bakker7a7c78f2009-01-04 18:15:48 +000086/*
87 * XTEA key schedule
88 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] )
Paul Bakker7a7c78f2009-01-04 18:15:48 +000090{
91 int i;
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 memset( ctx, 0, sizeof(mbedtls_xtea_context) );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000094
95 for( i = 0; i < 4; i++ )
96 {
Paul Bakker5c2364c2012-10-01 14:41:15 +000097 GET_UINT32_BE( ctx->k[i], key, i << 2 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000098 }
99}
100
101/*
102 * XTEA encrypt function
103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, int mode,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200105 const unsigned char input[8], unsigned char output[8])
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000106{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +0000107 uint32_t *k, v0, v1, i;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000108
109 k = ctx->k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200110
Paul Bakker5c2364c2012-10-01 14:41:15 +0000111 GET_UINT32_BE( v0, input, 0 );
112 GET_UINT32_BE( v1, input, 4 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 if( mode == MBEDTLS_XTEA_ENCRYPT )
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000115 {
Paul Bakker23986e52011-04-24 08:57:21 +0000116 uint32_t sum = 0, delta = 0x9E3779B9;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000117
Paul Bakker23986e52011-04-24 08:57:21 +0000118 for( i = 0; i < 32; i++ )
119 {
120 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
121 sum += delta;
122 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
123 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000124 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 else /* MBEDTLS_XTEA_DECRYPT */
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000126 {
Paul Bakker23986e52011-04-24 08:57:21 +0000127 uint32_t delta = 0x9E3779B9, sum = delta * 32;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000128
Paul Bakker23986e52011-04-24 08:57:21 +0000129 for( i = 0; i < 32; i++ )
130 {
131 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
132 sum -= delta;
133 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
134 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000135 }
136
Paul Bakker5c2364c2012-10-01 14:41:15 +0000137 PUT_UINT32_BE( v0, output, 0 );
138 PUT_UINT32_BE( v1, output, 4 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000139
140 return( 0 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000141}
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000144/*
145 * XTEA-CBC buffer encryption/decryption
146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, int mode, size_t length,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200148 unsigned char iv[8], const unsigned char *input,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000149 unsigned char *output)
150{
151 int i;
152 unsigned char temp[8];
153
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200154 if( length % 8 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 return( MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH );
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 if( mode == MBEDTLS_XTEA_DECRYPT )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000158 {
159 while( length > 0 )
160 {
161 memcpy( temp, input, 8 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_xtea_crypt_ecb( ctx, mode, input, output );
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000163
Paul Bakker66d5d072014-06-17 16:39:18 +0200164 for( i = 0; i < 8; i++ )
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000165 output[i] = (unsigned char)( output[i] ^ iv[i] );
166
167 memcpy( iv, temp, 8 );
168
169 input += 8;
170 output += 8;
171 length -= 8;
172 }
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200173 }
174 else
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000175 {
176 while( length > 0 )
177 {
178 for( i = 0; i < 8; i++ )
179 output[i] = (unsigned char)( input[i] ^ iv[i] );
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_xtea_crypt_ecb( ctx, mode, output, output );
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000182 memcpy( iv, output, 8 );
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200183
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000184 input += 8;
185 output += 8;
186 length -= 8;
187 }
188 }
189
190 return( 0 );
191}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#endif /* MBEDTLS_CIPHER_MODE_CBC */
193#endif /* !MBEDTLS_XTEA_ALT */
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_SELF_TEST)
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000196
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000197/*
198 * XTEA tests vectors (non-official)
199 */
200
201static const unsigned char xtea_test_key[6][16] =
202{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +0000203 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
204 0x0c, 0x0d, 0x0e, 0x0f },
205 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
206 0x0c, 0x0d, 0x0e, 0x0f },
207 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
208 0x0c, 0x0d, 0x0e, 0x0f },
209 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
210 0x00, 0x00, 0x00, 0x00 },
211 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
212 0x00, 0x00, 0x00, 0x00 },
213 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
214 0x00, 0x00, 0x00, 0x00 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000215};
216
217static const unsigned char xtea_test_pt[6][8] =
218{
219 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
220 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
221 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
222 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
223 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
224 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
225};
226
227static const unsigned char xtea_test_ct[6][8] =
228{
229 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
230 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
231 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
232 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
233 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
234 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
235};
236
237/*
238 * Checkup routine
239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240int mbedtls_xtea_self_test( int verbose )
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000241{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200242 int i, ret = 0;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000243 unsigned char buf[8];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_xtea_context ctx;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_xtea_init( &ctx );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000247 for( i = 0; i < 6; i++ )
248 {
249 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_printf( " XTEA test #%d: ", i + 1 );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000251
252 memcpy( buf, xtea_test_pt[i], 8 );
253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_xtea_setup( &ctx, xtea_test_key[i] );
255 mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000256
257 if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
258 {
259 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_printf( "failed\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000261
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200262 ret = 1;
263 goto exit;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000264 }
265
266 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_printf( "passed\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000268 }
269
270 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_printf( "\n" );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000272
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200273exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_xtea_free( &ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200275
276 return( ret );
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000277}
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#endif /* MBEDTLS_XTEA_C */