blob: ccf0b5fa4933066c054c2269a40cbbf6b9d3a08a [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jussi Kivilinna56d76c92013-04-13 13:46:55 +03002/*
3 * Glue Code for x86_64/AVX2 assembler optimized version of Serpent
4 *
5 * Copyright © 2012-2013 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
Jussi Kivilinna56d76c92013-04-13 13:46:55 +03006 */
7
8#include <linux/module.h>
9#include <linux/types.h>
10#include <linux/crypto.h>
11#include <linux/err.h>
12#include <crypto/algapi.h>
Eric Biggerse16bf972018-02-19 23:48:06 -080013#include <crypto/internal/simd.h>
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030014#include <crypto/serpent.h>
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030015
Ard Biesheuvela04ea6f2021-01-05 17:48:09 +010016#include "serpent-avx.h"
Ard Biesheuvel9ad58b42021-01-05 17:48:04 +010017#include "ecb_cbc_helpers.h"
18
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030019#define SERPENT_AVX2_PARALLEL_BLOCKS 16
20
21/* 16-way AVX2 parallel cipher functions */
Kees Cook9c1e8832019-11-26 22:08:02 -080022asmlinkage void serpent_ecb_enc_16way(const void *ctx, u8 *dst, const u8 *src);
23asmlinkage void serpent_ecb_dec_16way(const void *ctx, u8 *dst, const u8 *src);
24asmlinkage void serpent_cbc_dec_16way(const void *ctx, u8 *dst, const u8 *src);
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030025
Eric Biggerse16bf972018-02-19 23:48:06 -080026static int serpent_setkey_skcipher(struct crypto_skcipher *tfm,
27 const u8 *key, unsigned int keylen)
28{
29 return __serpent_setkey(crypto_skcipher_ctx(tfm), key, keylen);
30}
31
Eric Biggerse16bf972018-02-19 23:48:06 -080032static int ecb_encrypt(struct skcipher_request *req)
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030033{
Ard Biesheuvel9ad58b42021-01-05 17:48:04 +010034 ECB_WALK_START(req, SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS);
35 ECB_BLOCK(SERPENT_AVX2_PARALLEL_BLOCKS, serpent_ecb_enc_16way);
36 ECB_BLOCK(SERPENT_PARALLEL_BLOCKS, serpent_ecb_enc_8way_avx);
37 ECB_BLOCK(1, __serpent_encrypt);
38 ECB_WALK_END();
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030039}
40
Eric Biggerse16bf972018-02-19 23:48:06 -080041static int ecb_decrypt(struct skcipher_request *req)
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030042{
Ard Biesheuvel9ad58b42021-01-05 17:48:04 +010043 ECB_WALK_START(req, SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS);
44 ECB_BLOCK(SERPENT_AVX2_PARALLEL_BLOCKS, serpent_ecb_dec_16way);
45 ECB_BLOCK(SERPENT_PARALLEL_BLOCKS, serpent_ecb_dec_8way_avx);
46 ECB_BLOCK(1, __serpent_decrypt);
47 ECB_WALK_END();
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030048}
49
Eric Biggerse16bf972018-02-19 23:48:06 -080050static int cbc_encrypt(struct skcipher_request *req)
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030051{
Ard Biesheuvel9ad58b42021-01-05 17:48:04 +010052 CBC_WALK_START(req, SERPENT_BLOCK_SIZE, -1);
53 CBC_ENC_BLOCK(__serpent_encrypt);
54 CBC_WALK_END();
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030055}
56
Eric Biggerse16bf972018-02-19 23:48:06 -080057static int cbc_decrypt(struct skcipher_request *req)
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030058{
Ard Biesheuvel9ad58b42021-01-05 17:48:04 +010059 CBC_WALK_START(req, SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS);
60 CBC_DEC_BLOCK(SERPENT_AVX2_PARALLEL_BLOCKS, serpent_cbc_dec_16way);
61 CBC_DEC_BLOCK(SERPENT_PARALLEL_BLOCKS, serpent_cbc_dec_8way_avx);
62 CBC_DEC_BLOCK(1, __serpent_decrypt);
63 CBC_WALK_END();
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030064}
65
Eric Biggerse16bf972018-02-19 23:48:06 -080066static struct skcipher_alg serpent_algs[] = {
67 {
68 .base.cra_name = "__ecb(serpent)",
69 .base.cra_driver_name = "__ecb-serpent-avx2",
70 .base.cra_priority = 600,
71 .base.cra_flags = CRYPTO_ALG_INTERNAL,
72 .base.cra_blocksize = SERPENT_BLOCK_SIZE,
73 .base.cra_ctxsize = sizeof(struct serpent_ctx),
74 .base.cra_module = THIS_MODULE,
75 .min_keysize = SERPENT_MIN_KEY_SIZE,
76 .max_keysize = SERPENT_MAX_KEY_SIZE,
77 .setkey = serpent_setkey_skcipher,
78 .encrypt = ecb_encrypt,
79 .decrypt = ecb_decrypt,
80 }, {
81 .base.cra_name = "__cbc(serpent)",
82 .base.cra_driver_name = "__cbc-serpent-avx2",
83 .base.cra_priority = 600,
84 .base.cra_flags = CRYPTO_ALG_INTERNAL,
85 .base.cra_blocksize = SERPENT_BLOCK_SIZE,
86 .base.cra_ctxsize = sizeof(struct serpent_ctx),
87 .base.cra_module = THIS_MODULE,
88 .min_keysize = SERPENT_MIN_KEY_SIZE,
89 .max_keysize = SERPENT_MAX_KEY_SIZE,
90 .ivsize = SERPENT_BLOCK_SIZE,
91 .setkey = serpent_setkey_skcipher,
92 .encrypt = cbc_encrypt,
93 .decrypt = cbc_decrypt,
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030094 },
Eric Biggerse16bf972018-02-19 23:48:06 -080095};
96
97static struct simd_skcipher_alg *serpent_simd_algs[ARRAY_SIZE(serpent_algs)];
Jussi Kivilinna56d76c92013-04-13 13:46:55 +030098
99static int __init init(void)
100{
Ingo Molnar534ff062015-04-28 10:11:24 +0200101 const char *feature_name;
Jussi Kivilinna56d76c92013-04-13 13:46:55 +0300102
Borislav Petkovabcfdfe2016-04-04 22:24:54 +0200103 if (!boot_cpu_has(X86_FEATURE_AVX2) || !boot_cpu_has(X86_FEATURE_OSXSAVE)) {
Ingo Molnarb54b4bb2015-05-22 10:58:45 +0200104 pr_info("AVX2 instructions are not detected.\n");
105 return -ENODEV;
106 }
Dave Hansend91cab72015-09-02 16:31:26 -0700107 if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
108 &feature_name)) {
Ingo Molnar534ff062015-04-28 10:11:24 +0200109 pr_info("CPU feature '%s' is not supported.\n", feature_name);
Jussi Kivilinna56d76c92013-04-13 13:46:55 +0300110 return -ENODEV;
111 }
112
Eric Biggerse16bf972018-02-19 23:48:06 -0800113 return simd_register_skciphers_compat(serpent_algs,
114 ARRAY_SIZE(serpent_algs),
115 serpent_simd_algs);
Jussi Kivilinna56d76c92013-04-13 13:46:55 +0300116}
117
118static void __exit fini(void)
119{
Eric Biggerse16bf972018-02-19 23:48:06 -0800120 simd_unregister_skciphers(serpent_algs, ARRAY_SIZE(serpent_algs),
121 serpent_simd_algs);
Jussi Kivilinna56d76c92013-04-13 13:46:55 +0300122}
123
124module_init(init);
125module_exit(fini);
126
127MODULE_LICENSE("GPL");
128MODULE_DESCRIPTION("Serpent Cipher Algorithm, AVX2 optimized");
Kees Cook5d26a102014-11-20 17:05:53 -0800129MODULE_ALIAS_CRYPTO("serpent");
130MODULE_ALIAS_CRYPTO("serpent-asm");