add wc_EccPublicKeyToDer function

This commit is contained in:
Jacob Barthelmeh 2016-04-01 10:43:45 -06:00
parent a0cd888fbf
commit 4f8fffbc37
4 changed files with 58 additions and 4 deletions

1
.gitignore vendored
View File

@ -71,6 +71,7 @@ ntru-cert.pem
ntru-key.raw
key.der
key.pem
ecc-public-key.der
ecc-key.der
ecc-key.pem
certreq.der

View File

@ -5877,9 +5877,8 @@ static int SetSerial(const byte* serial, byte* output)
return length + CTC_SERIAL_SIZE;
}
#ifdef HAVE_ECC
#endif /* defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) */
#if defined(HAVE_ECC) && (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
/* Write a public ECC key to output */
static int SetEccPublicKey(byte* output, ecc_key* key, int with_header)
@ -5978,8 +5977,38 @@ static int SetEccPublicKey(byte* output, ecc_key* key, int with_header)
}
#endif /* HAVE_ECC */
/* returns the size of buffer used, the public ECC key in DER format is stored
in output buffer
with_AlgCurve is a flag for when to include a header that has the Algorithm
and Curve infromation */
int wc_EccPublicKeyToDer(ecc_key* key, byte* output, word32 inLen,
int with_AlgCurve)
{
word32 infoSz = 0;
if (output == NULL || key == NULL) {
return BAD_FUNC_ARG;
}
if (with_AlgCurve) {
int maxSetLength = 4; /* max buffer space needed for SetLength */
int asnBit = 1; /* buffer space needed for asn bit string macro */
infoSz += asnBit;
infoSz += maxSetLength + asnBit; /* SetSequence buffer needed */
infoSz += 2 * MAX_ALGO_SZ; /* buffer space for algorithm/curve */
infoSz += asnBit;
infoSz += maxSetLength;
}
if (inLen < wc_ecc_size(key) + infoSz) {
return BAD_FUNC_ARG;
}
return SetEccPublicKey(output, key, with_AlgCurve);
}
#endif /* HAVE_ECC && (WOLFSSL_CERT_GEN || WOLFSSL_KEY_GEN) */
#if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
static INLINE byte itob(int number)
{

View File

@ -5001,6 +5001,7 @@ int rsa_test(void)
free(tmp);
return -5415;
}
fclose(pemFile);
free(pem);
free(derCert);
@ -6486,6 +6487,25 @@ static int ecc_test_key_gen(WC_RNG* rng, int keySize)
return -1029;
}
/* test export of public key */
derSz = wc_EccPublicKeyToDer(&userA, der, FOURK_BUF, 1);
if (derSz <= 0) {
return -5516;
}
#ifdef FREESCALE_MQX
keyFile = fopen("a:\\certs\\ecc-public-key.der", "wb");
#else
keyFile = fopen("./ecc-public-key.der", "wb");
#endif
if (!keyFile) {
return -5417;
}
ret = (int)fwrite(der, 1, derSz, keyFile);
fclose(keyFile);
if (ret != derSz) {
return -5418;
}
wc_ecc_free(&userA);
return 0;

View File

@ -259,6 +259,10 @@ WOLFSSL_API int wc_SetCertificatePolicies(Cert *cert, const char **input);
/* public key helper */
WOLFSSL_API int wc_EccPublicKeyDecode(const byte*, word32*,
ecc_key*, word32);
#if (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
WOLFSSL_API int wc_EccPublicKeyToDer(ecc_key*, byte* output,
word32 inLen, int with_AlgCurve);
#endif
#endif
/* DER encode signature */