hmac.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Cryptographic API.
  4. *
  5. * HMAC: Keyed-Hashing for Message Authentication (RFC2104).
  6. *
  7. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  8. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  9. *
  10. * The HMAC implementation is derived from USAGI.
  11. * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI
  12. */
  13. #include <crypto/hmac.h>
  14. #include <crypto/internal/hash.h>
  15. #include <linux/err.h>
  16. #include <linux/fips.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/string.h>
  21. struct hmac_ctx {
  22. struct crypto_shash *hash;
  23. /* Contains 'u8 ipad[statesize];', then 'u8 opad[statesize];' */
  24. u8 pads[];
  25. };
  26. struct ahash_hmac_ctx {
  27. struct crypto_ahash *hash;
  28. /* Contains 'u8 ipad[statesize];', then 'u8 opad[statesize];' */
  29. u8 pads[];
  30. };
  31. static int hmac_setkey(struct crypto_shash *parent,
  32. const u8 *inkey, unsigned int keylen)
  33. {
  34. int bs = crypto_shash_blocksize(parent);
  35. int ds = crypto_shash_digestsize(parent);
  36. int ss = crypto_shash_statesize(parent);
  37. struct hmac_ctx *tctx = crypto_shash_ctx(parent);
  38. struct crypto_shash *hash = tctx->hash;
  39. u8 *ipad = &tctx->pads[0];
  40. u8 *opad = &tctx->pads[ss];
  41. SHASH_DESC_ON_STACK(shash, hash);
  42. int err, i;
  43. if (fips_enabled && (keylen < 112 / 8))
  44. return -EINVAL;
  45. shash->tfm = hash;
  46. if (keylen > bs) {
  47. int err;
  48. err = crypto_shash_digest(shash, inkey, keylen, ipad);
  49. if (err)
  50. return err;
  51. keylen = ds;
  52. } else
  53. memcpy(ipad, inkey, keylen);
  54. memset(ipad + keylen, 0, bs - keylen);
  55. memcpy(opad, ipad, bs);
  56. for (i = 0; i < bs; i++) {
  57. ipad[i] ^= HMAC_IPAD_VALUE;
  58. opad[i] ^= HMAC_OPAD_VALUE;
  59. }
  60. err = crypto_shash_init(shash) ?:
  61. crypto_shash_update(shash, ipad, bs) ?:
  62. crypto_shash_export(shash, ipad) ?:
  63. crypto_shash_init(shash) ?:
  64. crypto_shash_update(shash, opad, bs) ?:
  65. crypto_shash_export(shash, opad);
  66. shash_desc_zero(shash);
  67. return err;
  68. }
  69. static int hmac_export(struct shash_desc *pdesc, void *out)
  70. {
  71. struct shash_desc *desc = shash_desc_ctx(pdesc);
  72. return crypto_shash_export(desc, out);
  73. }
  74. static int hmac_import(struct shash_desc *pdesc, const void *in)
  75. {
  76. struct shash_desc *desc = shash_desc_ctx(pdesc);
  77. const struct hmac_ctx *tctx = crypto_shash_ctx(pdesc->tfm);
  78. desc->tfm = tctx->hash;
  79. return crypto_shash_import(desc, in);
  80. }
  81. static int hmac_export_core(struct shash_desc *pdesc, void *out)
  82. {
  83. struct shash_desc *desc = shash_desc_ctx(pdesc);
  84. return crypto_shash_export_core(desc, out);
  85. }
  86. static int hmac_import_core(struct shash_desc *pdesc, const void *in)
  87. {
  88. const struct hmac_ctx *tctx = crypto_shash_ctx(pdesc->tfm);
  89. struct shash_desc *desc = shash_desc_ctx(pdesc);
  90. desc->tfm = tctx->hash;
  91. return crypto_shash_import_core(desc, in);
  92. }
  93. static int hmac_init(struct shash_desc *pdesc)
  94. {
  95. const struct hmac_ctx *tctx = crypto_shash_ctx(pdesc->tfm);
  96. return hmac_import(pdesc, &tctx->pads[0]);
  97. }
  98. static int hmac_update(struct shash_desc *pdesc,
  99. const u8 *data, unsigned int nbytes)
  100. {
  101. struct shash_desc *desc = shash_desc_ctx(pdesc);
  102. return crypto_shash_update(desc, data, nbytes);
  103. }
  104. static int hmac_finup(struct shash_desc *pdesc, const u8 *data,
  105. unsigned int nbytes, u8 *out)
  106. {
  107. struct crypto_shash *parent = pdesc->tfm;
  108. int ds = crypto_shash_digestsize(parent);
  109. int ss = crypto_shash_statesize(parent);
  110. const struct hmac_ctx *tctx = crypto_shash_ctx(parent);
  111. const u8 *opad = &tctx->pads[ss];
  112. struct shash_desc *desc = shash_desc_ctx(pdesc);
  113. return crypto_shash_finup(desc, data, nbytes, out) ?:
  114. crypto_shash_import(desc, opad) ?:
  115. crypto_shash_finup(desc, out, ds, out);
  116. }
  117. static int hmac_init_tfm(struct crypto_shash *parent)
  118. {
  119. struct crypto_shash *hash;
  120. struct shash_instance *inst = shash_alg_instance(parent);
  121. struct crypto_shash_spawn *spawn = shash_instance_ctx(inst);
  122. struct hmac_ctx *tctx = crypto_shash_ctx(parent);
  123. hash = crypto_spawn_shash(spawn);
  124. if (IS_ERR(hash))
  125. return PTR_ERR(hash);
  126. tctx->hash = hash;
  127. return 0;
  128. }
  129. static int hmac_clone_tfm(struct crypto_shash *dst, struct crypto_shash *src)
  130. {
  131. struct hmac_ctx *sctx = crypto_shash_ctx(src);
  132. struct hmac_ctx *dctx = crypto_shash_ctx(dst);
  133. struct crypto_shash *hash;
  134. hash = crypto_clone_shash(sctx->hash);
  135. if (IS_ERR(hash))
  136. return PTR_ERR(hash);
  137. dctx->hash = hash;
  138. return 0;
  139. }
  140. static void hmac_exit_tfm(struct crypto_shash *parent)
  141. {
  142. struct hmac_ctx *tctx = crypto_shash_ctx(parent);
  143. crypto_free_shash(tctx->hash);
  144. }
  145. static int __hmac_create_shash(struct crypto_template *tmpl,
  146. struct rtattr **tb, u32 mask)
  147. {
  148. struct shash_instance *inst;
  149. struct crypto_shash_spawn *spawn;
  150. struct crypto_alg *alg;
  151. struct shash_alg *salg;
  152. int err;
  153. int ds;
  154. int ss;
  155. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  156. if (!inst)
  157. return -ENOMEM;
  158. spawn = shash_instance_ctx(inst);
  159. mask |= CRYPTO_AHASH_ALG_NO_EXPORT_CORE;
  160. err = crypto_grab_shash(spawn, shash_crypto_instance(inst),
  161. crypto_attr_alg_name(tb[1]), 0, mask);
  162. if (err)
  163. goto err_free_inst;
  164. salg = crypto_spawn_shash_alg(spawn);
  165. alg = &salg->base;
  166. /* The underlying hash algorithm must not require a key */
  167. err = -EINVAL;
  168. if (crypto_shash_alg_needs_key(salg))
  169. goto err_free_inst;
  170. ds = salg->digestsize;
  171. ss = salg->statesize;
  172. if (ds > alg->cra_blocksize ||
  173. ss < alg->cra_blocksize)
  174. goto err_free_inst;
  175. err = crypto_inst_setname(shash_crypto_instance(inst), "hmac",
  176. "hmac-shash", alg);
  177. if (err)
  178. goto err_free_inst;
  179. inst->alg.base.cra_priority = alg->cra_priority;
  180. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  181. inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) + (ss * 2);
  182. inst->alg.digestsize = ds;
  183. inst->alg.statesize = ss;
  184. inst->alg.descsize = sizeof(struct shash_desc) + salg->descsize;
  185. inst->alg.init = hmac_init;
  186. inst->alg.update = hmac_update;
  187. inst->alg.finup = hmac_finup;
  188. inst->alg.export = hmac_export;
  189. inst->alg.import = hmac_import;
  190. inst->alg.export_core = hmac_export_core;
  191. inst->alg.import_core = hmac_import_core;
  192. inst->alg.setkey = hmac_setkey;
  193. inst->alg.init_tfm = hmac_init_tfm;
  194. inst->alg.clone_tfm = hmac_clone_tfm;
  195. inst->alg.exit_tfm = hmac_exit_tfm;
  196. inst->free = shash_free_singlespawn_instance;
  197. err = shash_register_instance(tmpl, inst);
  198. if (err) {
  199. err_free_inst:
  200. shash_free_singlespawn_instance(inst);
  201. }
  202. return err;
  203. }
  204. static int hmac_setkey_ahash(struct crypto_ahash *parent,
  205. const u8 *inkey, unsigned int keylen)
  206. {
  207. struct ahash_hmac_ctx *tctx = crypto_ahash_ctx(parent);
  208. struct crypto_ahash *fb = crypto_ahash_fb(tctx->hash);
  209. int ds = crypto_ahash_digestsize(parent);
  210. int bs = crypto_ahash_blocksize(parent);
  211. int ss = crypto_ahash_statesize(parent);
  212. HASH_REQUEST_ON_STACK(req, fb);
  213. u8 *opad = &tctx->pads[ss];
  214. u8 *ipad = &tctx->pads[0];
  215. int err, i;
  216. if (fips_enabled && (keylen < 112 / 8))
  217. return -EINVAL;
  218. ahash_request_set_callback(req, 0, NULL, NULL);
  219. if (keylen > bs) {
  220. ahash_request_set_virt(req, inkey, ipad, keylen);
  221. err = crypto_ahash_digest(req);
  222. if (err)
  223. goto out_zero_req;
  224. keylen = ds;
  225. } else
  226. memcpy(ipad, inkey, keylen);
  227. memset(ipad + keylen, 0, bs - keylen);
  228. memcpy(opad, ipad, bs);
  229. for (i = 0; i < bs; i++) {
  230. ipad[i] ^= HMAC_IPAD_VALUE;
  231. opad[i] ^= HMAC_OPAD_VALUE;
  232. }
  233. ahash_request_set_virt(req, ipad, NULL, bs);
  234. err = crypto_ahash_init(req) ?:
  235. crypto_ahash_update(req) ?:
  236. crypto_ahash_export(req, ipad);
  237. ahash_request_set_virt(req, opad, NULL, bs);
  238. err = err ?:
  239. crypto_ahash_init(req) ?:
  240. crypto_ahash_update(req) ?:
  241. crypto_ahash_export(req, opad);
  242. out_zero_req:
  243. HASH_REQUEST_ZERO(req);
  244. return err;
  245. }
  246. static int hmac_export_ahash(struct ahash_request *preq, void *out)
  247. {
  248. return crypto_ahash_export(ahash_request_ctx(preq), out);
  249. }
  250. static int hmac_import_ahash(struct ahash_request *preq, const void *in)
  251. {
  252. struct crypto_ahash *tfm = crypto_ahash_reqtfm(preq);
  253. struct ahash_hmac_ctx *tctx = crypto_ahash_ctx(tfm);
  254. struct ahash_request *req = ahash_request_ctx(preq);
  255. ahash_request_set_tfm(req, tctx->hash);
  256. return crypto_ahash_import(req, in);
  257. }
  258. static int hmac_export_core_ahash(struct ahash_request *preq, void *out)
  259. {
  260. return crypto_ahash_export_core(ahash_request_ctx(preq), out);
  261. }
  262. static int hmac_import_core_ahash(struct ahash_request *preq, const void *in)
  263. {
  264. struct crypto_ahash *tfm = crypto_ahash_reqtfm(preq);
  265. struct ahash_hmac_ctx *tctx = crypto_ahash_ctx(tfm);
  266. struct ahash_request *req = ahash_request_ctx(preq);
  267. ahash_request_set_tfm(req, tctx->hash);
  268. return crypto_ahash_import_core(req, in);
  269. }
  270. static int hmac_init_ahash(struct ahash_request *preq)
  271. {
  272. struct crypto_ahash *tfm = crypto_ahash_reqtfm(preq);
  273. struct ahash_hmac_ctx *tctx = crypto_ahash_ctx(tfm);
  274. return hmac_import_ahash(preq, &tctx->pads[0]);
  275. }
  276. static int hmac_update_ahash(struct ahash_request *preq)
  277. {
  278. struct ahash_request *req = ahash_request_ctx(preq);
  279. ahash_request_set_callback(req, ahash_request_flags(preq),
  280. preq->base.complete, preq->base.data);
  281. if (ahash_request_isvirt(preq))
  282. ahash_request_set_virt(req, preq->svirt, NULL, preq->nbytes);
  283. else
  284. ahash_request_set_crypt(req, preq->src, NULL, preq->nbytes);
  285. return crypto_ahash_update(req);
  286. }
  287. static int hmac_finup_finish(struct ahash_request *preq, unsigned int mask)
  288. {
  289. struct crypto_ahash *tfm = crypto_ahash_reqtfm(preq);
  290. struct ahash_request *req = ahash_request_ctx(preq);
  291. struct ahash_hmac_ctx *tctx = crypto_ahash_ctx(tfm);
  292. int ds = crypto_ahash_digestsize(tfm);
  293. int ss = crypto_ahash_statesize(tfm);
  294. const u8 *opad = &tctx->pads[ss];
  295. ahash_request_set_callback(req, ahash_request_flags(preq) & ~mask,
  296. preq->base.complete, preq->base.data);
  297. ahash_request_set_virt(req, preq->result, preq->result, ds);
  298. return crypto_ahash_import(req, opad) ?:
  299. crypto_ahash_finup(req);
  300. }
  301. static void hmac_finup_done(void *data, int err)
  302. {
  303. struct ahash_request *preq = data;
  304. if (err)
  305. goto out;
  306. err = hmac_finup_finish(preq, CRYPTO_TFM_REQ_MAY_SLEEP);
  307. if (err == -EINPROGRESS || err == -EBUSY)
  308. return;
  309. out:
  310. ahash_request_complete(preq, err);
  311. }
  312. static int hmac_finup_ahash(struct ahash_request *preq)
  313. {
  314. struct ahash_request *req = ahash_request_ctx(preq);
  315. ahash_request_set_callback(req, ahash_request_flags(preq),
  316. hmac_finup_done, preq);
  317. if (ahash_request_isvirt(preq))
  318. ahash_request_set_virt(req, preq->svirt, preq->result,
  319. preq->nbytes);
  320. else
  321. ahash_request_set_crypt(req, preq->src, preq->result,
  322. preq->nbytes);
  323. return crypto_ahash_finup(req) ?:
  324. hmac_finup_finish(preq, 0);
  325. }
  326. static int hmac_digest_ahash(struct ahash_request *preq)
  327. {
  328. return hmac_init_ahash(preq) ?:
  329. hmac_finup_ahash(preq);
  330. }
  331. static int hmac_init_ahash_tfm(struct crypto_ahash *parent)
  332. {
  333. struct ahash_instance *inst = ahash_alg_instance(parent);
  334. struct ahash_hmac_ctx *tctx = crypto_ahash_ctx(parent);
  335. struct crypto_ahash *hash;
  336. hash = crypto_spawn_ahash(ahash_instance_ctx(inst));
  337. if (IS_ERR(hash))
  338. return PTR_ERR(hash);
  339. if (crypto_ahash_reqsize(parent) < sizeof(struct ahash_request) +
  340. crypto_ahash_reqsize(hash))
  341. return -EINVAL;
  342. tctx->hash = hash;
  343. return 0;
  344. }
  345. static int hmac_clone_ahash_tfm(struct crypto_ahash *dst,
  346. struct crypto_ahash *src)
  347. {
  348. struct ahash_hmac_ctx *sctx = crypto_ahash_ctx(src);
  349. struct ahash_hmac_ctx *dctx = crypto_ahash_ctx(dst);
  350. struct crypto_ahash *hash;
  351. hash = crypto_clone_ahash(sctx->hash);
  352. if (IS_ERR(hash))
  353. return PTR_ERR(hash);
  354. dctx->hash = hash;
  355. return 0;
  356. }
  357. static void hmac_exit_ahash_tfm(struct crypto_ahash *parent)
  358. {
  359. struct ahash_hmac_ctx *tctx = crypto_ahash_ctx(parent);
  360. crypto_free_ahash(tctx->hash);
  361. }
  362. static int hmac_create_ahash(struct crypto_template *tmpl, struct rtattr **tb,
  363. u32 mask)
  364. {
  365. struct crypto_ahash_spawn *spawn;
  366. struct ahash_instance *inst;
  367. struct crypto_alg *alg;
  368. struct hash_alg_common *halg;
  369. int ds, ss, err;
  370. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  371. if (!inst)
  372. return -ENOMEM;
  373. spawn = ahash_instance_ctx(inst);
  374. mask |= CRYPTO_AHASH_ALG_NO_EXPORT_CORE;
  375. err = crypto_grab_ahash(spawn, ahash_crypto_instance(inst),
  376. crypto_attr_alg_name(tb[1]), 0, mask);
  377. if (err)
  378. goto err_free_inst;
  379. halg = crypto_spawn_ahash_alg(spawn);
  380. alg = &halg->base;
  381. /* The underlying hash algorithm must not require a key */
  382. err = -EINVAL;
  383. if (crypto_hash_alg_needs_key(halg))
  384. goto err_free_inst;
  385. ds = halg->digestsize;
  386. ss = halg->statesize;
  387. if (ds > alg->cra_blocksize || ss < alg->cra_blocksize)
  388. goto err_free_inst;
  389. err = crypto_inst_setname(ahash_crypto_instance(inst), tmpl->name, alg);
  390. if (err)
  391. goto err_free_inst;
  392. inst->alg.halg.base.cra_flags = alg->cra_flags &
  393. CRYPTO_ALG_INHERITED_FLAGS;
  394. inst->alg.halg.base.cra_flags |= CRYPTO_ALG_REQ_VIRT;
  395. inst->alg.halg.base.cra_priority = alg->cra_priority + 100;
  396. inst->alg.halg.base.cra_blocksize = alg->cra_blocksize;
  397. inst->alg.halg.base.cra_ctxsize = sizeof(struct ahash_hmac_ctx) +
  398. (ss * 2);
  399. inst->alg.halg.base.cra_reqsize = sizeof(struct ahash_request) +
  400. alg->cra_reqsize;
  401. inst->alg.halg.digestsize = ds;
  402. inst->alg.halg.statesize = ss;
  403. inst->alg.init = hmac_init_ahash;
  404. inst->alg.update = hmac_update_ahash;
  405. inst->alg.finup = hmac_finup_ahash;
  406. inst->alg.digest = hmac_digest_ahash;
  407. inst->alg.export = hmac_export_ahash;
  408. inst->alg.import = hmac_import_ahash;
  409. inst->alg.export_core = hmac_export_core_ahash;
  410. inst->alg.import_core = hmac_import_core_ahash;
  411. inst->alg.setkey = hmac_setkey_ahash;
  412. inst->alg.init_tfm = hmac_init_ahash_tfm;
  413. inst->alg.clone_tfm = hmac_clone_ahash_tfm;
  414. inst->alg.exit_tfm = hmac_exit_ahash_tfm;
  415. inst->free = ahash_free_singlespawn_instance;
  416. err = ahash_register_instance(tmpl, inst);
  417. if (err) {
  418. err_free_inst:
  419. ahash_free_singlespawn_instance(inst);
  420. }
  421. return err;
  422. }
  423. static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
  424. {
  425. struct crypto_attr_type *algt;
  426. u32 mask;
  427. algt = crypto_get_attr_type(tb);
  428. if (IS_ERR(algt))
  429. return PTR_ERR(algt);
  430. mask = crypto_algt_inherited_mask(algt);
  431. if (!((algt->type ^ CRYPTO_ALG_TYPE_AHASH) &
  432. algt->mask & CRYPTO_ALG_TYPE_MASK))
  433. return hmac_create_ahash(tmpl, tb, mask);
  434. if ((algt->type ^ CRYPTO_ALG_TYPE_SHASH) &
  435. algt->mask & CRYPTO_ALG_TYPE_MASK)
  436. return -EINVAL;
  437. return __hmac_create_shash(tmpl, tb, mask);
  438. }
  439. static int hmac_create_shash(struct crypto_template *tmpl, struct rtattr **tb)
  440. {
  441. u32 mask;
  442. int err;
  443. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
  444. if (err)
  445. return err == -EINVAL ? -ENOENT : err;
  446. return __hmac_create_shash(tmpl, tb, mask);
  447. }
  448. static struct crypto_template hmac_tmpls[] = {
  449. {
  450. .name = "hmac",
  451. .create = hmac_create,
  452. .module = THIS_MODULE,
  453. },
  454. {
  455. .name = "hmac-shash",
  456. .create = hmac_create_shash,
  457. .module = THIS_MODULE,
  458. },
  459. };
  460. static int __init hmac_module_init(void)
  461. {
  462. return crypto_register_templates(hmac_tmpls, ARRAY_SIZE(hmac_tmpls));
  463. }
  464. static void __exit hmac_module_exit(void)
  465. {
  466. crypto_unregister_templates(hmac_tmpls, ARRAY_SIZE(hmac_tmpls));
  467. }
  468. module_init(hmac_module_init);
  469. module_exit(hmac_module_exit);
  470. MODULE_LICENSE("GPL");
  471. MODULE_DESCRIPTION("HMAC hash algorithm");
  472. MODULE_ALIAS_CRYPTO("hmac");