Blame view

crypto/hmac.c 6.48 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
  /*
   * Cryptographic API.
   *
   * HMAC: Keyed-Hashing for Message Authentication (RFC2104).
   *
   * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
8
   * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
10
11
   *
   * The HMAC implementation is derived from USAGI.
   * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
   */
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
13

03d7db565   Corentin LABBE   crypto: hmac - ad...
14
  #include <crypto/hmac.h>
5f7082ed4   Herbert Xu   crypto: hash - Ex...
15
  #include <crypto/internal/hash.h>
b2ab4a57b   Herbert Xu   [CRYPTO] scatterw...
16
  #include <crypto/scatterwalk.h>
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
17
18
19
20
  #include <linux/err.h>
  #include <linux/init.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
378f058cc   David Hardeman   [PATCH] Use sg_se...
21
  #include <linux/scatterlist.h>
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
22
23
24
  #include <linux/string.h>
  
  struct hmac_ctx {
0b767b4df   Herbert Xu   crypto: hmac - Pr...
25
  	struct crypto_shash *hash;
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
26
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27

0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
28
29
30
31
  static inline void *align_ptr(void *p, unsigned int align)
  {
  	return (void *)ALIGN((unsigned long)p, align);
  }
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
32
  static inline struct hmac_ctx *hmac_ctx(struct crypto_shash *tfm)
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
33
  {
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
34
  	return align_ptr(crypto_shash_ctx_aligned(tfm) +
0b767b4df   Herbert Xu   crypto: hmac - Pr...
35
  			 crypto_shash_statesize(tfm) * 2,
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
36
  			 crypto_tfm_ctx_alignment());
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
37
  }
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
38
  static int hmac_setkey(struct crypto_shash *parent,
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
39
40
  		       const u8 *inkey, unsigned int keylen)
  {
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
41
42
  	int bs = crypto_shash_blocksize(parent);
  	int ds = crypto_shash_digestsize(parent);
0b767b4df   Herbert Xu   crypto: hmac - Pr...
43
  	int ss = crypto_shash_statesize(parent);
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
44
  	char *ipad = crypto_shash_ctx_aligned(parent);
0b767b4df   Herbert Xu   crypto: hmac - Pr...
45
46
  	char *opad = ipad + ss;
  	struct hmac_ctx *ctx = align_ptr(opad + ss,
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
47
  					 crypto_tfm_ctx_alignment());
0b767b4df   Herbert Xu   crypto: hmac - Pr...
48
  	struct crypto_shash *hash = ctx->hash;
ffb32e973   Jan-Simon Möller   crypto: LLVMLinux...
49
  	SHASH_DESC_ON_STACK(shash, hash);
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
50
  	unsigned int i;
ffb32e973   Jan-Simon Möller   crypto: LLVMLinux...
51
  	shash->tfm = hash;
0b767b4df   Herbert Xu   crypto: hmac - Pr...
52

0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
53
  	if (keylen > bs) {
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
54
  		int err;
ffb32e973   Jan-Simon Möller   crypto: LLVMLinux...
55
  		err = crypto_shash_digest(shash, inkey, keylen, ipad);
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
56
57
  		if (err)
  			return err;
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
58
  		keylen = ds;
0b767b4df   Herbert Xu   crypto: hmac - Pr...
59
60
  	} else
  		memcpy(ipad, inkey, keylen);
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
61

0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
62
63
64
65
  	memset(ipad + keylen, 0, bs - keylen);
  	memcpy(opad, ipad, bs);
  
  	for (i = 0; i < bs; i++) {
03d7db565   Corentin LABBE   crypto: hmac - ad...
66
67
  		ipad[i] ^= HMAC_IPAD_VALUE;
  		opad[i] ^= HMAC_OPAD_VALUE;
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
68
  	}
ffb32e973   Jan-Simon Möller   crypto: LLVMLinux...
69
70
71
72
73
74
  	return crypto_shash_init(shash) ?:
  	       crypto_shash_update(shash, ipad, bs) ?:
  	       crypto_shash_export(shash, ipad) ?:
  	       crypto_shash_init(shash) ?:
  	       crypto_shash_update(shash, opad, bs) ?:
  	       crypto_shash_export(shash, opad);
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
75
  }
0b767b4df   Herbert Xu   crypto: hmac - Pr...
76
77
78
  static int hmac_export(struct shash_desc *pdesc, void *out)
  {
  	struct shash_desc *desc = shash_desc_ctx(pdesc);
0b767b4df   Herbert Xu   crypto: hmac - Pr...
79
80
81
82
  	return crypto_shash_export(desc, out);
  }
  
  static int hmac_import(struct shash_desc *pdesc, const void *in)
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
83
  {
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
84
  	struct shash_desc *desc = shash_desc_ctx(pdesc);
0b767b4df   Herbert Xu   crypto: hmac - Pr...
85
  	struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm);
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
86

0b767b4df   Herbert Xu   crypto: hmac - Pr...
87
  	desc->tfm = ctx->hash;
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
88

0b767b4df   Herbert Xu   crypto: hmac - Pr...
89
90
91
92
93
94
  	return crypto_shash_import(desc, in);
  }
  
  static int hmac_init(struct shash_desc *pdesc)
  {
  	return hmac_import(pdesc, crypto_shash_ctx_aligned(pdesc->tfm));
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
95
  }
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
96
97
  static int hmac_update(struct shash_desc *pdesc,
  		       const u8 *data, unsigned int nbytes)
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
98
  {
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
99
  	struct shash_desc *desc = shash_desc_ctx(pdesc);
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
100

8bd1209cf   Herbert Xu   crypto: hmac - Sw...
101
  	return crypto_shash_update(desc, data, nbytes);
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
102
  }
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
103
  static int hmac_final(struct shash_desc *pdesc, u8 *out)
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
104
  {
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
105
  	struct crypto_shash *parent = pdesc->tfm;
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
106
  	int ds = crypto_shash_digestsize(parent);
0b767b4df   Herbert Xu   crypto: hmac - Pr...
107
108
  	int ss = crypto_shash_statesize(parent);
  	char *opad = crypto_shash_ctx_aligned(parent) + ss;
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
109
  	struct shash_desc *desc = shash_desc_ctx(pdesc);
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
110

0b767b4df   Herbert Xu   crypto: hmac - Pr...
111
112
113
  	return crypto_shash_final(desc, out) ?:
  	       crypto_shash_import(desc, opad) ?:
  	       crypto_shash_finup(desc, out, ds, out);
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
114
  }
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
115
116
  static int hmac_finup(struct shash_desc *pdesc, const u8 *data,
  		      unsigned int nbytes, u8 *out)
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
117
  {
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
118

8bd1209cf   Herbert Xu   crypto: hmac - Sw...
119
  	struct crypto_shash *parent = pdesc->tfm;
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
120
  	int ds = crypto_shash_digestsize(parent);
0b767b4df   Herbert Xu   crypto: hmac - Pr...
121
122
  	int ss = crypto_shash_statesize(parent);
  	char *opad = crypto_shash_ctx_aligned(parent) + ss;
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
123
  	struct shash_desc *desc = shash_desc_ctx(pdesc);
78c2f0b8c   Jens Axboe   [SG] Update crypt...
124

0b767b4df   Herbert Xu   crypto: hmac - Pr...
125
126
127
  	return crypto_shash_finup(desc, data, nbytes, out) ?:
  	       crypto_shash_import(desc, opad) ?:
  	       crypto_shash_finup(desc, out, ds, out);
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
128
129
130
131
  }
  
  static int hmac_init_tfm(struct crypto_tfm *tfm)
  {
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
132
133
  	struct crypto_shash *parent = __crypto_shash_cast(tfm);
  	struct crypto_shash *hash;
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
134
  	struct crypto_instance *inst = (void *)tfm->__crt_alg;
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
135
136
  	struct crypto_shash_spawn *spawn = crypto_instance_ctx(inst);
  	struct hmac_ctx *ctx = hmac_ctx(parent);
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
137

8bd1209cf   Herbert Xu   crypto: hmac - Sw...
138
  	hash = crypto_spawn_shash(spawn);
2e306ee01   Herbert Xu   [CRYPTO] api: Add...
139
140
  	if (IS_ERR(hash))
  		return PTR_ERR(hash);
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
141

8bd1209cf   Herbert Xu   crypto: hmac - Sw...
142
143
  	parent->descsize = sizeof(struct shash_desc) +
  			   crypto_shash_descsize(hash);
7829a0c1c   Eric Biggers   crypto: hmac - fi...
144
145
  	if (WARN_ON(parent->descsize > HASH_MAX_DESCSIZE)) {
  		crypto_free_shash(hash);
e1354400b   Eric Biggers   crypto: hash - fi...
146
  		return -EINVAL;
7829a0c1c   Eric Biggers   crypto: hmac - fi...
147
  	}
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
148

0b767b4df   Herbert Xu   crypto: hmac - Pr...
149
  	ctx->hash = hash;
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
150
151
152
153
154
  	return 0;
  }
  
  static void hmac_exit_tfm(struct crypto_tfm *tfm)
  {
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
155
  	struct hmac_ctx *ctx = hmac_ctx(__crypto_shash_cast(tfm));
0b767b4df   Herbert Xu   crypto: hmac - Pr...
156
  	crypto_free_shash(ctx->hash);
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
157
  }
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
158
  static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
159
  {
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
160
  	struct shash_instance *inst;
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
161
  	struct crypto_alg *alg;
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
162
  	struct shash_alg *salg;
ebc610e5b   Herbert Xu   [CRYPTO] template...
163
  	int err;
ca786dc73   Herbert Xu   crypto: hash - Fi...
164
  	int ds;
0b767b4df   Herbert Xu   crypto: hmac - Pr...
165
  	int ss;
ebc610e5b   Herbert Xu   [CRYPTO] template...
166

8bd1209cf   Herbert Xu   crypto: hmac - Sw...
167
  	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
ebc610e5b   Herbert Xu   [CRYPTO] template...
168
  	if (err)
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
169
170
171
172
173
  		return err;
  
  	salg = shash_attr_alg(tb[1], 0, 0);
  	if (IS_ERR(salg))
  		return PTR_ERR(salg);
af3ff8045   Eric Biggers   crypto: hmac - re...
174
  	alg = &salg->base;
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
175

af3ff8045   Eric Biggers   crypto: hmac - re...
176
  	/* The underlying hash algorithm must be unkeyed */
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
177
  	err = -EINVAL;
af3ff8045   Eric Biggers   crypto: hmac - re...
178
179
  	if (crypto_shash_alg_has_setkey(salg))
  		goto out_put_alg;
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
180
  	ds = salg->digestsize;
0b767b4df   Herbert Xu   crypto: hmac - Pr...
181
  	ss = salg->statesize;
0b767b4df   Herbert Xu   crypto: hmac - Pr...
182
183
  	if (ds > alg->cra_blocksize ||
  	    ss < alg->cra_blocksize)
ca786dc73   Herbert Xu   crypto: hash - Fi...
184
  		goto out_put_alg;
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
185
  	inst = shash_alloc_instance("hmac", alg);
3b3fc322d   Herbert Xu   crypto: hmac - Fi...
186
  	err = PTR_ERR(inst);
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
187
188
  	if (IS_ERR(inst))
  		goto out_put_alg;
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
189
190
191
192
193
194
195
196
  	err = crypto_init_shash_spawn(shash_instance_ctx(inst), salg,
  				      shash_crypto_instance(inst));
  	if (err)
  		goto out_free_inst;
  
  	inst->alg.base.cra_priority = alg->cra_priority;
  	inst->alg.base.cra_blocksize = alg->cra_blocksize;
  	inst->alg.base.cra_alignmask = alg->cra_alignmask;
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
197

0b767b4df   Herbert Xu   crypto: hmac - Pr...
198
  	ss = ALIGN(ss, alg->cra_alignmask + 1);
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
199
  	inst->alg.digestsize = ds;
0b767b4df   Herbert Xu   crypto: hmac - Pr...
200
  	inst->alg.statesize = ss;
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
201

8bd1209cf   Herbert Xu   crypto: hmac - Sw...
202
  	inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) +
0b767b4df   Herbert Xu   crypto: hmac - Pr...
203
  				     ALIGN(ss * 2, crypto_tfm_ctx_alignment());
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
204

8bd1209cf   Herbert Xu   crypto: hmac - Sw...
205
206
  	inst->alg.base.cra_init = hmac_init_tfm;
  	inst->alg.base.cra_exit = hmac_exit_tfm;
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
207

8bd1209cf   Herbert Xu   crypto: hmac - Sw...
208
209
210
211
  	inst->alg.init = hmac_init;
  	inst->alg.update = hmac_update;
  	inst->alg.final = hmac_final;
  	inst->alg.finup = hmac_finup;
0b767b4df   Herbert Xu   crypto: hmac - Pr...
212
213
  	inst->alg.export = hmac_export;
  	inst->alg.import = hmac_import;
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
214
215
216
217
218
219
220
  	inst->alg.setkey = hmac_setkey;
  
  	err = shash_register_instance(tmpl, inst);
  	if (err) {
  out_free_inst:
  		shash_free_instance(shash_crypto_instance(inst));
  	}
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
221
222
223
  
  out_put_alg:
  	crypto_mod_put(alg);
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
224
  	return err;
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
225
226
227
228
  }
  
  static struct crypto_template hmac_tmpl = {
  	.name = "hmac",
8bd1209cf   Herbert Xu   crypto: hmac - Sw...
229
230
  	.create = hmac_create,
  	.free = shash_free_instance,
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
231
232
233
234
235
236
237
238
239
240
241
242
  	.module = THIS_MODULE,
  };
  
  static int __init hmac_module_init(void)
  {
  	return crypto_register_template(&hmac_tmpl);
  }
  
  static void __exit hmac_module_exit(void)
  {
  	crypto_unregister_template(&hmac_tmpl);
  }
c4741b230   Eric Biggers   crypto: run initc...
243
  subsys_initcall(hmac_module_init);
0796ae061   Herbert Xu   [CRYPTO] hmac: Ad...
244
245
246
247
  module_exit(hmac_module_exit);
  
  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("HMAC hash algorithm");
4943ba16b   Kees Cook   crypto: include c...
248
  MODULE_ALIAS_CRYPTO("hmac");