xcbc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 consts[];
  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 odds[];
  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. struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
  49. u8 *consts = ctx->consts;
  50. int err = 0;
  51. u8 key1[XCBC_BLOCKSIZE];
  52. int bs = sizeof(key1);
  53. if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
  54. return err;
  55. crypto_cipher_encrypt_one(ctx->child, consts, (u8 *)ks + bs);
  56. crypto_cipher_encrypt_one(ctx->child, consts + bs, (u8 *)ks + bs * 2);
  57. crypto_cipher_encrypt_one(ctx->child, key1, (u8 *)ks);
  58. return crypto_cipher_setkey(ctx->child, key1, bs);
  59. }
  60. static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
  61. {
  62. struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
  63. int bs = crypto_shash_blocksize(pdesc->tfm);
  64. u8 *prev = &ctx->odds[bs];
  65. ctx->len = 0;
  66. memset(prev, 0, bs);
  67. return 0;
  68. }
  69. static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
  70. unsigned int len)
  71. {
  72. struct crypto_shash *parent = pdesc->tfm;
  73. struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
  74. struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
  75. struct crypto_cipher *tfm = tctx->child;
  76. int bs = crypto_shash_blocksize(parent);
  77. u8 *odds = ctx->odds;
  78. u8 *prev = odds + bs;
  79. /* checking the data can fill the block */
  80. if ((ctx->len + len) <= bs) {
  81. memcpy(odds + ctx->len, p, len);
  82. ctx->len += len;
  83. return 0;
  84. }
  85. /* filling odds with new data and encrypting it */
  86. memcpy(odds + ctx->len, p, bs - ctx->len);
  87. len -= bs - ctx->len;
  88. p += bs - ctx->len;
  89. crypto_xor(prev, odds, bs);
  90. crypto_cipher_encrypt_one(tfm, prev, prev);
  91. /* clearing the length */
  92. ctx->len = 0;
  93. /* encrypting the rest of data */
  94. while (len > bs) {
  95. crypto_xor(prev, p, bs);
  96. crypto_cipher_encrypt_one(tfm, prev, prev);
  97. p += bs;
  98. len -= bs;
  99. }
  100. /* keeping the surplus of blocksize */
  101. if (len) {
  102. memcpy(odds, p, len);
  103. ctx->len = len;
  104. }
  105. return 0;
  106. }
  107. static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
  108. {
  109. struct crypto_shash *parent = pdesc->tfm;
  110. struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
  111. struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
  112. struct crypto_cipher *tfm = tctx->child;
  113. int bs = crypto_shash_blocksize(parent);
  114. u8 *odds = ctx->odds;
  115. u8 *prev = odds + bs;
  116. unsigned int offset = 0;
  117. if (ctx->len != bs) {
  118. unsigned int rlen;
  119. u8 *p = odds + ctx->len;
  120. *p = 0x80;
  121. p++;
  122. rlen = bs - ctx->len -1;
  123. if (rlen)
  124. memset(p, 0, rlen);
  125. offset += bs;
  126. }
  127. crypto_xor(prev, odds, bs);
  128. crypto_xor(prev, &tctx->consts[offset], bs);
  129. crypto_cipher_encrypt_one(tfm, out, prev);
  130. return 0;
  131. }
  132. static int xcbc_init_tfm(struct crypto_tfm *tfm)
  133. {
  134. struct crypto_cipher *cipher;
  135. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  136. struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
  137. struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
  138. cipher = crypto_spawn_cipher(spawn);
  139. if (IS_ERR(cipher))
  140. return PTR_ERR(cipher);
  141. ctx->child = cipher;
  142. return 0;
  143. };
  144. static void xcbc_exit_tfm(struct crypto_tfm *tfm)
  145. {
  146. struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
  147. crypto_free_cipher(ctx->child);
  148. }
  149. static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
  150. {
  151. struct shash_instance *inst;
  152. struct crypto_cipher_spawn *spawn;
  153. struct crypto_alg *alg;
  154. u32 mask;
  155. int err;
  156. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
  157. if (err)
  158. return err;
  159. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  160. if (!inst)
  161. return -ENOMEM;
  162. spawn = shash_instance_ctx(inst);
  163. err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
  164. crypto_attr_alg_name(tb[1]), 0, mask);
  165. if (err)
  166. goto err_free_inst;
  167. alg = crypto_spawn_cipher_alg(spawn);
  168. err = -EINVAL;
  169. if (alg->cra_blocksize != XCBC_BLOCKSIZE)
  170. goto err_free_inst;
  171. err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
  172. if (err)
  173. goto err_free_inst;
  174. inst->alg.base.cra_priority = alg->cra_priority;
  175. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  176. inst->alg.base.cra_ctxsize = sizeof(struct xcbc_tfm_ctx) +
  177. alg->cra_blocksize * 2;
  178. inst->alg.digestsize = alg->cra_blocksize;
  179. inst->alg.descsize = sizeof(struct xcbc_desc_ctx) +
  180. alg->cra_blocksize * 2;
  181. inst->alg.base.cra_init = xcbc_init_tfm;
  182. inst->alg.base.cra_exit = xcbc_exit_tfm;
  183. inst->alg.init = crypto_xcbc_digest_init;
  184. inst->alg.update = crypto_xcbc_digest_update;
  185. inst->alg.final = crypto_xcbc_digest_final;
  186. inst->alg.setkey = crypto_xcbc_digest_setkey;
  187. inst->free = shash_free_singlespawn_instance;
  188. err = shash_register_instance(tmpl, inst);
  189. if (err) {
  190. err_free_inst:
  191. shash_free_singlespawn_instance(inst);
  192. }
  193. return err;
  194. }
  195. static struct crypto_template crypto_xcbc_tmpl = {
  196. .name = "xcbc",
  197. .create = xcbc_create,
  198. .module = THIS_MODULE,
  199. };
  200. static int __init crypto_xcbc_module_init(void)
  201. {
  202. return crypto_register_template(&crypto_xcbc_tmpl);
  203. }
  204. static void __exit crypto_xcbc_module_exit(void)
  205. {
  206. crypto_unregister_template(&crypto_xcbc_tmpl);
  207. }
  208. subsys_initcall(crypto_xcbc_module_init);
  209. module_exit(crypto_xcbc_module_exit);
  210. MODULE_LICENSE("GPL");
  211. MODULE_DESCRIPTION("XCBC keyed hash algorithm");
  212. MODULE_ALIAS_CRYPTO("xcbc");
  213. MODULE_IMPORT_NS(CRYPTO_INTERNAL);