Blame view
arch/x86/crypto/camellia_aesni_avx_glue.c
8.51 KB
2874c5fd2 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
d9b1d2e7e crypto: camellia ... |
2 3 4 |
/* * Glue Code for x86_64/AVX/AES-NI assembler optimized version of Camellia * |
b5c5b072d crypto: x86/camel... |
5 |
* Copyright © 2012-2013 Jussi Kivilinna <jussi.kivilinna@iki.fi> |
d9b1d2e7e crypto: camellia ... |
6 |
*/ |
d9b1d2e7e crypto: camellia ... |
7 |
#include <asm/crypto/camellia.h> |
d9b1d2e7e crypto: camellia ... |
8 |
#include <asm/crypto/glue_helper.h> |
44893bc29 crypto: x86/camel... |
9 10 11 12 13 14 15 |
#include <crypto/algapi.h> #include <crypto/internal/simd.h> #include <crypto/xts.h> #include <linux/crypto.h> #include <linux/err.h> #include <linux/module.h> #include <linux/types.h> |
d9b1d2e7e crypto: camellia ... |
16 17 |
#define CAMELLIA_AESNI_PARALLEL_BLOCKS 16 |
f3f935a76 crypto: camellia ... |
18 |
/* 16-way parallel cipher functions (avx/aes-ni) */ |
9c1e8836e crypto: x86 - Reg... |
19 |
asmlinkage void camellia_ecb_enc_16way(const void *ctx, u8 *dst, const u8 *src); |
f3f935a76 crypto: camellia ... |
20 |
EXPORT_SYMBOL_GPL(camellia_ecb_enc_16way); |
9c1e8836e crypto: x86 - Reg... |
21 |
asmlinkage void camellia_ecb_dec_16way(const void *ctx, u8 *dst, const u8 *src); |
f3f935a76 crypto: camellia ... |
22 |
EXPORT_SYMBOL_GPL(camellia_ecb_dec_16way); |
d9b1d2e7e crypto: camellia ... |
23 |
|
9c1e8836e crypto: x86 - Reg... |
24 |
asmlinkage void camellia_cbc_dec_16way(const void *ctx, u8 *dst, const u8 *src); |
f3f935a76 crypto: camellia ... |
25 |
EXPORT_SYMBOL_GPL(camellia_cbc_dec_16way); |
9c1e8836e crypto: x86 - Reg... |
26 27 |
asmlinkage void camellia_ctr_16way(const void *ctx, u8 *dst, const u8 *src, le128 *iv); |
f3f935a76 crypto: camellia ... |
28 |
EXPORT_SYMBOL_GPL(camellia_ctr_16way); |
d9b1d2e7e crypto: camellia ... |
29 |
|
9c1e8836e crypto: x86 - Reg... |
30 31 |
asmlinkage void camellia_xts_enc_16way(const void *ctx, u8 *dst, const u8 *src, le128 *iv); |
f3f935a76 crypto: camellia ... |
32 |
EXPORT_SYMBOL_GPL(camellia_xts_enc_16way); |
9c1e8836e crypto: x86 - Reg... |
33 34 |
asmlinkage void camellia_xts_dec_16way(const void *ctx, u8 *dst, const u8 *src, le128 *iv); |
f3f935a76 crypto: camellia ... |
35 |
EXPORT_SYMBOL_GPL(camellia_xts_dec_16way); |
b5c5b072d crypto: x86/camel... |
36 |
|
9c1e8836e crypto: x86 - Reg... |
37 |
void camellia_xts_enc(const void *ctx, u8 *dst, const u8 *src, le128 *iv) |
b5c5b072d crypto: x86/camel... |
38 |
{ |
9c1e8836e crypto: x86 - Reg... |
39 |
glue_xts_crypt_128bit_one(ctx, dst, src, iv, camellia_enc_blk); |
b5c5b072d crypto: x86/camel... |
40 |
} |
f3f935a76 crypto: camellia ... |
41 |
EXPORT_SYMBOL_GPL(camellia_xts_enc); |
b5c5b072d crypto: x86/camel... |
42 |
|
9c1e8836e crypto: x86 - Reg... |
43 |
void camellia_xts_dec(const void *ctx, u8 *dst, const u8 *src, le128 *iv) |
b5c5b072d crypto: x86/camel... |
44 |
{ |
9c1e8836e crypto: x86 - Reg... |
45 |
glue_xts_crypt_128bit_one(ctx, dst, src, iv, camellia_dec_blk); |
b5c5b072d crypto: x86/camel... |
46 |
} |
f3f935a76 crypto: camellia ... |
47 |
EXPORT_SYMBOL_GPL(camellia_xts_dec); |
b5c5b072d crypto: x86/camel... |
48 |
|
d9b1d2e7e crypto: camellia ... |
49 50 51 52 53 54 |
static const struct common_glue_ctx camellia_enc = { .num_funcs = 3, .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS, .funcs = { { .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS, |
9c1e8836e crypto: x86 - Reg... |
55 |
.fn_u = { .ecb = camellia_ecb_enc_16way } |
d9b1d2e7e crypto: camellia ... |
56 57 |
}, { .num_blocks = 2, |
9c1e8836e crypto: x86 - Reg... |
58 |
.fn_u = { .ecb = camellia_enc_blk_2way } |
d9b1d2e7e crypto: camellia ... |
59 60 |
}, { .num_blocks = 1, |
9c1e8836e crypto: x86 - Reg... |
61 |
.fn_u = { .ecb = camellia_enc_blk } |
d9b1d2e7e crypto: camellia ... |
62 63 64 65 66 67 68 69 70 |
} } }; static const struct common_glue_ctx camellia_ctr = { .num_funcs = 3, .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS, .funcs = { { .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS, |
9c1e8836e crypto: x86 - Reg... |
71 |
.fn_u = { .ctr = camellia_ctr_16way } |
d9b1d2e7e crypto: camellia ... |
72 73 |
}, { .num_blocks = 2, |
9c1e8836e crypto: x86 - Reg... |
74 |
.fn_u = { .ctr = camellia_crypt_ctr_2way } |
d9b1d2e7e crypto: camellia ... |
75 76 |
}, { .num_blocks = 1, |
9c1e8836e crypto: x86 - Reg... |
77 |
.fn_u = { .ctr = camellia_crypt_ctr } |
d9b1d2e7e crypto: camellia ... |
78 79 |
} } }; |
b5c5b072d crypto: x86/camel... |
80 81 82 83 84 85 |
static const struct common_glue_ctx camellia_enc_xts = { .num_funcs = 2, .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS, .funcs = { { .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS, |
9c1e8836e crypto: x86 - Reg... |
86 |
.fn_u = { .xts = camellia_xts_enc_16way } |
b5c5b072d crypto: x86/camel... |
87 88 |
}, { .num_blocks = 1, |
9c1e8836e crypto: x86 - Reg... |
89 |
.fn_u = { .xts = camellia_xts_enc } |
b5c5b072d crypto: x86/camel... |
90 91 |
} } }; |
d9b1d2e7e crypto: camellia ... |
92 93 94 95 96 97 |
static const struct common_glue_ctx camellia_dec = { .num_funcs = 3, .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS, .funcs = { { .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS, |
9c1e8836e crypto: x86 - Reg... |
98 |
.fn_u = { .ecb = camellia_ecb_dec_16way } |
d9b1d2e7e crypto: camellia ... |
99 100 |
}, { .num_blocks = 2, |
9c1e8836e crypto: x86 - Reg... |
101 |
.fn_u = { .ecb = camellia_dec_blk_2way } |
d9b1d2e7e crypto: camellia ... |
102 103 |
}, { .num_blocks = 1, |
9c1e8836e crypto: x86 - Reg... |
104 |
.fn_u = { .ecb = camellia_dec_blk } |
d9b1d2e7e crypto: camellia ... |
105 106 107 108 109 110 111 112 113 |
} } }; static const struct common_glue_ctx camellia_dec_cbc = { .num_funcs = 3, .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS, .funcs = { { .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS, |
9c1e8836e crypto: x86 - Reg... |
114 |
.fn_u = { .cbc = camellia_cbc_dec_16way } |
d9b1d2e7e crypto: camellia ... |
115 116 |
}, { .num_blocks = 2, |
9c1e8836e crypto: x86 - Reg... |
117 |
.fn_u = { .cbc = camellia_decrypt_cbc_2way } |
d9b1d2e7e crypto: camellia ... |
118 119 |
}, { .num_blocks = 1, |
9c1e8836e crypto: x86 - Reg... |
120 |
.fn_u = { .cbc = camellia_dec_blk } |
d9b1d2e7e crypto: camellia ... |
121 122 |
} } }; |
b5c5b072d crypto: x86/camel... |
123 124 125 126 127 128 |
static const struct common_glue_ctx camellia_dec_xts = { .num_funcs = 2, .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS, .funcs = { { .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS, |
9c1e8836e crypto: x86 - Reg... |
129 |
.fn_u = { .xts = camellia_xts_dec_16way } |
b5c5b072d crypto: x86/camel... |
130 131 |
}, { .num_blocks = 1, |
9c1e8836e crypto: x86 - Reg... |
132 |
.fn_u = { .xts = camellia_xts_dec } |
b5c5b072d crypto: x86/camel... |
133 134 |
} } }; |
44893bc29 crypto: x86/camel... |
135 136 |
static int camellia_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen) |
d9b1d2e7e crypto: camellia ... |
137 |
{ |
674f368a9 crypto: remove CR... |
138 |
return __camellia_setkey(crypto_skcipher_ctx(tfm), key, keylen); |
d9b1d2e7e crypto: camellia ... |
139 |
} |
44893bc29 crypto: x86/camel... |
140 |
static int ecb_encrypt(struct skcipher_request *req) |
d9b1d2e7e crypto: camellia ... |
141 |
{ |
44893bc29 crypto: x86/camel... |
142 |
return glue_ecb_req_128bit(&camellia_enc, req); |
d9b1d2e7e crypto: camellia ... |
143 |
} |
44893bc29 crypto: x86/camel... |
144 |
static int ecb_decrypt(struct skcipher_request *req) |
d9b1d2e7e crypto: camellia ... |
145 |
{ |
44893bc29 crypto: x86/camel... |
146 |
return glue_ecb_req_128bit(&camellia_dec, req); |
d9b1d2e7e crypto: camellia ... |
147 |
} |
44893bc29 crypto: x86/camel... |
148 |
static int cbc_encrypt(struct skcipher_request *req) |
d9b1d2e7e crypto: camellia ... |
149 |
{ |
9c1e8836e crypto: x86 - Reg... |
150 |
return glue_cbc_encrypt_req_128bit(camellia_enc_blk, req); |
d9b1d2e7e crypto: camellia ... |
151 |
} |
44893bc29 crypto: x86/camel... |
152 |
static int cbc_decrypt(struct skcipher_request *req) |
d9b1d2e7e crypto: camellia ... |
153 |
{ |
44893bc29 crypto: x86/camel... |
154 |
return glue_cbc_decrypt_req_128bit(&camellia_dec_cbc, req); |
d9b1d2e7e crypto: camellia ... |
155 |
} |
44893bc29 crypto: x86/camel... |
156 |
static int ctr_crypt(struct skcipher_request *req) |
d9b1d2e7e crypto: camellia ... |
157 |
{ |
44893bc29 crypto: x86/camel... |
158 |
return glue_ctr_req_128bit(&camellia_ctr, req); |
d9b1d2e7e crypto: camellia ... |
159 |
} |
44893bc29 crypto: x86/camel... |
160 |
int xts_camellia_setkey(struct crypto_skcipher *tfm, const u8 *key, |
451cc4932 crypto: x86/camel... |
161 162 |
unsigned int keylen) { |
44893bc29 crypto: x86/camel... |
163 |
struct camellia_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
451cc4932 crypto: x86/camel... |
164 |
int err; |
44893bc29 crypto: x86/camel... |
165 |
err = xts_verify_key(tfm, key, keylen); |
451cc4932 crypto: x86/camel... |
166 167 168 169 |
if (err) return err; /* first half of xts-key is for crypt */ |
674f368a9 crypto: remove CR... |
170 |
err = __camellia_setkey(&ctx->crypt_ctx, key, keylen / 2); |
451cc4932 crypto: x86/camel... |
171 172 173 174 |
if (err) return err; /* second half of xts-key is for tweak */ |
674f368a9 crypto: remove CR... |
175 |
return __camellia_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2); |
451cc4932 crypto: x86/camel... |
176 177 |
} EXPORT_SYMBOL_GPL(xts_camellia_setkey); |
44893bc29 crypto: x86/camel... |
178 |
static int xts_encrypt(struct skcipher_request *req) |
d9b1d2e7e crypto: camellia ... |
179 |
{ |
44893bc29 crypto: x86/camel... |
180 181 |
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); struct camellia_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
d9b1d2e7e crypto: camellia ... |
182 |
|
9c1e8836e crypto: x86 - Reg... |
183 |
return glue_xts_req_128bit(&camellia_enc_xts, req, camellia_enc_blk, |
8ce5fac2d crypto: x86/xts -... |
184 |
&ctx->tweak_ctx, &ctx->crypt_ctx, false); |
d9b1d2e7e crypto: camellia ... |
185 |
} |
44893bc29 crypto: x86/camel... |
186 |
static int xts_decrypt(struct skcipher_request *req) |
d9b1d2e7e crypto: camellia ... |
187 |
{ |
44893bc29 crypto: x86/camel... |
188 189 |
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); struct camellia_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
d9b1d2e7e crypto: camellia ... |
190 |
|
9c1e8836e crypto: x86 - Reg... |
191 |
return glue_xts_req_128bit(&camellia_dec_xts, req, camellia_enc_blk, |
8ce5fac2d crypto: x86/xts -... |
192 |
&ctx->tweak_ctx, &ctx->crypt_ctx, true); |
d9b1d2e7e crypto: camellia ... |
193 |
} |
44893bc29 crypto: x86/camel... |
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
static struct skcipher_alg camellia_algs[] = { { .base.cra_name = "__ecb(camellia)", .base.cra_driver_name = "__ecb-camellia-aesni", .base.cra_priority = 400, .base.cra_flags = CRYPTO_ALG_INTERNAL, .base.cra_blocksize = CAMELLIA_BLOCK_SIZE, .base.cra_ctxsize = sizeof(struct camellia_ctx), .base.cra_module = THIS_MODULE, .min_keysize = CAMELLIA_MIN_KEY_SIZE, .max_keysize = CAMELLIA_MAX_KEY_SIZE, .setkey = camellia_setkey, .encrypt = ecb_encrypt, .decrypt = ecb_decrypt, }, { .base.cra_name = "__cbc(camellia)", .base.cra_driver_name = "__cbc-camellia-aesni", .base.cra_priority = 400, .base.cra_flags = CRYPTO_ALG_INTERNAL, .base.cra_blocksize = CAMELLIA_BLOCK_SIZE, .base.cra_ctxsize = sizeof(struct camellia_ctx), .base.cra_module = THIS_MODULE, .min_keysize = CAMELLIA_MIN_KEY_SIZE, .max_keysize = CAMELLIA_MAX_KEY_SIZE, .ivsize = CAMELLIA_BLOCK_SIZE, .setkey = camellia_setkey, .encrypt = cbc_encrypt, .decrypt = cbc_decrypt, }, { .base.cra_name = "__ctr(camellia)", .base.cra_driver_name = "__ctr-camellia-aesni", .base.cra_priority = 400, .base.cra_flags = CRYPTO_ALG_INTERNAL, .base.cra_blocksize = 1, .base.cra_ctxsize = sizeof(struct camellia_ctx), .base.cra_module = THIS_MODULE, .min_keysize = CAMELLIA_MIN_KEY_SIZE, .max_keysize = CAMELLIA_MAX_KEY_SIZE, .ivsize = CAMELLIA_BLOCK_SIZE, .chunksize = CAMELLIA_BLOCK_SIZE, .setkey = camellia_setkey, .encrypt = ctr_crypt, .decrypt = ctr_crypt, }, { .base.cra_name = "__xts(camellia)", .base.cra_driver_name = "__xts-camellia-aesni", .base.cra_priority = 400, .base.cra_flags = CRYPTO_ALG_INTERNAL, .base.cra_blocksize = CAMELLIA_BLOCK_SIZE, .base.cra_ctxsize = sizeof(struct camellia_xts_ctx), .base.cra_module = THIS_MODULE, .min_keysize = 2 * CAMELLIA_MIN_KEY_SIZE, .max_keysize = 2 * CAMELLIA_MAX_KEY_SIZE, .ivsize = CAMELLIA_BLOCK_SIZE, .setkey = xts_camellia_setkey, .encrypt = xts_encrypt, .decrypt = xts_decrypt, |
d9b1d2e7e crypto: camellia ... |
251 |
}, |
44893bc29 crypto: x86/camel... |
252 253 254 |
}; static struct simd_skcipher_alg *camellia_simd_algs[ARRAY_SIZE(camellia_algs)]; |
d9b1d2e7e crypto: camellia ... |
255 256 257 |
static int __init camellia_aesni_init(void) { |
ce4f5f9b6 x86/fpu, crypto x... |
258 |
const char *feature_name; |
d9b1d2e7e crypto: camellia ... |
259 |
|
da154e82a x86/cpufeature: R... |
260 |
if (!boot_cpu_has(X86_FEATURE_AVX) || |
1f4dd7938 x86/cpufeature: R... |
261 262 |
!boot_cpu_has(X86_FEATURE_AES) || !boot_cpu_has(X86_FEATURE_OSXSAVE)) { |
92b279070 crypto: camellia_... |
263 264 265 266 |
pr_info("AVX or AES-NI instructions are not detected. "); return -ENODEV; } |
d91cab781 x86/fpu: Rename X... |
267 268 |
if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, &feature_name)) { |
ce4f5f9b6 x86/fpu, crypto x... |
269 270 |
pr_info("CPU feature '%s' is not supported. ", feature_name); |
d9b1d2e7e crypto: camellia ... |
271 272 |
return -ENODEV; } |
44893bc29 crypto: x86/camel... |
273 274 275 |
return simd_register_skciphers_compat(camellia_algs, ARRAY_SIZE(camellia_algs), camellia_simd_algs); |
d9b1d2e7e crypto: camellia ... |
276 277 278 279 |
} static void __exit camellia_aesni_fini(void) { |
44893bc29 crypto: x86/camel... |
280 281 |
simd_unregister_skciphers(camellia_algs, ARRAY_SIZE(camellia_algs), camellia_simd_algs); |
d9b1d2e7e crypto: camellia ... |
282 283 284 285 286 287 288 |
} module_init(camellia_aesni_init); module_exit(camellia_aesni_fini); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Camellia Cipher Algorithm, AES-NI/AVX optimized"); |
5d26a105b crypto: prefix mo... |
289 290 |
MODULE_ALIAS_CRYPTO("camellia"); MODULE_ALIAS_CRYPTO("camellia-asm"); |