Back to home page

LXR

 
 

    


0001 /*
0002  * Cryptographic API.
0003  *
0004  * HMAC: Keyed-Hashing for Message Authentication (RFC2104).
0005  *
0006  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
0007  * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
0008  *
0009  * The HMAC implementation is derived from USAGI.
0010  * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI
0011  *
0012  * This program is free software; you can redistribute it and/or modify it
0013  * under the terms of the GNU General Public License as published by the Free
0014  * Software Foundation; either version 2 of the License, or (at your option)
0015  * any later version.
0016  *
0017  */
0018 
0019 #include <crypto/internal/hash.h>
0020 #include <crypto/scatterwalk.h>
0021 #include <linux/err.h>
0022 #include <linux/init.h>
0023 #include <linux/kernel.h>
0024 #include <linux/module.h>
0025 #include <linux/scatterlist.h>
0026 #include <linux/string.h>
0027 
0028 struct hmac_ctx {
0029     struct crypto_shash *hash;
0030 };
0031 
0032 static inline void *align_ptr(void *p, unsigned int align)
0033 {
0034     return (void *)ALIGN((unsigned long)p, align);
0035 }
0036 
0037 static inline struct hmac_ctx *hmac_ctx(struct crypto_shash *tfm)
0038 {
0039     return align_ptr(crypto_shash_ctx_aligned(tfm) +
0040              crypto_shash_statesize(tfm) * 2,
0041              crypto_tfm_ctx_alignment());
0042 }
0043 
0044 static int hmac_setkey(struct crypto_shash *parent,
0045                const u8 *inkey, unsigned int keylen)
0046 {
0047     int bs = crypto_shash_blocksize(parent);
0048     int ds = crypto_shash_digestsize(parent);
0049     int ss = crypto_shash_statesize(parent);
0050     char *ipad = crypto_shash_ctx_aligned(parent);
0051     char *opad = ipad + ss;
0052     struct hmac_ctx *ctx = align_ptr(opad + ss,
0053                      crypto_tfm_ctx_alignment());
0054     struct crypto_shash *hash = ctx->hash;
0055     SHASH_DESC_ON_STACK(shash, hash);
0056     unsigned int i;
0057 
0058     shash->tfm = hash;
0059     shash->flags = crypto_shash_get_flags(parent)
0060         & CRYPTO_TFM_REQ_MAY_SLEEP;
0061 
0062     if (keylen > bs) {
0063         int err;
0064 
0065         err = crypto_shash_digest(shash, inkey, keylen, ipad);
0066         if (err)
0067             return err;
0068 
0069         keylen = ds;
0070     } else
0071         memcpy(ipad, inkey, keylen);
0072 
0073     memset(ipad + keylen, 0, bs - keylen);
0074     memcpy(opad, ipad, bs);
0075 
0076     for (i = 0; i < bs; i++) {
0077         ipad[i] ^= 0x36;
0078         opad[i] ^= 0x5c;
0079     }
0080 
0081     return crypto_shash_init(shash) ?:
0082            crypto_shash_update(shash, ipad, bs) ?:
0083            crypto_shash_export(shash, ipad) ?:
0084            crypto_shash_init(shash) ?:
0085            crypto_shash_update(shash, opad, bs) ?:
0086            crypto_shash_export(shash, opad);
0087 }
0088 
0089 static int hmac_export(struct shash_desc *pdesc, void *out)
0090 {
0091     struct shash_desc *desc = shash_desc_ctx(pdesc);
0092 
0093     desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
0094 
0095     return crypto_shash_export(desc, out);
0096 }
0097 
0098 static int hmac_import(struct shash_desc *pdesc, const void *in)
0099 {
0100     struct shash_desc *desc = shash_desc_ctx(pdesc);
0101     struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm);
0102 
0103     desc->tfm = ctx->hash;
0104     desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
0105 
0106     return crypto_shash_import(desc, in);
0107 }
0108 
0109 static int hmac_init(struct shash_desc *pdesc)
0110 {
0111     return hmac_import(pdesc, crypto_shash_ctx_aligned(pdesc->tfm));
0112 }
0113 
0114 static int hmac_update(struct shash_desc *pdesc,
0115                const u8 *data, unsigned int nbytes)
0116 {
0117     struct shash_desc *desc = shash_desc_ctx(pdesc);
0118 
0119     desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
0120 
0121     return crypto_shash_update(desc, data, nbytes);
0122 }
0123 
0124 static int hmac_final(struct shash_desc *pdesc, u8 *out)
0125 {
0126     struct crypto_shash *parent = pdesc->tfm;
0127     int ds = crypto_shash_digestsize(parent);
0128     int ss = crypto_shash_statesize(parent);
0129     char *opad = crypto_shash_ctx_aligned(parent) + ss;
0130     struct shash_desc *desc = shash_desc_ctx(pdesc);
0131 
0132     desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
0133 
0134     return crypto_shash_final(desc, out) ?:
0135            crypto_shash_import(desc, opad) ?:
0136            crypto_shash_finup(desc, out, ds, out);
0137 }
0138 
0139 static int hmac_finup(struct shash_desc *pdesc, const u8 *data,
0140               unsigned int nbytes, u8 *out)
0141 {
0142 
0143     struct crypto_shash *parent = pdesc->tfm;
0144     int ds = crypto_shash_digestsize(parent);
0145     int ss = crypto_shash_statesize(parent);
0146     char *opad = crypto_shash_ctx_aligned(parent) + ss;
0147     struct shash_desc *desc = shash_desc_ctx(pdesc);
0148 
0149     desc->flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
0150 
0151     return crypto_shash_finup(desc, data, nbytes, out) ?:
0152            crypto_shash_import(desc, opad) ?:
0153            crypto_shash_finup(desc, out, ds, out);
0154 }
0155 
0156 static int hmac_init_tfm(struct crypto_tfm *tfm)
0157 {
0158     struct crypto_shash *parent = __crypto_shash_cast(tfm);
0159     struct crypto_shash *hash;
0160     struct crypto_instance *inst = (void *)tfm->__crt_alg;
0161     struct crypto_shash_spawn *spawn = crypto_instance_ctx(inst);
0162     struct hmac_ctx *ctx = hmac_ctx(parent);
0163 
0164     hash = crypto_spawn_shash(spawn);
0165     if (IS_ERR(hash))
0166         return PTR_ERR(hash);
0167 
0168     parent->descsize = sizeof(struct shash_desc) +
0169                crypto_shash_descsize(hash);
0170 
0171     ctx->hash = hash;
0172     return 0;
0173 }
0174 
0175 static void hmac_exit_tfm(struct crypto_tfm *tfm)
0176 {
0177     struct hmac_ctx *ctx = hmac_ctx(__crypto_shash_cast(tfm));
0178     crypto_free_shash(ctx->hash);
0179 }
0180 
0181 static int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
0182 {
0183     struct shash_instance *inst;
0184     struct crypto_alg *alg;
0185     struct shash_alg *salg;
0186     int err;
0187     int ds;
0188     int ss;
0189 
0190     err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
0191     if (err)
0192         return err;
0193 
0194     salg = shash_attr_alg(tb[1], 0, 0);
0195     if (IS_ERR(salg))
0196         return PTR_ERR(salg);
0197 
0198     err = -EINVAL;
0199     ds = salg->digestsize;
0200     ss = salg->statesize;
0201     alg = &salg->base;
0202     if (ds > alg->cra_blocksize ||
0203         ss < alg->cra_blocksize)
0204         goto out_put_alg;
0205 
0206     inst = shash_alloc_instance("hmac", alg);
0207     err = PTR_ERR(inst);
0208     if (IS_ERR(inst))
0209         goto out_put_alg;
0210 
0211     err = crypto_init_shash_spawn(shash_instance_ctx(inst), salg,
0212                       shash_crypto_instance(inst));
0213     if (err)
0214         goto out_free_inst;
0215 
0216     inst->alg.base.cra_priority = alg->cra_priority;
0217     inst->alg.base.cra_blocksize = alg->cra_blocksize;
0218     inst->alg.base.cra_alignmask = alg->cra_alignmask;
0219 
0220     ss = ALIGN(ss, alg->cra_alignmask + 1);
0221     inst->alg.digestsize = ds;
0222     inst->alg.statesize = ss;
0223 
0224     inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) +
0225                      ALIGN(ss * 2, crypto_tfm_ctx_alignment());
0226 
0227     inst->alg.base.cra_init = hmac_init_tfm;
0228     inst->alg.base.cra_exit = hmac_exit_tfm;
0229 
0230     inst->alg.init = hmac_init;
0231     inst->alg.update = hmac_update;
0232     inst->alg.final = hmac_final;
0233     inst->alg.finup = hmac_finup;
0234     inst->alg.export = hmac_export;
0235     inst->alg.import = hmac_import;
0236     inst->alg.setkey = hmac_setkey;
0237 
0238     err = shash_register_instance(tmpl, inst);
0239     if (err) {
0240 out_free_inst:
0241         shash_free_instance(shash_crypto_instance(inst));
0242     }
0243 
0244 out_put_alg:
0245     crypto_mod_put(alg);
0246     return err;
0247 }
0248 
0249 static struct crypto_template hmac_tmpl = {
0250     .name = "hmac",
0251     .create = hmac_create,
0252     .free = shash_free_instance,
0253     .module = THIS_MODULE,
0254 };
0255 
0256 static int __init hmac_module_init(void)
0257 {
0258     return crypto_register_template(&hmac_tmpl);
0259 }
0260 
0261 static void __exit hmac_module_exit(void)
0262 {
0263     crypto_unregister_template(&hmac_tmpl);
0264 }
0265 
0266 module_init(hmac_module_init);
0267 module_exit(hmac_module_exit);
0268 
0269 MODULE_LICENSE("GPL");
0270 MODULE_DESCRIPTION("HMAC hash algorithm");
0271 MODULE_ALIAS_CRYPTO("hmac");