Blame view
crypto/hmac.c
6.94 KB
1da177e4c Linux-2.6.12-rc2 |
1 2 3 4 5 6 |
/* * Cryptographic API. * * HMAC: Keyed-Hashing for Message Authentication (RFC2104). * * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
0796ae061 [CRYPTO] hmac: Ad... |
7 |
* Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
1da177e4c Linux-2.6.12-rc2 |
8 9 10 11 12 13 |
* * The HMAC implementation is derived from USAGI. * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free |
0796ae061 [CRYPTO] hmac: Ad... |
14 |
* Software Foundation; either version 2 of the License, or (at your option) |
1da177e4c Linux-2.6.12-rc2 |
15 16 17 |
* any later version. * */ |
0796ae061 [CRYPTO] hmac: Ad... |
18 |
|
03d7db565 crypto: hmac - ad... |
19 |
#include <crypto/hmac.h> |
5f7082ed4 crypto: hash - Ex... |
20 |
#include <crypto/internal/hash.h> |
b2ab4a57b [CRYPTO] scatterw... |
21 |
#include <crypto/scatterwalk.h> |
0796ae061 [CRYPTO] hmac: Ad... |
22 23 24 25 |
#include <linux/err.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/module.h> |
378f058cc [PATCH] Use sg_se... |
26 |
#include <linux/scatterlist.h> |
0796ae061 [CRYPTO] hmac: Ad... |
27 28 29 |
#include <linux/string.h> struct hmac_ctx { |
0b767b4df crypto: hmac - Pr... |
30 |
struct crypto_shash *hash; |
0796ae061 [CRYPTO] hmac: Ad... |
31 |
}; |
1da177e4c Linux-2.6.12-rc2 |
32 |
|
0796ae061 [CRYPTO] hmac: Ad... |
33 34 35 36 |
static inline void *align_ptr(void *p, unsigned int align) { return (void *)ALIGN((unsigned long)p, align); } |
8bd1209cf crypto: hmac - Sw... |
37 |
static inline struct hmac_ctx *hmac_ctx(struct crypto_shash *tfm) |
0796ae061 [CRYPTO] hmac: Ad... |
38 |
{ |
8bd1209cf crypto: hmac - Sw... |
39 |
return align_ptr(crypto_shash_ctx_aligned(tfm) + |
0b767b4df crypto: hmac - Pr... |
40 |
crypto_shash_statesize(tfm) * 2, |
8bd1209cf crypto: hmac - Sw... |
41 |
crypto_tfm_ctx_alignment()); |
0796ae061 [CRYPTO] hmac: Ad... |
42 |
} |
8bd1209cf crypto: hmac - Sw... |
43 |
static int hmac_setkey(struct crypto_shash *parent, |
0796ae061 [CRYPTO] hmac: Ad... |
44 45 |
const u8 *inkey, unsigned int keylen) { |
8bd1209cf crypto: hmac - Sw... |
46 47 |
int bs = crypto_shash_blocksize(parent); int ds = crypto_shash_digestsize(parent); |
0b767b4df crypto: hmac - Pr... |
48 |
int ss = crypto_shash_statesize(parent); |
8bd1209cf crypto: hmac - Sw... |
49 |
char *ipad = crypto_shash_ctx_aligned(parent); |
0b767b4df crypto: hmac - Pr... |
50 51 |
char *opad = ipad + ss; struct hmac_ctx *ctx = align_ptr(opad + ss, |
8bd1209cf crypto: hmac - Sw... |
52 |
crypto_tfm_ctx_alignment()); |
0b767b4df crypto: hmac - Pr... |
53 |
struct crypto_shash *hash = ctx->hash; |
ffb32e973 crypto: LLVMLinux... |
54 |
SHASH_DESC_ON_STACK(shash, hash); |
0796ae061 [CRYPTO] hmac: Ad... |
55 |
unsigned int i; |
ffb32e973 crypto: LLVMLinux... |
56 57 58 |
shash->tfm = hash; shash->flags = crypto_shash_get_flags(parent) & CRYPTO_TFM_REQ_MAY_SLEEP; |
0b767b4df crypto: hmac - Pr... |
59 |
|
0796ae061 [CRYPTO] hmac: Ad... |
60 |
if (keylen > bs) { |
0796ae061 [CRYPTO] hmac: Ad... |
61 |
int err; |
ffb32e973 crypto: LLVMLinux... |
62 |
err = crypto_shash_digest(shash, inkey, keylen, ipad); |
0796ae061 [CRYPTO] hmac: Ad... |
63 64 |
if (err) return err; |
0796ae061 [CRYPTO] hmac: Ad... |
65 |
keylen = ds; |
0b767b4df crypto: hmac - Pr... |
66 67 |
} else memcpy(ipad, inkey, keylen); |
0796ae061 [CRYPTO] hmac: Ad... |
68 |
|
0796ae061 [CRYPTO] hmac: Ad... |
69 70 71 72 |
memset(ipad + keylen, 0, bs - keylen); memcpy(opad, ipad, bs); for (i = 0; i < bs; i++) { |
03d7db565 crypto: hmac - ad... |
73 74 |
ipad[i] ^= HMAC_IPAD_VALUE; opad[i] ^= HMAC_OPAD_VALUE; |
0796ae061 [CRYPTO] hmac: Ad... |
75 |
} |
ffb32e973 crypto: LLVMLinux... |
76 77 78 79 80 81 |
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 [CRYPTO] hmac: Ad... |
82 |
} |
0b767b4df crypto: hmac - Pr... |
83 84 85 86 87 88 89 90 91 92 |
static int hmac_export(struct shash_desc *pdesc, void *out) { struct shash_desc *desc = shash_desc_ctx(pdesc); desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP; return crypto_shash_export(desc, out); } static int hmac_import(struct shash_desc *pdesc, const void *in) |
0796ae061 [CRYPTO] hmac: Ad... |
93 |
{ |
8bd1209cf crypto: hmac - Sw... |
94 |
struct shash_desc *desc = shash_desc_ctx(pdesc); |
0b767b4df crypto: hmac - Pr... |
95 |
struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm); |
8bd1209cf crypto: hmac - Sw... |
96 |
|
0b767b4df crypto: hmac - Pr... |
97 |
desc->tfm = ctx->hash; |
8bd1209cf crypto: hmac - Sw... |
98 |
desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP; |
0b767b4df crypto: hmac - Pr... |
99 100 101 102 103 104 |
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 [CRYPTO] hmac: Ad... |
105 |
} |
8bd1209cf crypto: hmac - Sw... |
106 107 |
static int hmac_update(struct shash_desc *pdesc, const u8 *data, unsigned int nbytes) |
0796ae061 [CRYPTO] hmac: Ad... |
108 |
{ |
8bd1209cf crypto: hmac - Sw... |
109 |
struct shash_desc *desc = shash_desc_ctx(pdesc); |
0796ae061 [CRYPTO] hmac: Ad... |
110 |
|
8bd1209cf crypto: hmac - Sw... |
111 |
desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP; |
0796ae061 [CRYPTO] hmac: Ad... |
112 |
|
8bd1209cf crypto: hmac - Sw... |
113 |
return crypto_shash_update(desc, data, nbytes); |
0796ae061 [CRYPTO] hmac: Ad... |
114 |
} |
8bd1209cf crypto: hmac - Sw... |
115 |
static int hmac_final(struct shash_desc *pdesc, u8 *out) |
0796ae061 [CRYPTO] hmac: Ad... |
116 |
{ |
8bd1209cf crypto: hmac - Sw... |
117 |
struct crypto_shash *parent = pdesc->tfm; |
8bd1209cf crypto: hmac - Sw... |
118 |
int ds = crypto_shash_digestsize(parent); |
0b767b4df crypto: hmac - Pr... |
119 120 |
int ss = crypto_shash_statesize(parent); char *opad = crypto_shash_ctx_aligned(parent) + ss; |
8bd1209cf crypto: hmac - Sw... |
121 |
struct shash_desc *desc = shash_desc_ctx(pdesc); |
0796ae061 [CRYPTO] hmac: Ad... |
122 |
|
8bd1209cf crypto: hmac - Sw... |
123 |
desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP; |
73af07de3 [CRYPTO] hmac: Fi... |
124 |
|
0b767b4df crypto: hmac - Pr... |
125 126 127 |
return crypto_shash_final(desc, out) ?: crypto_shash_import(desc, opad) ?: crypto_shash_finup(desc, out, ds, out); |
0796ae061 [CRYPTO] hmac: Ad... |
128 |
} |
8bd1209cf crypto: hmac - Sw... |
129 130 |
static int hmac_finup(struct shash_desc *pdesc, const u8 *data, unsigned int nbytes, u8 *out) |
0796ae061 [CRYPTO] hmac: Ad... |
131 |
{ |
0796ae061 [CRYPTO] hmac: Ad... |
132 |
|
8bd1209cf crypto: hmac - Sw... |
133 |
struct crypto_shash *parent = pdesc->tfm; |
8bd1209cf crypto: hmac - Sw... |
134 |
int ds = crypto_shash_digestsize(parent); |
0b767b4df crypto: hmac - Pr... |
135 136 |
int ss = crypto_shash_statesize(parent); char *opad = crypto_shash_ctx_aligned(parent) + ss; |
8bd1209cf crypto: hmac - Sw... |
137 |
struct shash_desc *desc = shash_desc_ctx(pdesc); |
78c2f0b8c [SG] Update crypt... |
138 |
|
8bd1209cf crypto: hmac - Sw... |
139 |
desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP; |
0796ae061 [CRYPTO] hmac: Ad... |
140 |
|
0b767b4df crypto: hmac - Pr... |
141 142 143 |
return crypto_shash_finup(desc, data, nbytes, out) ?: crypto_shash_import(desc, opad) ?: crypto_shash_finup(desc, out, ds, out); |
0796ae061 [CRYPTO] hmac: Ad... |
144 145 146 147 |
} static int hmac_init_tfm(struct crypto_tfm *tfm) { |
8bd1209cf crypto: hmac - Sw... |
148 149 |
struct crypto_shash *parent = __crypto_shash_cast(tfm); struct crypto_shash *hash; |
0796ae061 [CRYPTO] hmac: Ad... |
150 |
struct crypto_instance *inst = (void *)tfm->__crt_alg; |
8bd1209cf crypto: hmac - Sw... |
151 152 |
struct crypto_shash_spawn *spawn = crypto_instance_ctx(inst); struct hmac_ctx *ctx = hmac_ctx(parent); |
0796ae061 [CRYPTO] hmac: Ad... |
153 |
|
8bd1209cf crypto: hmac - Sw... |
154 |
hash = crypto_spawn_shash(spawn); |
2e306ee01 [CRYPTO] api: Add... |
155 156 |
if (IS_ERR(hash)) return PTR_ERR(hash); |
0796ae061 [CRYPTO] hmac: Ad... |
157 |
|
8bd1209cf crypto: hmac - Sw... |
158 159 |
parent->descsize = sizeof(struct shash_desc) + crypto_shash_descsize(hash); |
0b767b4df crypto: hmac - Pr... |
160 |
ctx->hash = hash; |
0796ae061 [CRYPTO] hmac: Ad... |
161 162 163 164 165 |
return 0; } static void hmac_exit_tfm(struct crypto_tfm *tfm) { |
8bd1209cf crypto: hmac - Sw... |
166 |
struct hmac_ctx *ctx = hmac_ctx(__crypto_shash_cast(tfm)); |
0b767b4df crypto: hmac - Pr... |
167 |
crypto_free_shash(ctx->hash); |
0796ae061 [CRYPTO] hmac: Ad... |
168 |
} |
8bd1209cf crypto: hmac - Sw... |
169 |
static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb) |
0796ae061 [CRYPTO] hmac: Ad... |
170 |
{ |
8bd1209cf crypto: hmac - Sw... |
171 |
struct shash_instance *inst; |
0796ae061 [CRYPTO] hmac: Ad... |
172 |
struct crypto_alg *alg; |
8bd1209cf crypto: hmac - Sw... |
173 |
struct shash_alg *salg; |
ebc610e5b [CRYPTO] template... |
174 |
int err; |
ca786dc73 crypto: hash - Fi... |
175 |
int ds; |
0b767b4df crypto: hmac - Pr... |
176 |
int ss; |
ebc610e5b [CRYPTO] template... |
177 |
|
8bd1209cf crypto: hmac - Sw... |
178 |
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH); |
ebc610e5b [CRYPTO] template... |
179 |
if (err) |
8bd1209cf crypto: hmac - Sw... |
180 181 182 183 184 |
return err; salg = shash_attr_alg(tb[1], 0, 0); if (IS_ERR(salg)) return PTR_ERR(salg); |
902ae89f8 crypto: hmac - re... |
185 |
alg = &salg->base; |
8bd1209cf crypto: hmac - Sw... |
186 |
|
902ae89f8 crypto: hmac - re... |
187 |
/* The underlying hash algorithm must be unkeyed */ |
8bd1209cf crypto: hmac - Sw... |
188 |
err = -EINVAL; |
902ae89f8 crypto: hmac - re... |
189 190 |
if (crypto_shash_alg_has_setkey(salg)) goto out_put_alg; |
8bd1209cf crypto: hmac - Sw... |
191 |
ds = salg->digestsize; |
0b767b4df crypto: hmac - Pr... |
192 |
ss = salg->statesize; |
0b767b4df crypto: hmac - Pr... |
193 194 |
if (ds > alg->cra_blocksize || ss < alg->cra_blocksize) |
ca786dc73 crypto: hash - Fi... |
195 |
goto out_put_alg; |
8bd1209cf crypto: hmac - Sw... |
196 |
inst = shash_alloc_instance("hmac", alg); |
3b3fc322d crypto: hmac - Fi... |
197 |
err = PTR_ERR(inst); |
0796ae061 [CRYPTO] hmac: Ad... |
198 199 |
if (IS_ERR(inst)) goto out_put_alg; |
8bd1209cf crypto: hmac - Sw... |
200 201 202 203 204 205 206 207 |
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 [CRYPTO] hmac: Ad... |
208 |
|
0b767b4df crypto: hmac - Pr... |
209 |
ss = ALIGN(ss, alg->cra_alignmask + 1); |
8bd1209cf crypto: hmac - Sw... |
210 |
inst->alg.digestsize = ds; |
0b767b4df crypto: hmac - Pr... |
211 |
inst->alg.statesize = ss; |
0796ae061 [CRYPTO] hmac: Ad... |
212 |
|
8bd1209cf crypto: hmac - Sw... |
213 |
inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) + |
0b767b4df crypto: hmac - Pr... |
214 |
ALIGN(ss * 2, crypto_tfm_ctx_alignment()); |
0796ae061 [CRYPTO] hmac: Ad... |
215 |
|
8bd1209cf crypto: hmac - Sw... |
216 217 |
inst->alg.base.cra_init = hmac_init_tfm; inst->alg.base.cra_exit = hmac_exit_tfm; |
0796ae061 [CRYPTO] hmac: Ad... |
218 |
|
8bd1209cf crypto: hmac - Sw... |
219 220 221 222 |
inst->alg.init = hmac_init; inst->alg.update = hmac_update; inst->alg.final = hmac_final; inst->alg.finup = hmac_finup; |
0b767b4df crypto: hmac - Pr... |
223 224 |
inst->alg.export = hmac_export; inst->alg.import = hmac_import; |
8bd1209cf crypto: hmac - Sw... |
225 226 227 228 229 230 231 |
inst->alg.setkey = hmac_setkey; err = shash_register_instance(tmpl, inst); if (err) { out_free_inst: shash_free_instance(shash_crypto_instance(inst)); } |
0796ae061 [CRYPTO] hmac: Ad... |
232 233 234 |
out_put_alg: crypto_mod_put(alg); |
8bd1209cf crypto: hmac - Sw... |
235 |
return err; |
0796ae061 [CRYPTO] hmac: Ad... |
236 237 238 239 |
} static struct crypto_template hmac_tmpl = { .name = "hmac", |
8bd1209cf crypto: hmac - Sw... |
240 241 |
.create = hmac_create, .free = shash_free_instance, |
0796ae061 [CRYPTO] hmac: Ad... |
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
.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); } module_init(hmac_module_init); module_exit(hmac_module_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("HMAC hash algorithm"); |
4943ba16b crypto: include c... |
260 |
MODULE_ALIAS_CRYPTO("hmac"); |