Skip to contentSkip to conversation

crypto: Add EC-RDSA algorithm

RFC PATCH Superseded Vitaly Chikunov 4 participants 24 messages Jan 6, 2019 – Mar 1, 2019

Subsystems (7)

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

cover crypto: Add EC-RDSA algorithm

This patchset changes akcipher api to support ECDSA style signature verification. Augments x509 parser to make it work with EC-RDSA certificates. And finally implements EC-RDSA (GOST 34.10) signature verification.

It is intended to use in IMA for appraisal.

Tested on x86_64 with openssl+gost-engine generated certificates.

Vitaly Chikunov (4):
X.509: Parse public key parameters from x509 for akcipher
akcipher: Introduce verify2 for public key algorithms
KEYS: set correct flags for keyctl if encrypt is not supported
crypto: Add EC-RDSA algorithm

 crypto/Kconfig                            |  63 ++--
 crypto/Makefile                           |   5 +-
 crypto/asymmetric_keys/public_key.c       |  74 +++--
 crypto/asymmetric_keys/x509.asn1          |   2 +-
 crypto/asymmetric_keys/x509_cert_parser.c |  51 ++-
 crypto/ecc.c                              | 421 +++++++++++++++++++++++--
 crypto/ecc.h                              | 162 +++++++++-
 crypto/ecc_curve_defs.h                   |  15 -
 crypto/ecrdsa.c                           | 494 ++++++++++++++++++++++++++++++
 crypto/testmgr.c                          |   5 +
 crypto/testmgr.h                          |   3 +
 include/crypto/akcipher.h                 |  87 +++++-
 include/crypto/public_key.h               |   4 +
 include/linux/oid_registry.h              |  18 ++
 14 files changed, 1319 insertions(+), 85 deletions(-)
 create mode 100644 crypto/ecrdsa.c
Signature · 1 line
2.11.0
#1 lore raw

RFC PATCH 1/4 X.509: Parse public key parameters from x509 for akcipher

Some public key algorithms (like ECDSA) keep in parameters field important data such as digest and curve OIDs (possibly more for different ECDSA variants). Thus, just setting a public key (as for RSA) is not enough.

Introduce set_params() callback for akcipher which will be used to pass DER encoded parameters array, with additional argument of algorithm OID.

This is done with the intent of adding support for EC-RDSA (ISO/IEC 14888-3:2018, RFC 7091, and basically ECDSA variant) public keys (which will be finally used in IMA subsystem). Thus, also oid_registry.h is updated.

Rationale:

- For such keys just setting public key without parameters is meaningless, so it would be possible to add parameters in crypto_akcipher_set_pub_key (and .set_pub_key) calls. But, this will needlessly change API for RSA akcipher. Also, additional callback making it possible to pass parameters after crypto_akcipher_set_priv_key (and .set_priv_key) in the future.

- Algorithm OID is passed to be validated in .set_params callback, otherwise, it could have the wrong value.

- Particular algorithm OIDs are checked in x509_note_params, (because this is called from AlgorithmIdentifier (ASN.1) parser, which is called multiple times, as it's used multiple times in X.509 certificate), to distinguish a public key call from a signature call.

---
 crypto/asymmetric_keys/public_key.c       | 16 +++++++++++++
 crypto/asymmetric_keys/x509.asn1          |  2 +-
 crypto/asymmetric_keys/x509_cert_parser.c | 38 ++++++++++++++++++++++++++++---
 crypto/testmgr.c                          |  5 ++++
 crypto/testmgr.h                          |  3 +++
 include/crypto/akcipher.h                 | 33 +++++++++++++++++++++++++++
 include/crypto/public_key.h               |  4 ++++
 include/linux/oid_registry.h              |  6 +++++
 8 files changed, 103 insertions(+), 4 deletions(-)
crypto/asymmetric_keys/public_key.c +16 -0
index f5d85b47fcc6..3bc090b8adef 100644
@@ -45,6 +45,7 @@ void public_key_free(struct public_key *key)
 {
 	if (key) {
 		kfree(key->key);
+		kfree(key->params);
 		kfree(key);
 	}
 }
@@ -124,6 +125,11 @@ static int software_key_query(const struct kernel_pkey_params *params,
 	if (ret < 0)
 		goto error_free_tfm;
 
+	ret = crypto_akcipher_set_params(tfm, pkey->algo, pkey->params,
+					 pkey->paramlen);
+	if (ret)
+		goto error_free_tfm;
+
 	len = crypto_akcipher_maxsize(tfm);
 	info->key_size = len * 8;
 	info->max_data_size = len;
@@ -182,6 +188,11 @@ static int software_key_eds_op(struct kernel_pkey_params *params,
 	if (ret)
 		goto error_free_req;
 
+	ret = crypto_akcipher_set_params(tfm, pkey->algo, pkey->params,
+					 pkey->paramlen);
+	if (ret)
+		goto error_free_req;
+
 	sg_init_one(&in_sg, in, params->in_len);
 	sg_init_one(&out_sg, out, params->out_len);
 	akcipher_request_set_crypt(req, &in_sg, &out_sg, params->in_len,
@@ -263,6 +274,11 @@ int public_key_verify_signature(const struct public_key *pkey,
 	if (ret)
 		goto error_free_req;
 
+	ret = crypto_akcipher_set_params(tfm, pkey->algo, pkey->params,
+					 pkey->paramlen);
+	if (ret)
+		goto error_free_req;
+
 	ret = -ENOMEM;
 	outlen = crypto_akcipher_maxsize(tfm);
 	output = kmalloc(outlen, GFP_KERNEL);
crypto/asymmetric_keys/x509.asn1 +1 -1
index aae0cde414e2..5c9f4e4a5231 100644
@@ -22,7 +22,7 @@ CertificateSerialNumber ::= INTEGER
 
 AlgorithmIdentifier ::= SEQUENCE {
 	algorithm		OBJECT IDENTIFIER ({ x509_note_OID }),
-	parameters		ANY OPTIONAL
+	parameters		ANY OPTIONAL ({ x509_note_params })
 }
 
 Name ::= SEQUENCE OF RelativeDistinguishedName
crypto/asymmetric_keys/x509_cert_parser.c +35 -3
index 991f4d735a4e..202a9dc66c93 100644
@@ -26,6 +26,9 @@ struct x509_parse_context {
 	const void	*cert_start;		/* Start of cert content */
 	const void	*key;			/* Key data */
 	size_t		key_size;		/* Size of key data */
+	const void	*params;		/* Key parameters */
+	size_t		params_size;		/* Size of key parameters */
+	enum OID	key_algo;		/* Public key algorithm */
 	enum OID	last_oid;		/* Last OID encountered */
 	enum OID	algo_oid;		/* Algorithm OID */
 	unsigned char	nr_mpi;			/* Number of MPIs stored */
@@ -109,6 +112,13 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen)
 
 	cert->pub->keylen = ctx->key_size;
 
+	cert->pub->params = kmemdup(ctx->params, ctx->params_size, GFP_KERNEL);
+	if (!cert->pub->params)
+		goto error_decode;
+
+	cert->pub->paramlen = ctx->params_size;
+	cert->pub->algo = ctx->key_algo;
+
 	/* Grab the signature bits */
 	ret = x509_get_sig_params(cert);
 	if (ret < 0)
@@ -401,6 +411,23 @@ int x509_note_subject(void *context, size_t hdrlen,
 }
 
 /*
+ * Extract parameters for particular keys
+ */
+int x509_note_params(void *context, size_t hdrlen,
+		     unsigned char tag,
+		     const void *value, size_t vlen)
+{
+	struct x509_parse_context *ctx = context;
+
+	if (ctx->last_oid != OID_gost2012PublicKey256 &&
+	    ctx->last_oid != OID_gost2012PublicKey512)
+		return 0;
+	ctx->params = value;
+	ctx->params_size = vlen;
+	return 0;
+}
+
+/*
  * Extract the data for the public key algorithm
  */
 int x509_extract_key_data(void *context, size_t hdrlen,
@@ -409,16 +436,21 @@ int x509_extract_key_data(void *context, size_t hdrlen,
 {
 	struct x509_parse_context *ctx = context;
 
-	if (ctx->last_oid != OID_rsaEncryption)
+	ctx->key_algo = ctx->last_oid;
+	if (ctx->last_oid = OID_rsaEncryption)
+		ctx->cert->pub->pkey_algo = "rsa";
+	else if (ctx->last_oid = OID_gost2012PublicKey256 ||
+		 ctx->last_oid = OID_gost2012PublicKey512)
+		ctx->cert->pub->pkey_algo = "ecrdsa";
+	else
 		return -ENOPKG;
 
-	ctx->cert->pub->pkey_algo = "rsa";
-
 	/* Discard the BIT STRING metadata */
 	if (vlen < 1 || *(const u8 *)value != 0)
 		return -EBADMSG;
 	ctx->key = value + 1;
 	ctx->key_size = vlen - 1;
+
 	return 0;
 }
 
crypto/testmgr.c +5 -0
index 0f684a414acb..a030526a6609 100644
@@ -2257,6 +2257,11 @@ static int test_akcipher_one(struct crypto_akcipher *tfm,
 	if (err)
 		goto free_req;
 
+	err = crypto_akcipher_set_params(tfm, vecs->algo, vecs->params,
+					 vecs->param_len);
+	if (err)
+		goto free_req;
+
 	err = -ENOMEM;
 	out_len_max = crypto_akcipher_maxsize(tfm);
 	outbuf_enc = kzalloc(out_len_max, GFP_KERNEL);
crypto/testmgr.h +3 -0
index e7e56a8febbc..9855da080eaf 100644
@@ -124,13 +124,16 @@ struct drbg_testvec {
 
 struct akcipher_testvec {
 	const unsigned char *key;
+	const unsigned char *params;
 	const unsigned char *m;
 	const unsigned char *c;
 	unsigned int key_len;
+	unsigned int param_len;
 	unsigned int m_size;
 	unsigned int c_size;
 	bool public_key_vec;
 	bool siggen_sigver_test;
+	enum OID algo;
 };
 
 struct kpp_testvec {
include/crypto/akcipher.h +33 -0
index afac71119396..84d68dfb551a 100644
@@ -13,6 +13,7 @@
 #ifndef _CRYPTO_AKCIPHER_H
 #define _CRYPTO_AKCIPHER_H
 #include <linux/crypto.h>
+#include <linux/oid_registry.h>
 
 /**
  * struct akcipher_request - public key request
@@ -73,6 +74,9 @@ struct crypto_akcipher {
  * @set_priv_key: Function invokes the algorithm specific set private key
  *		function, which knows how to decode and interpret
  *		the BER encoded private key
+ * @set_params: Function invokes the algorithm specific set parameters
+ *		function, which knows how to decode and interpret
+ *		the DER encoded public key parameters
  * @max_size:	Function returns dest buffer size required for a given key.
  * @init:	Initialize the cryptographic transformation object.
  *		This function is used to initialize the cryptographic
@@ -98,6 +102,8 @@ struct akcipher_alg {
 			   unsigned int keylen);
 	int (*set_priv_key)(struct crypto_akcipher *tfm, const void *key,
 			    unsigned int keylen);
+	int (*set_params)(struct crypto_akcipher *tfm, enum OID algo,
+			  const void *params, unsigned int paramlen);
 	unsigned int (*max_size)(struct crypto_akcipher *tfm);
 	int (*init)(struct crypto_akcipher *tfm);
 	void (*exit)(struct crypto_akcipher *tfm);
@@ -452,4 +458,31 @@ static inline int crypto_akcipher_set_priv_key(struct crypto_akcipher *tfm,
 
 	return alg->set_priv_key(tfm, key, keylen);
 }
+
+/**
+ * crypto_akcipher_set_params() - Invoke set parameters operation
+ *
+ * Function invokes the algorithm specific set parameters function, which
+ * knows how to decode and interpret the encoded parameters
+ *
+ * @tfm:	tfm handle
+ * @algo:	OID of the key algorithm
+ * @params:	DER encoded key parameters
+ * @paramlen:	length of the parameters
+ *
+ * Return: zero on success; error code in case of error
+ */
+static inline int crypto_akcipher_set_params(struct crypto_akcipher *tfm,
+					     enum OID algo,
+					     const void *params,
+					     unsigned int paramlen)
+{
+	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
+
+	if (alg->set_params)
+		return alg->set_params(tfm, algo, params, paramlen);
+	if (!params || !paramlen)
+		return 0;
+	return -ENOTSUPP;
+}
 #endif
include/crypto/public_key.h +4 -0
index be626eac9113..712fe1214b5f 100644
@@ -15,6 +15,7 @@
 #define _LINUX_PUBLIC_KEY_H
 
 #include <linux/keyctl.h>
+#include <linux/oid_registry.h>
 
 /*
  * Cryptographic data for the public-key subtype of the asymmetric key type.
@@ -25,6 +26,9 @@
 struct public_key {
 	void *key;
 	u32 keylen;
+	enum OID algo;
+	void *params;
+	u32 paramlen;
 	bool key_is_private;
 	const char *id_type;
 	const char *pkey_algo;
include/linux/oid_registry.h +6 -0
index d2fa9ca42e9a..e2e323fd4826 100644
@@ -93,6 +93,12 @@ enum OID {
 	OID_authorityKeyIdentifier,	/* 2.5.29.35 */
 	OID_extKeyUsage,		/* 2.5.29.37 */
 
+	/* EC-RDSA */
+	OID_gost2012PublicKey256,	/* 1.2.643.7.1.1.1.1 */
+	OID_gost2012PublicKey512,	/* 1.2.643.7.1.1.1.2 */
+	OID_gost2012Signature256,	/* 1.2.643.7.1.1.3.2 */
+	OID_gost2012Signature512,	/* 1.2.643.7.1.1.3.3 */
+
 	OID__NR
 };
 
Signature · 1 line
2.11.0
#2 lore raw

Re: [RFC PATCH 1/4] X.509: Parse public key parameters from x509 for akcipher

Quoted 35 lines · Vitaly Chikunov

Some public key algorithms (like ECDSA) keep in parameters field important data such as digest and curve OIDs (possibly more for different ECDSA variants). Thus, just setting a public key (as for RSA) is not enough.

Introduce set_params() callback for akcipher which will be used to pass DER encoded parameters array, with additional argument of algorithm OID.

This is done with the intent of adding support for EC-RDSA (ISO/IEC 14888-3:2018, RFC 7091, and basically ECDSA variant) public keys (which will be finally used in IMA subsystem). Thus, also oid_registry.h is updated.

Rationale:

- For such keys just setting public key without parameters is meaningless, so it would be possible to add parameters in crypto_akcipher_set_pub_key (and .set_pub_key) calls. But, this will needlessly change API for RSA akcipher. Also, additional callback making it possible to pass parameters after crypto_akcipher_set_priv_key (and .set_priv_key) in the future.

- Algorithm OID is passed to be validated in .set_params callback, otherwise, it could have the wrong value.

- Particular algorithm OIDs are checked in x509_note_params, (because this is called from AlgorithmIdentifier (ASN.1) parser, which is called multiple times, as it's used multiple times in X.509 certificate), to distinguish a public key call from a signature call.

---

Review int public_key_verify_signature(const struct public_key *pkey,

Quoted 10 lines
(quoted diff) +2 -0
@@ -263,6 +274,11 @@ int public_key_verify_signature(const struct public_key *pkey,
       if (pkey->key_is_private)
               ret = crypto_akcipher_set_priv_key(tfm,
                                                  pkey->key, pkey->keylen);
       else
               ret = crypto_akcipher_set_pub_key(tfm,
                                                 pkey->key, pkey->keylen);
       if (ret)
               goto error_free_req;
+	ret = crypto_akcipher_set_params(tfm, pkey->algo, pkey->params,
+					 pkey->paramlen);

Nobody said anything if this is a good idea to call set_params after set_{pub,priv}_key and not before.

When `struct crypto_akcipher' is allocated ctx data is never zeroed, thus either call will be the first to zero ctx, making these calls not swappable in the future.

Also, algorithm parameters could be interpreted without knowing the key, but the key cannot be interpreted without knowing the parameters.

Quoted 6 lines
crypto/asymmetric_keys/public_key.c +3 -0
+	if (ret)
+		goto error_free_req;
+
 	ret = -ENOMEM;
 	outlen = crypto_akcipher_maxsize(tfm);
 	output = kmalloc(outlen, GFP_KERNEL);
#3 lore raw

Re: [RFC PATCH 1/4] X.509: Parse public key parameters from x509 for akcipher

Quoted 50 lines · Vitaly Chikunov
Quoted 35 lines · Vitaly Chikunov

Some public key algorithms (like ECDSA) keep in parameters field important data such as digest and curve OIDs (possibly more for different ECDSA variants). Thus, just setting a public key (as for RSA) is not enough.

Introduce set_params() callback for akcipher which will be used to pass DER encoded parameters array, with additional argument of algorithm OID.

This is done with the intent of adding support for EC-RDSA (ISO/IEC 14888-3:2018, RFC 7091, and basically ECDSA variant) public keys (which will be finally used in IMA subsystem). Thus, also oid_registry.h is updated.

Rationale:

- For such keys just setting public key without parameters is meaningless, so it would be possible to add parameters in crypto_akcipher_set_pub_key (and .set_pub_key) calls. But, this will needlessly change API for RSA akcipher. Also, additional callback making it possible to pass parameters after crypto_akcipher_set_priv_key (and .set_priv_key) in the future.

- Algorithm OID is passed to be validated in .set_params callback, otherwise, it could have the wrong value.

- Particular algorithm OIDs are checked in x509_note_params, (because this is called from AlgorithmIdentifier (ASN.1) parser, which is called multiple times, as it's used multiple times in X.509 certificate), to distinguish a public key call from a signature call.

---

Review int public_key_verify_signature(const struct public_key *pkey,

Quoted 10 lines
(quoted diff) +2 -0
@@ -263,6 +274,11 @@ int public_key_verify_signature(const struct public_key *pkey,
       if (pkey->key_is_private)
               ret = crypto_akcipher_set_priv_key(tfm,
                                                  pkey->key, pkey->keylen);
       else
               ret = crypto_akcipher_set_pub_key(tfm,
                                                 pkey->key, pkey->keylen);
       if (ret)
               goto error_free_req;
+	ret = crypto_akcipher_set_params(tfm, pkey->algo, pkey->params,
+					 pkey->paramlen);

Nobody said anything if this is a good idea to call set_params after set_{pub,priv}_key and not before.

Quoted 6 lines

When `struct crypto_akcipher' is allocated ctx data is never zeroed, thus either call will be the first to zero ctx, making these calls not swappable in the future.

Also, algorithm parameters could be interpreted without knowing the key, but the key cannot be interpreted without knowing the parameters.

From the other point of view, set_params may never be called or implemented. So, making it called first and move memory zeroing into set_params may create more complications than simplicity.

Making both callbacks callable in any order also will not make things simpler. (Need to be prepared to be called in different order.)

Maybe it's better to make memzero in akcipher_request_alloc() and allow optional call to crypto_akcipher_set_params() strictly before crypto_akcipher_set_{pub,priv}_key(), so, set_{pub,priv}_key will already know everything to set the key properly. set_params may not be implemented if akcipher does not need it (as with RSA).

Quoted 7 lines
Quoted 7 lines
crypto/asymmetric_keys/public_key.c +3 -0
+	if (ret)
+		goto error_free_req;
+
 	ret = -ENOMEM;
 	outlen = crypto_akcipher_maxsize(tfm);
 	output = kmalloc(outlen, GFP_KERNEL);
maintainer
#4 lore raw

Re: [RFC PATCH 1/4] X.509: Parse public key parameters from x509 for akcipher

Quoted 8 lines · Vitaly Chikunov
Quoted 2 lines

From the other point of view, set_params may never be called or

implemented. So, making it called first and move memory zeroing into set_params may create more complications than simplicity.

Making both callbacks callable in any order also will not make things simpler. (Need to be prepared to be called in different order.)

How about encoding these parameters together with the public/private keys so that they can be set through the existing setkey functions?

You might want to have a look at how we handle this in crypto/dh.c.

Cheers,

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
#5 lore raw

Re: [RFC PATCH 1/4] X.509: Parse public key parameters from x509 for akcipher

Herbert,

Quoted 6 lines · Herbert Xu
Quoted 5 lines · Vitaly Chikunov
Quoted 2 lines

From the other point of view, set_params may never be called or

implemented. So, making it called first and move memory zeroing into set_params may create more complications than simplicity.

Quoted 8 lines
Quoted 3 lines

Making both callbacks callable in any order also will not make things simpler. (Need to be prepared to be called in different order.)

How about encoding these parameters together with the public/private keys so that they can be set through the existing setkey functions?

You might want to have a look at how we handle this in crypto/dh.c.

Thanks. I viewed and thought about this idea. But, I think separate set_params call will be the most simple and backward compatible approach.

[In the new patchset] I made set_params to be called before set_p*_key call, thus set_p*_key will know everything to start processing the key immediately. Also, I made set_params completely optional, so code that rely on RSA can just not call it, and driver that actually needs set_params may check if required parameters are set or return an error.

(I overthought about zeroing of memory (in previous mail) that turned out completely non-problem as ctx in struct crypto_akcipher' is always zeroed and I don't need to use ctx from struct akcipher_request').

More thoughts. Parameters are part of AlgorithmIdentifier which is included in SubjectPublicKeyInfo together with subjectPublicKey.

This, to pass into set_params callback AlgorithmIdentifier is the same as passing just parameters. But, passing SubjectPublicKeyInfo will overlap with passing the key into set_pub_key. So, if we pass other structure (SubjectPublicKeyInfo) into set_params we will logically conflict with set_pub_key and that call will be alternative to set_pub_key, making one of them redundant.

If we pass SubjectPublicKeyInfo into set_pub_key itself (making set_params not needed) we will break ABI and compatibility with RSA drivers, because whole SubjectPublicKeyInfo is not expected by the drivers, or new set_pub_key need somehow signal to the driver that is different parameters are going (callers should be aware of that too), and/or we will need to change all RSA-centric code to use SubjectPublicKeyInfo which will affect to much code (more than verify2 required I think). And this will offload work which is already done by x509 parser into the drivers bloating their code needlessly.

Thus, I think to try to remove set_params will only increase complexity. Now it's small, very flexible, and back compatible.

Thanks,

maintainer
#6 lore raw

Re: [RFC PATCH 1/4] X.509: Parse public key parameters from x509 for akcipher

Quoted 4 lines · Vitaly Chikunov

If we pass SubjectPublicKeyInfo into set_pub_key itself (making set_params not needed) we will break ABI and compatibility with RSA drivers, because whole SubjectPublicKeyInfo is not expected by the

This compatibility does not matter. We can always add translating layers into the crypto API to deal with this. The only ABI that matters is the one to user-space.

Cheers,

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
#7 lore raw

Re: [RFC PATCH 1/4] X.509: Parse public key parameters from x509 for akcipher

Herbert,

Quoted 9 lines · Herbert Xu
Quoted 4 lines · Vitaly Chikunov

If we pass SubjectPublicKeyInfo into set_pub_key itself (making set_params not needed) we will break ABI and compatibility with RSA drivers, because whole SubjectPublicKeyInfo is not expected by the

This compatibility does not matter. We can always add translating layers into the crypto API to deal with this. The only ABI that matters is the one to user-space.

It seems that you insist on set_params to be removed and both key and params to be passed into set_{pub,priv}_key. This means reworking all existing RSA drivers and callers, right? Can you please confirm that huge rework to avoid misunderstanding?

I think to pass SubjectPublicKeyInfo into set_*_key would be overkill, because TPM drivers may not have it and we would need BER encoder just for that.

So, probably, something simple like length, key data, length, params data will be enough?

Thanks,

#8 lore raw

Re: [RFC PATCH 1/4] X.509: Parse public key parameters from x509 for akcipher

Quoted 18 lines · Vitaly Chikunov

Herbert,

Quoted 9 lines · Herbert Xu
Quoted 4 lines · Vitaly Chikunov

If we pass SubjectPublicKeyInfo into set_pub_key itself (making set_params not needed) we will break ABI and compatibility with RSA drivers, because whole SubjectPublicKeyInfo is not expected by the

This compatibility does not matter. We can always add translating layers into the crypto API to deal with this. The only ABI that matters is the one to user-space.

It seems that you insist on set_params to be removed and both key and params to be passed into set_{pub,priv}_key. This means reworking all existing RSA drivers and callers, right? Can you please confirm that huge rework to avoid misunderstanding?

Quoted 6 lines

I think to pass SubjectPublicKeyInfo into set_*_key would be overkill, because TPM drivers may not have it and we would need BER encoder just for that.

So, probably, something simple like length, key data, length, params data will be enough?

Or maybe we could just add additional argument to set_{pub,priv}_key? (If you agree to change that ABI anyway).

Quoted 1 line

Thanks,

maintainer
#9 lore raw

Re: [RFC PATCH 1/4] X.509: Parse public key parameters from x509 for akcipher

In reply to Vitaly Chikunov
Quoted 5 lines · Vitaly Chikunov

It seems that you insist on set_params to be removed and both key and params to be passed into set_{pub,priv}_key. This means reworking all existing RSA drivers and callers, right? Can you please confirm that huge rework to avoid misunderstanding?

I don't understand why we even need to touch the existing RSA drivers. Nothing needs to change as far as they're concerned.

Only the new algorithms would need to decode the extra parameters in the key stream.

Cheers,

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
#10 lore raw

Re: [RFC PATCH 1/4] X.509: Parse public key parameters from x509 for akcipher

Quoted 7 lines · Herbert Xu
Quoted 5 lines · Vitaly Chikunov

It seems that you insist on set_params to be removed and both key and params to be passed into set_{pub,priv}_key. This means reworking all existing RSA drivers and callers, right? Can you please confirm that huge rework to avoid misunderstanding?

Quoted 5 lines

I don't understand why we even need to touch the existing RSA drivers. Nothing needs to change as far as they're concerned.

Only the new algorithms would need to decode the extra parameters in the key stream.

int (*set_pub_key)(struct crypto_akcipher *tfm, const void *key,
unsigned int keylen);
int (*set_priv_key)(struct crypto_akcipher *tfm, const void *key,
unsigned int keylen);

So you want keylen' not to cover parameters data, but parameters actually present in key after keylen' bytes (in come packed format)? (And if there is no parameters appended, there is still appended some marker, like 0, to signify that there is no parameters.)

This looks a bit counter-intuitive usage of arguments (as argument signifying length does not cover all arguments data), is this ok to you?

More intuitive would be to add at least paramlen argument to signify how much data is appended. Or (if we add argument anyway) additional const void *params, unsigned int paramlen - which callers who don't have params will pass NULL, and RSA drivers just ignore.

maintainer
#11 lore raw

Re: [RFC PATCH 1/4] X.509: Parse public key parameters from x509 for akcipher

Quoted 19 lines · Vitaly Chikunov
Quoted 7 lines · Herbert Xu
Quoted 5 lines · Vitaly Chikunov

It seems that you insist on set_params to be removed and both key and params to be passed into set_{pub,priv}_key. This means reworking all existing RSA drivers and callers, right? Can you please confirm that huge rework to avoid misunderstanding?

Quoted 5 lines

I don't understand why we even need to touch the existing RSA drivers. Nothing needs to change as far as they're concerned.

Only the new algorithms would need to decode the extra parameters in the key stream.

int (*set_pub_key)(struct crypto_akcipher *tfm, const void *key,
unsigned int keylen);
int (*set_priv_key)(struct crypto_akcipher *tfm, const void *key,
unsigned int keylen);

Quoted 7 lines

So you want keylen' not to cover parameters data, but parameters actually present in key after keylen' bytes (in come packed format)? (And if there is no parameters appended, there is still appended some marker, like 0, to signify that there is no parameters.)

This looks a bit counter-intuitive usage of arguments (as argument signifying length does not cover all arguments data), is this ok to you?

This is how we handle things in DH as well as other places such as authenc.

Cheers,

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
#12 lore raw

Re: [RFC PATCH 1/4] X.509: Parse public key parameters from x509 for akcipher

Quoted 25 lines · Herbert Xu
Quoted 24 lines · Vitaly Chikunov
Quoted 7 lines · Herbert Xu
Quoted 5 lines · Vitaly Chikunov

It seems that you insist on set_params to be removed and both key and params to be passed into set_{pub,priv}_key. This means reworking all existing RSA drivers and callers, right? Can you please confirm that huge rework to avoid misunderstanding?

Quoted 5 lines

I don't understand why we even need to touch the existing RSA drivers. Nothing needs to change as far as they're concerned.

Only the new algorithms would need to decode the extra parameters in the key stream.

int (*set_pub_key)(struct crypto_akcipher *tfm, const void *key,
unsigned int keylen);
int (*set_priv_key)(struct crypto_akcipher *tfm, const void *key,
unsigned int keylen);

So you want keylen' not to cover parameters data, but parameters actually present in key after keylen' bytes (in come packed format)? (And if there is no parameters appended, there is still appended some marker, like 0, to signify that there is no parameters.)

Quoted 5 lines
Quoted 2 lines

This looks a bit counter-intuitive usage of arguments (as argument signifying length does not cover all arguments data), is this ok to you?

This is how we handle things in DH as well as other places such as authenc.

dh.c (set_secret) is getting buf, len arguments and then parameters are unpacked from that buffer. I think you want me to do this.

To make the same for set_{pub,priv}_key it will require patching RSA drivers anyway, since length of the key is stored just once as keylen argument.

Drivers will be changed to interpret key, keylen as buf, len and actual keylen will be stored into the buf, and will be unpacked from there.

Thanks,

Quoted 6 lines

Cheers,

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
maintainer
#13 lore raw

Re: [RFC PATCH 1/4] X.509: Parse public key parameters from x509 for akcipher

Quoted 4 lines · Vitaly Chikunov

To make the same for set_{pub,priv}_key it will require patching RSA drivers anyway, since length of the key is stored just once as keylen argument.

No we don't need to use the same format for different algorithms. RSA should stay as is.

Cheers,

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
#14 lore raw

Re: [RFC PATCH 1/4] X.509: Parse public key parameters from x509 for akcipher

Herbert,

Quoted 8 lines · Herbert Xu
Quoted 4 lines · Vitaly Chikunov

To make the same for set_{pub,priv}_key it will require patching RSA drivers anyway, since length of the key is stored just once as keylen argument.

No we don't need to use the same format for different algorithms. RSA should stay as is.

I will rework as you suggest. But, just want to state that I disagree with this approach of implicitly appending parameters data to the key stream without any argument signifying it or length covering it. This fitting into the old API is also somewhat disagree to your words that we could change internal API:

Quoted 5 lines · Herbert Xu

On Sun, Feb 24, 2019 at 09:48:40AM +0300, Vitaly Chikunov wrote:
...
This compatibility does not matter. We can always add translating layers into the crypto API to deal with this. The only ABI that matters is the one to user-space.

#15 lore raw

RFC PATCH 2/4 akcipher: Introduce verify2 for public key algorithms

In reply to Vitaly Chikunov

Current akcipher .verify() just decrypts signature to uncover message hash, which is then verified in upper level public_key_verify_signature by memcmp with the expected signature value, which is never passed into verify().

This approach is incompatible with ECDSA algorithms, because, to verify a signature ECDSA algorithm also needs a hash value as input; also, hash is used in ECDSA (together with a signature divided into halves r||s), not to produce hash, but to produce a number, which is then compared to r (first part of the signature) to determine if the signature is correct. Thus, for ECDSA, nor requirements of .verify() itself, nor its output expectations in public_key_verify_signature aren't satisfied.

Make alternative .verify2() call which gets hash value and produce complete signature check (without any output, thus max_size() call will not be needed for verify2() operation).

If .verify2() call is present, it should be used in place of .verify().

---
 crypto/asymmetric_keys/public_key.c | 57 ++++++++++++++++++++++++-------------
 include/crypto/akcipher.h           | 54 +++++++++++++++++++++++++++++++++--
 2 files changed, 89 insertions(+), 22 deletions(-)
crypto/asymmetric_keys/public_key.c +38 -19
index 3bc090b8adef..51dc1c858c7c 100644
@@ -242,6 +242,7 @@ int public_key_verify_signature(const struct public_key *pkey,
 	char alg_name[CRYPTO_MAX_ALG_NAME];
 	void *output;
 	unsigned int outlen;
+	int verify2;
 	int ret;
 
 	pr_devel("=>%s()\n", __func__);
@@ -279,14 +280,23 @@ int public_key_verify_signature(const struct public_key *pkey,
 	if (ret)
 		goto error_free_req;
 
-	ret = -ENOMEM;
-	outlen = crypto_akcipher_maxsize(tfm);
-	output = kmalloc(outlen, GFP_KERNEL);
-	if (!output)
-		goto error_free_req;
-
+	verify2 = crypto_akcipher_have_verify2(req);
+	if (!verify2) {
+		/* verify2 does not need output buffer */
+		ret = -ENOMEM;
+		outlen = crypto_akcipher_maxsize(tfm);
+		output = kmalloc(outlen, GFP_KERNEL);
+		if (!output)
+			goto error_free_req;
+
+		sg_init_one(&digest_sg, output, outlen);
+	} else {
+		/* dummy init digest_sg */
+		memset(&digest_sg, 0, sizeof(digest_sg));
+		output = NULL;
+		outlen = 0;
+	}
 	sg_init_one(&sig_sg, sig->s, sig->s_size);
-	sg_init_one(&digest_sg, output, outlen);
 	akcipher_request_set_crypt(req, &sig_sg, &digest_sg, sig->s_size,
 				   outlen);
 	crypto_init_wait(&cwait);
@@ -294,18 +304,27 @@ int public_key_verify_signature(const struct public_key *pkey,
 				      CRYPTO_TFM_REQ_MAY_SLEEP,
 				      crypto_req_done, &cwait);
 
-	/* Perform the verification calculation.  This doesn't actually do the
-	 * verification, but rather calculates the hash expected by the
-	 * signature and returns that to us.
-	 */
-	ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
-	if (ret)
-		goto out_free_output;
-
-	/* Do the actual verification step. */
-	if (req->dst_len != sig->digest_size ||
-	    memcmp(sig->digest, output, sig->digest_size) != 0)
-		ret = -EKEYREJECTED;
+	if (!verify2) {
+		/* Perform the verification calculation.  This doesn't actually
+		 * do the verification, but rather calculates the hash expected
+		 * by the signature and returns that to us.
+		 */
+		ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
+		if (ret)
+			goto out_free_output;
+
+		/* Do the actual verification step. */
+		if (req->dst_len != sig->digest_size ||
+		    memcmp(sig->digest, output, sig->digest_size) != 0)
+			ret = -EKEYREJECTED;
+	} else {
+		/* Perform full verification in one call. */
+		req->digest = sig->digest;
+		req->digest_len = sig->digest_size;
+		ret = crypto_wait_req(crypto_akcipher_verify2(req), &cwait);
+		if (ret)
+			goto out_free_output;
+	}
 
 out_free_output:
 	kfree(output);
include/crypto/akcipher.h +51 -3

92 lines not on this page · raw message

Signature · 1 line
2.11.0
maintainer
#16 lore raw

Re: [RFC PATCH 2/4] akcipher: Introduce verify2 for public key algorithms

Quoted 4 lines · Vitaly Chikunov

Current akcipher .verify() just decrypts signature to uncover message hash, which is then verified in upper level public_key_verify_signature by memcmp with the expected signature value, which is never passed into verify().

I think it would be better to make ->verify() take the data hash we've been given rather than returning the expected hash for the caller to compare. That way the callers don't have to do two different things, depending on how the crypto algo works.

David

#17 lore raw

RFC PATCH 3/4 KEYS: set correct flags for keyctl if encrypt is not supported

In reply to Vitaly Chikunov
---
 crypto/asymmetric_keys/public_key.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
crypto/asymmetric_keys/public_key.c +3 -2
index 51dc1c858c7c..382cf67f510e 100644
@@ -136,8 +136,9 @@ static int software_key_query(const struct kernel_pkey_params *params,
 	info->max_sig_size = len;
 	info->max_enc_size = len;
 	info->max_dec_size = len;
-	info->supported_ops = (KEYCTL_SUPPORTS_ENCRYPT |
-			       KEYCTL_SUPPORTS_VERIFY);
+	info->supported_ops = KEYCTL_SUPPORTS_VERIFY;
+	if (crypto_akcipher_alg(tfm)->encrypt)
+		info->supported_ops |= KEYCTL_SUPPORTS_ENCRYPT;
 	if (pkey->key_is_private)
 		info->supported_ops |= (KEYCTL_SUPPORTS_DECRYPT |
 					KEYCTL_SUPPORTS_SIGN);
Signature · 1 line
2.11.0
#18 lore raw

RFC PATCH 4/4 crypto: Add EC-RDSA algorithm

In reply to Vitaly Chikunov

Add Elliptic Curve Russian Digital Signature Algorithm (GOST R 34.10-2012, RFC 7091, ISO/IEC 14888-3) is one of the Russian (and since 2018 the CIS countries) cryptographic standard algorithms (called GOST algorithms). Only signature verification is supported, with intent to be used in the IMA.

Summary of the changes:

crypto/Kconfig:
- Public-key methods are moved to the new "Public-key cryptography" section;
- Where EC-RDSA is added.

crypto/Makefile:
- ecc.o is split into a separate module to use both by ecdh and ecrdsa.

crypto/asymmetric_keys/x509_cert_parser.c:
- Recognize EC-RDSA and Streebog OIDs.

include/linux/oid_registry.h:
- EC-RDSA OIDs are added to the enum. Also, a few currently not used curve OIDs are added for possible extension later (to not change numbering and grouping).

crypto/ecc.c:
- Kenneth MacKay copyright date is updated to 2014, because vli_mmod_slow, ecc_point_add, ecc_point_mult_shamir are based on his code from micro-ecc.
- Functions needed for ecrdsa are EXPORT_SYMBOL'ed.
- New functions:
vli_is_negative - helper to determine sign of vli; vli_from_be64 - unpack big-endian array into vli (used for

    a signature);
  vli_from_le64 - unpack little-endian array into vli (used for
    a public key);
  vli_uadd, vli_usub - add/sub u64 value to/from vli (used for
    increment/decrement);
  mul_64_64 - optimized to use __int128 where appropriate, this speeds
    up point multiplication (and as a consequence signature
    verification) by the factor of 1.5-2;
  vli_umult - multiply vli by a small value (speeds up point
    multiplication by another factor of 1.5-2, depending on vli sizes);
  vli_mmod_special - module reduction for some form of Pseudo-Mersenne
    primes (used for the curves A);
  vli_mmod_special2 - module reduction for another form of
    Pseudo-Mersenne primes (used for the curves B);
  vli_mmod_barrett - module reduction using pre-computed value (used
    for the curve C);
  vli_mmod_slow - more general module reduction which is much slower
   (used when the modulus is subgroup order);
  vli_mmod_fast - integrated non-strict heuristic to call optimal
    module reduction function depending on the prime value;

vli_mod_mult_slow - modular multiplication;
ecc_point_add - add two points;
ecc_point_mult_shamir - add two points multiplied by scalars in one
combined multiplication (this gives speed up by another factor 2 in compare to two separate multiplications).

crypto/ecc.h:
- struct ecc_point and struct ecc_curve definitions are moved from ecc.c and slightly extended.
- All exported functions are documented.

crypto/ecrdsa.c:
- All five curves are defined locally since they are limited to use by EC-RDSA. (Possible by ECDH too, though.)
- Only signature verification is implemented.

---
 crypto/Kconfig                            |  63 ++--
 crypto/Makefile                           |   5 +-
 crypto/asymmetric_keys/x509_cert_parser.c |  13 +
 crypto/ecc.c                              | 421 +++++++++++++++++++++++--
 crypto/ecc.h                              | 162 +++++++++-
 crypto/ecc_curve_defs.h                   |  15 -
 crypto/ecrdsa.c                           | 494 ++++++++++++++++++++++++++++++
 include/linux/oid_registry.h              |  12 +
 8 files changed, 1126 insertions(+), 59 deletions(-)
 create mode 100644 crypto/ecrdsa.c
crypto/Kconfig +40 -23
index b6376d5d973e..75783af857ce 100644
@@ -113,29 +113,6 @@ config CRYPTO_ACOMP
 	select CRYPTO_ALGAPI
 	select CRYPTO_ACOMP2
 
-config CRYPTO_RSA
-	tristate "RSA algorithm"
-	select CRYPTO_AKCIPHER
-	select CRYPTO_MANAGER
-	select MPILIB
-	select ASN1
-	help
-	  Generic implementation of the RSA public key algorithm.
-
-config CRYPTO_DH
-	tristate "Diffie-Hellman algorithm"
-	select CRYPTO_KPP
-	select MPILIB
-	help
-	  Generic implementation of the Diffie-Hellman algorithm.
-
-config CRYPTO_ECDH
-	tristate "ECDH algorithm"
-	select CRYPTO_KPP
-	select CRYPTO_RNG_DEFAULT
-	help
-	  Generic implementation of the ECDH algorithm
-
 config CRYPTO_MANAGER
 	tristate "Cryptographic algorithm manager"
 	select CRYPTO_MANAGER2
@@ -243,6 +220,46 @@ config CRYPTO_GLUE_HELPER_X86
 config CRYPTO_ENGINE
 	tristate
 
+comment "Public-key cryptography"
+
+config CRYPTO_RSA
+	tristate "RSA algorithm"
+	select CRYPTO_AKCIPHER
+	select CRYPTO_MANAGER
+	select MPILIB
+	select ASN1
+	help
+	  Generic implementation of the RSA public key algorithm.
+
+config CRYPTO_DH
+	tristate "Diffie-Hellman algorithm"
+	select CRYPTO_KPP
+	select MPILIB
+	help
+	  Generic implementation of the Diffie-Hellman algorithm.
+
+config CRYPTO_ECC
+	tristate
+
+config CRYPTO_ECDH
+	tristate "ECDH algorithm"
+	select CRYPTO_ECC
+	select CRYPTO_KPP
+	select CRYPTO_RNG_DEFAULT
+	help
+	  Generic implementation of the ECDH algorithm
+
+config CRYPTO_ECRDSA
+	tristate "EC-RDSA (GOST 34.10) algorithm"
+	select CRYPTO_ECC
+	select CRYPTO_AKCIPHER
+	select CRYPTO_STREEBOG
+	help
+	  Elliptic Curve Russian Digital Signature Algorithm (GOST R 34.10-2012,
+	  RFC 7091, ISO/IEC 14888-3:2018) is one of the Russian cryptographic
+	  standard algorithms (called GOST algorithms). Only signature verification
+	  is supported.
+
 comment "Authenticated Encryption with Associated Data"
 
 config CRYPTO_CCM
crypto/Makefile +4 -1
index 5e789dc2d4fd..5f0e24f75b02 100644
@@ -146,12 +146,15 @@ 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
 
-ecdh_generic-y := ecc.o
 ecdh_generic-y += ecdh.o
 ecdh_generic-y += ecdh_helper.o
 obj-$(CONFIG_CRYPTO_ECDH) += ecdh_generic.o
 
+ecrdsa_generic-y += ecrdsa.o
+obj-$(CONFIG_CRYPTO_ECRDSA) += ecrdsa_generic.o
+
 #
 # generic algorithms and the async_tx api
 #
crypto/asymmetric_keys/x509_cert_parser.c +13 -0
index 202a9dc66c93..a76ea216cd46 100644
@@ -230,6 +230,14 @@ int x509_note_pkey_algo(void *context, size_t hdrlen,
 	case OID_sha224WithRSAEncryption:
 		ctx->cert->sig->hash_algo = "sha224";
 		goto rsa_pkcs1;
+
+	case OID_gost2012Signature256:
+		ctx->cert->sig->hash_algo = "streebog256";
+		goto ecrdsa;
+
+	case OID_gost2012Signature512:
+		ctx->cert->sig->hash_algo = "streebog512";
+		goto ecrdsa;
 	}
 
 rsa_pkcs1:
@@ -237,6 +245,11 @@ int x509_note_pkey_algo(void *context, size_t hdrlen,
 	ctx->cert->sig->encoding = "pkcs1";
 	ctx->algo_oid = ctx->last_oid;
 	return 0;
+ecrdsa:
+	ctx->cert->sig->pkey_algo = "ecrdsa";
+	ctx->cert->sig->encoding = "raw";
+	ctx->algo_oid = ctx->last_oid;
+	return 0;
 }
 
 /*
crypto/ecc.c +402 -19

608 lines not on this page · raw message

crypto/ecc.h +161 -1

176 lines not on this page · raw message

crypto/ecc_curve_defs.h +0 -15
index 336ab1805639..69be6c7d228f 100644
@@ -2,21 +2,6 @@
 #ifndef _CRYTO_ECC_CURVE_DEFS_H
 #define _CRYTO_ECC_CURVE_DEFS_H
 
-struct ecc_point {
-	u64 *x;
-	u64 *y;
-	u8 ndigits;
-};
-
-struct ecc_curve {
-	char *name;
-	struct ecc_point g;
-	u64 *p;
-	u64 *n;
-	u64 *a;
-	u64 *b;
-};
-
 /* NIST P-192: a = p - 3 */
 static u64 nist_p192_g_x[] = { 0xF4FF0AFD82FF1012ull, 0x7CBF20EB43A18800ull,
 				0x188DA80EB03090F6ull };
crypto/ecrdsa.c new file +494 -0

494 lines not on this page · raw message

include/linux/oid_registry.h +12 -0
index e2e323fd4826..1cc4d58d3bff 100644
@@ -94,10 +94,22 @@ enum OID {
 	OID_extKeyUsage,		/* 2.5.29.37 */
 
 	/* EC-RDSA */
+	OID_gostCPSignA,		/* 1.2.643.2.2.35.1 */
+	OID_gostCPSignB,		/* 1.2.643.2.2.35.2 */
+	OID_gostCPSignC,		/* 1.2.643.2.2.35.3 */
 	OID_gost2012PublicKey256,	/* 1.2.643.7.1.1.1.1 */
 	OID_gost2012PublicKey512,	/* 1.2.643.7.1.1.1.2 */
+	OID_gost2012Digest256,		/* 1.2.643.7.1.1.2.2 */
+	OID_gost2012Digest512,		/* 1.2.643.7.1.1.2.3 */
 	OID_gost2012Signature256,	/* 1.2.643.7.1.1.3.2 */
 	OID_gost2012Signature512,	/* 1.2.643.7.1.1.3.3 */
+	OID_gostTC26Sign256A,		/* 1.2.643.7.1.2.1.1.1 */
+	OID_gostTC26Sign256B,		/* 1.2.643.7.1.2.1.1.2 */
+	OID_gostTC26Sign256C,		/* 1.2.643.7.1.2.1.1.3 */
+	OID_gostTC26Sign256D,		/* 1.2.643.7.1.2.1.1.4 */
+	OID_gostTC26Sign512A,		/* 1.2.643.7.1.2.1.2.1 */
+	OID_gostTC26Sign512B,		/* 1.2.643.7.1.2.1.2.2 */
+	OID_gostTC26Sign512C,		/* 1.2.643.7.1.2.1.2.3 */
 
 	OID__NR
 };
Signature · 1 line
2.11.0
#19 lore raw

Am Sonntag, 6. Januar 2019, 14:36:08 CET schrieb Vitaly Chikunov:

Hi Vitaly,

Quoted 5 lines

Add Elliptic Curve Russian Digital Signature Algorithm (GOST R 34.10-2012, RFC 7091, ISO/IEC 14888-3) is one of the Russian (and since 2018 the CIS countries) cryptographic standard algorithms (called GOST algorithms). Only signature verification is supported, with intent to be used in the IMA.

Do you happen to have test vectors for the testmgr?

Quoted 12 lines
crypto/ecrdsa.c new file +12 -0
+
+/* Parse DER encoded subjectPublicKey. */
+static int ecrdsa_set_pub_key(struct crypto_akcipher *tfm, const void *ber,
+			      unsigned int len)
+{
+	struct ecrdsa_ctx *ctx = akcipher_tfm_ctx(tfm);
+	unsigned int ndigits;
+	const u8 *k = ber;
+	unsigned int offset;
+
+	/* First chance to zero ctx */
+	memset(ctx, 0, sizeof(*ctx));

crypto/ecrdsa.c line 368

Quoted 10 lines
crypto/ecrdsa.c new file +10 -0
+
+	if (len < 3 ||
+	    k[0] != 0x04 || /* OCTET STRING */
+	    (k[1] < 0x80 && len != k[1] + 2) ||
+	    (k[1] = 0x81 && len != k[2] + 3) ||
+	    k[1] > 0x81)
+		return -EBADMSG;
+	offset = (k[1] < 0x80)? 2 : 3;
+	k += offset;
+	len -= offset;

Why do you manually parse the ASN.1 structure instead of using the ASN.1 parser?

Quoted 30 lines
crypto/ecrdsa.c new file +13 -0
+	/* Key is two 256- or 512-bit coordinates. */
+	if (len != (2 * 256 / 8) &&
+	    len != (2 * 512 / 8))
+		return -ENOPKG;
+	ndigits = len / sizeof(u64) / 2;
+	ctx->pub_key = ECC_POINT_INIT(ctx->_pubp[0], ctx->_pubp[1], ndigits);
+	vli_from_le64(ctx->pub_key.x, k, ndigits);
+	vli_from_le64(ctx->pub_key.y, k + ndigits * sizeof(u64), ndigits);
+
+	return 0;
+}
+
+/* Parse DER encoded SubjectPublicKeyInfo.AlgorithmIdentifier.parameters.

*/ +static int ecrdsa_set_params(struct crypto_akcipher *tfm, enum OID
algo, + const void *params, unsigned int paramlen)

crypto/ecrdsa.c new file +15 -0
+{
+	struct ecrdsa_ctx *ctx = akcipher_tfm_ctx(tfm);
+	const u8 *p = params;
+	int i;
+
+	if (algo = OID_gost2012PublicKey256) {
+		ctx->digest	= "streebog256";
+		ctx->digest_oid	= OID_gost2012Digest256;
+		ctx->digest_len	= 256 / 8;
+	} else if (algo = OID_gost2012PublicKey512) {
+		ctx->digest	= "streebog512";
+		ctx->digest_oid	= OID_gost2012Digest512;
+		ctx->digest_len	= 512 / 8;
+	} else
+		return -ENOPKG;

crypto/ecrdsa.c line 408

Quoted 10 lines
crypto/ecrdsa.c new file +10 -0
+	ctx->curve = NULL;
+	ctx->curve_oid = 0;
+	ctx->algo_oid = algo;
+
+	for (i = 0; i < paramlen; i += p[i + 1] + 2) {
+		const struct ecc_curve *curve;
+		enum OID oid;
+
+		if (paramlen - i < 2 ||
+		    p[i] != 0x06 || /* OBJECT IDENTIFIER */

Same here and in the following

Quoted 23 lines
crypto/ecrdsa.c new file +23 -0
+		    p[i + 1] > paramlen - i - 2)
+			return -EBADMSG;
+		oid = look_up_OID(p + i + 2, p[i + 1]);
+		if (oid = OID__NR)
+			return -ENOPKG;
+
+		if (oid = OID_gost2012Digest256 ||
+		    oid = OID_gost2012Digest512) {
+			if (oid != ctx->digest_oid)
+				return -ENOPKG;
+		} else {
+			curve = get_curve_by_oid(oid);
+			if (!curve || ctx->curve)
+				return -ENOPKG;
+			ctx->curve = curve;
+			ctx->curve_oid = oid;
+		}
+	}
+	/* Sizes of algo, curve, pub_key, and digest should match each other. */
+	if (!ctx->curve ||
+	    ctx->curve->g.ndigits * sizeof(u64) != ctx->digest_len ||
+	    ctx->curve->g.ndigits != ctx->pub_key.ndigits)
+		return -ENOPKG;

crypto/ecrdsa.c line 494

Quoted 10 lines
crypto/ecrdsa.c new file +9 -0
+
+	/* First chance to validate the public key. */
+	if (ecc_is_pubkey_valid_partial(ctx->curve, &ctx->pub_key))
+		return -EKEYREJECTED;
+
+	return 0;
+}
+
 
+MODULE_ALIAS_CRYPTO("ecrdsa");

I do not think you need that alias as the module name already will be named this way. I guess you rather should add ecrdsa-generic as module alias.

Ciao
Stephan

#20 lore raw

Stephan,

Quoted 9 lines · Stephan Müller

Am Sonntag, 6. Januar 2019, 14:36:08 CET schrieb Vitaly Chikunov:

Quoted 5 lines

Add Elliptic Curve Russian Digital Signature Algorithm (GOST R 34.10-2012, RFC 7091, ISO/IEC 14888-3) is one of the Russian (and since 2018 the CIS countries) cryptographic standard algorithms (called GOST algorithms). Only signature verification is supported, with intent to be used in the IMA.

Do you happen to have test vectors for the testmgr?

Yes, I will add this.

Quoted 16 lines
Quoted 16 lines
crypto/ecrdsa.c new file +16 -0
+/* Parse DER encoded subjectPublicKey. */
+static int ecrdsa_set_pub_key(struct crypto_akcipher *tfm, const void *ber,
+			      unsigned int len)
+{
+	struct ecrdsa_ctx *ctx = akcipher_tfm_ctx(tfm);
+	unsigned int ndigits;
+	const u8 *k = ber;
+	unsigned int offset;
+
+	/* First chance to zero ctx */
+	memset(ctx, 0, sizeof(*ctx));
+
+	if (len < 3 ||
+	    k[0] != 0x04 || /* OCTET STRING */
+	    (k[1] < 0x80 && len != k[1] + 2) ||
+	    (k[1] = 0x81 && len != k[2] + 3) ||
Quoted 8 lines

crypto/ecrdsa.c line 368

Quoted 5 lines
crypto/ecrdsa.c new file +5 -0
+	    k[1] > 0x81)
+		return -EBADMSG;
+	offset = (k[1] < 0x80)? 2 : 3;
+	k += offset;
+	len -= offset;

Why do you manually parse the ASN.1 structure instead of using the ASN.1 parser?

I am not sure this worth effort and will not be most degenerate use of asn1_ber_decoder, since 1) I only need to parse one type in each case:
OCTET STRING string above code, and OIDs in below code; 2) this data is said to be in DER format, which asn1_ber_decoder can not enforce. Surely this will also produce more code and files.

Quoted 31 lines
Quoted 18 lines

+/* Parse DER encoded SubjectPublicKeyInfo.AlgorithmIdentifier.parameters. */ +static int ecrdsa_set_params(struct crypto_akcipher *tfm, enum OID
algo, + const void *params, unsigned int paramlen)

crypto/ecrdsa.c new file +15 -0
+{
+	struct ecrdsa_ctx *ctx = akcipher_tfm_ctx(tfm);
+	const u8 *p = params;
+	int i;
+
+	if (algo = OID_gost2012PublicKey256) {
+		ctx->digest	= "streebog256";
+		ctx->digest_oid	= OID_gost2012Digest256;
+		ctx->digest_len	= 256 / 8;
+	} else if (algo = OID_gost2012PublicKey512) {
+		ctx->digest	= "streebog512";
+		ctx->digest_oid	= OID_gost2012Digest512;
+		ctx->digest_len	= 512 / 8;
+	} else
+		return -ENOPKG;

crypto/ecrdsa.c line 408

Quoted 10 lines
crypto/ecrdsa.c new file +10 -0
+	ctx->curve = NULL;
+	ctx->curve_oid = 0;
+	ctx->algo_oid = algo;
+
+	for (i = 0; i < paramlen; i += p[i + 1] + 2) {
+		const struct ecc_curve *curve;
+		enum OID oid;
+
+		if (paramlen - i < 2 ||
+		    p[i] != 0x06 || /* OBJECT IDENTIFIER */

Same here and in the following

Quoted 8 lines

crypto/ecrdsa.c

Quoted 5 lines
crypto/ecrdsa.c new file +4 -0
+		    p[i + 1] > paramlen - i - 2)
+			return -EBADMSG;
+		oid = look_up_OID(p + i + 2, p[i + 1]);
 
+MODULE_ALIAS_CRYPTO("ecrdsa");

I do not think you need that alias as the module name already will be named this way. I guess you rather should add ecrdsa-generic as module alias.

Thanks!

#21 lore raw

Am Montag, 7. Januar 2019, 09:07:10 CET schrieb Vitaly Chikunov:

Hi Vitaly,

Quoted 8 lines
Quoted 2 lines

Why do you manually parse the ASN.1 structure instead of using the ASN.1 parser?

I am not sure this worth effort and will not be most degenerate use of asn1_ber_decoder, since 1) I only need to parse one type in each case:
OCTET STRING string above code, and OIDs in below code; 2) this data is said to be in DER format, which asn1_ber_decoder can not enforce. Surely this will also produce more code and files.

RSA public keys also only contain n and e in the ASN.1 structure for which the ASN.1 parser is used (see linux/crypto/rsapubkey.asn1). As ASN.1 parsing is always having security issues, I would rather suggest to have this parsing implemented in one spot and not here and there.

Regarding your comment (2), I am not sure I understand. Why do you say that the DER format cannot be parsed by the kernel's ASN.1 parser? For example, when you generate RSA keys in DER format with, say, openssl, the kernel ASN.1 parser will happily use them. Also, when I created my (not accepted) patch to load PQG domain parameters for DH using the ASN.1 parser, the PQG domain parameters created by openssl in DER format were processed well.

Ciao
Stephan

#22 lore raw

Stephan,

Quoted 5 lines · Stephan Mueller

Am Montag, 7. Januar 2019, 09:07:10 CET schrieb Vitaly Chikunov:

Quoted 3 lines
Quoted 2 lines

Why do you manually parse the ASN.1 structure instead of using the ASN.1 parser?

Quoted 8 lines
Quoted 5 lines

I am not sure this worth effort and will not be most degenerate use of asn1_ber_decoder, since 1) I only need to parse one type in each case:
OCTET STRING string above code, and OIDs in below code; 2) this data is said to be in DER format, which asn1_ber_decoder can not enforce. Surely this will also produce more code and files.

RSA public keys also only contain n and e in the ASN.1 structure for which the ASN.1 parser is used (see linux/crypto/rsapubkey.asn1).

Yes I saw it, but at least there is two values (n and e), while EC-RDSA pub_key is just one OCTET STRING value (which internally is not defined to be ASN.1).

Quoted 2 lines

As ASN.1 parsing is always having security issues, I would rather suggest to have this parsing implemented in one spot and not here and there.

Yes. So I tried to parse it very carefully and strictly.

Quoted 2 lines

Regarding your comment (2), I am not sure I understand. Why do you say that the DER format cannot be parsed by the kernel's ASN.1 parser? For example,

It can, but DER is stricter than BER. For example, in DER 'OCTET STRING' length field should be encoded in just one byte if it's smaller than 128, in BER it could be encoded in multiple encodings. This does not seem like a big deal, though. With BER decoder an improperly DER-encoded certificate could be successfully parsed.

Quoted 8 lines

when you generate RSA keys in DER format with, say, openssl, the kernel ASN.1 parser will happily use them. Also, when I created my (not accepted) patch to load PQG domain parameters for DH using the ASN.1 parser, the PQG domain parameters created by openssl in DER format were processed well.

Ciao
Stephan

maintainer
#23 lore raw
Quoted 8 lines · Vitaly Chikunov
Quoted 2 lines

Regarding your comment (2), I am not sure I understand. Why do you say that the DER format cannot be parsed by the kernel's ASN.1 parser? For example,

It can, but DER is stricter than BER. For example, in DER 'OCTET STRING' length field should be encoded in just one byte if it's smaller than 128, in BER it could be encoded in multiple encodings. This does not seem like a big deal, though. With BER decoder an improperly DER-encoded certificate could be successfully parsed.

Whilst that is true, I don't think it's that big a deal. DER is a subset of BER, so a BER decoder ought to be able to parse it. Note that X.509 is supposed to be DER, but we use a BER decoder for that too.

David