Back to home page

LXR

 
 

    


0001 /*
0002  * CMAC: Cipher Block Mode for Authentication
0003  *
0004  * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
0005  *
0006  * Based on work by:
0007  *  Copyright © 2013 Tom St Denis <tstdenis@elliptictech.com>
0008  * Based on crypto/xcbc.c:
0009  *  Copyright © 2006 USAGI/WIDE Project,
0010  *   Author: Kazunori Miyazawa <miyazawa@linux-ipv6.org>
0011  *
0012  * This program is free software; you can redistribute it and/or modify
0013  * it under the terms of the GNU General Public License as published by
0014  * the Free Software Foundation; either version 2 of the License, or
0015  * (at your option) any later version.
0016  *
0017  */
0018 
0019 #include <crypto/internal/hash.h>
0020 #include <linux/err.h>
0021 #include <linux/kernel.h>
0022 #include <linux/module.h>
0023 
0024 /*
0025  * +------------------------
0026  * | <parent tfm>
0027  * +------------------------
0028  * | cmac_tfm_ctx
0029  * +------------------------
0030  * | consts (block size * 2)
0031  * +------------------------
0032  */
0033 struct cmac_tfm_ctx {
0034     struct crypto_cipher *child;
0035     u8 ctx[];
0036 };
0037 
0038 /*
0039  * +------------------------
0040  * | <shash desc>
0041  * +------------------------
0042  * | cmac_desc_ctx
0043  * +------------------------
0044  * | odds (block size)
0045  * +------------------------
0046  * | prev (block size)
0047  * +------------------------
0048  */
0049 struct cmac_desc_ctx {
0050     unsigned int len;
0051     u8 ctx[];
0052 };
0053 
0054 static int crypto_cmac_digest_setkey(struct crypto_shash *parent,
0055                      const u8 *inkey, unsigned int keylen)
0056 {
0057     unsigned long alignmask = crypto_shash_alignmask(parent);
0058     struct cmac_tfm_ctx *ctx = crypto_shash_ctx(parent);
0059     unsigned int bs = crypto_shash_blocksize(parent);
0060     __be64 *consts = PTR_ALIGN((void *)ctx->ctx,
0061                    (alignmask | (__alignof__(__be64) - 1)) + 1);
0062     u64 _const[2];
0063     int i, err = 0;
0064     u8 msb_mask, gfmask;
0065 
0066     err = crypto_cipher_setkey(ctx->child, inkey, keylen);
0067     if (err)
0068         return err;
0069 
0070     /* encrypt the zero block */
0071     memset(consts, 0, bs);
0072     crypto_cipher_encrypt_one(ctx->child, (u8 *)consts, (u8 *)consts);
0073 
0074     switch (bs) {
0075     case 16:
0076         gfmask = 0x87;
0077         _const[0] = be64_to_cpu(consts[1]);
0078         _const[1] = be64_to_cpu(consts[0]);
0079 
0080         /* gf(2^128) multiply zero-ciphertext with u and u^2 */
0081         for (i = 0; i < 4; i += 2) {
0082             msb_mask = ((s64)_const[1] >> 63) & gfmask;
0083             _const[1] = (_const[1] << 1) | (_const[0] >> 63);
0084             _const[0] = (_const[0] << 1) ^ msb_mask;
0085 
0086             consts[i + 0] = cpu_to_be64(_const[1]);
0087             consts[i + 1] = cpu_to_be64(_const[0]);
0088         }
0089 
0090         break;
0091     case 8:
0092         gfmask = 0x1B;
0093         _const[0] = be64_to_cpu(consts[0]);
0094 
0095         /* gf(2^64) multiply zero-ciphertext with u and u^2 */
0096         for (i = 0; i < 2; i++) {
0097             msb_mask = ((s64)_const[0] >> 63) & gfmask;
0098             _const[0] = (_const[0] << 1) ^ msb_mask;
0099 
0100             consts[i] = cpu_to_be64(_const[0]);
0101         }
0102 
0103         break;
0104     }
0105 
0106     return 0;
0107 }
0108 
0109 static int crypto_cmac_digest_init(struct shash_desc *pdesc)
0110 {
0111     unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
0112     struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
0113     int bs = crypto_shash_blocksize(pdesc->tfm);
0114     u8 *prev = PTR_ALIGN((void *)ctx->ctx, alignmask + 1) + bs;
0115 
0116     ctx->len = 0;
0117     memset(prev, 0, bs);
0118 
0119     return 0;
0120 }
0121 
0122 static int crypto_cmac_digest_update(struct shash_desc *pdesc, const u8 *p,
0123                      unsigned int len)
0124 {
0125     struct crypto_shash *parent = pdesc->tfm;
0126     unsigned long alignmask = crypto_shash_alignmask(parent);
0127     struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
0128     struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
0129     struct crypto_cipher *tfm = tctx->child;
0130     int bs = crypto_shash_blocksize(parent);
0131     u8 *odds = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
0132     u8 *prev = odds + bs;
0133 
0134     /* checking the data can fill the block */
0135     if ((ctx->len + len) <= bs) {
0136         memcpy(odds + ctx->len, p, len);
0137         ctx->len += len;
0138         return 0;
0139     }
0140 
0141     /* filling odds with new data and encrypting it */
0142     memcpy(odds + ctx->len, p, bs - ctx->len);
0143     len -= bs - ctx->len;
0144     p += bs - ctx->len;
0145 
0146     crypto_xor(prev, odds, bs);
0147     crypto_cipher_encrypt_one(tfm, prev, prev);
0148 
0149     /* clearing the length */
0150     ctx->len = 0;
0151 
0152     /* encrypting the rest of data */
0153     while (len > bs) {
0154         crypto_xor(prev, p, bs);
0155         crypto_cipher_encrypt_one(tfm, prev, prev);
0156         p += bs;
0157         len -= bs;
0158     }
0159 
0160     /* keeping the surplus of blocksize */
0161     if (len) {
0162         memcpy(odds, p, len);
0163         ctx->len = len;
0164     }
0165 
0166     return 0;
0167 }
0168 
0169 static int crypto_cmac_digest_final(struct shash_desc *pdesc, u8 *out)
0170 {
0171     struct crypto_shash *parent = pdesc->tfm;
0172     unsigned long alignmask = crypto_shash_alignmask(parent);
0173     struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
0174     struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
0175     struct crypto_cipher *tfm = tctx->child;
0176     int bs = crypto_shash_blocksize(parent);
0177     u8 *consts = PTR_ALIGN((void *)tctx->ctx,
0178                    (alignmask | (__alignof__(__be64) - 1)) + 1);
0179     u8 *odds = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
0180     u8 *prev = odds + bs;
0181     unsigned int offset = 0;
0182 
0183     if (ctx->len != bs) {
0184         unsigned int rlen;
0185         u8 *p = odds + ctx->len;
0186 
0187         *p = 0x80;
0188         p++;
0189 
0190         rlen = bs - ctx->len - 1;
0191         if (rlen)
0192             memset(p, 0, rlen);
0193 
0194         offset += bs;
0195     }
0196 
0197     crypto_xor(prev, odds, bs);
0198     crypto_xor(prev, consts + offset, bs);
0199 
0200     crypto_cipher_encrypt_one(tfm, out, prev);
0201 
0202     return 0;
0203 }
0204 
0205 static int cmac_init_tfm(struct crypto_tfm *tfm)
0206 {
0207     struct crypto_cipher *cipher;
0208     struct crypto_instance *inst = (void *)tfm->__crt_alg;
0209     struct crypto_spawn *spawn = crypto_instance_ctx(inst);
0210     struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
0211 
0212     cipher = crypto_spawn_cipher(spawn);
0213     if (IS_ERR(cipher))
0214         return PTR_ERR(cipher);
0215 
0216     ctx->child = cipher;
0217 
0218     return 0;
0219 };
0220 
0221 static void cmac_exit_tfm(struct crypto_tfm *tfm)
0222 {
0223     struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
0224     crypto_free_cipher(ctx->child);
0225 }
0226 
0227 static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb)
0228 {
0229     struct shash_instance *inst;
0230     struct crypto_alg *alg;
0231     unsigned long alignmask;
0232     int err;
0233 
0234     err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
0235     if (err)
0236         return err;
0237 
0238     alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
0239                   CRYPTO_ALG_TYPE_MASK);
0240     if (IS_ERR(alg))
0241         return PTR_ERR(alg);
0242 
0243     switch (alg->cra_blocksize) {
0244     case 16:
0245     case 8:
0246         break;
0247     default:
0248         err = -EINVAL;
0249         goto out_put_alg;
0250     }
0251 
0252     inst = shash_alloc_instance("cmac", alg);
0253     err = PTR_ERR(inst);
0254     if (IS_ERR(inst))
0255         goto out_put_alg;
0256 
0257     err = crypto_init_spawn(shash_instance_ctx(inst), alg,
0258                 shash_crypto_instance(inst),
0259                 CRYPTO_ALG_TYPE_MASK);
0260     if (err)
0261         goto out_free_inst;
0262 
0263     /* We access the data as u32s when xoring. */
0264     alignmask = alg->cra_alignmask | (__alignof__(u32) - 1);
0265     inst->alg.base.cra_alignmask = alignmask;
0266     inst->alg.base.cra_priority = alg->cra_priority;
0267     inst->alg.base.cra_blocksize = alg->cra_blocksize;
0268 
0269     inst->alg.digestsize = alg->cra_blocksize;
0270     inst->alg.descsize =
0271         ALIGN(sizeof(struct cmac_desc_ctx), crypto_tfm_ctx_alignment())
0272         + (alignmask & ~(crypto_tfm_ctx_alignment() - 1))
0273         + alg->cra_blocksize * 2;
0274 
0275     inst->alg.base.cra_ctxsize =
0276         ALIGN(sizeof(struct cmac_tfm_ctx), crypto_tfm_ctx_alignment())
0277         + ((alignmask | (__alignof__(__be64) - 1)) &
0278            ~(crypto_tfm_ctx_alignment() - 1))
0279         + alg->cra_blocksize * 2;
0280 
0281     inst->alg.base.cra_init = cmac_init_tfm;
0282     inst->alg.base.cra_exit = cmac_exit_tfm;
0283 
0284     inst->alg.init = crypto_cmac_digest_init;
0285     inst->alg.update = crypto_cmac_digest_update;
0286     inst->alg.final = crypto_cmac_digest_final;
0287     inst->alg.setkey = crypto_cmac_digest_setkey;
0288 
0289     err = shash_register_instance(tmpl, inst);
0290     if (err) {
0291 out_free_inst:
0292         shash_free_instance(shash_crypto_instance(inst));
0293     }
0294 
0295 out_put_alg:
0296     crypto_mod_put(alg);
0297     return err;
0298 }
0299 
0300 static struct crypto_template crypto_cmac_tmpl = {
0301     .name = "cmac",
0302     .create = cmac_create,
0303     .free = shash_free_instance,
0304     .module = THIS_MODULE,
0305 };
0306 
0307 static int __init crypto_cmac_module_init(void)
0308 {
0309     return crypto_register_template(&crypto_cmac_tmpl);
0310 }
0311 
0312 static void __exit crypto_cmac_module_exit(void)
0313 {
0314     crypto_unregister_template(&crypto_cmac_tmpl);
0315 }
0316 
0317 module_init(crypto_cmac_module_init);
0318 module_exit(crypto_cmac_module_exit);
0319 
0320 MODULE_LICENSE("GPL");
0321 MODULE_DESCRIPTION("CMAC keyed hash algorithm");
0322 MODULE_ALIAS_CRYPTO("cmac");