Lynx framebuffers multidomain implementation.
[linux/elbrus.git] / crypto / hmac.c
blobba017e70f537b9b161552df7652a05b67bc172ae
1 /*
2 * Cryptographic API.
4 * HMAC: Keyed-Hashing for Message Authentication (RFC2104).
6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
7 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
9 * The HMAC implementation is derived from USAGI.
10 * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
19 #include <crypto/internal/hash.h>
20 #include <crypto/scatterwalk.h>
21 #include <linux/err.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/scatterlist.h>
26 #include <linux/string.h>
28 struct hmac_ctx {
29 struct crypto_shash *hash;
32 static inline void *align_ptr(void *p, unsigned int align)
34 return (void *)ALIGN((unsigned long)p, align);
37 static inline struct hmac_ctx *hmac_ctx(struct crypto_shash *tfm)
39 return align_ptr(crypto_shash_ctx_aligned(tfm) +
40 crypto_shash_statesize(tfm) * 2,
41 crypto_tfm_ctx_alignment());
44 /* LCC does not understand VLA in structures */
45 #if !defined CONFIG_MCST || !defined __LCC__
46 static int hmac_setkey(struct crypto_shash *parent,
47 const u8 *inkey, unsigned int keylen)
49 int bs = crypto_shash_blocksize(parent);
50 int ds = crypto_shash_digestsize(parent);
51 int ss = crypto_shash_statesize(parent);
52 char *ipad = crypto_shash_ctx_aligned(parent);
53 char *opad = ipad + ss;
54 struct hmac_ctx *ctx = align_ptr(opad + ss,
55 crypto_tfm_ctx_alignment());
56 struct crypto_shash *hash = ctx->hash;
57 struct {
58 struct shash_desc shash;
59 char ctx[crypto_shash_descsize(hash)];
60 } desc;
61 unsigned int i;
63 desc.shash.tfm = hash;
64 desc.shash.flags = crypto_shash_get_flags(parent) &
65 CRYPTO_TFM_REQ_MAY_SLEEP;
67 if (keylen > bs) {
68 int err;
70 err = crypto_shash_digest(&desc.shash, inkey, keylen, ipad);
71 if (err)
72 return err;
74 keylen = ds;
75 } else
76 memcpy(ipad, inkey, keylen);
78 memset(ipad + keylen, 0, bs - keylen);
79 memcpy(opad, ipad, bs);
81 for (i = 0; i < bs; i++) {
82 ipad[i] ^= 0x36;
83 opad[i] ^= 0x5c;
86 return crypto_shash_init(&desc.shash) ?:
87 crypto_shash_update(&desc.shash, ipad, bs) ?:
88 crypto_shash_export(&desc.shash, ipad) ?:
89 crypto_shash_init(&desc.shash) ?:
90 crypto_shash_update(&desc.shash, opad, bs) ?:
91 crypto_shash_export(&desc.shash, opad);
93 #else /* !defined CONFIG_MCST || !defined __LCC__ || __LCC__ != 118 */
94 static int hmac_setkey(struct crypto_shash *parent,
95 const u8 *inkey, unsigned int keylen)
97 int bs = crypto_shash_blocksize(parent);
98 int ds = crypto_shash_digestsize(parent);
99 int ss = crypto_shash_statesize(parent);
100 char *ipad = crypto_shash_ctx_aligned(parent);
101 char *opad = ipad + ss;
102 struct hmac_ctx *ctx = align_ptr(opad + ss,
103 crypto_tfm_ctx_alignment());
104 struct crypto_shash *hash = ctx->hash;
105 struct shash_desc *desc;
106 unsigned int i;
108 desc = __builtin_alloca(sizeof(*desc) + crypto_shash_descsize(hash));
110 desc->tfm = hash;
111 desc->flags = crypto_shash_get_flags(parent) &
112 CRYPTO_TFM_REQ_MAY_SLEEP;
114 if (keylen > bs) {
115 int err;
117 err = crypto_shash_digest(desc, inkey, keylen, ipad);
118 if (err)
119 return err;
121 keylen = ds;
122 } else
123 memcpy(ipad, inkey, keylen);
125 memset(ipad + keylen, 0, bs - keylen);
126 memcpy(opad, ipad, bs);
128 for (i = 0; i < bs; i++) {
129 ipad[i] ^= 0x36;
130 opad[i] ^= 0x5c;
133 return crypto_shash_init(desc) ?:
134 crypto_shash_update(desc, ipad, bs) ?:
135 crypto_shash_export(desc, ipad) ?:
136 crypto_shash_init(desc) ?:
137 crypto_shash_update(desc, opad, bs) ?:
138 crypto_shash_export(desc, opad);
140 #endif /* !defined CONFIG_MCST || !defined __LCC__ || __LCC__ != 118 */
142 static int hmac_export(struct shash_desc *pdesc, void *out)
144 struct shash_desc *desc = shash_desc_ctx(pdesc);
146 desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
148 return crypto_shash_export(desc, out);
151 static int hmac_import(struct shash_desc *pdesc, const void *in)
153 struct shash_desc *desc = shash_desc_ctx(pdesc);
154 struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm);
156 desc->tfm = ctx->hash;
157 desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
159 return crypto_shash_import(desc, in);
162 static int hmac_init(struct shash_desc *pdesc)
164 return hmac_import(pdesc, crypto_shash_ctx_aligned(pdesc->tfm));
167 static int hmac_update(struct shash_desc *pdesc,
168 const u8 *data, unsigned int nbytes)
170 struct shash_desc *desc = shash_desc_ctx(pdesc);
172 desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
174 return crypto_shash_update(desc, data, nbytes);
177 static int hmac_final(struct shash_desc *pdesc, u8 *out)
179 struct crypto_shash *parent = pdesc->tfm;
180 int ds = crypto_shash_digestsize(parent);
181 int ss = crypto_shash_statesize(parent);
182 char *opad = crypto_shash_ctx_aligned(parent) + ss;
183 struct shash_desc *desc = shash_desc_ctx(pdesc);
185 desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
187 return crypto_shash_final(desc, out) ?:
188 crypto_shash_import(desc, opad) ?:
189 crypto_shash_finup(desc, out, ds, out);
192 static int hmac_finup(struct shash_desc *pdesc, const u8 *data,
193 unsigned int nbytes, u8 *out)
196 struct crypto_shash *parent = pdesc->tfm;
197 int ds = crypto_shash_digestsize(parent);
198 int ss = crypto_shash_statesize(parent);
199 char *opad = crypto_shash_ctx_aligned(parent) + ss;
200 struct shash_desc *desc = shash_desc_ctx(pdesc);
202 desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
204 return crypto_shash_finup(desc, data, nbytes, out) ?:
205 crypto_shash_import(desc, opad) ?:
206 crypto_shash_finup(desc, out, ds, out);
209 static int hmac_init_tfm(struct crypto_tfm *tfm)
211 struct crypto_shash *parent = __crypto_shash_cast(tfm);
212 struct crypto_shash *hash;
213 struct crypto_instance *inst = (void *)tfm->__crt_alg;
214 struct crypto_shash_spawn *spawn = crypto_instance_ctx(inst);
215 struct hmac_ctx *ctx = hmac_ctx(parent);
217 hash = crypto_spawn_shash(spawn);
218 if (IS_ERR(hash))
219 return PTR_ERR(hash);
221 parent->descsize = sizeof(struct shash_desc) +
222 crypto_shash_descsize(hash);
224 ctx->hash = hash;
225 return 0;
228 static void hmac_exit_tfm(struct crypto_tfm *tfm)
230 struct hmac_ctx *ctx = hmac_ctx(__crypto_shash_cast(tfm));
231 crypto_free_shash(ctx->hash);
234 static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
236 struct shash_instance *inst;
237 struct crypto_alg *alg;
238 struct shash_alg *salg;
239 int err;
240 int ds;
241 int ss;
243 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
244 if (err)
245 return err;
247 salg = shash_attr_alg(tb[1], 0, 0);
248 if (IS_ERR(salg))
249 return PTR_ERR(salg);
251 err = -EINVAL;
252 ds = salg->digestsize;
253 ss = salg->statesize;
254 alg = &salg->base;
255 if (ds > alg->cra_blocksize ||
256 ss < alg->cra_blocksize)
257 goto out_put_alg;
259 inst = shash_alloc_instance("hmac", alg);
260 err = PTR_ERR(inst);
261 if (IS_ERR(inst))
262 goto out_put_alg;
264 err = crypto_init_shash_spawn(shash_instance_ctx(inst), salg,
265 shash_crypto_instance(inst));
266 if (err)
267 goto out_free_inst;
269 inst->alg.base.cra_priority = alg->cra_priority;
270 inst->alg.base.cra_blocksize = alg->cra_blocksize;
271 inst->alg.base.cra_alignmask = alg->cra_alignmask;
273 ss = ALIGN(ss, alg->cra_alignmask + 1);
274 inst->alg.digestsize = ds;
275 inst->alg.statesize = ss;
277 inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) +
278 ALIGN(ss * 2, crypto_tfm_ctx_alignment());
280 inst->alg.base.cra_init = hmac_init_tfm;
281 inst->alg.base.cra_exit = hmac_exit_tfm;
283 inst->alg.init = hmac_init;
284 inst->alg.update = hmac_update;
285 inst->alg.final = hmac_final;
286 inst->alg.finup = hmac_finup;
287 inst->alg.export = hmac_export;
288 inst->alg.import = hmac_import;
289 inst->alg.setkey = hmac_setkey;
291 err = shash_register_instance(tmpl, inst);
292 if (err) {
293 out_free_inst:
294 shash_free_instance(shash_crypto_instance(inst));
297 out_put_alg:
298 crypto_mod_put(alg);
299 return err;
302 static struct crypto_template hmac_tmpl = {
303 .name = "hmac",
304 .create = hmac_create,
305 .free = shash_free_instance,
306 .module = THIS_MODULE,
309 static int __init hmac_module_init(void)
311 return crypto_register_template(&hmac_tmpl);
314 static void __exit hmac_module_exit(void)
316 crypto_unregister_template(&hmac_tmpl);
319 module_init(hmac_module_init);
320 module_exit(hmac_module_exit);
322 MODULE_LICENSE("GPL");
323 MODULE_DESCRIPTION("HMAC hash algorithm");
324 MODULE_ALIAS_CRYPTO("hmac");