blob: e37342748f2fa7ff837876f6e59e8aa20820537d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Cryptographic API.
3 *
4 * HMAC: Keyed-Hashing for Message Authentication (RFC2104).
5 *
6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
Herbert Xu0796ae02006-08-21 20:50:52 +10007 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * The HMAC implementation is derived from USAGI.
10 * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI
11 *
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
Herbert Xu0796ae02006-08-21 20:50:52 +100014 * Software Foundation; either version 2 of the License, or (at your option)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * any later version.
16 *
17 */
Herbert Xu0796ae02006-08-21 20:50:52 +100018
Herbert Xu5f7082e2008-08-31 22:21:09 +100019#include <crypto/internal/hash.h>
Herbert Xub2ab4a52007-12-05 20:59:25 +110020#include <crypto/scatterwalk.h>
Herbert Xu0796ae02006-08-21 20:50:52 +100021#include <linux/err.h>
22#include <linux/init.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
David Hardeman378f0582005-09-17 17:55:31 +100025#include <linux/scatterlist.h>
Herbert Xu0796ae02006-08-21 20:50:52 +100026#include <linux/slab.h>
27#include <linux/string.h>
28
29struct hmac_ctx {
Herbert Xu8bd12092009-07-09 12:43:37 +080030 struct shash_desc *desc;
Herbert Xu0796ae02006-08-21 20:50:52 +100031};
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Herbert Xu0796ae02006-08-21 20:50:52 +100033static inline void *align_ptr(void *p, unsigned int align)
34{
35 return (void *)ALIGN((unsigned long)p, align);
36}
37
Herbert Xu8bd12092009-07-09 12:43:37 +080038static inline struct hmac_ctx *hmac_ctx(struct crypto_shash *tfm)
Herbert Xu0796ae02006-08-21 20:50:52 +100039{
Herbert Xu8bd12092009-07-09 12:43:37 +080040 return align_ptr(crypto_shash_ctx_aligned(tfm) +
41 crypto_shash_blocksize(tfm) * 2 +
42 crypto_shash_digestsize(tfm),
43 crypto_tfm_ctx_alignment());
Herbert Xu0796ae02006-08-21 20:50:52 +100044}
45
Herbert Xu8bd12092009-07-09 12:43:37 +080046static int hmac_setkey(struct crypto_shash *parent,
Herbert Xu0796ae02006-08-21 20:50:52 +100047 const u8 *inkey, unsigned int keylen)
48{
Herbert Xu8bd12092009-07-09 12:43:37 +080049 int bs = crypto_shash_blocksize(parent);
50 int ds = crypto_shash_digestsize(parent);
51 char *ipad = crypto_shash_ctx_aligned(parent);
Herbert Xu0796ae02006-08-21 20:50:52 +100052 char *opad = ipad + bs;
53 char *digest = opad + bs;
Herbert Xu8bd12092009-07-09 12:43:37 +080054 struct hmac_ctx *ctx = align_ptr(digest + ds,
55 crypto_tfm_ctx_alignment());
Herbert Xu0796ae02006-08-21 20:50:52 +100056 unsigned int i;
57
58 if (keylen > bs) {
Herbert Xu0796ae02006-08-21 20:50:52 +100059 int err;
60
Herbert Xu8bd12092009-07-09 12:43:37 +080061 ctx->desc->flags = crypto_shash_get_flags(parent) &
62 CRYPTO_TFM_REQ_MAY_SLEEP;
Herbert Xu0796ae02006-08-21 20:50:52 +100063
Herbert Xu8bd12092009-07-09 12:43:37 +080064 err = crypto_shash_digest(ctx->desc, inkey, keylen, digest);
Herbert Xu0796ae02006-08-21 20:50:52 +100065 if (err)
66 return err;
67
68 inkey = digest;
69 keylen = ds;
70 }
71
72 memcpy(ipad, inkey, keylen);
73 memset(ipad + keylen, 0, bs - keylen);
74 memcpy(opad, ipad, bs);
75
76 for (i = 0; i < bs; i++) {
77 ipad[i] ^= 0x36;
78 opad[i] ^= 0x5c;
79 }
80
81 return 0;
82}
83
Herbert Xu8bd12092009-07-09 12:43:37 +080084static int hmac_init(struct shash_desc *pdesc)
Herbert Xu0796ae02006-08-21 20:50:52 +100085{
Herbert Xu8bd12092009-07-09 12:43:37 +080086 struct crypto_shash *parent = pdesc->tfm;
87 int bs = crypto_shash_blocksize(parent);
88 int ds = crypto_shash_digestsize(parent);
89 char *ipad = crypto_shash_ctx_aligned(parent);
90 struct hmac_ctx *ctx = align_ptr(ipad + bs * 2 + ds,
91 crypto_tfm_ctx_alignment());
92 struct shash_desc *desc = shash_desc_ctx(pdesc);
Herbert Xu0796ae02006-08-21 20:50:52 +100093
Herbert Xu8bd12092009-07-09 12:43:37 +080094 desc->tfm = ctx->desc->tfm;
95 desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
Herbert Xu0796ae02006-08-21 20:50:52 +100096
Herbert Xu8bd12092009-07-09 12:43:37 +080097 return crypto_shash_init(desc) ?:
98 crypto_shash_update(desc, ipad, bs);
Herbert Xu0796ae02006-08-21 20:50:52 +100099}
100
Herbert Xu8bd12092009-07-09 12:43:37 +0800101static int hmac_update(struct shash_desc *pdesc,
102 const u8 *data, unsigned int nbytes)
Herbert Xu0796ae02006-08-21 20:50:52 +1000103{
Herbert Xu8bd12092009-07-09 12:43:37 +0800104 struct shash_desc *desc = shash_desc_ctx(pdesc);
Herbert Xu0796ae02006-08-21 20:50:52 +1000105
Herbert Xu8bd12092009-07-09 12:43:37 +0800106 desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
Herbert Xu0796ae02006-08-21 20:50:52 +1000107
Herbert Xu8bd12092009-07-09 12:43:37 +0800108 return crypto_shash_update(desc, data, nbytes);
Herbert Xu0796ae02006-08-21 20:50:52 +1000109}
110
Herbert Xu8bd12092009-07-09 12:43:37 +0800111static int hmac_final(struct shash_desc *pdesc, u8 *out)
Herbert Xu0796ae02006-08-21 20:50:52 +1000112{
Herbert Xu8bd12092009-07-09 12:43:37 +0800113 struct crypto_shash *parent = pdesc->tfm;
114 int bs = crypto_shash_blocksize(parent);
115 int ds = crypto_shash_digestsize(parent);
116 char *opad = crypto_shash_ctx_aligned(parent) + bs;
Herbert Xu0796ae02006-08-21 20:50:52 +1000117 char *digest = opad + bs;
Herbert Xu8bd12092009-07-09 12:43:37 +0800118 struct shash_desc *desc = shash_desc_ctx(pdesc);
Herbert Xu0796ae02006-08-21 20:50:52 +1000119
Herbert Xu8bd12092009-07-09 12:43:37 +0800120 desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
Herbert Xu0796ae02006-08-21 20:50:52 +1000121
Herbert Xu8bd12092009-07-09 12:43:37 +0800122 return crypto_shash_final(desc, digest) ?:
123 crypto_shash_digest(desc, opad, bs + 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;
131 int bs = crypto_shash_blocksize(parent);
132 int ds = crypto_shash_digestsize(parent);
133 char *opad = crypto_shash_ctx_aligned(parent) + bs;
Herbert Xu0796ae02006-08-21 20:50:52 +1000134 char *digest = opad + bs;
Herbert Xu8bd12092009-07-09 12:43:37 +0800135 struct shash_desc *desc = shash_desc_ctx(pdesc);
Herbert Xu0796ae02006-08-21 20:50:52 +1000136
Herbert Xu8bd12092009-07-09 12:43:37 +0800137 desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
Herbert Xu0796ae02006-08-21 20:50:52 +1000138
Herbert Xu8bd12092009-07-09 12:43:37 +0800139 return crypto_shash_finup(desc, data, nbytes, digest) ?:
140 crypto_shash_digest(desc, opad, bs + ds, out);
Herbert Xu0796ae02006-08-21 20:50:52 +1000141}
142
143static int hmac_init_tfm(struct crypto_tfm *tfm)
144{
Herbert Xu8bd12092009-07-09 12:43:37 +0800145 struct crypto_shash *parent = __crypto_shash_cast(tfm);
146 struct crypto_shash *hash;
Herbert Xu0796ae02006-08-21 20:50:52 +1000147 struct crypto_instance *inst = (void *)tfm->__crt_alg;
Herbert Xu8bd12092009-07-09 12:43:37 +0800148 struct crypto_shash_spawn *spawn = crypto_instance_ctx(inst);
149 struct hmac_ctx *ctx = hmac_ctx(parent);
Herbert Xu0796ae02006-08-21 20:50:52 +1000150
Herbert Xu8bd12092009-07-09 12:43:37 +0800151 hash = crypto_spawn_shash(spawn);
Herbert Xu2e306ee2006-12-17 10:05:58 +1100152 if (IS_ERR(hash))
153 return PTR_ERR(hash);
Herbert Xu0796ae02006-08-21 20:50:52 +1000154
Herbert Xu8bd12092009-07-09 12:43:37 +0800155 parent->descsize = sizeof(struct shash_desc) +
156 crypto_shash_descsize(hash);
157
158 ctx->desc = kmalloc(parent->descsize, GFP_KERNEL);
159 if (!ctx->desc) {
160 crypto_free_shash(hash);
161 return -ENOMEM;
162 }
163
164 ctx->desc->tfm = hash;
Herbert Xu0796ae02006-08-21 20:50:52 +1000165 return 0;
166}
167
168static void hmac_exit_tfm(struct crypto_tfm *tfm)
169{
Herbert Xu8bd12092009-07-09 12:43:37 +0800170 struct hmac_ctx *ctx = hmac_ctx(__crypto_shash_cast(tfm));
171 crypto_free_shash(ctx->desc->tfm);
172 kzfree(ctx->desc);
Herbert Xu0796ae02006-08-21 20:50:52 +1000173}
174
Herbert Xu8bd12092009-07-09 12:43:37 +0800175static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu0796ae02006-08-21 20:50:52 +1000176{
Herbert Xu8bd12092009-07-09 12:43:37 +0800177 struct shash_instance *inst;
Herbert Xu0796ae02006-08-21 20:50:52 +1000178 struct crypto_alg *alg;
Herbert Xu8bd12092009-07-09 12:43:37 +0800179 struct shash_alg *salg;
Herbert Xuebc610e2007-01-01 18:37:02 +1100180 int err;
Herbert Xuca786dc2008-07-07 20:23:56 +0800181 int ds;
Herbert Xu0796ae02006-08-21 20:50:52 +1000182
Herbert Xu8bd12092009-07-09 12:43:37 +0800183 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
Herbert Xuebc610e2007-01-01 18:37:02 +1100184 if (err)
Herbert Xu8bd12092009-07-09 12:43:37 +0800185 return err;
Herbert Xuebc610e2007-01-01 18:37:02 +1100186
Herbert Xu8bd12092009-07-09 12:43:37 +0800187 salg = shash_attr_alg(tb[1], 0, 0);
188 if (IS_ERR(salg))
189 return PTR_ERR(salg);
Herbert Xu0796ae02006-08-21 20:50:52 +1000190
Herbert Xu8bd12092009-07-09 12:43:37 +0800191 err = -EINVAL;
192 ds = salg->digestsize;
193 alg = &salg->base;
Herbert Xuca786dc2008-07-07 20:23:56 +0800194 if (ds > alg->cra_blocksize)
195 goto out_put_alg;
196
Herbert Xu8bd12092009-07-09 12:43:37 +0800197 inst = shash_alloc_instance("hmac", alg);
Herbert Xu0796ae02006-08-21 20:50:52 +1000198 if (IS_ERR(inst))
199 goto out_put_alg;
200
Herbert Xu8bd12092009-07-09 12:43:37 +0800201 err = crypto_init_shash_spawn(shash_instance_ctx(inst), salg,
202 shash_crypto_instance(inst));
203 if (err)
204 goto out_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 Xu8bd12092009-07-09 12:43:37 +0800210 inst->alg.digestsize = ds;
Herbert Xu0796ae02006-08-21 20:50:52 +1000211
Herbert Xu8bd12092009-07-09 12:43:37 +0800212 inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) +
213 ALIGN(alg->cra_blocksize * 2 + ds,
214 crypto_tfm_ctx_alignment());
Herbert Xu0796ae02006-08-21 20:50:52 +1000215
Herbert Xu8bd12092009-07-09 12:43:37 +0800216 inst->alg.base.cra_init = hmac_init_tfm;
217 inst->alg.base.cra_exit = hmac_exit_tfm;
218
219 inst->alg.init = hmac_init;
220 inst->alg.update = hmac_update;
221 inst->alg.final = hmac_final;
222 inst->alg.finup = hmac_finup;
223 inst->alg.setkey = hmac_setkey;
224
225 err = shash_register_instance(tmpl, inst);
226 if (err) {
227out_free_inst:
228 shash_free_instance(shash_crypto_instance(inst));
229 }
Herbert Xu0796ae02006-08-21 20:50:52 +1000230
231out_put_alg:
232 crypto_mod_put(alg);
Herbert Xu8bd12092009-07-09 12:43:37 +0800233 return err;
Herbert Xu0796ae02006-08-21 20:50:52 +1000234}
235
236static struct crypto_template hmac_tmpl = {
237 .name = "hmac",
Herbert Xu8bd12092009-07-09 12:43:37 +0800238 .create = hmac_create,
239 .free = shash_free_instance,
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
253module_init(hmac_module_init);
254module_exit(hmac_module_exit);
255
256MODULE_LICENSE("GPL");
257MODULE_DESCRIPTION("HMAC hash algorithm");