Skip to content

Commit 44893bc

Browse files
ebiggersherbertx
authored andcommitted
crypto: x86/camellia-aesni-avx, avx2 - convert to skcipher interface
Convert the AESNI AVX and AESNI AVX2 implementations of Camellia from the (deprecated) ablkcipher and blkcipher interfaces over to the skcipher interface. Note that this includes replacing the use of ablk_helper with crypto_simd. Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 1af6d03 commit 44893bc

4 files changed

Lines changed: 208 additions & 443 deletions

File tree

‎arch/x86/crypto/camellia_aesni_avx2_glue.c‎

Lines changed: 99 additions & 214 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@
1010
*
1111
*/
1212

13-
#include <linux/module.h>
14-
#include <linux/types.h>
15-
#include <linux/crypto.h>
16-
#include <linux/err.h>
17-
#include <crypto/ablk_helper.h>
18-
#include <crypto/algapi.h>
19-
#include <crypto/ctr.h>
20-
#include <crypto/xts.h>
21-
#include <asm/fpu/api.h>
2213
#include <asm/crypto/camellia.h>
2314
#include <asm/crypto/glue_helper.h>
15+
#include <crypto/algapi.h>
16+
#include <crypto/internal/simd.h>
17+
#include <crypto/xts.h>
18+
#include <linux/crypto.h>
19+
#include <linux/err.h>
20+
#include <linux/module.h>
21+
#include <linux/types.h>
2422

2523
#define CAMELLIA_AESNI_PARALLEL_BLOCKS 16
2624
#define CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS 32
@@ -149,236 +147,120 @@ static const struct common_glue_ctx camellia_dec_xts = {
149147
} }
150148
};
151149

152-
static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
153-
struct scatterlist *src, unsigned int nbytes)
150+
static int camellia_setkey(struct crypto_skcipher *tfm, const u8 *key,
151+
unsigned int keylen)
154152
{
155-
return glue_ecb_crypt_128bit(&camellia_enc, desc, dst, src, nbytes);
153+
return __camellia_setkey(crypto_skcipher_ctx(tfm), key, keylen,
154+
&tfm->base.crt_flags);
156155
}
157156

158-
static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
159-
struct scatterlist *src, unsigned int nbytes)
157+
static int ecb_encrypt(struct skcipher_request *req)
160158
{
161-
return glue_ecb_crypt_128bit(&camellia_dec, desc, dst, src, nbytes);
159+
return glue_ecb_req_128bit(&camellia_enc, req);
162160
}
163161

164-
static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
165-
struct scatterlist *src, unsigned int nbytes)
162+
static int ecb_decrypt(struct skcipher_request *req)
166163
{
167-
return glue_cbc_encrypt_128bit(GLUE_FUNC_CAST(camellia_enc_blk), desc,
168-
dst, src, nbytes);
164+
return glue_ecb_req_128bit(&camellia_dec, req);
169165
}
170166

171-
static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
172-
struct scatterlist *src, unsigned int nbytes)
167+
static int cbc_encrypt(struct skcipher_request *req)
173168
{
174-
return glue_cbc_decrypt_128bit(&camellia_dec_cbc, desc, dst, src,
175-
nbytes);
169+
return glue_cbc_encrypt_req_128bit(GLUE_FUNC_CAST(camellia_enc_blk),
170+
req);
176171
}
177172

178-
static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
179-
struct scatterlist *src, unsigned int nbytes)
173+
static int cbc_decrypt(struct skcipher_request *req)
180174
{
181-
return glue_ctr_crypt_128bit(&camellia_ctr, desc, dst, src, nbytes);
175+
return glue_cbc_decrypt_req_128bit(&camellia_dec_cbc, req);
182176
}
183177

184-
static int camellia_setkey(struct crypto_tfm *tfm, const u8 *in_key,
185-
unsigned int key_len)
178+
static int ctr_crypt(struct skcipher_request *req)
186179
{
187-
return __camellia_setkey(crypto_tfm_ctx(tfm), in_key, key_len,
188-
&tfm->crt_flags);
180+
return glue_ctr_req_128bit(&camellia_ctr, req);
189181
}
190182

191-
static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
192-
struct scatterlist *src, unsigned int nbytes)
183+
static int xts_encrypt(struct skcipher_request *req)
193184
{
194-
struct camellia_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
185+
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
186+
struct camellia_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
195187

196-
return glue_xts_crypt_128bit(&camellia_enc_xts, desc, dst, src, nbytes,
197-
XTS_TWEAK_CAST(camellia_enc_blk),
198-
&ctx->tweak_ctx, &ctx->crypt_ctx);
188+
return glue_xts_req_128bit(&camellia_enc_xts, req,
189+
XTS_TWEAK_CAST(camellia_enc_blk),
190+
&ctx->tweak_ctx, &ctx->crypt_ctx);
199191
}
200192

201-
static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
202-
struct scatterlist *src, unsigned int nbytes)
193+
static int xts_decrypt(struct skcipher_request *req)
203194
{
204-
struct camellia_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
195+
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
196+
struct camellia_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
205197

206-
return glue_xts_crypt_128bit(&camellia_dec_xts, desc, dst, src, nbytes,
207-
XTS_TWEAK_CAST(camellia_enc_blk),
208-
&ctx->tweak_ctx, &ctx->crypt_ctx);
198+
return glue_xts_req_128bit(&camellia_dec_xts, req,
199+
XTS_TWEAK_CAST(camellia_enc_blk),
200+
&ctx->tweak_ctx, &ctx->crypt_ctx);
209201
}
210202

211-
static struct crypto_alg cmll_algs[] = { {
212-
.cra_name = "__ecb-camellia-aesni-avx2",
213-
.cra_driver_name = "__driver-ecb-camellia-aesni-avx2",
214-
.cra_priority = 0,
215-
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
216-
CRYPTO_ALG_INTERNAL,
217-
.cra_blocksize = CAMELLIA_BLOCK_SIZE,
218-
.cra_ctxsize = sizeof(struct camellia_ctx),
219-
.cra_alignmask = 0,
220-
.cra_type = &crypto_blkcipher_type,
221-
.cra_module = THIS_MODULE,
222-
.cra_u = {
223-
.blkcipher = {
224-
.min_keysize = CAMELLIA_MIN_KEY_SIZE,
225-
.max_keysize = CAMELLIA_MAX_KEY_SIZE,
226-
.setkey = camellia_setkey,
227-
.encrypt = ecb_encrypt,
228-
.decrypt = ecb_decrypt,
229-
},
230-
},
231-
}, {
232-
.cra_name = "__cbc-camellia-aesni-avx2",
233-
.cra_driver_name = "__driver-cbc-camellia-aesni-avx2",
234-
.cra_priority = 0,
235-
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
236-
CRYPTO_ALG_INTERNAL,
237-
.cra_blocksize = CAMELLIA_BLOCK_SIZE,
238-
.cra_ctxsize = sizeof(struct camellia_ctx),
239-
.cra_alignmask = 0,
240-
.cra_type = &crypto_blkcipher_type,
241-
.cra_module = THIS_MODULE,
242-
.cra_u = {
243-
.blkcipher = {
244-
.min_keysize = CAMELLIA_MIN_KEY_SIZE,
245-
.max_keysize = CAMELLIA_MAX_KEY_SIZE,
246-
.setkey = camellia_setkey,
247-
.encrypt = cbc_encrypt,
248-
.decrypt = cbc_decrypt,
249-
},
250-
},
251-
}, {
252-
.cra_name = "__ctr-camellia-aesni-avx2",
253-
.cra_driver_name = "__driver-ctr-camellia-aesni-avx2",
254-
.cra_priority = 0,
255-
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
256-
CRYPTO_ALG_INTERNAL,
257-
.cra_blocksize = 1,
258-
.cra_ctxsize = sizeof(struct camellia_ctx),
259-
.cra_alignmask = 0,
260-
.cra_type = &crypto_blkcipher_type,
261-
.cra_module = THIS_MODULE,
262-
.cra_u = {
263-
.blkcipher = {
264-
.min_keysize = CAMELLIA_MIN_KEY_SIZE,
265-
.max_keysize = CAMELLIA_MAX_KEY_SIZE,
266-
.ivsize = CAMELLIA_BLOCK_SIZE,
267-
.setkey = camellia_setkey,
268-
.encrypt = ctr_crypt,
269-
.decrypt = ctr_crypt,
270-
},
271-
},
272-
}, {
273-
.cra_name = "__xts-camellia-aesni-avx2",
274-
.cra_driver_name = "__driver-xts-camellia-aesni-avx2",
275-
.cra_priority = 0,
276-
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
277-
CRYPTO_ALG_INTERNAL,
278-
.cra_blocksize = CAMELLIA_BLOCK_SIZE,
279-
.cra_ctxsize = sizeof(struct camellia_xts_ctx),
280-
.cra_alignmask = 0,
281-
.cra_type = &crypto_blkcipher_type,
282-
.cra_module = THIS_MODULE,
283-
.cra_u = {
284-
.blkcipher = {
285-
.min_keysize = CAMELLIA_MIN_KEY_SIZE * 2,
286-
.max_keysize = CAMELLIA_MAX_KEY_SIZE * 2,
287-
.ivsize = CAMELLIA_BLOCK_SIZE,
288-
.setkey = xts_camellia_setkey,
289-
.encrypt = xts_encrypt,
290-
.decrypt = xts_decrypt,
291-
},
292-
},
293-
}, {
294-
.cra_name = "ecb(camellia)",
295-
.cra_driver_name = "ecb-camellia-aesni-avx2",
296-
.cra_priority = 500,
297-
.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
298-
.cra_blocksize = CAMELLIA_BLOCK_SIZE,
299-
.cra_ctxsize = sizeof(struct async_helper_ctx),
300-
.cra_alignmask = 0,
301-
.cra_type = &crypto_ablkcipher_type,
302-
.cra_module = THIS_MODULE,
303-
.cra_init = ablk_init,
304-
.cra_exit = ablk_exit,
305-
.cra_u = {
306-
.ablkcipher = {
307-
.min_keysize = CAMELLIA_MIN_KEY_SIZE,
308-
.max_keysize = CAMELLIA_MAX_KEY_SIZE,
309-
.setkey = ablk_set_key,
310-
.encrypt = ablk_encrypt,
311-
.decrypt = ablk_decrypt,
312-
},
313-
},
314-
}, {
315-
.cra_name = "cbc(camellia)",
316-
.cra_driver_name = "cbc-camellia-aesni-avx2",
317-
.cra_priority = 500,
318-
.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
319-
.cra_blocksize = CAMELLIA_BLOCK_SIZE,
320-
.cra_ctxsize = sizeof(struct async_helper_ctx),
321-
.cra_alignmask = 0,
322-
.cra_type = &crypto_ablkcipher_type,
323-
.cra_module = THIS_MODULE,
324-
.cra_init = ablk_init,
325-
.cra_exit = ablk_exit,
326-
.cra_u = {
327-
.ablkcipher = {
328-
.min_keysize = CAMELLIA_MIN_KEY_SIZE,
329-
.max_keysize = CAMELLIA_MAX_KEY_SIZE,
330-
.ivsize = CAMELLIA_BLOCK_SIZE,
331-
.setkey = ablk_set_key,
332-
.encrypt = __ablk_encrypt,
333-
.decrypt = ablk_decrypt,
334-
},
335-
},
336-
}, {
337-
.cra_name = "ctr(camellia)",
338-
.cra_driver_name = "ctr-camellia-aesni-avx2",
339-
.cra_priority = 500,
340-
.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
341-
.cra_blocksize = 1,
342-
.cra_ctxsize = sizeof(struct async_helper_ctx),
343-
.cra_alignmask = 0,
344-
.cra_type = &crypto_ablkcipher_type,
345-
.cra_module = THIS_MODULE,
346-
.cra_init = ablk_init,
347-
.cra_exit = ablk_exit,
348-
.cra_u = {
349-
.ablkcipher = {
350-
.min_keysize = CAMELLIA_MIN_KEY_SIZE,
351-
.max_keysize = CAMELLIA_MAX_KEY_SIZE,
352-
.ivsize = CAMELLIA_BLOCK_SIZE,
353-
.setkey = ablk_set_key,
354-
.encrypt = ablk_encrypt,
355-
.decrypt = ablk_encrypt,
356-
.geniv = "chainiv",
357-
},
358-
},
359-
}, {
360-
.cra_name = "xts(camellia)",
361-
.cra_driver_name = "xts-camellia-aesni-avx2",
362-
.cra_priority = 500,
363-
.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
364-
.cra_blocksize = CAMELLIA_BLOCK_SIZE,
365-
.cra_ctxsize = sizeof(struct async_helper_ctx),
366-
.cra_alignmask = 0,
367-
.cra_type = &crypto_ablkcipher_type,
368-
.cra_module = THIS_MODULE,
369-
.cra_init = ablk_init,
370-
.cra_exit = ablk_exit,
371-
.cra_u = {
372-
.ablkcipher = {
373-
.min_keysize = CAMELLIA_MIN_KEY_SIZE * 2,
374-
.max_keysize = CAMELLIA_MAX_KEY_SIZE * 2,
375-
.ivsize = CAMELLIA_BLOCK_SIZE,
376-
.setkey = ablk_set_key,
377-
.encrypt = ablk_encrypt,
378-
.decrypt = ablk_decrypt,
379-
},
203+
static struct skcipher_alg camellia_algs[] = {
204+
{
205+
.base.cra_name = "__ecb(camellia)",
206+
.base.cra_driver_name = "__ecb-camellia-aesni-avx2",
207+
.base.cra_priority = 500,
208+
.base.cra_flags = CRYPTO_ALG_INTERNAL,
209+
.base.cra_blocksize = CAMELLIA_BLOCK_SIZE,
210+
.base.cra_ctxsize = sizeof(struct camellia_ctx),
211+
.base.cra_module = THIS_MODULE,
212+
.min_keysize = CAMELLIA_MIN_KEY_SIZE,
213+
.max_keysize = CAMELLIA_MAX_KEY_SIZE,
214+
.setkey = camellia_setkey,
215+
.encrypt = ecb_encrypt,
216+
.decrypt = ecb_decrypt,
217+
}, {
218+
.base.cra_name = "__cbc(camellia)",
219+
.base.cra_driver_name = "__cbc-camellia-aesni-avx2",
220+
.base.cra_priority = 500,
221+
.base.cra_flags = CRYPTO_ALG_INTERNAL,
222+
.base.cra_blocksize = CAMELLIA_BLOCK_SIZE,
223+
.base.cra_ctxsize = sizeof(struct camellia_ctx),
224+
.base.cra_module = THIS_MODULE,
225+
.min_keysize = CAMELLIA_MIN_KEY_SIZE,
226+
.max_keysize = CAMELLIA_MAX_KEY_SIZE,
227+
.ivsize = CAMELLIA_BLOCK_SIZE,
228+
.setkey = camellia_setkey,
229+
.encrypt = cbc_encrypt,
230+
.decrypt = cbc_decrypt,
231+
}, {
232+
.base.cra_name = "__ctr(camellia)",
233+
.base.cra_driver_name = "__ctr-camellia-aesni-avx2",
234+
.base.cra_priority = 500,
235+
.base.cra_flags = CRYPTO_ALG_INTERNAL,
236+
.base.cra_blocksize = 1,
237+
.base.cra_ctxsize = sizeof(struct camellia_ctx),
238+
.base.cra_module = THIS_MODULE,
239+
.min_keysize = CAMELLIA_MIN_KEY_SIZE,
240+
.max_keysize = CAMELLIA_MAX_KEY_SIZE,
241+
.ivsize = CAMELLIA_BLOCK_SIZE,
242+
.chunksize = CAMELLIA_BLOCK_SIZE,
243+
.setkey = camellia_setkey,
244+
.encrypt = ctr_crypt,
245+
.decrypt = ctr_crypt,
246+
}, {
247+
.base.cra_name = "__xts(camellia)",
248+
.base.cra_driver_name = "__xts-camellia-aesni-avx2",
249+
.base.cra_priority = 500,
250+
.base.cra_flags = CRYPTO_ALG_INTERNAL,
251+
.base.cra_blocksize = CAMELLIA_BLOCK_SIZE,
252+
.base.cra_ctxsize = sizeof(struct camellia_xts_ctx),
253+
.base.cra_module = THIS_MODULE,
254+
.min_keysize = 2 * CAMELLIA_MIN_KEY_SIZE,
255+
.max_keysize = 2 * CAMELLIA_MAX_KEY_SIZE,
256+
.ivsize = CAMELLIA_BLOCK_SIZE,
257+
.setkey = xts_camellia_setkey,
258+
.encrypt = xts_encrypt,
259+
.decrypt = xts_decrypt,
380260
},
381-
} };
261+
};
262+
263+
static struct simd_skcipher_alg *camellia_simd_algs[ARRAY_SIZE(camellia_algs)];
382264

383265
static int __init camellia_aesni_init(void)
384266
{
@@ -398,12 +280,15 @@ static int __init camellia_aesni_init(void)
398280
return -ENODEV;
399281
}
400282

401-
return crypto_register_algs(cmll_algs, ARRAY_SIZE(cmll_algs));
283+
return simd_register_skciphers_compat(camellia_algs,
284+
ARRAY_SIZE(camellia_algs),
285+
camellia_simd_algs);
402286
}
403287

404288
static void __exit camellia_aesni_fini(void)
405289
{
406-
crypto_unregister_algs(cmll_algs, ARRAY_SIZE(cmll_algs));
290+
simd_unregister_skciphers(camellia_algs, ARRAY_SIZE(camellia_algs),
291+
camellia_simd_algs);
407292
}
408293

409294
module_init(camellia_aesni_init);

0 commit comments

Comments
 (0)