xtea.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * An 32-bit implementation of the XTEA algorithm
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_XTEA_C)
  27. #include "mbedtls/xtea.h"
  28. #include <string.h>
  29. #if defined(MBEDTLS_SELF_TEST)
  30. #if defined(MBEDTLS_PLATFORM_C)
  31. #include "mbedtls/platform.h"
  32. #else
  33. #ifdef PRINTF_STDLIB
  34. #include <stdio.h>
  35. #endif
  36. #ifdef PRINTF_CUSTOM
  37. #include "tinystdio.h"
  38. #endif
  39. #define mbedtls_printf printf
  40. #endif /* MBEDTLS_PLATFORM_C */
  41. #endif /* MBEDTLS_SELF_TEST */
  42. #if !defined(MBEDTLS_XTEA_ALT)
  43. /* Implementation that should never be optimized out by the compiler */
  44. static void mbedtls_zeroize( void *v, size_t n ) {
  45. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  46. }
  47. /*
  48. * 32-bit integer manipulation macros (big endian)
  49. */
  50. #ifndef GET_UINT32_BE
  51. #define GET_UINT32_BE(n,b,i) \
  52. { \
  53. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  54. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  55. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  56. | ( (uint32_t) (b)[(i) + 3] ); \
  57. }
  58. #endif
  59. #ifndef PUT_UINT32_BE
  60. #define PUT_UINT32_BE(n,b,i) \
  61. { \
  62. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  63. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  64. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  65. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  66. }
  67. #endif
  68. void mbedtls_xtea_init( mbedtls_xtea_context *ctx )
  69. {
  70. memset( ctx, 0, sizeof( mbedtls_xtea_context ) );
  71. }
  72. void mbedtls_xtea_free( mbedtls_xtea_context *ctx )
  73. {
  74. if( ctx == NULL )
  75. return;
  76. mbedtls_zeroize( ctx, sizeof( mbedtls_xtea_context ) );
  77. }
  78. /*
  79. * XTEA key schedule
  80. */
  81. void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] )
  82. {
  83. int i;
  84. memset( ctx, 0, sizeof(mbedtls_xtea_context) );
  85. for( i = 0; i < 4; i++ )
  86. {
  87. GET_UINT32_BE( ctx->k[i], key, i << 2 );
  88. }
  89. }
  90. /*
  91. * XTEA encrypt function
  92. */
  93. int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, int mode,
  94. const unsigned char input[8], unsigned char output[8])
  95. {
  96. uint32_t *k, v0, v1, i;
  97. k = ctx->k;
  98. GET_UINT32_BE( v0, input, 0 );
  99. GET_UINT32_BE( v1, input, 4 );
  100. if( mode == MBEDTLS_XTEA_ENCRYPT )
  101. {
  102. uint32_t sum = 0, delta = 0x9E3779B9;
  103. for( i = 0; i < 32; i++ )
  104. {
  105. v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  106. sum += delta;
  107. v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  108. }
  109. }
  110. else /* MBEDTLS_XTEA_DECRYPT */
  111. {
  112. uint32_t delta = 0x9E3779B9, sum = delta * 32;
  113. for( i = 0; i < 32; i++ )
  114. {
  115. v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  116. sum -= delta;
  117. v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  118. }
  119. }
  120. PUT_UINT32_BE( v0, output, 0 );
  121. PUT_UINT32_BE( v1, output, 4 );
  122. return( 0 );
  123. }
  124. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  125. /*
  126. * XTEA-CBC buffer encryption/decryption
  127. */
  128. int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, int mode, size_t length,
  129. unsigned char iv[8], const unsigned char *input,
  130. unsigned char *output)
  131. {
  132. int i;
  133. unsigned char temp[8];
  134. if( length % 8 )
  135. return( MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH );
  136. if( mode == MBEDTLS_XTEA_DECRYPT )
  137. {
  138. while( length > 0 )
  139. {
  140. memcpy( temp, input, 8 );
  141. mbedtls_xtea_crypt_ecb( ctx, mode, input, output );
  142. for( i = 0; i < 8; i++ )
  143. output[i] = (unsigned char)( output[i] ^ iv[i] );
  144. memcpy( iv, temp, 8 );
  145. input += 8;
  146. output += 8;
  147. length -= 8;
  148. }
  149. }
  150. else
  151. {
  152. while( length > 0 )
  153. {
  154. for( i = 0; i < 8; i++ )
  155. output[i] = (unsigned char)( input[i] ^ iv[i] );
  156. mbedtls_xtea_crypt_ecb( ctx, mode, output, output );
  157. memcpy( iv, output, 8 );
  158. input += 8;
  159. output += 8;
  160. length -= 8;
  161. }
  162. }
  163. return( 0 );
  164. }
  165. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  166. #endif /* !MBEDTLS_XTEA_ALT */
  167. #if defined(MBEDTLS_SELF_TEST)
  168. /*
  169. * XTEA tests vectors (non-official)
  170. */
  171. static const unsigned char xtea_test_key[6][16] =
  172. {
  173. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  174. 0x0c, 0x0d, 0x0e, 0x0f },
  175. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  176. 0x0c, 0x0d, 0x0e, 0x0f },
  177. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  178. 0x0c, 0x0d, 0x0e, 0x0f },
  179. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  180. 0x00, 0x00, 0x00, 0x00 },
  181. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  182. 0x00, 0x00, 0x00, 0x00 },
  183. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  184. 0x00, 0x00, 0x00, 0x00 }
  185. };
  186. static const unsigned char xtea_test_pt[6][8] =
  187. {
  188. { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
  189. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  190. { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
  191. { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
  192. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  193. { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
  194. };
  195. static const unsigned char xtea_test_ct[6][8] =
  196. {
  197. { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
  198. { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
  199. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  200. { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
  201. { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
  202. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
  203. };
  204. /*
  205. * Checkup routine
  206. */
  207. int mbedtls_xtea_self_test( int verbose )
  208. {
  209. int i, ret = 0;
  210. unsigned char buf[8];
  211. mbedtls_xtea_context ctx;
  212. mbedtls_xtea_init( &ctx );
  213. for( i = 0; i < 6; i++ )
  214. {
  215. if( verbose != 0 )
  216. mbedtls_printf( " XTEA test #%d: ", i + 1 );
  217. memcpy( buf, xtea_test_pt[i], 8 );
  218. mbedtls_xtea_setup( &ctx, xtea_test_key[i] );
  219. mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf );
  220. if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
  221. {
  222. if( verbose != 0 )
  223. mbedtls_printf( "failed\n" );
  224. ret = 1;
  225. goto exit;
  226. }
  227. if( verbose != 0 )
  228. mbedtls_printf( "passed\n" );
  229. }
  230. if( verbose != 0 )
  231. mbedtls_printf( "\n" );
  232. exit:
  233. mbedtls_xtea_free( &ctx );
  234. return( ret );
  235. }
  236. #endif /* MBEDTLS_SELF_TEST */
  237. #endif /* MBEDTLS_XTEA_C */