xtea.c 7.0 KB

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