Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * HMAC: Keyed-Hashing for Message Authentication (RFC2104). |
| 5 | * |
| 6 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 7 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 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 Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 14 | * Software Foundation; either version 2 of the License, or (at your option) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * any later version. |
| 16 | * |
| 17 | */ |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 18 | |
| 19 | #include <crypto/algapi.h> |
Herbert Xu | b2ab4a5 | 2007-12-05 20:59:25 +1100 | [diff] [blame] | 20 | #include <crypto/scatterwalk.h> |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 21 | #include <linux/err.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
David Hardeman | 378f058 | 2005-09-17 17:55:31 +1000 | [diff] [blame] | 25 | #include <linux/scatterlist.h> |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | #include <linux/string.h> |
| 28 | |
| 29 | struct hmac_ctx { |
| 30 | struct crypto_hash *child; |
| 31 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 33 | static inline void *align_ptr(void *p, unsigned int align) |
| 34 | { |
| 35 | return (void *)ALIGN((unsigned long)p, align); |
| 36 | } |
| 37 | |
| 38 | static inline struct hmac_ctx *hmac_ctx(struct crypto_hash *tfm) |
| 39 | { |
| 40 | return align_ptr(crypto_hash_ctx_aligned(tfm) + |
| 41 | crypto_hash_blocksize(tfm) * 2 + |
| 42 | crypto_hash_digestsize(tfm), sizeof(void *)); |
| 43 | } |
| 44 | |
| 45 | static int hmac_setkey(struct crypto_hash *parent, |
| 46 | const u8 *inkey, unsigned int keylen) |
| 47 | { |
| 48 | int bs = crypto_hash_blocksize(parent); |
| 49 | int ds = crypto_hash_digestsize(parent); |
| 50 | char *ipad = crypto_hash_ctx_aligned(parent); |
| 51 | char *opad = ipad + bs; |
| 52 | char *digest = opad + bs; |
| 53 | struct hmac_ctx *ctx = align_ptr(digest + ds, sizeof(void *)); |
| 54 | struct crypto_hash *tfm = ctx->child; |
| 55 | unsigned int i; |
| 56 | |
| 57 | if (keylen > bs) { |
| 58 | struct hash_desc desc; |
| 59 | struct scatterlist tmp; |
| 60 | int err; |
| 61 | |
| 62 | desc.tfm = tfm; |
| 63 | desc.flags = crypto_hash_get_flags(parent); |
| 64 | desc.flags &= CRYPTO_TFM_REQ_MAY_SLEEP; |
David S. Miller | a676772 | 2007-10-26 00:37:12 -0700 | [diff] [blame] | 65 | sg_init_one(&tmp, inkey, keylen); |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 66 | |
| 67 | err = crypto_hash_digest(&desc, &tmp, keylen, digest); |
| 68 | if (err) |
| 69 | return err; |
| 70 | |
| 71 | inkey = digest; |
| 72 | keylen = ds; |
| 73 | } |
| 74 | |
| 75 | memcpy(ipad, inkey, keylen); |
| 76 | memset(ipad + keylen, 0, bs - keylen); |
| 77 | memcpy(opad, ipad, bs); |
| 78 | |
| 79 | for (i = 0; i < bs; i++) { |
| 80 | ipad[i] ^= 0x36; |
| 81 | opad[i] ^= 0x5c; |
| 82 | } |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int hmac_init(struct hash_desc *pdesc) |
| 88 | { |
| 89 | struct crypto_hash *parent = pdesc->tfm; |
| 90 | int bs = crypto_hash_blocksize(parent); |
| 91 | int ds = crypto_hash_digestsize(parent); |
| 92 | char *ipad = crypto_hash_ctx_aligned(parent); |
| 93 | struct hmac_ctx *ctx = align_ptr(ipad + bs * 2 + ds, sizeof(void *)); |
| 94 | struct hash_desc desc; |
| 95 | struct scatterlist tmp; |
Herbert Xu | 73af07d | 2006-09-24 09:30:19 +1000 | [diff] [blame] | 96 | int err; |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 97 | |
| 98 | desc.tfm = ctx->child; |
| 99 | desc.flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP; |
David S. Miller | a676772 | 2007-10-26 00:37:12 -0700 | [diff] [blame] | 100 | sg_init_one(&tmp, ipad, bs); |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 101 | |
Herbert Xu | 73af07d | 2006-09-24 09:30:19 +1000 | [diff] [blame] | 102 | err = crypto_hash_init(&desc); |
| 103 | if (unlikely(err)) |
| 104 | return err; |
| 105 | |
| 106 | return crypto_hash_update(&desc, &tmp, bs); |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static int hmac_update(struct hash_desc *pdesc, |
| 110 | struct scatterlist *sg, unsigned int nbytes) |
| 111 | { |
| 112 | struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm); |
| 113 | struct hash_desc desc; |
| 114 | |
| 115 | desc.tfm = ctx->child; |
| 116 | desc.flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP; |
| 117 | |
| 118 | return crypto_hash_update(&desc, sg, nbytes); |
| 119 | } |
| 120 | |
| 121 | static int hmac_final(struct hash_desc *pdesc, u8 *out) |
| 122 | { |
| 123 | struct crypto_hash *parent = pdesc->tfm; |
| 124 | int bs = crypto_hash_blocksize(parent); |
| 125 | int ds = crypto_hash_digestsize(parent); |
| 126 | char *opad = crypto_hash_ctx_aligned(parent) + bs; |
| 127 | char *digest = opad + bs; |
| 128 | struct hmac_ctx *ctx = align_ptr(digest + ds, sizeof(void *)); |
| 129 | struct hash_desc desc; |
| 130 | struct scatterlist tmp; |
Herbert Xu | 73af07d | 2006-09-24 09:30:19 +1000 | [diff] [blame] | 131 | int err; |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 132 | |
| 133 | desc.tfm = ctx->child; |
| 134 | desc.flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP; |
David S. Miller | a676772 | 2007-10-26 00:37:12 -0700 | [diff] [blame] | 135 | sg_init_one(&tmp, opad, bs + ds); |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 136 | |
Herbert Xu | 73af07d | 2006-09-24 09:30:19 +1000 | [diff] [blame] | 137 | err = crypto_hash_final(&desc, digest); |
| 138 | if (unlikely(err)) |
| 139 | return err; |
| 140 | |
| 141 | return crypto_hash_digest(&desc, &tmp, bs + ds, out); |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static int hmac_digest(struct hash_desc *pdesc, struct scatterlist *sg, |
| 145 | unsigned int nbytes, u8 *out) |
| 146 | { |
| 147 | struct crypto_hash *parent = pdesc->tfm; |
| 148 | int bs = crypto_hash_blocksize(parent); |
| 149 | int ds = crypto_hash_digestsize(parent); |
| 150 | char *ipad = crypto_hash_ctx_aligned(parent); |
| 151 | char *opad = ipad + bs; |
| 152 | char *digest = opad + bs; |
| 153 | struct hmac_ctx *ctx = align_ptr(digest + ds, sizeof(void *)); |
| 154 | struct hash_desc desc; |
| 155 | struct scatterlist sg1[2]; |
| 156 | struct scatterlist sg2[1]; |
Herbert Xu | 73af07d | 2006-09-24 09:30:19 +1000 | [diff] [blame] | 157 | int err; |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 158 | |
| 159 | desc.tfm = ctx->child; |
| 160 | desc.flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP; |
| 161 | |
Vlad Yasevich | 41fb285 | 2007-10-25 18:46:26 -0700 | [diff] [blame] | 162 | sg_init_table(sg1, 2); |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 163 | sg_set_buf(sg1, ipad, bs); |
Herbert Xu | b2ab4a5 | 2007-12-05 20:59:25 +1100 | [diff] [blame] | 164 | scatterwalk_sg_chain(sg1, 2, sg); |
Jens Axboe | 78c2f0b | 2007-10-22 19:40:16 +0200 | [diff] [blame] | 165 | |
Vlad Yasevich | 41fb285 | 2007-10-25 18:46:26 -0700 | [diff] [blame] | 166 | sg_init_table(sg2, 1); |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 167 | sg_set_buf(sg2, opad, bs + ds); |
| 168 | |
Herbert Xu | 73af07d | 2006-09-24 09:30:19 +1000 | [diff] [blame] | 169 | err = crypto_hash_digest(&desc, sg1, nbytes + bs, digest); |
| 170 | if (unlikely(err)) |
| 171 | return err; |
| 172 | |
| 173 | return crypto_hash_digest(&desc, sg2, bs + ds, out); |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static int hmac_init_tfm(struct crypto_tfm *tfm) |
| 177 | { |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 178 | struct crypto_hash *hash; |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 179 | struct crypto_instance *inst = (void *)tfm->__crt_alg; |
| 180 | struct crypto_spawn *spawn = crypto_instance_ctx(inst); |
| 181 | struct hmac_ctx *ctx = hmac_ctx(__crypto_hash_cast(tfm)); |
| 182 | |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 183 | hash = crypto_spawn_hash(spawn); |
| 184 | if (IS_ERR(hash)) |
| 185 | return PTR_ERR(hash); |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 186 | |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 187 | ctx->child = hash; |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static void hmac_exit_tfm(struct crypto_tfm *tfm) |
| 192 | { |
| 193 | struct hmac_ctx *ctx = hmac_ctx(__crypto_hash_cast(tfm)); |
| 194 | crypto_free_hash(ctx->child); |
| 195 | } |
| 196 | |
| 197 | static void hmac_free(struct crypto_instance *inst) |
| 198 | { |
| 199 | crypto_drop_spawn(crypto_instance_ctx(inst)); |
| 200 | kfree(inst); |
| 201 | } |
| 202 | |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 203 | static struct crypto_instance *hmac_alloc(struct rtattr **tb) |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 204 | { |
| 205 | struct crypto_instance *inst; |
| 206 | struct crypto_alg *alg; |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 207 | int err; |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 208 | |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 209 | err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_HASH); |
| 210 | if (err) |
| 211 | return ERR_PTR(err); |
| 212 | |
| 213 | alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_HASH, |
| 214 | CRYPTO_ALG_TYPE_HASH_MASK); |
Herbert Xu | 0796ae0 | 2006-08-21 20:50:52 +1000 | [diff] [blame] | 215 | if (IS_ERR(alg)) |
| 216 | return ERR_PTR(PTR_ERR(alg)); |
| 217 | |
| 218 | inst = crypto_alloc_instance("hmac", alg); |
| 219 | if (IS_ERR(inst)) |
| 220 | goto out_put_alg; |
| 221 | |
| 222 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_HASH; |
| 223 | inst->alg.cra_priority = alg->cra_priority; |
| 224 | inst->alg.cra_blocksize = alg->cra_blocksize; |
| 225 | inst->alg.cra_alignmask = alg->cra_alignmask; |
| 226 | inst->alg.cra_type = &crypto_hash_type; |
| 227 | |
| 228 | inst->alg.cra_hash.digestsize = |
| 229 | (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == |
| 230 | CRYPTO_ALG_TYPE_HASH ? alg->cra_hash.digestsize : |
| 231 | alg->cra_digest.dia_digestsize; |
| 232 | |
| 233 | inst->alg.cra_ctxsize = sizeof(struct hmac_ctx) + |
| 234 | ALIGN(inst->alg.cra_blocksize * 2 + |
| 235 | inst->alg.cra_hash.digestsize, |
| 236 | sizeof(void *)); |
| 237 | |
| 238 | inst->alg.cra_init = hmac_init_tfm; |
| 239 | inst->alg.cra_exit = hmac_exit_tfm; |
| 240 | |
| 241 | inst->alg.cra_hash.init = hmac_init; |
| 242 | inst->alg.cra_hash.update = hmac_update; |
| 243 | inst->alg.cra_hash.final = hmac_final; |
| 244 | inst->alg.cra_hash.digest = hmac_digest; |
| 245 | inst->alg.cra_hash.setkey = hmac_setkey; |
| 246 | |
| 247 | out_put_alg: |
| 248 | crypto_mod_put(alg); |
| 249 | return inst; |
| 250 | } |
| 251 | |
| 252 | static struct crypto_template hmac_tmpl = { |
| 253 | .name = "hmac", |
| 254 | .alloc = hmac_alloc, |
| 255 | .free = hmac_free, |
| 256 | .module = THIS_MODULE, |
| 257 | }; |
| 258 | |
| 259 | static int __init hmac_module_init(void) |
| 260 | { |
| 261 | return crypto_register_template(&hmac_tmpl); |
| 262 | } |
| 263 | |
| 264 | static void __exit hmac_module_exit(void) |
| 265 | { |
| 266 | crypto_unregister_template(&hmac_tmpl); |
| 267 | } |
| 268 | |
| 269 | module_init(hmac_module_init); |
| 270 | module_exit(hmac_module_exit); |
| 271 | |
| 272 | MODULE_LICENSE("GPL"); |
| 273 | MODULE_DESCRIPTION("HMAC hash algorithm"); |