tea.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Cryptographic API.
  3. *
  4. * TEA, XTEA, and XETA crypto alogrithms
  5. *
  6. * The TEA and Xtended TEA algorithms were developed by David Wheeler
  7. * and Roger Needham at the Computer Laboratory of Cambridge University.
  8. *
  9. * Due to the order of evaluation in XTEA many people have incorrectly
  10. * implemented it. XETA (XTEA in the wrong order), exists for
  11. * compatibility with these implementations.
  12. *
  13. * Copyright (c) 2004 Aaron Grothe ajgrothe@yahoo.com
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. */
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/mm.h>
  24. #include <asm/scatterlist.h>
  25. #include <linux/crypto.h>
  26. #define TEA_KEY_SIZE 16
  27. #define TEA_BLOCK_SIZE 8
  28. #define TEA_ROUNDS 32
  29. #define TEA_DELTA 0x9e3779b9
  30. #define XTEA_KEY_SIZE 16
  31. #define XTEA_BLOCK_SIZE 8
  32. #define XTEA_ROUNDS 32
  33. #define XTEA_DELTA 0x9e3779b9
  34. #define u32_in(x) le32_to_cpu(*(const __le32 *)(x))
  35. #define u32_out(to, from) (*(__le32 *)(to) = cpu_to_le32(from))
  36. struct tea_ctx {
  37. u32 KEY[4];
  38. };
  39. struct xtea_ctx {
  40. u32 KEY[4];
  41. };
  42. static int tea_setkey(void *ctx_arg, const u8 *in_key,
  43. unsigned int key_len, u32 *flags)
  44. {
  45. struct tea_ctx *ctx = ctx_arg;
  46. if (key_len != 16)
  47. {
  48. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  49. return -EINVAL;
  50. }
  51. ctx->KEY[0] = u32_in (in_key);
  52. ctx->KEY[1] = u32_in (in_key + 4);
  53. ctx->KEY[2] = u32_in (in_key + 8);
  54. ctx->KEY[3] = u32_in (in_key + 12);
  55. return 0;
  56. }
  57. static void tea_encrypt(void *ctx_arg, u8 *dst, const u8 *src)
  58. {
  59. u32 y, z, n, sum = 0;
  60. u32 k0, k1, k2, k3;
  61. struct tea_ctx *ctx = ctx_arg;
  62. y = u32_in (src);
  63. z = u32_in (src + 4);
  64. k0 = ctx->KEY[0];
  65. k1 = ctx->KEY[1];
  66. k2 = ctx->KEY[2];
  67. k3 = ctx->KEY[3];
  68. n = TEA_ROUNDS;
  69. while (n-- > 0) {
  70. sum += TEA_DELTA;
  71. y += ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
  72. z += ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
  73. }
  74. u32_out (dst, y);
  75. u32_out (dst + 4, z);
  76. }
  77. static void tea_decrypt(void *ctx_arg, u8 *dst, const u8 *src)
  78. {
  79. u32 y, z, n, sum;
  80. u32 k0, k1, k2, k3;
  81. struct tea_ctx *ctx = ctx_arg;
  82. y = u32_in (src);
  83. z = u32_in (src + 4);
  84. k0 = ctx->KEY[0];
  85. k1 = ctx->KEY[1];
  86. k2 = ctx->KEY[2];
  87. k3 = ctx->KEY[3];
  88. sum = TEA_DELTA << 5;
  89. n = TEA_ROUNDS;
  90. while (n-- > 0) {
  91. z -= ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
  92. y -= ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
  93. sum -= TEA_DELTA;
  94. }
  95. u32_out (dst, y);
  96. u32_out (dst + 4, z);
  97. }
  98. static int xtea_setkey(void *ctx_arg, const u8 *in_key,
  99. unsigned int key_len, u32 *flags)
  100. {
  101. struct xtea_ctx *ctx = ctx_arg;
  102. if (key_len != 16)
  103. {
  104. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  105. return -EINVAL;
  106. }
  107. ctx->KEY[0] = u32_in (in_key);
  108. ctx->KEY[1] = u32_in (in_key + 4);
  109. ctx->KEY[2] = u32_in (in_key + 8);
  110. ctx->KEY[3] = u32_in (in_key + 12);
  111. return 0;
  112. }
  113. static void xtea_encrypt(void *ctx_arg, u8 *dst, const u8 *src)
  114. {
  115. u32 y, z, sum = 0;
  116. u32 limit = XTEA_DELTA * XTEA_ROUNDS;
  117. struct xtea_ctx *ctx = ctx_arg;
  118. y = u32_in (src);
  119. z = u32_in (src + 4);
  120. while (sum != limit) {
  121. y += ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum&3]);
  122. sum += XTEA_DELTA;
  123. z += ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 &3]);
  124. }
  125. u32_out (dst, y);
  126. u32_out (dst + 4, z);
  127. }
  128. static void xtea_decrypt(void *ctx_arg, u8 *dst, const u8 *src)
  129. {
  130. u32 y, z, sum;
  131. struct tea_ctx *ctx = ctx_arg;
  132. y = u32_in (src);
  133. z = u32_in (src + 4);
  134. sum = XTEA_DELTA * XTEA_ROUNDS;
  135. while (sum) {
  136. z -= ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 & 3]);
  137. sum -= XTEA_DELTA;
  138. y -= ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum & 3]);
  139. }
  140. u32_out (dst, y);
  141. u32_out (dst + 4, z);
  142. }
  143. static void xeta_encrypt(void *ctx_arg, u8 *dst, const u8 *src)
  144. {
  145. u32 y, z, sum = 0;
  146. u32 limit = XTEA_DELTA * XTEA_ROUNDS;
  147. struct xtea_ctx *ctx = ctx_arg;
  148. y = u32_in (src);
  149. z = u32_in (src + 4);
  150. while (sum != limit) {
  151. y += (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3];
  152. sum += XTEA_DELTA;
  153. z += (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3];
  154. }
  155. u32_out (dst, y);
  156. u32_out (dst + 4, z);
  157. }
  158. static void xeta_decrypt(void *ctx_arg, u8 *dst, const u8 *src)
  159. {
  160. u32 y, z, sum;
  161. struct tea_ctx *ctx = ctx_arg;
  162. y = u32_in (src);
  163. z = u32_in (src + 4);
  164. sum = XTEA_DELTA * XTEA_ROUNDS;
  165. while (sum) {
  166. z -= (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 & 3];
  167. sum -= XTEA_DELTA;
  168. y -= (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3];
  169. }
  170. u32_out (dst, y);
  171. u32_out (dst + 4, z);
  172. }
  173. static struct crypto_alg tea_alg = {
  174. .cra_name = "tea",
  175. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  176. .cra_blocksize = TEA_BLOCK_SIZE,
  177. .cra_ctxsize = sizeof (struct tea_ctx),
  178. .cra_module = THIS_MODULE,
  179. .cra_list = LIST_HEAD_INIT(tea_alg.cra_list),
  180. .cra_u = { .cipher = {
  181. .cia_min_keysize = TEA_KEY_SIZE,
  182. .cia_max_keysize = TEA_KEY_SIZE,
  183. .cia_setkey = tea_setkey,
  184. .cia_encrypt = tea_encrypt,
  185. .cia_decrypt = tea_decrypt } }
  186. };
  187. static struct crypto_alg xtea_alg = {
  188. .cra_name = "xtea",
  189. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  190. .cra_blocksize = XTEA_BLOCK_SIZE,
  191. .cra_ctxsize = sizeof (struct xtea_ctx),
  192. .cra_module = THIS_MODULE,
  193. .cra_list = LIST_HEAD_INIT(xtea_alg.cra_list),
  194. .cra_u = { .cipher = {
  195. .cia_min_keysize = XTEA_KEY_SIZE,
  196. .cia_max_keysize = XTEA_KEY_SIZE,
  197. .cia_setkey = xtea_setkey,
  198. .cia_encrypt = xtea_encrypt,
  199. .cia_decrypt = xtea_decrypt } }
  200. };
  201. static struct crypto_alg xeta_alg = {
  202. .cra_name = "xeta",
  203. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  204. .cra_blocksize = XTEA_BLOCK_SIZE,
  205. .cra_ctxsize = sizeof (struct xtea_ctx),
  206. .cra_module = THIS_MODULE,
  207. .cra_list = LIST_HEAD_INIT(xtea_alg.cra_list),
  208. .cra_u = { .cipher = {
  209. .cia_min_keysize = XTEA_KEY_SIZE,
  210. .cia_max_keysize = XTEA_KEY_SIZE,
  211. .cia_setkey = xtea_setkey,
  212. .cia_encrypt = xeta_encrypt,
  213. .cia_decrypt = xeta_decrypt } }
  214. };
  215. static int __init init(void)
  216. {
  217. int ret = 0;
  218. ret = crypto_register_alg(&tea_alg);
  219. if (ret < 0)
  220. goto out;
  221. ret = crypto_register_alg(&xtea_alg);
  222. if (ret < 0) {
  223. crypto_unregister_alg(&tea_alg);
  224. goto out;
  225. }
  226. ret = crypto_register_alg(&xeta_alg);
  227. if (ret < 0) {
  228. crypto_unregister_alg(&tea_alg);
  229. crypto_unregister_alg(&xtea_alg);
  230. goto out;
  231. }
  232. out:
  233. return ret;
  234. }
  235. static void __exit fini(void)
  236. {
  237. crypto_unregister_alg(&tea_alg);
  238. crypto_unregister_alg(&xtea_alg);
  239. crypto_unregister_alg(&xeta_alg);
  240. }
  241. MODULE_ALIAS("xtea");
  242. MODULE_ALIAS("xeta");
  243. module_init(init);
  244. module_exit(fini);
  245. MODULE_LICENSE("GPL");
  246. MODULE_DESCRIPTION("TEA, XTEA & XETA Cryptographic Algorithms");