cmac.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * CMAC: Cipher Block Mode for Authentication
  4. *
  5. * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
  6. *
  7. * Based on work by:
  8. * Copyright © 2013 Tom St Denis <tstdenis@elliptictech.com>
  9. * Based on crypto/xcbc.c:
  10. * Copyright © 2006 USAGI/WIDE Project,
  11. * Author: Kazunori Miyazawa <miyazawa@linux-ipv6.org>
  12. */
  13. #include <crypto/internal/cipher.h>
  14. #include <crypto/internal/hash.h>
  15. #include <crypto/utils.h>
  16. #include <linux/err.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/string.h>
  21. /*
  22. * +------------------------
  23. * | <parent tfm>
  24. * +------------------------
  25. * | cmac_tfm_ctx
  26. * +------------------------
  27. * | consts (block size * 2)
  28. * +------------------------
  29. */
  30. struct cmac_tfm_ctx {
  31. struct crypto_cipher *child;
  32. __be64 consts[];
  33. };
  34. static int crypto_cmac_digest_setkey(struct crypto_shash *parent,
  35. const u8 *inkey, unsigned int keylen)
  36. {
  37. struct cmac_tfm_ctx *ctx = crypto_shash_ctx(parent);
  38. unsigned int bs = crypto_shash_blocksize(parent);
  39. __be64 *consts = ctx->consts;
  40. u64 _const[2];
  41. int i, err = 0;
  42. u8 msb_mask, gfmask;
  43. err = crypto_cipher_setkey(ctx->child, inkey, keylen);
  44. if (err)
  45. return err;
  46. /* encrypt the zero block */
  47. memset(consts, 0, bs);
  48. crypto_cipher_encrypt_one(ctx->child, (u8 *)consts, (u8 *)consts);
  49. switch (bs) {
  50. case 16:
  51. gfmask = 0x87;
  52. _const[0] = be64_to_cpu(consts[1]);
  53. _const[1] = be64_to_cpu(consts[0]);
  54. /* gf(2^128) multiply zero-ciphertext with u and u^2 */
  55. for (i = 0; i < 4; i += 2) {
  56. msb_mask = ((s64)_const[1] >> 63) & gfmask;
  57. _const[1] = (_const[1] << 1) | (_const[0] >> 63);
  58. _const[0] = (_const[0] << 1) ^ msb_mask;
  59. consts[i + 0] = cpu_to_be64(_const[1]);
  60. consts[i + 1] = cpu_to_be64(_const[0]);
  61. }
  62. break;
  63. case 8:
  64. gfmask = 0x1B;
  65. _const[0] = be64_to_cpu(consts[0]);
  66. /* gf(2^64) multiply zero-ciphertext with u and u^2 */
  67. for (i = 0; i < 2; i++) {
  68. msb_mask = ((s64)_const[0] >> 63) & gfmask;
  69. _const[0] = (_const[0] << 1) ^ msb_mask;
  70. consts[i] = cpu_to_be64(_const[0]);
  71. }
  72. break;
  73. }
  74. return 0;
  75. }
  76. static int crypto_cmac_digest_init(struct shash_desc *pdesc)
  77. {
  78. int bs = crypto_shash_blocksize(pdesc->tfm);
  79. u8 *prev = shash_desc_ctx(pdesc);
  80. memset(prev, 0, bs);
  81. return 0;
  82. }
  83. static int crypto_cmac_digest_update(struct shash_desc *pdesc, const u8 *p,
  84. unsigned int len)
  85. {
  86. struct crypto_shash *parent = pdesc->tfm;
  87. struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
  88. struct crypto_cipher *tfm = tctx->child;
  89. int bs = crypto_shash_blocksize(parent);
  90. u8 *prev = shash_desc_ctx(pdesc);
  91. do {
  92. crypto_xor(prev, p, bs);
  93. crypto_cipher_encrypt_one(tfm, prev, prev);
  94. p += bs;
  95. len -= bs;
  96. } while (len >= bs);
  97. return len;
  98. }
  99. static int crypto_cmac_digest_finup(struct shash_desc *pdesc, const u8 *src,
  100. unsigned int len, u8 *out)
  101. {
  102. struct crypto_shash *parent = pdesc->tfm;
  103. struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
  104. struct crypto_cipher *tfm = tctx->child;
  105. int bs = crypto_shash_blocksize(parent);
  106. u8 *prev = shash_desc_ctx(pdesc);
  107. unsigned int offset = 0;
  108. crypto_xor(prev, src, len);
  109. if (len != bs) {
  110. prev[len] ^= 0x80;
  111. offset += bs;
  112. }
  113. crypto_xor(prev, (const u8 *)tctx->consts + offset, bs);
  114. crypto_cipher_encrypt_one(tfm, out, prev);
  115. return 0;
  116. }
  117. static int cmac_init_tfm(struct crypto_shash *tfm)
  118. {
  119. struct shash_instance *inst = shash_alg_instance(tfm);
  120. struct cmac_tfm_ctx *ctx = crypto_shash_ctx(tfm);
  121. struct crypto_cipher_spawn *spawn;
  122. struct crypto_cipher *cipher;
  123. spawn = shash_instance_ctx(inst);
  124. cipher = crypto_spawn_cipher(spawn);
  125. if (IS_ERR(cipher))
  126. return PTR_ERR(cipher);
  127. ctx->child = cipher;
  128. return 0;
  129. }
  130. static int cmac_clone_tfm(struct crypto_shash *tfm, struct crypto_shash *otfm)
  131. {
  132. struct cmac_tfm_ctx *octx = crypto_shash_ctx(otfm);
  133. struct cmac_tfm_ctx *ctx = crypto_shash_ctx(tfm);
  134. struct crypto_cipher *cipher;
  135. cipher = crypto_clone_cipher(octx->child);
  136. if (IS_ERR(cipher))
  137. return PTR_ERR(cipher);
  138. ctx->child = cipher;
  139. return 0;
  140. }
  141. static void cmac_exit_tfm(struct crypto_shash *tfm)
  142. {
  143. struct cmac_tfm_ctx *ctx = crypto_shash_ctx(tfm);
  144. crypto_free_cipher(ctx->child);
  145. }
  146. static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb)
  147. {
  148. struct shash_instance *inst;
  149. struct crypto_cipher_spawn *spawn;
  150. struct crypto_alg *alg;
  151. u32 mask;
  152. int err;
  153. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
  154. if (err)
  155. return err;
  156. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  157. if (!inst)
  158. return -ENOMEM;
  159. spawn = shash_instance_ctx(inst);
  160. err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
  161. crypto_attr_alg_name(tb[1]), 0, mask);
  162. if (err)
  163. goto err_free_inst;
  164. alg = crypto_spawn_cipher_alg(spawn);
  165. switch (alg->cra_blocksize) {
  166. case 16:
  167. case 8:
  168. break;
  169. default:
  170. err = -EINVAL;
  171. goto err_free_inst;
  172. }
  173. err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
  174. if (err)
  175. goto err_free_inst;
  176. inst->alg.base.cra_priority = alg->cra_priority;
  177. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  178. inst->alg.base.cra_ctxsize = sizeof(struct cmac_tfm_ctx) +
  179. alg->cra_blocksize * 2;
  180. inst->alg.base.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY |
  181. CRYPTO_AHASH_ALG_FINAL_NONZERO;
  182. inst->alg.digestsize = alg->cra_blocksize;
  183. inst->alg.descsize = alg->cra_blocksize;
  184. inst->alg.init = crypto_cmac_digest_init;
  185. inst->alg.update = crypto_cmac_digest_update;
  186. inst->alg.finup = crypto_cmac_digest_finup;
  187. inst->alg.setkey = crypto_cmac_digest_setkey;
  188. inst->alg.init_tfm = cmac_init_tfm;
  189. inst->alg.clone_tfm = cmac_clone_tfm;
  190. inst->alg.exit_tfm = cmac_exit_tfm;
  191. inst->free = shash_free_singlespawn_instance;
  192. err = shash_register_instance(tmpl, inst);
  193. if (err) {
  194. err_free_inst:
  195. shash_free_singlespawn_instance(inst);
  196. }
  197. return err;
  198. }
  199. static struct crypto_template crypto_cmac_tmpl = {
  200. .name = "cmac",
  201. .create = cmac_create,
  202. .module = THIS_MODULE,
  203. };
  204. static int __init crypto_cmac_module_init(void)
  205. {
  206. return crypto_register_template(&crypto_cmac_tmpl);
  207. }
  208. static void __exit crypto_cmac_module_exit(void)
  209. {
  210. crypto_unregister_template(&crypto_cmac_tmpl);
  211. }
  212. module_init(crypto_cmac_module_init);
  213. module_exit(crypto_cmac_module_exit);
  214. MODULE_LICENSE("GPL");
  215. MODULE_DESCRIPTION("CMAC keyed hash algorithm");
  216. MODULE_ALIAS_CRYPTO("cmac");
  217. MODULE_IMPORT_NS("CRYPTO_INTERNAL");