mbedtls ported to mbed-classic
Fork of mbedtls by
Embed:
(wiki syntax)
Show/hide line numbers
xtea.c
00001 /* 00002 * An 32-bit implementation of the XTEA algorithm 00003 * 00004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 00005 * SPDX-License-Identifier: Apache-2.0 00006 * 00007 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00008 * not use this file except in compliance with the License. 00009 * You may obtain a copy of the License at 00010 * 00011 * http://www.apache.org/licenses/LICENSE-2.0 00012 * 00013 * Unless required by applicable law or agreed to in writing, software 00014 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00015 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00016 * See the License for the specific language governing permissions and 00017 * limitations under the License. 00018 * 00019 * This file is part of mbed TLS (https://tls.mbed.org) 00020 */ 00021 00022 #if !defined(MBEDTLS_CONFIG_FILE) 00023 #include "mbedtls/config.h" 00024 #else 00025 #include MBEDTLS_CONFIG_FILE 00026 #endif 00027 00028 #if defined(MBEDTLS_XTEA_C) 00029 00030 #include "mbedtls/xtea.h" 00031 00032 #include <string.h> 00033 00034 #if defined(MBEDTLS_SELF_TEST) 00035 #if defined(MBEDTLS_PLATFORM_C) 00036 #include "mbedtls/platform.h" 00037 #else 00038 #include <stdio.h> 00039 #define mbedtls_printf printf 00040 #endif /* MBEDTLS_PLATFORM_C */ 00041 #endif /* MBEDTLS_SELF_TEST */ 00042 00043 #if !defined(MBEDTLS_XTEA_ALT) 00044 00045 /* Implementation that should never be optimized out by the compiler */ 00046 static void mbedtls_zeroize( void *v, size_t n ) { 00047 volatile unsigned char *p = v; while( n-- ) *p++ = 0; 00048 } 00049 00050 /* 00051 * 32-bit integer manipulation macros (big endian) 00052 */ 00053 #ifndef GET_UINT32_BE 00054 #define GET_UINT32_BE(n,b,i) \ 00055 { \ 00056 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ 00057 | ( (uint32_t) (b)[(i) + 1] << 16 ) \ 00058 | ( (uint32_t) (b)[(i) + 2] << 8 ) \ 00059 | ( (uint32_t) (b)[(i) + 3] ); \ 00060 } 00061 #endif 00062 00063 #ifndef PUT_UINT32_BE 00064 #define PUT_UINT32_BE(n,b,i) \ 00065 { \ 00066 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ 00067 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ 00068 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ 00069 (b)[(i) + 3] = (unsigned char) ( (n) ); \ 00070 } 00071 #endif 00072 00073 void mbedtls_xtea_init( mbedtls_xtea_context *ctx ) 00074 { 00075 memset( ctx, 0, sizeof( mbedtls_xtea_context ) ); 00076 } 00077 00078 void mbedtls_xtea_free( mbedtls_xtea_context *ctx ) 00079 { 00080 if( ctx == NULL ) 00081 return; 00082 00083 mbedtls_zeroize( ctx, sizeof( mbedtls_xtea_context ) ); 00084 } 00085 00086 /* 00087 * XTEA key schedule 00088 */ 00089 void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] ) 00090 { 00091 int i; 00092 00093 memset( ctx, 0, sizeof(mbedtls_xtea_context) ); 00094 00095 for( i = 0; i < 4; i++ ) 00096 { 00097 GET_UINT32_BE( ctx->k [i], key, i << 2 ); 00098 } 00099 } 00100 00101 /* 00102 * XTEA encrypt function 00103 */ 00104 int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, int mode, 00105 const unsigned char input[8], unsigned char output[8]) 00106 { 00107 uint32_t *k, v0, v1, i; 00108 00109 k = ctx->k ; 00110 00111 GET_UINT32_BE( v0, input, 0 ); 00112 GET_UINT32_BE( v1, input, 4 ); 00113 00114 if( mode == MBEDTLS_XTEA_ENCRYPT ) 00115 { 00116 uint32_t sum = 0, delta = 0x9E3779B9; 00117 00118 for( i = 0; i < 32; i++ ) 00119 { 00120 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); 00121 sum += delta; 00122 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]); 00123 } 00124 } 00125 else /* MBEDTLS_XTEA_DECRYPT */ 00126 { 00127 uint32_t delta = 0x9E3779B9, sum = delta * 32; 00128 00129 for( i = 0; i < 32; i++ ) 00130 { 00131 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]); 00132 sum -= delta; 00133 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); 00134 } 00135 } 00136 00137 PUT_UINT32_BE( v0, output, 0 ); 00138 PUT_UINT32_BE( v1, output, 4 ); 00139 00140 return( 0 ); 00141 } 00142 00143 #if defined(MBEDTLS_CIPHER_MODE_CBC) 00144 /* 00145 * XTEA-CBC buffer encryption/decryption 00146 */ 00147 int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, int mode, size_t length, 00148 unsigned char iv[8], const unsigned char *input, 00149 unsigned char *output) 00150 { 00151 int i; 00152 unsigned char temp[8]; 00153 00154 if( length % 8 ) 00155 return( MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH ); 00156 00157 if( mode == MBEDTLS_XTEA_DECRYPT ) 00158 { 00159 while( length > 0 ) 00160 { 00161 memcpy( temp, input, 8 ); 00162 mbedtls_xtea_crypt_ecb( ctx, mode, input, output ); 00163 00164 for( i = 0; i < 8; i++ ) 00165 output[i] = (unsigned char)( output[i] ^ iv[i] ); 00166 00167 memcpy( iv, temp, 8 ); 00168 00169 input += 8; 00170 output += 8; 00171 length -= 8; 00172 } 00173 } 00174 else 00175 { 00176 while( length > 0 ) 00177 { 00178 for( i = 0; i < 8; i++ ) 00179 output[i] = (unsigned char)( input[i] ^ iv[i] ); 00180 00181 mbedtls_xtea_crypt_ecb( ctx, mode, output, output ); 00182 memcpy( iv, output, 8 ); 00183 00184 input += 8; 00185 output += 8; 00186 length -= 8; 00187 } 00188 } 00189 00190 return( 0 ); 00191 } 00192 #endif /* MBEDTLS_CIPHER_MODE_CBC */ 00193 #endif /* !MBEDTLS_XTEA_ALT */ 00194 00195 #if defined(MBEDTLS_SELF_TEST) 00196 00197 /* 00198 * XTEA tests vectors (non-official) 00199 */ 00200 00201 static const unsigned char xtea_test_key[6][16] = 00202 { 00203 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 00204 0x0c, 0x0d, 0x0e, 0x0f }, 00205 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 00206 0x0c, 0x0d, 0x0e, 0x0f }, 00207 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 00208 0x0c, 0x0d, 0x0e, 0x0f }, 00209 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00210 0x00, 0x00, 0x00, 0x00 }, 00211 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00212 0x00, 0x00, 0x00, 0x00 }, 00213 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00214 0x00, 0x00, 0x00, 0x00 } 00215 }; 00216 00217 static const unsigned char xtea_test_pt[6][8] = 00218 { 00219 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, 00220 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, 00221 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f }, 00222 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, 00223 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, 00224 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 } 00225 }; 00226 00227 static const unsigned char xtea_test_ct[6][8] = 00228 { 00229 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 }, 00230 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 }, 00231 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, 00232 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 }, 00233 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d }, 00234 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 } 00235 }; 00236 00237 /* 00238 * Checkup routine 00239 */ 00240 int mbedtls_xtea_self_test( int verbose ) 00241 { 00242 int i, ret = 0; 00243 unsigned char buf[8]; 00244 mbedtls_xtea_context ctx; 00245 00246 mbedtls_xtea_init( &ctx ); 00247 for( i = 0; i < 6; i++ ) 00248 { 00249 if( verbose != 0 ) 00250 mbedtls_printf( " XTEA test #%d: ", i + 1 ); 00251 00252 memcpy( buf, xtea_test_pt[i], 8 ); 00253 00254 mbedtls_xtea_setup( &ctx, xtea_test_key[i] ); 00255 mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf ); 00256 00257 if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 ) 00258 { 00259 if( verbose != 0 ) 00260 mbedtls_printf( "failed\n" ); 00261 00262 ret = 1; 00263 goto exit; 00264 } 00265 00266 if( verbose != 0 ) 00267 mbedtls_printf( "passed\n" ); 00268 } 00269 00270 if( verbose != 0 ) 00271 mbedtls_printf( "\n" ); 00272 00273 exit: 00274 mbedtls_xtea_free( &ctx ); 00275 00276 return( ret ); 00277 } 00278 00279 #endif /* MBEDTLS_SELF_TEST */ 00280 00281 #endif /* MBEDTLS_XTEA_C */
Generated on Tue Jul 12 2022 12:52:50 by 1.7.2