blob: e38bfb948278458bdd11d983d0424b03f4a362b5 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Cryptographic API.
4 *
5 * HMAC: Keyed-Hashing for Message Authentication (RFC2104).
6 *
7 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
Herbert Xu0796ae02006-08-21 20:50:52 +10008 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * The HMAC implementation is derived from USAGI.
11 * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
Herbert Xu0796ae02006-08-21 20:50:52 +100013
Corentin LABBE03d7db52017-05-19 08:53:23 +020014#include <crypto/hmac.h>
Herbert Xu5f7082e2008-08-31 22:21:09 +100015#include <crypto/internal/hash.h>
Herbert Xub2ab4a52007-12-05 20:59:25 +110016#include <crypto/scatterwalk.h>
Herbert Xu0796ae02006-08-21 20:50:52 +100017#include <linux/err.h>
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
David Hardeman378f0582005-09-17 17:55:31 +100021#include <linux/scatterlist.h>
Herbert Xu0796ae02006-08-21 20:50:52 +100022#include <linux/string.h>
23
24struct hmac_ctx {
Herbert Xu0b767b42009-07-24 15:18:41 +080025 struct crypto_shash *hash;
Herbert Xu0796ae02006-08-21 20:50:52 +100026};
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Herbert Xu0796ae02006-08-21 20:50:52 +100028static inline void *align_ptr(void *p, unsigned int align)
29{
30 return (void *)ALIGN((unsigned long)p, align);
31}
32
Herbert Xu8bd12092009-07-09 12:43:37 +080033static inline struct hmac_ctx *hmac_ctx(struct crypto_shash *tfm)
Herbert Xu0796ae02006-08-21 20:50:52 +100034{
Herbert Xu8bd12092009-07-09 12:43:37 +080035 return align_ptr(crypto_shash_ctx_aligned(tfm) +
Herbert Xu0b767b42009-07-24 15:18:41 +080036 crypto_shash_statesize(tfm) * 2,
Herbert Xu8bd12092009-07-09 12:43:37 +080037 crypto_tfm_ctx_alignment());
Herbert Xu0796ae02006-08-21 20:50:52 +100038}
39
Herbert Xu8bd12092009-07-09 12:43:37 +080040static int hmac_setkey(struct crypto_shash *parent,
Herbert Xu0796ae02006-08-21 20:50:52 +100041 const u8 *inkey, unsigned int keylen)
42{
Herbert Xu8bd12092009-07-09 12:43:37 +080043 int bs = crypto_shash_blocksize(parent);
44 int ds = crypto_shash_digestsize(parent);
Herbert Xu0b767b42009-07-24 15:18:41 +080045 int ss = crypto_shash_statesize(parent);
Herbert Xu8bd12092009-07-09 12:43:37 +080046 char *ipad = crypto_shash_ctx_aligned(parent);
Herbert Xu0b767b42009-07-24 15:18:41 +080047 char *opad = ipad + ss;
48 struct hmac_ctx *ctx = align_ptr(opad + ss,
Herbert Xu8bd12092009-07-09 12:43:37 +080049 crypto_tfm_ctx_alignment());
Herbert Xu0b767b42009-07-24 15:18:41 +080050 struct crypto_shash *hash = ctx->hash;
Jan-Simon Möllerffb32e92012-07-02 13:47:40 +020051 SHASH_DESC_ON_STACK(shash, hash);
Herbert Xu0796ae02006-08-21 20:50:52 +100052 unsigned int i;
53
Jan-Simon Möllerffb32e92012-07-02 13:47:40 +020054 shash->tfm = hash;
Herbert Xu0b767b42009-07-24 15:18:41 +080055
Herbert Xu0796ae02006-08-21 20:50:52 +100056 if (keylen > bs) {
Herbert Xu0796ae02006-08-21 20:50:52 +100057 int err;
58
Jan-Simon Möllerffb32e92012-07-02 13:47:40 +020059 err = crypto_shash_digest(shash, inkey, keylen, ipad);
Herbert Xu0796ae02006-08-21 20:50:52 +100060 if (err)
61 return err;
62
Herbert Xu0796ae02006-08-21 20:50:52 +100063 keylen = ds;
Herbert Xu0b767b42009-07-24 15:18:41 +080064 } else
65 memcpy(ipad, inkey, keylen);
Herbert Xu0796ae02006-08-21 20:50:52 +100066
Herbert Xu0796ae02006-08-21 20:50:52 +100067 memset(ipad + keylen, 0, bs - keylen);
68 memcpy(opad, ipad, bs);
69
70 for (i = 0; i < bs; i++) {
Corentin LABBE03d7db52017-05-19 08:53:23 +020071 ipad[i] ^= HMAC_IPAD_VALUE;
72 opad[i] ^= HMAC_OPAD_VALUE;
Herbert Xu0796ae02006-08-21 20:50:52 +100073 }
74
Jan-Simon Möllerffb32e92012-07-02 13:47:40 +020075 return crypto_shash_init(shash) ?:
76 crypto_shash_update(shash, ipad, bs) ?:
77 crypto_shash_export(shash, ipad) ?:
78 crypto_shash_init(shash) ?:
79 crypto_shash_update(shash, opad, bs) ?:
80 crypto_shash_export(shash, opad);
Herbert Xu0b767b42009-07-24 15:18:41 +080081}
82
83static int hmac_export(struct shash_desc *pdesc, void *out)
84{
85 struct shash_desc *desc = shash_desc_ctx(pdesc);
86
Herbert Xu0b767b42009-07-24 15:18:41 +080087 return crypto_shash_export(desc, out);
88}
89
90static int hmac_import(struct shash_desc *pdesc, const void *in)
91{
92 struct shash_desc *desc = shash_desc_ctx(pdesc);
93 struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm);
94
95 desc->tfm = ctx->hash;
Herbert Xu0b767b42009-07-24 15:18:41 +080096
97 return crypto_shash_import(desc, in);
Herbert Xu0796ae02006-08-21 20:50:52 +100098}
99
Herbert Xu8bd12092009-07-09 12:43:37 +0800100static int hmac_init(struct shash_desc *pdesc)
Herbert Xu0796ae02006-08-21 20:50:52 +1000101{
Herbert Xu0b767b42009-07-24 15:18:41 +0800102 return hmac_import(pdesc, crypto_shash_ctx_aligned(pdesc->tfm));
Herbert Xu0796ae02006-08-21 20:50:52 +1000103}
104
Herbert Xu8bd12092009-07-09 12:43:37 +0800105static int hmac_update(struct shash_desc *pdesc,
106 const u8 *data, unsigned int nbytes)
Herbert Xu0796ae02006-08-21 20:50:52 +1000107{
Herbert Xu8bd12092009-07-09 12:43:37 +0800108 struct shash_desc *desc = shash_desc_ctx(pdesc);
Herbert Xu0796ae02006-08-21 20:50:52 +1000109
Herbert Xu8bd12092009-07-09 12:43:37 +0800110 return crypto_shash_update(desc, data, nbytes);
Herbert Xu0796ae02006-08-21 20:50:52 +1000111}
112
Herbert Xu8bd12092009-07-09 12:43:37 +0800113static int hmac_final(struct shash_desc *pdesc, u8 *out)
Herbert Xu0796ae02006-08-21 20:50:52 +1000114{
Herbert Xu8bd12092009-07-09 12:43:37 +0800115 struct crypto_shash *parent = pdesc->tfm;
Herbert Xu8bd12092009-07-09 12:43:37 +0800116 int ds = crypto_shash_digestsize(parent);
Herbert Xu0b767b42009-07-24 15:18:41 +0800117 int ss = crypto_shash_statesize(parent);
118 char *opad = crypto_shash_ctx_aligned(parent) + ss;
Herbert Xu8bd12092009-07-09 12:43:37 +0800119 struct shash_desc *desc = shash_desc_ctx(pdesc);
Herbert Xu0796ae02006-08-21 20:50:52 +1000120
Herbert Xu0b767b42009-07-24 15:18:41 +0800121 return crypto_shash_final(desc, out) ?:
122 crypto_shash_import(desc, opad) ?:
123 crypto_shash_finup(desc, out, ds, out);
Herbert Xu0796ae02006-08-21 20:50:52 +1000124}
125
Herbert Xu8bd12092009-07-09 12:43:37 +0800126static int hmac_finup(struct shash_desc *pdesc, const u8 *data,
127 unsigned int nbytes, u8 *out)
Herbert Xu0796ae02006-08-21 20:50:52 +1000128{
Herbert Xu8bd12092009-07-09 12:43:37 +0800129
130 struct crypto_shash *parent = pdesc->tfm;
Herbert Xu8bd12092009-07-09 12:43:37 +0800131 int ds = crypto_shash_digestsize(parent);
Herbert Xu0b767b42009-07-24 15:18:41 +0800132 int ss = crypto_shash_statesize(parent);
133 char *opad = crypto_shash_ctx_aligned(parent) + ss;
Herbert Xu8bd12092009-07-09 12:43:37 +0800134 struct shash_desc *desc = shash_desc_ctx(pdesc);
Herbert Xu0796ae02006-08-21 20:50:52 +1000135
Herbert Xu0b767b42009-07-24 15:18:41 +0800136 return crypto_shash_finup(desc, data, nbytes, out) ?:
137 crypto_shash_import(desc, opad) ?:
138 crypto_shash_finup(desc, out, ds, out);
Herbert Xu0796ae02006-08-21 20:50:52 +1000139}
140
Herbert Xud9e16702019-12-08 13:42:53 +0800141static int hmac_init_tfm(struct crypto_shash *parent)
Herbert Xu0796ae02006-08-21 20:50:52 +1000142{
Herbert Xu8bd12092009-07-09 12:43:37 +0800143 struct crypto_shash *hash;
Herbert Xud9e16702019-12-08 13:42:53 +0800144 struct shash_instance *inst = shash_alg_instance(parent);
145 struct crypto_shash_spawn *spawn = shash_instance_ctx(inst);
Herbert Xu8bd12092009-07-09 12:43:37 +0800146 struct hmac_ctx *ctx = hmac_ctx(parent);
Herbert Xu0796ae02006-08-21 20:50:52 +1000147
Herbert Xu8bd12092009-07-09 12:43:37 +0800148 hash = crypto_spawn_shash(spawn);
Herbert Xu2e306ee2006-12-17 10:05:58 +1100149 if (IS_ERR(hash))
150 return PTR_ERR(hash);
Herbert Xu0796ae02006-08-21 20:50:52 +1000151
Herbert Xu8bd12092009-07-09 12:43:37 +0800152 parent->descsize = sizeof(struct shash_desc) +
153 crypto_shash_descsize(hash);
154
Herbert Xu0b767b42009-07-24 15:18:41 +0800155 ctx->hash = hash;
Herbert Xu0796ae02006-08-21 20:50:52 +1000156 return 0;
157}
158
Herbert Xud9e16702019-12-08 13:42:53 +0800159static void hmac_exit_tfm(struct crypto_shash *parent)
Herbert Xu0796ae02006-08-21 20:50:52 +1000160{
Herbert Xud9e16702019-12-08 13:42:53 +0800161 struct hmac_ctx *ctx = hmac_ctx(parent);
Herbert Xu0b767b42009-07-24 15:18:41 +0800162 crypto_free_shash(ctx->hash);
Herbert Xu0796ae02006-08-21 20:50:52 +1000163}
164
Herbert Xu8bd12092009-07-09 12:43:37 +0800165static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu0796ae02006-08-21 20:50:52 +1000166{
Herbert Xu8bd12092009-07-09 12:43:37 +0800167 struct shash_instance *inst;
Eric Biggers39e7a282020-01-02 19:58:54 -0800168 struct crypto_shash_spawn *spawn;
Herbert Xu0796ae02006-08-21 20:50:52 +1000169 struct crypto_alg *alg;
Herbert Xu8bd12092009-07-09 12:43:37 +0800170 struct shash_alg *salg;
Herbert Xuebc610e2007-01-01 18:37:02 +1100171 int err;
Herbert Xuca786dc2008-07-07 20:23:56 +0800172 int ds;
Herbert Xu0b767b42009-07-24 15:18:41 +0800173 int ss;
Herbert Xu0796ae02006-08-21 20:50:52 +1000174
Herbert Xu8bd12092009-07-09 12:43:37 +0800175 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
Herbert Xuebc610e2007-01-01 18:37:02 +1100176 if (err)
Herbert Xu8bd12092009-07-09 12:43:37 +0800177 return err;
Herbert Xuebc610e2007-01-01 18:37:02 +1100178
Eric Biggers39e7a282020-01-02 19:58:54 -0800179 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
180 if (!inst)
181 return -ENOMEM;
182 spawn = shash_instance_ctx(inst);
183
184 err = crypto_grab_shash(spawn, shash_crypto_instance(inst),
185 crypto_attr_alg_name(tb[1]), 0, 0);
186 if (err)
187 goto err_free_inst;
188 salg = crypto_spawn_shash_alg(spawn);
Eric Biggersaf3ff802017-11-28 18:01:38 -0800189 alg = &salg->base;
Herbert Xu0796ae02006-08-21 20:50:52 +1000190
Eric Biggersc2881782019-11-29 11:35:22 -0800191 /* The underlying hash algorithm must not require a key */
Herbert Xu8bd12092009-07-09 12:43:37 +0800192 err = -EINVAL;
Eric Biggersc2881782019-11-29 11:35:22 -0800193 if (crypto_shash_alg_needs_key(salg))
Eric Biggers39e7a282020-01-02 19:58:54 -0800194 goto err_free_inst;
Eric Biggersaf3ff802017-11-28 18:01:38 -0800195
Herbert Xu8bd12092009-07-09 12:43:37 +0800196 ds = salg->digestsize;
Herbert Xu0b767b42009-07-24 15:18:41 +0800197 ss = salg->statesize;
Herbert Xu0b767b42009-07-24 15:18:41 +0800198 if (ds > alg->cra_blocksize ||
199 ss < alg->cra_blocksize)
Eric Biggers39e7a282020-01-02 19:58:54 -0800200 goto err_free_inst;
Herbert Xuca786dc2008-07-07 20:23:56 +0800201
Eric Biggers39e7a282020-01-02 19:58:54 -0800202 err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
Herbert Xu8bd12092009-07-09 12:43:37 +0800203 if (err)
Eric Biggers39e7a282020-01-02 19:58:54 -0800204 goto err_free_inst;
Herbert Xu0796ae02006-08-21 20:50:52 +1000205
Herbert Xu8bd12092009-07-09 12:43:37 +0800206 inst->alg.base.cra_priority = alg->cra_priority;
207 inst->alg.base.cra_blocksize = alg->cra_blocksize;
208 inst->alg.base.cra_alignmask = alg->cra_alignmask;
Herbert Xu0796ae02006-08-21 20:50:52 +1000209
Herbert Xu0b767b42009-07-24 15:18:41 +0800210 ss = ALIGN(ss, alg->cra_alignmask + 1);
Herbert Xu8bd12092009-07-09 12:43:37 +0800211 inst->alg.digestsize = ds;
Herbert Xu0b767b42009-07-24 15:18:41 +0800212 inst->alg.statesize = ss;
Herbert Xu0796ae02006-08-21 20:50:52 +1000213
Herbert Xu8bd12092009-07-09 12:43:37 +0800214 inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) +
Herbert Xu0b767b42009-07-24 15:18:41 +0800215 ALIGN(ss * 2, crypto_tfm_ctx_alignment());
Herbert Xu0796ae02006-08-21 20:50:52 +1000216
Herbert Xu8bd12092009-07-09 12:43:37 +0800217 inst->alg.init = hmac_init;
218 inst->alg.update = hmac_update;
219 inst->alg.final = hmac_final;
220 inst->alg.finup = hmac_finup;
Herbert Xu0b767b42009-07-24 15:18:41 +0800221 inst->alg.export = hmac_export;
222 inst->alg.import = hmac_import;
Herbert Xu8bd12092009-07-09 12:43:37 +0800223 inst->alg.setkey = hmac_setkey;
Herbert Xud9e16702019-12-08 13:42:53 +0800224 inst->alg.init_tfm = hmac_init_tfm;
225 inst->alg.exit_tfm = hmac_exit_tfm;
Herbert Xu8bd12092009-07-09 12:43:37 +0800226
Eric Biggersa39c66c2020-01-02 20:04:38 -0800227 inst->free = shash_free_singlespawn_instance;
228
Herbert Xu8bd12092009-07-09 12:43:37 +0800229 err = shash_register_instance(tmpl, inst);
230 if (err) {
Eric Biggers39e7a282020-01-02 19:58:54 -0800231err_free_inst:
Eric Biggersa39c66c2020-01-02 20:04:38 -0800232 shash_free_singlespawn_instance(inst);
Herbert Xu8bd12092009-07-09 12:43:37 +0800233 }
Herbert Xu8bd12092009-07-09 12:43:37 +0800234 return err;
Herbert Xu0796ae02006-08-21 20:50:52 +1000235}
236
237static struct crypto_template hmac_tmpl = {
238 .name = "hmac",
Herbert Xu8bd12092009-07-09 12:43:37 +0800239 .create = hmac_create,
Herbert Xu0796ae02006-08-21 20:50:52 +1000240 .module = THIS_MODULE,
241};
242
243static int __init hmac_module_init(void)
244{
245 return crypto_register_template(&hmac_tmpl);
246}
247
248static void __exit hmac_module_exit(void)
249{
250 crypto_unregister_template(&hmac_tmpl);
251}
252
Eric Biggersc4741b22019-04-11 21:57:42 -0700253subsys_initcall(hmac_module_init);
Herbert Xu0796ae02006-08-21 20:50:52 +1000254module_exit(hmac_module_exit);
255
256MODULE_LICENSE("GPL");
257MODULE_DESCRIPTION("HMAC hash algorithm");
Kees Cook4943ba12014-11-24 16:32:38 -0800258MODULE_ALIAS_CRYPTO("hmac");