Skip to content

Instantly share code, notes, and snippets.

@blluv
Created July 8, 2024 12:41
Show Gist options
  • Save blluv/10c656410586110944f43a72b927bffb to your computer and use it in GitHub Desktop.
Save blluv/10c656410586110944f43a72b927bffb to your computer and use it in GitHub Desktop.
openssl3 ecc
#include <openssl/ec.h>
#include <openssl/obj_mac.h>
#include <openssl/objects.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/core_names.h>
void print_hex(const unsigned char *buf, size_t buf_len)
{
for (size_t i = 0; i < buf_len; i++)
{
printf("%02x", buf[i]);
}
printf("\n");
}
int main()
{
EVP_PKEY *pkey = EVP_PKEY_new();
EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
EVP_PKEY_keygen_init(pctx);
EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_X9_62_prime256v1);
EVP_PKEY_keygen(pctx, &pkey);
if (EVP_PKEY_set_utf8_string_param(pkey, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, "compressed") != 1)
{
printf("EVP_PKEY_set_utf8_string_param failed\n");
return 1;
}
OSSL_PARAM *params;
if (EVP_PKEY_todata(pkey, EVP_PKEY_PUBLIC_KEY, &params) == 0)
{
printf("EVP_PKEY_todata failed\n");
return 1;
}
OSSL_PARAM *params_pub = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PUB_KEY);
if (params_pub == NULL)
{
printf("OSSL_PARAM_locate failed\n");
return 1;
}
printf("params: %s\n", params_pub->key);
print_hex((unsigned char *)params_pub->data, params_pub->data_size);
OSSL_PARAM_free(params);
EVP_PKEY_free(pkey);
EVP_PKEY_CTX_free(pctx);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment