Skip to content

Commit 349d03f

Browse files
nefigtutherbertx
authored andcommitted
crypto: s390 - add crypto library interface for ChaCha20
Implement a crypto library interface for the s390-native ChaCha20 cipher algorithm. This allows us to stop to select CRYPTO_CHACHA20 and instead select CRYPTO_ARCH_HAVE_LIB_CHACHA. This allows BIG_KEYS=y not to build a whole ChaCha20 crypto infrastructure as a built-in, but build a smaller CRYPTO_LIB_CHACHA instead. Make CRYPTO_CHACHA_S390 config entry to look like similar ones on other architectures. Remove CRYPTO_ALGAPI select as anyway it is selected by CRYPTO_SKCIPHER. Add a new test module and a test script for ChaCha20 cipher and its interfaces. Here are test results on an idle z15 machine: Data | Generic crypto TFM | s390 crypto TFM | s390 lib size | enc dec | enc dec | enc dec -----+--------------------+------------------+---------------- 512b | 1545ns 1295ns | 604ns 446ns | 430ns 407ns 4k | 9536ns 9463ns | 2329ns 2174ns | 2170ns 2154ns 64k | 149.6us 149.3us | 34.4us 34.5us | 33.9us 33.1us 6M | 23.61ms 23.11ms | 4223us 4160us | 3951us 4008us 60M | 143.9ms 143.9ms | 33.5ms 33.2ms | 32.2ms 32.1ms Signed-off-by: Vladis Dronov <[email protected]> Reviewed-by: Harald Freudenberger <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 6ae7a8b commit 349d03f

File tree

5 files changed

+452
-4
lines changed

5 files changed

+452
-4
lines changed

arch/s390/crypto/chacha-glue.c

+32-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ static int chacha20_s390(struct skcipher_request *req)
6262
return rc;
6363
}
6464

65+
void hchacha_block_arch(const u32 *state, u32 *stream, int nrounds)
66+
{
67+
/* TODO: implement hchacha_block_arch() in assembly */
68+
hchacha_block_generic(state, stream, nrounds);
69+
}
70+
EXPORT_SYMBOL(hchacha_block_arch);
71+
72+
void chacha_init_arch(u32 *state, const u32 *key, const u8 *iv)
73+
{
74+
chacha_init_generic(state, key, iv);
75+
}
76+
EXPORT_SYMBOL(chacha_init_arch);
77+
78+
void chacha_crypt_arch(u32 *state, u8 *dst, const u8 *src,
79+
unsigned int bytes, int nrounds)
80+
{
81+
/* s390 chacha20 implementation has 20 rounds hard-coded,
82+
* it cannot handle a block of data or less, but otherwise
83+
* it can handle data of arbitrary size
84+
*/
85+
if (bytes <= CHACHA_BLOCK_SIZE || nrounds != 20)
86+
chacha_crypt_generic(state, dst, src, bytes, nrounds);
87+
else
88+
chacha20_crypt_s390(state, dst, src, bytes,
89+
&state[4], &state[12]);
90+
}
91+
EXPORT_SYMBOL(chacha_crypt_arch);
92+
6593
static struct skcipher_alg chacha_algs[] = {
6694
{
6795
.base.cra_name = "chacha20",
@@ -83,12 +111,14 @@ static struct skcipher_alg chacha_algs[] = {
83111

84112
static int __init chacha_mod_init(void)
85113
{
86-
return crypto_register_skciphers(chacha_algs, ARRAY_SIZE(chacha_algs));
114+
return IS_REACHABLE(CONFIG_CRYPTO_SKCIPHER) ?
115+
crypto_register_skciphers(chacha_algs, ARRAY_SIZE(chacha_algs)) : 0;
87116
}
88117

89118
static void __exit chacha_mod_fini(void)
90119
{
91-
crypto_unregister_skciphers(chacha_algs, ARRAY_SIZE(chacha_algs));
120+
if (IS_REACHABLE(CONFIG_CRYPTO_SKCIPHER))
121+
crypto_unregister_skciphers(chacha_algs, ARRAY_SIZE(chacha_algs));
92122
}
93123

94124
module_cpu_feature_match(VXRS, chacha_mod_init);

drivers/crypto/Kconfig

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ config CRYPTO_AES_S390
216216
config CRYPTO_CHACHA_S390
217217
tristate "ChaCha20 stream cipher"
218218
depends on S390
219-
select CRYPTO_ALGAPI
220219
select CRYPTO_SKCIPHER
221-
select CRYPTO_CHACHA20
220+
select CRYPTO_LIB_CHACHA_GENERIC
221+
select CRYPTO_ARCH_HAVE_LIB_CHACHA
222222
help
223223
This is the s390 SIMD implementation of the ChaCha20 stream
224224
cipher (RFC 7539).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
#
3+
# Copyright (C) 2022 Red Hat, Inc.
4+
# Author: Vladis Dronov <[email protected]>
5+
6+
obj-m += test_cipher.o
7+
test_cipher-y := test-cipher.o
8+
9+
all:
10+
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
11+
clean:
12+
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
#
4+
# Copyright (C) 2022 Red Hat, Inc.
5+
# Author: Vladis Dronov <[email protected]>
6+
#
7+
# This script runs (via instmod) test-cipher.ko module which invokes
8+
# generic and s390-native ChaCha20 encryprion algorithms with different
9+
# size of data. Check 'dmesg' for results.
10+
#
11+
# The insmod error is expected:
12+
# insmod: ERROR: could not insert module test_cipher.ko: Operation not permitted
13+
14+
lsmod | grep chacha | cut -f1 -d' ' | xargs rmmod
15+
modprobe chacha_generic
16+
modprobe chacha_s390
17+
18+
# run encryption for different data size, including whole block(s) +/- 1
19+
insmod test_cipher.ko size=63
20+
insmod test_cipher.ko size=64
21+
insmod test_cipher.ko size=65
22+
insmod test_cipher.ko size=127
23+
insmod test_cipher.ko size=128
24+
insmod test_cipher.ko size=129
25+
insmod test_cipher.ko size=511
26+
insmod test_cipher.ko size=512
27+
insmod test_cipher.ko size=513
28+
insmod test_cipher.ko size=4096
29+
insmod test_cipher.ko size=65611
30+
insmod test_cipher.ko size=6291456
31+
insmod test_cipher.ko size=62914560
32+
33+
# print test logs
34+
dmesg | tail -170

0 commit comments

Comments
 (0)