Reputation: 21
EVP_PKEY_CTX *openssl_ctx = NULL;
openssl_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_ED25519, NULL);
if (NULL == openssl_ctx)
{
printf("New Key fail...\n");
goto cleanup;
}
if (EVP_PKEY_keygen_init(openssl_ctx)<=0)
{
printf("EVP_PKEY_keygen_init fail\n");
goto cleanup;
}
if (EVP_PKEY_keygen(openssl_ctx, ppKey)<=0)
{
printf("EVP_PKEY_kengen fail\n");
goto cleanup;
}
ret = ERR_OK;
unsigned char sk[2048], pk[2048];[enter image description here][1]
size_t skLen, pkLen;
if (1 != EVP_PKEY_get_raw_public_key(*ppKey, pk, &pkLen))
{
printf("EVP_PKEY_get_raw_public_key fail");
char *errStr;
int line;
unsigned long err = ERR_get_error_line((const char **)&errStr, &line);
printf("show me the error: %lu, %s:%i\n", err, errStr, line);
}
int p = EVP_PKEY_get_raw_private_key(*ppKey, sk, &skLen);
if (p != 1)
{
printf("EVP_PKEY_get_raw_private_key fail\n");
char *errStr;
int line;
unsigned long err = ERR_get_error_line((const char **)&errStr, &line);
printf("show me the error: %lu, %s:%i\n", err, errStr, line);
}
I want to export the private key and public key of ed25519 from EVP_PKEY, when I use both EVP_PKEY_get_raw_public_key() and EVP_PKEY_get_raw_private_key(), it returns error: EVP_PKEY_get_raw_public_key failshow me the error: 101494966, ../crypto/evp/p_lib.c:310
But when I just use EVP_PKEY_get_raw_public_key() or EVP_PKEY_get_raw_private_key(), it reports no error. Is EVP_PKEY_get_raw_public_key() incompatible with EVP_PKEY_get_raw_private_key()?
Upvotes: 2
Views: 2235
Reputation: 9372
From the man page for EVP_PKEY_get_raw_public_key()
:
int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub, size_t *len);
EVP_PKEY_get_raw_public_key() fills the buffer provided by pub with raw public key data. The size of the pub buffer should be in *len on entry to the function, and on exit *len is updated with the number of bytes actually written. If the buffer pub is NULL then *len is populated with the number of bytes required to hold the key. The calling application is responsible for ensuring that the buffer is large enough to receive the public key data.
Note the entry conditions: "The size of the pub buffer should be in *len on entry to the function". You are sending an uninitialised value so it may or may not work.
The wording on the man page is similar for EVP_PKEY_get_raw_private_key
.
Make sure your skLen
and pkLen
variables are initialised to the size of the buffer before calling these functions.
Upvotes: 2