afs: Provide a splice-read wrapper
[linux-block.git] / crypto / xcbc.c
CommitLineData
1ccea77e 1// SPDX-License-Identifier: GPL-2.0-or-later
333b0d7e
KM
2/*
3 * Copyright (C)2006 USAGI/WIDE Project
4 *
333b0d7e
KM
5 * Author:
6 * Kazunori Miyazawa <miyazawa@linux-ipv6.org>
7 */
8
0eb76ba2 9#include <crypto/internal/cipher.h>
3106caab 10#include <crypto/internal/hash.h>
333b0d7e
KM
11#include <linux/err.h>
12#include <linux/kernel.h>
4bb33cc8 13#include <linux/module.h>
333b0d7e 14
5b37538a
AB
15static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
16 0x02020202, 0x02020202, 0x02020202, 0x02020202,
17 0x03030303, 0x03030303, 0x03030303, 0x03030303};
ac95301f 18
333b0d7e
KM
19/*
20 * +------------------------
21 * | <parent tfm>
22 * +------------------------
ac95301f 23 * | xcbc_tfm_ctx
333b0d7e 24 * +------------------------
ac95301f 25 * | consts (block size * 2)
333b0d7e 26 * +------------------------
ac95301f
HX
27 */
28struct xcbc_tfm_ctx {
29 struct crypto_cipher *child;
30 u8 ctx[];
31};
32
33/*
333b0d7e 34 * +------------------------
ac95301f 35 * | <shash desc>
333b0d7e 36 * +------------------------
ac95301f
HX
37 * | xcbc_desc_ctx
38 * +------------------------
39 * | odds (block size)
40 * +------------------------
41 * | prev (block size)
333b0d7e
KM
42 * +------------------------
43 */
ac95301f 44struct xcbc_desc_ctx {
333b0d7e 45 unsigned int len;
ac95301f 46 u8 ctx[];
333b0d7e
KM
47};
48
3bdd23f8
KC
49#define XCBC_BLOCKSIZE 16
50
ac95301f
HX
51static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
52 const u8 *inkey, unsigned int keylen)
333b0d7e 53{
ac95301f
HX
54 unsigned long alignmask = crypto_shash_alignmask(parent);
55 struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
ac95301f 56 u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
333b0d7e 57 int err = 0;
3bdd23f8
KC
58 u8 key1[XCBC_BLOCKSIZE];
59 int bs = sizeof(key1);
333b0d7e 60
ac95301f
HX
61 if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
62 return err;
333b0d7e 63
ac95301f
HX
64 crypto_cipher_encrypt_one(ctx->child, consts, (u8 *)ks + bs);
65 crypto_cipher_encrypt_one(ctx->child, consts + bs, (u8 *)ks + bs * 2);
66 crypto_cipher_encrypt_one(ctx->child, key1, (u8 *)ks);
333b0d7e
KM
67
68 return crypto_cipher_setkey(ctx->child, key1, bs);
333b0d7e 69
333b0d7e
KM
70}
71
3106caab 72static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
333b0d7e 73{
ac95301f
HX
74 unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
75 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
3106caab 76 int bs = crypto_shash_blocksize(pdesc->tfm);
ac95301f 77 u8 *prev = PTR_ALIGN(&ctx->ctx[0], alignmask + 1) + bs;
333b0d7e
KM
78
79 ctx->len = 0;
ac95301f 80 memset(prev, 0, bs);
333b0d7e
KM
81
82 return 0;
83}
84
3106caab
HX
85static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
86 unsigned int len)
333b0d7e 87{
3106caab 88 struct crypto_shash *parent = pdesc->tfm;
ac95301f
HX
89 unsigned long alignmask = crypto_shash_alignmask(parent);
90 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
91 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
92 struct crypto_cipher *tfm = tctx->child;
3106caab 93 int bs = crypto_shash_blocksize(parent);
ac95301f
HX
94 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
95 u8 *prev = odds + bs;
3106caab
HX
96
97 /* checking the data can fill the block */
98 if ((ctx->len + len) <= bs) {
ac95301f 99 memcpy(odds + ctx->len, p, len);
3106caab
HX
100 ctx->len += len;
101 return 0;
1edcf2e1 102 }
333b0d7e 103
3106caab 104 /* filling odds with new data and encrypting it */
ac95301f 105 memcpy(odds + ctx->len, p, bs - ctx->len);
3106caab
HX
106 len -= bs - ctx->len;
107 p += bs - ctx->len;
333b0d7e 108
ac95301f
HX
109 crypto_xor(prev, odds, bs);
110 crypto_cipher_encrypt_one(tfm, prev, prev);
3106caab
HX
111
112 /* clearing the length */
113 ctx->len = 0;
114
115 /* encrypting the rest of data */
116 while (len > bs) {
ac95301f
HX
117 crypto_xor(prev, p, bs);
118 crypto_cipher_encrypt_one(tfm, prev, prev);
3106caab
HX
119 p += bs;
120 len -= bs;
121 }
122
123 /* keeping the surplus of blocksize */
124 if (len) {
ac95301f 125 memcpy(odds, p, len);
3106caab
HX
126 ctx->len = len;
127 }
128
129 return 0;
fb469840
HX
130}
131
3106caab 132static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
333b0d7e 133{
3106caab 134 struct crypto_shash *parent = pdesc->tfm;
ac95301f
HX
135 unsigned long alignmask = crypto_shash_alignmask(parent);
136 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
137 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
138 struct crypto_cipher *tfm = tctx->child;
3106caab 139 int bs = crypto_shash_blocksize(parent);
ac95301f
HX
140 u8 *consts = PTR_ALIGN(&tctx->ctx[0], alignmask + 1);
141 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
142 u8 *prev = odds + bs;
143 unsigned int offset = 0;
333b0d7e 144
ac95301f 145 if (ctx->len != bs) {
333b0d7e 146 unsigned int rlen;
ac95301f
HX
147 u8 *p = odds + ctx->len;
148
333b0d7e
KM
149 *p = 0x80;
150 p++;
151
152 rlen = bs - ctx->len -1;
153 if (rlen)
154 memset(p, 0, rlen);
155
ac95301f
HX
156 offset += bs;
157 }
333b0d7e 158
ac95301f
HX
159 crypto_xor(prev, odds, bs);
160 crypto_xor(prev, consts + offset, bs);
333b0d7e 161
ac95301f 162 crypto_cipher_encrypt_one(tfm, out, prev);
333b0d7e
KM
163
164 return 0;
165}
166
333b0d7e
KM
167static int xcbc_init_tfm(struct crypto_tfm *tfm)
168{
2e306ee0 169 struct crypto_cipher *cipher;
333b0d7e 170 struct crypto_instance *inst = (void *)tfm->__crt_alg;
d5ed3b65 171 struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
ac95301f 172 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
333b0d7e 173
2e306ee0
HX
174 cipher = crypto_spawn_cipher(spawn);
175 if (IS_ERR(cipher))
176 return PTR_ERR(cipher);
333b0d7e 177
2e306ee0 178 ctx->child = cipher;
333b0d7e
KM
179
180 return 0;
181};
182
183static void xcbc_exit_tfm(struct crypto_tfm *tfm)
184{
ac95301f 185 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
333b0d7e
KM
186 crypto_free_cipher(ctx->child);
187}
188
3106caab 189static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
333b0d7e 190{
3106caab 191 struct shash_instance *inst;
1e212a6a 192 struct crypto_cipher_spawn *spawn;
333b0d7e 193 struct crypto_alg *alg;
36f87a4a 194 unsigned long alignmask;
7bcb2c99 195 u32 mask;
ebc610e5
HX
196 int err;
197
7bcb2c99 198 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
ebc610e5 199 if (err)
3106caab 200 return err;
ebc610e5 201
1e212a6a
EB
202 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
203 if (!inst)
204 return -ENOMEM;
205 spawn = shash_instance_ctx(inst);
333b0d7e 206
1e212a6a 207 err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
7bcb2c99 208 crypto_attr_alg_name(tb[1]), 0, mask);
1e212a6a
EB
209 if (err)
210 goto err_free_inst;
211 alg = crypto_spawn_cipher_alg(spawn);
333b0d7e 212
1e212a6a
EB
213 err = -EINVAL;
214 if (alg->cra_blocksize != XCBC_BLOCKSIZE)
215 goto err_free_inst;
333b0d7e 216
1e212a6a 217 err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
3106caab 218 if (err)
1e212a6a 219 goto err_free_inst;
3106caab 220
36f87a4a
SK
221 alignmask = alg->cra_alignmask | 3;
222 inst->alg.base.cra_alignmask = alignmask;
3106caab
HX
223 inst->alg.base.cra_priority = alg->cra_priority;
224 inst->alg.base.cra_blocksize = alg->cra_blocksize;
3106caab
HX
225
226 inst->alg.digestsize = alg->cra_blocksize;
ac95301f
HX
227 inst->alg.descsize = ALIGN(sizeof(struct xcbc_desc_ctx),
228 crypto_tfm_ctx_alignment()) +
36f87a4a 229 (alignmask &
ac95301f
HX
230 ~(crypto_tfm_ctx_alignment() - 1)) +
231 alg->cra_blocksize * 2;
232
233 inst->alg.base.cra_ctxsize = ALIGN(sizeof(struct xcbc_tfm_ctx),
36f87a4a 234 alignmask + 1) +
ac95301f 235 alg->cra_blocksize * 2;
3106caab
HX
236 inst->alg.base.cra_init = xcbc_init_tfm;
237 inst->alg.base.cra_exit = xcbc_exit_tfm;
238
239 inst->alg.init = crypto_xcbc_digest_init;
240 inst->alg.update = crypto_xcbc_digest_update;
241 inst->alg.final = crypto_xcbc_digest_final;
242 inst->alg.setkey = crypto_xcbc_digest_setkey;
243
a39c66cc
EB
244 inst->free = shash_free_singlespawn_instance;
245
3106caab
HX
246 err = shash_register_instance(tmpl, inst);
247 if (err) {
1e212a6a 248err_free_inst:
a39c66cc 249 shash_free_singlespawn_instance(inst);
3106caab 250 }
3106caab 251 return err;
333b0d7e
KM
252}
253
254static struct crypto_template crypto_xcbc_tmpl = {
255 .name = "xcbc",
3106caab 256 .create = xcbc_create,
333b0d7e
KM
257 .module = THIS_MODULE,
258};
259
260static int __init crypto_xcbc_module_init(void)
261{
262 return crypto_register_template(&crypto_xcbc_tmpl);
263}
264
265static void __exit crypto_xcbc_module_exit(void)
266{
267 crypto_unregister_template(&crypto_xcbc_tmpl);
268}
269
c4741b23 270subsys_initcall(crypto_xcbc_module_init);
333b0d7e
KM
271module_exit(crypto_xcbc_module_exit);
272
273MODULE_LICENSE("GPL");
274MODULE_DESCRIPTION("XCBC keyed hash algorithm");
4943ba16 275MODULE_ALIAS_CRYPTO("xcbc");
0eb76ba2 276MODULE_IMPORT_NS(CRYPTO_INTERNAL);