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.

214 lines
5.9KB

  1. /*
  2. * A 32-bit implementation of the TEA algorithm
  3. * Copyright (c) 2015 Vesselin Bontchev
  4. *
  5. * Loosely based on the implementation of David Wheeler and Roger Needham,
  6. * https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm#Reference_code
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "avutil.h"
  25. #include "common.h"
  26. #include "intreadwrite.h"
  27. #include "tea.h"
  28. typedef struct AVTEA {
  29. uint32_t key[16];
  30. int rounds;
  31. } AVTEA;
  32. struct AVTEA *av_tea_alloc(void)
  33. {
  34. return av_mallocz(sizeof(struct AVTEA));
  35. }
  36. const int av_tea_size = sizeof(AVTEA);
  37. void av_tea_init(AVTEA *ctx, const uint8_t key[16], int rounds)
  38. {
  39. int i;
  40. for (i = 0; i < 4; i++)
  41. ctx->key[i] = AV_RB32(key + (i << 2));
  42. ctx->rounds = rounds;
  43. }
  44. static void tea_crypt_ecb(AVTEA *ctx, uint8_t *dst, const uint8_t *src,
  45. int decrypt, uint8_t *iv)
  46. {
  47. uint32_t v0, v1;
  48. int rounds = ctx->rounds;
  49. uint32_t k0, k1, k2, k3;
  50. k0 = ctx->key[0];
  51. k1 = ctx->key[1];
  52. k2 = ctx->key[2];
  53. k3 = ctx->key[3];
  54. v0 = AV_RB32(src);
  55. v1 = AV_RB32(src + 4);
  56. if (decrypt) {
  57. int i;
  58. uint32_t delta = 0x9E3779B9U, sum = delta * (rounds / 2);
  59. for (i = 0; i < rounds / 2; i++) {
  60. v1 -= ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3);
  61. v0 -= ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1);
  62. sum -= delta;
  63. }
  64. if (iv) {
  65. v0 ^= AV_RB32(iv);
  66. v1 ^= AV_RB32(iv + 4);
  67. memcpy(iv, src, 8);
  68. }
  69. } else {
  70. int i;
  71. uint32_t sum = 0, delta = 0x9E3779B9U;
  72. for (i = 0; i < rounds / 2; i++) {
  73. sum += delta;
  74. v0 += ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1);
  75. v1 += ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3);
  76. }
  77. }
  78. AV_WB32(dst, v0);
  79. AV_WB32(dst + 4, v1);
  80. }
  81. void av_tea_crypt(AVTEA *ctx, uint8_t *dst, const uint8_t *src, int count,
  82. uint8_t *iv, int decrypt)
  83. {
  84. int i;
  85. if (decrypt) {
  86. while (count--) {
  87. tea_crypt_ecb(ctx, dst, src, decrypt, iv);
  88. src += 8;
  89. dst += 8;
  90. }
  91. } else {
  92. while (count--) {
  93. if (iv) {
  94. for (i = 0; i < 8; i++)
  95. dst[i] = src[i] ^ iv[i];
  96. tea_crypt_ecb(ctx, dst, dst, decrypt, NULL);
  97. memcpy(iv, dst, 8);
  98. } else {
  99. tea_crypt_ecb(ctx, dst, src, decrypt, NULL);
  100. }
  101. src += 8;
  102. dst += 8;
  103. }
  104. }
  105. }
  106. #ifdef TEST
  107. #include <stdio.h>
  108. #define TEA_NUM_TESTS 4
  109. // https://github.com/logandrews/TeaCrypt/blob/master/tea/tea_test.go
  110. static const uint8_t tea_test_key[TEA_NUM_TESTS][16] = {
  111. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  112. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  113. },
  114. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  115. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  116. },
  117. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  118. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  119. },
  120. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  121. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  122. }
  123. };
  124. static const uint8_t tea_test_pt[TEA_NUM_TESTS][8] = {
  125. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  126. { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 },
  127. { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 },
  128. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }
  129. };
  130. static const uint8_t tea_test_ct[TEA_NUM_TESTS][8] = {
  131. { 0x41, 0xEA, 0x3A, 0x0A, 0x94, 0xBA, 0xA9, 0x40 },
  132. { 0x6A, 0x2F, 0x9C, 0xF3, 0xFC, 0xCF, 0x3C, 0x55 },
  133. { 0xDE, 0xB1, 0xC0, 0xA2, 0x7E, 0x74, 0x5D, 0xB3 },
  134. { 0x12, 0x6C, 0x6B, 0x92, 0xC0, 0x65, 0x3A, 0x3E }
  135. };
  136. static void test_tea(AVTEA *ctx, uint8_t *dst, const uint8_t *src,
  137. const uint8_t *ref, int len, uint8_t *iv, int dir,
  138. const char *test)
  139. {
  140. av_tea_crypt(ctx, dst, src, len, iv, dir);
  141. if (memcmp(dst, ref, 8*len)) {
  142. int i;
  143. printf("%s failed\ngot ", test);
  144. for (i = 0; i < 8*len; i++)
  145. printf("%02x ", dst[i]);
  146. printf("\nexpected ");
  147. for (i = 0; i < 8*len; i++)
  148. printf("%02x ", ref[i]);
  149. printf("\n");
  150. exit(1);
  151. }
  152. }
  153. int main(void)
  154. {
  155. AVTEA *ctx;
  156. uint8_t buf[8], iv[8];
  157. int i;
  158. static const uint8_t src[32] = "HelloWorldHelloWorldHelloWorld";
  159. uint8_t ct[32];
  160. uint8_t pl[32];
  161. ctx = av_tea_alloc();
  162. if (!ctx)
  163. return 1;
  164. for (i = 0; i < TEA_NUM_TESTS; i++) {
  165. av_tea_init(ctx, tea_test_key[i], 64);
  166. test_tea(ctx, buf, tea_test_pt[i], tea_test_ct[i], 1, NULL, 0, "encryption");
  167. test_tea(ctx, buf, tea_test_ct[i], tea_test_pt[i], 1, NULL, 1, "decryption");
  168. /* encrypt */
  169. memcpy(iv, "HALLO123", 8);
  170. av_tea_crypt(ctx, ct, src, 4, iv, 0);
  171. /* decrypt into pl */
  172. memcpy(iv, "HALLO123", 8);
  173. test_tea(ctx, pl, ct, src, 4, iv, 1, "CBC decryption");
  174. memcpy(iv, "HALLO123", 8);
  175. test_tea(ctx, ct, ct, src, 4, iv, 1, "CBC inplace decryption");
  176. }
  177. printf("Test encryption/decryption success.\n");
  178. av_free(ctx);
  179. return 0;
  180. }
  181. #endif