You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
8.2KB

  1. /*
  2. * A 32-bit implementation of the XTEA algorithm
  3. * Copyright (c) 2012 Samuel Pitoiset
  4. *
  5. * loosely based on the implementation of David Wheeler and Roger Needham
  6. *
  7. * This file is part of Libav.
  8. *
  9. * Libav is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * Libav is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * @brief XTEA 32-bit implementation
  26. * @author Samuel Pitoiset
  27. * @ingroup lavu_xtea
  28. */
  29. #include "avutil.h"
  30. #include "common.h"
  31. #include "intreadwrite.h"
  32. #include "mem.h"
  33. #include "xtea.h"
  34. #if !FF_API_CRYPTO_CONTEXT
  35. struct AVXTEA {
  36. uint32_t key[16];
  37. };
  38. #endif
  39. AVXTEA *av_xtea_alloc(void)
  40. {
  41. return av_mallocz(sizeof(struct AVXTEA));
  42. }
  43. void av_xtea_init(AVXTEA *ctx, const uint8_t key[16])
  44. {
  45. int i;
  46. for (i = 0; i < 4; i++)
  47. ctx->key[i] = AV_RB32(key + (i << 2));
  48. }
  49. void av_xtea_le_init(AVXTEA *ctx, const uint8_t key[16])
  50. {
  51. int i;
  52. for (i = 0; i < 4; i++)
  53. ctx->key[i] = AV_RL32(key + (i << 2));
  54. }
  55. static void xtea_crypt_ecb(AVXTEA *ctx, uint8_t *dst, const uint8_t *src,
  56. int decrypt, uint8_t *iv)
  57. {
  58. uint32_t v0, v1;
  59. int i;
  60. v0 = AV_RB32(src);
  61. v1 = AV_RB32(src + 4);
  62. if (decrypt) {
  63. uint32_t delta = 0x9E3779B9, sum = delta * 32;
  64. for (i = 0; i < 32; i++) {
  65. v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + ctx->key[(sum >> 11) & 3]);
  66. sum -= delta;
  67. v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + ctx->key[sum & 3]);
  68. }
  69. if (iv) {
  70. v0 ^= AV_RB32(iv);
  71. v1 ^= AV_RB32(iv + 4);
  72. memcpy(iv, src, 8);
  73. }
  74. } else {
  75. uint32_t sum = 0, delta = 0x9E3779B9;
  76. for (i = 0; i < 32; i++) {
  77. v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + ctx->key[sum & 3]);
  78. sum += delta;
  79. v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + ctx->key[(sum >> 11) & 3]);
  80. }
  81. }
  82. AV_WB32(dst, v0);
  83. AV_WB32(dst + 4, v1);
  84. }
  85. static void xtea_le_crypt_ecb(AVXTEA *ctx, uint8_t *dst, const uint8_t *src,
  86. int decrypt, uint8_t *iv)
  87. {
  88. uint32_t v0, v1;
  89. int i;
  90. v0 = AV_RL32(src);
  91. v1 = AV_RL32(src + 4);
  92. if (decrypt) {
  93. uint32_t delta = 0x9E3779B9, sum = delta * 32;
  94. for (i = 0; i < 32; i++) {
  95. v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + ctx->key[(sum >> 11) & 3]);
  96. sum -= delta;
  97. v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + ctx->key[sum & 3]);
  98. }
  99. if (iv) {
  100. v0 ^= AV_RL32(iv);
  101. v1 ^= AV_RL32(iv + 4);
  102. memcpy(iv, src, 8);
  103. }
  104. } else {
  105. uint32_t sum = 0, delta = 0x9E3779B9;
  106. for (i = 0; i < 32; i++) {
  107. v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + ctx->key[sum & 3]);
  108. sum += delta;
  109. v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + ctx->key[(sum >> 11) & 3]);
  110. }
  111. }
  112. AV_WL32(dst, v0);
  113. AV_WL32(dst + 4, v1);
  114. }
  115. static void xtea_crypt(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, int count,
  116. uint8_t *iv, int decrypt,
  117. void (*crypt)(AVXTEA *, uint8_t *, const uint8_t *, int, uint8_t *))
  118. {
  119. int i;
  120. if (decrypt) {
  121. while (count--) {
  122. crypt(ctx, dst, src, decrypt, iv);
  123. src += 8;
  124. dst += 8;
  125. }
  126. } else {
  127. while (count--) {
  128. if (iv) {
  129. for (i = 0; i < 8; i++)
  130. dst[i] = src[i] ^ iv[i];
  131. crypt(ctx, dst, dst, decrypt, NULL);
  132. memcpy(iv, dst, 8);
  133. } else {
  134. crypt(ctx, dst, src, decrypt, NULL);
  135. }
  136. src += 8;
  137. dst += 8;
  138. }
  139. }
  140. }
  141. void av_xtea_crypt(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, int count,
  142. uint8_t *iv, int decrypt)
  143. {
  144. xtea_crypt(ctx, dst, src, count, iv, decrypt, xtea_crypt_ecb);
  145. }
  146. void av_xtea_le_crypt(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, int count,
  147. uint8_t *iv, int decrypt)
  148. {
  149. xtea_crypt(ctx, dst, src, count, iv, decrypt, xtea_le_crypt_ecb);
  150. }
  151. #ifdef TEST
  152. #include <stdio.h>
  153. #define XTEA_NUM_TESTS 6
  154. static const uint8_t xtea_test_key[XTEA_NUM_TESTS][16] = {
  155. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  156. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  157. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  158. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  159. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  160. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  161. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  162. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  163. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  164. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  165. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  166. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  167. };
  168. static const uint8_t xtea_test_pt[XTEA_NUM_TESTS][8] = {
  169. { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
  170. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  171. { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
  172. { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
  173. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  174. { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
  175. };
  176. static const uint8_t xtea_test_ct[XTEA_NUM_TESTS][8] = {
  177. { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
  178. { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
  179. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  180. { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
  181. { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
  182. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
  183. };
  184. static void test_xtea(AVXTEA *ctx, uint8_t *dst, const uint8_t *src,
  185. const uint8_t *ref, int len, uint8_t *iv, int dir,
  186. const char *test,
  187. void (*crypt)(AVXTEA *, uint8_t *, const uint8_t *, int, uint8_t *, int))
  188. {
  189. crypt(ctx, dst, src, len, iv, dir);
  190. if (memcmp(dst, ref, 8*len)) {
  191. int i;
  192. printf("%s failed\ngot ", test);
  193. for (i = 0; i < 8*len; i++)
  194. printf("%02x ", dst[i]);
  195. printf("\nexpected ");
  196. for (i = 0; i < 8*len; i++)
  197. printf("%02x ", ref[i]);
  198. printf("\n");
  199. exit(1);
  200. }
  201. }
  202. int main(void)
  203. {
  204. AVXTEA ctx;
  205. uint8_t buf[16], iv[8];
  206. int i, j;
  207. const uint8_t src[32] = "HelloWorldHelloWorldHelloWorld";
  208. uint8_t ct[32];
  209. uint8_t pl[32];
  210. for (i = 0; i < XTEA_NUM_TESTS; i++) {
  211. av_xtea_init(&ctx, xtea_test_key[i]);
  212. test_xtea(&ctx, buf, xtea_test_pt[i], xtea_test_ct[i], 1, NULL, 0, "encryption", av_xtea_crypt);
  213. test_xtea(&ctx, buf, xtea_test_ct[i], xtea_test_pt[i], 1, NULL, 1, "decryption", av_xtea_crypt);
  214. for (j = 0; j < 4; j++)
  215. AV_WL32(&buf[4*j], AV_RB32(&xtea_test_key[i][4*j]));
  216. av_xtea_le_init(&ctx, buf);
  217. for (j = 0; j < 2; j++) {
  218. AV_WL32(&ct[4*j], AV_RB32(&xtea_test_ct[i][4*j]));
  219. AV_WL32(&pl[4*j], AV_RB32(&xtea_test_pt[i][4*j]));
  220. }
  221. test_xtea(&ctx, buf, pl, ct, 1, NULL, 0, "encryption", av_xtea_le_crypt);
  222. test_xtea(&ctx, buf, ct, pl, 1, NULL, 1, "decryption", av_xtea_le_crypt);
  223. /* encrypt */
  224. memcpy(iv, "HALLO123", 8);
  225. av_xtea_crypt(&ctx, ct, src, 4, iv, 0);
  226. /* decrypt into pl */
  227. memcpy(iv, "HALLO123", 8);
  228. test_xtea(&ctx, pl, ct, src, 4, iv, 1, "CBC decryption", av_xtea_crypt);
  229. memcpy(iv, "HALLO123", 8);
  230. test_xtea(&ctx, ct, ct, src, 4, iv, 1, "CBC inplace decryption", av_xtea_crypt);
  231. }
  232. printf("Test encryption/decryption success.\n");
  233. return 0;
  234. }
  235. #endif