tea.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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_algs[3] = { {
  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_u = { .cipher = {
  184. .cia_min_keysize = TEA_KEY_SIZE,
  185. .cia_max_keysize = TEA_KEY_SIZE,
  186. .cia_setkey = tea_setkey,
  187. .cia_encrypt = tea_encrypt,
  188. .cia_decrypt = tea_decrypt } }
  189. }, {
  190. .cra_name = "xtea",
  191. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  192. .cra_blocksize = XTEA_BLOCK_SIZE,
  193. .cra_ctxsize = sizeof (struct xtea_ctx),
  194. .cra_alignmask = 3,
  195. .cra_module = THIS_MODULE,
  196. .cra_u = { .cipher = {
  197. .cia_min_keysize = XTEA_KEY_SIZE,
  198. .cia_max_keysize = XTEA_KEY_SIZE,
  199. .cia_setkey = xtea_setkey,
  200. .cia_encrypt = xtea_encrypt,
  201. .cia_decrypt = xtea_decrypt } }
  202. }, {
  203. .cra_name = "xeta",
  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_u = { .cipher = {
  210. .cia_min_keysize = XTEA_KEY_SIZE,
  211. .cia_max_keysize = XTEA_KEY_SIZE,
  212. .cia_setkey = xtea_setkey,
  213. .cia_encrypt = xeta_encrypt,
  214. .cia_decrypt = xeta_decrypt } }
  215. } };
  216. static int __init tea_mod_init(void)
  217. {
  218. return crypto_register_algs(tea_algs, ARRAY_SIZE(tea_algs));
  219. }
  220. static void __exit tea_mod_fini(void)
  221. {
  222. crypto_unregister_algs(tea_algs, ARRAY_SIZE(tea_algs));
  223. }
  224. MODULE_ALIAS_CRYPTO("tea");
  225. MODULE_ALIAS_CRYPTO("xtea");
  226. MODULE_ALIAS_CRYPTO("xeta");
  227. module_init(tea_mod_init);
  228. module_exit(tea_mod_fini);
  229. MODULE_LICENSE("GPL");
  230. MODULE_DESCRIPTION("TEA, XTEA & XETA Cryptographic Algorithms");