0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <crypto/internal/cipher.h>
0015 #include <crypto/internal/hash.h>
0016 #include <linux/err.h>
0017 #include <linux/kernel.h>
0018 #include <linux/module.h>
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 struct cmac_tfm_ctx {
0030 struct crypto_cipher *child;
0031 u8 ctx[];
0032 };
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 struct cmac_desc_ctx {
0046 unsigned int len;
0047 u8 ctx[];
0048 };
0049
0050 static int crypto_cmac_digest_setkey(struct crypto_shash *parent,
0051 const u8 *inkey, unsigned int keylen)
0052 {
0053 unsigned long alignmask = crypto_shash_alignmask(parent);
0054 struct cmac_tfm_ctx *ctx = crypto_shash_ctx(parent);
0055 unsigned int bs = crypto_shash_blocksize(parent);
0056 __be64 *consts = PTR_ALIGN((void *)ctx->ctx,
0057 (alignmask | (__alignof__(__be64) - 1)) + 1);
0058 u64 _const[2];
0059 int i, err = 0;
0060 u8 msb_mask, gfmask;
0061
0062 err = crypto_cipher_setkey(ctx->child, inkey, keylen);
0063 if (err)
0064 return err;
0065
0066
0067 memset(consts, 0, bs);
0068 crypto_cipher_encrypt_one(ctx->child, (u8 *)consts, (u8 *)consts);
0069
0070 switch (bs) {
0071 case 16:
0072 gfmask = 0x87;
0073 _const[0] = be64_to_cpu(consts[1]);
0074 _const[1] = be64_to_cpu(consts[0]);
0075
0076
0077 for (i = 0; i < 4; i += 2) {
0078 msb_mask = ((s64)_const[1] >> 63) & gfmask;
0079 _const[1] = (_const[1] << 1) | (_const[0] >> 63);
0080 _const[0] = (_const[0] << 1) ^ msb_mask;
0081
0082 consts[i + 0] = cpu_to_be64(_const[1]);
0083 consts[i + 1] = cpu_to_be64(_const[0]);
0084 }
0085
0086 break;
0087 case 8:
0088 gfmask = 0x1B;
0089 _const[0] = be64_to_cpu(consts[0]);
0090
0091
0092 for (i = 0; i < 2; i++) {
0093 msb_mask = ((s64)_const[0] >> 63) & gfmask;
0094 _const[0] = (_const[0] << 1) ^ msb_mask;
0095
0096 consts[i] = cpu_to_be64(_const[0]);
0097 }
0098
0099 break;
0100 }
0101
0102 return 0;
0103 }
0104
0105 static int crypto_cmac_digest_init(struct shash_desc *pdesc)
0106 {
0107 unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
0108 struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
0109 int bs = crypto_shash_blocksize(pdesc->tfm);
0110 u8 *prev = PTR_ALIGN((void *)ctx->ctx, alignmask + 1) + bs;
0111
0112 ctx->len = 0;
0113 memset(prev, 0, bs);
0114
0115 return 0;
0116 }
0117
0118 static int crypto_cmac_digest_update(struct shash_desc *pdesc, const u8 *p,
0119 unsigned int len)
0120 {
0121 struct crypto_shash *parent = pdesc->tfm;
0122 unsigned long alignmask = crypto_shash_alignmask(parent);
0123 struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
0124 struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
0125 struct crypto_cipher *tfm = tctx->child;
0126 int bs = crypto_shash_blocksize(parent);
0127 u8 *odds = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
0128 u8 *prev = odds + bs;
0129
0130
0131 if ((ctx->len + len) <= bs) {
0132 memcpy(odds + ctx->len, p, len);
0133 ctx->len += len;
0134 return 0;
0135 }
0136
0137
0138 memcpy(odds + ctx->len, p, bs - ctx->len);
0139 len -= bs - ctx->len;
0140 p += bs - ctx->len;
0141
0142 crypto_xor(prev, odds, bs);
0143 crypto_cipher_encrypt_one(tfm, prev, prev);
0144
0145
0146 ctx->len = 0;
0147
0148
0149 while (len > bs) {
0150 crypto_xor(prev, p, bs);
0151 crypto_cipher_encrypt_one(tfm, prev, prev);
0152 p += bs;
0153 len -= bs;
0154 }
0155
0156
0157 if (len) {
0158 memcpy(odds, p, len);
0159 ctx->len = len;
0160 }
0161
0162 return 0;
0163 }
0164
0165 static int crypto_cmac_digest_final(struct shash_desc *pdesc, u8 *out)
0166 {
0167 struct crypto_shash *parent = pdesc->tfm;
0168 unsigned long alignmask = crypto_shash_alignmask(parent);
0169 struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
0170 struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
0171 struct crypto_cipher *tfm = tctx->child;
0172 int bs = crypto_shash_blocksize(parent);
0173 u8 *consts = PTR_ALIGN((void *)tctx->ctx,
0174 (alignmask | (__alignof__(__be64) - 1)) + 1);
0175 u8 *odds = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
0176 u8 *prev = odds + bs;
0177 unsigned int offset = 0;
0178
0179 if (ctx->len != bs) {
0180 unsigned int rlen;
0181 u8 *p = odds + ctx->len;
0182
0183 *p = 0x80;
0184 p++;
0185
0186 rlen = bs - ctx->len - 1;
0187 if (rlen)
0188 memset(p, 0, rlen);
0189
0190 offset += bs;
0191 }
0192
0193 crypto_xor(prev, odds, bs);
0194 crypto_xor(prev, consts + offset, bs);
0195
0196 crypto_cipher_encrypt_one(tfm, out, prev);
0197
0198 return 0;
0199 }
0200
0201 static int cmac_init_tfm(struct crypto_tfm *tfm)
0202 {
0203 struct crypto_cipher *cipher;
0204 struct crypto_instance *inst = (void *)tfm->__crt_alg;
0205 struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
0206 struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
0207
0208 cipher = crypto_spawn_cipher(spawn);
0209 if (IS_ERR(cipher))
0210 return PTR_ERR(cipher);
0211
0212 ctx->child = cipher;
0213
0214 return 0;
0215 };
0216
0217 static void cmac_exit_tfm(struct crypto_tfm *tfm)
0218 {
0219 struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
0220 crypto_free_cipher(ctx->child);
0221 }
0222
0223 static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb)
0224 {
0225 struct shash_instance *inst;
0226 struct crypto_cipher_spawn *spawn;
0227 struct crypto_alg *alg;
0228 unsigned long alignmask;
0229 u32 mask;
0230 int err;
0231
0232 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
0233 if (err)
0234 return err;
0235
0236 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
0237 if (!inst)
0238 return -ENOMEM;
0239 spawn = shash_instance_ctx(inst);
0240
0241 err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
0242 crypto_attr_alg_name(tb[1]), 0, mask);
0243 if (err)
0244 goto err_free_inst;
0245 alg = crypto_spawn_cipher_alg(spawn);
0246
0247 switch (alg->cra_blocksize) {
0248 case 16:
0249 case 8:
0250 break;
0251 default:
0252 err = -EINVAL;
0253 goto err_free_inst;
0254 }
0255
0256 err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
0257 if (err)
0258 goto err_free_inst;
0259
0260 alignmask = alg->cra_alignmask;
0261 inst->alg.base.cra_alignmask = alignmask;
0262 inst->alg.base.cra_priority = alg->cra_priority;
0263 inst->alg.base.cra_blocksize = alg->cra_blocksize;
0264
0265 inst->alg.digestsize = alg->cra_blocksize;
0266 inst->alg.descsize =
0267 ALIGN(sizeof(struct cmac_desc_ctx), crypto_tfm_ctx_alignment())
0268 + (alignmask & ~(crypto_tfm_ctx_alignment() - 1))
0269 + alg->cra_blocksize * 2;
0270
0271 inst->alg.base.cra_ctxsize =
0272 ALIGN(sizeof(struct cmac_tfm_ctx), crypto_tfm_ctx_alignment())
0273 + ((alignmask | (__alignof__(__be64) - 1)) &
0274 ~(crypto_tfm_ctx_alignment() - 1))
0275 + alg->cra_blocksize * 2;
0276
0277 inst->alg.base.cra_init = cmac_init_tfm;
0278 inst->alg.base.cra_exit = cmac_exit_tfm;
0279
0280 inst->alg.init = crypto_cmac_digest_init;
0281 inst->alg.update = crypto_cmac_digest_update;
0282 inst->alg.final = crypto_cmac_digest_final;
0283 inst->alg.setkey = crypto_cmac_digest_setkey;
0284
0285 inst->free = shash_free_singlespawn_instance;
0286
0287 err = shash_register_instance(tmpl, inst);
0288 if (err) {
0289 err_free_inst:
0290 shash_free_singlespawn_instance(inst);
0291 }
0292 return err;
0293 }
0294
0295 static struct crypto_template crypto_cmac_tmpl = {
0296 .name = "cmac",
0297 .create = cmac_create,
0298 .module = THIS_MODULE,
0299 };
0300
0301 static int __init crypto_cmac_module_init(void)
0302 {
0303 return crypto_register_template(&crypto_cmac_tmpl);
0304 }
0305
0306 static void __exit crypto_cmac_module_exit(void)
0307 {
0308 crypto_unregister_template(&crypto_cmac_tmpl);
0309 }
0310
0311 subsys_initcall(crypto_cmac_module_init);
0312 module_exit(crypto_cmac_module_exit);
0313
0314 MODULE_LICENSE("GPL");
0315 MODULE_DESCRIPTION("CMAC keyed hash algorithm");
0316 MODULE_ALIAS_CRYPTO("cmac");
0317 MODULE_IMPORT_NS(CRYPTO_INTERNAL);