blob: 0bb26e8f6f5a080913d9c0d452938433b07682f3 [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +10002/*
3 * Copyright (C)2006 USAGI/WIDE Project
4 *
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +10005 * Author:
6 * Kazunori Miyazawa <miyazawa@linux-ipv6.org>
7 */
8
Herbert Xu3106caa2009-07-12 12:48:32 +08009#include <crypto/internal/hash.h>
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100010#include <linux/err.h>
11#include <linux/kernel.h>
Paul Gortmaker4bb33cc2011-05-27 14:41:48 -040012#include <linux/module.h>
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100013
Adrian Bunk5b375382006-11-17 13:43:04 +110014static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
15 0x02020202, 0x02020202, 0x02020202, 0x02020202,
16 0x03030303, 0x03030303, 0x03030303, 0x03030303};
Herbert Xuac953012009-07-22 14:37:15 +080017
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100018/*
19 * +------------------------
20 * | <parent tfm>
21 * +------------------------
Herbert Xuac953012009-07-22 14:37:15 +080022 * | xcbc_tfm_ctx
23 * +------------------------
24 * | consts (block size * 2)
25 * +------------------------
26 */
27struct xcbc_tfm_ctx {
28 struct crypto_cipher *child;
29 u8 ctx[];
30};
31
32/*
33 * +------------------------
34 * | <shash desc>
35 * +------------------------
36 * | xcbc_desc_ctx
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100037 * +------------------------
38 * | odds (block size)
39 * +------------------------
40 * | prev (block size)
41 * +------------------------
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100042 */
Herbert Xuac953012009-07-22 14:37:15 +080043struct xcbc_desc_ctx {
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100044 unsigned int len;
Herbert Xuac953012009-07-22 14:37:15 +080045 u8 ctx[];
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100046};
47
Kees Cook3bdd23f2018-08-07 14:18:35 -070048#define XCBC_BLOCKSIZE 16
49
Herbert Xu3106caa2009-07-12 12:48:32 +080050static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100051 const u8 *inkey, unsigned int keylen)
52{
Herbert Xuac953012009-07-22 14:37:15 +080053 unsigned long alignmask = crypto_shash_alignmask(parent);
54 struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
Herbert Xuac953012009-07-22 14:37:15 +080055 u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
56 int err = 0;
Kees Cook3bdd23f2018-08-07 14:18:35 -070057 u8 key1[XCBC_BLOCKSIZE];
58 int bs = sizeof(key1);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100059
Herbert Xuac953012009-07-22 14:37:15 +080060 if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
61 return err;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100062
Herbert Xuac953012009-07-22 14:37:15 +080063 crypto_cipher_encrypt_one(ctx->child, consts, (u8 *)ks + bs);
64 crypto_cipher_encrypt_one(ctx->child, consts + bs, (u8 *)ks + bs * 2);
65 crypto_cipher_encrypt_one(ctx->child, key1, (u8 *)ks);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100066
Herbert Xuac953012009-07-22 14:37:15 +080067 return crypto_cipher_setkey(ctx->child, key1, bs);
68
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100069}
70
Herbert Xu3106caa2009-07-12 12:48:32 +080071static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100072{
Herbert Xuac953012009-07-22 14:37:15 +080073 unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
74 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
Herbert Xu3106caa2009-07-12 12:48:32 +080075 int bs = crypto_shash_blocksize(pdesc->tfm);
Herbert Xuac953012009-07-22 14:37:15 +080076 u8 *prev = PTR_ALIGN(&ctx->ctx[0], alignmask + 1) + bs;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100077
78 ctx->len = 0;
Herbert Xuac953012009-07-22 14:37:15 +080079 memset(prev, 0, bs);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100080
81 return 0;
82}
83
Herbert Xu3106caa2009-07-12 12:48:32 +080084static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
85 unsigned int len)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100086{
Herbert Xu3106caa2009-07-12 12:48:32 +080087 struct crypto_shash *parent = pdesc->tfm;
Herbert Xuac953012009-07-22 14:37:15 +080088 unsigned long alignmask = crypto_shash_alignmask(parent);
89 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
90 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
91 struct crypto_cipher *tfm = tctx->child;
Herbert Xu3106caa2009-07-12 12:48:32 +080092 int bs = crypto_shash_blocksize(parent);
Herbert Xuac953012009-07-22 14:37:15 +080093 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
94 u8 *prev = odds + bs;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100095
Herbert Xu3106caa2009-07-12 12:48:32 +080096 /* checking the data can fill the block */
97 if ((ctx->len + len) <= bs) {
Herbert Xuac953012009-07-22 14:37:15 +080098 memcpy(odds + ctx->len, p, len);
Herbert Xu3106caa2009-07-12 12:48:32 +080099 ctx->len += len;
100 return 0;
101 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000102
Herbert Xu3106caa2009-07-12 12:48:32 +0800103 /* filling odds with new data and encrypting it */
Herbert Xuac953012009-07-22 14:37:15 +0800104 memcpy(odds + ctx->len, p, bs - ctx->len);
Herbert Xu3106caa2009-07-12 12:48:32 +0800105 len -= bs - ctx->len;
106 p += bs - ctx->len;
Joy Latten2f40a172008-03-06 19:28:44 +0800107
Herbert Xuac953012009-07-22 14:37:15 +0800108 crypto_xor(prev, odds, bs);
109 crypto_cipher_encrypt_one(tfm, prev, prev);
Joy Latten2f40a172008-03-06 19:28:44 +0800110
Herbert Xu3106caa2009-07-12 12:48:32 +0800111 /* clearing the length */
112 ctx->len = 0;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000113
Herbert Xu3106caa2009-07-12 12:48:32 +0800114 /* encrypting the rest of data */
115 while (len > bs) {
Herbert Xuac953012009-07-22 14:37:15 +0800116 crypto_xor(prev, p, bs);
117 crypto_cipher_encrypt_one(tfm, prev, prev);
Herbert Xu3106caa2009-07-12 12:48:32 +0800118 p += bs;
119 len -= bs;
120 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000121
Herbert Xu3106caa2009-07-12 12:48:32 +0800122 /* keeping the surplus of blocksize */
123 if (len) {
Herbert Xuac953012009-07-22 14:37:15 +0800124 memcpy(odds, p, len);
Herbert Xu3106caa2009-07-12 12:48:32 +0800125 ctx->len = len;
Joy Latten1edcf2e2008-04-02 14:36:09 +0800126 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000127
128 return 0;
129}
130
Herbert Xu3106caa2009-07-12 12:48:32 +0800131static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
Herbert Xufb469842006-12-10 10:45:28 +1100132{
Herbert Xu3106caa2009-07-12 12:48:32 +0800133 struct crypto_shash *parent = pdesc->tfm;
Herbert Xuac953012009-07-22 14:37:15 +0800134 unsigned long alignmask = crypto_shash_alignmask(parent);
135 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
136 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
137 struct crypto_cipher *tfm = tctx->child;
Herbert Xu3106caa2009-07-12 12:48:32 +0800138 int bs = crypto_shash_blocksize(parent);
Herbert Xuac953012009-07-22 14:37:15 +0800139 u8 *consts = PTR_ALIGN(&tctx->ctx[0], alignmask + 1);
140 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
141 u8 *prev = odds + bs;
142 unsigned int offset = 0;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000143
Herbert Xuac953012009-07-22 14:37:15 +0800144 if (ctx->len != bs) {
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000145 unsigned int rlen;
Herbert Xuac953012009-07-22 14:37:15 +0800146 u8 *p = odds + ctx->len;
147
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000148 *p = 0x80;
149 p++;
150
151 rlen = bs - ctx->len -1;
152 if (rlen)
153 memset(p, 0, rlen);
154
Herbert Xuac953012009-07-22 14:37:15 +0800155 offset += bs;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000156 }
157
Herbert Xuac953012009-07-22 14:37:15 +0800158 crypto_xor(prev, odds, bs);
159 crypto_xor(prev, consts + offset, bs);
160
161 crypto_cipher_encrypt_one(tfm, out, prev);
162
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000163 return 0;
164}
165
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000166static int xcbc_init_tfm(struct crypto_tfm *tfm)
167{
Herbert Xu2e306ee2006-12-17 10:05:58 +1100168 struct crypto_cipher *cipher;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000169 struct crypto_instance *inst = (void *)tfm->__crt_alg;
170 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
Herbert Xuac953012009-07-22 14:37:15 +0800171 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000172
Herbert Xu2e306ee2006-12-17 10:05:58 +1100173 cipher = crypto_spawn_cipher(spawn);
174 if (IS_ERR(cipher))
175 return PTR_ERR(cipher);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000176
Herbert Xu2e306ee2006-12-17 10:05:58 +1100177 ctx->child = cipher;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000178
179 return 0;
180};
181
182static void xcbc_exit_tfm(struct crypto_tfm *tfm)
183{
Herbert Xuac953012009-07-22 14:37:15 +0800184 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000185 crypto_free_cipher(ctx->child);
186}
187
Herbert Xu3106caa2009-07-12 12:48:32 +0800188static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000189{
Herbert Xu3106caa2009-07-12 12:48:32 +0800190 struct shash_instance *inst;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000191 struct crypto_alg *alg;
Steffen Klassert36f87a42009-08-20 17:58:04 +1000192 unsigned long alignmask;
Herbert Xuebc610e2007-01-01 18:37:02 +1100193 int err;
194
Herbert Xu3106caa2009-07-12 12:48:32 +0800195 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
Herbert Xuebc610e2007-01-01 18:37:02 +1100196 if (err)
Herbert Xu3106caa2009-07-12 12:48:32 +0800197 return err;
Herbert Xuebc610e2007-01-01 18:37:02 +1100198
199 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
200 CRYPTO_ALG_TYPE_MASK);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000201 if (IS_ERR(alg))
Herbert Xu3106caa2009-07-12 12:48:32 +0800202 return PTR_ERR(alg);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000203
204 switch(alg->cra_blocksize) {
Kees Cook3bdd23f2018-08-07 14:18:35 -0700205 case XCBC_BLOCKSIZE:
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000206 break;
207 default:
Herbert Xu1b878872008-01-01 15:44:50 +1100208 goto out_put_alg;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000209 }
210
Herbert Xu3106caa2009-07-12 12:48:32 +0800211 inst = shash_alloc_instance("xcbc", alg);
Herbert Xub5ebd442009-07-15 16:53:33 +0800212 err = PTR_ERR(inst);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000213 if (IS_ERR(inst))
214 goto out_put_alg;
215
Herbert Xu3106caa2009-07-12 12:48:32 +0800216 err = crypto_init_spawn(shash_instance_ctx(inst), alg,
217 shash_crypto_instance(inst),
218 CRYPTO_ALG_TYPE_MASK);
219 if (err)
220 goto out_free_inst;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000221
Steffen Klassert36f87a42009-08-20 17:58:04 +1000222 alignmask = alg->cra_alignmask | 3;
223 inst->alg.base.cra_alignmask = alignmask;
Herbert Xu3106caa2009-07-12 12:48:32 +0800224 inst->alg.base.cra_priority = alg->cra_priority;
225 inst->alg.base.cra_blocksize = alg->cra_blocksize;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000226
Herbert Xu3106caa2009-07-12 12:48:32 +0800227 inst->alg.digestsize = alg->cra_blocksize;
Herbert Xuac953012009-07-22 14:37:15 +0800228 inst->alg.descsize = ALIGN(sizeof(struct xcbc_desc_ctx),
229 crypto_tfm_ctx_alignment()) +
Steffen Klassert36f87a42009-08-20 17:58:04 +1000230 (alignmask &
Herbert Xuac953012009-07-22 14:37:15 +0800231 ~(crypto_tfm_ctx_alignment() - 1)) +
232 alg->cra_blocksize * 2;
233
234 inst->alg.base.cra_ctxsize = ALIGN(sizeof(struct xcbc_tfm_ctx),
Steffen Klassert36f87a42009-08-20 17:58:04 +1000235 alignmask + 1) +
Herbert Xuac953012009-07-22 14:37:15 +0800236 alg->cra_blocksize * 2;
Herbert Xu3106caa2009-07-12 12:48:32 +0800237 inst->alg.base.cra_init = xcbc_init_tfm;
238 inst->alg.base.cra_exit = xcbc_exit_tfm;
239
240 inst->alg.init = crypto_xcbc_digest_init;
241 inst->alg.update = crypto_xcbc_digest_update;
242 inst->alg.final = crypto_xcbc_digest_final;
243 inst->alg.setkey = crypto_xcbc_digest_setkey;
244
245 err = shash_register_instance(tmpl, inst);
246 if (err) {
247out_free_inst:
248 shash_free_instance(shash_crypto_instance(inst));
249 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000250
251out_put_alg:
252 crypto_mod_put(alg);
Herbert Xu3106caa2009-07-12 12:48:32 +0800253 return err;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000254}
255
256static struct crypto_template crypto_xcbc_tmpl = {
257 .name = "xcbc",
Herbert Xu3106caa2009-07-12 12:48:32 +0800258 .create = xcbc_create,
259 .free = shash_free_instance,
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000260 .module = THIS_MODULE,
261};
262
263static int __init crypto_xcbc_module_init(void)
264{
265 return crypto_register_template(&crypto_xcbc_tmpl);
266}
267
268static void __exit crypto_xcbc_module_exit(void)
269{
270 crypto_unregister_template(&crypto_xcbc_tmpl);
271}
272
Eric Biggersc4741b22019-04-11 21:57:42 -0700273subsys_initcall(crypto_xcbc_module_init);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000274module_exit(crypto_xcbc_module_exit);
275
276MODULE_LICENSE("GPL");
277MODULE_DESCRIPTION("XCBC keyed hash algorithm");
Kees Cook4943ba12014-11-24 16:32:38 -0800278MODULE_ALIAS_CRYPTO("xcbc");