blob: 4a9a9f2ee1d8065af829a638a7552bba791b6dea [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Johannes Goetzfried7efe4072012-06-12 16:47:43 +08002/*
3 * Glue Code for AVX assembler versions of Serpent Cipher
4 *
5 * Copyright (C) 2012 Johannes Goetzfried
6 * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
7 *
Jussi Kivilinnaa05248e2013-04-08 21:50:55 +03008 * Copyright © 2011-2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
Johannes Goetzfried7efe4072012-06-12 16:47:43 +08009 */
10
11#include <linux/module.h>
Johannes Goetzfried7efe4072012-06-12 16:47:43 +080012#include <linux/types.h>
13#include <linux/crypto.h>
14#include <linux/err.h>
15#include <crypto/algapi.h>
Eric Biggerse16bf972018-02-19 23:48:06 -080016#include <crypto/internal/simd.h>
Johannes Goetzfried7efe4072012-06-12 16:47:43 +080017#include <crypto/serpent.h>
Johannes Goetzfried7efe4072012-06-12 16:47:43 +080018#include <crypto/xts.h>
Jussi Kivilinna1d0debb2012-06-18 14:07:24 +030019#include <asm/crypto/glue_helper.h>
Eric Biggerse16bf972018-02-19 23:48:06 -080020#include <asm/crypto/serpent-avx.h>
Johannes Goetzfried7efe4072012-06-12 16:47:43 +080021
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030022/* 8-way parallel cipher functions */
23asmlinkage void serpent_ecb_enc_8way_avx(struct serpent_ctx *ctx, u8 *dst,
24 const u8 *src);
25EXPORT_SYMBOL_GPL(serpent_ecb_enc_8way_avx);
26
27asmlinkage void serpent_ecb_dec_8way_avx(struct serpent_ctx *ctx, u8 *dst,
28 const u8 *src);
29EXPORT_SYMBOL_GPL(serpent_ecb_dec_8way_avx);
30
31asmlinkage void serpent_cbc_dec_8way_avx(struct serpent_ctx *ctx, u8 *dst,
32 const u8 *src);
33EXPORT_SYMBOL_GPL(serpent_cbc_dec_8way_avx);
34
35asmlinkage void serpent_ctr_8way_avx(struct serpent_ctx *ctx, u8 *dst,
36 const u8 *src, le128 *iv);
37EXPORT_SYMBOL_GPL(serpent_ctr_8way_avx);
38
39asmlinkage void serpent_xts_enc_8way_avx(struct serpent_ctx *ctx, u8 *dst,
40 const u8 *src, le128 *iv);
41EXPORT_SYMBOL_GPL(serpent_xts_enc_8way_avx);
42
43asmlinkage void serpent_xts_dec_8way_avx(struct serpent_ctx *ctx, u8 *dst,
44 const u8 *src, le128 *iv);
45EXPORT_SYMBOL_GPL(serpent_xts_dec_8way_avx);
46
47void __serpent_crypt_ctr(void *ctx, u128 *dst, const u128 *src, le128 *iv)
Johannes Goetzfried7efe4072012-06-12 16:47:43 +080048{
Jussi Kivilinna1d0debb2012-06-18 14:07:24 +030049 be128 ctrblk;
50
Jussi Kivilinna58990982012-10-20 15:06:36 +030051 le128_to_be128(&ctrblk, iv);
52 le128_inc(iv);
Jussi Kivilinna1d0debb2012-06-18 14:07:24 +030053
54 __serpent_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
55 u128_xor(dst, src, (u128 *)&ctrblk);
Johannes Goetzfried7efe4072012-06-12 16:47:43 +080056}
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030057EXPORT_SYMBOL_GPL(__serpent_crypt_ctr);
Johannes Goetzfried7efe4072012-06-12 16:47:43 +080058
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030059void serpent_xts_enc(void *ctx, u128 *dst, const u128 *src, le128 *iv)
Jussi Kivilinnaa05248e2013-04-08 21:50:55 +030060{
61 glue_xts_crypt_128bit_one(ctx, dst, src, iv,
62 GLUE_FUNC_CAST(__serpent_encrypt));
63}
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030064EXPORT_SYMBOL_GPL(serpent_xts_enc);
Jussi Kivilinnaa05248e2013-04-08 21:50:55 +030065
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030066void serpent_xts_dec(void *ctx, u128 *dst, const u128 *src, le128 *iv)
Jussi Kivilinnaa05248e2013-04-08 21:50:55 +030067{
68 glue_xts_crypt_128bit_one(ctx, dst, src, iv,
69 GLUE_FUNC_CAST(__serpent_decrypt));
70}
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030071EXPORT_SYMBOL_GPL(serpent_xts_dec);
72
Eric Biggerse16bf972018-02-19 23:48:06 -080073static int serpent_setkey_skcipher(struct crypto_skcipher *tfm,
74 const u8 *key, unsigned int keylen)
75{
76 return __serpent_setkey(crypto_skcipher_ctx(tfm), key, keylen);
77}
78
79int xts_serpent_setkey(struct crypto_skcipher *tfm, const u8 *key,
80 unsigned int keylen)
81{
82 struct serpent_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
83 int err;
84
85 err = xts_verify_key(tfm, key, keylen);
86 if (err)
87 return err;
88
89 /* first half of xts-key is for crypt */
90 err = __serpent_setkey(&ctx->crypt_ctx, key, keylen / 2);
91 if (err)
92 return err;
93
94 /* second half of xts-key is for tweak */
95 return __serpent_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2);
96}
97EXPORT_SYMBOL_GPL(xts_serpent_setkey);
Jussi Kivilinnaa05248e2013-04-08 21:50:55 +030098
Jussi Kivilinna1d0debb2012-06-18 14:07:24 +030099static const struct common_glue_ctx serpent_enc = {
100 .num_funcs = 2,
101 .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
102
103 .funcs = { {
104 .num_blocks = SERPENT_PARALLEL_BLOCKS,
Jussi Kivilinnafacd4162012-10-20 15:06:51 +0300105 .fn_u = { .ecb = GLUE_FUNC_CAST(serpent_ecb_enc_8way_avx) }
Jussi Kivilinna1d0debb2012-06-18 14:07:24 +0300106 }, {
107 .num_blocks = 1,
108 .fn_u = { .ecb = GLUE_FUNC_CAST(__serpent_encrypt) }
109 } }
110};
111
112static const struct common_glue_ctx serpent_ctr = {
113 .num_funcs = 2,
114 .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
115
116 .funcs = { {
117 .num_blocks = SERPENT_PARALLEL_BLOCKS,
Jussi Kivilinnafacd4162012-10-20 15:06:51 +0300118 .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(serpent_ctr_8way_avx) }
Jussi Kivilinna1d0debb2012-06-18 14:07:24 +0300119 }, {
120 .num_blocks = 1,
Jussi Kivilinna56d76c92013-04-13 13:46:55 +0300121 .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(__serpent_crypt_ctr) }
Jussi Kivilinna1d0debb2012-06-18 14:07:24 +0300122 } }
123};
124
Jussi Kivilinnaa05248e2013-04-08 21:50:55 +0300125static const struct common_glue_ctx serpent_enc_xts = {
126 .num_funcs = 2,
127 .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
128
129 .funcs = { {
130 .num_blocks = SERPENT_PARALLEL_BLOCKS,
131 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(serpent_xts_enc_8way_avx) }
132 }, {
133 .num_blocks = 1,
134 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(serpent_xts_enc) }
135 } }
136};
137
Jussi Kivilinna1d0debb2012-06-18 14:07:24 +0300138static const struct common_glue_ctx serpent_dec = {
139 .num_funcs = 2,
140 .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
141
142 .funcs = { {
143 .num_blocks = SERPENT_PARALLEL_BLOCKS,
Jussi Kivilinnafacd4162012-10-20 15:06:51 +0300144 .fn_u = { .ecb = GLUE_FUNC_CAST(serpent_ecb_dec_8way_avx) }
Jussi Kivilinna1d0debb2012-06-18 14:07:24 +0300145 }, {
146 .num_blocks = 1,
147 .fn_u = { .ecb = GLUE_FUNC_CAST(__serpent_decrypt) }
148 } }
149};
150
151static const struct common_glue_ctx serpent_dec_cbc = {
152 .num_funcs = 2,
153 .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
154
155 .funcs = { {
156 .num_blocks = SERPENT_PARALLEL_BLOCKS,
Jussi Kivilinnafacd4162012-10-20 15:06:51 +0300157 .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(serpent_cbc_dec_8way_avx) }
Jussi Kivilinna1d0debb2012-06-18 14:07:24 +0300158 }, {
159 .num_blocks = 1,
160 .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(__serpent_decrypt) }
161 } }
162};
163
Jussi Kivilinnaa05248e2013-04-08 21:50:55 +0300164static const struct common_glue_ctx serpent_dec_xts = {
165 .num_funcs = 2,
166 .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
167
168 .funcs = { {
169 .num_blocks = SERPENT_PARALLEL_BLOCKS,
170 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(serpent_xts_dec_8way_avx) }
171 }, {
172 .num_blocks = 1,
173 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(serpent_xts_dec) }
174 } }
175};
176
Eric Biggerse16bf972018-02-19 23:48:06 -0800177static int ecb_encrypt(struct skcipher_request *req)
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800178{
Eric Biggerse16bf972018-02-19 23:48:06 -0800179 return glue_ecb_req_128bit(&serpent_enc, req);
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800180}
181
Eric Biggerse16bf972018-02-19 23:48:06 -0800182static int ecb_decrypt(struct skcipher_request *req)
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800183{
Eric Biggerse16bf972018-02-19 23:48:06 -0800184 return glue_ecb_req_128bit(&serpent_dec, req);
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800185}
186
Eric Biggerse16bf972018-02-19 23:48:06 -0800187static int cbc_encrypt(struct skcipher_request *req)
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800188{
Eric Biggerse16bf972018-02-19 23:48:06 -0800189 return glue_cbc_encrypt_req_128bit(GLUE_FUNC_CAST(__serpent_encrypt),
190 req);
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800191}
192
Eric Biggerse16bf972018-02-19 23:48:06 -0800193static int cbc_decrypt(struct skcipher_request *req)
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800194{
Eric Biggerse16bf972018-02-19 23:48:06 -0800195 return glue_cbc_decrypt_req_128bit(&serpent_dec_cbc, req);
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800196}
197
Eric Biggerse16bf972018-02-19 23:48:06 -0800198static int ctr_crypt(struct skcipher_request *req)
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800199{
Eric Biggerse16bf972018-02-19 23:48:06 -0800200 return glue_ctr_req_128bit(&serpent_ctr, req);
Jussi Kivilinna1d0debb2012-06-18 14:07:24 +0300201}
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800202
Eric Biggerse16bf972018-02-19 23:48:06 -0800203static int xts_encrypt(struct skcipher_request *req)
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800204{
Eric Biggerse16bf972018-02-19 23:48:06 -0800205 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
206 struct serpent_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800207
Eric Biggerse16bf972018-02-19 23:48:06 -0800208 return glue_xts_req_128bit(&serpent_enc_xts, req,
209 XTS_TWEAK_CAST(__serpent_encrypt),
210 &ctx->tweak_ctx, &ctx->crypt_ctx);
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800211}
212
Eric Biggerse16bf972018-02-19 23:48:06 -0800213static int xts_decrypt(struct skcipher_request *req)
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800214{
Eric Biggerse16bf972018-02-19 23:48:06 -0800215 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
216 struct serpent_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800217
Eric Biggerse16bf972018-02-19 23:48:06 -0800218 return glue_xts_req_128bit(&serpent_dec_xts, req,
219 XTS_TWEAK_CAST(__serpent_encrypt),
220 &ctx->tweak_ctx, &ctx->crypt_ctx);
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800221}
222
Eric Biggerse16bf972018-02-19 23:48:06 -0800223static struct skcipher_alg serpent_algs[] = {
224 {
225 .base.cra_name = "__ecb(serpent)",
226 .base.cra_driver_name = "__ecb-serpent-avx",
227 .base.cra_priority = 500,
228 .base.cra_flags = CRYPTO_ALG_INTERNAL,
229 .base.cra_blocksize = SERPENT_BLOCK_SIZE,
230 .base.cra_ctxsize = sizeof(struct serpent_ctx),
231 .base.cra_module = THIS_MODULE,
232 .min_keysize = SERPENT_MIN_KEY_SIZE,
233 .max_keysize = SERPENT_MAX_KEY_SIZE,
234 .setkey = serpent_setkey_skcipher,
235 .encrypt = ecb_encrypt,
236 .decrypt = ecb_decrypt,
237 }, {
238 .base.cra_name = "__cbc(serpent)",
239 .base.cra_driver_name = "__cbc-serpent-avx",
240 .base.cra_priority = 500,
241 .base.cra_flags = CRYPTO_ALG_INTERNAL,
242 .base.cra_blocksize = SERPENT_BLOCK_SIZE,
243 .base.cra_ctxsize = sizeof(struct serpent_ctx),
244 .base.cra_module = THIS_MODULE,
245 .min_keysize = SERPENT_MIN_KEY_SIZE,
246 .max_keysize = SERPENT_MAX_KEY_SIZE,
247 .ivsize = SERPENT_BLOCK_SIZE,
248 .setkey = serpent_setkey_skcipher,
249 .encrypt = cbc_encrypt,
250 .decrypt = cbc_decrypt,
251 }, {
252 .base.cra_name = "__ctr(serpent)",
253 .base.cra_driver_name = "__ctr-serpent-avx",
254 .base.cra_priority = 500,
255 .base.cra_flags = CRYPTO_ALG_INTERNAL,
256 .base.cra_blocksize = 1,
257 .base.cra_ctxsize = sizeof(struct serpent_ctx),
258 .base.cra_module = THIS_MODULE,
259 .min_keysize = SERPENT_MIN_KEY_SIZE,
260 .max_keysize = SERPENT_MAX_KEY_SIZE,
261 .ivsize = SERPENT_BLOCK_SIZE,
262 .chunksize = SERPENT_BLOCK_SIZE,
263 .setkey = serpent_setkey_skcipher,
264 .encrypt = ctr_crypt,
265 .decrypt = ctr_crypt,
266 }, {
267 .base.cra_name = "__xts(serpent)",
268 .base.cra_driver_name = "__xts-serpent-avx",
269 .base.cra_priority = 500,
270 .base.cra_flags = CRYPTO_ALG_INTERNAL,
271 .base.cra_blocksize = SERPENT_BLOCK_SIZE,
272 .base.cra_ctxsize = sizeof(struct serpent_xts_ctx),
273 .base.cra_module = THIS_MODULE,
274 .min_keysize = 2 * SERPENT_MIN_KEY_SIZE,
275 .max_keysize = 2 * SERPENT_MAX_KEY_SIZE,
276 .ivsize = SERPENT_BLOCK_SIZE,
277 .setkey = xts_serpent_setkey,
278 .encrypt = xts_encrypt,
279 .decrypt = xts_decrypt,
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800280 },
Eric Biggerse16bf972018-02-19 23:48:06 -0800281};
282
283static struct simd_skcipher_alg *serpent_simd_algs[ARRAY_SIZE(serpent_algs)];
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800284
285static int __init serpent_init(void)
286{
Ingo Molnarc1c23f72015-04-28 10:11:24 +0200287 const char *feature_name;
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800288
Dave Hansend91cab72015-09-02 16:31:26 -0700289 if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
290 &feature_name)) {
Ingo Molnarc1c23f72015-04-28 10:11:24 +0200291 pr_info("CPU feature '%s' is not supported.\n", feature_name);
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800292 return -ENODEV;
293 }
294
Eric Biggerse16bf972018-02-19 23:48:06 -0800295 return simd_register_skciphers_compat(serpent_algs,
296 ARRAY_SIZE(serpent_algs),
297 serpent_simd_algs);
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800298}
299
300static void __exit serpent_exit(void)
301{
Eric Biggerse16bf972018-02-19 23:48:06 -0800302 simd_unregister_skciphers(serpent_algs, ARRAY_SIZE(serpent_algs),
303 serpent_simd_algs);
Johannes Goetzfried7efe4072012-06-12 16:47:43 +0800304}
305
306module_init(serpent_init);
307module_exit(serpent_exit);
308
309MODULE_DESCRIPTION("Serpent Cipher Algorithm, AVX optimized");
310MODULE_LICENSE("GPL");
Kees Cook5d26a102014-11-20 17:05:53 -0800311MODULE_ALIAS_CRYPTO("serpent");