Skip to contentSkip to conversation

Add support for x509 certs with NIST p256 and p192 keys

PATCH Superseded Stefan Berger 3 participants 10 messages Jan 31, 2021 – Feb 1, 2021

Subsystems (10)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Stefan Berger

author reviewer
#0 lore raw

v6 cover Add support for x509 certs with NIST p256 and p192 keys

This series of patches adds support for x509 certificates signed by a CA that uses NIST p256 or p192 keys for signing. It also adds support for certificates where the public key is a NIST p256 or p192 key. The math for ECDSA signature verification is also added.

Since self-signed certificates are verified upon loading, the following script can be used for testing:

k=$(keyctl newring test @u)

while :; do

	for hash in sha1 sha224 sha256 sha384 sha512; do
		openssl req \
			-x509 \
			-${hash} \
			-newkey ec \
			-pkeyopt ec_paramgen_curve:prime256v1 \
			-keyout key.pem \
			-days 365 \
			-subj '/CN=test' \
			-nodes \
			-outform der \
			-out cert.der
		keyctl padd asymmetric testkey $k < cert.der
		if [ $? -ne 0 ]; then
			echo "ERROR"
			exit 1
		fi
	done

done

It also works with restricted keyrings where an RSA key is used to sign a NIST P256/P192 key. Scripts for testing are here:

https://github.com/stefanberger/eckey-testing

The ECDSA signature verification will be used by IMA Appraisal where ECDSA file signatures stored in RPM packages will use substantially less space than if RSA signatures were to be used.

Stefan

v5->v6:
- moved ecdsa code into its own module ecdsa_generic built from ecdsa.c
- added script-generated test vectors for NIST P256 & P192 and all hashes
- parsing of OID that contain header with new parse_oid()

v4->v5:
- registering crypto support under names ecdsa-nist-p256/p192 following Hubert Xu's suggestion in other thread
- appended IMA ECDSA support patch

v3->v4:
- split off of ecdsa crypto part; registering akcipher as "ecdsa" and deriving used curve from digits in parsed key

v2->v3:
- patch 2 now includes linux/scatterlist.h

v1->v2:
- using faster vli_sub rather than newly added vli_mod_fast to 'reduce' result
- rearranged switch statements to follow after RSA
- 3rd patch from 1st posting is now 1st patch

Stefan Berger (4):
crypto: Add support for ECDSA signature verification
x509: Detect sm2 keys by their parameters OID
x509: Add support for parsing x509 certs with ECDSA keys
ima: Support EC keys for signature verification

 crypto/Kconfig                            |  10 +
 crypto/Makefile                           |   6 +
 crypto/asymmetric_keys/public_key.c       |  19 ++
 crypto/asymmetric_keys/x509_cert_parser.c |  44 ++-
 crypto/ecc.c                              |  13 +-
 crypto/ecc.h                              |  28 ++
 crypto/ecdsa.c                            | 361 ++++++++++++++++++++++
 crypto/ecdsasignature.asn1                |   4 +
 crypto/testmgr.c                          |  12 +
 crypto/testmgr.h                          | 267 ++++++++++++++++
 include/keys/asymmetric-type.h            |   6 +
 include/linux/oid_registry.h              |   7 +
 lib/oid_registry.c                        |  13 +
 security/integrity/digsig_asymmetric.c    |  29 +-
 14 files changed, 790 insertions(+), 29 deletions(-)
 create mode 100644 crypto/ecdsa.c
 create mode 100644 crypto/ecdsasignature.asn1
Signature · 1 line
2.29.2
author reviewer
#1 lore raw

PATCH v6 1/4 crypto: Add support for ECDSA signature verification

Add support for parsing the parameters of a NIST P256 or NIST P192 key. Enable signature verification using these keys. The new module is enabled with CONFIG_ECDSA:
Elliptic Curve Digital Signature Algorithm (NIST P192, P256 etc.) is A NIST cryptographic standard algorithm. Only signature verification is implemented.

---
 crypto/Kconfig             |  10 +
 crypto/Makefile            |   6 +
 crypto/ecc.c               |  13 +-
 crypto/ecc.h               |  28 +++
 crypto/ecdsa.c             | 361 +++++++++++++++++++++++++++++++++++++
 crypto/ecdsasignature.asn1 |   4 +
 crypto/testmgr.c           |  12 ++
 crypto/testmgr.h           | 267 +++++++++++++++++++++++++++
 8 files changed, 690 insertions(+), 11 deletions(-)
 create mode 100644 crypto/ecdsa.c
 create mode 100644 crypto/ecdsasignature.asn1
crypto/Kconfig +10 -0
index 094ef56ab7b4..152a4ee54fc6 100644
@@ -247,6 +247,16 @@ config CRYPTO_ECDH
 	help
 	  Generic implementation of the ECDH algorithm
 
+config CRYPTO_ECDSA
+	tristate "ECDSA (NIST P192, P256 etc.) algorithm"
+	select CRYPTO_ECC
+	select CRYPTO_AKCIPHER
+	select ASN1
+	help
+	  Elliptic Curve Digital Signature Algorithm (NIST P192, P256 etc.)
+	  is A NIST cryptographic standard algorithm. Only signature verification
+	  is implemented.
+
 config CRYPTO_ECRDSA
 	tristate "EC-RDSA (GOST 34.10) algorithm"
 	select CRYPTO_ECC
crypto/Makefile +6 -0
index b279483fba50..982066c6bdfb 100644
@@ -50,6 +50,12 @@ sm2_generic-y += sm2.o
 
 obj-$(CONFIG_CRYPTO_SM2) += sm2_generic.o
 
+$(obj)/ecdsasignature.asn1.o: $(obj)/ecdsasignature.asn1.c $(obj)/ecdsasignature.asn1.h
+$(obj)/ecdsa.o: $(obj)/ecdsasignature.asn1.h
+ecdsa_generic-y += ecdsa.o
+ecdsa_generic-y += ecdsasignature.asn1.o
+obj-$(CONFIG_CRYPTO_ECDSA) += ecdsa_generic.o
+
 crypto_acompress-y := acompress.o
 crypto_acompress-y += scompress.o
 obj-$(CONFIG_CRYPTO_ACOMP2) += crypto_acompress.o
crypto/ecc.c +2 -11
index c80aa25994a0..25e79fd70566 100644
@@ -42,7 +42,7 @@ typedef struct {
 	u64 m_high;
 } uint128_t;
 
-static inline const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
+const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
 {
 	switch (curve_id) {
 	/* In FIPS mode only allow P256 and higher */
@@ -54,6 +54,7 @@ static inline const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
 		return NULL;
 	}
 }
+EXPORT_SYMBOL(ecc_get_curve);
 
 static u64 *ecc_alloc_digits_space(unsigned int ndigits)
 {
@@ -1281,16 +1282,6 @@ void ecc_point_mult_shamir(const struct ecc_point *result,
 }
 EXPORT_SYMBOL(ecc_point_mult_shamir);
 
-static inline void ecc_swap_digits(const u64 *in, u64 *out,
-				   unsigned int ndigits)
-{
-	const __be64 *src = (__force __be64 *)in;
-	int i;
-
-	for (i = 0; i < ndigits; i++)
-		out[i] = be64_to_cpu(src[ndigits - 1 - i]);
-}
-
 static int __ecc_is_key_valid(const struct ecc_curve *curve,
 			      const u64 *private_key, unsigned int ndigits)
 {
crypto/ecc.h +28 -0
index d4e546b9ad79..2ea86dfb5cf7 100644
@@ -33,6 +33,8 @@
 
 #define ECC_DIGITS_TO_BYTES_SHIFT 3
 
+#define ECC_MAX_BYTES (ECC_MAX_DIGITS << ECC_DIGITS_TO_BYTES_SHIFT)
+
 /**
  * struct ecc_point - elliptic curve point in affine coordinates
  *
@@ -70,6 +72,32 @@ struct ecc_curve {
 	u64 *b;
 };
 
+/**
+ * ecc_swap_digits() - Copy ndigits from big endian array to native array
+ *
+ * @in:       input array
+ * @out:      output array
+ * @ndigits:  number of digits to copy
+ */
+static inline void ecc_swap_digits(const u64 *in, u64 *out,
+				   unsigned int ndigits)
+{
+	const __be64 *src = (__force __be64 *)in;
+	int i;
+
+	for (i = 0; i < ndigits; i++)
+		out[i] = be64_to_cpu(src[ndigits - 1 - i]);
+}
+
+/**
+ * ecc_get_curve()  - Get a curve given its curve_id
+ *
+ * @curve_id:  Id of the curve
+ *
+ * Returns pointer to the curve data, NULL if curve is not available
+ */
+const struct ecc_curve *ecc_get_curve(unsigned int curve_id);
+
 /**
  * ecc_is_key_valid() - Validate a given ECDH private key
  *
crypto/ecdsa.c new file +361 -0

361 lines not on this page · raw message

crypto/ecdsasignature.asn1 new file +4 -0
new file mode 100644
index 000000000000..621ab754fb9f
@@ -0,0 +1,4 @@
+ECDSASignature ::= SEQUENCE {
+	r	INTEGER ({ ecdsa_get_signature_r }),
+	s	INTEGER ({ ecdsa_get_signature_s })
+}
crypto/testmgr.c +12 -0
index a64a639eddfa..7303d82efac6 100644
@@ -4926,6 +4926,18 @@ static const struct alg_test_desc alg_test_descs[] = {
 		.suite = {
 			.kpp = __VECS(ecdh_tv_template)
 		}
+	}, {
+		.alg = "ecdsa-nist-p192",
+		.test = alg_test_akcipher,
+		.suite = {
+			.akcipher = __VECS(ecdsa_nist_p192_tv_template)
+		}
+	}, {
+		.alg = "ecdsa-nist-p256",
+		.test = alg_test_akcipher,
+		.suite = {
+			.akcipher = __VECS(ecdsa_nist_p256_tv_template)
+		}
 	}, {
 		.alg = "ecrdsa",
 		.test = alg_test_akcipher,
crypto/testmgr.h +267 -0

273 lines not on this page · raw message

Signature · 1 line
2.29.2
#2 lore raw

Re: [PATCH v6 1/4] crypto: Add support for ECDSA signature verification

crypto/ecc.h struct ecc_curve { · line 102

Quoted 11 lines · Stefan Berger
crypto/ecc.h +9 -0
+/**
+ * ecc_get_curve()  - Get a curve given its curve_id
+ *
+ * @curve_id:  Id of the curve
+ *
+ * Returns pointer to the curve data, NULL if curve is not available
+ */
+const struct ecc_curve *ecc_get_curve(unsigned int curve_id);
+
  /**
   * ecc_is_key_valid() - Validate a given ECDH private key

Shall we move this definition to 'include/crypto'? Other drivers may also want to use it.

author reviewer
#3 lore raw

Re: [PATCH v6 1/4] crypto: Add support for ECDSA signature verification

Quoted 17 lines · yumeng
Quoted 11 lines · Stefan Berger
crypto/ecc.h +9 -0
+/**
+ * ecc_get_curve()  - Get a curve given its curve_id
+ *
+ * @curve_id:  Id of the curve
+ *
+ * Returns pointer to the curve data, NULL if curve is not available
+ */
+const struct ecc_curve *ecc_get_curve(unsigned int curve_id);
+

  /**
   * ecc_is_key_valid() - Validate a given ECDH private key

Shall we move this definition to 'include/crypto'? Other drivers may also want to use it.

Maybe the driver that starts using would move it?

author reviewer
#4 lore raw

PATCH v6 2/4 x509: Detect sm2 keys by their parameters OID

In reply to Stefan Berger

Detect whether a key is an sm2 type of key by its OID in the parameters array rather than assuming that everything under OID_id_ecPublicKey is sm2, which is not the case.

---
 crypto/asymmetric_keys/x509_cert_parser.c | 12 +++++++++++-
 include/linux/oid_registry.h              |  1 +
 lib/oid_registry.c                        | 13 +++++++++++++
 3 files changed, 25 insertions(+), 1 deletion(-)
crypto/asymmetric_keys/x509_cert_parser.c +11 -1
index 52c9b455fc7d..1621ceaf5c95 100644
@@ -459,6 +459,7 @@ int x509_extract_key_data(void *context, size_t hdrlen,
 			  const void *value, size_t vlen)
 {
 	struct x509_parse_context *ctx = context;
+	enum OID oid;
 
 	ctx->key_algo = ctx->last_oid;
 	switch (ctx->last_oid) {
@@ -470,7 +471,16 @@ int x509_extract_key_data(void *context, size_t hdrlen,
 		ctx->cert->pub->pkey_algo = "ecrdsa";
 		break;
 	case OID_id_ecPublicKey:
-		ctx->cert->pub->pkey_algo = "sm2";
+		if (parse_OID(ctx->params, ctx->params_size, &oid) != 0)
+			return -EBADMSG;
+
+		switch (oid) {
+		case OID_sm2:
+			ctx->cert->pub->pkey_algo = "sm2";
+			break;
+		default:
+			return -ENOPKG;
+		}
 		break;
 	default:
 		return -ENOPKG;
include/linux/oid_registry.h +1 -0
index 4462ed2c18cd..d4982e42c0d2 100644
@@ -117,6 +117,7 @@ enum OID {
 };
 
 extern enum OID look_up_OID(const void *data, size_t datasize);
+extern int parse_OID(const void *data, size_t datasize, enum OID *oid);
 extern int sprint_oid(const void *, size_t, char *, size_t);
 extern int sprint_OID(enum OID, char *, size_t);
 
lib/oid_registry.c +13 -0
index f7ad43f28579..508e0b34b5f0 100644
@@ -11,6 +11,7 @@
 #include <linux/kernel.h>
 #include <linux/errno.h>
 #include <linux/bug.h>
+#include <linux/asn1.h>
 #include "oid_registry_data.c"
 
 MODULE_DESCRIPTION("OID Registry");
@@ -92,6 +93,18 @@ enum OID look_up_OID(const void *data, size_t datasize)
 }
 EXPORT_SYMBOL_GPL(look_up_OID);
 
+int parse_OID(const void *data, size_t datasize, enum OID *oid)
+{
+	const unsigned char *v = data;
+
+	if (datasize < 2 || v[0] != ASN1_OID || v[1] != datasize - 2)
+		return -EBADMSG;
+
+	*oid = look_up_OID(data + 2, datasize - 2);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(parse_OID);
+
 /*
  * sprint_OID - Print an Object Identifier into a buffer
  * @data: The encoded OID to print
Signature · 1 line
2.29.2
Reviewed-by on v6 2/4
#5 lore raw

Re: [PATCH v6 2/4] x509: Detect sm2 keys by their parameters OID

Quoted 77 lines · Stefan Berger

Detect whether a key is an sm2 type of key by its OID in the parameters array rather than assuming that everything under OID_id_ecPublicKey is sm2, which is not the case.

---
  crypto/asymmetric_keys/x509_cert_parser.c | 12 +++++++++++-
  include/linux/oid_registry.h              |  1 +
  lib/oid_registry.c                        | 13 +++++++++++++
  3 files changed, 25 insertions(+), 1 deletion(-)
crypto/asymmetric_keys/x509_cert_parser.c +11 -1
index 52c9b455fc7d..1621ceaf5c95 100644
@@ -459,6 +459,7 @@ int x509_extract_key_data(void *context, size_t hdrlen,
  			  const void *value, size_t vlen)
  {
  	struct x509_parse_context *ctx = context;
+	enum OID oid;
 
  	ctx->key_algo = ctx->last_oid;
  	switch (ctx->last_oid) {
@@ -470,7 +471,16 @@ int x509_extract_key_data(void *context, size_t hdrlen,
  		ctx->cert->pub->pkey_algo = "ecrdsa";
  		break;
  	case OID_id_ecPublicKey:
-		ctx->cert->pub->pkey_algo = "sm2";
+		if (parse_OID(ctx->params, ctx->params_size, &oid) != 0)
+			return -EBADMSG;
+
+		switch (oid) {
+		case OID_sm2:
+			ctx->cert->pub->pkey_algo = "sm2";
+			break;
+		default:
+			return -ENOPKG;
+		}
  		break;
  	default:
  		return -ENOPKG;
include/linux/oid_registry.h +1 -0
index 4462ed2c18cd..d4982e42c0d2 100644
@@ -117,6 +117,7 @@ enum OID {
  };
 
  extern enum OID look_up_OID(const void *data, size_t datasize);
+extern int parse_OID(const void *data, size_t datasize, enum OID *oid);
  extern int sprint_oid(const void *, size_t, char *, size_t);
  extern int sprint_OID(enum OID, char *, size_t);
 
lib/oid_registry.c +6 -0
index f7ad43f28579..508e0b34b5f0 100644
@@ -11,6 +11,7 @@
  #include <linux/kernel.h>
  #include <linux/errno.h>
  #include <linux/bug.h>
+#include <linux/asn1.h>
  #include "oid_registry_data.c"
 
  MODULE_DESCRIPTION("OID Registry");
@@ -92,6 +93,18 @@ enum OID look_up_OID(const void *data, size_t datasize)
  }
  EXPORT_SYMBOL_GPL(look_up_OID);
 
+int parse_OID(const void *data, size_t datasize, enum OID *oid)
+{
+	const unsigned char *v = data;
+
+	if (datasize < 2 || v[0] != ASN1_OID || v[1] != datasize - 2)

lib/oid_registry.c enum OID look_up_OID(const void *data, size_t datasize) · line 110

Quoted 10 lines
lib/oid_registry.c +7 -0
+		return -EBADMSG;
+
+	*oid = look_up_OID(data + 2, datasize - 2);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(parse_OID);
+
  /*
   * sprint_OID - Print an Object Identifier into a buffer
   * @data: The encoded OID to print

Great job, I'm just curious why we need to add a new function, this seems unnecessary, if possible, please add

Best regards,
Tianjia

author reviewer
#6 lore raw

Re: [PATCH v6 2/4] x509: Detect sm2 keys by their parameters OID

Quoted 25 lines · Tianjia Zhang
Quoted 25 lines

index f7ad43f28579..508e0b34b5f0 100644

lib/oid_registry.c +10 -0
@@ -11,6 +11,7 @@
   #include <linux/kernel.h>
   #include <linux/errno.h>
   #include <linux/bug.h>
+#include <linux/asn1.h>
   #include "oid_registry_data.c"
     MODULE_DESCRIPTION("OID Registry");
@@ -92,6 +93,18 @@ enum OID look_up_OID(const void *data, size_t 
 datasize)
   }
   EXPORT_SYMBOL_GPL(look_up_OID);
   +int parse_OID(const void *data, size_t datasize, enum OID *oid)
+{
+    const unsigned char *v = data;
+
+    if (datasize < 2 || v[0] != ASN1_OID || v[1] != datasize - 2)
+        return -EBADMSG;
+
+    *oid = look_up_OID(data + 2, datasize - 2);
+    return 0;
+}
Quoted 8 lines
Quoted 6 lines
lib/oid_registry.c +2 -0
+EXPORT_SYMBOL_GPL(parse_OID);
+
   /*
    * sprint_OID - Print an Object Identifier into a buffer

   * @data: The encoded OID to print

Great job, I'm just curious why we need to add a new function, this seems unnecessary, if possible, please add

Thanks. I call this function in two places now. I thought it was 'worth it'.

Quoted 5 lines

Best regards,
Tianjia

author reviewer
#7 lore raw

PATCH v6 3/4 x509: Add support for parsing x509 certs with ECDSA keys

In reply to Stefan Berger

This patch adds support for parsing of x509 certificates that contain ECDSA keys, such as NIST P256, that have been signed by a CA using any of the current SHA hash algorithms.

---
 crypto/asymmetric_keys/public_key.c       | 19 ++++++++++++++
 crypto/asymmetric_keys/x509_cert_parser.c | 32 ++++++++++++++++++++++-
 include/linux/oid_registry.h              |  6 +++++
 3 files changed, 56 insertions(+), 1 deletion(-)
crypto/asymmetric_keys/public_key.c +19 -0
index 8892908ad58c..7dae61b79d5a 100644
@@ -14,6 +14,7 @@
 #include <linux/slab.h>
 #include <linux/seq_file.h>
 #include <linux/scatterlist.h>
+#include <linux/asn1.h>
 #include <keys/asymmetric-subtype.h>
 #include <crypto/public_key.h>
 #include <crypto/akcipher.h>
@@ -90,6 +91,24 @@ int software_key_determine_akcipher(const char *encoding,
 		return 0;
 	}
 
+	if (strcmp(encoding, "x962") == 0) {
+		enum OID oid;
+
+		if (parse_OID(pkey->params, pkey->paramlen, &oid) != 0)
+			return -EBADMSG;
+
+		switch (oid) {
+		case OID_id_prime192v1:
+			strcpy(alg_name, "ecdsa-nist-p192");
+			return 0;
+		case OID_id_prime256v1:
+			strcpy(alg_name, "ecdsa-nist-p256");
+			return 0;
+		default:
+			return -EINVAL;
+		}
+	}
+
 	return -ENOPKG;
 }
 
crypto/asymmetric_keys/x509_cert_parser.c +31 -1
index 1621ceaf5c95..0aff4e584b11 100644
@@ -227,6 +227,26 @@ int x509_note_pkey_algo(void *context, size_t hdrlen,
 		ctx->cert->sig->hash_algo = "sha224";
 		goto rsa_pkcs1;
 
+	case OID_id_ecdsa_with_sha1:
+		ctx->cert->sig->hash_algo = "sha1";
+		goto ecdsa;
+
+	case OID_id_ecdsa_with_sha224:
+		ctx->cert->sig->hash_algo = "sha224";
+		goto ecdsa;
+
+	case OID_id_ecdsa_with_sha256:
+		ctx->cert->sig->hash_algo = "sha256";
+		goto ecdsa;
+
+	case OID_id_ecdsa_with_sha384:
+		ctx->cert->sig->hash_algo = "sha384";
+		goto ecdsa;
+
+	case OID_id_ecdsa_with_sha512:
+		ctx->cert->sig->hash_algo = "sha512";
+		goto ecdsa;
+
 	case OID_gost2012Signature256:
 		ctx->cert->sig->hash_algo = "streebog256";
 		goto ecrdsa;
@@ -255,6 +275,11 @@ int x509_note_pkey_algo(void *context, size_t hdrlen,
 	ctx->cert->sig->encoding = "raw";
 	ctx->algo_oid = ctx->last_oid;
 	return 0;
+ecdsa:
+	ctx->cert->sig->pkey_algo = "ecdsa";
+	ctx->cert->sig->encoding = "x962";
+	ctx->algo_oid = ctx->last_oid;
+	return 0;
 }
 
 /*
@@ -276,7 +301,8 @@ int x509_note_signature(void *context, size_t hdrlen,
 
 	if (strcmp(ctx->cert->sig->pkey_algo, "rsa") == 0 ||
 	    strcmp(ctx->cert->sig->pkey_algo, "ecrdsa") == 0 ||
-	    strcmp(ctx->cert->sig->pkey_algo, "sm2") == 0) {
+	    strcmp(ctx->cert->sig->pkey_algo, "sm2") == 0 ||
+	    strcmp(ctx->cert->sig->pkey_algo, "ecdsa") == 0) {
 		/* Discard the BIT STRING metadata */
 		if (vlen < 1 || *(const u8 *)value != 0)
 			return -EBADMSG;
@@ -478,6 +504,10 @@ int x509_extract_key_data(void *context, size_t hdrlen,
 		case OID_sm2:
 			ctx->cert->pub->pkey_algo = "sm2";
 			break;
+		case OID_id_prime192v1:
+		case OID_id_prime256v1:
+			ctx->cert->pub->pkey_algo = "ecdsa";
+			break;
 		default:
 			return -ENOPKG;
 		}
include/linux/oid_registry.h +6 -0
index d4982e42c0d2..ff3cad9f8c1f 100644
@@ -21,6 +21,12 @@ enum OID {
 	OID_id_dsa,			/* 1.2.840.10040.4.1 */
 	OID_id_ecdsa_with_sha1,		/* 1.2.840.10045.4.1 */
 	OID_id_ecPublicKey,		/* 1.2.840.10045.2.1 */
+	OID_id_prime192v1,		/* 1.2.840.10045.3.1.1 */
+	OID_id_prime256v1,		/* 1.2.840.10045.3.1.7 */
+	OID_id_ecdsa_with_sha224,	/* 1.2.840.10045.4.3.1 */
+	OID_id_ecdsa_with_sha256,	/* 1.2.840.10045.4.3.2 */
+	OID_id_ecdsa_with_sha384,	/* 1.2.840.10045.4.3.3 */
+	OID_id_ecdsa_with_sha512,	/* 1.2.840.10045.4.3.4 */
 
 	/* PKCS#1 {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-1(1)} */
 	OID_rsaEncryption,		/* 1.2.840.113549.1.1.1 */
Signature · 1 line
2.29.2
author reviewer
#8 lore raw

PATCH v6 4/4 ima: Support EC keys for signature verification

In reply to Stefan Berger

Add support for IMA signature verification for EC keys. Since SHA type of hashes can be used by RSA and ECDSA signature schemes we need to look at the key and derive from the key which signature scheme to use. Since this can be applied to all types of keys, we change the selection of the encoding type to be driven by the key's signature scheme rather than by the hash type.

---
 include/keys/asymmetric-type.h         |  6 ++++++
 security/integrity/digsig_asymmetric.c | 29 ++++++++++++--------------
 2 files changed, 19 insertions(+), 16 deletions(-)
include/keys/asymmetric-type.h +6 -0
index a29d3ff2e7e8..c432fdb8547f 100644
@@ -72,6 +72,12 @@ const struct asymmetric_key_ids *asymmetric_key_ids(const struct key *key)
 	return key->payload.data[asym_key_ids];
 }
 
+static inline
+const struct public_key *asymmetric_key_public_key(const struct key *key)
+{
+	return key->payload.data[asym_crypto];
+}
+
 extern struct key *find_asymmetric_key(struct key *keyring,
 				       const struct asymmetric_key_id *id_0,
 				       const struct asymmetric_key_id *id_1,
security/integrity/digsig_asymmetric.c +13 -16
index a662024b4c70..29002d016607 100644
@@ -84,6 +84,7 @@ int asymmetric_verify(struct key *keyring, const char *sig,
 {
 	struct public_key_signature pks;
 	struct signature_v2_hdr *hdr = (struct signature_v2_hdr *)sig;
+	const struct public_key *pk;
 	struct key *key;
 	int ret;
 
@@ -105,23 +106,19 @@ int asymmetric_verify(struct key *keyring, const char *sig,
 	memset(&pks, 0, sizeof(pks));
 
 	pks.hash_algo = hash_algo_name[hdr->hash_algo];
-	switch (hdr->hash_algo) {
-	case HASH_ALGO_STREEBOG_256:
-	case HASH_ALGO_STREEBOG_512:
-		/* EC-RDSA and Streebog should go together. */
-		pks.pkey_algo = "ecrdsa";
-		pks.encoding = "raw";
-		break;
-	case HASH_ALGO_SM3_256:
-		/* SM2 and SM3 should go together. */
-		pks.pkey_algo = "sm2";
-		pks.encoding = "raw";
-		break;
-	default:
-		pks.pkey_algo = "rsa";
+
+	pk = asymmetric_key_public_key(key);
+	pks.pkey_algo = pk->pkey_algo;
+	if (!strcmp(pk->pkey_algo, "rsa"))
 		pks.encoding = "pkcs1";
-		break;
-	}
+	else if (!strcmp(pk->pkey_algo, "ecdsa"))
+		pks.encoding = "x962";
+	else if (!strcmp(pk->pkey_algo, "ecrdsa") ||
+		   !strcmp(pk->pkey_algo, "sm2"))
+		pks.encoding = "raw";
+	else
+		return -ENOPKG;
+
 	pks.digest = (u8 *)data;
 	pks.digest_size = datalen;
 	pks.s = hdr->sig;
Signature · 1 line
2.29.2
Reviewed-by on v6 4/4
#9 lore raw

Re: [PATCH v6 4/4] ima: Support EC keys for signature verification

Quoted 77 lines · Stefan Berger

Add support for IMA signature verification for EC keys. Since SHA type of hashes can be used by RSA and ECDSA signature schemes we need to look at the key and derive from the key which signature scheme to use. Since this can be applied to all types of keys, we change the selection of the encoding type to be driven by the key's signature scheme rather than by the hash type.

---
  include/keys/asymmetric-type.h         |  6 ++++++
  security/integrity/digsig_asymmetric.c | 29 ++++++++++++--------------
  2 files changed, 19 insertions(+), 16 deletions(-)
include/keys/asymmetric-type.h +6 -0
index a29d3ff2e7e8..c432fdb8547f 100644
@@ -72,6 +72,12 @@ const struct asymmetric_key_ids *asymmetric_key_ids(const struct key *key)
  	return key->payload.data[asym_key_ids];
  }
 
+static inline
+const struct public_key *asymmetric_key_public_key(const struct key *key)
+{
+	return key->payload.data[asym_crypto];
+}
+
  extern struct key *find_asymmetric_key(struct key *keyring,
  				       const struct asymmetric_key_id *id_0,
  				       const struct asymmetric_key_id *id_1,
security/integrity/digsig_asymmetric.c +6 -16
index a662024b4c70..29002d016607 100644
@@ -84,6 +84,7 @@ int asymmetric_verify(struct key *keyring, const char *sig,
  {
  	struct public_key_signature pks;
  	struct signature_v2_hdr *hdr = (struct signature_v2_hdr *)sig;
+	const struct public_key *pk;
  	struct key *key;
  	int ret;
 
@@ -105,23 +106,19 @@ int asymmetric_verify(struct key *keyring, const char *sig,
  	memset(&pks, 0, sizeof(pks));
 
  	pks.hash_algo = hash_algo_name[hdr->hash_algo];
-	switch (hdr->hash_algo) {
-	case HASH_ALGO_STREEBOG_256:
-	case HASH_ALGO_STREEBOG_512:
-		/* EC-RDSA and Streebog should go together. */
-		pks.pkey_algo = "ecrdsa";
-		pks.encoding = "raw";
-		break;
-	case HASH_ALGO_SM3_256:
-		/* SM2 and SM3 should go together. */
-		pks.pkey_algo = "sm2";
-		pks.encoding = "raw";
-		break;
-	default:
-		pks.pkey_algo = "rsa";
+
+	pk = asymmetric_key_public_key(key);
+	pks.pkey_algo = pk->pkey_algo;
+	if (!strcmp(pk->pkey_algo, "rsa"))
  		pks.encoding = "pkcs1";
-		break;
-	}
+	else if (!strcmp(pk->pkey_algo, "ecdsa"))

security/integrity/digsig_asymmetric.c int asymmetric_verify(struct key *keyring, const char *sig, · line 124

Quoted 10 lines
security/integrity/digsig_asymmetric.c +7 -0
+		pks.encoding = "x962";
+	else if (!strcmp(pk->pkey_algo, "ecrdsa") ||
+		   !strcmp(pk->pkey_algo, "sm2"))
+		pks.encoding = "raw";
+	else
+		return -ENOPKG;
+
  	pks.digest = (u8 *)data;
  	pks.digest_size = datalen;
  	pks.s = hdr->sig;

Looks good to me, so

Thanks,
Tianjia