tea.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. }