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