xtea.c 6.3 KB

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