xcbc.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C)2006 USAGI/WIDE Project
  4. *
  5. * Author:
  6. * Kazunori Miyazawa <miyazawa@linux-ipv6.org>
  7. */
  8. #include <crypto/internal/cipher.h>
  9. #include <crypto/internal/hash.h>
  10. #include <linux/err.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
  14. 0x02020202, 0x02020202, 0x02020202, 0x02020202,
  15. 0x03030303, 0x03030303, 0x03030303, 0x03030303};
  16. /*
  17. * +------------------------
  18. * | <parent tfm>
  19. * +------------------------
  20. * | xcbc_tfm_ctx
  21. * +------------------------
  22. * | consts (block size * 2)
  23. * +------------------------
  24. */
  25. struct xcbc_tfm_ctx {
  26. struct crypto_cipher *child;
  27. u8 ctx[];
  28. };
  29. /*
  30. * +------------------------
  31. * | <shash desc>
  32. * +------------------------
  33. * | xcbc_desc_ctx
  34. * +------------------------
  35. * | odds (block size)
  36. * +------------------------
  37. * | prev (block size)
  38. * +------------------------
  39. */
  40. struct xcbc_desc_ctx {
  41. unsigned int len;
  42. u8 ctx[];
  43. };
  44. #define XCBC_BLOCKSIZE 16
  45. static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
  46. const u8 *inkey, unsigned int keylen)
  47. {
  48. unsigned long alignmask = crypto_shash_alignmask(parent);
  49. struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
  50. u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
  51. int err = 0;
  52. u8 key1[XCBC_BLOCKSIZE];
  53. int bs = sizeof(key1);
  54. if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
  55. return err;
  56. crypto_cipher_encrypt_one(ctx->child, consts, (u8 *)ks + bs);
  57. crypto_cipher_encrypt_one(ctx->child, consts + bs, (u8 *)ks + bs * 2);
  58. crypto_cipher_encrypt_one(ctx->child, key1, (u8 *)ks);
  59. return crypto_cipher_setkey(ctx->child, key1, bs);
  60. }
  61. static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
  62. {
  63. unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
  64. struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
  65. int bs = crypto_shash_blocksize(pdesc->tfm);
  66. u8 *prev = PTR_ALIGN(&ctx->ctx[0], alignmask + 1) + bs;
  67. ctx->len = 0;
  68. memset(prev, 0, bs);
  69. return 0;
  70. }
  71. static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
  72. unsigned int len)
  73. {
  74. struct crypto_shash *parent = pdesc->tfm;
  75. unsigned long alignmask = crypto_shash_alignmask(parent);
  76. struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
  77. struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
  78. struct crypto_cipher *tfm = tctx->child;
  79. int bs = crypto_shash_blocksize(parent);
  80. u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
  81. u8 *prev = odds + bs;
  82. /* checking the data can fill the block */
  83. if ((ctx->len + len) <= bs) {
  84. memcpy(odds + ctx->len, p, len);
  85. ctx->len += len;
  86. return 0;
  87. }
  88. /* filling odds with new data and encrypting it */
  89. memcpy(odds + ctx->len, p, bs - ctx->len);
  90. len -= bs - ctx->len;
  91. p += bs - ctx->len;
  92. crypto_xor(prev, odds, bs);
  93. crypto_cipher_encrypt_one(tfm, prev, prev);
  94. /* clearing the length */
  95. ctx->len = 0;
  96. /* encrypting the rest of data */
  97. while (len > bs) {
  98. crypto_xor(prev, p, bs);
  99. crypto_cipher_encrypt_one(tfm, prev, prev);
  100. p += bs;
  101. len -= bs;
  102. }
  103. /* keeping the surplus of blocksize */
  104. if (len) {
  105. memcpy(odds, p, len);
  106. ctx->len = len;
  107. }
  108. return 0;
  109. }
  110. static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
  111. {
  112. struct crypto_shash *parent = pdesc->tfm;
  113. unsigned long alignmask = crypto_shash_alignmask(parent);
  114. struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
  115. struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
  116. struct crypto_cipher *tfm = tctx->child;
  117. int bs = crypto_shash_blocksize(parent);
  118. u8 *consts = PTR_ALIGN(&tctx->ctx[0], alignmask + 1);
  119. u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
  120. u8 *prev = odds + bs;
  121. unsigned int offset = 0;
  122. if (ctx->len != bs) {
  123. unsigned int rlen;
  124. u8 *p = odds + ctx->len;
  125. *p = 0x80;
  126. p++;
  127. rlen = bs - ctx->len -1;
  128. if (rlen)
  129. memset(p, 0, rlen);
  130. offset += bs;
  131. }
  132. crypto_xor(prev, odds, bs);
  133. crypto_xor(prev, consts + offset, bs);
  134. crypto_cipher_encrypt_one(tfm, out, prev);
  135. return 0;
  136. }
  137. static int xcbc_init_tfm(struct crypto_tfm *tfm)
  138. {
  139. struct crypto_cipher *cipher;
  140. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  141. struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
  142. struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
  143. cipher = crypto_spawn_cipher(spawn);
  144. if (IS_ERR(cipher))
  145. return PTR_ERR(cipher);
  146. ctx->child = cipher;
  147. return 0;
  148. };
  149. static void xcbc_exit_tfm(struct crypto_tfm *tfm)
  150. {
  151. struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
  152. crypto_free_cipher(ctx->child);
  153. }
  154. static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
  155. {
  156. struct shash_instance *inst;
  157. struct crypto_cipher_spawn *spawn;
  158. struct crypto_alg *alg;
  159. unsigned long alignmask;
  160. u32 mask;
  161. int err;
  162. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
  163. if (err)
  164. return err;
  165. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  166. if (!inst)
  167. return -ENOMEM;
  168. spawn = shash_instance_ctx(inst);
  169. err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
  170. crypto_attr_alg_name(tb[1]), 0, mask);
  171. if (err)
  172. goto err_free_inst;
  173. alg = crypto_spawn_cipher_alg(spawn);
  174. err = -EINVAL;
  175. if (alg->cra_blocksize != XCBC_BLOCKSIZE)
  176. goto err_free_inst;
  177. err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
  178. if (err)
  179. goto err_free_inst;
  180. alignmask = alg->cra_alignmask | 3;
  181. inst->alg.base.cra_alignmask = alignmask;
  182. inst->alg.base.cra_priority = alg->cra_priority;
  183. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  184. inst->alg.digestsize = alg->cra_blocksize;
  185. inst->alg.descsize = ALIGN(sizeof(struct xcbc_desc_ctx),
  186. crypto_tfm_ctx_alignment()) +
  187. (alignmask &
  188. ~(crypto_tfm_ctx_alignment() - 1)) +
  189. alg->cra_blocksize * 2;
  190. inst->alg.base.cra_ctxsize = ALIGN(sizeof(struct xcbc_tfm_ctx),
  191. alignmask + 1) +
  192. alg->cra_blocksize * 2;
  193. inst->alg.base.cra_init = xcbc_init_tfm;
  194. inst->alg.base.cra_exit = xcbc_exit_tfm;
  195. inst->alg.init = crypto_xcbc_digest_init;
  196. inst->alg.update = crypto_xcbc_digest_update;
  197. inst->alg.final = crypto_xcbc_digest_final;
  198. inst->alg.setkey = crypto_xcbc_digest_setkey;
  199. inst->free = shash_free_singlespawn_instance;
  200. err = shash_register_instance(tmpl, inst);
  201. if (err) {
  202. err_free_inst:
  203. shash_free_singlespawn_instance(inst);
  204. }
  205. return err;
  206. }
  207. static struct crypto_template crypto_xcbc_tmpl = {
  208. .name = "xcbc",
  209. .create = xcbc_create,
  210. .module = THIS_MODULE,
  211. };
  212. static int __init crypto_xcbc_module_init(void)
  213. {
  214. return crypto_register_template(&crypto_xcbc_tmpl);
  215. }
  216. static void __exit crypto_xcbc_module_exit(void)
  217. {
  218. crypto_unregister_template(&crypto_xcbc_tmpl);
  219. }
  220. subsys_initcall(crypto_xcbc_module_init);
  221. module_exit(crypto_xcbc_module_exit);
  222. MODULE_LICENSE("GPL");
  223. MODULE_DESCRIPTION("XCBC keyed hash algorithm");
  224. MODULE_ALIAS_CRYPTO("xcbc");
  225. MODULE_IMPORT_NS(CRYPTO_INTERNAL);