Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

crypto: chacha - register only "-lib" drivers

For the "chacha20", "xchacha20", and "xchacha12" skcipher algorithms,
instead of registering "*-generic" drivers as well as conditionally
registering "*-$(ARCH)" drivers, instead just register "*-lib" drivers.
These just use the regular library functions, so they just do the right
thing and are fully accelerated when supported by the CPU.

This eliminates the need for the ChaCha library to support
chacha_crypt_generic() and hchacha_block_generic() as part of its
external interface. A later commit will make chacha_crypt_generic() a
static function.

Since this commit removes several "*-generic" driver names which
crypto/testmgr.c expects to exist, update testmgr.c accordingly.

Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20250827151131.27733-3-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>

+29 -110
-1
crypto/Kconfig
··· 648 648 config CRYPTO_CHACHA20 649 649 tristate "ChaCha" 650 650 select CRYPTO_LIB_CHACHA 651 - select CRYPTO_LIB_CHACHA_GENERIC 652 651 select CRYPTO_SKCIPHER 653 652 help 654 653 The ChaCha20, XChaCha20, and XChaCha12 stream cipher algorithms
+22 -107
crypto/chacha.c
··· 47 47 48 48 static int chacha_stream_xor(struct skcipher_request *req, 49 49 const struct chacha_ctx *ctx, 50 - const u8 iv[CHACHA_IV_SIZE], bool arch) 50 + const u8 iv[CHACHA_IV_SIZE]) 51 51 { 52 52 struct skcipher_walk walk; 53 53 struct chacha_state state; ··· 63 63 if (nbytes < walk.total) 64 64 nbytes = round_down(nbytes, CHACHA_BLOCK_SIZE); 65 65 66 - if (arch) 67 - chacha_crypt(&state, walk.dst.virt.addr, 68 - walk.src.virt.addr, nbytes, ctx->nrounds); 69 - else 70 - chacha_crypt_generic(&state, walk.dst.virt.addr, 71 - walk.src.virt.addr, nbytes, 72 - ctx->nrounds); 66 + chacha_crypt(&state, walk.dst.virt.addr, walk.src.virt.addr, 67 + nbytes, ctx->nrounds); 73 68 err = skcipher_walk_done(&walk, walk.nbytes - nbytes); 74 69 } 75 70 76 71 return err; 77 72 } 78 73 79 - static int crypto_chacha_crypt_generic(struct skcipher_request *req) 74 + static int crypto_chacha_crypt(struct skcipher_request *req) 80 75 { 81 76 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 82 77 const struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm); 83 78 84 - return chacha_stream_xor(req, ctx, req->iv, false); 79 + return chacha_stream_xor(req, ctx, req->iv); 85 80 } 86 81 87 - static int crypto_chacha_crypt_arch(struct skcipher_request *req) 88 - { 89 - struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 90 - const struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm); 91 - 92 - return chacha_stream_xor(req, ctx, req->iv, true); 93 - } 94 - 95 - static int crypto_xchacha_crypt(struct skcipher_request *req, bool arch) 82 + static int crypto_xchacha_crypt(struct skcipher_request *req) 96 83 { 97 84 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 98 85 const struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm); ··· 89 102 90 103 /* Compute the subkey given the original key and first 128 nonce bits */ 91 104 chacha_init(&state, ctx->key, req->iv); 92 - if (arch) 93 - hchacha_block(&state, subctx.key, ctx->nrounds); 94 - else 95 - hchacha_block_generic(&state, subctx.key, ctx->nrounds); 105 + hchacha_block(&state, subctx.key, ctx->nrounds); 96 106 subctx.nrounds = ctx->nrounds; 97 107 98 108 /* Build the real IV */ ··· 97 113 memcpy(&real_iv[8], req->iv + 16, 8); /* remaining 64 nonce bits */ 98 114 99 115 /* Generate the stream and XOR it with the data */ 100 - return chacha_stream_xor(req, &subctx, real_iv, arch); 101 - } 102 - 103 - static int crypto_xchacha_crypt_generic(struct skcipher_request *req) 104 - { 105 - return crypto_xchacha_crypt(req, false); 106 - } 107 - 108 - static int crypto_xchacha_crypt_arch(struct skcipher_request *req) 109 - { 110 - return crypto_xchacha_crypt(req, true); 116 + return chacha_stream_xor(req, &subctx, real_iv); 111 117 } 112 118 113 119 static struct skcipher_alg algs[] = { 114 120 { 115 121 .base.cra_name = "chacha20", 116 - .base.cra_driver_name = "chacha20-generic", 117 - .base.cra_priority = 100, 118 - .base.cra_blocksize = 1, 119 - .base.cra_ctxsize = sizeof(struct chacha_ctx), 120 - .base.cra_module = THIS_MODULE, 121 - 122 - .min_keysize = CHACHA_KEY_SIZE, 123 - .max_keysize = CHACHA_KEY_SIZE, 124 - .ivsize = CHACHA_IV_SIZE, 125 - .chunksize = CHACHA_BLOCK_SIZE, 126 - .setkey = chacha20_setkey, 127 - .encrypt = crypto_chacha_crypt_generic, 128 - .decrypt = crypto_chacha_crypt_generic, 129 - }, 130 - { 131 - .base.cra_name = "xchacha20", 132 - .base.cra_driver_name = "xchacha20-generic", 133 - .base.cra_priority = 100, 134 - .base.cra_blocksize = 1, 135 - .base.cra_ctxsize = sizeof(struct chacha_ctx), 136 - .base.cra_module = THIS_MODULE, 137 - 138 - .min_keysize = CHACHA_KEY_SIZE, 139 - .max_keysize = CHACHA_KEY_SIZE, 140 - .ivsize = XCHACHA_IV_SIZE, 141 - .chunksize = CHACHA_BLOCK_SIZE, 142 - .setkey = chacha20_setkey, 143 - .encrypt = crypto_xchacha_crypt_generic, 144 - .decrypt = crypto_xchacha_crypt_generic, 145 - }, 146 - { 147 - .base.cra_name = "xchacha12", 148 - .base.cra_driver_name = "xchacha12-generic", 149 - .base.cra_priority = 100, 150 - .base.cra_blocksize = 1, 151 - .base.cra_ctxsize = sizeof(struct chacha_ctx), 152 - .base.cra_module = THIS_MODULE, 153 - 154 - .min_keysize = CHACHA_KEY_SIZE, 155 - .max_keysize = CHACHA_KEY_SIZE, 156 - .ivsize = XCHACHA_IV_SIZE, 157 - .chunksize = CHACHA_BLOCK_SIZE, 158 - .setkey = chacha12_setkey, 159 - .encrypt = crypto_xchacha_crypt_generic, 160 - .decrypt = crypto_xchacha_crypt_generic, 161 - }, 162 - { 163 - .base.cra_name = "chacha20", 164 - .base.cra_driver_name = "chacha20-" __stringify(ARCH), 122 + .base.cra_driver_name = "chacha20-lib", 165 123 .base.cra_priority = 300, 166 124 .base.cra_blocksize = 1, 167 125 .base.cra_ctxsize = sizeof(struct chacha_ctx), ··· 114 188 .ivsize = CHACHA_IV_SIZE, 115 189 .chunksize = CHACHA_BLOCK_SIZE, 116 190 .setkey = chacha20_setkey, 117 - .encrypt = crypto_chacha_crypt_arch, 118 - .decrypt = crypto_chacha_crypt_arch, 191 + .encrypt = crypto_chacha_crypt, 192 + .decrypt = crypto_chacha_crypt, 119 193 }, 120 194 { 121 195 .base.cra_name = "xchacha20", 122 - .base.cra_driver_name = "xchacha20-" __stringify(ARCH), 196 + .base.cra_driver_name = "xchacha20-lib", 123 197 .base.cra_priority = 300, 124 198 .base.cra_blocksize = 1, 125 199 .base.cra_ctxsize = sizeof(struct chacha_ctx), ··· 130 204 .ivsize = XCHACHA_IV_SIZE, 131 205 .chunksize = CHACHA_BLOCK_SIZE, 132 206 .setkey = chacha20_setkey, 133 - .encrypt = crypto_xchacha_crypt_arch, 134 - .decrypt = crypto_xchacha_crypt_arch, 207 + .encrypt = crypto_xchacha_crypt, 208 + .decrypt = crypto_xchacha_crypt, 135 209 }, 136 210 { 137 211 .base.cra_name = "xchacha12", 138 - .base.cra_driver_name = "xchacha12-" __stringify(ARCH), 212 + .base.cra_driver_name = "xchacha12-lib", 139 213 .base.cra_priority = 300, 140 214 .base.cra_blocksize = 1, 141 215 .base.cra_ctxsize = sizeof(struct chacha_ctx), ··· 146 220 .ivsize = XCHACHA_IV_SIZE, 147 221 .chunksize = CHACHA_BLOCK_SIZE, 148 222 .setkey = chacha12_setkey, 149 - .encrypt = crypto_xchacha_crypt_arch, 150 - .decrypt = crypto_xchacha_crypt_arch, 223 + .encrypt = crypto_xchacha_crypt, 224 + .decrypt = crypto_xchacha_crypt, 151 225 } 152 226 }; 153 227 154 - static unsigned int num_algs; 155 - 156 228 static int __init crypto_chacha_mod_init(void) 157 229 { 158 - /* register the arch flavours only if they differ from generic */ 159 - num_algs = ARRAY_SIZE(algs); 160 - BUILD_BUG_ON(ARRAY_SIZE(algs) % 2 != 0); 161 - if (!chacha_is_arch_optimized()) 162 - num_algs /= 2; 163 - 164 - return crypto_register_skciphers(algs, num_algs); 230 + return crypto_register_skciphers(algs, ARRAY_SIZE(algs)); 165 231 } 166 232 167 233 static void __exit crypto_chacha_mod_fini(void) 168 234 { 169 - crypto_unregister_skciphers(algs, num_algs); 235 + crypto_unregister_skciphers(algs, ARRAY_SIZE(algs)); 170 236 } 171 237 172 238 module_init(crypto_chacha_mod_init); ··· 168 250 MODULE_AUTHOR("Martin Willi <martin@strongswan.org>"); 169 251 MODULE_DESCRIPTION("Crypto API wrappers for the ChaCha20, XChaCha20, and XChaCha12 stream ciphers"); 170 252 MODULE_ALIAS_CRYPTO("chacha20"); 171 - MODULE_ALIAS_CRYPTO("chacha20-generic"); 172 - MODULE_ALIAS_CRYPTO("chacha20-" __stringify(ARCH)); 253 + MODULE_ALIAS_CRYPTO("chacha20-lib"); 173 254 MODULE_ALIAS_CRYPTO("xchacha20"); 174 - MODULE_ALIAS_CRYPTO("xchacha20-generic"); 175 - MODULE_ALIAS_CRYPTO("xchacha20-" __stringify(ARCH)); 255 + MODULE_ALIAS_CRYPTO("xchacha20-lib"); 176 256 MODULE_ALIAS_CRYPTO("xchacha12"); 177 - MODULE_ALIAS_CRYPTO("xchacha12-generic"); 178 - MODULE_ALIAS_CRYPTO("xchacha12-" __stringify(ARCH)); 257 + MODULE_ALIAS_CRYPTO("xchacha12-lib");
+7 -2
crypto/testmgr.c
··· 4152 4152 static const struct alg_test_desc alg_test_descs[] = { 4153 4153 { 4154 4154 .alg = "adiantum(xchacha12,aes)", 4155 - .generic_driver = "adiantum(xchacha12-generic,aes-generic,nhpoly1305-generic)", 4155 + .generic_driver = "adiantum(xchacha12-lib,aes-generic,nhpoly1305-generic)", 4156 4156 .test = alg_test_skcipher, 4157 4157 .suite = { 4158 4158 .cipher = __VECS(adiantum_xchacha12_aes_tv_template) 4159 4159 }, 4160 4160 }, { 4161 4161 .alg = "adiantum(xchacha20,aes)", 4162 - .generic_driver = "adiantum(xchacha20-generic,aes-generic,nhpoly1305-generic)", 4162 + .generic_driver = "adiantum(xchacha20-lib,aes-generic,nhpoly1305-generic)", 4163 4163 .test = alg_test_skcipher, 4164 4164 .suite = { 4165 4165 .cipher = __VECS(adiantum_xchacha20_aes_tv_template) ··· 4485 4485 } 4486 4486 }, { 4487 4487 .alg = "chacha20", 4488 + .generic_driver = "chacha20-lib", 4488 4489 .test = alg_test_skcipher, 4489 4490 .suite = { 4490 4491 .cipher = __VECS(chacha20_tv_template) ··· 5421 5420 } 5422 5421 }, { 5423 5422 .alg = "rfc7539(chacha20,poly1305)", 5423 + .generic_driver = "rfc7539(chacha20-lib,poly1305-generic)", 5424 5424 .test = alg_test_aead, 5425 5425 .suite = { 5426 5426 .aead = __VECS(rfc7539_tv_template) 5427 5427 } 5428 5428 }, { 5429 5429 .alg = "rfc7539esp(chacha20,poly1305)", 5430 + .generic_driver = "rfc7539esp(chacha20-lib,poly1305-generic)", 5430 5431 .test = alg_test_aead, 5431 5432 .suite = { 5432 5433 .aead = { ··· 5594 5591 } 5595 5592 }, { 5596 5593 .alg = "xchacha12", 5594 + .generic_driver = "xchacha12-lib", 5597 5595 .test = alg_test_skcipher, 5598 5596 .suite = { 5599 5597 .cipher = __VECS(xchacha12_tv_template) 5600 5598 }, 5601 5599 }, { 5602 5600 .alg = "xchacha20", 5601 + .generic_driver = "xchacha20-lib", 5603 5602 .test = alg_test_skcipher, 5604 5603 .suite = { 5605 5604 .cipher = __VECS(xchacha20_tv_template)