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