cmac.c 7.2 KB

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