tea.c 7.1 KB

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