tea.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Cryptographic API.
  3. *
  4. * TEA and Xtended TEA Algorithms
  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. * Copyright (c) 2004 Aaron Grothe ajgrothe@yahoo.com
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. */
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/mm.h>
  20. #include <asm/scatterlist.h>
  21. #include <linux/crypto.h>
  22. #define TEA_KEY_SIZE 16
  23. #define TEA_BLOCK_SIZE 8
  24. #define TEA_ROUNDS 32
  25. #define TEA_DELTA 0x9e3779b9
  26. #define XTEA_KEY_SIZE 16
  27. #define XTEA_BLOCK_SIZE 8
  28. #define XTEA_ROUNDS 32
  29. #define XTEA_DELTA 0x9e3779b9
  30. #define u32_in(x) le32_to_cpu(*(const __le32 *)(x))
  31. #define u32_out(to, from) (*(__le32 *)(to) = cpu_to_le32(from))
  32. struct tea_ctx {
  33. u32 KEY[4];
  34. };
  35. struct xtea_ctx {
  36. u32 KEY[4];
  37. };
  38. static int tea_setkey(void *ctx_arg, const u8 *in_key,
  39. unsigned int key_len, u32 *flags)
  40. {
  41. struct tea_ctx *ctx = ctx_arg;
  42. if (key_len != 16)
  43. {
  44. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  45. return -EINVAL;
  46. }
  47. ctx->KEY[0] = u32_in (in_key);
  48. ctx->KEY[1] = u32_in (in_key + 4);
  49. ctx->KEY[2] = u32_in (in_key + 8);
  50. ctx->KEY[3] = u32_in (in_key + 12);
  51. return 0;
  52. }
  53. static void tea_encrypt(void *ctx_arg, u8 *dst, const u8 *src)
  54. {
  55. u32 y, z, n, sum = 0;
  56. u32 k0, k1, k2, k3;
  57. struct tea_ctx *ctx = ctx_arg;
  58. y = u32_in (src);
  59. z = u32_in (src + 4);
  60. k0 = ctx->KEY[0];
  61. k1 = ctx->KEY[1];
  62. k2 = ctx->KEY[2];
  63. k3 = ctx->KEY[3];
  64. n = TEA_ROUNDS;
  65. while (n-- > 0) {
  66. sum += TEA_DELTA;
  67. y += ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
  68. z += ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
  69. }
  70. u32_out (dst, y);
  71. u32_out (dst + 4, z);
  72. }
  73. static void tea_decrypt(void *ctx_arg, u8 *dst, const u8 *src)
  74. {
  75. u32 y, z, n, sum;
  76. u32 k0, k1, k2, k3;
  77. struct tea_ctx *ctx = ctx_arg;
  78. y = u32_in (src);
  79. z = u32_in (src + 4);
  80. k0 = ctx->KEY[0];
  81. k1 = ctx->KEY[1];
  82. k2 = ctx->KEY[2];
  83. k3 = ctx->KEY[3];
  84. sum = TEA_DELTA << 5;
  85. n = TEA_ROUNDS;
  86. while (n-- > 0) {
  87. z -= ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
  88. y -= ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
  89. sum -= TEA_DELTA;
  90. }
  91. u32_out (dst, y);
  92. u32_out (dst + 4, z);
  93. }
  94. static int xtea_setkey(void *ctx_arg, const u8 *in_key,
  95. unsigned int key_len, u32 *flags)
  96. {
  97. struct xtea_ctx *ctx = ctx_arg;
  98. if (key_len != 16)
  99. {
  100. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  101. return -EINVAL;
  102. }
  103. ctx->KEY[0] = u32_in (in_key);
  104. ctx->KEY[1] = u32_in (in_key + 4);
  105. ctx->KEY[2] = u32_in (in_key + 8);
  106. ctx->KEY[3] = u32_in (in_key + 12);
  107. return 0;
  108. }
  109. static void xtea_encrypt(void *ctx_arg, u8 *dst, const u8 *src)
  110. {
  111. u32 y, z, sum = 0;
  112. u32 limit = XTEA_DELTA * XTEA_ROUNDS;
  113. struct xtea_ctx *ctx = ctx_arg;
  114. y = u32_in (src);
  115. z = u32_in (src + 4);
  116. while (sum != limit) {
  117. y += (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3];
  118. sum += XTEA_DELTA;
  119. z += (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3];
  120. }
  121. u32_out (dst, y);
  122. u32_out (dst + 4, z);
  123. }
  124. static void xtea_decrypt(void *ctx_arg, u8 *dst, const u8 *src)
  125. {
  126. u32 y, z, sum;
  127. struct tea_ctx *ctx = ctx_arg;
  128. y = u32_in (src);
  129. z = u32_in (src + 4);
  130. sum = XTEA_DELTA * XTEA_ROUNDS;
  131. while (sum) {
  132. z -= (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 & 3];
  133. sum -= XTEA_DELTA;
  134. y -= (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3];
  135. }
  136. u32_out (dst, y);
  137. u32_out (dst + 4, z);
  138. }
  139. static struct crypto_alg tea_alg = {
  140. .cra_name = "tea",
  141. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  142. .cra_blocksize = TEA_BLOCK_SIZE,
  143. .cra_ctxsize = sizeof (struct tea_ctx),
  144. .cra_module = THIS_MODULE,
  145. .cra_list = LIST_HEAD_INIT(tea_alg.cra_list),
  146. .cra_u = { .cipher = {
  147. .cia_min_keysize = TEA_KEY_SIZE,
  148. .cia_max_keysize = TEA_KEY_SIZE,
  149. .cia_setkey = tea_setkey,
  150. .cia_encrypt = tea_encrypt,
  151. .cia_decrypt = tea_decrypt } }
  152. };
  153. static struct crypto_alg xtea_alg = {
  154. .cra_name = "xtea",
  155. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  156. .cra_blocksize = XTEA_BLOCK_SIZE,
  157. .cra_ctxsize = sizeof (struct xtea_ctx),
  158. .cra_module = THIS_MODULE,
  159. .cra_list = LIST_HEAD_INIT(xtea_alg.cra_list),
  160. .cra_u = { .cipher = {
  161. .cia_min_keysize = XTEA_KEY_SIZE,
  162. .cia_max_keysize = XTEA_KEY_SIZE,
  163. .cia_setkey = xtea_setkey,
  164. .cia_encrypt = xtea_encrypt,
  165. .cia_decrypt = xtea_decrypt } }
  166. };
  167. static int __init init(void)
  168. {
  169. int ret = 0;
  170. ret = crypto_register_alg(&tea_alg);
  171. if (ret < 0)
  172. goto out;
  173. ret = crypto_register_alg(&xtea_alg);
  174. if (ret < 0) {
  175. crypto_unregister_alg(&tea_alg);
  176. goto out;
  177. }
  178. out:
  179. return ret;
  180. }
  181. static void __exit fini(void)
  182. {
  183. crypto_unregister_alg(&tea_alg);
  184. crypto_unregister_alg(&xtea_alg);
  185. }
  186. MODULE_ALIAS("xtea");
  187. module_init(init);
  188. module_exit(fini);
  189. MODULE_LICENSE("GPL");
  190. MODULE_DESCRIPTION("TEA & XTEA Cryptographic Algorithms");