xcbc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 <crypto/utils.h>
  11. #include <linux/err.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/string.h>
  16. static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
  17. 0x02020202, 0x02020202, 0x02020202, 0x02020202,
  18. 0x03030303, 0x03030303, 0x03030303, 0x03030303};
  19. /*
  20. * +------------------------
  21. * | <parent tfm>
  22. * +------------------------
  23. * | xcbc_tfm_ctx
  24. * +------------------------
  25. * | consts (block size * 2)
  26. * +------------------------
  27. */
  28. struct xcbc_tfm_ctx {
  29. struct crypto_cipher *child;
  30. u8 consts[];
  31. };
  32. #define XCBC_BLOCKSIZE 16
  33. static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
  34. const u8 *inkey, unsigned int keylen)
  35. {
  36. struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
  37. u8 *consts = ctx->consts;
  38. int err = 0;
  39. u8 key1[XCBC_BLOCKSIZE];
  40. int bs = sizeof(key1);
  41. if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
  42. return err;
  43. crypto_cipher_encrypt_one(ctx->child, consts, (u8 *)ks + bs);
  44. crypto_cipher_encrypt_one(ctx->child, consts + bs, (u8 *)ks + bs * 2);
  45. crypto_cipher_encrypt_one(ctx->child, key1, (u8 *)ks);
  46. return crypto_cipher_setkey(ctx->child, key1, bs);
  47. }
  48. static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
  49. {
  50. int bs = crypto_shash_blocksize(pdesc->tfm);
  51. u8 *prev = shash_desc_ctx(pdesc);
  52. memset(prev, 0, bs);
  53. return 0;
  54. }
  55. static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
  56. unsigned int len)
  57. {
  58. struct crypto_shash *parent = pdesc->tfm;
  59. struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
  60. struct crypto_cipher *tfm = tctx->child;
  61. int bs = crypto_shash_blocksize(parent);
  62. u8 *prev = shash_desc_ctx(pdesc);
  63. do {
  64. crypto_xor(prev, p, bs);
  65. crypto_cipher_encrypt_one(tfm, prev, prev);
  66. p += bs;
  67. len -= bs;
  68. } while (len >= bs);
  69. return len;
  70. }
  71. static int crypto_xcbc_digest_finup(struct shash_desc *pdesc, const u8 *src,
  72. unsigned int len, u8 *out)
  73. {
  74. struct crypto_shash *parent = pdesc->tfm;
  75. struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
  76. struct crypto_cipher *tfm = tctx->child;
  77. int bs = crypto_shash_blocksize(parent);
  78. u8 *prev = shash_desc_ctx(pdesc);
  79. unsigned int offset = 0;
  80. crypto_xor(prev, src, len);
  81. if (len != bs) {
  82. prev[len] ^= 0x80;
  83. offset += bs;
  84. }
  85. crypto_xor(prev, &tctx->consts[offset], bs);
  86. crypto_cipher_encrypt_one(tfm, out, prev);
  87. return 0;
  88. }
  89. static int xcbc_init_tfm(struct crypto_tfm *tfm)
  90. {
  91. struct crypto_cipher *cipher;
  92. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  93. struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
  94. struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
  95. cipher = crypto_spawn_cipher(spawn);
  96. if (IS_ERR(cipher))
  97. return PTR_ERR(cipher);
  98. ctx->child = cipher;
  99. return 0;
  100. };
  101. static void xcbc_exit_tfm(struct crypto_tfm *tfm)
  102. {
  103. struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
  104. crypto_free_cipher(ctx->child);
  105. }
  106. static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
  107. {
  108. struct shash_instance *inst;
  109. struct crypto_cipher_spawn *spawn;
  110. struct crypto_alg *alg;
  111. u32 mask;
  112. int err;
  113. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
  114. if (err)
  115. return err;
  116. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  117. if (!inst)
  118. return -ENOMEM;
  119. spawn = shash_instance_ctx(inst);
  120. err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
  121. crypto_attr_alg_name(tb[1]), 0, mask);
  122. if (err)
  123. goto err_free_inst;
  124. alg = crypto_spawn_cipher_alg(spawn);
  125. err = -EINVAL;
  126. if (alg->cra_blocksize != XCBC_BLOCKSIZE)
  127. goto err_free_inst;
  128. err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
  129. if (err)
  130. goto err_free_inst;
  131. inst->alg.base.cra_priority = alg->cra_priority;
  132. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  133. inst->alg.base.cra_ctxsize = sizeof(struct xcbc_tfm_ctx) +
  134. alg->cra_blocksize * 2;
  135. inst->alg.base.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY |
  136. CRYPTO_AHASH_ALG_FINAL_NONZERO;
  137. inst->alg.digestsize = alg->cra_blocksize;
  138. inst->alg.descsize = alg->cra_blocksize;
  139. inst->alg.base.cra_init = xcbc_init_tfm;
  140. inst->alg.base.cra_exit = xcbc_exit_tfm;
  141. inst->alg.init = crypto_xcbc_digest_init;
  142. inst->alg.update = crypto_xcbc_digest_update;
  143. inst->alg.finup = crypto_xcbc_digest_finup;
  144. inst->alg.setkey = crypto_xcbc_digest_setkey;
  145. inst->free = shash_free_singlespawn_instance;
  146. err = shash_register_instance(tmpl, inst);
  147. if (err) {
  148. err_free_inst:
  149. shash_free_singlespawn_instance(inst);
  150. }
  151. return err;
  152. }
  153. static struct crypto_template crypto_xcbc_tmpl = {
  154. .name = "xcbc",
  155. .create = xcbc_create,
  156. .module = THIS_MODULE,
  157. };
  158. static int __init crypto_xcbc_module_init(void)
  159. {
  160. return crypto_register_template(&crypto_xcbc_tmpl);
  161. }
  162. static void __exit crypto_xcbc_module_exit(void)
  163. {
  164. crypto_unregister_template(&crypto_xcbc_tmpl);
  165. }
  166. module_init(crypto_xcbc_module_init);
  167. module_exit(crypto_xcbc_module_exit);
  168. MODULE_LICENSE("GPL");
  169. MODULE_DESCRIPTION("XCBC keyed hash algorithm");
  170. MODULE_ALIAS_CRYPTO("xcbc");
  171. MODULE_IMPORT_NS("CRYPTO_INTERNAL");