00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "polarssl/config.h"
00027
00028 #if defined(POLARSSL_XTEA_C)
00029
00030 #include "polarssl/xtea.h"
00031
00032 #if !defined(POLARSSL_XTEA_ALT)
00033
00034
00035
00036
00037 #ifndef GET_UINT32_BE
00038 #define GET_UINT32_BE(n,b,i) \
00039 { \
00040 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
00041 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
00042 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
00043 | ( (uint32_t) (b)[(i) + 3] ); \
00044 }
00045 #endif
00046
00047 #ifndef PUT_UINT32_BE
00048 #define PUT_UINT32_BE(n,b,i) \
00049 { \
00050 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
00051 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
00052 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
00053 (b)[(i) + 3] = (unsigned char) ( (n) ); \
00054 }
00055 #endif
00056
00057
00058
00059
00060 void xtea_setup( xtea_context *ctx, const unsigned char key[16] )
00061 {
00062 int i;
00063
00064 memset(ctx, 0, sizeof(xtea_context));
00065
00066 for( i = 0; i < 4; i++ )
00067 {
00068 GET_UINT32_BE( ctx->k[i], key, i << 2 );
00069 }
00070 }
00071
00072
00073
00074
00075 int xtea_crypt_ecb( xtea_context *ctx, int mode,
00076 const unsigned char input[8], unsigned char output[8])
00077 {
00078 uint32_t *k, v0, v1, i;
00079
00080 k = ctx->k;
00081
00082 GET_UINT32_BE( v0, input, 0 );
00083 GET_UINT32_BE( v1, input, 4 );
00084
00085 if( mode == XTEA_ENCRYPT )
00086 {
00087 uint32_t sum = 0, delta = 0x9E3779B9;
00088
00089 for( i = 0; i < 32; i++ )
00090 {
00091 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
00092 sum += delta;
00093 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
00094 }
00095 }
00096 else
00097 {
00098 uint32_t delta = 0x9E3779B9, sum = delta * 32;
00099
00100 for( i = 0; i < 32; i++ )
00101 {
00102 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
00103 sum -= delta;
00104 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
00105 }
00106 }
00107
00108 PUT_UINT32_BE( v0, output, 0 );
00109 PUT_UINT32_BE( v1, output, 4 );
00110
00111 return( 0 );
00112 }
00113
00114 #if defined(POLARSSL_CIPHER_MODE_CBC)
00115
00116
00117
00118 int xtea_crypt_cbc( xtea_context *ctx, int mode, size_t length,
00119 unsigned char iv[8], const unsigned char *input,
00120 unsigned char *output)
00121 {
00122 int i;
00123 unsigned char temp[8];
00124
00125 if( length % 8 )
00126 return( POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH );
00127
00128 if( mode == XTEA_DECRYPT )
00129 {
00130 while( length > 0 )
00131 {
00132 memcpy( temp, input, 8 );
00133 xtea_crypt_ecb( ctx, mode, input, output );
00134
00135 for(i = 0; i < 8; i++)
00136 output[i] = (unsigned char)( output[i] ^ iv[i] );
00137
00138 memcpy( iv, temp, 8 );
00139
00140 input += 8;
00141 output += 8;
00142 length -= 8;
00143 }
00144 }
00145 else
00146 {
00147 while( length > 0 )
00148 {
00149 for( i = 0; i < 8; i++ )
00150 output[i] = (unsigned char)( input[i] ^ iv[i] );
00151
00152 xtea_crypt_ecb( ctx, mode, output, output );
00153 memcpy( iv, output, 8 );
00154
00155 input += 8;
00156 output += 8;
00157 length -= 8;
00158 }
00159 }
00160
00161 return( 0 );
00162 }
00163 #endif
00164 #endif
00165
00166 #if defined(POLARSSL_SELF_TEST)
00167
00168 #include <string.h>
00169 #include <stdio.h>
00170
00171
00172
00173
00174
00175 static const unsigned char xtea_test_key[6][16] =
00176 {
00177 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
00178 0x0c, 0x0d, 0x0e, 0x0f },
00179 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
00180 0x0c, 0x0d, 0x0e, 0x0f },
00181 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
00182 0x0c, 0x0d, 0x0e, 0x0f },
00183 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00184 0x00, 0x00, 0x00, 0x00 },
00185 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00186 0x00, 0x00, 0x00, 0x00 },
00187 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00188 0x00, 0x00, 0x00, 0x00 }
00189 };
00190
00191 static const unsigned char xtea_test_pt[6][8] =
00192 {
00193 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
00194 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
00195 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
00196 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
00197 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
00198 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
00199 };
00200
00201 static const unsigned char xtea_test_ct[6][8] =
00202 {
00203 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
00204 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
00205 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
00206 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
00207 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
00208 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
00209 };
00210
00211
00212
00213
00214 int xtea_self_test( int verbose )
00215 {
00216 int i;
00217 unsigned char buf[8];
00218 xtea_context ctx;
00219
00220 for( i = 0; i < 6; i++ )
00221 {
00222 if( verbose != 0 )
00223 printf( " XTEA test #%d: ", i + 1 );
00224
00225 memcpy( buf, xtea_test_pt[i], 8 );
00226
00227 xtea_setup( &ctx, xtea_test_key[i] );
00228 xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, buf, buf );
00229
00230 if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
00231 {
00232 if( verbose != 0 )
00233 printf( "failed\n" );
00234
00235 return( 1 );
00236 }
00237
00238 if( verbose != 0 )
00239 printf( "passed\n" );
00240 }
00241
00242 if( verbose != 0 )
00243 printf( "\n" );
00244
00245 return( 0 );
00246 }
00247
00248 #endif
00249
00250 #endif