Skip to contentSkip to conversation

crypto: lib/chachapoly - Drop dependency on CRYPTO_ALGAPI

PATCH In review Ard Biesheuvel 2 participants 2 messages Feb 28, 2025 – Mar 8, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 crypto: lib/chachapoly - Drop dependency on CRYPTO_ALGAPI

From: Ard Biesheuvel <[email protected]>

The ChaCha20-Poly1305 library code uses the sg_miter API to process input presented via scatterlists, except for the special case where the digest buffer is not covered entirely by the same scatterlist entry as the last byte of input. In that case, it uses scatterwalk_map_and_copy() to access the memory in the input scatterlist where the digest is stored.

This results in a dependency on crypto/scatterwalk.c and therefore on CONFIG_CRYPTO_ALGAPI, which is unnecessary, as the sg_miter API already provides this functionality via sg_copy_to_buffer(). So use that instead, and drop the dependencies on CONFIG_CRYPTO_ALGAPI and CONFIG_CRYPTO.

---
v2: - replace include of crypto/algapi.h with crypto/utils.h
- drop dependency on CONFIG_CRYPTO

 lib/crypto/Kconfig            | 2 --
 lib/crypto/chacha20poly1305.c | 7 +++----
 2 files changed, 3 insertions(+), 6 deletions(-)
lib/crypto/Kconfig +0 -2
index c542ef1d64d0..562906be4f93 100644
@@ -142,10 +142,8 @@ config CRYPTO_LIB_POLY1305
 
 config CRYPTO_LIB_CHACHA20POLY1305
 	tristate "ChaCha20-Poly1305 AEAD support (8-byte nonce library version)"
-	depends on CRYPTO
 	select CRYPTO_LIB_CHACHA
 	select CRYPTO_LIB_POLY1305
-	select CRYPTO_ALGAPI
 
 config CRYPTO_LIB_SHA1
 	tristate
lib/crypto/chacha20poly1305.c +3 -4
index a839c0ac60b2..9cfa886f1f89 100644
@@ -7,11 +7,10 @@
  * Information: https://tools.ietf.org/html/rfc8439
  */
 
-#include <crypto/algapi.h>
 #include <crypto/chacha20poly1305.h>
 #include <crypto/chacha.h>
 #include <crypto/poly1305.h>
-#include <crypto/scatterwalk.h>
+#include <crypto/utils.h>
 
 #include <linux/unaligned.h>
 #include <linux/kernel.h>
@@ -318,8 +317,8 @@ bool chacha20poly1305_crypt_sg_inplace(struct scatterlist *src,
 
 	if (unlikely(sl > -POLY1305_DIGEST_SIZE)) {
 		poly1305_final(&poly1305_state, b.mac[1]);
-		scatterwalk_map_and_copy(b.mac[encrypt], src, src_len,
-					 sizeof(b.mac[1]), encrypt);
+		sg_copy_buffer(src, sg_nents(src), b.mac[encrypt],
+			       sizeof(b.mac[1]), src_len, !encrypt);
 		ret = encrypt ||
 		      !crypto_memneq(b.mac[0], b.mac[1], POLY1305_DIGEST_SIZE);
 	}
Signature · 1 line
2.48.1.711.g2feabab25a-goog
maintainer
#1 lore raw
Quoted 17 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

The ChaCha20-Poly1305 library code uses the sg_miter API to process input presented via scatterlists, except for the special case where the digest buffer is not covered entirely by the same scatterlist entry as the last byte of input. In that case, it uses scatterwalk_map_and_copy() to access the memory in the input scatterlist where the digest is stored.

This results in a dependency on crypto/scatterwalk.c and therefore on CONFIG_CRYPTO_ALGAPI, which is unnecessary, as the sg_miter API already provides this functionality via sg_copy_to_buffer(). So use that instead, and drop the dependencies on CONFIG_CRYPTO_ALGAPI and CONFIG_CRYPTO.

Quoted 7 lines

---
v2: - replace include of crypto/algapi.h with crypto/utils.h
- drop dependency on CONFIG_CRYPTO

 lib/crypto/Kconfig            | 2 --
 lib/crypto/chacha20poly1305.c | 7 +++----
 2 files changed, 3 insertions(+), 6 deletions(-)

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt