Skip to contentSkip to conversation

certs: Move RSA self-test data to separate file

PATCH Superseded Joachim Vandersmissen 3 participants 6 messages May 3, 2024 – May 10, 2024

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 certs: Move RSA self-test data to separate file

v3: adding the FIPS_SIGNATURE_SELFTEST_RSA configuration option to enforce that RSA (and SHA-256) are either built-in; or RSA (and SHA-256) and x509_selftest are modules. This will prevent a misconfiguration where RSA (or SHA-256) are modules but x509_selftest is built-in (and thus panics on boot).

It is still possible to panic if x509_selftest is compiled as a module and loaded before the RSA and SHA-256 modules at runtime. I'm not sure if this can be avoided.

---8<---

In preparation of adding new ECDSA self-tests, the existing data is moved to a separate file. A new configuration option is added to control the compilation of the separate file. This configuration option also enforces dependencies that were missing from the existing CONFIG_FIPS_SIGNATURE_SELFTEST option.
The old fips_signature_selftest is no longer an init function, but now a helper function called from fips_signature_selftest_rsa.

---
 crypto/asymmetric_keys/Kconfig        |  10 ++
 crypto/asymmetric_keys/Makefile       |   1 +
 crypto/asymmetric_keys/selftest.c     | 218 ++++----------------------
 crypto/asymmetric_keys/selftest.h     |  16 ++
 crypto/asymmetric_keys/selftest_rsa.c | 172 ++++++++++++++++++++
 5 files changed, 228 insertions(+), 189 deletions(-)
 create mode 100644 crypto/asymmetric_keys/selftest.h
 create mode 100644 crypto/asymmetric_keys/selftest_rsa.c
crypto/asymmetric_keys/Kconfig +10 -0
index 59ec726b7c77..68434c745b3c 100644
@@ -86,4 +86,14 @@ config FIPS_SIGNATURE_SELFTEST
 	depends on PKCS7_MESSAGE_PARSER=X509_CERTIFICATE_PARSER
 	depends on X509_CERTIFICATE_PARSER
 
+config FIPS_SIGNATURE_SELFTEST_RSA
+	bool "Include RSA selftests"
+	help
+	  This option includes the RSA selftests in the X.509+PKCS7 selftest
+	  code.  RSA and SHA-256 must be available when the selftests are
+	  executed; this is enforced by the dependencies.
+	depends on FIPS_SIGNATURE_SELFTEST
+	depends on CRYPTO_SHA256=y || CRYPTO_SHA256=FIPS_SIGNATURE_SELFTEST
+	depends on CRYPTO_RSA=y || CRYPTO_RSA=FIPS_SIGNATURE_SELFTEST
+
 endif # ASYMMETRIC_KEY_TYPE
crypto/asymmetric_keys/Makefile +1 -0
index 1a273d6df3eb..ac1402e27324 100644
@@ -24,6 +24,7 @@ x509_key_parser-y := \
 	x509_public_key.o
 obj-$(CONFIG_FIPS_SIGNATURE_SELFTEST) += x509_selftest.o
 x509_selftest-y += selftest.o
+x509_selftest-$(CONFIG_FIPS_SIGNATURE_SELFTEST_RSA) += selftest_rsa.o
 
 $(obj)/x509_cert_parser.o: \
 	$(obj)/x509.asn1.h \
crypto/asymmetric_keys/selftest.c +29 -189

251 lines not on this page · raw message

crypto/asymmetric_keys/selftest.h new file +16 -0
new file mode 100644
index 000000000000..842ac3cf86b4
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* Helper function for self-testing PKCS#7 signature verification.
+ *
+ * Copyright (C) 2024 Joachim Vandersmissen <[email protected]>
+ */
+
+void fips_signature_selftest(const char *name,
+			     const u8 *keys, size_t keys_len,
+			     const u8 *data, size_t data_len,
+			     const u8 *sig, size_t sig_len);
+
+#ifdef CONFIG_FIPS_SIGNATURE_SELFTEST_RSA
+void __init fips_signature_selftest_rsa(void);
+#else
+static inline void __init fips_signature_selftest_rsa(void) { }
+#endif
crypto/asymmetric_keys/selftest_rsa.c new file +172 -0

172 lines not on this page · raw message

Signature · 1 line
2.45.0
#1 lore raw

PATCH v3 2/2 certs: Add ECDSA signature verification self-test

v3: adding the FIPS_SIGNATURE_SELFTEST_RSA configuration option to enforce that ECDSA (and SHA-256) are either built-in; or ECDSA (and SHA-256) and x509_selftest are modules.

---8<---

Commit c27b2d2012e1 ("crypto: testmgr - allow ecdsa-nist-p256 and -p384 in FIPS mode") enabled support for ECDSA in crypto/testmgr.c. The PKCS#7 signature verification API builds upon the KCAPI primitives to perform its high-level operations. Therefore, this change in testmgr.c also allows ECDSA to be used by the PKCS#7 signature verification API (in FIPS mode).

However, from a FIPS perspective, the PKCS#7 signature verification API is a distinct "service" from the KCAPI primitives. This is because the PKCS#7 API performs a "full" signature verification, which consists of both hashing the data to be verified, and the public key operation. On the other hand, the KCAPI primitive does not perform this hashing step - it accepts pre-hashed data from the caller and only performs the public key operation.

For this reason, the ECDSA self-tests in crypto/testmgr.c are not sufficient to cover ECDSA signature verification offered by the PKCS#7 API. This is reflected by the self-test already present in this file for RSA PKCS#1 v1.5 signature verification.

The solution is simply to add a second self-test here for ECDSA. P-256 with SHA-256 hashing was chosen as those parameters should remain FIPS-approved for the foreseeable future, while keeping the performance impact to a minimum. The ECDSA certificate and PKCS#7 signed data was generated using OpenSSL. The input data is identical to the input data for the existing RSA self-test.

---
 crypto/asymmetric_keys/Kconfig          | 10 +++
 crypto/asymmetric_keys/Makefile         |  1 +
 crypto/asymmetric_keys/selftest.c       |  1 +
 crypto/asymmetric_keys/selftest.h       |  6 ++
 crypto/asymmetric_keys/selftest_ecdsa.c | 89 +++++++++++++++++++++++++
 5 files changed, 107 insertions(+)
 create mode 100644 crypto/asymmetric_keys/selftest_ecdsa.c
crypto/asymmetric_keys/Kconfig +10 -0
index 68434c745b3c..f01ae999a0d6 100644
@@ -96,4 +96,14 @@ config FIPS_SIGNATURE_SELFTEST_RSA
 	depends on CRYPTO_SHA256=y || CRYPTO_SHA256=FIPS_SIGNATURE_SELFTEST
 	depends on CRYPTO_RSA=y || CRYPTO_RSA=FIPS_SIGNATURE_SELFTEST
 
+config FIPS_SIGNATURE_SELFTEST_ECDSA
+	bool "Include ECDSA selftests"
+	help
+	  This option includes the ECDSA selftests in the X.509+PKCS7 selftest
+	  code.  ECDSA and SHA-256 must be available when the selftests are
+	  executed; this is enforced by the dependencies.
+	depends on FIPS_SIGNATURE_SELFTEST
+	depends on CRYPTO_SHA256=y || CRYPTO_SHA256=FIPS_SIGNATURE_SELFTEST
+	depends on CRYPTO_ECDSA=y || CRYPTO_ECDSA=FIPS_SIGNATURE_SELFTEST
+
 endif # ASYMMETRIC_KEY_TYPE
crypto/asymmetric_keys/Makefile +1 -0
index ac1402e27324..bc65d3b98dcb 100644
@@ -25,6 +25,7 @@ x509_key_parser-y := \
 obj-$(CONFIG_FIPS_SIGNATURE_SELFTEST) += x509_selftest.o
 x509_selftest-y += selftest.o
 x509_selftest-$(CONFIG_FIPS_SIGNATURE_SELFTEST_RSA) += selftest_rsa.o
+x509_selftest-$(CONFIG_FIPS_SIGNATURE_SELFTEST_ECDSA) += selftest_ecdsa.o
 
 $(obj)/x509_cert_parser.o: \
 	$(obj)/x509.asn1.h \
crypto/asymmetric_keys/selftest.c +1 -0
index ec289d2d065c..98dc5cdfdebe 100644
@@ -61,6 +61,7 @@ void fips_signature_selftest(const char *name,
 static int __init fips_signature_selftest_init(void)
 {
 	fips_signature_selftest_rsa();
+	fips_signature_selftest_ecdsa();
 	return 0;
 }
 
crypto/asymmetric_keys/selftest.h +6 -0
index 842ac3cf86b4..4139f05906cb 100644
@@ -14,3 +14,9 @@ void __init fips_signature_selftest_rsa(void);
 #else
 static inline void __init fips_signature_selftest_rsa(void) { }
 #endif
+
+#ifdef CONFIG_FIPS_SIGNATURE_SELFTEST_ECDSA
+void __init fips_signature_selftest_ecdsa(void);
+#else
+static inline void __init fips_signature_selftest_ecdsa(void) { }
+#endif
crypto/asymmetric_keys/selftest_ecdsa.c new file +89 -0

89 lines not on this page · raw message

Signature · 1 line
2.45.0
maintainer
#2 lore raw
In reply to Joachim Vandersmissen

crypto/asymmetric_keys/Kconfig config FIPS_SIGNATURE_SELFTEST · line 90

Quoted 11 lines · Joachim Vandersmissen
crypto/asymmetric_keys/Kconfig +2 -0
index 59ec726b7c77..68434c745b3c 100644
@@ -86,4 +86,14 @@ config FIPS_SIGNATURE_SELFTEST
 	depends on PKCS7_MESSAGE_PARSER=X509_CERTIFICATE_PARSER
 	depends on X509_CERTIFICATE_PARSER
 
+config FIPS_SIGNATURE_SELFTEST_RSA
+	bool "Include RSA selftests"

Please don't ask questinons in Kconfig that we can avoid. In this case I see no valid reason for having this extra knob.

One question for FIPS_SIGNATURE_SELFTEST is enough.

Oh and please cc Jarkko Sakkinen <[email protected]> since he picked up two earlier fixes for this and it's best if this went through his tree.

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
#3 lore raw
Quoted 16 lines · Herbert Xu

crypto/asymmetric_keys/Kconfig config FIPS_SIGNATURE_SELFTEST · line 90

Quoted 11 lines · Joachim Vandersmissen
crypto/asymmetric_keys/Kconfig +2 -0
index 59ec726b7c77..68434c745b3c 100644
@@ -86,4 +86,14 @@ config FIPS_SIGNATURE_SELFTEST
 	depends on PKCS7_MESSAGE_PARSER=X509_CERTIFICATE_PARSER
 	depends on X509_CERTIFICATE_PARSER
 
+config FIPS_SIGNATURE_SELFTEST_RSA
+	bool "Include RSA selftests"

Please don't ask questinons in Kconfig that we can avoid. In this case I see no valid reason for having this extra knob.

Quoted 7 lines

One question for FIPS_SIGNATURE_SELFTEST is enough.

Oh and please cc Jarkko Sakkinen <[email protected]> since he picked up two earlier fixes for this and it's best if this went through his tree.

Thanks,

Fine with me. I've yet to send asymmetric keys pull request (last remaining) so there is some window left too.

BR, Jarkko

#4 lore raw
In reply to Herbert Xu

Hi Herbert,

Quoted 9 lines · Herbert Xu
Quoted 8 lines · Joachim Vandersmissen
crypto/asymmetric_keys/Kconfig +0 -0
index 59ec726b7c77..68434c745b3c 100644
@@ -86,4 +86,14 @@ config FIPS_SIGNATURE_SELFTEST
  	depends on PKCS7_MESSAGE_PARSER=X509_CERTIFICATE_PARSER
  	depends on X509_CERTIFICATE_PARSER
 
Quoted 6 lines

crypto/asymmetric_keys/Kconfig config FIPS_SIGNATURE_SELFTEST · line 90

Quoted 2 lines
crypto/asymmetric_keys/Kconfig +2 -0
+config FIPS_SIGNATURE_SELFTEST_RSA
+	bool "Include RSA selftests"

Please don't ask questinons in Kconfig that we can avoid. In this case I see no valid reason for having this extra knob.

One question for FIPS_SIGNATURE_SELFTEST is enough.

Just to clarify, you'd like to see FIPS_SIGNATURE_SELFTEST_RSA automatically set based on the values in FIPS_SIGNATURE_SELFTEST, CRYPTO_RSA, etc.?

Quoted 4 lines

Oh and please cc Jarkko Sakkinen <[email protected]> since he picked up two earlier fixes for this and it's best if this went through his tree.

Will do, thanks.

Quoted 2 lines

Thanks,

maintainer
#5 lore raw
Quoted 3 lines · Joachim Vandersmissen

Just to clarify, you'd like to see FIPS_SIGNATURE_SELFTEST_RSA automatically set based on the values in FIPS_SIGNATURE_SELFTEST, CRYPTO_RSA, etc.?

Yes just change

bool "Blah blah blah"

to

bool

and I think it should be all good.

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