blob: 5fdf1931d0690090bd5d6b3296910a907af65e17 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jussi Kivilinna937c30d2011-11-09 16:26:25 +02002/*
3 * Glue Code for SSE2 assembler versions of Serpent Cipher
4 *
5 * Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
6 *
7 * Glue code based on aesni-intel_glue.c by:
8 * Copyright (C) 2008, Intel Corp.
9 * Author: Huang Ying <ying.huang@intel.com>
10 *
11 * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
12 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
13 * CTR part based on code (crypto/ctr.c) by:
14 * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
Jussi Kivilinna937c30d2011-11-09 16:26:25 +020015 */
16
17#include <linux/module.h>
Jussi Kivilinna937c30d2011-11-09 16:26:25 +020018#include <linux/types.h>
19#include <linux/crypto.h>
20#include <linux/err.h>
21#include <crypto/algapi.h>
Jussi Kivilinna937c30d2011-11-09 16:26:25 +020022#include <crypto/b128ops.h>
Eric Biggerse0f409d2018-02-19 23:48:03 -080023#include <crypto/internal/simd.h>
24#include <crypto/serpent.h>
Jussi Kivilinnad4af0e92012-06-18 14:07:45 +030025#include <asm/crypto/serpent-sse2.h>
Jussi Kivilinna596d8752012-06-18 14:07:19 +030026#include <asm/crypto/glue_helper.h>
Jussi Kivilinna937c30d2011-11-09 16:26:25 +020027
Eric Biggerse0f409d2018-02-19 23:48:03 -080028static int serpent_setkey_skcipher(struct crypto_skcipher *tfm,
29 const u8 *key, unsigned int keylen)
30{
31 return __serpent_setkey(crypto_skcipher_ctx(tfm), key, keylen);
32}
33
Jussi Kivilinnae81792f2012-06-18 14:07:14 +030034static void serpent_decrypt_cbc_xway(void *ctx, u128 *dst, const u128 *src)
35{
36 u128 ivs[SERPENT_PARALLEL_BLOCKS - 1];
37 unsigned int j;
38
39 for (j = 0; j < SERPENT_PARALLEL_BLOCKS - 1; j++)
40 ivs[j] = src[j];
41
42 serpent_dec_blk_xway(ctx, (u8 *)dst, (u8 *)src);
43
44 for (j = 0; j < SERPENT_PARALLEL_BLOCKS - 1; j++)
45 u128_xor(dst + (j + 1), dst + (j + 1), ivs + j);
46}
47
Jussi Kivilinna58990982012-10-20 15:06:36 +030048static void serpent_crypt_ctr(void *ctx, u128 *dst, const u128 *src, le128 *iv)
Jussi Kivilinnae81792f2012-06-18 14:07:14 +030049{
50 be128 ctrblk;
51
Jussi Kivilinna58990982012-10-20 15:06:36 +030052 le128_to_be128(&ctrblk, iv);
53 le128_inc(iv);
Jussi Kivilinnae81792f2012-06-18 14:07:14 +030054
55 __serpent_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
56 u128_xor(dst, src, (u128 *)&ctrblk);
57}
58
59static void serpent_crypt_ctr_xway(void *ctx, u128 *dst, const u128 *src,
Jussi Kivilinna58990982012-10-20 15:06:36 +030060 le128 *iv)
Jussi Kivilinnae81792f2012-06-18 14:07:14 +030061{
62 be128 ctrblks[SERPENT_PARALLEL_BLOCKS];
63 unsigned int i;
64
65 for (i = 0; i < SERPENT_PARALLEL_BLOCKS; i++) {
66 if (dst != src)
67 dst[i] = src[i];
68
Jussi Kivilinna58990982012-10-20 15:06:36 +030069 le128_to_be128(&ctrblks[i], iv);
70 le128_inc(iv);
Jussi Kivilinnae81792f2012-06-18 14:07:14 +030071 }
72
73 serpent_enc_blk_xway_xor(ctx, (u8 *)dst, (u8 *)ctrblks);
74}
75
76static const struct common_glue_ctx serpent_enc = {
77 .num_funcs = 2,
78 .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
79
80 .funcs = { {
81 .num_blocks = SERPENT_PARALLEL_BLOCKS,
82 .fn_u = { .ecb = GLUE_FUNC_CAST(serpent_enc_blk_xway) }
83 }, {
84 .num_blocks = 1,
85 .fn_u = { .ecb = GLUE_FUNC_CAST(__serpent_encrypt) }
86 } }
87};
88
89static const struct common_glue_ctx serpent_ctr = {
90 .num_funcs = 2,
91 .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
92
93 .funcs = { {
94 .num_blocks = SERPENT_PARALLEL_BLOCKS,
95 .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(serpent_crypt_ctr_xway) }
96 }, {
97 .num_blocks = 1,
98 .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(serpent_crypt_ctr) }
99 } }
100};
101
102static const struct common_glue_ctx serpent_dec = {
103 .num_funcs = 2,
104 .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
105
106 .funcs = { {
107 .num_blocks = SERPENT_PARALLEL_BLOCKS,
108 .fn_u = { .ecb = GLUE_FUNC_CAST(serpent_dec_blk_xway) }
109 }, {
110 .num_blocks = 1,
111 .fn_u = { .ecb = GLUE_FUNC_CAST(__serpent_decrypt) }
112 } }
113};
114
115static const struct common_glue_ctx serpent_dec_cbc = {
116 .num_funcs = 2,
117 .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
118
119 .funcs = { {
120 .num_blocks = SERPENT_PARALLEL_BLOCKS,
121 .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(serpent_decrypt_cbc_xway) }
122 }, {
123 .num_blocks = 1,
124 .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(__serpent_decrypt) }
125 } }
126};
127
Eric Biggerse0f409d2018-02-19 23:48:03 -0800128static int ecb_encrypt(struct skcipher_request *req)
Jussi Kivilinnae81792f2012-06-18 14:07:14 +0300129{
Eric Biggerse0f409d2018-02-19 23:48:03 -0800130 return glue_ecb_req_128bit(&serpent_enc, req);
Jussi Kivilinnae81792f2012-06-18 14:07:14 +0300131}
132
Eric Biggerse0f409d2018-02-19 23:48:03 -0800133static int ecb_decrypt(struct skcipher_request *req)
Jussi Kivilinnae81792f2012-06-18 14:07:14 +0300134{
Eric Biggerse0f409d2018-02-19 23:48:03 -0800135 return glue_ecb_req_128bit(&serpent_dec, req);
Jussi Kivilinnae81792f2012-06-18 14:07:14 +0300136}
137
Eric Biggerse0f409d2018-02-19 23:48:03 -0800138static int cbc_encrypt(struct skcipher_request *req)
Jussi Kivilinnae81792f2012-06-18 14:07:14 +0300139{
Eric Biggerse0f409d2018-02-19 23:48:03 -0800140 return glue_cbc_encrypt_req_128bit(GLUE_FUNC_CAST(__serpent_encrypt),
141 req);
Jussi Kivilinnae81792f2012-06-18 14:07:14 +0300142}
143
Eric Biggerse0f409d2018-02-19 23:48:03 -0800144static int cbc_decrypt(struct skcipher_request *req)
Jussi Kivilinnae81792f2012-06-18 14:07:14 +0300145{
Eric Biggerse0f409d2018-02-19 23:48:03 -0800146 return glue_cbc_decrypt_req_128bit(&serpent_dec_cbc, req);
Jussi Kivilinnae81792f2012-06-18 14:07:14 +0300147}
148
Eric Biggerse0f409d2018-02-19 23:48:03 -0800149static int ctr_crypt(struct skcipher_request *req)
Jussi Kivilinnae81792f2012-06-18 14:07:14 +0300150{
Eric Biggerse0f409d2018-02-19 23:48:03 -0800151 return glue_ctr_req_128bit(&serpent_ctr, req);
Jussi Kivilinnae81792f2012-06-18 14:07:14 +0300152}
153
Eric Biggerse0f409d2018-02-19 23:48:03 -0800154static struct skcipher_alg serpent_algs[] = {
155 {
156 .base.cra_name = "__ecb(serpent)",
157 .base.cra_driver_name = "__ecb-serpent-sse2",
158 .base.cra_priority = 400,
159 .base.cra_flags = CRYPTO_ALG_INTERNAL,
160 .base.cra_blocksize = SERPENT_BLOCK_SIZE,
161 .base.cra_ctxsize = sizeof(struct serpent_ctx),
162 .base.cra_module = THIS_MODULE,
163 .min_keysize = SERPENT_MIN_KEY_SIZE,
164 .max_keysize = SERPENT_MAX_KEY_SIZE,
165 .setkey = serpent_setkey_skcipher,
166 .encrypt = ecb_encrypt,
167 .decrypt = ecb_decrypt,
168 }, {
169 .base.cra_name = "__cbc(serpent)",
170 .base.cra_driver_name = "__cbc-serpent-sse2",
171 .base.cra_priority = 400,
172 .base.cra_flags = CRYPTO_ALG_INTERNAL,
173 .base.cra_blocksize = SERPENT_BLOCK_SIZE,
174 .base.cra_ctxsize = sizeof(struct serpent_ctx),
175 .base.cra_module = THIS_MODULE,
176 .min_keysize = SERPENT_MIN_KEY_SIZE,
177 .max_keysize = SERPENT_MAX_KEY_SIZE,
178 .ivsize = SERPENT_BLOCK_SIZE,
179 .setkey = serpent_setkey_skcipher,
180 .encrypt = cbc_encrypt,
181 .decrypt = cbc_decrypt,
182 }, {
183 .base.cra_name = "__ctr(serpent)",
184 .base.cra_driver_name = "__ctr-serpent-sse2",
185 .base.cra_priority = 400,
186 .base.cra_flags = CRYPTO_ALG_INTERNAL,
187 .base.cra_blocksize = 1,
188 .base.cra_ctxsize = sizeof(struct serpent_ctx),
189 .base.cra_module = THIS_MODULE,
190 .min_keysize = SERPENT_MIN_KEY_SIZE,
191 .max_keysize = SERPENT_MAX_KEY_SIZE,
192 .ivsize = SERPENT_BLOCK_SIZE,
193 .chunksize = SERPENT_BLOCK_SIZE,
194 .setkey = serpent_setkey_skcipher,
195 .encrypt = ctr_crypt,
196 .decrypt = ctr_crypt,
Jussi Kivilinna35474c32012-02-17 22:48:37 +0200197 },
Eric Biggerse0f409d2018-02-19 23:48:03 -0800198};
199
200static struct simd_skcipher_alg *serpent_simd_algs[ARRAY_SIZE(serpent_algs)];
Jussi Kivilinna5962f8b2011-11-09 16:26:41 +0200201
Jussi Kivilinna937c30d2011-11-09 16:26:25 +0200202static int __init serpent_sse2_init(void)
203{
Borislav Petkov054efb62016-03-29 17:42:00 +0200204 if (!boot_cpu_has(X86_FEATURE_XMM2)) {
Jussi Kivilinna937c30d2011-11-09 16:26:25 +0200205 printk(KERN_INFO "SSE2 instructions are not detected.\n");
206 return -ENODEV;
207 }
208
Eric Biggerse0f409d2018-02-19 23:48:03 -0800209 return simd_register_skciphers_compat(serpent_algs,
210 ARRAY_SIZE(serpent_algs),
211 serpent_simd_algs);
Jussi Kivilinna937c30d2011-11-09 16:26:25 +0200212}
213
214static void __exit serpent_sse2_exit(void)
215{
Eric Biggerse0f409d2018-02-19 23:48:03 -0800216 simd_unregister_skciphers(serpent_algs, ARRAY_SIZE(serpent_algs),
217 serpent_simd_algs);
Jussi Kivilinna937c30d2011-11-09 16:26:25 +0200218}
219
220module_init(serpent_sse2_init);
221module_exit(serpent_sse2_exit);
222
223MODULE_DESCRIPTION("Serpent Cipher Algorithm, SSE2 optimized");
224MODULE_LICENSE("GPL");
Kees Cook5d26a102014-11-20 17:05:53 -0800225MODULE_ALIAS_CRYPTO("serpent");