treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / crypto / xcbc.c
blob598ec88abf0f9f6945a2bccdb82d2a5cbaeba529
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C)2006 USAGI/WIDE Project
5 * Author:
6 * Kazunori Miyazawa <miyazawa@linux-ipv6.org>
7 */
9 #include <crypto/internal/hash.h>
10 #include <linux/err.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
14 static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
15 0x02020202, 0x02020202, 0x02020202, 0x02020202,
16 0x03030303, 0x03030303, 0x03030303, 0x03030303};
19 * +------------------------
20 * | <parent tfm>
21 * +------------------------
22 * | xcbc_tfm_ctx
23 * +------------------------
24 * | consts (block size * 2)
25 * +------------------------
27 struct xcbc_tfm_ctx {
28 struct crypto_cipher *child;
29 u8 ctx[];
33 * +------------------------
34 * | <shash desc>
35 * +------------------------
36 * | xcbc_desc_ctx
37 * +------------------------
38 * | odds (block size)
39 * +------------------------
40 * | prev (block size)
41 * +------------------------
43 struct xcbc_desc_ctx {
44 unsigned int len;
45 u8 ctx[];
48 #define XCBC_BLOCKSIZE 16
50 static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
51 const u8 *inkey, unsigned int keylen)
53 unsigned long alignmask = crypto_shash_alignmask(parent);
54 struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
55 u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
56 int err = 0;
57 u8 key1[XCBC_BLOCKSIZE];
58 int bs = sizeof(key1);
60 if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
61 return err;
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);
67 return crypto_cipher_setkey(ctx->child, key1, bs);
71 static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
73 unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
74 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
75 int bs = crypto_shash_blocksize(pdesc->tfm);
76 u8 *prev = PTR_ALIGN(&ctx->ctx[0], alignmask + 1) + bs;
78 ctx->len = 0;
79 memset(prev, 0, bs);
81 return 0;
84 static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
85 unsigned int len)
87 struct crypto_shash *parent = pdesc->tfm;
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;
92 int bs = crypto_shash_blocksize(parent);
93 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
94 u8 *prev = odds + bs;
96 /* checking the data can fill the block */
97 if ((ctx->len + len) <= bs) {
98 memcpy(odds + ctx->len, p, len);
99 ctx->len += len;
100 return 0;
103 /* filling odds with new data and encrypting it */
104 memcpy(odds + ctx->len, p, bs - ctx->len);
105 len -= bs - ctx->len;
106 p += bs - ctx->len;
108 crypto_xor(prev, odds, bs);
109 crypto_cipher_encrypt_one(tfm, prev, prev);
111 /* clearing the length */
112 ctx->len = 0;
114 /* encrypting the rest of data */
115 while (len > bs) {
116 crypto_xor(prev, p, bs);
117 crypto_cipher_encrypt_one(tfm, prev, prev);
118 p += bs;
119 len -= bs;
122 /* keeping the surplus of blocksize */
123 if (len) {
124 memcpy(odds, p, len);
125 ctx->len = len;
128 return 0;
131 static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
133 struct crypto_shash *parent = pdesc->tfm;
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;
138 int bs = crypto_shash_blocksize(parent);
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;
144 if (ctx->len != bs) {
145 unsigned int rlen;
146 u8 *p = odds + ctx->len;
148 *p = 0x80;
149 p++;
151 rlen = bs - ctx->len -1;
152 if (rlen)
153 memset(p, 0, rlen);
155 offset += bs;
158 crypto_xor(prev, odds, bs);
159 crypto_xor(prev, consts + offset, bs);
161 crypto_cipher_encrypt_one(tfm, out, prev);
163 return 0;
166 static int xcbc_init_tfm(struct crypto_tfm *tfm)
168 struct crypto_cipher *cipher;
169 struct crypto_instance *inst = (void *)tfm->__crt_alg;
170 struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
171 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
173 cipher = crypto_spawn_cipher(spawn);
174 if (IS_ERR(cipher))
175 return PTR_ERR(cipher);
177 ctx->child = cipher;
179 return 0;
182 static void xcbc_exit_tfm(struct crypto_tfm *tfm)
184 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
185 crypto_free_cipher(ctx->child);
188 static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
190 struct shash_instance *inst;
191 struct crypto_cipher_spawn *spawn;
192 struct crypto_alg *alg;
193 unsigned long alignmask;
194 int err;
196 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
197 if (err)
198 return err;
200 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
201 if (!inst)
202 return -ENOMEM;
203 spawn = shash_instance_ctx(inst);
205 err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
206 crypto_attr_alg_name(tb[1]), 0, 0);
207 if (err)
208 goto err_free_inst;
209 alg = crypto_spawn_cipher_alg(spawn);
211 err = -EINVAL;
212 if (alg->cra_blocksize != XCBC_BLOCKSIZE)
213 goto err_free_inst;
215 err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
216 if (err)
217 goto err_free_inst;
219 alignmask = alg->cra_alignmask | 3;
220 inst->alg.base.cra_alignmask = alignmask;
221 inst->alg.base.cra_priority = alg->cra_priority;
222 inst->alg.base.cra_blocksize = alg->cra_blocksize;
224 inst->alg.digestsize = alg->cra_blocksize;
225 inst->alg.descsize = ALIGN(sizeof(struct xcbc_desc_ctx),
226 crypto_tfm_ctx_alignment()) +
227 (alignmask &
228 ~(crypto_tfm_ctx_alignment() - 1)) +
229 alg->cra_blocksize * 2;
231 inst->alg.base.cra_ctxsize = ALIGN(sizeof(struct xcbc_tfm_ctx),
232 alignmask + 1) +
233 alg->cra_blocksize * 2;
234 inst->alg.base.cra_init = xcbc_init_tfm;
235 inst->alg.base.cra_exit = xcbc_exit_tfm;
237 inst->alg.init = crypto_xcbc_digest_init;
238 inst->alg.update = crypto_xcbc_digest_update;
239 inst->alg.final = crypto_xcbc_digest_final;
240 inst->alg.setkey = crypto_xcbc_digest_setkey;
242 inst->free = shash_free_singlespawn_instance;
244 err = shash_register_instance(tmpl, inst);
245 if (err) {
246 err_free_inst:
247 shash_free_singlespawn_instance(inst);
249 return err;
252 static struct crypto_template crypto_xcbc_tmpl = {
253 .name = "xcbc",
254 .create = xcbc_create,
255 .module = THIS_MODULE,
258 static int __init crypto_xcbc_module_init(void)
260 return crypto_register_template(&crypto_xcbc_tmpl);
263 static void __exit crypto_xcbc_module_exit(void)
265 crypto_unregister_template(&crypto_xcbc_tmpl);
268 subsys_initcall(crypto_xcbc_module_init);
269 module_exit(crypto_xcbc_module_exit);
271 MODULE_LICENSE("GPL");
272 MODULE_DESCRIPTION("XCBC keyed hash algorithm");
273 MODULE_ALIAS_CRYPTO("xcbc");