cmac.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 <linux/err.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. /*
  19. * +------------------------
  20. * | <parent tfm>
  21. * +------------------------
  22. * | cmac_tfm_ctx
  23. * +------------------------
  24. * | consts (block size * 2)
  25. * +------------------------
  26. */
  27. struct cmac_tfm_ctx {
  28. struct crypto_cipher *child;
  29. u8 ctx[];
  30. };
  31. /*
  32. * +------------------------
  33. * | <shash desc>
  34. * +------------------------
  35. * | cmac_desc_ctx
  36. * +------------------------
  37. * | odds (block size)
  38. * +------------------------
  39. * | prev (block size)
  40. * +------------------------
  41. */
  42. struct cmac_desc_ctx {
  43. unsigned int len;
  44. u8 ctx[];
  45. };
  46. static int crypto_cmac_digest_setkey(struct crypto_shash *parent,
  47. const u8 *inkey, unsigned int keylen)
  48. {
  49. unsigned long alignmask = crypto_shash_alignmask(parent);
  50. struct cmac_tfm_ctx *ctx = crypto_shash_ctx(parent);
  51. unsigned int bs = crypto_shash_blocksize(parent);
  52. __be64 *consts = PTR_ALIGN((void *)ctx->ctx,
  53. (alignmask | (__alignof__(__be64) - 1)) + 1);
  54. u64 _const[2];
  55. int i, err = 0;
  56. u8 msb_mask, gfmask;
  57. err = crypto_cipher_setkey(ctx->child, inkey, keylen);
  58. if (err)
  59. return err;
  60. /* encrypt the zero block */
  61. memset(consts, 0, bs);
  62. crypto_cipher_encrypt_one(ctx->child, (u8 *)consts, (u8 *)consts);
  63. switch (bs) {
  64. case 16:
  65. gfmask = 0x87;
  66. _const[0] = be64_to_cpu(consts[1]);
  67. _const[1] = be64_to_cpu(consts[0]);
  68. /* gf(2^128) multiply zero-ciphertext with u and u^2 */
  69. for (i = 0; i < 4; i += 2) {
  70. msb_mask = ((s64)_const[1] >> 63) & gfmask;
  71. _const[1] = (_const[1] << 1) | (_const[0] >> 63);
  72. _const[0] = (_const[0] << 1) ^ msb_mask;
  73. consts[i + 0] = cpu_to_be64(_const[1]);
  74. consts[i + 1] = cpu_to_be64(_const[0]);
  75. }
  76. break;
  77. case 8:
  78. gfmask = 0x1B;
  79. _const[0] = be64_to_cpu(consts[0]);
  80. /* gf(2^64) multiply zero-ciphertext with u and u^2 */
  81. for (i = 0; i < 2; i++) {
  82. msb_mask = ((s64)_const[0] >> 63) & gfmask;
  83. _const[0] = (_const[0] << 1) ^ msb_mask;
  84. consts[i] = cpu_to_be64(_const[0]);
  85. }
  86. break;
  87. }
  88. return 0;
  89. }
  90. static int crypto_cmac_digest_init(struct shash_desc *pdesc)
  91. {
  92. unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
  93. struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
  94. int bs = crypto_shash_blocksize(pdesc->tfm);
  95. u8 *prev = PTR_ALIGN((void *)ctx->ctx, alignmask + 1) + bs;
  96. ctx->len = 0;
  97. memset(prev, 0, bs);
  98. return 0;
  99. }
  100. static int crypto_cmac_digest_update(struct shash_desc *pdesc, const u8 *p,
  101. unsigned int len)
  102. {
  103. struct crypto_shash *parent = pdesc->tfm;
  104. unsigned long alignmask = crypto_shash_alignmask(parent);
  105. struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
  106. struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
  107. struct crypto_cipher *tfm = tctx->child;
  108. int bs = crypto_shash_blocksize(parent);
  109. u8 *odds = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
  110. u8 *prev = odds + bs;
  111. /* checking the data can fill the block */
  112. if ((ctx->len + len) <= bs) {
  113. memcpy(odds + ctx->len, p, len);
  114. ctx->len += len;
  115. return 0;
  116. }
  117. /* filling odds with new data and encrypting it */
  118. memcpy(odds + ctx->len, p, bs - ctx->len);
  119. len -= bs - ctx->len;
  120. p += bs - ctx->len;
  121. crypto_xor(prev, odds, bs);
  122. crypto_cipher_encrypt_one(tfm, prev, prev);
  123. /* clearing the length */
  124. ctx->len = 0;
  125. /* encrypting the rest of data */
  126. while (len > bs) {
  127. crypto_xor(prev, p, bs);
  128. crypto_cipher_encrypt_one(tfm, prev, prev);
  129. p += bs;
  130. len -= bs;
  131. }
  132. /* keeping the surplus of blocksize */
  133. if (len) {
  134. memcpy(odds, p, len);
  135. ctx->len = len;
  136. }
  137. return 0;
  138. }
  139. static int crypto_cmac_digest_final(struct shash_desc *pdesc, u8 *out)
  140. {
  141. struct crypto_shash *parent = pdesc->tfm;
  142. unsigned long alignmask = crypto_shash_alignmask(parent);
  143. struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
  144. struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
  145. struct crypto_cipher *tfm = tctx->child;
  146. int bs = crypto_shash_blocksize(parent);
  147. u8 *consts = PTR_ALIGN((void *)tctx->ctx,
  148. (alignmask | (__alignof__(__be64) - 1)) + 1);
  149. u8 *odds = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
  150. u8 *prev = odds + bs;
  151. unsigned int offset = 0;
  152. if (ctx->len != bs) {
  153. unsigned int rlen;
  154. u8 *p = odds + ctx->len;
  155. *p = 0x80;
  156. p++;
  157. rlen = bs - ctx->len - 1;
  158. if (rlen)
  159. memset(p, 0, rlen);
  160. offset += bs;
  161. }
  162. crypto_xor(prev, odds, bs);
  163. crypto_xor(prev, consts + offset, bs);
  164. crypto_cipher_encrypt_one(tfm, out, prev);
  165. return 0;
  166. }
  167. static int cmac_init_tfm(struct crypto_tfm *tfm)
  168. {
  169. struct crypto_cipher *cipher;
  170. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  171. struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
  172. struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
  173. cipher = crypto_spawn_cipher(spawn);
  174. if (IS_ERR(cipher))
  175. return PTR_ERR(cipher);
  176. ctx->child = cipher;
  177. return 0;
  178. };
  179. static void cmac_exit_tfm(struct crypto_tfm *tfm)
  180. {
  181. struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
  182. crypto_free_cipher(ctx->child);
  183. }
  184. static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb)
  185. {
  186. struct shash_instance *inst;
  187. struct crypto_cipher_spawn *spawn;
  188. struct crypto_alg *alg;
  189. unsigned long alignmask;
  190. u32 mask;
  191. int err;
  192. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
  193. if (err)
  194. return err;
  195. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  196. if (!inst)
  197. return -ENOMEM;
  198. spawn = shash_instance_ctx(inst);
  199. err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
  200. crypto_attr_alg_name(tb[1]), 0, mask);
  201. if (err)
  202. goto err_free_inst;
  203. alg = crypto_spawn_cipher_alg(spawn);
  204. switch (alg->cra_blocksize) {
  205. case 16:
  206. case 8:
  207. break;
  208. default:
  209. err = -EINVAL;
  210. goto err_free_inst;
  211. }
  212. err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
  213. if (err)
  214. goto err_free_inst;
  215. alignmask = alg->cra_alignmask;
  216. inst->alg.base.cra_alignmask = alignmask;
  217. inst->alg.base.cra_priority = alg->cra_priority;
  218. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  219. inst->alg.digestsize = alg->cra_blocksize;
  220. inst->alg.descsize =
  221. ALIGN(sizeof(struct cmac_desc_ctx), crypto_tfm_ctx_alignment())
  222. + (alignmask & ~(crypto_tfm_ctx_alignment() - 1))
  223. + alg->cra_blocksize * 2;
  224. inst->alg.base.cra_ctxsize =
  225. ALIGN(sizeof(struct cmac_tfm_ctx), crypto_tfm_ctx_alignment())
  226. + ((alignmask | (__alignof__(__be64) - 1)) &
  227. ~(crypto_tfm_ctx_alignment() - 1))
  228. + alg->cra_blocksize * 2;
  229. inst->alg.base.cra_init = cmac_init_tfm;
  230. inst->alg.base.cra_exit = cmac_exit_tfm;
  231. inst->alg.init = crypto_cmac_digest_init;
  232. inst->alg.update = crypto_cmac_digest_update;
  233. inst->alg.final = crypto_cmac_digest_final;
  234. inst->alg.setkey = crypto_cmac_digest_setkey;
  235. inst->free = shash_free_singlespawn_instance;
  236. err = shash_register_instance(tmpl, inst);
  237. if (err) {
  238. err_free_inst:
  239. shash_free_singlespawn_instance(inst);
  240. }
  241. return err;
  242. }
  243. static struct crypto_template crypto_cmac_tmpl = {
  244. .name = "cmac",
  245. .create = cmac_create,
  246. .module = THIS_MODULE,
  247. };
  248. static int __init crypto_cmac_module_init(void)
  249. {
  250. return crypto_register_template(&crypto_cmac_tmpl);
  251. }
  252. static void __exit crypto_cmac_module_exit(void)
  253. {
  254. crypto_unregister_template(&crypto_cmac_tmpl);
  255. }
  256. subsys_initcall(crypto_cmac_module_init);
  257. module_exit(crypto_cmac_module_exit);
  258. MODULE_LICENSE("GPL");
  259. MODULE_DESCRIPTION("CMAC keyed hash algorithm");
  260. MODULE_ALIAS_CRYPTO("cmac");
  261. MODULE_IMPORT_NS(CRYPTO_INTERNAL);