Apply Crypto API wrappers to the exported crypto symbol in
CONFIG_CRYPTO_ECDH-related crypto to convert them into pluggable
interface.

This patch is partially based on work by Vegard Nossum, with
modifications. Unlike the original, we do not include
DEFINE_CRYPTO_API since only one copy of the crypto symbols is
kept, either in the crypto module or in the main kernel, and we ensure
such wrapper do not have impact on crypto already chosen built as
module.

Co-developed-by: Vegard Nossum <[email protected]>
Signed-off-by: Jay Wang <[email protected]>
---
 crypto/Makefile              |  2 +-
 crypto/ecdh.c                |  4 ++--
 crypto/fips140/fips140-api.c | 12 ++++++++++++
 include/crypto/ecdh.h        | 14 +++++++++++---
 4 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/crypto/Makefile b/crypto/Makefile
index 3247d53c1fc6..cdc74dcea2f1 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -183,7 +183,7 @@ crypto-objs-$(CONFIG_CRYPTO_ESSIV) += essiv.o
 
 ecdh_generic-y += ecdh.o
 ecdh_generic-y += ecdh_helper.o
-obj-$(CONFIG_CRYPTO_ECDH) += ecdh_generic.o
+crypto-objs-$(CONFIG_CRYPTO_ECDH) += ecdh_generic.o
 
 $(obj)/ecrdsa_params.asn1.o: $(obj)/ecrdsa_params.asn1.c 
$(obj)/ecrdsa_params.asn1.h
 $(obj)/ecrdsa_pub_key.asn1.o: $(obj)/ecrdsa_pub_key.asn1.c 
$(obj)/ecrdsa_pub_key.asn1.h
diff --git a/crypto/ecdh.c b/crypto/ecdh.c
index 9f0b93b3166d..32c98cebfda6 100644
--- a/crypto/ecdh.c
+++ b/crypto/ecdh.c
@@ -240,8 +240,8 @@ static void __exit ecdh_exit(void)
        crypto_unregister_kpp(&ecdh_nist_p384);
 }
 
-module_init(ecdh_init);
-module_exit(ecdh_exit);
+crypto_module_init(ecdh_init);
+crypto_module_exit(ecdh_exit);
 MODULE_ALIAS_CRYPTO("ecdh");
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("ECDH generic algorithm");
diff --git a/crypto/fips140/fips140-api.c b/crypto/fips140/fips140-api.c
index 70b57939c753..298ab496a395 100644
--- a/crypto/fips140/fips140-api.c
+++ b/crypto/fips140/fips140-api.c
@@ -785,3 +785,15 @@ DEFINE_CRYPTO_API_STUB(aria_decrypt);
 DEFINE_CRYPTO_API_STUB(crypto_krb5enc_extractkeys);
 
 #endif
+/*
+ * crypto/ecdh_helper.c
+ */
+#if IS_BUILTIN(CONFIG_CRYPTO_ECDH)
+
+#include <crypto/ecdh.h>
+
+DEFINE_CRYPTO_API_STUB(crypto_ecdh_key_len);
+DEFINE_CRYPTO_API_STUB(crypto_ecdh_encode_key);
+DEFINE_CRYPTO_API_STUB(crypto_ecdh_decode_key);
+
+#endif
\ No newline at end of file
diff --git a/include/crypto/ecdh.h b/include/crypto/ecdh.h
index 9784ecdd2fb4..03d54f531ca2 100644
--- a/include/crypto/ecdh.h
+++ b/include/crypto/ecdh.h
@@ -8,6 +8,8 @@
 #ifndef _CRYPTO_ECDH_
 #define _CRYPTO_ECDH_
 
+#include <crypto/api.h>
+
 /**
  * DOC: ECDH Helper Functions
  *
@@ -49,7 +51,9 @@ struct ecdh {
  *
  * Return: size of the key in bytes
  */
-unsigned int crypto_ecdh_key_len(const struct ecdh *params);
+DECLARE_CRYPTO_API(CONFIG_CRYPTO_ECDH, crypto_ecdh_key_len, unsigned int,
+       (const struct ecdh *params),
+       (params));
 
 /**
  * crypto_ecdh_encode_key() - encode the private key
@@ -64,7 +68,9 @@ unsigned int crypto_ecdh_key_len(const struct ecdh *params);
  *
  * Return:     -EINVAL if buffer has insufficient size, 0 on success
  */
-int crypto_ecdh_encode_key(char *buf, unsigned int len, const struct ecdh *p);
+DECLARE_CRYPTO_API(CONFIG_CRYPTO_ECDH, crypto_ecdh_encode_key, int,
+       (char *buf, unsigned int len, const struct ecdh *p),
+       (buf, len, p));
 
 /**
  * crypto_ecdh_decode_key() - decode a private key
@@ -78,6 +84,8 @@ int crypto_ecdh_encode_key(char *buf, unsigned int len, const 
struct ecdh *p);
  *
  * Return:     -EINVAL if buffer has insufficient size, 0 on success
  */
-int crypto_ecdh_decode_key(const char *buf, unsigned int len, struct ecdh *p);
+DECLARE_CRYPTO_API(CONFIG_CRYPTO_ECDH, crypto_ecdh_decode_key, int,
+       (const char *buf, unsigned int len, struct ecdh *p),
+       (buf, len, p));
 
 #endif
-- 
2.47.3


Reply via email to