xtea.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * An 32-bit implementation of the XTEA algorithm
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #include "config.h"
  26. #if defined(POLARSSL_XTEA_C)
  27. #include "polarssl/xtea.h"
  28. /*
  29. * 32-bit integer manipulation macros (big endian)
  30. */
  31. #ifndef GET_ULONG_BE
  32. #define GET_ULONG_BE(n,b,i) \
  33. { \
  34. (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
  35. | ( (unsigned long) (b)[(i) + 1] << 16 ) \
  36. | ( (unsigned long) (b)[(i) + 2] << 8 ) \
  37. | ( (unsigned long) (b)[(i) + 3] ); \
  38. }
  39. #endif
  40. #ifndef PUT_ULONG_BE
  41. #define PUT_ULONG_BE(n,b,i) \
  42. { \
  43. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  44. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  45. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  46. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  47. }
  48. #endif
  49. /*
  50. * XTEA key schedule
  51. */
  52. void xtea_setup( xtea_context *ctx, unsigned char key[16] )
  53. {
  54. int i;
  55. memset(ctx, 0, sizeof(xtea_context));
  56. for( i = 0; i < 4; i++ )
  57. {
  58. GET_ULONG_BE( ctx->k[i], key, i << 2 );
  59. }
  60. }
  61. /*
  62. * XTEA encrypt function
  63. */
  64. int xtea_crypt_ecb( xtea_context *ctx, int mode, unsigned char input[8],
  65. unsigned char output[8])
  66. {
  67. uint32_t *k, v0, v1, i;
  68. k = ctx->k;
  69. GET_ULONG_BE( v0, input, 0 );
  70. GET_ULONG_BE( v1, input, 4 );
  71. if( mode == XTEA_ENCRYPT )
  72. {
  73. uint32_t sum = 0, delta = 0x9E3779B9;
  74. for( i = 0; i < 32; i++ )
  75. {
  76. v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  77. sum += delta;
  78. v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  79. }
  80. }
  81. else /* XTEA_DECRYPT */
  82. {
  83. uint32_t delta = 0x9E3779B9, sum = delta * 32;
  84. for( i = 0; i < 32; i++ )
  85. {
  86. v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  87. sum -= delta;
  88. v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  89. }
  90. }
  91. PUT_ULONG_BE( v0, output, 0 );
  92. PUT_ULONG_BE( v1, output, 4 );
  93. return( 0 );
  94. }
  95. /*
  96. * XTEA-CBC buffer encryption/decryption
  97. */
  98. int xtea_crypt_cbc( xtea_context *ctx,
  99. int mode,
  100. size_t length,
  101. unsigned char iv[8],
  102. unsigned char *input,
  103. unsigned char *output)
  104. {
  105. int i;
  106. unsigned char temp[8];
  107. if(length % 8)
  108. return( POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH );
  109. if( mode == XTEA_DECRYPT )
  110. {
  111. while( length > 0 )
  112. {
  113. memcpy( temp, input, 8 );
  114. xtea_crypt_ecb( ctx, mode, input, output );
  115. for(i = 0; i < 8; i++)
  116. output[i] = (unsigned char)( output[i] ^ iv[i] );
  117. memcpy( iv, temp, 8 );
  118. input += 8;
  119. output += 8;
  120. length -= 8;
  121. }
  122. }
  123. else
  124. {
  125. while( length > 0 )
  126. {
  127. for( i = 0; i < 8; i++ )
  128. output[i] = (unsigned char)( input[i] ^ iv[i] );
  129. xtea_crypt_ecb( ctx, mode, output, output );
  130. memcpy( iv, output, 8 );
  131. input += 8;
  132. output += 8;
  133. length -= 8;
  134. }
  135. }
  136. return( 0 );
  137. }
  138. #if defined(POLARSSL_SELF_TEST)
  139. #include <string.h>
  140. #ifdef PRINTF_STDLIB
  141. #include <stdio.h>
  142. #endif
  143. #ifdef PRINTF_CUSTOM
  144. #include "tinystdio.h"
  145. #endif
  146. /*
  147. * XTEA tests vectors (non-official)
  148. */
  149. static const unsigned char xtea_test_key[6][16] =
  150. {
  151. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  152. 0x0c, 0x0d, 0x0e, 0x0f },
  153. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  154. 0x0c, 0x0d, 0x0e, 0x0f },
  155. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  156. 0x0c, 0x0d, 0x0e, 0x0f },
  157. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  158. 0x00, 0x00, 0x00, 0x00 },
  159. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  160. 0x00, 0x00, 0x00, 0x00 },
  161. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  162. 0x00, 0x00, 0x00, 0x00 }
  163. };
  164. static const unsigned char xtea_test_pt[6][8] =
  165. {
  166. { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
  167. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  168. { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
  169. { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
  170. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  171. { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
  172. };
  173. static const unsigned char xtea_test_ct[6][8] =
  174. {
  175. { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
  176. { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
  177. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  178. { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
  179. { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
  180. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
  181. };
  182. /*
  183. * Checkup routine
  184. */
  185. int xtea_self_test( int verbose )
  186. {
  187. int i;
  188. unsigned char buf[8];
  189. xtea_context ctx;
  190. for( i = 0; i < 6; i++ )
  191. {
  192. if( verbose != 0 )
  193. printf( " XTEA test #%d: ", i + 1 );
  194. memcpy( buf, xtea_test_pt[i], 8 );
  195. xtea_setup( &ctx, (unsigned char *) xtea_test_key[i] );
  196. xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, buf, buf );
  197. if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
  198. {
  199. if( verbose != 0 )
  200. printf( "failed\n" );
  201. return( 1 );
  202. }
  203. if( verbose != 0 )
  204. printf( "passed\n" );
  205. }
  206. if( verbose != 0 )
  207. printf( "\n" );
  208. return( 0 );
  209. }
  210. #endif
  211. #endif