Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma
[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;
a2b11180 30 u8 consts[];
ac95301f
HX
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;
a2b11180 46 u8 odds[];
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 54 struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
a2b11180 55 u8 *consts = ctx->consts;
333b0d7e 56 int err = 0;
3bdd23f8
KC
57 u8 key1[XCBC_BLOCKSIZE];
58 int bs = sizeof(key1);
333b0d7e 59
ac95301f
HX
60 if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
61 return err;
333b0d7e 62
ac95301f
HX
63 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);
333b0d7e
KM
66
67 return crypto_cipher_setkey(ctx->child, key1, bs);
333b0d7e 68
333b0d7e
KM
69}
70
3106caab 71static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
333b0d7e 72{
ac95301f 73 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
3106caab 74 int bs = crypto_shash_blocksize(pdesc->tfm);
a2b11180 75 u8 *prev = &ctx->odds[bs];
333b0d7e
KM
76
77 ctx->len = 0;
ac95301f 78 memset(prev, 0, bs);
333b0d7e
KM
79
80 return 0;
81}
82
3106caab
HX
83static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
84 unsigned int len)
333b0d7e 85{
3106caab 86 struct crypto_shash *parent = pdesc->tfm;
ac95301f
HX
87 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
88 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
89 struct crypto_cipher *tfm = tctx->child;
3106caab 90 int bs = crypto_shash_blocksize(parent);
a2b11180 91 u8 *odds = ctx->odds;
ac95301f 92 u8 *prev = odds + bs;
3106caab
HX
93
94 /* checking the data can fill the block */
95 if ((ctx->len + len) <= bs) {
ac95301f 96 memcpy(odds + ctx->len, p, len);
3106caab
HX
97 ctx->len += len;
98 return 0;
1edcf2e1 99 }
333b0d7e 100
3106caab 101 /* filling odds with new data and encrypting it */
ac95301f 102 memcpy(odds + ctx->len, p, bs - ctx->len);
3106caab
HX
103 len -= bs - ctx->len;
104 p += bs - ctx->len;
333b0d7e 105
ac95301f
HX
106 crypto_xor(prev, odds, bs);
107 crypto_cipher_encrypt_one(tfm, prev, prev);
3106caab
HX
108
109 /* clearing the length */
110 ctx->len = 0;
111
112 /* encrypting the rest of data */
113 while (len > bs) {
ac95301f
HX
114 crypto_xor(prev, p, bs);
115 crypto_cipher_encrypt_one(tfm, prev, prev);
3106caab
HX
116 p += bs;
117 len -= bs;
118 }
119
120 /* keeping the surplus of blocksize */
121 if (len) {
ac95301f 122 memcpy(odds, p, len);
3106caab
HX
123 ctx->len = len;
124 }
125
126 return 0;
fb469840
HX
127}
128
3106caab 129static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
333b0d7e 130{
3106caab 131 struct crypto_shash *parent = pdesc->tfm;
ac95301f
HX
132 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
133 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
134 struct crypto_cipher *tfm = tctx->child;
3106caab 135 int bs = crypto_shash_blocksize(parent);
a2b11180 136 u8 *odds = ctx->odds;
ac95301f
HX
137 u8 *prev = odds + bs;
138 unsigned int offset = 0;
333b0d7e 139
ac95301f 140 if (ctx->len != bs) {
333b0d7e 141 unsigned int rlen;
ac95301f
HX
142 u8 *p = odds + ctx->len;
143
333b0d7e
KM
144 *p = 0x80;
145 p++;
146
147 rlen = bs - ctx->len -1;
148 if (rlen)
149 memset(p, 0, rlen);
150
ac95301f
HX
151 offset += bs;
152 }
333b0d7e 153
ac95301f 154 crypto_xor(prev, odds, bs);
a2b11180 155 crypto_xor(prev, &tctx->consts[offset], bs);
333b0d7e 156
ac95301f 157 crypto_cipher_encrypt_one(tfm, out, prev);
333b0d7e
KM
158
159 return 0;
160}
161
333b0d7e
KM
162static int xcbc_init_tfm(struct crypto_tfm *tfm)
163{
2e306ee0 164 struct crypto_cipher *cipher;
333b0d7e 165 struct crypto_instance *inst = (void *)tfm->__crt_alg;
d5ed3b65 166 struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
ac95301f 167 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
333b0d7e 168
2e306ee0
HX
169 cipher = crypto_spawn_cipher(spawn);
170 if (IS_ERR(cipher))
171 return PTR_ERR(cipher);
333b0d7e 172
2e306ee0 173 ctx->child = cipher;
333b0d7e
KM
174
175 return 0;
176};
177
178static void xcbc_exit_tfm(struct crypto_tfm *tfm)
179{
ac95301f 180 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
333b0d7e
KM
181 crypto_free_cipher(ctx->child);
182}
183
3106caab 184static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
333b0d7e 185{
3106caab 186 struct shash_instance *inst;
1e212a6a 187 struct crypto_cipher_spawn *spawn;
333b0d7e 188 struct crypto_alg *alg;
7bcb2c99 189 u32 mask;
ebc610e5
HX
190 int err;
191
7bcb2c99 192 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
ebc610e5 193 if (err)
3106caab 194 return err;
ebc610e5 195
1e212a6a
EB
196 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
197 if (!inst)
198 return -ENOMEM;
199 spawn = shash_instance_ctx(inst);
333b0d7e 200
1e212a6a 201 err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
7bcb2c99 202 crypto_attr_alg_name(tb[1]), 0, mask);
1e212a6a
EB
203 if (err)
204 goto err_free_inst;
205 alg = crypto_spawn_cipher_alg(spawn);
333b0d7e 206
1e212a6a
EB
207 err = -EINVAL;
208 if (alg->cra_blocksize != XCBC_BLOCKSIZE)
209 goto err_free_inst;
333b0d7e 210
1e212a6a 211 err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
3106caab 212 if (err)
1e212a6a 213 goto err_free_inst;
3106caab
HX
214
215 inst->alg.base.cra_priority = alg->cra_priority;
216 inst->alg.base.cra_blocksize = alg->cra_blocksize;
a2b11180
EB
217 inst->alg.base.cra_ctxsize = sizeof(struct xcbc_tfm_ctx) +
218 alg->cra_blocksize * 2;
3106caab
HX
219
220 inst->alg.digestsize = alg->cra_blocksize;
a2b11180 221 inst->alg.descsize = sizeof(struct xcbc_desc_ctx) +
ac95301f
HX
222 alg->cra_blocksize * 2;
223
3106caab
HX
224 inst->alg.base.cra_init = xcbc_init_tfm;
225 inst->alg.base.cra_exit = xcbc_exit_tfm;
226
227 inst->alg.init = crypto_xcbc_digest_init;
228 inst->alg.update = crypto_xcbc_digest_update;
229 inst->alg.final = crypto_xcbc_digest_final;
230 inst->alg.setkey = crypto_xcbc_digest_setkey;
231
a39c66cc
EB
232 inst->free = shash_free_singlespawn_instance;
233
3106caab
HX
234 err = shash_register_instance(tmpl, inst);
235 if (err) {
1e212a6a 236err_free_inst:
a39c66cc 237 shash_free_singlespawn_instance(inst);
3106caab 238 }
3106caab 239 return err;
333b0d7e
KM
240}
241
242static struct crypto_template crypto_xcbc_tmpl = {
243 .name = "xcbc",
3106caab 244 .create = xcbc_create,
333b0d7e
KM
245 .module = THIS_MODULE,
246};
247
248static int __init crypto_xcbc_module_init(void)
249{
250 return crypto_register_template(&crypto_xcbc_tmpl);
251}
252
253static void __exit crypto_xcbc_module_exit(void)
254{
255 crypto_unregister_template(&crypto_xcbc_tmpl);
256}
257
c4741b23 258subsys_initcall(crypto_xcbc_module_init);
333b0d7e
KM
259module_exit(crypto_xcbc_module_exit);
260
261MODULE_LICENSE("GPL");
262MODULE_DESCRIPTION("XCBC keyed hash algorithm");
4943ba16 263MODULE_ALIAS_CRYPTO("xcbc");
0eb76ba2 264MODULE_IMPORT_NS(CRYPTO_INTERNAL);