Skip to content

Commit 6c91530

Browse files
committed
Fix Edwards and Montgomery "field_length"
The Edwards and Montgomery "field_length" is changed from 255 to 256 and for Ed448, 448 is changed to 456 as it needs an extra byte. The field_length in bits, and is used to caculate number of bytes. The BYTES4BITS(255) returns 32 and happens to work but should be BTYES4BITS(256) so the 25519 curves have been working but not as expected. But BYTES4BITS(448) returns 56, but Ed448 needed an extra byte so will use BYTES4BITS(456) to return 57 as required in the RFCs. On branch ED448-X448-fix Changes to be committed: modified: src/libopensc/pkcs15-pubkey.c modified: src/pkcs11/framework-pkcs15.c modified: src/tools/pkcs11-tool.c modified: src/tools/pkcs15-init.c
1 parent 52bc91c commit 6c91530

File tree

4 files changed

+29
-27
lines changed

4 files changed

+29
-27
lines changed

src/libopensc/pkcs15-pubkey.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -1524,17 +1524,18 @@ static struct ec_curve_info {
15241524

15251525
/* OpenPGP extensions by Yubikey and GNUK are not defined in RFCs but we know the oid written to card */
15261526

1527-
{"edwards25519", "1.3.6.1.4.1.11591.15.1", {(u8 *)"\x06\x09\x2B\x06\x01\x04\x01\xDA\x47\x0F\x01", 11}, 255, SC_ALGORITHM_EDDSA},
1528-
{"curve25519", "1.3.6.1.4.1.3029.1.5.1", {(u8 *)"\x06\x0A\x2B\x06\x01\x04\x01\x97\x55\x01\x05\x01", 12}, 255, SC_ALGORITHM_XEDDSA},
1527+
{"edwards25519", "1.3.6.1.4.1.11591.15.1", {(u8 *)"\x06\x09\x2B\x06\x01\x04\x01\xDA\x47\x0F\x01", 11}, 256, SC_ALGORITHM_EDDSA},
1528+
{"curve25519", "1.3.6.1.4.1.3029.1.5.1", {(u8 *)"\x06\x0A\x2B\x06\x01\x04\x01\x97\x55\x01\x05\x01", 12}, 256, SC_ALGORITHM_XEDDSA},
15291529

15301530
/* RFC 8410 defined curves */
1531-
{"X25519", "1.3.101.110", {(u8 *)"\x06\x03\x2b\x65\x6e", 5}, 255, SC_ALGORITHM_XEDDSA},
1531+
{"X25519", "1.3.101.110", {(u8 *)"\x06\x03\x2b\x65\x6e", 5}, 256, SC_ALGORITHM_XEDDSA},
15321532
{"X448", "1.3.101.111", {(u8 *)"\x06\x03\x2b\x65\x6f", 5}, 448, SC_ALGORITHM_XEDDSA},
1533-
{"Ed25519", "1.3.101.112", {(u8 *)"\x06\x03\x2b\x65\x70", 5}, 255, SC_ALGORITHM_EDDSA},
1534-
{"Ed448", "1.3.101.113", {(u8 *)"\x06\x03\x2b\x65\x71", 5}, 448, SC_ALGORITHM_EDDSA},
1533+
{"Ed25519", "1.3.101.112", {(u8 *)"\x06\x03\x2b\x65\x70", 5}, 256, SC_ALGORITHM_EDDSA},
1534+
/* Ed448 needs extra byte thus 456 */
1535+
{"Ed448", "1.3.101.113", {(u8 *)"\x06\x03\x2b\x65\x71", 5}, 456, SC_ALGORITHM_EDDSA},
15351536
/* GnuPG openpgp curves as used in gnupg-card are equivalent to RFC8410 OIDs */
1536-
{"cv25519", "1.3.101.110", {(u8 *)"\x06\x03\x2b\x65\x6e", 5}, 255, SC_ALGORITHM_XEDDSA},
1537-
{"ed25519", "1.3.101.112", {(u8 *)"\x06\x03\x2b\x65\x70", 5}, 255, SC_ALGORITHM_EDDSA},
1537+
{"cv25519", "1.3.101.110", {(u8 *)"\x06\x03\x2b\x65\x6e", 5}, 256, SC_ALGORITHM_XEDDSA},
1538+
{"ed25519", "1.3.101.112", {(u8 *)"\x06\x03\x2b\x65\x70", 5}, 256, SC_ALGORITHM_EDDSA},
15381539

15391540
{NULL, NULL, {NULL, 0}, 0, 0}, /* Do not touch this */
15401541
};

src/pkcs11/framework-pkcs15.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -2866,15 +2866,15 @@ pkcs15_create_public_key(struct sc_pkcs11_slot *slot, struct sc_profile *profile
28662866
}
28672867

28682868
if (key_type == CKK_EC_EDWARDS) {
2869-
if (ec->ecpointQ.len == BYTES4BITS(255))
2869+
if (ec->ecpointQ.len == BYTES4BITS(256))
28702870
ec->params.id = oid_ED25519;
2871-
else if (ec->ecpointQ.len == BYTES4BITS(448))
2871+
else if (ec->ecpointQ.len == BYTES4BITS(456)) /* note extra byte */
28722872
ec->params.id = oid_ED448;
28732873
else
28742874
return CKR_ATTRIBUTE_VALUE_INVALID;
28752875

28762876
} else if (key_type == CKK_EC_MONTGOMERY) {
2877-
if (ec->ecpointQ.len == BYTES4BITS(255))
2877+
if (ec->ecpointQ.len == BYTES4BITS(256))
28782878
ec->params.id = oid_X25519;
28792879
else if (ec->ecpointQ.len == BYTES4BITS(448))
28802880
ec->params.id = oid_X448;

src/tools/pkcs11-tool.c

+12-11
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,19 @@ static struct ec_curve_info {
154154
/* Some of the following may not yet be supported by the OpenSC module, but may be by other modules */
155155
/* OpenPGP extensions by Yubikey and GNUK are not defined in RFCs, so pass by printable string */
156156
/* See PKCS#11 3.0 2.3.7 */
157-
{"edwards25519", "1.3.6.1.4.1.11591.15.1", (unsigned char*)"\x13\x0c\x65\x64\x77\x61\x72\x64\x73\x32\x35\x35\x31\x39", 14, 255, CKM_EC_EDWARDS_KEY_PAIR_GEN}, /* send by curve name */
158-
{"curve25519", "1.3.6.1.4.1.3029.1.5.1", (unsigned char*)"\x13\x0a\x63\x75\x72\x76\x65\x32\x35\x35\x31\x39", 12, 255, CKM_EC_MONTGOMERY_KEY_PAIR_GEN}, /* send by curve name */
157+
{"edwards25519", "1.3.6.1.4.1.11591.15.1", (unsigned char*)"\x13\x0c\x65\x64\x77\x61\x72\x64\x73\x32\x35\x35\x31\x39", 14, 256, CKM_EC_EDWARDS_KEY_PAIR_GEN}, /* send by curve name */
158+
{"curve25519", "1.3.6.1.4.1.3029.1.5.1", (unsigned char*)"\x13\x0a\x63\x75\x72\x76\x65\x32\x35\x35\x31\x39", 12, 256, CKM_EC_MONTGOMERY_KEY_PAIR_GEN}, /* send by curve name */
159159

160160
/* RFC8410, EDWARDS and MONTGOMERY curves are used by GnuPG and also by OpenSSL */
161161

162-
{"X25519", "1.3.101.110", (unsigned char*)"\x06\x03\x2b\x65\x6e", 5, 255, CKM_EC_MONTGOMERY_KEY_PAIR_GEN}, /* RFC 4810 send by OID */
162+
{"X25519", "1.3.101.110", (unsigned char*)"\x06\x03\x2b\x65\x6e", 5, 256, CKM_EC_MONTGOMERY_KEY_PAIR_GEN}, /* RFC 4810 send by OID */
163163
{"X448", "1.3.101.111", (unsigned char*)"\x06\x03\x2b\x65\x6f", 5, 448, CKM_EC_MONTGOMERY_KEY_PAIR_GEN}, /* RFC 4810 send by OID */
164164
{"Ed25519", "1.3.101.112", (unsigned char*)"\x06\x03\x2b\x65\x70", 5, 255, CKM_EC_EDWARDS_KEY_PAIR_GEN}, /* RFC 4810 send by OID */
165-
{"Ed448", "1.3.101.113", (unsigned char*)"\x06\x03\x2b\x65\x71", 5, 448, CKM_EC_EDWARDS_KEY_PAIR_GEN}, /* RFC 4810 send by OID */
165+
{"Ed448", "1.3.101.113", (unsigned char*)"\x06\x03\x2b\x65\x71", 5, 456, CKM_EC_EDWARDS_KEY_PAIR_GEN}, /* RFC 4810 send by OID */
166166

167167
/* GnuPG openpgp curves as used in gnupg-card are equivalent to RFC8410 OIDs */
168-
{"cv25519", "1.3.101.110", (unsigned char*)"\x06\x03\x2b\x65\x6e", 5, 255, CKM_EC_MONTGOMERY_KEY_PAIR_GEN},
169-
{"ed25519", "1.3.101.112", (unsigned char*)"\x06\x03\x2b\x65\x70", 5, 255, CKM_EC_EDWARDS_KEY_PAIR_GEN},
168+
{"cv25519", "1.3.101.110", (unsigned char*)"\x06\x03\x2b\x65\x6e", 5, 256, CKM_EC_MONTGOMERY_KEY_PAIR_GEN},
169+
{"ed25519", "1.3.101.112", (unsigned char*)"\x06\x03\x2b\x65\x70", 5, 256, CKM_EC_EDWARDS_KEY_PAIR_GEN},
170170
/* OpenSC card-openpgp.c will map these to what is need on the card */
171171

172172
{NULL, NULL, NULL, 0, 0, 0},
@@ -2452,7 +2452,7 @@ static void sign_data(CK_SLOT_ID slot, CK_SESSION_HANDLE session,
24522452
}
24532453

24542454
/* Ed448: need the params defined but default to false */
2455-
if (curve->size == 448) {
2455+
if (curve->size == 456) {
24562456
mech.pParameter = &eddsa_params;
24572457
mech.ulParameterLen = (CK_ULONG)sizeof(eddsa_params);
24582458
}
@@ -2607,7 +2607,7 @@ static void verify_signature(CK_SLOT_ID slot, CK_SESSION_HANDLE session,
26072607
}
26082608

26092609
/* Ed448: need the params defined but default to false */
2610-
if (curve->size == 448) {
2610+
if (curve->size == 456) {
26112611
mech.pParameter = &eddsa_params;
26122612
mech.ulParameterLen = (CK_ULONG)sizeof(eddsa_params);
26132613
}
@@ -6589,14 +6589,15 @@ static int read_object(CK_SESSION_HANDLE session)
65896589
}
65906590
}
65916591

6592-
if (type == CKK_EC_EDWARDS && os->length == BYTES4BITS(255))
6592+
if (type == CKK_EC_EDWARDS && os->length == BYTES4BITS(256))
65936593
raw_pk = EVP_PKEY_ED25519;
65946594
#if defined(EVP_PKEY_ED448)
6595-
else if (type == CKK_EC_EDWARDS && os->length == BYTES4BITS(448))
6595+
/* note extra byte */
6596+
else if (type == CKK_EC_EDWARDS && os->length == BYTES4BITS(456))
65966597
raw_pk = EVP_PKEY_ED448;
65976598
#endif /* EVP_PKEY_ED448 */
65986599
#if defined(EVP_PKEY_X25519)
6599-
else if (type == CKK_EC_MONTGOMERY && os->length == BYTES4BITS(255))
6600+
else if (type == CKK_EC_MONTGOMERY && os->length == BYTES4BITS(256))
66006601
raw_pk = EVP_PKEY_X25519;
66016602
#endif /*EVP_PKEY_X25519 */
66026603
#if defined(EVP_PKEY_X448)

src/tools/pkcs15-init.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -730,15 +730,15 @@ static const struct alg_spec alg_types_asym[] = {
730730
{ "gost2001", SC_ALGORITHM_GOSTR3410, SC_PKCS15_GOSTR3410_KEYSIZE },
731731
{ "ec", SC_ALGORITHM_EC, 0 }, /* keybits derived from curve */
732732
/* RFC 8410 */
733-
{ "Ed25519", SC_ALGORITHM_EDDSA, 255 }, /* RFC 8410 and gunpg */
734-
{ "Ed448", SC_ALGORITHM_EDDSA, 448 },
735-
{ "X25519", SC_ALGORITHM_XEDDSA, 255 },
733+
{ "Ed25519", SC_ALGORITHM_EDDSA, 256 }, /* RFC 8410 and gunpg */
734+
{ "Ed448", SC_ALGORITHM_EDDSA, 456 }, /* note extra btye */
735+
{ "X25519", SC_ALGORITHM_XEDDSA, 256 },
736736
{ "X448", SC_ALGORITHM_XEDDSA, 448 },
737737
/* used by Yubikey and GNUK */
738-
{ "edwards25519", SC_ALGORITHM_EDDSA, 255 },
739-
{ "curve25519", SC_ALGORITHM_XEDDSA, 255 },
738+
{ "edwards25519", SC_ALGORITHM_EDDSA, 256 },
739+
{ "curve25519", SC_ALGORITHM_XEDDSA, 256 },
740740
/* gnupg */
741-
{ "cv25519", SC_ALGORITHM_XEDDSA, 255 },
741+
{ "cv25519", SC_ALGORITHM_XEDDSA, 256 },
742742

743743
{ NULL, -1, 0 }
744744
};

0 commit comments

Comments
 (0)