GNU Linux-libre 5.4.274-gnu1
[releases.git] / arch / x86 / crypto / cast6_avx_glue.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Glue Code for the AVX assembler implementation of the Cast6 Cipher
4  *
5  * Copyright (C) 2012 Johannes Goetzfried
6  *     <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
7  *
8  * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
9  */
10
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/crypto.h>
14 #include <linux/err.h>
15 #include <crypto/algapi.h>
16 #include <crypto/cast6.h>
17 #include <crypto/internal/simd.h>
18 #include <crypto/xts.h>
19 #include <asm/crypto/glue_helper.h>
20
21 #define CAST6_PARALLEL_BLOCKS 8
22
23 asmlinkage void cast6_ecb_enc_8way(const void *ctx, u8 *dst, const u8 *src);
24 asmlinkage void cast6_ecb_dec_8way(const void *ctx, u8 *dst, const u8 *src);
25
26 asmlinkage void cast6_cbc_dec_8way(const void *ctx, u8 *dst, const u8 *src);
27 asmlinkage void cast6_ctr_8way(const void *ctx, u8 *dst, const u8 *src,
28                                le128 *iv);
29
30 asmlinkage void cast6_xts_enc_8way(const void *ctx, u8 *dst, const u8 *src,
31                                    le128 *iv);
32 asmlinkage void cast6_xts_dec_8way(const void *ctx, u8 *dst, const u8 *src,
33                                    le128 *iv);
34
35 static int cast6_setkey_skcipher(struct crypto_skcipher *tfm,
36                                  const u8 *key, unsigned int keylen)
37 {
38         return cast6_setkey(&tfm->base, key, keylen);
39 }
40
41 static void cast6_xts_enc(const void *ctx, u8 *dst, const u8 *src, le128 *iv)
42 {
43         glue_xts_crypt_128bit_one(ctx, dst, src, iv, __cast6_encrypt);
44 }
45
46 static void cast6_xts_dec(const void *ctx, u8 *dst, const u8 *src, le128 *iv)
47 {
48         glue_xts_crypt_128bit_one(ctx, dst, src, iv, __cast6_decrypt);
49 }
50
51 static void cast6_crypt_ctr(const void *ctx, u8 *d, const u8 *s, le128 *iv)
52 {
53         be128 ctrblk;
54         u128 *dst = (u128 *)d;
55         const u128 *src = (const u128 *)s;
56
57         le128_to_be128(&ctrblk, iv);
58         le128_inc(iv);
59
60         __cast6_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
61         u128_xor(dst, src, (u128 *)&ctrblk);
62 }
63
64 static const struct common_glue_ctx cast6_enc = {
65         .num_funcs = 2,
66         .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS,
67
68         .funcs = { {
69                 .num_blocks = CAST6_PARALLEL_BLOCKS,
70                 .fn_u = { .ecb = cast6_ecb_enc_8way }
71         }, {
72                 .num_blocks = 1,
73                 .fn_u = { .ecb = __cast6_encrypt }
74         } }
75 };
76
77 static const struct common_glue_ctx cast6_ctr = {
78         .num_funcs = 2,
79         .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS,
80
81         .funcs = { {
82                 .num_blocks = CAST6_PARALLEL_BLOCKS,
83                 .fn_u = { .ctr = cast6_ctr_8way }
84         }, {
85                 .num_blocks = 1,
86                 .fn_u = { .ctr = cast6_crypt_ctr }
87         } }
88 };
89
90 static const struct common_glue_ctx cast6_enc_xts = {
91         .num_funcs = 2,
92         .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS,
93
94         .funcs = { {
95                 .num_blocks = CAST6_PARALLEL_BLOCKS,
96                 .fn_u = { .xts = cast6_xts_enc_8way }
97         }, {
98                 .num_blocks = 1,
99                 .fn_u = { .xts = cast6_xts_enc }
100         } }
101 };
102
103 static const struct common_glue_ctx cast6_dec = {
104         .num_funcs = 2,
105         .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS,
106
107         .funcs = { {
108                 .num_blocks = CAST6_PARALLEL_BLOCKS,
109                 .fn_u = { .ecb = cast6_ecb_dec_8way }
110         }, {
111                 .num_blocks = 1,
112                 .fn_u = { .ecb = __cast6_decrypt }
113         } }
114 };
115
116 static const struct common_glue_ctx cast6_dec_cbc = {
117         .num_funcs = 2,
118         .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS,
119
120         .funcs = { {
121                 .num_blocks = CAST6_PARALLEL_BLOCKS,
122                 .fn_u = { .cbc = cast6_cbc_dec_8way }
123         }, {
124                 .num_blocks = 1,
125                 .fn_u = { .cbc = __cast6_decrypt }
126         } }
127 };
128
129 static const struct common_glue_ctx cast6_dec_xts = {
130         .num_funcs = 2,
131         .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS,
132
133         .funcs = { {
134                 .num_blocks = CAST6_PARALLEL_BLOCKS,
135                 .fn_u = { .xts = cast6_xts_dec_8way }
136         }, {
137                 .num_blocks = 1,
138                 .fn_u = { .xts = cast6_xts_dec }
139         } }
140 };
141
142 static int ecb_encrypt(struct skcipher_request *req)
143 {
144         return glue_ecb_req_128bit(&cast6_enc, req);
145 }
146
147 static int ecb_decrypt(struct skcipher_request *req)
148 {
149         return glue_ecb_req_128bit(&cast6_dec, req);
150 }
151
152 static int cbc_encrypt(struct skcipher_request *req)
153 {
154         return glue_cbc_encrypt_req_128bit(__cast6_encrypt, req);
155 }
156
157 static int cbc_decrypt(struct skcipher_request *req)
158 {
159         return glue_cbc_decrypt_req_128bit(&cast6_dec_cbc, req);
160 }
161
162 static int ctr_crypt(struct skcipher_request *req)
163 {
164         return glue_ctr_req_128bit(&cast6_ctr, req);
165 }
166
167 struct cast6_xts_ctx {
168         struct cast6_ctx tweak_ctx;
169         struct cast6_ctx crypt_ctx;
170 };
171
172 static int xts_cast6_setkey(struct crypto_skcipher *tfm, const u8 *key,
173                             unsigned int keylen)
174 {
175         struct cast6_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
176         u32 *flags = &tfm->base.crt_flags;
177         int err;
178
179         err = xts_verify_key(tfm, key, keylen);
180         if (err)
181                 return err;
182
183         /* first half of xts-key is for crypt */
184         err = __cast6_setkey(&ctx->crypt_ctx, key, keylen / 2, flags);
185         if (err)
186                 return err;
187
188         /* second half of xts-key is for tweak */
189         return __cast6_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2,
190                               flags);
191 }
192
193 static int xts_encrypt(struct skcipher_request *req)
194 {
195         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
196         struct cast6_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
197
198         return glue_xts_req_128bit(&cast6_enc_xts, req, __cast6_encrypt,
199                                    &ctx->tweak_ctx, &ctx->crypt_ctx, false);
200 }
201
202 static int xts_decrypt(struct skcipher_request *req)
203 {
204         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
205         struct cast6_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
206
207         return glue_xts_req_128bit(&cast6_dec_xts, req, __cast6_encrypt,
208                                    &ctx->tweak_ctx, &ctx->crypt_ctx, true);
209 }
210
211 static struct skcipher_alg cast6_algs[] = {
212         {
213                 .base.cra_name          = "__ecb(cast6)",
214                 .base.cra_driver_name   = "__ecb-cast6-avx",
215                 .base.cra_priority      = 200,
216                 .base.cra_flags         = CRYPTO_ALG_INTERNAL,
217                 .base.cra_blocksize     = CAST6_BLOCK_SIZE,
218                 .base.cra_ctxsize       = sizeof(struct cast6_ctx),
219                 .base.cra_module        = THIS_MODULE,
220                 .min_keysize            = CAST6_MIN_KEY_SIZE,
221                 .max_keysize            = CAST6_MAX_KEY_SIZE,
222                 .setkey                 = cast6_setkey_skcipher,
223                 .encrypt                = ecb_encrypt,
224                 .decrypt                = ecb_decrypt,
225         }, {
226                 .base.cra_name          = "__cbc(cast6)",
227                 .base.cra_driver_name   = "__cbc-cast6-avx",
228                 .base.cra_priority      = 200,
229                 .base.cra_flags         = CRYPTO_ALG_INTERNAL,
230                 .base.cra_blocksize     = CAST6_BLOCK_SIZE,
231                 .base.cra_ctxsize       = sizeof(struct cast6_ctx),
232                 .base.cra_module        = THIS_MODULE,
233                 .min_keysize            = CAST6_MIN_KEY_SIZE,
234                 .max_keysize            = CAST6_MAX_KEY_SIZE,
235                 .ivsize                 = CAST6_BLOCK_SIZE,
236                 .setkey                 = cast6_setkey_skcipher,
237                 .encrypt                = cbc_encrypt,
238                 .decrypt                = cbc_decrypt,
239         }, {
240                 .base.cra_name          = "__ctr(cast6)",
241                 .base.cra_driver_name   = "__ctr-cast6-avx",
242                 .base.cra_priority      = 200,
243                 .base.cra_flags         = CRYPTO_ALG_INTERNAL,
244                 .base.cra_blocksize     = 1,
245                 .base.cra_ctxsize       = sizeof(struct cast6_ctx),
246                 .base.cra_module        = THIS_MODULE,
247                 .min_keysize            = CAST6_MIN_KEY_SIZE,
248                 .max_keysize            = CAST6_MAX_KEY_SIZE,
249                 .ivsize                 = CAST6_BLOCK_SIZE,
250                 .chunksize              = CAST6_BLOCK_SIZE,
251                 .setkey                 = cast6_setkey_skcipher,
252                 .encrypt                = ctr_crypt,
253                 .decrypt                = ctr_crypt,
254         }, {
255                 .base.cra_name          = "__xts(cast6)",
256                 .base.cra_driver_name   = "__xts-cast6-avx",
257                 .base.cra_priority      = 200,
258                 .base.cra_flags         = CRYPTO_ALG_INTERNAL,
259                 .base.cra_blocksize     = CAST6_BLOCK_SIZE,
260                 .base.cra_ctxsize       = sizeof(struct cast6_xts_ctx),
261                 .base.cra_module        = THIS_MODULE,
262                 .min_keysize            = 2 * CAST6_MIN_KEY_SIZE,
263                 .max_keysize            = 2 * CAST6_MAX_KEY_SIZE,
264                 .ivsize                 = CAST6_BLOCK_SIZE,
265                 .setkey                 = xts_cast6_setkey,
266                 .encrypt                = xts_encrypt,
267                 .decrypt                = xts_decrypt,
268         },
269 };
270
271 static struct simd_skcipher_alg *cast6_simd_algs[ARRAY_SIZE(cast6_algs)];
272
273 static int __init cast6_init(void)
274 {
275         const char *feature_name;
276
277         if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
278                                 &feature_name)) {
279                 pr_info("CPU feature '%s' is not supported.\n", feature_name);
280                 return -ENODEV;
281         }
282
283         return simd_register_skciphers_compat(cast6_algs,
284                                               ARRAY_SIZE(cast6_algs),
285                                               cast6_simd_algs);
286 }
287
288 static void __exit cast6_exit(void)
289 {
290         simd_unregister_skciphers(cast6_algs, ARRAY_SIZE(cast6_algs),
291                                   cast6_simd_algs);
292 }
293
294 module_init(cast6_init);
295 module_exit(cast6_exit);
296
297 MODULE_DESCRIPTION("Cast6 Cipher Algorithm, AVX optimized");
298 MODULE_LICENSE("GPL");
299 MODULE_ALIAS_CRYPTO("cast6");