blob: c055f57fab11aa927069f7610b03f99eb0e09d55 [file] [log] [blame]
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +10001/*
2 * Copyright (C)2006 USAGI/WIDE Project
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Martin Kepplinger1af39da2017-11-14 10:25:15 +010015 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100016 *
17 * Author:
18 * Kazunori Miyazawa <miyazawa@linux-ipv6.org>
19 */
20
Herbert Xu3106caa2009-07-12 12:48:32 +080021#include <crypto/internal/hash.h>
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100022#include <linux/err.h>
23#include <linux/kernel.h>
Paul Gortmaker4bb33cc2011-05-27 14:41:48 -040024#include <linux/module.h>
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100025
Adrian Bunk5b375382006-11-17 13:43:04 +110026static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
27 0x02020202, 0x02020202, 0x02020202, 0x02020202,
28 0x03030303, 0x03030303, 0x03030303, 0x03030303};
Herbert Xuac953012009-07-22 14:37:15 +080029
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100030/*
31 * +------------------------
32 * | <parent tfm>
33 * +------------------------
Herbert Xuac953012009-07-22 14:37:15 +080034 * | xcbc_tfm_ctx
35 * +------------------------
36 * | consts (block size * 2)
37 * +------------------------
38 */
39struct xcbc_tfm_ctx {
40 struct crypto_cipher *child;
41 u8 ctx[];
42};
43
44/*
45 * +------------------------
46 * | <shash desc>
47 * +------------------------
48 * | xcbc_desc_ctx
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100049 * +------------------------
50 * | odds (block size)
51 * +------------------------
52 * | prev (block size)
53 * +------------------------
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100054 */
Herbert Xuac953012009-07-22 14:37:15 +080055struct xcbc_desc_ctx {
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100056 unsigned int len;
Herbert Xuac953012009-07-22 14:37:15 +080057 u8 ctx[];
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100058};
59
Kees Cook3bdd23f2018-08-07 14:18:35 -070060#define XCBC_BLOCKSIZE 16
61
Herbert Xu3106caa2009-07-12 12:48:32 +080062static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100063 const u8 *inkey, unsigned int keylen)
64{
Herbert Xuac953012009-07-22 14:37:15 +080065 unsigned long alignmask = crypto_shash_alignmask(parent);
66 struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
Herbert Xuac953012009-07-22 14:37:15 +080067 u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
68 int err = 0;
Kees Cook3bdd23f2018-08-07 14:18:35 -070069 u8 key1[XCBC_BLOCKSIZE];
70 int bs = sizeof(key1);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100071
Herbert Xuac953012009-07-22 14:37:15 +080072 if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
73 return err;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100074
Herbert Xuac953012009-07-22 14:37:15 +080075 crypto_cipher_encrypt_one(ctx->child, consts, (u8 *)ks + bs);
76 crypto_cipher_encrypt_one(ctx->child, consts + bs, (u8 *)ks + bs * 2);
77 crypto_cipher_encrypt_one(ctx->child, key1, (u8 *)ks);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100078
Herbert Xuac953012009-07-22 14:37:15 +080079 return crypto_cipher_setkey(ctx->child, key1, bs);
80
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100081}
82
Herbert Xu3106caa2009-07-12 12:48:32 +080083static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100084{
Herbert Xuac953012009-07-22 14:37:15 +080085 unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
86 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
Herbert Xu3106caa2009-07-12 12:48:32 +080087 int bs = crypto_shash_blocksize(pdesc->tfm);
Herbert Xuac953012009-07-22 14:37:15 +080088 u8 *prev = PTR_ALIGN(&ctx->ctx[0], alignmask + 1) + bs;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100089
90 ctx->len = 0;
Herbert Xuac953012009-07-22 14:37:15 +080091 memset(prev, 0, bs);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100092
93 return 0;
94}
95
Herbert Xu3106caa2009-07-12 12:48:32 +080096static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
97 unsigned int len)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100098{
Herbert Xu3106caa2009-07-12 12:48:32 +080099 struct crypto_shash *parent = pdesc->tfm;
Herbert Xuac953012009-07-22 14:37:15 +0800100 unsigned long alignmask = crypto_shash_alignmask(parent);
101 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
102 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
103 struct crypto_cipher *tfm = tctx->child;
Herbert Xu3106caa2009-07-12 12:48:32 +0800104 int bs = crypto_shash_blocksize(parent);
Herbert Xuac953012009-07-22 14:37:15 +0800105 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
106 u8 *prev = odds + bs;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000107
Herbert Xu3106caa2009-07-12 12:48:32 +0800108 /* checking the data can fill the block */
109 if ((ctx->len + len) <= bs) {
Herbert Xuac953012009-07-22 14:37:15 +0800110 memcpy(odds + ctx->len, p, len);
Herbert Xu3106caa2009-07-12 12:48:32 +0800111 ctx->len += len;
112 return 0;
113 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000114
Herbert Xu3106caa2009-07-12 12:48:32 +0800115 /* filling odds with new data and encrypting it */
Herbert Xuac953012009-07-22 14:37:15 +0800116 memcpy(odds + ctx->len, p, bs - ctx->len);
Herbert Xu3106caa2009-07-12 12:48:32 +0800117 len -= bs - ctx->len;
118 p += bs - ctx->len;
Joy Latten2f40a172008-03-06 19:28:44 +0800119
Herbert Xuac953012009-07-22 14:37:15 +0800120 crypto_xor(prev, odds, bs);
121 crypto_cipher_encrypt_one(tfm, prev, prev);
Joy Latten2f40a172008-03-06 19:28:44 +0800122
Herbert Xu3106caa2009-07-12 12:48:32 +0800123 /* clearing the length */
124 ctx->len = 0;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000125
Herbert Xu3106caa2009-07-12 12:48:32 +0800126 /* encrypting the rest of data */
127 while (len > bs) {
Herbert Xuac953012009-07-22 14:37:15 +0800128 crypto_xor(prev, p, bs);
129 crypto_cipher_encrypt_one(tfm, prev, prev);
Herbert Xu3106caa2009-07-12 12:48:32 +0800130 p += bs;
131 len -= bs;
132 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000133
Herbert Xu3106caa2009-07-12 12:48:32 +0800134 /* keeping the surplus of blocksize */
135 if (len) {
Herbert Xuac953012009-07-22 14:37:15 +0800136 memcpy(odds, p, len);
Herbert Xu3106caa2009-07-12 12:48:32 +0800137 ctx->len = len;
Joy Latten1edcf2e2008-04-02 14:36:09 +0800138 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000139
140 return 0;
141}
142
Herbert Xu3106caa2009-07-12 12:48:32 +0800143static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
Herbert Xufb469842006-12-10 10:45:28 +1100144{
Herbert Xu3106caa2009-07-12 12:48:32 +0800145 struct crypto_shash *parent = pdesc->tfm;
Herbert Xuac953012009-07-22 14:37:15 +0800146 unsigned long alignmask = crypto_shash_alignmask(parent);
147 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
148 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
149 struct crypto_cipher *tfm = tctx->child;
Herbert Xu3106caa2009-07-12 12:48:32 +0800150 int bs = crypto_shash_blocksize(parent);
Herbert Xuac953012009-07-22 14:37:15 +0800151 u8 *consts = PTR_ALIGN(&tctx->ctx[0], alignmask + 1);
152 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
153 u8 *prev = odds + bs;
154 unsigned int offset = 0;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000155
Herbert Xuac953012009-07-22 14:37:15 +0800156 if (ctx->len != bs) {
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000157 unsigned int rlen;
Herbert Xuac953012009-07-22 14:37:15 +0800158 u8 *p = odds + ctx->len;
159
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000160 *p = 0x80;
161 p++;
162
163 rlen = bs - ctx->len -1;
164 if (rlen)
165 memset(p, 0, rlen);
166
Herbert Xuac953012009-07-22 14:37:15 +0800167 offset += bs;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000168 }
169
Herbert Xuac953012009-07-22 14:37:15 +0800170 crypto_xor(prev, odds, bs);
171 crypto_xor(prev, consts + offset, bs);
172
173 crypto_cipher_encrypt_one(tfm, out, prev);
174
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000175 return 0;
176}
177
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000178static int xcbc_init_tfm(struct crypto_tfm *tfm)
179{
Herbert Xu2e306ee2006-12-17 10:05:58 +1100180 struct crypto_cipher *cipher;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000181 struct crypto_instance *inst = (void *)tfm->__crt_alg;
182 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
Herbert Xuac953012009-07-22 14:37:15 +0800183 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000184
Herbert Xu2e306ee2006-12-17 10:05:58 +1100185 cipher = crypto_spawn_cipher(spawn);
186 if (IS_ERR(cipher))
187 return PTR_ERR(cipher);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000188
Herbert Xu2e306ee2006-12-17 10:05:58 +1100189 ctx->child = cipher;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000190
191 return 0;
192};
193
194static void xcbc_exit_tfm(struct crypto_tfm *tfm)
195{
Herbert Xuac953012009-07-22 14:37:15 +0800196 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000197 crypto_free_cipher(ctx->child);
198}
199
Herbert Xu3106caa2009-07-12 12:48:32 +0800200static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000201{
Herbert Xu3106caa2009-07-12 12:48:32 +0800202 struct shash_instance *inst;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000203 struct crypto_alg *alg;
Steffen Klassert36f87a42009-08-20 17:58:04 +1000204 unsigned long alignmask;
Herbert Xuebc610e2007-01-01 18:37:02 +1100205 int err;
206
Herbert Xu3106caa2009-07-12 12:48:32 +0800207 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
Herbert Xuebc610e2007-01-01 18:37:02 +1100208 if (err)
Herbert Xu3106caa2009-07-12 12:48:32 +0800209 return err;
Herbert Xuebc610e2007-01-01 18:37:02 +1100210
211 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
212 CRYPTO_ALG_TYPE_MASK);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000213 if (IS_ERR(alg))
Herbert Xu3106caa2009-07-12 12:48:32 +0800214 return PTR_ERR(alg);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000215
216 switch(alg->cra_blocksize) {
Kees Cook3bdd23f2018-08-07 14:18:35 -0700217 case XCBC_BLOCKSIZE:
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000218 break;
219 default:
Herbert Xu1b878872008-01-01 15:44:50 +1100220 goto out_put_alg;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000221 }
222
Herbert Xu3106caa2009-07-12 12:48:32 +0800223 inst = shash_alloc_instance("xcbc", alg);
Herbert Xub5ebd442009-07-15 16:53:33 +0800224 err = PTR_ERR(inst);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000225 if (IS_ERR(inst))
226 goto out_put_alg;
227
Herbert Xu3106caa2009-07-12 12:48:32 +0800228 err = crypto_init_spawn(shash_instance_ctx(inst), alg,
229 shash_crypto_instance(inst),
230 CRYPTO_ALG_TYPE_MASK);
231 if (err)
232 goto out_free_inst;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000233
Steffen Klassert36f87a42009-08-20 17:58:04 +1000234 alignmask = alg->cra_alignmask | 3;
235 inst->alg.base.cra_alignmask = alignmask;
Herbert Xu3106caa2009-07-12 12:48:32 +0800236 inst->alg.base.cra_priority = alg->cra_priority;
237 inst->alg.base.cra_blocksize = alg->cra_blocksize;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000238
Herbert Xu3106caa2009-07-12 12:48:32 +0800239 inst->alg.digestsize = alg->cra_blocksize;
Herbert Xuac953012009-07-22 14:37:15 +0800240 inst->alg.descsize = ALIGN(sizeof(struct xcbc_desc_ctx),
241 crypto_tfm_ctx_alignment()) +
Steffen Klassert36f87a42009-08-20 17:58:04 +1000242 (alignmask &
Herbert Xuac953012009-07-22 14:37:15 +0800243 ~(crypto_tfm_ctx_alignment() - 1)) +
244 alg->cra_blocksize * 2;
245
246 inst->alg.base.cra_ctxsize = ALIGN(sizeof(struct xcbc_tfm_ctx),
Steffen Klassert36f87a42009-08-20 17:58:04 +1000247 alignmask + 1) +
Herbert Xuac953012009-07-22 14:37:15 +0800248 alg->cra_blocksize * 2;
Herbert Xu3106caa2009-07-12 12:48:32 +0800249 inst->alg.base.cra_init = xcbc_init_tfm;
250 inst->alg.base.cra_exit = xcbc_exit_tfm;
251
252 inst->alg.init = crypto_xcbc_digest_init;
253 inst->alg.update = crypto_xcbc_digest_update;
254 inst->alg.final = crypto_xcbc_digest_final;
255 inst->alg.setkey = crypto_xcbc_digest_setkey;
256
257 err = shash_register_instance(tmpl, inst);
258 if (err) {
259out_free_inst:
260 shash_free_instance(shash_crypto_instance(inst));
261 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000262
263out_put_alg:
264 crypto_mod_put(alg);
Herbert Xu3106caa2009-07-12 12:48:32 +0800265 return err;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000266}
267
268static struct crypto_template crypto_xcbc_tmpl = {
269 .name = "xcbc",
Herbert Xu3106caa2009-07-12 12:48:32 +0800270 .create = xcbc_create,
271 .free = shash_free_instance,
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000272 .module = THIS_MODULE,
273};
274
275static int __init crypto_xcbc_module_init(void)
276{
277 return crypto_register_template(&crypto_xcbc_tmpl);
278}
279
280static void __exit crypto_xcbc_module_exit(void)
281{
282 crypto_unregister_template(&crypto_xcbc_tmpl);
283}
284
285module_init(crypto_xcbc_module_init);
286module_exit(crypto_xcbc_module_exit);
287
288MODULE_LICENSE("GPL");
289MODULE_DESCRIPTION("XCBC keyed hash algorithm");
Kees Cook4943ba12014-11-24 16:32:38 -0800290MODULE_ALIAS_CRYPTO("xcbc");