linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys
@ 2021-01-27 12:33 Stefan Berger
  2021-01-27 12:33 ` [PATCH v3 1/3] x509: Detect sm2 keys by their parameters OID Stefan Berger
                   ` (5 more replies)
  0 siblings, 6 replies; 15+ messages in thread
From: Stefan Berger @ 2021-01-27 12:33 UTC (permalink / raw)
  To: dhowells, keyrings
  Cc: linux-kernel, herbert, davem, linux-crypto, patrick, Stefan Berger

From: Stefan Berger <stefanb@linux.ibm.com>

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=$(keyctrl 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

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 (3):
  x509: Detect sm2 keys by their parameters OID
  x509: Add support for parsing x509 certs with NIST p256 keys
  x509: Add support for NIST p192 keys in certificates and akcipher

 crypto/Makefile                           |   9 +-
 crypto/asymmetric_keys/public_key.c       |  19 ++
 crypto/asymmetric_keys/x509_cert_parser.c |  45 ++-
 crypto/ecc.c                              | 318 ++++++++++++++++++++++
 crypto/ecc.h                              |   2 +
 crypto/ecc_curve_defs.h                   |   4 +
 crypto/eccsignature.asn1                  |   4 +
 include/linux/oid_registry.h              |   6 +
 8 files changed, 404 insertions(+), 3 deletions(-)
 create mode 100644 crypto/eccsignature.asn1

-- 
2.25.4


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v3 1/3] x509: Detect sm2 keys by their parameters OID
  2021-01-27 12:33 [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys Stefan Berger
@ 2021-01-27 12:33 ` Stefan Berger
  2021-01-30 21:26   ` Jarkko Sakkinen
  2021-01-27 12:33 ` [PATCH v3 2/3] x509: Add support for parsing x509 certs with NIST p256 keys Stefan Berger
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 15+ messages in thread
From: Stefan Berger @ 2021-01-27 12:33 UTC (permalink / raw)
  To: dhowells, keyrings
  Cc: linux-kernel, herbert, davem, linux-crypto, patrick, Stefan Berger

From: Stefan Berger <stefanb@linux.ibm.com>

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.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
 crypto/asymmetric_keys/x509_cert_parser.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c
index 52c9b455fc7d..4643fe5ed69a 100644
--- a/crypto/asymmetric_keys/x509_cert_parser.c
+++ b/crypto/asymmetric_keys/x509_cert_parser.c
@@ -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,17 @@ 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 (ctx->params_size < 2)
+			return -ENOPKG;
+
+		oid = look_up_OID(ctx->params + 2, ctx->params_size - 2);
+		switch (oid) {
+		case OID_sm2:
+			ctx->cert->pub->pkey_algo = "sm2";
+			break;
+		default:
+			return -ENOPKG;
+		}
 		break;
 	default:
 		return -ENOPKG;
-- 
2.25.4


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v3 2/3] x509: Add support for parsing x509 certs with NIST p256 keys
  2021-01-27 12:33 [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys Stefan Berger
  2021-01-27 12:33 ` [PATCH v3 1/3] x509: Detect sm2 keys by their parameters OID Stefan Berger
@ 2021-01-27 12:33 ` Stefan Berger
  2021-01-27 19:31   ` Herbert Xu
  2021-01-27 12:33 ` [PATCH v3 3/3] x509: Add support for NIST p192 keys in certificates and akcipher Stefan Berger
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 15+ messages in thread
From: Stefan Berger @ 2021-01-27 12:33 UTC (permalink / raw)
  To: dhowells, keyrings
  Cc: linux-kernel, herbert, davem, linux-crypto, patrick, Stefan Berger

From: Stefan Berger <stefanb@linux.ibm.com>

This patch adds support for parsing of x509 certificates that contain
NIST P256 keys that have been signed by a CA using any of the current SHA
hash algorithms. Since self-signed certificates are verified, the ecc math
for signature verification is also added.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
 crypto/Makefile                           |   9 +-
 crypto/asymmetric_keys/public_key.c       |  16 ++
 crypto/asymmetric_keys/x509_cert_parser.c |  31 ++-
 crypto/ecc.c                              | 284 ++++++++++++++++++++++
 crypto/ecc.h                              |   2 +
 crypto/ecc_curve_defs.h                   |   4 +
 crypto/eccsignature.asn1                  |   4 +
 include/linux/oid_registry.h              |   5 +
 8 files changed, 353 insertions(+), 2 deletions(-)
 create mode 100644 crypto/eccsignature.asn1

diff --git a/crypto/Makefile b/crypto/Makefile
index b279483fba50..09ae06235274 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -50,6 +50,14 @@ sm2_generic-y += sm2.o
 
 obj-$(CONFIG_CRYPTO_SM2) += sm2_generic.o
 
+$(obj)/eccsignature.asn1.o: $(obj)/eccsignature.asn1.c $(obj)/eccsignature.asn1.h
+$(obj)/ecc.o: $(obj)/sm2signature.asn1.h
+
+ecc_generic-y += eccsignature.asn1.o
+ecc_generic-y += ecc.o
+
+obj-$(CONFIG_CRYPTO_ECC) += ecc_generic.o
+
 crypto_acompress-y := acompress.o
 crypto_acompress-y += scompress.o
 obj-$(CONFIG_CRYPTO_ACOMP2) += crypto_acompress.o
@@ -172,7 +180,6 @@ obj-$(CONFIG_CRYPTO_USER_API_RNG) += algif_rng.o
 obj-$(CONFIG_CRYPTO_USER_API_AEAD) += algif_aead.o
 obj-$(CONFIG_CRYPTO_ZSTD) += zstd.o
 obj-$(CONFIG_CRYPTO_OFB) += ofb.o
-obj-$(CONFIG_CRYPTO_ECC) += ecc.o
 obj-$(CONFIG_CRYPTO_ESSIV) += essiv.o
 obj-$(CONFIG_CRYPTO_CURVE25519) += curve25519-generic.o
 
diff --git a/crypto/asymmetric_keys/public_key.c b/crypto/asymmetric_keys/public_key.c
index 8892908ad58c..0fcbaec0ded0 100644
--- a/crypto/asymmetric_keys/public_key.c
+++ b/crypto/asymmetric_keys/public_key.c
@@ -90,6 +90,22 @@ int software_key_determine_akcipher(const char *encoding,
 		return 0;
 	}
 
+	if (strcmp(encoding, "x962") == 0) {
+		enum OID oid;
+
+		if (pkey->paramlen < 2)
+			return -EINVAL;
+
+		oid = look_up_OID(pkey->params + 2, pkey->paramlen - 2);
+		switch (oid) {
+		case OID_id_prime256v1:
+			strcpy(alg_name, "nist_p256");
+			return 0;
+		default:
+			return -EINVAL;
+		}
+	}
+
 	return -ENOPKG;
 }
 
diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c
index 4643fe5ed69a..50f6ecc70d8b 100644
--- a/crypto/asymmetric_keys/x509_cert_parser.c
+++ b/crypto/asymmetric_keys/x509_cert_parser.c
@@ -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;
@@ -479,6 +505,9 @@ int x509_extract_key_data(void *context, size_t hdrlen,
 		case OID_sm2:
 			ctx->cert->pub->pkey_algo = "sm2";
 			break;
+		case OID_id_prime256v1:
+			ctx->cert->pub->pkey_algo = "ecdsa";
+			break;
 		default:
 			return -ENOPKG;
 		}
diff --git a/crypto/ecc.c b/crypto/ecc.c
index c80aa25994a0..98577ed99aaf 100644
--- a/crypto/ecc.c
+++ b/crypto/ecc.c
@@ -29,13 +29,18 @@
 #include <linux/slab.h>
 #include <linux/swab.h>
 #include <linux/fips.h>
+#include <crypto/internal/akcipher.h>
+#include <crypto/akcipher.h>
 #include <crypto/ecdh.h>
 #include <crypto/rng.h>
 #include <asm/unaligned.h>
 #include <linux/ratelimit.h>
+#include <linux/asn1_decoder.h>
+#include <linux/scatterlist.h>
 
 #include "ecc.h"
 #include "ecc_curve_defs.h"
+#include "eccsignature.asn1.h"
 
 typedef struct {
 	u64 m_low;
@@ -1542,4 +1547,283 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
 }
 EXPORT_SYMBOL(crypto_ecdh_shared_secret);
 
+struct ecc_ctx {
+	unsigned int curve_id;
+	const struct ecc_curve *curve;
+	struct ecc_point *pub_key;
+};
+
+struct ecdsa_signature_ctx {
+	const struct ecc_curve *curve;
+	u64 r[ECC_MAX_DIGITS];
+	u64 s[ECC_MAX_DIGITS];
+};
+
+/*
+ * Get the r and s components of a signature from the X509 certificate.
+ */
+static int ecc_get_signature_rs(u64 *dest, size_t hdrlen, unsigned char tag,
+				const void *value, size_t vlen,
+				unsigned int ndigits)
+{
+	size_t keylen = ndigits * sizeof(u64);
+	ssize_t diff = vlen - keylen;
+	const char *d = value;
+	u8 rs[ECC_MAX_BYTES];
+
+	if (!value || !vlen)
+		return -EINVAL;
+
+	/* diff = 0: 'value' has exacly the right size
+	 * diff > 0: 'value' has too many bytes; one leading zero is allowed that
+	 *           makes the value a positive integer; error on more
+	 * diff < 0: 'value' is missing leading zeros, which we add
+	 */
+	if (diff > 0) {
+		/* skip over leading zeros that make 'value' a positive int */
+		if (*d == 0) {
+			vlen -= 1;
+			diff--;
+			d++;
+		}
+		if (diff)
+			return -EINVAL;
+	}
+	if (-diff >= keylen)
+		return -EINVAL;
+
+	if (diff) {
+		/* leading zeros not given in 'value' */
+		memset(rs, 0, -diff);
+	}
+
+	memcpy(&rs[-diff], d, vlen);
+
+	ecc_swap_digits((u64 *)rs, dest, ndigits);
+
+	return 0;
+}
+
+int ecc_get_signature_r(void *context, size_t hdrlen, unsigned char tag,
+			const void *value, size_t vlen)
+{
+	struct ecdsa_signature_ctx *sig = context;
+
+	return ecc_get_signature_rs(sig->r, hdrlen, tag, value, vlen,
+				    sig->curve->g.ndigits);
+}
+
+int ecc_get_signature_s(void *context, size_t hdrlen, unsigned char tag,
+			const void *value, size_t vlen)
+{
+	struct ecdsa_signature_ctx *sig = context;
+
+	return ecc_get_signature_rs(sig->s, hdrlen, tag, value, vlen,
+				    sig->curve->g.ndigits);
+}
+
+static int _ecdsa_verify(struct ecc_ctx *ctx, const u64 *hash,
+			 const u64 *r, const u64 *s)
+{
+	const struct ecc_curve *curve = ctx->curve;
+	unsigned int ndigits = curve->g.ndigits;
+	u64 s1[ECC_MAX_DIGITS];
+	u64 u1[ECC_MAX_DIGITS];
+	u64 u2[ECC_MAX_DIGITS];
+	u64 x1[ECC_MAX_DIGITS];
+	u64 y1[ECC_MAX_DIGITS];
+	struct ecc_point res = ECC_POINT_INIT(x1, y1, ndigits);
+
+	/* 0 < r < n  and 0 < s < n */
+	if (vli_is_zero(r, ndigits) || vli_cmp(r, curve->n, ndigits) >= 0 ||
+	    vli_is_zero(s, ndigits) || vli_cmp(s, curve->n, ndigits) >= 0)
+		return -EBADMSG;
+
+	/* hash is given */
+	pr_devel("hash : %016llx %016llx ... %016llx\n",
+		 hash[ndigits - 1], hash[ndigits - 2], hash[0]);
+
+	/* s1 = (s^-1) mod n */
+	vli_mod_inv(s1, s, curve->n, ndigits);
+	/* u1 = (hash * s1) mod n */
+	vli_mod_mult_slow(u1, hash, s1, curve->n, ndigits);
+	/* u2 = (r * s1) mod n */
+	vli_mod_mult_slow(u2, r, s1, curve->n, ndigits);
+	/* res = u1*G + u2 * pub_key */
+	ecc_point_mult_shamir(&res, u1, &curve->g, u2, ctx->pub_key, curve);
+
+	/* res.x = res.x mod n (if res.x > order) */
+	if (unlikely(vli_cmp(res.x, curve->n, ndigits) == 1))
+		/* faster alternative for NIST p256 & p192 */
+		vli_sub(res.x, res.x, curve->n, ndigits);
+
+	if (!vli_cmp(res.x, r, ndigits))
+		return 0;
+
+	return -EKEYREJECTED;
+}
+
+/*
+ * Verify an ECDSA signature.
+ */
+static int ecdsa_verify(struct akcipher_request *req)
+{
+	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
+	struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm);
+	size_t keylen = ctx->curve->g.ndigits * sizeof(u64);
+	struct ecdsa_signature_ctx sig_ctx = {
+		.curve = ctx->curve,
+	};
+	u8 rawhash[ECC_MAX_BYTES];
+	u64 hash[ECC_MAX_DIGITS];
+	unsigned char *buffer;
+	ssize_t diff;
+	int ret;
+
+	if (unlikely(!ctx->pub_key))
+		return -EINVAL;
+
+	buffer = kmalloc(req->src_len + req->dst_len, GFP_KERNEL);
+	if (!buffer)
+		return -ENOMEM;
+
+	sg_pcopy_to_buffer(req->src,
+		sg_nents_for_len(req->src, req->src_len + req->dst_len),
+		buffer, req->src_len + req->dst_len, 0);
+
+	ret = asn1_ber_decoder(&eccsignature_decoder, &sig_ctx,
+			       buffer, req->src_len);
+	if (ret < 0)
+		goto error;
+
+	/* if the hash is shorter then we will add leading zeros to fit to ndigits */
+	diff = keylen - req->dst_len;
+	if (diff >= 0) {
+		if (diff)
+			memset(rawhash, 0, diff);
+		memcpy(&rawhash[diff], buffer + req->src_len, req->dst_len);
+	} else if (diff < 0) {
+		/* given hash is longer, we take the left-most bytes */
+		memcpy(&rawhash, buffer + req->src_len, keylen);
+	}
+
+	ecc_swap_digits((u64 *)rawhash, hash, ctx->curve->g.ndigits);
+
+	ret = _ecdsa_verify(ctx, hash, sig_ctx.r, sig_ctx.s);
+
+error:
+	kfree(buffer);
+
+	return ret;
+}
+
+static int ecc_ec_ctx_init(struct ecc_ctx *ctx, unsigned int curve_id)
+{
+	ctx->curve_id = curve_id;
+	ctx->curve = ecc_get_curve(curve_id);
+	if (!ctx->curve)
+		return -EINVAL;
+
+	return 0;
+}
+
+static void ecc_ec_ctx_deinit(struct ecc_ctx *ctx)
+{
+	ecc_free_point(ctx->pub_key);
+	ctx->pub_key = NULL;
+}
+
+static int ecc_ec_ctx_reset(struct ecc_ctx *ctx)
+{
+	unsigned int curve_id = ctx->curve_id;
+
+	ecc_ec_ctx_deinit(ctx);
+	return ecc_ec_ctx_init(ctx, curve_id);
+}
+
+/*
+ * Set the public key given the raw uncompressed key data from an X509
+ * certificate. The key data contain the concatenated X and Y coordinates of
+ * the public key.
+ */
+static int ecc_set_pub_key(struct crypto_akcipher *tfm,
+			   const void *key, unsigned int keylen)
+{
+	struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm);
+	const unsigned char *d = key;
+	const u64 *digits = (const u64 *)&d[1];
+	unsigned int ndigits;
+	int ret;
+
+	ret = ecc_ec_ctx_reset(ctx);
+	if (ret < 0)
+		return ret;
+
+	if (keylen < 1 || (((keylen - 1) >> 1) % sizeof(u64)) != 0)
+		return -EINVAL;
+	/* we only accept uncompressed format */
+	if (d[0] != 4)
+		return -EINVAL;
+
+	keylen--;
+	ndigits = (keylen >> 1) / sizeof(u64);
+	if (ndigits != ctx->curve->g.ndigits)
+		return -EINVAL;
+
+	ctx->pub_key = ecc_alloc_point(ndigits);
+	if (!ctx->pub_key)
+		return -ENOMEM;
+
+	ecc_swap_digits(digits, ctx->pub_key->x, ndigits);
+	ecc_swap_digits(&digits[ndigits], ctx->pub_key->y, ndigits);
+	return ecc_is_pubkey_valid_full(ctx->curve, ctx->pub_key);
+}
+
+static void ecc_exit_tfm(struct crypto_akcipher *tfm)
+{
+	struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm);
+
+	ecc_ec_ctx_deinit(ctx);
+}
+
+static unsigned int ecc_nist_p256_max_size(struct crypto_akcipher *tfm)
+{
+	return NIST_P256_KEY_SIZE;
+}
+
+static int ecc_nist_p256_init_tfm(struct crypto_akcipher *tfm)
+{
+	struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm);
+
+	return ecc_ec_ctx_init(ctx, ECC_CURVE_NIST_P256);
+}
+
+static struct akcipher_alg ecc_nist_p256 = {
+	.verify = ecdsa_verify,
+	.set_pub_key = ecc_set_pub_key,
+	.max_size = ecc_nist_p256_max_size,
+	.init = ecc_nist_p256_init_tfm,
+	.exit = ecc_exit_tfm,
+	.base = {
+		.cra_name = "nist_p256",
+		.cra_driver_name = "ecc-nist-p256",
+		.cra_priority = 100,
+		.cra_module = THIS_MODULE,
+		.cra_ctxsize = sizeof(struct ecc_ctx),
+	},
+};
+
+static int ecc_init(void)
+{
+	return crypto_register_akcipher(&ecc_nist_p256);
+}
+
+static void ecc_exit(void)
+{
+	crypto_unregister_akcipher(&ecc_nist_p256);
+}
+
+subsys_initcall(ecc_init);
+module_exit(ecc_exit);
+
 MODULE_LICENSE("Dual BSD/GPL");
diff --git a/crypto/ecc.h b/crypto/ecc.h
index d4e546b9ad79..26d7c83908bc 100644
--- a/crypto/ecc.h
+++ b/crypto/ecc.h
@@ -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
  *
diff --git a/crypto/ecc_curve_defs.h b/crypto/ecc_curve_defs.h
index 69be6c7d228f..562651f28ef5 100644
--- a/crypto/ecc_curve_defs.h
+++ b/crypto/ecc_curve_defs.h
@@ -28,6 +28,8 @@ static struct ecc_curve nist_p192 = {
 	.b = nist_p192_b
 };
 
+#define NIST_P192_KEY_SIZE 24
+
 /* NIST P-256: a = p - 3 */
 static u64 nist_p256_g_x[] = { 0xF4A13945D898C296ull, 0x77037D812DEB33A0ull,
 				0xF8BCE6E563A440F2ull, 0x6B17D1F2E12C4247ull };
@@ -54,4 +56,6 @@ static struct ecc_curve nist_p256 = {
 	.b = nist_p256_b
 };
 
+#define NIST_P256_KEY_SIZE 32
+
 #endif
diff --git a/crypto/eccsignature.asn1 b/crypto/eccsignature.asn1
new file mode 100644
index 000000000000..e6c82381f19d
--- /dev/null
+++ b/crypto/eccsignature.asn1
@@ -0,0 +1,4 @@
+ECDSASignature ::= SEQUENCE {
+	r	INTEGER ({ ecc_get_signature_r }),
+	s	INTEGER ({ ecc_get_signature_s })
+}
diff --git a/include/linux/oid_registry.h b/include/linux/oid_registry.h
index 4462ed2c18cd..9060f19c80eb 100644
--- a/include/linux/oid_registry.h
+++ b/include/linux/oid_registry.h
@@ -21,6 +21,11 @@ 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_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 */
-- 
2.25.4


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v3 3/3] x509: Add support for NIST p192 keys in certificates and akcipher
  2021-01-27 12:33 [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys Stefan Berger
  2021-01-27 12:33 ` [PATCH v3 1/3] x509: Detect sm2 keys by their parameters OID Stefan Berger
  2021-01-27 12:33 ` [PATCH v3 2/3] x509: Add support for parsing x509 certs with NIST p256 keys Stefan Berger
@ 2021-01-27 12:33 ` Stefan Berger
  2021-01-27 13:05 ` [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys David Howells
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 15+ messages in thread
From: Stefan Berger @ 2021-01-27 12:33 UTC (permalink / raw)
  To: dhowells, keyrings
  Cc: linux-kernel, herbert, davem, linux-crypto, patrick, Stefan Berger

From: Stefan Berger <stefanb@linux.ibm.com>

Add support for NIST p192 keys in x509 certificates and support it in
'akcipher'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
 crypto/asymmetric_keys/public_key.c       |  3 ++
 crypto/asymmetric_keys/x509_cert_parser.c |  1 +
 crypto/ecc.c                              | 36 ++++++++++++++++++++++-
 include/linux/oid_registry.h              |  1 +
 4 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/crypto/asymmetric_keys/public_key.c b/crypto/asymmetric_keys/public_key.c
index 0fcbaec0ded0..bb4a7cc0e3c8 100644
--- a/crypto/asymmetric_keys/public_key.c
+++ b/crypto/asymmetric_keys/public_key.c
@@ -98,6 +98,9 @@ int software_key_determine_akcipher(const char *encoding,
 
 		oid = look_up_OID(pkey->params + 2, pkey->paramlen - 2);
 		switch (oid) {
+		case OID_id_prime192v1:
+			strcpy(alg_name, "nist_p192");
+			return 0;
 		case OID_id_prime256v1:
 			strcpy(alg_name, "nist_p256");
 			return 0;
diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c
index 50f6ecc70d8b..5ff891f8235d 100644
--- a/crypto/asymmetric_keys/x509_cert_parser.c
+++ b/crypto/asymmetric_keys/x509_cert_parser.c
@@ -505,6 +505,7 @@ 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;
diff --git a/crypto/ecc.c b/crypto/ecc.c
index 98577ed99aaf..0690c37f74e3 100644
--- a/crypto/ecc.c
+++ b/crypto/ecc.c
@@ -1813,13 +1813,47 @@ static struct akcipher_alg ecc_nist_p256 = {
 	},
 };
 
+static unsigned int ecc_nist_p192_max_size(struct crypto_akcipher *tfm)
+{
+	return NIST_P192_KEY_SIZE;
+}
+
+static int ecc_nist_p192_init_tfm(struct crypto_akcipher *tfm)
+{
+	struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm);
+
+	return ecc_ec_ctx_init(ctx, ECC_CURVE_NIST_P192);
+}
+
+static struct akcipher_alg ecc_nist_p192 = {
+	.verify = ecdsa_verify,
+	.set_pub_key = ecc_set_pub_key,
+	.max_size = ecc_nist_p192_max_size,
+	.init = ecc_nist_p192_init_tfm,
+	.exit = ecc_exit_tfm,
+	.base = {
+		.cra_name = "nist_p192",
+		.cra_driver_name = "ecc-nist-p192",
+		.cra_priority = 100,
+		.cra_module = THIS_MODULE,
+		.cra_ctxsize = sizeof(struct ecc_ctx),
+	},
+};
+
 static int ecc_init(void)
 {
-	return crypto_register_akcipher(&ecc_nist_p256);
+	int ret;
+
+	ret = crypto_register_akcipher(&ecc_nist_p256);
+	if (ret)
+		return ret;
+
+	return crypto_register_akcipher(&ecc_nist_p192);
 }
 
 static void ecc_exit(void)
 {
+	crypto_unregister_akcipher(&ecc_nist_p192);
 	crypto_unregister_akcipher(&ecc_nist_p256);
 }
 
diff --git a/include/linux/oid_registry.h b/include/linux/oid_registry.h
index 9060f19c80eb..e8071133d0e2 100644
--- a/include/linux/oid_registry.h
+++ b/include/linux/oid_registry.h
@@ -21,6 +21,7 @@ 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 */
-- 
2.25.4


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys
  2021-01-27 12:33 [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys Stefan Berger
                   ` (2 preceding siblings ...)
  2021-01-27 12:33 ` [PATCH v3 3/3] x509: Add support for NIST p192 keys in certificates and akcipher Stefan Berger
@ 2021-01-27 13:05 ` David Howells
  2021-01-27 14:22 ` David Howells
  2021-01-27 16:12 ` Nym Seddon
  5 siblings, 0 replies; 15+ messages in thread
From: David Howells @ 2021-01-27 13:05 UTC (permalink / raw)
  To: Stefan Berger
  Cc: dhowells, keyrings, linux-kernel, herbert, davem, linux-crypto,
	patrick, Stefan Berger

Stefan Berger <stefanb@linux.vnet.ibm.com> wrote:

> k=$(keyctrl newring test @u)

keyctl - but I can fix that.

David


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys
  2021-01-27 12:33 [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys Stefan Berger
                   ` (3 preceding siblings ...)
  2021-01-27 13:05 ` [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys David Howells
@ 2021-01-27 14:22 ` David Howells
  2021-01-27 19:32   ` Herbert Xu
  2021-01-27 21:08   ` David Howells
  2021-01-27 16:12 ` Nym Seddon
  5 siblings, 2 replies; 15+ messages in thread
From: David Howells @ 2021-01-27 14:22 UTC (permalink / raw)
  To: Stefan Berger
  Cc: dhowells, keyrings, linux-kernel, herbert, davem, linux-crypto,
	patrick, Stefan Berger

Stefan Berger <stefanb@linux.vnet.ibm.com> wrote:

> 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=$(keyctrl 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.

I've pulled this into my keys-next branch.

David


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys
  2021-01-27 12:33 [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys Stefan Berger
                   ` (4 preceding siblings ...)
  2021-01-27 14:22 ` David Howells
@ 2021-01-27 16:12 ` Nym Seddon
  2021-01-27 22:42   ` Stefan Berger
  5 siblings, 1 reply; 15+ messages in thread
From: Nym Seddon @ 2021-01-27 16:12 UTC (permalink / raw)
  To: Stefan Berger
  Cc: dhowells, keyrings, linux-kernel, herbert, davem, linux-crypto,
	patrick, Stefan Berger

Hi Stefan,

In the recommendations from SafeCurves (https://safecurves.cr.yp.to/twist.html) there are a number of attacks against ECC twists. Two of those attacks are relevant against NIST P192: invalid-curve attacks and invalid-curve attacks against ladders.

Both attacks can be mitigated by checking the supplied public key is on the correct curve, before performing curve operations.

Not sure if the right place for those checks are in the signature verification code provided in these patches, or when reading public keys from the certificates. Does the kernel provide functions for checking curve points satisfy their respective curve equations?

There are also tables describing the cost of combined attacks on various curves, where NIST P224 already falls below the safe threshold. Because of that, I would recommend not implementing support for NIST P192 (since it would fair even worse).

What are your thoughts?

Best,
Nym

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Wednesday, January 27, 2021 12:33 PM, Stefan Berger <stefanb@linux.vnet.ibm.com> wrote:

> From: Stefan Berger stefanb@linux.ibm.com
>
> 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=$(keyctrl 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
>
> 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 (3):
>     x509: Detect sm2 keys by their parameters OID
>     x509: Add support for parsing x509 certs with NIST p256 keys
>     x509: Add support for NIST p192 keys in certificates and akcipher
>
>     crypto/Makefile | 9 +-
>     crypto/asymmetric_keys/public_key.c | 19 ++
>     crypto/asymmetric_keys/x509_cert_parser.c | 45 ++-
>     crypto/ecc.c | 318 ++++++++++++++++++++++
>     crypto/ecc.h | 2 +
>     crypto/ecc_curve_defs.h | 4 +
>     crypto/eccsignature.asn1 | 4 +
>     include/linux/oid_registry.h | 6 +
>     8 files changed, 404 insertions(+), 3 deletions(-)
>     create mode 100644 crypto/eccsignature.asn1
>
>     --
>     2.25.4
>



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v3 2/3] x509: Add support for parsing x509 certs with NIST p256 keys
  2021-01-27 12:33 ` [PATCH v3 2/3] x509: Add support for parsing x509 certs with NIST p256 keys Stefan Berger
@ 2021-01-27 19:31   ` Herbert Xu
  2021-01-27 22:39     ` Stefan Berger
  0 siblings, 1 reply; 15+ messages in thread
From: Herbert Xu @ 2021-01-27 19:31 UTC (permalink / raw)
  To: Stefan Berger
  Cc: dhowells, keyrings, linux-kernel, davem, linux-crypto, patrick,
	Stefan Berger

On Wed, Jan 27, 2021 at 07:33:49AM -0500, Stefan Berger wrote:
>
> +static struct akcipher_alg ecc_nist_p256 = {
> +	.verify = ecdsa_verify,
> +	.set_pub_key = ecc_set_pub_key,
> +	.max_size = ecc_nist_p256_max_size,
> +	.init = ecc_nist_p256_init_tfm,
> +	.exit = ecc_exit_tfm,
> +	.base = {
> +		.cra_name = "nist_p256",
> +		.cra_driver_name = "ecc-nist-p256",
> +		.cra_priority = 100,
> +		.cra_module = THIS_MODULE,
> +		.cra_ctxsize = sizeof(struct ecc_ctx),
> +	},
> +};

This is not how we name generic algorithms.

Please split this out and submit them through the crypto tree
instead.

Thanks,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys
  2021-01-27 14:22 ` David Howells
@ 2021-01-27 19:32   ` Herbert Xu
  2021-01-27 21:08   ` David Howells
  1 sibling, 0 replies; 15+ messages in thread
From: Herbert Xu @ 2021-01-27 19:32 UTC (permalink / raw)
  To: David Howells
  Cc: Stefan Berger, keyrings, linux-kernel, davem, linux-crypto,
	patrick, Stefan Berger

On Wed, Jan 27, 2021 at 02:22:08PM +0000, David Howells wrote:
>
> I've pulled this into my keys-next branch.

David, please drop them because there are issues with the Crypto API
bits.

Thanks,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys
  2021-01-27 14:22 ` David Howells
  2021-01-27 19:32   ` Herbert Xu
@ 2021-01-27 21:08   ` David Howells
  1 sibling, 0 replies; 15+ messages in thread
From: David Howells @ 2021-01-27 21:08 UTC (permalink / raw)
  To: Herbert Xu
  Cc: dhowells, Stefan Berger, keyrings, linux-kernel, davem,
	linux-crypto, patrick, Stefan Berger

Herbert Xu <herbert@gondor.apana.org.au> wrote:

> > I've pulled this into my keys-next branch.
> 
> David, please drop them because there are issues with the Crypto API
> bits.

Okay, dropped.

David


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v3 2/3] x509: Add support for parsing x509 certs with NIST p256 keys
  2021-01-27 19:31   ` Herbert Xu
@ 2021-01-27 22:39     ` Stefan Berger
  0 siblings, 0 replies; 15+ messages in thread
From: Stefan Berger @ 2021-01-27 22:39 UTC (permalink / raw)
  To: Herbert Xu, Stefan Berger
  Cc: dhowells, keyrings, linux-kernel, davem, linux-crypto, patrick

On 1/27/21 2:31 PM, Herbert Xu wrote:
> On Wed, Jan 27, 2021 at 07:33:49AM -0500, Stefan Berger wrote:
>> +static struct akcipher_alg ecc_nist_p256 = {
>> +	.verify = ecdsa_verify,
>> +	.set_pub_key = ecc_set_pub_key,
>> +	.max_size = ecc_nist_p256_max_size,
>> +	.init = ecc_nist_p256_init_tfm,
>> +	.exit = ecc_exit_tfm,
>> +	.base = {
>> +		.cra_name = "nist_p256",
>> +		.cra_driver_name = "ecc-nist-p256",
>> +		.cra_priority = 100,
>> +		.cra_module = THIS_MODULE,
>> +		.cra_ctxsize = sizeof(struct ecc_ctx),
>> +	},
>> +};
> This is not how we name generic algorithms.


Are saying that I cannot name it "nist_p256" but should call it 'ecdsa' 
and the driver becomes "ecdsa-generic"?



>
> Please split this out and submit them through the crypto tree
> instead.

Will do.


>
> Thanks,



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys
  2021-01-27 16:12 ` Nym Seddon
@ 2021-01-27 22:42   ` Stefan Berger
  0 siblings, 0 replies; 15+ messages in thread
From: Stefan Berger @ 2021-01-27 22:42 UTC (permalink / raw)
  To: Nym Seddon, Stefan Berger
  Cc: dhowells, keyrings, linux-kernel, herbert, davem, linux-crypto, patrick

On 1/27/21 11:12 AM, Nym Seddon wrote:
> Hi Stefan,
>
> In the recommendations from SafeCurves (https://safecurves.cr.yp.to/twist.html) there are a number of attacks against ECC twists. Two of those attacks are relevant against NIST P192: invalid-curve attacks and invalid-curve attacks against ladders.
>
> Both attacks can be mitigated by checking the supplied public key is on the correct curve, before performing curve operations.
>
> Not sure if the right place for those checks are in the signature verification code provided in these patches, or when reading public keys from the certificates. Does the kernel provide functions for checking curve points satisfy their respective curve equations?
>
> There are also tables describing the cost of combined attacks on various curves, where NIST P224 already falls below the safe threshold. Because of that, I would recommend not implementing support for NIST P192 (since it would fair even worse).
>
> What are your thoughts?


I am calling into a function performing such a test at the end of the 
function parsing the public key:

  return ecc_is_pubkey_valid_full(ctx->curve, ctx->pub_key)

https://elixir.bootlin.com/linux/latest/source/crypto/ecc.c#L1458

Is that good 'enough' ?

    Stefan


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v3 1/3] x509: Detect sm2 keys by their parameters OID
  2021-01-27 12:33 ` [PATCH v3 1/3] x509: Detect sm2 keys by their parameters OID Stefan Berger
@ 2021-01-30 21:26   ` Jarkko Sakkinen
  2021-01-31  2:57     ` Stefan Berger
  0 siblings, 1 reply; 15+ messages in thread
From: Jarkko Sakkinen @ 2021-01-30 21:26 UTC (permalink / raw)
  To: Stefan Berger, dhowells, keyrings
  Cc: linux-kernel, herbert, davem, linux-crypto, patrick, Stefan Berger

On Wed, 2021-01-27 at 07:33 -0500, Stefan Berger wrote:
> From: Stefan Berger <stefanb@linux.ibm.com>
> 
> 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.
> 
> Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
> ---
>  crypto/asymmetric_keys/x509_cert_parser.c | 13 ++++++++++++-
>  1 file changed, 12 insertions(+), 1 deletion(-)
> 
> diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c
> index 52c9b455fc7d..4643fe5ed69a 100644
> --- a/crypto/asymmetric_keys/x509_cert_parser.c
> +++ b/crypto/asymmetric_keys/x509_cert_parser.c
> @@ -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,17 @@ 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 (ctx->params_size < 2)

Either a named constant, or at least a comment instead of just '2'.


> +                       return -ENOPKG;
> +
> +               oid = look_up_OID(ctx->params + 2, ctx->params_size - 2);
> +               switch (oid) {
> +               case OID_sm2:
> +                       ctx->cert->pub->pkey_algo = "sm2";
> +                       break;
> +               default:
> +                       return -ENOPKG;
> +               }
>                 break;
>         default:
>                 return -ENOPKG;

/Jarkko

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v3 1/3] x509: Detect sm2 keys by their parameters OID
  2021-01-30 21:26   ` Jarkko Sakkinen
@ 2021-01-31  2:57     ` Stefan Berger
  2021-02-02 15:16       ` Jarkko Sakkinen
  0 siblings, 1 reply; 15+ messages in thread
From: Stefan Berger @ 2021-01-31  2:57 UTC (permalink / raw)
  To: Jarkko Sakkinen, Stefan Berger, dhowells, keyrings
  Cc: linux-kernel, herbert, davem, linux-crypto, patrick

On 1/30/21 4:26 PM, Jarkko Sakkinen wrote:
> On Wed, 2021-01-27 at 07:33 -0500, Stefan Berger wrote:
>> From: Stefan Berger <stefanb@linux.ibm.com>
>>
>> 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.
>>
>> Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
>> ---
>>   crypto/asymmetric_keys/x509_cert_parser.c | 13 ++++++++++++-
>>   1 file changed, 12 insertions(+), 1 deletion(-)
>>
>> diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c
>> index 52c9b455fc7d..4643fe5ed69a 100644
>> --- a/crypto/asymmetric_keys/x509_cert_parser.c
>> +++ b/crypto/asymmetric_keys/x509_cert_parser.c
>> @@ -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,17 @@ 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 (ctx->params_size < 2)
> Either a named constant, or at least a comment instead of just '2'.


I will look at the 2 entries whether they contain the expected values: 
ASN1_OID and length

Thanks!

    Stefan


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v3 1/3] x509: Detect sm2 keys by their parameters OID
  2021-01-31  2:57     ` Stefan Berger
@ 2021-02-02 15:16       ` Jarkko Sakkinen
  0 siblings, 0 replies; 15+ messages in thread
From: Jarkko Sakkinen @ 2021-02-02 15:16 UTC (permalink / raw)
  To: Stefan Berger
  Cc: Stefan Berger, dhowells, keyrings, linux-kernel, herbert, davem,
	linux-crypto, patrick

On Sat, Jan 30, 2021 at 09:57:40PM -0500, Stefan Berger wrote:
> On 1/30/21 4:26 PM, Jarkko Sakkinen wrote:
> > On Wed, 2021-01-27 at 07:33 -0500, Stefan Berger wrote:
> > > From: Stefan Berger <stefanb@linux.ibm.com>
> > > 
> > > 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.
> > > 
> > > Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
> > > ---
> > >   crypto/asymmetric_keys/x509_cert_parser.c | 13 ++++++++++++-
> > >   1 file changed, 12 insertions(+), 1 deletion(-)
> > > 
> > > diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c
> > > index 52c9b455fc7d..4643fe5ed69a 100644
> > > --- a/crypto/asymmetric_keys/x509_cert_parser.c
> > > +++ b/crypto/asymmetric_keys/x509_cert_parser.c
> > > @@ -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,17 @@ 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 (ctx->params_size < 2)
> > Either a named constant, or at least a comment instead of just '2'.
> 
> 
> I will look at the 2 entries whether they contain the expected values:
> ASN1_OID and length
> 
> Thanks!
> 
>    Stefan

Just add inline comment that explains that.

/Jarkko

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2021-02-02 15:20 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-27 12:33 [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys Stefan Berger
2021-01-27 12:33 ` [PATCH v3 1/3] x509: Detect sm2 keys by their parameters OID Stefan Berger
2021-01-30 21:26   ` Jarkko Sakkinen
2021-01-31  2:57     ` Stefan Berger
2021-02-02 15:16       ` Jarkko Sakkinen
2021-01-27 12:33 ` [PATCH v3 2/3] x509: Add support for parsing x509 certs with NIST p256 keys Stefan Berger
2021-01-27 19:31   ` Herbert Xu
2021-01-27 22:39     ` Stefan Berger
2021-01-27 12:33 ` [PATCH v3 3/3] x509: Add support for NIST p192 keys in certificates and akcipher Stefan Berger
2021-01-27 13:05 ` [PATCH v3 0/3] Add support for x509 certs with NIST p256 and p192 keys David Howells
2021-01-27 14:22 ` David Howells
2021-01-27 19:32   ` Herbert Xu
2021-01-27 21:08   ` David Howells
2021-01-27 16:12 ` Nym Seddon
2021-01-27 22:42   ` Stefan Berger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).