xtea.c 7.2 KB

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