1
#pragma clang diagnostic ignored "-Wunknown-warning-option"
2
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
3
#pragma clang diagnostic ignored "-Wsign-conversion"
5
#pragma GCC diagnostic ignored "-Wconversion"
7
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
8
#pragma clang diagnostic ignored "-Wsign-conversion"
10
#pragma GCC diagnostic ignored "-Wconversion"
12
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
13
#pragma clang diagnostic ignored "-Wsign-conversion"
15
* An 32-bit implementation of the XTEA algorithm
17
* Copyright The Mbed TLS Contributors
18
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
20
* This file is provided under the Apache License 2.0, or the
21
* GNU General Public License v2.0 or later.
26
* Licensed under the Apache License, Version 2.0 (the "License"); you may
27
* not use this file except in compliance with the License.
28
* You may obtain a copy of the License at
30
* http://www.apache.org/licenses/LICENSE-2.0
32
* Unless required by applicable law or agreed to in writing, software
33
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
34
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35
* See the License for the specific language governing permissions and
36
* limitations under the License.
41
* GNU General Public License v2.0 or later:
43
* This program is free software; you can redistribute it and/or modify
44
* it under the terms of the GNU General Public License as published by
45
* the Free Software Foundation; either version 2 of the License, or
46
* (at your option) any later version.
48
* This program is distributed in the hope that it will be useful,
49
* but WITHOUT ANY WARRANTY; without even the implied warranty of
50
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51
* GNU General Public License for more details.
53
* You should have received a copy of the GNU General Public License along
54
* with this program; if not, write to the Free Software Foundation, Inc.,
55
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
60
#if !defined(MBEDTLS_CONFIG_FILE)
61
#include "mbedtls/config.h"
63
#include MBEDTLS_CONFIG_FILE
66
#if defined(MBEDTLS_XTEA_C)
68
#include "mbedtls/xtea.h"
69
#include "mbedtls/platform_util.h"
73
#if defined(MBEDTLS_SELF_TEST)
74
#if defined(MBEDTLS_PLATFORM_C)
75
#include "mbedtls/platform.h"
78
#define mbedtls_printf printf
79
#endif /* MBEDTLS_PLATFORM_C */
80
#endif /* MBEDTLS_SELF_TEST */
82
#if !defined(MBEDTLS_XTEA_ALT)
85
* 32-bit integer manipulation macros (big endian)
88
#define GET_UINT32_BE(n,b,i) \
90
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
91
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
92
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
93
| ( (uint32_t) (b)[(i) + 3] ); \
98
#define PUT_UINT32_BE(n,b,i) \
100
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
101
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
102
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
103
(b)[(i) + 3] = (unsigned char) ( (n) ); \
107
void mbedtls_xtea_init( mbedtls_xtea_context *ctx )
109
memset( ctx, 0, sizeof( mbedtls_xtea_context ) );
112
void mbedtls_xtea_free( mbedtls_xtea_context *ctx )
117
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_xtea_context ) );
123
void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] )
127
memset( ctx, 0, sizeof(mbedtls_xtea_context) );
129
for( i = 0; i < 4; i++ )
131
GET_UINT32_BE( ctx->k[i], key, i << 2 );
136
* XTEA encrypt function
138
int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, int mode,
139
const unsigned char input[8], unsigned char output[8])
141
uint32_t *k, v0, v1, i;
145
GET_UINT32_BE( v0, input, 0 );
146
GET_UINT32_BE( v1, input, 4 );
148
if( mode == MBEDTLS_XTEA_ENCRYPT )
150
uint32_t sum = 0, delta = 0x9E3779B9;
152
for( i = 0; i < 32; i++ )
154
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
156
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
159
else /* MBEDTLS_XTEA_DECRYPT */
161
uint32_t delta = 0x9E3779B9, sum = delta * 32;
163
for( i = 0; i < 32; i++ )
165
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
167
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
171
PUT_UINT32_BE( v0, output, 0 );
172
PUT_UINT32_BE( v1, output, 4 );
177
#if defined(MBEDTLS_CIPHER_MODE_CBC)
179
* XTEA-CBC buffer encryption/decryption
181
int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, int mode, size_t length,
182
unsigned char iv[8], const unsigned char *input,
183
unsigned char *output)
186
unsigned char temp[8];
189
return( MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH );
191
if( mode == MBEDTLS_XTEA_DECRYPT )
195
memcpy( temp, input, 8 );
196
mbedtls_xtea_crypt_ecb( ctx, mode, input, output );
198
for( i = 0; i < 8; i++ )
199
output[i] = (unsigned char)( output[i] ^ iv[i] );
201
memcpy( iv, temp, 8 );
212
for( i = 0; i < 8; i++ )
213
output[i] = (unsigned char)( input[i] ^ iv[i] );
215
mbedtls_xtea_crypt_ecb( ctx, mode, output, output );
216
memcpy( iv, output, 8 );
226
#endif /* MBEDTLS_CIPHER_MODE_CBC */
227
#endif /* !MBEDTLS_XTEA_ALT */
229
#if defined(MBEDTLS_SELF_TEST)
232
* XTEA tests vectors (non-official)
235
static const unsigned char xtea_test_key[6][16] =
237
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
238
0x0c, 0x0d, 0x0e, 0x0f },
239
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
240
0x0c, 0x0d, 0x0e, 0x0f },
241
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
242
0x0c, 0x0d, 0x0e, 0x0f },
243
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
244
0x00, 0x00, 0x00, 0x00 },
245
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
246
0x00, 0x00, 0x00, 0x00 },
247
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
248
0x00, 0x00, 0x00, 0x00 }
251
static const unsigned char xtea_test_pt[6][8] =
253
{ 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
254
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
255
{ 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
256
{ 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
257
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
258
{ 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
261
static const unsigned char xtea_test_ct[6][8] =
263
{ 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
264
{ 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
265
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
266
{ 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
267
{ 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
268
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
274
int mbedtls_xtea_self_test( int verbose )
277
unsigned char buf[8];
278
mbedtls_xtea_context ctx;
280
mbedtls_xtea_init( &ctx );
281
for( i = 0; i < 6; i++ )
284
mbedtls_printf( " XTEA test #%d: ", i + 1 );
286
memcpy( buf, xtea_test_pt[i], 8 );
288
mbedtls_xtea_setup( &ctx, xtea_test_key[i] );
289
mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf );
291
if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
294
mbedtls_printf( "failed\n" );
301
mbedtls_printf( "passed\n" );
305
mbedtls_printf( "\n" );
308
mbedtls_xtea_free( &ctx );
313
#endif /* MBEDTLS_SELF_TEST */
315
#endif /* MBEDTLS_XTEA_C */