From 00ad5e342e5a6f921dc04347ee6f78e9a2305182 Mon Sep 17 00:00:00 2001 From: Sourabh Hegde Date: Wed, 4 Nov 2026 16:58:31 +0100 Subject: [PATCH 1/4] fix Windows offline ECDSA issue --- src/libopensc/card-piv.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/libopensc/card-piv.c b/src/libopensc/card-piv.c index 9299104fdc..5da3373484 100644 --- a/src/libopensc/card-piv.c +++ b/src/libopensc/card-piv.c @@ -585,6 +585,7 @@ static struct piv_aid piv_aids[] = { #define CI_RSA_4096 0x00080000U /* Card supports rsa 4096 */ #define CI_25519 0x00100000U /* Card supports ED25519 and X25519 */ #define CI_EC521 0x00200000U /* Card supports EC 521 */ +#define CI_ENFORCES_EC_HASH_EQ_KEYSIZE 0x00400000U /* Applet requires ECDSA input length to equal EC field length */ /* * Flags in the piv_object: @@ -4717,6 +4718,9 @@ piv_compute_signature(sc_card_t *card, const u8 * data, size_t datalen, { piv_private_data_t * priv = PIV_DATA(card); int r; + u8 *ecdata = NULL; + size_t ecdatalen = 0; + size_t ecpadlen = 0; size_t nLen; u8 rbuf[165]; /* For EC conversions 512 will fit */ @@ -4730,6 +4734,22 @@ piv_compute_signature(sc_card_t *card, const u8 * data, size_t datalen, if (priv->alg_id == 0x11 || priv->alg_id == 0x14 || priv->alg_id == 0x32) { nLen = BYTES4BITS(priv->key_size); + + if ((priv->card_issues & CI_ENFORCES_EC_HASH_EQ_KEYSIZE) && + card->ctx->app_name != NULL && + !strcmp(card->ctx->app_name, "cardmod") && + datalen < nLen) { + ecpadlen = nLen - datalen; + } + + ecdatalen = datalen + ecpadlen; + ecdata = calloc(ecdatalen, sizeof(*ecdata)); + if (ecdata == NULL) { + r = SC_ERROR_OUT_OF_MEMORY; + goto err; + } + memcpy(ecdata + ecpadlen, data, datalen); + if (outlen < 2*nLen) { sc_log(card->ctx, " output too small for EC signature %"SC_FORMAT_LEN_SIZE_T"u < %"SC_FORMAT_LEN_SIZE_T"u", @@ -4738,7 +4758,7 @@ piv_compute_signature(sc_card_t *card, const u8 * data, size_t datalen, goto err; } - r = piv_validate_general_authentication(card, data, datalen, rbuf, sizeof rbuf); + r = piv_validate_general_authentication(card, ecdata, ecdatalen, rbuf, sizeof rbuf); if (r < 0) goto err; @@ -4760,6 +4780,7 @@ piv_compute_signature(sc_card_t *card, const u8 * data, size_t datalen, } err: + free(ecdata); SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r); } @@ -5820,7 +5841,7 @@ static int piv_match_card_continued(sc_card_t *card) break; case SC_CARD_TYPE_PIV_II_SWISSBIT2: - priv->card_issues |= 0; /* could add others here */ + priv->card_issues |= CI_ENFORCES_EC_HASH_EQ_KEYSIZE; if (priv->swissbit_version >= 0x00010200) priv->card_issues |= CI_RSA_4096 | CI_EC521; break; From 3abb28141d4c946c63cbb1e4be393a173af6f23a Mon Sep 17 00:00:00 2001 From: Sourabh Hegde Date: Wed, 4 Nov 2026 17:02:46 +0100 Subject: [PATCH 2/4] update --- src/libopensc/card-piv.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libopensc/card-piv.c b/src/libopensc/card-piv.c index 5da3373484..b9654a4d21 100644 --- a/src/libopensc/card-piv.c +++ b/src/libopensc/card-piv.c @@ -585,7 +585,7 @@ static struct piv_aid piv_aids[] = { #define CI_RSA_4096 0x00080000U /* Card supports rsa 4096 */ #define CI_25519 0x00100000U /* Card supports ED25519 and X25519 */ #define CI_EC521 0x00200000U /* Card supports EC 521 */ -#define CI_ENFORCES_EC_HASH_EQ_KEYSIZE 0x00400000U /* Applet requires ECDSA input length to equal EC field length */ +#define CI_ENFORCES_EC_HASH_EQ_KEYSIZE 0x00400000U /* Applet requires ECDSA input length to equal EC field length */ /* * Flags in the piv_object: @@ -4735,9 +4735,9 @@ piv_compute_signature(sc_card_t *card, const u8 * data, size_t datalen, if (priv->alg_id == 0x11 || priv->alg_id == 0x14 || priv->alg_id == 0x32) { nLen = BYTES4BITS(priv->key_size); - if ((priv->card_issues & CI_ENFORCES_EC_HASH_EQ_KEYSIZE) && - card->ctx->app_name != NULL && - !strcmp(card->ctx->app_name, "cardmod") && + if ((priv->card_issues & CI_ENFORCES_EC_HASH_EQ_KEYSIZE) && + card->ctx->app_name != NULL && + !strcmp(card->ctx->app_name, "cardmod") && datalen < nLen) { ecpadlen = nLen - datalen; } @@ -4780,7 +4780,7 @@ piv_compute_signature(sc_card_t *card, const u8 * data, size_t datalen, } err: - free(ecdata); + free(ecdata); SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r); } From e7b8d01e8374969c59a35c7465b3bca98df5e7b0 Mon Sep 17 00:00:00 2001 From: Claus Steuer Date: Wed, 2 Dec 2026 11:07:16 +0100 Subject: [PATCH 3/4] make changes similar to OpenSC upstream changes --- src/libopensc/card-piv.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/libopensc/card-piv.c b/src/libopensc/card-piv.c index b9654a4d21..0f1c056300 100644 --- a/src/libopensc/card-piv.c +++ b/src/libopensc/card-piv.c @@ -585,7 +585,7 @@ static struct piv_aid piv_aids[] = { #define CI_RSA_4096 0x00080000U /* Card supports rsa 4096 */ #define CI_25519 0x00100000U /* Card supports ED25519 and X25519 */ #define CI_EC521 0x00200000U /* Card supports EC 521 */ -#define CI_ENFORCES_EC_HASH_EQ_KEYSIZE 0x00400000U /* Applet requires ECDSA input length to equal EC field length */ +#define CI_ENFORCES_EC_HASH_EQ_KEYSIZE 0x00400000U /* Applet enforces hash size == EC field size for ECDSA. Windows issue */ /* * Flags in the piv_object: @@ -4735,15 +4735,21 @@ piv_compute_signature(sc_card_t *card, const u8 * data, size_t datalen, if (priv->alg_id == 0x11 || priv->alg_id == 0x14 || priv->alg_id == 0x32) { nLen = BYTES4BITS(priv->key_size); - if ((priv->card_issues & CI_ENFORCES_EC_HASH_EQ_KEYSIZE) && - card->ctx->app_name != NULL && + /* One applet is known to enforce the hash size + * must match the field_length in bytes. But Windows + * may try ECDSA without a hash when offline + * So we will pad input to hash size. + */ + if (priv->card_issues & CI_ENFORCES_EC_HASH_EQ_KEYSIZE && !strcmp(card->ctx->app_name, "cardmod") && - datalen < nLen) { + (datalen < nLen)) { ecpadlen = nLen - datalen; + } else { + ecpadlen = 0; } ecdatalen = datalen + ecpadlen; - ecdata = calloc(ecdatalen, sizeof(*ecdata)); + ecdata = calloc(ecdatalen, sizeof(u8)); if (ecdata == NULL) { r = SC_ERROR_OUT_OF_MEMORY; goto err; From 6367fc51dc21596d995800d69aebfcb4088cc514 Mon Sep 17 00:00:00 2001 From: Claus Steuer Date: Wed, 2 Dec 2026 19:54:03 +0100 Subject: [PATCH 4/4] fix signature generation for P-521 --- src/libopensc/card-piv.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/libopensc/card-piv.c b/src/libopensc/card-piv.c index 0f1c056300..8a134d5677 100644 --- a/src/libopensc/card-piv.c +++ b/src/libopensc/card-piv.c @@ -585,7 +585,7 @@ static struct piv_aid piv_aids[] = { #define CI_RSA_4096 0x00080000U /* Card supports rsa 4096 */ #define CI_25519 0x00100000U /* Card supports ED25519 and X25519 */ #define CI_EC521 0x00200000U /* Card supports EC 521 */ -#define CI_ENFORCES_EC_HASH_EQ_KEYSIZE 0x00400000U /* Applet enforces hash size == EC field size for ECDSA. Windows issue */ +#define CI_ENFORCES_ECDSA_HASH_SIZE 0x00400000U /* Applet requires a fixed hash size for each ECDSA curve. */ /* * Flags in the piv_object: @@ -4735,15 +4735,16 @@ piv_compute_signature(sc_card_t *card, const u8 * data, size_t datalen, if (priv->alg_id == 0x11 || priv->alg_id == 0x14 || priv->alg_id == 0x32) { nLen = BYTES4BITS(priv->key_size); - /* One applet is known to enforce the hash size - * must match the field_length in bytes. But Windows - * may try ECDSA without a hash when offline - * So we will pad input to hash size. + /* The applet enforces a fixed hash size for each curve. + * Windows may pass shorter input during offline sign-in, + * so pad the input to the expected hash size. */ - if (priv->card_issues & CI_ENFORCES_EC_HASH_EQ_KEYSIZE && + const size_t hash_len = priv->alg_id == 0x11 ? 32 : + priv->alg_id == 0x14 ? 48 : 64; + if (priv->card_issues & CI_ENFORCES_ECDSA_HASH_SIZE && !strcmp(card->ctx->app_name, "cardmod") && - (datalen < nLen)) { - ecpadlen = nLen - datalen; + (datalen < hash_len)) { + ecpadlen = hash_len - datalen; } else { ecpadlen = 0; } @@ -5847,7 +5848,7 @@ static int piv_match_card_continued(sc_card_t *card) break; case SC_CARD_TYPE_PIV_II_SWISSBIT2: - priv->card_issues |= CI_ENFORCES_EC_HASH_EQ_KEYSIZE; + priv->card_issues |= CI_ENFORCES_ECDSA_HASH_SIZE; if (priv->swissbit_version >= 0x00010200) priv->card_issues |= CI_RSA_4096 | CI_EC521; break;