blob: 8b2a212eb0ad412e1d8a34d178c53d6eefb71342 [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
141static int hmac_init_tfm(struct crypto_tfm *tfm)
142{
Herbert Xu8bd12092009-07-09 12:43:37 +0800143 struct crypto_shash *parent = __crypto_shash_cast(tfm);
144 struct crypto_shash *hash;
Herbert Xu0796ae02006-08-21 20:50:52 +1000145 struct crypto_instance *inst = (void *)tfm->__crt_alg;
Herbert Xu8bd12092009-07-09 12:43:37 +0800146 struct crypto_shash_spawn *spawn = crypto_instance_ctx(inst);
147 struct hmac_ctx *ctx = hmac_ctx(parent);
Herbert Xu0796ae02006-08-21 20:50:52 +1000148
Herbert Xu8bd12092009-07-09 12:43:37 +0800149 hash = crypto_spawn_shash(spawn);
Herbert Xu2e306ee2006-12-17 10:05:58 +1100150 if (IS_ERR(hash))
151 return PTR_ERR(hash);
Herbert Xu0796ae02006-08-21 20:50:52 +1000152
Herbert Xu8bd12092009-07-09 12:43:37 +0800153 parent->descsize = sizeof(struct shash_desc) +
154 crypto_shash_descsize(hash);
Eric Biggers7829a0c2019-05-22 12:42:29 -0700155 if (WARN_ON(parent->descsize > HASH_MAX_DESCSIZE)) {
156 crypto_free_shash(hash);
Eric Biggerse1354402019-05-14 16:13:15 -0700157 return -EINVAL;
Eric Biggers7829a0c2019-05-22 12:42:29 -0700158 }
Herbert Xu8bd12092009-07-09 12:43:37 +0800159
Herbert Xu0b767b42009-07-24 15:18:41 +0800160 ctx->hash = hash;
Herbert Xu0796ae02006-08-21 20:50:52 +1000161 return 0;
162}
163
164static void hmac_exit_tfm(struct crypto_tfm *tfm)
165{
Herbert Xu8bd12092009-07-09 12:43:37 +0800166 struct hmac_ctx *ctx = hmac_ctx(__crypto_shash_cast(tfm));
Herbert Xu0b767b42009-07-24 15:18:41 +0800167 crypto_free_shash(ctx->hash);
Herbert Xu0796ae02006-08-21 20:50:52 +1000168}
169
Herbert Xu8bd12092009-07-09 12:43:37 +0800170static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu0796ae02006-08-21 20:50:52 +1000171{
Herbert Xu8bd12092009-07-09 12:43:37 +0800172 struct shash_instance *inst;
Herbert Xu0796ae02006-08-21 20:50:52 +1000173 struct crypto_alg *alg;
Herbert Xu8bd12092009-07-09 12:43:37 +0800174 struct shash_alg *salg;
Herbert Xuebc610e2007-01-01 18:37:02 +1100175 int err;
Herbert Xuca786dc2008-07-07 20:23:56 +0800176 int ds;
Herbert Xu0b767b42009-07-24 15:18:41 +0800177 int ss;
Herbert Xu0796ae02006-08-21 20:50:52 +1000178
Herbert Xu8bd12092009-07-09 12:43:37 +0800179 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
Herbert Xuebc610e2007-01-01 18:37:02 +1100180 if (err)
Herbert Xu8bd12092009-07-09 12:43:37 +0800181 return err;
Herbert Xuebc610e2007-01-01 18:37:02 +1100182
Herbert Xu8bd12092009-07-09 12:43:37 +0800183 salg = shash_attr_alg(tb[1], 0, 0);
184 if (IS_ERR(salg))
185 return PTR_ERR(salg);
Eric Biggersaf3ff802017-11-28 18:01:38 -0800186 alg = &salg->base;
Herbert Xu0796ae02006-08-21 20:50:52 +1000187
Eric Biggersaf3ff802017-11-28 18:01:38 -0800188 /* The underlying hash algorithm must be unkeyed */
Herbert Xu8bd12092009-07-09 12:43:37 +0800189 err = -EINVAL;
Eric Biggersaf3ff802017-11-28 18:01:38 -0800190 if (crypto_shash_alg_has_setkey(salg))
191 goto out_put_alg;
192
Herbert Xu8bd12092009-07-09 12:43:37 +0800193 ds = salg->digestsize;
Herbert Xu0b767b42009-07-24 15:18:41 +0800194 ss = salg->statesize;
Herbert Xu0b767b42009-07-24 15:18:41 +0800195 if (ds > alg->cra_blocksize ||
196 ss < alg->cra_blocksize)
Herbert Xuca786dc2008-07-07 20:23:56 +0800197 goto out_put_alg;
198
Herbert Xu8bd12092009-07-09 12:43:37 +0800199 inst = shash_alloc_instance("hmac", alg);
Herbert Xu3b3fc322009-07-15 16:52:55 +0800200 err = PTR_ERR(inst);
Herbert Xu0796ae02006-08-21 20:50:52 +1000201 if (IS_ERR(inst))
202 goto out_put_alg;
203
Herbert Xu8bd12092009-07-09 12:43:37 +0800204 err = crypto_init_shash_spawn(shash_instance_ctx(inst), salg,
205 shash_crypto_instance(inst));
206 if (err)
207 goto out_free_inst;
Herbert Xu0796ae02006-08-21 20:50:52 +1000208
Herbert Xu8bd12092009-07-09 12:43:37 +0800209 inst->alg.base.cra_priority = alg->cra_priority;
210 inst->alg.base.cra_blocksize = alg->cra_blocksize;
211 inst->alg.base.cra_alignmask = alg->cra_alignmask;
Herbert Xu0796ae02006-08-21 20:50:52 +1000212
Herbert Xu0b767b42009-07-24 15:18:41 +0800213 ss = ALIGN(ss, alg->cra_alignmask + 1);
Herbert Xu8bd12092009-07-09 12:43:37 +0800214 inst->alg.digestsize = ds;
Herbert Xu0b767b42009-07-24 15:18:41 +0800215 inst->alg.statesize = ss;
Herbert Xu0796ae02006-08-21 20:50:52 +1000216
Herbert Xu8bd12092009-07-09 12:43:37 +0800217 inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) +
Herbert Xu0b767b42009-07-24 15:18:41 +0800218 ALIGN(ss * 2, crypto_tfm_ctx_alignment());
Herbert Xu0796ae02006-08-21 20:50:52 +1000219
Herbert Xu8bd12092009-07-09 12:43:37 +0800220 inst->alg.base.cra_init = hmac_init_tfm;
221 inst->alg.base.cra_exit = hmac_exit_tfm;
222
223 inst->alg.init = hmac_init;
224 inst->alg.update = hmac_update;
225 inst->alg.final = hmac_final;
226 inst->alg.finup = hmac_finup;
Herbert Xu0b767b42009-07-24 15:18:41 +0800227 inst->alg.export = hmac_export;
228 inst->alg.import = hmac_import;
Herbert Xu8bd12092009-07-09 12:43:37 +0800229 inst->alg.setkey = hmac_setkey;
230
231 err = shash_register_instance(tmpl, inst);
232 if (err) {
233out_free_inst:
234 shash_free_instance(shash_crypto_instance(inst));
235 }
Herbert Xu0796ae02006-08-21 20:50:52 +1000236
237out_put_alg:
238 crypto_mod_put(alg);
Herbert Xu8bd12092009-07-09 12:43:37 +0800239 return err;
Herbert Xu0796ae02006-08-21 20:50:52 +1000240}
241
242static struct crypto_template hmac_tmpl = {
243 .name = "hmac",
Herbert Xu8bd12092009-07-09 12:43:37 +0800244 .create = hmac_create,
245 .free = shash_free_instance,
Herbert Xu0796ae02006-08-21 20:50:52 +1000246 .module = THIS_MODULE,
247};
248
249static int __init hmac_module_init(void)
250{
251 return crypto_register_template(&hmac_tmpl);
252}
253
254static void __exit hmac_module_exit(void)
255{
256 crypto_unregister_template(&hmac_tmpl);
257}
258
Eric Biggersc4741b22019-04-11 21:57:42 -0700259subsys_initcall(hmac_module_init);
Herbert Xu0796ae02006-08-21 20:50:52 +1000260module_exit(hmac_module_exit);
261
262MODULE_LICENSE("GPL");
263MODULE_DESCRIPTION("HMAC hash algorithm");
Kees Cook4943ba12014-11-24 16:32:38 -0800264MODULE_ALIAS_CRYPTO("hmac");