xtea.c 8.1 KB

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