xtea.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #if !defined(MBEDTLS_CONFIG_FILE)
  24. #include "mbedtls/config.h"
  25. #else
  26. #include MBEDTLS_CONFIG_FILE
  27. #endif
  28. #if defined(MBEDTLS_XTEA_C)
  29. #include "mbedtls/xtea.h"
  30. #include <string.h>
  31. #if defined(MBEDTLS_SELF_TEST)
  32. #if defined(MBEDTLS_PLATFORM_C)
  33. #include "mbedtls/platform.h"
  34. #else
  35. #include <stdio.h>
  36. #define mbedtls_printf printf
  37. #endif /* MBEDTLS_PLATFORM_C */
  38. #endif /* MBEDTLS_SELF_TEST */
  39. #if !defined(MBEDTLS_XTEA_ALT)
  40. /* Implementation that should never be optimized out by the compiler */
  41. static void mbedtls_zeroize( void *v, size_t n ) {
  42. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  43. }
  44. /*
  45. * 32-bit integer manipulation macros (big endian)
  46. */
  47. #ifndef GET_UINT32_BE
  48. #define GET_UINT32_BE(n,b,i) \
  49. { \
  50. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  51. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  52. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  53. | ( (uint32_t) (b)[(i) + 3] ); \
  54. }
  55. #endif
  56. #ifndef PUT_UINT32_BE
  57. #define PUT_UINT32_BE(n,b,i) \
  58. { \
  59. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  60. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  61. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  62. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  63. }
  64. #endif
  65. void mbedtls_xtea_init( mbedtls_xtea_context *ctx )
  66. {
  67. memset( ctx, 0, sizeof( mbedtls_xtea_context ) );
  68. }
  69. void mbedtls_xtea_free( mbedtls_xtea_context *ctx )
  70. {
  71. if( ctx == NULL )
  72. return;
  73. mbedtls_zeroize( ctx, sizeof( mbedtls_xtea_context ) );
  74. }
  75. /*
  76. * XTEA key schedule
  77. */
  78. void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] )
  79. {
  80. int i;
  81. memset( ctx, 0, sizeof(mbedtls_xtea_context) );
  82. for( i = 0; i < 4; i++ )
  83. {
  84. GET_UINT32_BE( ctx->k[i], key, i << 2 );
  85. }
  86. }
  87. /*
  88. * XTEA encrypt function
  89. */
  90. int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, int mode,
  91. const unsigned char input[8], unsigned char output[8])
  92. {
  93. uint32_t *k, v0, v1, i;
  94. k = ctx->k;
  95. GET_UINT32_BE( v0, input, 0 );
  96. GET_UINT32_BE( v1, input, 4 );
  97. if( mode == MBEDTLS_XTEA_ENCRYPT )
  98. {
  99. uint32_t sum = 0, delta = 0x9E3779B9;
  100. for( i = 0; i < 32; i++ )
  101. {
  102. v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  103. sum += delta;
  104. v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  105. }
  106. }
  107. else /* MBEDTLS_XTEA_DECRYPT */
  108. {
  109. uint32_t delta = 0x9E3779B9, sum = delta * 32;
  110. for( i = 0; i < 32; i++ )
  111. {
  112. v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  113. sum -= delta;
  114. v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  115. }
  116. }
  117. PUT_UINT32_BE( v0, output, 0 );
  118. PUT_UINT32_BE( v1, output, 4 );
  119. return( 0 );
  120. }
  121. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  122. /*
  123. * XTEA-CBC buffer encryption/decryption
  124. */
  125. int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, int mode, size_t length,
  126. unsigned char iv[8], const unsigned char *input,
  127. unsigned char *output)
  128. {
  129. int i;
  130. unsigned char temp[8];
  131. if( length % 8 )
  132. return( MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH );
  133. if( mode == MBEDTLS_XTEA_DECRYPT )
  134. {
  135. while( length > 0 )
  136. {
  137. memcpy( temp, input, 8 );
  138. mbedtls_xtea_crypt_ecb( ctx, mode, input, output );
  139. for( i = 0; i < 8; i++ )
  140. output[i] = (unsigned char)( output[i] ^ iv[i] );
  141. memcpy( iv, temp, 8 );
  142. input += 8;
  143. output += 8;
  144. length -= 8;
  145. }
  146. }
  147. else
  148. {
  149. while( length > 0 )
  150. {
  151. for( i = 0; i < 8; i++ )
  152. output[i] = (unsigned char)( input[i] ^ iv[i] );
  153. mbedtls_xtea_crypt_ecb( ctx, mode, output, output );
  154. memcpy( iv, output, 8 );
  155. input += 8;
  156. output += 8;
  157. length -= 8;
  158. }
  159. }
  160. return( 0 );
  161. }
  162. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  163. #endif /* !MBEDTLS_XTEA_ALT */
  164. #if defined(MBEDTLS_SELF_TEST)
  165. /*
  166. * XTEA tests vectors (non-official)
  167. */
  168. static const unsigned char xtea_test_key[6][16] =
  169. {
  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, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  175. 0x0c, 0x0d, 0x0e, 0x0f },
  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. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  181. 0x00, 0x00, 0x00, 0x00 }
  182. };
  183. static const unsigned char xtea_test_pt[6][8] =
  184. {
  185. { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
  186. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  187. { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
  188. { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
  189. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  190. { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
  191. };
  192. static const unsigned char xtea_test_ct[6][8] =
  193. {
  194. { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
  195. { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
  196. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  197. { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
  198. { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
  199. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
  200. };
  201. /*
  202. * Checkup routine
  203. */
  204. int mbedtls_xtea_self_test( int verbose )
  205. {
  206. int i, ret = 0;
  207. unsigned char buf[8];
  208. mbedtls_xtea_context ctx;
  209. mbedtls_xtea_init( &ctx );
  210. for( i = 0; i < 6; i++ )
  211. {
  212. if( verbose != 0 )
  213. mbedtls_printf( " XTEA test #%d: ", i + 1 );
  214. memcpy( buf, xtea_test_pt[i], 8 );
  215. mbedtls_xtea_setup( &ctx, xtea_test_key[i] );
  216. mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf );
  217. if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
  218. {
  219. if( verbose != 0 )
  220. mbedtls_printf( "failed\n" );
  221. ret = 1;
  222. goto exit;
  223. }
  224. if( verbose != 0 )
  225. mbedtls_printf( "passed\n" );
  226. }
  227. if( verbose != 0 )
  228. mbedtls_printf( "\n" );
  229. exit:
  230. mbedtls_xtea_free( &ctx );
  231. return( ret );
  232. }
  233. #endif /* MBEDTLS_SELF_TEST */
  234. #endif /* MBEDTLS_XTEA_C */