1
0

tea.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Cryptographic API.
  4. *
  5. * TEA, XTEA, and XETA crypto alogrithms
  6. *
  7. * The TEA and Xtended TEA algorithms were developed by David Wheeler
  8. * and Roger Needham at the Computer Laboratory of Cambridge University.
  9. *
  10. * Due to the order of evaluation in XTEA many people have incorrectly
  11. * implemented it. XETA (XTEA in the wrong order), exists for
  12. * compatibility with these implementations.
  13. *
  14. * Copyright (c) 2004 Aaron Grothe ajgrothe@yahoo.com
  15. */
  16. #include <crypto/algapi.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/mm.h>
  20. #include <linux/unaligned.h>
  21. #include <linux/types.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. struct tea_ctx {
  31. u32 KEY[4];
  32. };
  33. struct xtea_ctx {
  34. u32 KEY[4];
  35. };
  36. static int tea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
  37. unsigned int key_len)
  38. {
  39. struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
  40. ctx->KEY[0] = get_unaligned_le32(&in_key[0]);
  41. ctx->KEY[1] = get_unaligned_le32(&in_key[4]);
  42. ctx->KEY[2] = get_unaligned_le32(&in_key[8]);
  43. ctx->KEY[3] = get_unaligned_le32(&in_key[12]);
  44. return 0;
  45. }
  46. static void tea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  47. {
  48. u32 y, z, n, sum = 0;
  49. u32 k0, k1, k2, k3;
  50. struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
  51. y = get_unaligned_le32(&src[0]);
  52. z = get_unaligned_le32(&src[4]);
  53. k0 = ctx->KEY[0];
  54. k1 = ctx->KEY[1];
  55. k2 = ctx->KEY[2];
  56. k3 = ctx->KEY[3];
  57. n = TEA_ROUNDS;
  58. while (n-- > 0) {
  59. sum += TEA_DELTA;
  60. y += ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
  61. z += ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
  62. }
  63. put_unaligned_le32(y, &dst[0]);
  64. put_unaligned_le32(z, &dst[4]);
  65. }
  66. static void tea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  67. {
  68. u32 y, z, n, sum;
  69. u32 k0, k1, k2, k3;
  70. struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
  71. y = get_unaligned_le32(&src[0]);
  72. z = get_unaligned_le32(&src[4]);
  73. k0 = ctx->KEY[0];
  74. k1 = ctx->KEY[1];
  75. k2 = ctx->KEY[2];
  76. k3 = ctx->KEY[3];
  77. sum = TEA_DELTA << 5;
  78. n = TEA_ROUNDS;
  79. while (n-- > 0) {
  80. z -= ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
  81. y -= ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
  82. sum -= TEA_DELTA;
  83. }
  84. put_unaligned_le32(y, &dst[0]);
  85. put_unaligned_le32(z, &dst[4]);
  86. }
  87. static int xtea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
  88. unsigned int key_len)
  89. {
  90. struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
  91. ctx->KEY[0] = get_unaligned_le32(&in_key[0]);
  92. ctx->KEY[1] = get_unaligned_le32(&in_key[4]);
  93. ctx->KEY[2] = get_unaligned_le32(&in_key[8]);
  94. ctx->KEY[3] = get_unaligned_le32(&in_key[12]);
  95. return 0;
  96. }
  97. static void xtea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  98. {
  99. u32 y, z, sum = 0;
  100. u32 limit = XTEA_DELTA * XTEA_ROUNDS;
  101. struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
  102. y = get_unaligned_le32(&src[0]);
  103. z = get_unaligned_le32(&src[4]);
  104. while (sum != limit) {
  105. y += ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum&3]);
  106. sum += XTEA_DELTA;
  107. z += ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 &3]);
  108. }
  109. put_unaligned_le32(y, &dst[0]);
  110. put_unaligned_le32(z, &dst[4]);
  111. }
  112. static void xtea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  113. {
  114. u32 y, z, sum;
  115. struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
  116. y = get_unaligned_le32(&src[0]);
  117. z = get_unaligned_le32(&src[4]);
  118. sum = XTEA_DELTA * XTEA_ROUNDS;
  119. while (sum) {
  120. z -= ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 & 3]);
  121. sum -= XTEA_DELTA;
  122. y -= ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum & 3]);
  123. }
  124. put_unaligned_le32(y, &dst[0]);
  125. put_unaligned_le32(z, &dst[4]);
  126. }
  127. static void xeta_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  128. {
  129. u32 y, z, sum = 0;
  130. u32 limit = XTEA_DELTA * XTEA_ROUNDS;
  131. struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
  132. y = get_unaligned_le32(&src[0]);
  133. z = get_unaligned_le32(&src[4]);
  134. while (sum != limit) {
  135. y += (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3];
  136. sum += XTEA_DELTA;
  137. z += (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3];
  138. }
  139. put_unaligned_le32(y, &dst[0]);
  140. put_unaligned_le32(z, &dst[4]);
  141. }
  142. static void xeta_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  143. {
  144. u32 y, z, sum;
  145. struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
  146. y = get_unaligned_le32(&src[0]);
  147. z = get_unaligned_le32(&src[4]);
  148. sum = XTEA_DELTA * XTEA_ROUNDS;
  149. while (sum) {
  150. z -= (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 & 3];
  151. sum -= XTEA_DELTA;
  152. y -= (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3];
  153. }
  154. put_unaligned_le32(y, &dst[0]);
  155. put_unaligned_le32(z, &dst[4]);
  156. }
  157. static struct crypto_alg tea_algs[3] = { {
  158. .cra_name = "tea",
  159. .cra_driver_name = "tea-generic",
  160. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  161. .cra_blocksize = TEA_BLOCK_SIZE,
  162. .cra_ctxsize = sizeof (struct tea_ctx),
  163. .cra_module = THIS_MODULE,
  164. .cra_u = { .cipher = {
  165. .cia_min_keysize = TEA_KEY_SIZE,
  166. .cia_max_keysize = TEA_KEY_SIZE,
  167. .cia_setkey = tea_setkey,
  168. .cia_encrypt = tea_encrypt,
  169. .cia_decrypt = tea_decrypt } }
  170. }, {
  171. .cra_name = "xtea",
  172. .cra_driver_name = "xtea-generic",
  173. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  174. .cra_blocksize = XTEA_BLOCK_SIZE,
  175. .cra_ctxsize = sizeof (struct xtea_ctx),
  176. .cra_module = THIS_MODULE,
  177. .cra_u = { .cipher = {
  178. .cia_min_keysize = XTEA_KEY_SIZE,
  179. .cia_max_keysize = XTEA_KEY_SIZE,
  180. .cia_setkey = xtea_setkey,
  181. .cia_encrypt = xtea_encrypt,
  182. .cia_decrypt = xtea_decrypt } }
  183. }, {
  184. .cra_name = "xeta",
  185. .cra_driver_name = "xeta-generic",
  186. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  187. .cra_blocksize = XTEA_BLOCK_SIZE,
  188. .cra_ctxsize = sizeof (struct xtea_ctx),
  189. .cra_module = THIS_MODULE,
  190. .cra_u = { .cipher = {
  191. .cia_min_keysize = XTEA_KEY_SIZE,
  192. .cia_max_keysize = XTEA_KEY_SIZE,
  193. .cia_setkey = xtea_setkey,
  194. .cia_encrypt = xeta_encrypt,
  195. .cia_decrypt = xeta_decrypt } }
  196. } };
  197. static int __init tea_mod_init(void)
  198. {
  199. return crypto_register_algs(tea_algs, ARRAY_SIZE(tea_algs));
  200. }
  201. static void __exit tea_mod_fini(void)
  202. {
  203. crypto_unregister_algs(tea_algs, ARRAY_SIZE(tea_algs));
  204. }
  205. MODULE_ALIAS_CRYPTO("tea");
  206. MODULE_ALIAS_CRYPTO("xtea");
  207. MODULE_ALIAS_CRYPTO("xeta");
  208. module_init(tea_mod_init);
  209. module_exit(tea_mod_fini);
  210. MODULE_LICENSE("GPL");
  211. MODULE_DESCRIPTION("TEA, XTEA & XETA Cryptographic Algorithms");