xtea.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #include <stdio.h>
  34. #define mbedtls_printf printf
  35. #endif /* MBEDTLS_PLATFORM_C */
  36. #endif /* MBEDTLS_SELF_TEST */
  37. #if !defined(MBEDTLS_XTEA_ALT)
  38. /* Implementation that should never be optimized out by the compiler */
  39. static void mbedtls_zeroize( void *v, size_t n ) {
  40. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  41. }
  42. /*
  43. * 32-bit integer manipulation macros (big endian)
  44. */
  45. #ifndef GET_UINT32_BE
  46. #define GET_UINT32_BE(n,b,i) \
  47. { \
  48. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  49. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  50. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  51. | ( (uint32_t) (b)[(i) + 3] ); \
  52. }
  53. #endif
  54. #ifndef PUT_UINT32_BE
  55. #define PUT_UINT32_BE(n,b,i) \
  56. { \
  57. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  58. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  59. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  60. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  61. }
  62. #endif
  63. void mbedtls_xtea_init( mbedtls_xtea_context *ctx )
  64. {
  65. memset( ctx, 0, sizeof( mbedtls_xtea_context ) );
  66. }
  67. void mbedtls_xtea_free( mbedtls_xtea_context *ctx )
  68. {
  69. if( ctx == NULL )
  70. return;
  71. mbedtls_zeroize( ctx, sizeof( mbedtls_xtea_context ) );
  72. }
  73. /*
  74. * XTEA key schedule
  75. */
  76. void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] )
  77. {
  78. int i;
  79. memset( ctx, 0, sizeof(mbedtls_xtea_context) );
  80. for( i = 0; i < 4; i++ )
  81. {
  82. GET_UINT32_BE( ctx->k[i], key, i << 2 );
  83. }
  84. }
  85. /*
  86. * XTEA encrypt function
  87. */
  88. int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, int mode,
  89. const unsigned char input[8], unsigned char output[8])
  90. {
  91. uint32_t *k, v0, v1, i;
  92. k = ctx->k;
  93. GET_UINT32_BE( v0, input, 0 );
  94. GET_UINT32_BE( v1, input, 4 );
  95. if( mode == MBEDTLS_XTEA_ENCRYPT )
  96. {
  97. uint32_t sum = 0, delta = 0x9E3779B9;
  98. for( i = 0; i < 32; i++ )
  99. {
  100. v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  101. sum += delta;
  102. v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  103. }
  104. }
  105. else /* MBEDTLS_XTEA_DECRYPT */
  106. {
  107. uint32_t delta = 0x9E3779B9, sum = delta * 32;
  108. for( i = 0; i < 32; i++ )
  109. {
  110. v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  111. sum -= delta;
  112. v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  113. }
  114. }
  115. PUT_UINT32_BE( v0, output, 0 );
  116. PUT_UINT32_BE( v1, output, 4 );
  117. return( 0 );
  118. }
  119. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  120. /*
  121. * XTEA-CBC buffer encryption/decryption
  122. */
  123. int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, int mode, size_t length,
  124. unsigned char iv[8], const unsigned char *input,
  125. unsigned char *output)
  126. {
  127. int i;
  128. unsigned char temp[8];
  129. if( length % 8 )
  130. return( MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH );
  131. if( mode == MBEDTLS_XTEA_DECRYPT )
  132. {
  133. while( length > 0 )
  134. {
  135. memcpy( temp, input, 8 );
  136. mbedtls_xtea_crypt_ecb( ctx, mode, input, output );
  137. for( i = 0; i < 8; i++ )
  138. output[i] = (unsigned char)( output[i] ^ iv[i] );
  139. memcpy( iv, temp, 8 );
  140. input += 8;
  141. output += 8;
  142. length -= 8;
  143. }
  144. }
  145. else
  146. {
  147. while( length > 0 )
  148. {
  149. for( i = 0; i < 8; i++ )
  150. output[i] = (unsigned char)( input[i] ^ iv[i] );
  151. mbedtls_xtea_crypt_ecb( ctx, mode, output, output );
  152. memcpy( iv, output, 8 );
  153. input += 8;
  154. output += 8;
  155. length -= 8;
  156. }
  157. }
  158. return( 0 );
  159. }
  160. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  161. #endif /* !MBEDTLS_XTEA_ALT */
  162. #if defined(MBEDTLS_SELF_TEST)
  163. /*
  164. * XTEA tests vectors (non-official)
  165. */
  166. static const unsigned char xtea_test_key[6][16] =
  167. {
  168. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  169. 0x0c, 0x0d, 0x0e, 0x0f },
  170. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  171. 0x0c, 0x0d, 0x0e, 0x0f },
  172. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  173. 0x0c, 0x0d, 0x0e, 0x0f },
  174. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  175. 0x00, 0x00, 0x00, 0x00 },
  176. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  177. 0x00, 0x00, 0x00, 0x00 },
  178. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  179. 0x00, 0x00, 0x00, 0x00 }
  180. };
  181. static const unsigned char xtea_test_pt[6][8] =
  182. {
  183. { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
  184. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  185. { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
  186. { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
  187. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  188. { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
  189. };
  190. static const unsigned char xtea_test_ct[6][8] =
  191. {
  192. { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
  193. { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
  194. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  195. { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
  196. { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
  197. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
  198. };
  199. /*
  200. * Checkup routine
  201. */
  202. int mbedtls_xtea_self_test( int verbose )
  203. {
  204. int i, ret = 0;
  205. unsigned char buf[8];
  206. mbedtls_xtea_context ctx;
  207. mbedtls_xtea_init( &ctx );
  208. for( i = 0; i < 6; i++ )
  209. {
  210. if( verbose != 0 )
  211. mbedtls_printf( " XTEA test #%d: ", i + 1 );
  212. memcpy( buf, xtea_test_pt[i], 8 );
  213. mbedtls_xtea_setup( &ctx, xtea_test_key[i] );
  214. mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf );
  215. if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
  216. {
  217. if( verbose != 0 )
  218. mbedtls_printf( "failed\n" );
  219. ret = 1;
  220. goto exit;
  221. }
  222. if( verbose != 0 )
  223. mbedtls_printf( "passed\n" );
  224. }
  225. if( verbose != 0 )
  226. mbedtls_printf( "\n" );
  227. exit:
  228. mbedtls_xtea_free( &ctx );
  229. return( ret );
  230. }
  231. #endif /* MBEDTLS_SELF_TEST */
  232. #endif /* MBEDTLS_XTEA_C */