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.

176 lines
4.7KB

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