Merge pull request #9104 from kojiws/export_long_key_orig_asn

Improve original implementation on SetAsymKeyDer() and the test
This commit is contained in:
Sean Parkinson
2025-08-18 22:11:25 +10:00
committed by GitHub
5 changed files with 400 additions and 44 deletions
+387
View File
@@ -23,6 +23,393 @@
#include <tests/api/test_asn.h>
#if defined(WC_ENABLE_ASYM_KEY_EXPORT) && defined(HAVE_ED25519)
static int test_SetAsymKeyDer_once(byte* privKey, word32 privKeySz, byte* pubKey,
word32 pubKeySz, byte* trueDer, word32 trueDerSz)
{
EXPECT_DECLS;
byte* calcDer = NULL;
word32 calcDerSz = 0;
ExpectIntEQ(calcDerSz = SetAsymKeyDer(privKey, privKeySz, pubKey, pubKeySz,
NULL, 0, ED25519k), trueDerSz);
ExpectNotNull(calcDer = (byte*)XMALLOC(calcDerSz, NULL,
DYNAMIC_TYPE_TMP_BUFFER));
ExpectIntEQ(calcDerSz = SetAsymKeyDer(privKey, privKeySz, pubKey, pubKeySz,
calcDer, calcDerSz, ED25519k), trueDerSz);
ExpectIntEQ(XMEMCMP(calcDer, trueDer, trueDerSz), 0);
XFREE(calcDer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return EXPECT_RESULT();
}
#endif /* WC_ENABLE_ASYM_KEY_EXPORT && HAVE_ED25519 */
int test_SetAsymKeyDer(void)
{
EXPECT_DECLS;
#if defined(WC_ENABLE_ASYM_KEY_EXPORT) && defined(HAVE_ED25519)
/* We can't access the keyEd25519Oid variable, so declare it instead */
byte algId[] = {43, 101, 112};
byte version[] = {0x0};
byte keyPat = 0xcc;
byte* privKey = NULL;
word32 privKeySz = 0;
byte* pubKey = NULL;
word32 pubKeySz = 0;
byte trueDer[310]; /* The largest size is 310 bytes on Condition 8 */
word32 trueDerSz = 0;
/*
* Condition 1:
* PKEY data = 34 (1 to 127)
* PKEY_CURVEPKEY data = 32 (1 to 127)
* PUBKEY data = 0 (Empty)
* SEQ data = 46 (1 to 127)
*/
privKeySz = 32;
pubKeySz = 0;
trueDerSz = 48;
/* SEQ */
trueDer[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[1] = trueDerSz - 2;
/* VER */
trueDer[2] = ASN_INTEGER;
trueDer[3] = sizeof(version);
trueDer[4] = version[0];
/* PKEYALGO_SEQ */
trueDer[5] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[6] = sizeof(algId) + 2;
trueDer[7] = ASN_OBJECT_ID;
trueDer[8] = sizeof(algId);
trueDer[9] = algId[0];
trueDer[10] = algId[1];
trueDer[11] = algId[2];
/* PKEY */
trueDer[12] = ASN_OCTET_STRING;
trueDer[13] = privKeySz + 2;
trueDer[14] = ASN_OCTET_STRING;
trueDer[15] = privKeySz;
privKey = &trueDer[16];
XMEMSET(privKey, keyPat, privKeySz); /* trueDer[16] to trueDer[47] */
/* PUBKEY */
pubKey = NULL; /* Empty */
EXPECT_TEST(test_SetAsymKeyDer_once(privKey, privKeySz, pubKey, pubKeySz,
trueDer, trueDerSz));
/*
* Condition 2:
* PKEY data = 129 (128 to 255)
* PKEY_CURVEKEY data = 127 (0 to 127)
* PUBKEY data = 0 (Empty)
* SEQ data = 142 (128 to 255)
*/
privKeySz = 127;
pubKeySz = 0;
trueDerSz = 145;
/* SEQ */
trueDer[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[1] = 0x81;
trueDer[2] = trueDerSz - 3;
/* VER */
trueDer[3] = ASN_INTEGER;
trueDer[4] = sizeof(version);
trueDer[5] = version[0];
/* PKEYALGO_SEQ */
trueDer[6] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[7] = sizeof(algId) + 2;
trueDer[8] = ASN_OBJECT_ID;
trueDer[9] = sizeof(algId);
trueDer[10] = algId[0];
trueDer[11] = algId[1];
trueDer[12] = algId[2];
/* PKEY */
trueDer[13] = ASN_OCTET_STRING;
trueDer[14] = 0x81;
trueDer[15] = privKeySz + 2;
trueDer[16] = ASN_OCTET_STRING;
trueDer[17] = privKeySz;
privKey = &trueDer[18];
XMEMSET(privKey, keyPat, privKeySz); /* trueDer[18] to trueDer[144] */
/* PUBKEY */
pubKey = NULL; /* Empty */
EXPECT_TEST(test_SetAsymKeyDer_once(privKey, privKeySz, pubKey, pubKeySz,
trueDer, trueDerSz));
/*
* Condition 3:
* PKEY data = 131 (128 to 255)
* PKEY_CURVEKEY = 128 (128 to 255)
* PUBKEY data = 0 (Empty)
* SEQ data =144 (128 to 255)
*/
privKeySz = 128;
pubKeySz = 0;
trueDerSz = 147;
/* SEQ */
trueDer[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[1] = 0x81;
trueDer[2] = trueDerSz - 3;
/* VER */
trueDer[3] = ASN_INTEGER;
trueDer[4] = sizeof(version);
trueDer[5] = version[0];
/* PKEYALGO_SEQ */
trueDer[6] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[7] = sizeof(algId) + 2;
trueDer[8] = ASN_OBJECT_ID;
trueDer[9] = sizeof(algId);
trueDer[10] = algId[0];
trueDer[11] = algId[1];
trueDer[12] = algId[2];
/* PKEY */
trueDer[13] = ASN_OCTET_STRING;
trueDer[14] = 0x81;
trueDer[15] = privKeySz + 3;
trueDer[16] = ASN_OCTET_STRING;
trueDer[17] = 0x81;
trueDer[18] = privKeySz;
privKey = &trueDer[19];
XMEMSET(privKey, keyPat, privKeySz); /* trueDer[19] to trueDer[146] */
/* PUBKEY */
pubKey = NULL; /* Empty */
EXPECT_TEST(test_SetAsymKeyDer_once(privKey, privKeySz, pubKey, pubKeySz,
trueDer, trueDerSz));
/*
* Condition 4:
* PKEY data = 258 (256 to 65535)
* PKEY_CURVEPKEY data = 255 (128 to 255)
* PUBKEY data = 0 (Empty)
* SEQ data = 272 (256 to 65536)
*/
privKeySz = 255;
pubKeySz = 0;
trueDerSz = 276;
/* SEQ */
trueDer[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[1] = 0x82;
trueDer[2] = ((trueDerSz - 4) >> 8) & 0xff;
trueDer[3] = (trueDerSz - 4) & 0xff;
/* VER */
trueDer[4] = ASN_INTEGER;
trueDer[5] = sizeof(version);
trueDer[6] = version[0];
/* PKEYALGO_SEQ */
trueDer[7] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[8] = sizeof(algId) + 2;
trueDer[9] = ASN_OBJECT_ID;
trueDer[10] = sizeof(algId);
trueDer[11] = algId[0];
trueDer[12] = algId[1];
trueDer[13] = algId[2];
/* PKEY */
trueDer[14] = ASN_OCTET_STRING;
trueDer[15] = 0x82;
trueDer[16] = ((privKeySz + 3) >> 8) & 0xff;
trueDer[17] = (privKeySz + 3) & 0xff;
trueDer[18] = ASN_OCTET_STRING;
trueDer[19] = 0x81;
trueDer[20] = privKeySz;
privKey = &trueDer[21];
XMEMSET(privKey, keyPat, privKeySz); /* trueDer[21] to trueDer[275] */
/* PUBKEY */
pubKey = NULL; /* Empty */
EXPECT_TEST(test_SetAsymKeyDer_once(privKey, privKeySz, pubKey, pubKeySz,
trueDer, trueDerSz));
/*
* Condition 5:
* PKEY data = 260 (256 to 65535)
* PKEY_CURVEPKEY data = 256 (256 to 65535)
* PUBKEY data = 0 (Empty)
* SEQ data = 274 (256 to 65535)
*/
privKeySz = 256;
pubKeySz = 0;
trueDerSz = 278;
/* SEQ */
trueDer[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[1] = 0x82;
trueDer[2] = ((trueDerSz - 4) >> 8) & 0xff;
trueDer[3] = (trueDerSz - 4) & 0xff;
/* VER */
trueDer[4] = ASN_INTEGER;
trueDer[5] = sizeof(version);
trueDer[6] = version[0];
/* PKEYALGO_SEQ */
trueDer[7] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[8] = sizeof(algId) + 2;
trueDer[9] = ASN_OBJECT_ID;
trueDer[10] = sizeof(algId);
trueDer[11] = algId[0];
trueDer[12] = algId[1];
trueDer[13] = algId[2];
/* PKEY */
trueDer[14] = ASN_OCTET_STRING;
trueDer[15] = 0x82;
trueDer[16] = ((privKeySz + 4) >> 8) & 0xff;
trueDer[17] = (privKeySz + 4) & 0xff;
trueDer[18] = ASN_OCTET_STRING;
trueDer[19] = 0x82;
trueDer[20] = (privKeySz >> 8) & 0xff;
trueDer[21] = privKeySz & 0xff;
privKey = &trueDer[22];
XMEMSET(privKey, keyPat, privKeySz); /* trueDer[22] to trueDer[277] */
/* PUBKEY */
pubKey = NULL; /* Empty */
EXPECT_TEST(test_SetAsymKeyDer_once(privKey, privKeySz, pubKey, pubKeySz,
trueDer, trueDerSz));
/*
* Condition 6:
* PKEY data = 34 (1 to 127)
* PKEY_CURVEPKEY data = 32 (1 to 127)
* PUBKEY data = 32 (1 to 127)
* SEQ data = 80 (1 to 127)
*/
privKeySz = 32;
pubKeySz = 32;
trueDerSz = 82;
/* SEQ */
trueDer[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[1] = trueDerSz - 2;
/* VER */
trueDer[2] = ASN_INTEGER;
trueDer[3] = sizeof(version);
trueDer[4] = version[0];
/* PKEYALGO_SEQ */
trueDer[5] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[6] = sizeof(algId) + 2;
trueDer[7] = ASN_OBJECT_ID;
trueDer[8] = sizeof(algId);
trueDer[9] = algId[0];
trueDer[10] = algId[1];
trueDer[11] = algId[2];
/* PKEY */
trueDer[12] = ASN_OCTET_STRING;
trueDer[13] = privKeySz + 2;
trueDer[14] = ASN_OCTET_STRING;
trueDer[15] = privKeySz;
privKey = &trueDer[16];
XMEMSET(privKey, keyPat, privKeySz); /* trueDer[16] to trueDer[47] */
/* PUBKEY */
trueDer[48] = ASN_CONTEXT_SPECIFIC | ASN_ASYMKEY_PUBKEY;
trueDer[49] = pubKeySz;
pubKey = &trueDer[50];
XMEMSET(pubKey, keyPat, pubKeySz); /* trueDer[50] to trueDer[81] */
EXPECT_TEST(test_SetAsymKeyDer_once(privKey, privKeySz, pubKey, pubKeySz,
trueDer, trueDerSz));
/*
* Condition 7:
* PKEY data = 34 (1 to 127)
* PKEY_CURVEPKEY data = 32 (1 to 127)
* PUBKEY data = 128 (128 to 255)
* SEQ data = 180 (128 to 255)
*/
privKeySz = 32;
pubKeySz = 128;
trueDerSz = 180;
/* SEQ */
trueDer[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[1] = 0x81;
trueDer[2] = trueDerSz - 3;
/* VER */
trueDer[3] = ASN_INTEGER;
trueDer[4] = sizeof(version);
trueDer[5] = version[0];
/* PKEYALGO_SEQ */
trueDer[6] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[7] = sizeof(algId) + 2;
trueDer[8] = ASN_OBJECT_ID;
trueDer[9] = sizeof(algId);
trueDer[10] = algId[0];
trueDer[11] = algId[1];
trueDer[12] = algId[2];
/* PKEY */
trueDer[13] = ASN_OCTET_STRING;
trueDer[14] = privKeySz + 2;
trueDer[15] = ASN_OCTET_STRING;
trueDer[16] = privKeySz;
privKey = &trueDer[17];
XMEMSET(privKey, keyPat, privKeySz); /* trueDer[17] to trueDer[48] */
/* PUBKEY */
trueDer[49] = ASN_CONTEXT_SPECIFIC | ASN_ASYMKEY_PUBKEY;
trueDer[50] = 0x81;
trueDer[51] = pubKeySz;
pubKey = &trueDer[52];
XMEMSET(pubKey, keyPat, pubKeySz); /* trueDer[52] to trueDer[179] */
EXPECT_TEST(test_SetAsymKeyDer_once(privKey, privKeySz, pubKey, pubKeySz,
trueDer, trueDerSz));
/*
* Condition 8:
* PKEY data = 34 (1 to 127)
* PKEY_CURVEPKEY data = 32 (1 to 127)
* PUBKEY data = 256 (256 to 65535)
* SEQ data = 306 (256 to 65535)
*/
privKeySz = 32;
pubKeySz = 256;
trueDerSz = 310;
/* SEQ */
trueDer[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[1] = 0x82;
trueDer[2] = ((trueDerSz - 4) >> 8) & 0xff;
trueDer[3] = (trueDerSz - 4) & 0xff;
/* VER */
trueDer[4] = ASN_INTEGER;
trueDer[5] = sizeof(version);
trueDer[6] = version[0];
/* PKEYALGO_SEQ */
trueDer[7] = ASN_SEQUENCE | ASN_CONSTRUCTED;
trueDer[8] = sizeof(algId) + 2;
trueDer[9] = ASN_OBJECT_ID;
trueDer[10] = sizeof(algId);
trueDer[11] = algId[0];
trueDer[12] = algId[1];
trueDer[13] = algId[2];
/* PKEY */
trueDer[14] = ASN_OCTET_STRING;
trueDer[15] = privKeySz + 2;
trueDer[16] = ASN_OCTET_STRING;
trueDer[17] = privKeySz;
privKey = &trueDer[18];
XMEMSET(privKey, keyPat, privKeySz); /* trueDer[18] to trueDer[49] */
/* PUBKEY */
trueDer[50] = ASN_CONTEXT_SPECIFIC | ASN_ASYMKEY_PUBKEY;
trueDer[51] = 0x82;
trueDer[52] = (pubKeySz >> 8) & 0xff;
trueDer[53] = pubKeySz & 0xff;
pubKey = &trueDer[54];
XMEMSET(pubKey, keyPat, pubKeySz); /* trueDer[54] to trueDer[309] */
EXPECT_TEST(test_SetAsymKeyDer_once(privKey, privKeySz, pubKey, pubKeySz,
trueDer, trueDerSz));
#endif /* WC_ENABLE_ASYM_KEY_EXPORT && HAVE_ED25519 */
return EXPECT_RESULT();
}
#ifndef NO_ASN
static int test_SetShortInt_once(word32 val, byte* valDer, word32 valDerSz)
{
+2
View File
@@ -24,10 +24,12 @@
#include <tests/api/api_decl.h>
int test_SetAsymKeyDer(void);
int test_SetShortInt(void);
int test_wc_IndexSequenceOf(void);
#define TEST_ASN_DECLS \
TEST_DECL_GROUP("asn", test_SetAsymKeyDer), \
TEST_DECL_GROUP("asn", test_SetShortInt), \
TEST_DECL_GROUP("asn", test_wc_IndexSequenceOf)
-32
View File
@@ -3004,13 +3004,8 @@ int test_wc_dilithium_der(void)
ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(NULL, NULL,
0 ), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
#ifndef WOLFSSL_ASN_TEMPLATE
ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key , NULL,
0 ), BAD_FUNC_ARG);
#else
ExpectIntGT(wc_Dilithium_PrivateKeyToDer(key , NULL,
0 ), 0);
#endif
ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(NULL, der ,
0 ), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(NULL, NULL,
@@ -3020,23 +3015,13 @@ int test_wc_dilithium_der(void)
ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key , der ,
0 ), WC_NO_ERR_TRACE(BUFFER_E));
/* Get length only. */
#ifndef WOLFSSL_ASN_TEMPLATE
ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key , NULL,
DILITHIUM_MAX_DER_SIZE), BAD_FUNC_ARG);
#else
ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key , NULL,
DILITHIUM_MAX_DER_SIZE), privDerLen);
#endif
ExpectIntEQ(wc_Dilithium_KeyToDer(NULL, NULL, 0 ),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
#ifndef WOLFSSL_ASN_TEMPLATE
ExpectIntEQ(wc_Dilithium_KeyToDer(key , NULL, 0 ),
BAD_FUNC_ARG);
#else
ExpectIntGT(wc_Dilithium_KeyToDer(key , NULL, 0 ),
0 );
#endif
ExpectIntEQ(wc_Dilithium_KeyToDer(NULL, der , 0 ),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_Dilithium_KeyToDer(NULL, NULL, DILITHIUM_MAX_DER_SIZE),
@@ -3046,13 +3031,8 @@ int test_wc_dilithium_der(void)
ExpectIntEQ(wc_Dilithium_KeyToDer(key , der , 0 ),
WC_NO_ERR_TRACE(BUFFER_E));
/* Get length only. */
#ifndef WOLFSSL_ASN_TEMPLATE
ExpectIntEQ(wc_Dilithium_KeyToDer(key , NULL, DILITHIUM_MAX_DER_SIZE),
BAD_FUNC_ARG);
#else
ExpectIntEQ(wc_Dilithium_KeyToDer(key , NULL, DILITHIUM_MAX_DER_SIZE),
keyDerLen);
#endif
ExpectIntEQ(wc_Dilithium_PublicKeyDecode(NULL, NULL, NULL, 0 ),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
@@ -3101,25 +3081,15 @@ int test_wc_dilithium_der(void)
idx = 0;
ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der, &idx, key, len), 0);
#ifndef WOLFSSL_ASN_TEMPLATE
ExpectIntEQ(len = wc_Dilithium_PrivateKeyToDer(key, der,
DILITHIUM_MAX_DER_SIZE), BAD_FUNC_ARG);
#else
ExpectIntEQ(len = wc_Dilithium_PrivateKeyToDer(key, der,
DILITHIUM_MAX_DER_SIZE), privDerLen);
idx = 0;
ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der, &idx, key, len), 0);
#endif
#ifndef WOLFSSL_ASN_TEMPLATE
ExpectIntEQ(len = wc_Dilithium_KeyToDer(key, der, DILITHIUM_MAX_DER_SIZE),
BAD_FUNC_ARG);
#else
ExpectIntEQ(len = wc_Dilithium_KeyToDer(key, der, DILITHIUM_MAX_DER_SIZE),
keyDerLen);
idx = 0;
ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der, &idx, key, len), 0);
#endif
wc_dilithium_free(key);
@@ -3127,8 +3097,6 @@ int test_wc_dilithium_der(void)
XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
(void)keyDerLen;
#endif
return EXPECT_RESULT();
}
+10 -11
View File
@@ -37869,7 +37869,7 @@ int SetAsymKeyDer(const byte* privKey, word32 privKeyLen,
{
int ret = 0;
#ifndef WOLFSSL_ASN_TEMPLATE
word32 idx = 0, seqSz, verSz, algoSz, privSz, pubSz = 0, sz;
word32 idx = 0, seqSz, verSz, algoSz, tmpSz, privSz, pubSz = 0, sz;
#else
DECL_ASNSETDATA(dataASN, privateKeyASN_Length);
int sz = 0;
@@ -37884,16 +37884,15 @@ int SetAsymKeyDer(const byte* privKey, word32 privKeyLen,
}
#ifndef WOLFSSL_ASN_TEMPLATE
if (privKeyLen >= 128 || pubKeyLen >= 128) {
/* privKeyLen and pubKeyLen are assumed to be less than 128 */
return BAD_FUNC_ARG;
}
/* calculate size */
if (pubKey) {
pubSz = 2 + pubKeyLen;
pubSz = SetHeader(ASN_CONTEXT_SPECIFIC | ASN_ASYMKEY_PUBKEY, pubKeyLen,
NULL, 0) + pubKeyLen;
}
privSz = 2 + 2 + privKeyLen;
tmpSz = SetOctetString(privKeyLen, NULL) + privKeyLen;
privSz = SetOctetString(tmpSz, NULL) + tmpSz;
algoSz = SetAlgoID(keyType, NULL, oidKeyType, 0);
verSz = 3; /* version is 3 bytes (enum + id + version(byte)) */
seqSz = SetSequence(verSz + algoSz + privSz + pubSz, NULL);
@@ -37916,14 +37915,14 @@ int SetAsymKeyDer(const byte* privKey, word32 privKeyLen,
algoSz = SetAlgoID(keyType, output + idx, oidKeyType, 0);
idx += algoSz;
/* privKey */
idx += SetOctetString(2 + privKeyLen, output + idx);
idx += SetOctetString(tmpSz, output + idx);
idx += SetOctetString(privKeyLen, output + idx);
XMEMCPY(output + idx, privKey, privKeyLen);
idx += privKeyLen;
/* pubKey */
if (pubKey) {
idx += SetHeader(ASN_CONTEXT_SPECIFIC | ASN_ASYMKEY_PUBKEY |
1, pubKeyLen, output + idx, 0);
idx += SetHeader(ASN_CONTEXT_SPECIFIC | ASN_ASYMKEY_PUBKEY,
pubKeyLen, output + idx, 0);
XMEMCPY(output + idx, pubKey, pubKeyLen);
idx += pubKeyLen;
}
+1 -1
View File
@@ -2763,7 +2763,7 @@ WOLFSSL_LOCAL int DecodeAsymKey(const byte* input, word32* inOutIdx,
#endif
#ifdef WC_ENABLE_ASYM_KEY_EXPORT
WOLFSSL_LOCAL int SetAsymKeyDer(const byte* privKey, word32 privKeyLen,
WOLFSSL_TEST_VIS int SetAsymKeyDer(const byte* privKey, word32 privKeyLen,
const byte* pubKey, word32 pubKeyLen, byte* output, word32 outLen,
int keyType);
#endif