xtea.c 7.47 KB
Newer Older
1 2 3
/*
 *  An 32-bit implementation of the XTEA algorithm
 *
4
 *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
5
 *
6
 *  This file is part of mbed TLS (https://tls.mbed.org)
7
 *
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

23
#if !defined(MBEDTLS_CONFIG_FILE)
24
#include "mbedtls/config.h"
25
#else
26
#include MBEDTLS_CONFIG_FILE
27
#endif
28

29
#if defined(MBEDTLS_XTEA_C)
30

31
#include "mbedtls/xtea.h"
32

33 34
#include <string.h>

35 36
#if defined(MBEDTLS_SELF_TEST)
#if defined(MBEDTLS_PLATFORM_C)
37
#include "mbedtls/platform.h"
38
#else
39
#include <stdio.h>
40 41 42
#define mbedtls_printf printf
#endif /* MBEDTLS_PLATFORM_C */
#endif /* MBEDTLS_SELF_TEST */
43

44
#if !defined(MBEDTLS_XTEA_ALT)
45

46
/* Implementation that should never be optimized out by the compiler */
47
static void mbedtls_zeroize( void *v, size_t n ) {
48 49 50
    volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}

51 52 53
/*
 * 32-bit integer manipulation macros (big endian)
 */
54 55
#ifndef GET_UINT32_BE
#define GET_UINT32_BE(n,b,i)                            \
56
{                                                       \
57 58 59 60
    (n) = ( (uint32_t) (b)[(i)    ] << 24 )             \
        | ( (uint32_t) (b)[(i) + 1] << 16 )             \
        | ( (uint32_t) (b)[(i) + 2] <<  8 )             \
        | ( (uint32_t) (b)[(i) + 3]       );            \
61 62 63
}
#endif

64 65
#ifndef PUT_UINT32_BE
#define PUT_UINT32_BE(n,b,i)                            \
66 67 68 69 70 71 72 73
{                                                       \
    (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \
    (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \
    (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \
    (b)[(i) + 3] = (unsigned char) ( (n)       );       \
}
#endif

74
void mbedtls_xtea_init( mbedtls_xtea_context *ctx )
75
{
76
    memset( ctx, 0, sizeof( mbedtls_xtea_context ) );
77 78
}

79
void mbedtls_xtea_free( mbedtls_xtea_context *ctx )
80 81 82 83
{
    if( ctx == NULL )
        return;

84
    mbedtls_zeroize( ctx, sizeof( mbedtls_xtea_context ) );
85 86
}

87 88 89
/*
 * XTEA key schedule
 */
90
void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] )
91 92 93
{
    int i;

94
    memset( ctx, 0, sizeof(mbedtls_xtea_context) );
95 96 97

    for( i = 0; i < 4; i++ )
    {
98
        GET_UINT32_BE( ctx->k[i], key, i << 2 );
99 100 101 102 103 104
    }
}

/*
 * XTEA encrypt function
 */
105
int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, int mode,
106
                    const unsigned char input[8], unsigned char output[8])
107
{
108
    uint32_t *k, v0, v1, i;
109 110

    k = ctx->k;
111

112 113
    GET_UINT32_BE( v0, input, 0 );
    GET_UINT32_BE( v1, input, 4 );
114

115
    if( mode == MBEDTLS_XTEA_ENCRYPT )
116
    {
117 118 119 120 121 122 123 124
        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]);
        }
125
    }
126
    else /* MBEDTLS_XTEA_DECRYPT */
127
    {
128 129 130 131 132 133 134 135
        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]);
        }
136 137
    }

138 139
    PUT_UINT32_BE( v0, output, 0 );
    PUT_UINT32_BE( v1, output, 4 );
140 141

    return( 0 );
142 143
}

144
#if defined(MBEDTLS_CIPHER_MODE_CBC)
145 146 147
/*
 * XTEA-CBC buffer encryption/decryption
 */
148
int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, int mode, size_t length,
149
                    unsigned char iv[8], const unsigned char *input,
150 151 152 153 154
                    unsigned char *output)
{
    int i;
    unsigned char temp[8];

155
    if( length % 8 )
156
        return( MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH );
157

158
    if( mode == MBEDTLS_XTEA_DECRYPT )
159 160 161 162
    {
        while( length > 0 )
        {
            memcpy( temp, input, 8 );
163
            mbedtls_xtea_crypt_ecb( ctx, mode, input, output );
164

165
            for( i = 0; i < 8; i++ )
166 167 168 169 170 171 172 173
                output[i] = (unsigned char)( output[i] ^ iv[i] );

            memcpy( iv, temp, 8 );

            input  += 8;
            output += 8;
            length -= 8;
        }
174 175
    }
    else
176 177 178 179 180 181
    {
        while( length > 0 )
        {
            for( i = 0; i < 8; i++ )
                output[i] = (unsigned char)( input[i] ^ iv[i] );

182
            mbedtls_xtea_crypt_ecb( ctx, mode, output, output );
183
            memcpy( iv, output, 8 );
184

185 186 187 188 189 190 191 192
            input  += 8;
            output += 8;
            length -= 8;
        }
    }

    return( 0 );
}
193 194
#endif /* MBEDTLS_CIPHER_MODE_CBC */
#endif /* !MBEDTLS_XTEA_ALT */
195

196
#if defined(MBEDTLS_SELF_TEST)
197 198 199 200 201 202 203

/*
 * XTEA tests vectors (non-official)
 */

static const unsigned char xtea_test_key[6][16] =
{
204 205 206 207 208 209 210 211 212 213 214 215
   { 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 }
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
};

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
 */
241
int mbedtls_xtea_self_test( int verbose )
242
{
243
    int i, ret = 0;
244
    unsigned char buf[8];
245
    mbedtls_xtea_context ctx;
246

247
    mbedtls_xtea_init( &ctx );
248 249 250
    for( i = 0; i < 6; i++ )
    {
        if( verbose != 0 )
251
            mbedtls_printf( "  XTEA test #%d: ", i + 1 );
252 253 254

        memcpy( buf, xtea_test_pt[i], 8 );

255 256
        mbedtls_xtea_setup( &ctx, xtea_test_key[i] );
        mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf );
257 258 259 260

        if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
        {
            if( verbose != 0 )
261
                mbedtls_printf( "failed\n" );
262

263 264
            ret = 1;
            goto exit;
265 266 267
        }

        if( verbose != 0 )
268
            mbedtls_printf( "passed\n" );
269 270 271
    }

    if( verbose != 0 )
272
        mbedtls_printf( "\n" );
273

274
exit:
275
    mbedtls_xtea_free( &ctx );
276 277

    return( ret );
278 279
}

280
#endif /* MBEDTLS_SELF_TEST */
281

282
#endif /* MBEDTLS_XTEA_C */