Fixes for ED25519/ED448 private key with public key export (RFC8410). Added length only support.

pull/5356/head
David Garske 2022-07-13 16:17:08 -07:00
parent a2b7b44163
commit 2d5bc72c9b
2 changed files with 44 additions and 37 deletions

View File

@ -26646,19 +26646,20 @@ static int test_wc_Ed25519KeyToDer (void)
ret = 0; ret = 0;
} }
} }
if (ret == 0) {
ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
if (ret == BAD_FUNC_ARG) {
ret = 0;
}
}
if (ret == 0) { if (ret == 0) {
ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0); ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
if (ret == BAD_FUNC_ARG) { if (ret == BAD_FUNC_ARG) {
ret = 0; ret = 0;
} }
} }
/* Good Case */ /* Good Cases */
if (ret == 0) {
/* length only */
ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
if (ret > 0) {
ret = 0;
}
}
if (ret == 0) { if (ret == 0) {
ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen); ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
if (ret > 0) { if (ret > 0) {
@ -26713,19 +26714,20 @@ static int test_wc_Ed25519PrivateKeyToDer (void)
ret = 0; ret = 0;
} }
} }
if (ret == 0) {
ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
if (ret == BAD_FUNC_ARG) {
ret = 0;
}
}
if (ret == 0) { if (ret == 0) {
ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0); ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
if (ret == BAD_FUNC_ARG) { if (ret == BAD_FUNC_ARG) {
ret = 0; ret = 0;
} }
} }
/* Good Case */ /* Good Cases */
if (ret == 0) {
/* length only */
ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
if (ret > 0) {
ret = 0;
}
}
if (ret == 0) { if (ret == 0) {
ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen); ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
if (ret > 0) { if (ret > 0) {
@ -26779,19 +26781,20 @@ static int test_wc_Ed448KeyToDer (void)
ret = 0; ret = 0;
} }
} }
if (ret == 0) {
ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
if (ret == BAD_FUNC_ARG) {
ret = 0;
}
}
if (ret == 0) { if (ret == 0) {
ret = wc_Ed448KeyToDer(&ed448Key, output, 0); ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
if (ret == BAD_FUNC_ARG) { if (ret == BAD_FUNC_ARG) {
ret = 0; ret = 0;
} }
} }
/* Good Case */ /* Good Cases */
if (ret == 0) {
/* length only */
ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
if (ret > 0) {
ret = 0;
}
}
if (ret == 0) { if (ret == 0) {
ret = wc_Ed448KeyToDer(&ed448Key, output, inLen); ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
if (ret > 0) { if (ret > 0) {
@ -26845,19 +26848,20 @@ static int test_wc_Ed448PrivateKeyToDer (void)
ret = 0; ret = 0;
} }
} }
if (ret == 0) {
ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
if (ret == BAD_FUNC_ARG) {
ret = 0;
}
}
if (ret == 0) { if (ret == 0) {
ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0); ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
if (ret == BAD_FUNC_ARG) { if (ret == BAD_FUNC_ARG) {
ret = 0; ret = 0;
} }
} }
/* Good case */ /* Good cases */
if (ret == 0) {
/* length only */
ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
if (ret > 0) {
ret = 0;
}
}
if (ret == 0) { if (ret == 0) {
ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen); ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
if (ret > 0) { if (ret > 0) {

View File

@ -29755,7 +29755,7 @@ static int DecodeAsymKey(const byte* input, word32* inOutIdx, word32 inSz,
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
} }
if (GetASNHeader(input, ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 1, if (GetASNHeader(input, ASN_CONTEXT_SPECIFIC | ASN_ASYMKEY_PUBKEY | 1,
inOutIdx, &length, inSz) < 0) { inOutIdx, &length, inSz) < 0) {
return ASN_PARSE_E; return ASN_PARSE_E;
} }
@ -30023,7 +30023,6 @@ int wc_Curve25519PublicKeyDecode(const byte* input, word32* inOutIdx,
* @return Size of encoded data in bytes on success * @return Size of encoded data in bytes on success
* @return BAD_FUNC_ARG when key is NULL. * @return BAD_FUNC_ARG when key is NULL.
* @return MEMORY_E when dynamic memory allocation failed. * @return MEMORY_E when dynamic memory allocation failed.
* @return LENGTH_ONLY_E return length only.
*/ */
static int SetAsymKeyDer(const byte* privKey, word32 privKeyLen, static int SetAsymKeyDer(const byte* privKey, word32 privKeyLen,
const byte* pubKey, word32 pubKeyLen, const byte* pubKey, word32 pubKeyLen,
@ -30045,7 +30044,7 @@ static int SetAsymKeyDer(const byte* privKey, word32 privKeyLen,
#ifndef WOLFSSL_ASN_TEMPLATE #ifndef WOLFSSL_ASN_TEMPLATE
/* calculate size */ /* calculate size */
if (pubKey) { if (pubKey) {
pubSz = 2 + 2 + pubKeyLen; pubSz = 2 + pubKeyLen;
} }
privSz = 2 + 2 + privKeyLen; privSz = 2 + 2 + privKeyLen;
algoSz = SetAlgoID(keyType, NULL, oidKeyType, 0); algoSz = SetAlgoID(keyType, NULL, oidKeyType, 0);
@ -30061,7 +30060,7 @@ static int SetAsymKeyDer(const byte* privKey, word32 privKeyLen,
if (ret == 0 && output != NULL) { if (ret == 0 && output != NULL) {
/* write out */ /* write out */
/* seq */ /* seq */
seqSz = SetSequence(verSz + algoSz + privSz + pubSz, output); seqSz = SetSequence(verSz + algoSz + privSz + pubSz, output);
idx = seqSz; idx = seqSz;
/* ver */ /* ver */
SetMyVersion(0, output + idx, FALSE); SetMyVersion(0, output + idx, FALSE);
@ -30076,13 +30075,16 @@ static int SetAsymKeyDer(const byte* privKey, word32 privKeyLen,
idx += privKeyLen; idx += privKeyLen;
/* pubKey */ /* pubKey */
if (pubKey) { if (pubKey) {
idx += SetExplicit(1, 2 + pubKeyLen, output + idx); idx += SetHeader(ASN_CONTEXT_SPECIFIC | ASN_ASYMKEY_PUBKEY |
idx += SetOctetString(pubKeyLen, output + idx); 1, pubKeyLen, output + idx);
XMEMCPY(output + idx, pubKey, pubKeyLen); XMEMCPY(output + idx, pubKey, pubKeyLen);
idx += pubKeyLen; idx += pubKeyLen;
} }
sz = idx;
ret = idx; }
if (ret == 0) {
/* Return size of encoding. */
ret = sz;
} }
#else #else
@ -30128,7 +30130,8 @@ static int SetAsymKeyDer(const byte* privKey, word32 privKeyLen,
XMEMCPY((byte*)dataASN[EDKEYASN_IDX_PUBKEY_VAL].data.buffer.data, XMEMCPY((byte*)dataASN[EDKEYASN_IDX_PUBKEY_VAL].data.buffer.data,
pubKey, pubKeyLen); pubKey, pubKeyLen);
} }
}
if (ret == 0) {
/* Return size of encoding. */ /* Return size of encoding. */
ret = sz; ret = sz;
} }