cmac.c 7.7 KB

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