tea.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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/byteorder.h>
  25. #include <asm/scatterlist.h>
  26. #include <linux/crypto.h>
  27. #include <linux/types.h>
  28. #define TEA_KEY_SIZE 16
  29. #define TEA_BLOCK_SIZE 8
  30. #define TEA_ROUNDS 32
  31. #define TEA_DELTA 0x9e3779b9
  32. #define XTEA_KEY_SIZE 16
  33. #define XTEA_BLOCK_SIZE 8
  34. #define XTEA_ROUNDS 32
  35. #define XTEA_DELTA 0x9e3779b9
  36. struct tea_ctx {
  37. u32 KEY[4];
  38. };
  39. struct xtea_ctx {
  40. u32 KEY[4];
  41. };
  42. static int tea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
  43. unsigned int key_len)
  44. {
  45. struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
  46. const __le32 *key = (const __le32 *)in_key;
  47. ctx->KEY[0] = le32_to_cpu(key[0]);
  48. ctx->KEY[1] = le32_to_cpu(key[1]);
  49. ctx->KEY[2] = le32_to_cpu(key[2]);
  50. ctx->KEY[3] = le32_to_cpu(key[3]);
  51. return 0;
  52. }
  53. static void tea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  54. {
  55. u32 y, z, n, sum = 0;
  56. u32 k0, k1, k2, k3;
  57. struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
  58. const __le32 *in = (const __le32 *)src;
  59. __le32 *out = (__le32 *)dst;
  60. y = le32_to_cpu(in[0]);
  61. z = le32_to_cpu(in[1]);
  62. k0 = ctx->KEY[0];
  63. k1 = ctx->KEY[1];
  64. k2 = ctx->KEY[2];
  65. k3 = ctx->KEY[3];
  66. n = TEA_ROUNDS;
  67. while (n-- > 0) {
  68. sum += TEA_DELTA;
  69. y += ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
  70. z += ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
  71. }
  72. out[0] = cpu_to_le32(y);
  73. out[1] = cpu_to_le32(z);
  74. }
  75. static void tea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  76. {
  77. u32 y, z, n, sum;
  78. u32 k0, k1, k2, k3;
  79. struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
  80. const __le32 *in = (const __le32 *)src;
  81. __le32 *out = (__le32 *)dst;
  82. y = le32_to_cpu(in[0]);
  83. z = le32_to_cpu(in[1]);
  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. out[0] = cpu_to_le32(y);
  96. out[1] = cpu_to_le32(z);
  97. }
  98. static int xtea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
  99. unsigned int key_len)
  100. {
  101. struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
  102. const __le32 *key = (const __le32 *)in_key;
  103. ctx->KEY[0] = le32_to_cpu(key[0]);
  104. ctx->KEY[1] = le32_to_cpu(key[1]);
  105. ctx->KEY[2] = le32_to_cpu(key[2]);
  106. ctx->KEY[3] = le32_to_cpu(key[3]);
  107. return 0;
  108. }
  109. static void xtea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  110. {
  111. u32 y, z, sum = 0;
  112. u32 limit = XTEA_DELTA * XTEA_ROUNDS;
  113. struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
  114. const __le32 *in = (const __le32 *)src;
  115. __le32 *out = (__le32 *)dst;
  116. y = le32_to_cpu(in[0]);
  117. z = le32_to_cpu(in[1]);
  118. while (sum != limit) {
  119. y += ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum&3]);
  120. sum += XTEA_DELTA;
  121. z += ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 &3]);
  122. }
  123. out[0] = cpu_to_le32(y);
  124. out[1] = cpu_to_le32(z);
  125. }
  126. static void xtea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  127. {
  128. u32 y, z, sum;
  129. struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
  130. const __le32 *in = (const __le32 *)src;
  131. __le32 *out = (__le32 *)dst;
  132. y = le32_to_cpu(in[0]);
  133. z = le32_to_cpu(in[1]);
  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. out[0] = cpu_to_le32(y);
  141. out[1] = cpu_to_le32(z);
  142. }
  143. static void xeta_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  144. {
  145. u32 y, z, sum = 0;
  146. u32 limit = XTEA_DELTA * XTEA_ROUNDS;
  147. struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
  148. const __le32 *in = (const __le32 *)src;
  149. __le32 *out = (__le32 *)dst;
  150. y = le32_to_cpu(in[0]);
  151. z = le32_to_cpu(in[1]);
  152. while (sum != limit) {
  153. y += (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3];
  154. sum += XTEA_DELTA;
  155. z += (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3];
  156. }
  157. out[0] = cpu_to_le32(y);
  158. out[1] = cpu_to_le32(z);
  159. }
  160. static void xeta_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  161. {
  162. u32 y, z, sum;
  163. struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
  164. const __le32 *in = (const __le32 *)src;
  165. __le32 *out = (__le32 *)dst;
  166. y = le32_to_cpu(in[0]);
  167. z = le32_to_cpu(in[1]);
  168. sum = XTEA_DELTA * XTEA_ROUNDS;
  169. while (sum) {
  170. z -= (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 & 3];
  171. sum -= XTEA_DELTA;
  172. y -= (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3];
  173. }
  174. out[0] = cpu_to_le32(y);
  175. out[1] = cpu_to_le32(z);
  176. }
  177. static struct crypto_alg tea_alg = {
  178. .cra_name = "tea",
  179. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  180. .cra_blocksize = TEA_BLOCK_SIZE,
  181. .cra_ctxsize = sizeof (struct tea_ctx),
  182. .cra_alignmask = 3,
  183. .cra_module = THIS_MODULE,
  184. .cra_list = LIST_HEAD_INIT(tea_alg.cra_list),
  185. .cra_u = { .cipher = {
  186. .cia_min_keysize = TEA_KEY_SIZE,
  187. .cia_max_keysize = TEA_KEY_SIZE,
  188. .cia_setkey = tea_setkey,
  189. .cia_encrypt = tea_encrypt,
  190. .cia_decrypt = tea_decrypt } }
  191. };
  192. static struct crypto_alg xtea_alg = {
  193. .cra_name = "xtea",
  194. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  195. .cra_blocksize = XTEA_BLOCK_SIZE,
  196. .cra_ctxsize = sizeof (struct xtea_ctx),
  197. .cra_alignmask = 3,
  198. .cra_module = THIS_MODULE,
  199. .cra_list = LIST_HEAD_INIT(xtea_alg.cra_list),
  200. .cra_u = { .cipher = {
  201. .cia_min_keysize = XTEA_KEY_SIZE,
  202. .cia_max_keysize = XTEA_KEY_SIZE,
  203. .cia_setkey = xtea_setkey,
  204. .cia_encrypt = xtea_encrypt,
  205. .cia_decrypt = xtea_decrypt } }
  206. };
  207. static struct crypto_alg xeta_alg = {
  208. .cra_name = "xeta",
  209. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  210. .cra_blocksize = XTEA_BLOCK_SIZE,
  211. .cra_ctxsize = sizeof (struct xtea_ctx),
  212. .cra_alignmask = 3,
  213. .cra_module = THIS_MODULE,
  214. .cra_list = LIST_HEAD_INIT(xtea_alg.cra_list),
  215. .cra_u = { .cipher = {
  216. .cia_min_keysize = XTEA_KEY_SIZE,
  217. .cia_max_keysize = XTEA_KEY_SIZE,
  218. .cia_setkey = xtea_setkey,
  219. .cia_encrypt = xeta_encrypt,
  220. .cia_decrypt = xeta_decrypt } }
  221. };
  222. static int __init init(void)
  223. {
  224. int ret = 0;
  225. ret = crypto_register_alg(&tea_alg);
  226. if (ret < 0)
  227. goto out;
  228. ret = crypto_register_alg(&xtea_alg);
  229. if (ret < 0) {
  230. crypto_unregister_alg(&tea_alg);
  231. goto out;
  232. }
  233. ret = crypto_register_alg(&xeta_alg);
  234. if (ret < 0) {
  235. crypto_unregister_alg(&tea_alg);
  236. crypto_unregister_alg(&xtea_alg);
  237. goto out;
  238. }
  239. out:
  240. return ret;
  241. }
  242. static void __exit fini(void)
  243. {
  244. crypto_unregister_alg(&tea_alg);
  245. crypto_unregister_alg(&xtea_alg);
  246. crypto_unregister_alg(&xeta_alg);
  247. }
  248. MODULE_ALIAS("xtea");
  249. MODULE_ALIAS("xeta");
  250. module_init(init);
  251. module_exit(fini);
  252. MODULE_LICENSE("GPL");
  253. MODULE_DESCRIPTION("TEA, XTEA & XETA Cryptographic Algorithms");