[pkg-opensc-commit] [libp11] 07/33: Truncate the EC signed message digest if needed

Eric Dorland eric at moszumanska.debian.org
Mon Sep 19 02:11:02 UTC 2016


This is an automated email from the git hooks/post-receive script.

eric pushed a commit to branch master
in repository libp11.

commit 1dbfda7add9ba451e6b07c744d048fe65046be28
Author: Michał Trojnara <Michal.Trojnara at stunnel.org>
Date:   Thu Apr 21 23:28:13 2016 +0200

    Truncate the EC signed message digest if needed
    
    The signed message digest is truncated if it is too long for the
    signing curve.  Closes #78.
---
 NEWS         |  2 ++
 src/p11_ec.c | 14 +++++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index f3db415..20af81f 100644
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,8 @@
 NEWS for Libp11 -- History of user visible changes
 
 New in 0.4.1; unreleased
+* The signed message digest is truncated if it is too long for the
+  signing curve (David von Oheimb)
 * Workaround for broken PKCS#11 modules not returning CKA_EC_POINT
   in the ASN1_OCTET_STRING format (Michał Trojnara)
 * Improved building against OpenSSL 1.1.0-dev (Michał Trojnara)
diff --git a/src/p11_ec.c b/src/p11_ec.c
index f93fbfc..1852920 100644
--- a/src/p11_ec.c
+++ b/src/p11_ec.c
@@ -242,7 +242,7 @@ static ECDSA_SIG *pkcs11_ecdsa_sign_sig(const unsigned char *dgst, int dlen,
 	ECDSA_SIG *sig;
 	PKCS11_KEY *key;
 	unsigned int siglen;
-	BIGNUM *r, *s;
+	BIGNUM *r, *s, *order;
 
 	(void)kinv; /* Precomputed values are not used for PKCS#11 */
 	(void)rp; /* Precomputed values are not used for PKCS#11 */
@@ -258,6 +258,18 @@ static ECDSA_SIG *pkcs11_ecdsa_sign_sig(const unsigned char *dgst, int dlen,
 	}
 	/* TODO: Add an atfork check */
 
+	/* Truncate digest if its byte size is longer than needed */
+	order = BN_new();
+	if (order) {
+		const EC_GROUP *group = EC_KEY_get0_group(ec);
+		if (group && EC_GROUP_get_order(group, order, NULL)) {
+			int klen = BN_num_bits(order);
+			if (klen < 8*dlen)
+				dlen = (klen+7)/8;
+		}
+		BN_free(order);
+	}
+
 	siglen = sizeof sigret;
 	if (pkcs11_ecdsa_sign(dgst, dlen, sigret, &siglen, key) <= 0)
 		return NULL;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-opensc/libp11.git



More information about the pkg-opensc-commit mailing list