cmac.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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, alignmask + 1);
  57. u64 _const[2];
  58. int i, err = 0;
  59. u8 msb_mask, gfmask;
  60. err = crypto_cipher_setkey(ctx->child, inkey, keylen);
  61. if (err)
  62. return err;
  63. /* encrypt the zero block */
  64. memset(consts, 0, bs);
  65. crypto_cipher_encrypt_one(ctx->child, (u8 *)consts, (u8 *)consts);
  66. switch (bs) {
  67. case 16:
  68. gfmask = 0x87;
  69. _const[0] = be64_to_cpu(consts[1]);
  70. _const[1] = be64_to_cpu(consts[0]);
  71. /* gf(2^128) multiply zero-ciphertext with u and u^2 */
  72. for (i = 0; i < 4; i += 2) {
  73. msb_mask = ((s64)_const[1] >> 63) & gfmask;
  74. _const[1] = (_const[1] << 1) | (_const[0] >> 63);
  75. _const[0] = (_const[0] << 1) ^ msb_mask;
  76. consts[i + 0] = cpu_to_be64(_const[1]);
  77. consts[i + 1] = cpu_to_be64(_const[0]);
  78. }
  79. break;
  80. case 8:
  81. gfmask = 0x1B;
  82. _const[0] = be64_to_cpu(consts[0]);
  83. /* gf(2^64) multiply zero-ciphertext with u and u^2 */
  84. for (i = 0; i < 2; i++) {
  85. msb_mask = ((s64)_const[0] >> 63) & gfmask;
  86. _const[0] = (_const[0] << 1) ^ msb_mask;
  87. consts[i] = cpu_to_be64(_const[0]);
  88. }
  89. break;
  90. }
  91. return 0;
  92. }
  93. static int crypto_cmac_digest_init(struct shash_desc *pdesc)
  94. {
  95. unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
  96. struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
  97. int bs = crypto_shash_blocksize(pdesc->tfm);
  98. u8 *prev = PTR_ALIGN((void *)ctx->ctx, alignmask + 1) + bs;
  99. ctx->len = 0;
  100. memset(prev, 0, bs);
  101. return 0;
  102. }
  103. static int crypto_cmac_digest_update(struct shash_desc *pdesc, const u8 *p,
  104. unsigned int len)
  105. {
  106. struct crypto_shash *parent = pdesc->tfm;
  107. unsigned long alignmask = crypto_shash_alignmask(parent);
  108. struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
  109. struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
  110. struct crypto_cipher *tfm = tctx->child;
  111. int bs = crypto_shash_blocksize(parent);
  112. u8 *odds = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
  113. u8 *prev = odds + bs;
  114. /* checking the data can fill the block */
  115. if ((ctx->len + len) <= bs) {
  116. memcpy(odds + ctx->len, p, len);
  117. ctx->len += len;
  118. return 0;
  119. }
  120. /* filling odds with new data and encrypting it */
  121. memcpy(odds + ctx->len, p, bs - ctx->len);
  122. len -= bs - ctx->len;
  123. p += bs - ctx->len;
  124. crypto_xor(prev, odds, bs);
  125. crypto_cipher_encrypt_one(tfm, prev, prev);
  126. /* clearing the length */
  127. ctx->len = 0;
  128. /* encrypting the rest of data */
  129. while (len > bs) {
  130. crypto_xor(prev, p, bs);
  131. crypto_cipher_encrypt_one(tfm, prev, prev);
  132. p += bs;
  133. len -= bs;
  134. }
  135. /* keeping the surplus of blocksize */
  136. if (len) {
  137. memcpy(odds, p, len);
  138. ctx->len = len;
  139. }
  140. return 0;
  141. }
  142. static int crypto_cmac_digest_final(struct shash_desc *pdesc, u8 *out)
  143. {
  144. struct crypto_shash *parent = pdesc->tfm;
  145. unsigned long alignmask = crypto_shash_alignmask(parent);
  146. struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
  147. struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
  148. struct crypto_cipher *tfm = tctx->child;
  149. int bs = crypto_shash_blocksize(parent);
  150. u8 *consts = PTR_ALIGN((void *)tctx->ctx, alignmask + 1);
  151. u8 *odds = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
  152. u8 *prev = odds + bs;
  153. unsigned int offset = 0;
  154. if (ctx->len != bs) {
  155. unsigned int rlen;
  156. u8 *p = odds + ctx->len;
  157. *p = 0x80;
  158. p++;
  159. rlen = bs - ctx->len - 1;
  160. if (rlen)
  161. memset(p, 0, rlen);
  162. offset += bs;
  163. }
  164. crypto_xor(prev, odds, bs);
  165. crypto_xor(prev, consts + offset, bs);
  166. crypto_cipher_encrypt_one(tfm, out, prev);
  167. return 0;
  168. }
  169. static int cmac_init_tfm(struct crypto_tfm *tfm)
  170. {
  171. struct crypto_cipher *cipher;
  172. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  173. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  174. struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
  175. cipher = crypto_spawn_cipher(spawn);
  176. if (IS_ERR(cipher))
  177. return PTR_ERR(cipher);
  178. ctx->child = cipher;
  179. return 0;
  180. };
  181. static void cmac_exit_tfm(struct crypto_tfm *tfm)
  182. {
  183. struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
  184. crypto_free_cipher(ctx->child);
  185. }
  186. static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb)
  187. {
  188. struct shash_instance *inst;
  189. struct crypto_alg *alg;
  190. unsigned long alignmask;
  191. int err;
  192. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
  193. if (err)
  194. return err;
  195. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
  196. CRYPTO_ALG_TYPE_MASK);
  197. if (IS_ERR(alg))
  198. return PTR_ERR(alg);
  199. switch (alg->cra_blocksize) {
  200. case 16:
  201. case 8:
  202. break;
  203. default:
  204. goto out_put_alg;
  205. }
  206. inst = shash_alloc_instance("cmac", alg);
  207. err = PTR_ERR(inst);
  208. if (IS_ERR(inst))
  209. goto out_put_alg;
  210. err = crypto_init_spawn(shash_instance_ctx(inst), alg,
  211. shash_crypto_instance(inst),
  212. CRYPTO_ALG_TYPE_MASK);
  213. if (err)
  214. goto out_free_inst;
  215. alignmask = alg->cra_alignmask | (sizeof(long) - 1);
  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), alignmask + 1)
  226. + alg->cra_blocksize * 2;
  227. inst->alg.base.cra_init = cmac_init_tfm;
  228. inst->alg.base.cra_exit = cmac_exit_tfm;
  229. inst->alg.init = crypto_cmac_digest_init;
  230. inst->alg.update = crypto_cmac_digest_update;
  231. inst->alg.final = crypto_cmac_digest_final;
  232. inst->alg.setkey = crypto_cmac_digest_setkey;
  233. err = shash_register_instance(tmpl, inst);
  234. if (err) {
  235. out_free_inst:
  236. shash_free_instance(shash_crypto_instance(inst));
  237. }
  238. out_put_alg:
  239. crypto_mod_put(alg);
  240. return err;
  241. }
  242. static struct crypto_template crypto_cmac_tmpl = {
  243. .name = "cmac",
  244. .create = cmac_create,
  245. .free = shash_free_instance,
  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. module_init(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");