xtea.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 FFmpeg.
  8. *
  9. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. AVXTEA *av_xtea_alloc(void)
  35. {
  36. return av_mallocz(sizeof(struct AVXTEA));
  37. }
  38. void av_xtea_init(AVXTEA *ctx, const uint8_t key[16])
  39. {
  40. int i;
  41. for (i = 0; i < 4; i++)
  42. ctx->key[i] = AV_RB32(key + (i << 2));
  43. }
  44. void av_xtea_le_init(AVXTEA *ctx, const uint8_t key[16])
  45. {
  46. int i;
  47. for (i = 0; i < 4; i++)
  48. ctx->key[i] = AV_RL32(key + (i << 2));
  49. }
  50. static void xtea_crypt_ecb(AVXTEA *ctx, uint8_t *dst, const uint8_t *src,
  51. int decrypt, uint8_t *iv)
  52. {
  53. uint32_t v0, v1;
  54. #if !CONFIG_SMALL
  55. uint32_t k0 = ctx->key[0];
  56. uint32_t k1 = ctx->key[1];
  57. uint32_t k2 = ctx->key[2];
  58. uint32_t k3 = ctx->key[3];
  59. #endif
  60. v0 = AV_RB32(src);
  61. v1 = AV_RB32(src + 4);
  62. if (decrypt) {
  63. #if CONFIG_SMALL
  64. int i;
  65. uint32_t delta = 0x9E3779B9U, sum = delta * 32;
  66. for (i = 0; i < 32; i++) {
  67. v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + ctx->key[(sum >> 11) & 3]);
  68. sum -= delta;
  69. v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + ctx->key[sum & 3]);
  70. }
  71. #else
  72. #define DSTEP(SUM, K0, K1) \
  73. v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (SUM + K0); \
  74. v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (SUM - 0x9E3779B9U + K1)
  75. DSTEP(0xC6EF3720U, k2, k3);
  76. DSTEP(0x28B7BD67U, k3, k2);
  77. DSTEP(0x8A8043AEU, k0, k1);
  78. DSTEP(0xEC48C9F5U, k1, k0);
  79. DSTEP(0x4E11503CU, k2, k3);
  80. DSTEP(0xAFD9D683U, k2, k2);
  81. DSTEP(0x11A25CCAU, k3, k1);
  82. DSTEP(0x736AE311U, k0, k0);
  83. DSTEP(0xD5336958U, k1, k3);
  84. DSTEP(0x36FBEF9FU, k1, k2);
  85. DSTEP(0x98C475E6U, k2, k1);
  86. DSTEP(0xFA8CFC2DU, k3, k0);
  87. DSTEP(0x5C558274U, k0, k3);
  88. DSTEP(0xBE1E08BBU, k1, k2);
  89. DSTEP(0x1FE68F02U, k1, k1);
  90. DSTEP(0x81AF1549U, k2, k0);
  91. DSTEP(0xE3779B90U, k3, k3);
  92. DSTEP(0x454021D7U, k0, k2);
  93. DSTEP(0xA708A81EU, k1, k1);
  94. DSTEP(0x08D12E65U, k1, k0);
  95. DSTEP(0x6A99B4ACU, k2, k3);
  96. DSTEP(0xCC623AF3U, k3, k2);
  97. DSTEP(0x2E2AC13AU, k0, k1);
  98. DSTEP(0x8FF34781U, k0, k0);
  99. DSTEP(0xF1BBCDC8U, k1, k3);
  100. DSTEP(0x5384540FU, k2, k2);
  101. DSTEP(0xB54CDA56U, k3, k1);
  102. DSTEP(0x1715609DU, k0, k0);
  103. DSTEP(0x78DDE6E4U, k0, k3);
  104. DSTEP(0xDAA66D2BU, k1, k2);
  105. DSTEP(0x3C6EF372U, k2, k1);
  106. DSTEP(0x9E3779B9U, k3, k0);
  107. #endif
  108. if (iv) {
  109. v0 ^= AV_RB32(iv);
  110. v1 ^= AV_RB32(iv + 4);
  111. memcpy(iv, src, 8);
  112. }
  113. } else {
  114. #if CONFIG_SMALL
  115. int i;
  116. uint32_t sum = 0, delta = 0x9E3779B9U;
  117. for (i = 0; i < 32; i++) {
  118. v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + ctx->key[sum & 3]);
  119. sum += delta;
  120. v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + ctx->key[(sum >> 11) & 3]);
  121. }
  122. #else
  123. #define ESTEP(SUM, K0, K1) \
  124. v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (SUM + K0);\
  125. v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (SUM + 0x9E3779B9U + K1)
  126. ESTEP(0x00000000U, k0, k3);
  127. ESTEP(0x9E3779B9U, k1, k2);
  128. ESTEP(0x3C6EF372U, k2, k1);
  129. ESTEP(0xDAA66D2BU, k3, k0);
  130. ESTEP(0x78DDE6E4U, k0, k0);
  131. ESTEP(0x1715609DU, k1, k3);
  132. ESTEP(0xB54CDA56U, k2, k2);
  133. ESTEP(0x5384540FU, k3, k1);
  134. ESTEP(0xF1BBCDC8U, k0, k0);
  135. ESTEP(0x8FF34781U, k1, k0);
  136. ESTEP(0x2E2AC13AU, k2, k3);
  137. ESTEP(0xCC623AF3U, k3, k2);
  138. ESTEP(0x6A99B4ACU, k0, k1);
  139. ESTEP(0x08D12E65U, k1, k1);
  140. ESTEP(0xA708A81EU, k2, k0);
  141. ESTEP(0x454021D7U, k3, k3);
  142. ESTEP(0xE3779B90U, k0, k2);
  143. ESTEP(0x81AF1549U, k1, k1);
  144. ESTEP(0x1FE68F02U, k2, k1);
  145. ESTEP(0xBE1E08BBU, k3, k0);
  146. ESTEP(0x5C558274U, k0, k3);
  147. ESTEP(0xFA8CFC2DU, k1, k2);
  148. ESTEP(0x98C475E6U, k2, k1);
  149. ESTEP(0x36FBEF9FU, k3, k1);
  150. ESTEP(0xD5336958U, k0, k0);
  151. ESTEP(0x736AE311U, k1, k3);
  152. ESTEP(0x11A25CCAU, k2, k2);
  153. ESTEP(0xAFD9D683U, k3, k2);
  154. ESTEP(0x4E11503CU, k0, k1);
  155. ESTEP(0xEC48C9F5U, k1, k0);
  156. ESTEP(0x8A8043AEU, k2, k3);
  157. ESTEP(0x28B7BD67U, k3, k2);
  158. #endif
  159. }
  160. AV_WB32(dst, v0);
  161. AV_WB32(dst + 4, v1);
  162. }
  163. static void xtea_le_crypt_ecb(AVXTEA *ctx, uint8_t *dst, const uint8_t *src,
  164. int decrypt, uint8_t *iv)
  165. {
  166. uint32_t v0, v1;
  167. int i;
  168. v0 = AV_RL32(src);
  169. v1 = AV_RL32(src + 4);
  170. if (decrypt) {
  171. uint32_t delta = 0x9E3779B9, sum = delta * 32;
  172. for (i = 0; i < 32; i++) {
  173. v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + ctx->key[(sum >> 11) & 3]);
  174. sum -= delta;
  175. v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + ctx->key[sum & 3]);
  176. }
  177. if (iv) {
  178. v0 ^= AV_RL32(iv);
  179. v1 ^= AV_RL32(iv + 4);
  180. memcpy(iv, src, 8);
  181. }
  182. } else {
  183. uint32_t sum = 0, delta = 0x9E3779B9;
  184. for (i = 0; i < 32; i++) {
  185. v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + ctx->key[sum & 3]);
  186. sum += delta;
  187. v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + ctx->key[(sum >> 11) & 3]);
  188. }
  189. }
  190. AV_WL32(dst, v0);
  191. AV_WL32(dst + 4, v1);
  192. }
  193. static void xtea_crypt(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, int count,
  194. uint8_t *iv, int decrypt,
  195. void (*crypt)(AVXTEA *, uint8_t *, const uint8_t *, int, uint8_t *))
  196. {
  197. int i;
  198. if (decrypt) {
  199. while (count--) {
  200. crypt(ctx, dst, src, decrypt, iv);
  201. src += 8;
  202. dst += 8;
  203. }
  204. } else {
  205. while (count--) {
  206. if (iv) {
  207. for (i = 0; i < 8; i++)
  208. dst[i] = src[i] ^ iv[i];
  209. crypt(ctx, dst, dst, decrypt, NULL);
  210. memcpy(iv, dst, 8);
  211. } else {
  212. crypt(ctx, dst, src, decrypt, NULL);
  213. }
  214. src += 8;
  215. dst += 8;
  216. }
  217. }
  218. }
  219. void av_xtea_crypt(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, int count,
  220. uint8_t *iv, int decrypt)
  221. {
  222. xtea_crypt(ctx, dst, src, count, iv, decrypt, xtea_crypt_ecb);
  223. }
  224. void av_xtea_le_crypt(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, int count,
  225. uint8_t *iv, int decrypt)
  226. {
  227. xtea_crypt(ctx, dst, src, count, iv, decrypt, xtea_le_crypt_ecb);
  228. }