2009-01-04 19:15:48 +01:00
|
|
|
/*
|
|
|
|
* An 32-bit implementation of the XTEA algorithm
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-01-04 19:15:48 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2009-01-04 19:15:48 +01:00
|
|
|
*/
|
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2009-01-04 19:15:48 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_XTEA_C)
|
2009-01-04 19:15:48 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/xtea.h"
|
2018-04-17 16:51:09 +02:00
|
|
|
#include "mbedtls/platform_util.h"
|
2009-01-04 19:15:48 +01:00
|
|
|
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2014-02-01 22:50:26 +01:00
|
|
|
#else
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <stdio.h>
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_printf printf
|
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2014-02-01 22:50:26 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_XTEA_ALT)
|
2013-06-24 19:20:35 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_xtea_init( mbedtls_xtea_context *ctx )
|
2014-06-18 11:12:03 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
memset( ctx, 0, sizeof( mbedtls_xtea_context ) );
|
2014-06-18 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_xtea_free( mbedtls_xtea_context *ctx )
|
2014-06-18 11:12:03 +02:00
|
|
|
{
|
|
|
|
if( ctx == NULL )
|
|
|
|
return;
|
|
|
|
|
2018-04-17 16:51:09 +02:00
|
|
|
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_xtea_context ) );
|
2014-06-18 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
2009-01-04 19:15:48 +01:00
|
|
|
/*
|
|
|
|
* XTEA key schedule
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] )
|
2009-01-04 19:15:48 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
memset( ctx, 0, sizeof(mbedtls_xtea_context) );
|
2009-01-04 19:15:48 +01:00
|
|
|
|
|
|
|
for( i = 0; i < 4; i++ )
|
|
|
|
{
|
2021-08-03 17:42:42 +02:00
|
|
|
ctx->k[i] = MBEDTLS_GET_UINT32_BE( key, i << 2 );
|
2009-01-04 19:15:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XTEA encrypt function
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, int mode,
|
2013-06-25 16:25:17 +02:00
|
|
|
const unsigned char input[8], unsigned char output[8])
|
2009-01-04 19:15:48 +01:00
|
|
|
{
|
2009-05-03 14:54:07 +02:00
|
|
|
uint32_t *k, v0, v1, i;
|
2009-01-04 19:15:48 +01:00
|
|
|
|
|
|
|
k = ctx->k;
|
2013-06-25 16:25:17 +02:00
|
|
|
|
2021-08-03 17:42:42 +02:00
|
|
|
v0 = MBEDTLS_GET_UINT32_BE( input, 0 );
|
|
|
|
v1 = MBEDTLS_GET_UINT32_BE( input, 4 );
|
2009-01-04 19:15:48 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mode == MBEDTLS_XTEA_ENCRYPT )
|
2009-01-04 19:15:48 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
uint32_t sum = 0, delta = 0x9E3779B9;
|
|
|
|
|
|
|
|
for( i = 0; i < 32; i++ )
|
|
|
|
{
|
|
|
|
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
|
|
|
|
sum += delta;
|
|
|
|
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
|
|
|
|
}
|
2009-01-04 19:15:48 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
else /* MBEDTLS_XTEA_DECRYPT */
|
2009-01-04 19:15:48 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
uint32_t delta = 0x9E3779B9, sum = delta * 32;
|
|
|
|
|
|
|
|
for( i = 0; i < 32; i++ )
|
|
|
|
{
|
|
|
|
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
|
|
|
|
sum -= delta;
|
|
|
|
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
|
|
|
|
}
|
2009-01-04 19:15:48 +01:00
|
|
|
}
|
|
|
|
|
2021-08-03 17:42:42 +02:00
|
|
|
MBEDTLS_PUT_UINT32_BE( v0, output, 0 );
|
|
|
|
MBEDTLS_PUT_UINT32_BE( v1, output, 4 );
|
2010-03-18 22:21:02 +01:00
|
|
|
|
|
|
|
return( 0 );
|
2009-01-04 19:15:48 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
2011-04-19 16:29:23 +02:00
|
|
|
/*
|
|
|
|
* XTEA-CBC buffer encryption/decryption
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, int mode, size_t length,
|
2013-06-25 16:25:17 +02:00
|
|
|
unsigned char iv[8], const unsigned char *input,
|
2011-04-19 16:29:23 +02:00
|
|
|
unsigned char *output)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char temp[8];
|
|
|
|
|
2013-06-25 16:25:17 +02:00
|
|
|
if( length % 8 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH );
|
2011-04-19 16:29:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mode == MBEDTLS_XTEA_DECRYPT )
|
2011-04-19 16:29:23 +02:00
|
|
|
{
|
|
|
|
while( length > 0 )
|
|
|
|
{
|
|
|
|
memcpy( temp, input, 8 );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_xtea_crypt_ecb( ctx, mode, input, output );
|
2011-04-19 16:29:23 +02:00
|
|
|
|
2014-06-17 16:39:18 +02:00
|
|
|
for( i = 0; i < 8; i++ )
|
2011-04-19 16:29:23 +02:00
|
|
|
output[i] = (unsigned char)( output[i] ^ iv[i] );
|
|
|
|
|
|
|
|
memcpy( iv, temp, 8 );
|
|
|
|
|
|
|
|
input += 8;
|
|
|
|
output += 8;
|
|
|
|
length -= 8;
|
|
|
|
}
|
2013-06-25 16:25:17 +02:00
|
|
|
}
|
|
|
|
else
|
2011-04-19 16:29:23 +02:00
|
|
|
{
|
|
|
|
while( length > 0 )
|
|
|
|
{
|
|
|
|
for( i = 0; i < 8; i++ )
|
|
|
|
output[i] = (unsigned char)( input[i] ^ iv[i] );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_xtea_crypt_ecb( ctx, mode, output, output );
|
2011-04-19 16:29:23 +02:00
|
|
|
memcpy( iv, output, 8 );
|
2013-06-25 16:25:17 +02:00
|
|
|
|
2011-04-19 16:29:23 +02:00
|
|
|
input += 8;
|
|
|
|
output += 8;
|
|
|
|
length -= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
|
|
|
#endif /* !MBEDTLS_XTEA_ALT */
|
2011-04-19 16:29:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2009-01-04 19:15:48 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XTEA tests vectors (non-official)
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const unsigned char xtea_test_key[6][16] =
|
|
|
|
{
|
2009-05-03 14:54:07 +02:00
|
|
|
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
|
|
|
|
0x0c, 0x0d, 0x0e, 0x0f },
|
|
|
|
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
|
|
|
|
0x0c, 0x0d, 0x0e, 0x0f },
|
|
|
|
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
|
|
|
|
0x0c, 0x0d, 0x0e, 0x0f },
|
|
|
|
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00 },
|
|
|
|
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00 },
|
|
|
|
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00 }
|
2009-01-04 19:15:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char xtea_test_pt[6][8] =
|
|
|
|
{
|
|
|
|
{ 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
|
|
|
|
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
|
|
|
|
{ 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
|
|
|
|
{ 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
|
|
|
|
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
|
|
|
|
{ 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char xtea_test_ct[6][8] =
|
|
|
|
{
|
|
|
|
{ 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
|
|
|
|
{ 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
|
|
|
|
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
|
|
|
|
{ 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
|
|
|
|
{ 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
|
|
|
|
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checkup routine
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_xtea_self_test( int verbose )
|
2009-01-04 19:15:48 +01:00
|
|
|
{
|
2014-06-18 11:12:03 +02:00
|
|
|
int i, ret = 0;
|
2009-01-04 19:15:48 +01:00
|
|
|
unsigned char buf[8];
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_xtea_context ctx;
|
2009-01-04 19:15:48 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_xtea_init( &ctx );
|
2009-01-04 19:15:48 +01:00
|
|
|
for( i = 0; i < 6; i++ )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " XTEA test #%d: ", i + 1 );
|
2009-01-04 19:15:48 +01:00
|
|
|
|
|
|
|
memcpy( buf, xtea_test_pt[i], 8 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_xtea_setup( &ctx, xtea_test_key[i] );
|
|
|
|
mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf );
|
2009-01-04 19:15:48 +01:00
|
|
|
|
|
|
|
if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "failed\n" );
|
2009-01-04 19:15:48 +01:00
|
|
|
|
2014-06-18 11:12:03 +02:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
2009-01-04 19:15:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "passed\n" );
|
2009-01-04 19:15:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "\n" );
|
2009-01-04 19:15:48 +01:00
|
|
|
|
2014-06-18 11:12:03 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_xtea_free( &ctx );
|
2014-06-18 11:12:03 +02:00
|
|
|
|
|
|
|
return( ret );
|
2009-01-04 19:15:48 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2009-01-04 19:15:48 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_XTEA_C */
|