创建sm2密钥对demo
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <openssl/evp.h>
#include <openssl/sm2.h>
#include <openssl/sm3.h>
#include <openssl/pem.h>
#include <openssl/bio.h>
#include <openssl/pkcs7.h>
#include <openssl/objects.h>
void print_hex(const char *tips, const unsigned char *pdata, const int len) {
if (tips)
printf("%s[%d]: \n", tips, len);
int i = 0;
for (i = 0; i < len; i++) {
printf("0x%02X", pdata[i]);
if (i != len - 1) printf(", ");
if ((i + 1) % 8 == 0) printf("\n");
}
printf("\n");
}
static EC_KEY *create_sm2(void)
{
EC_KEY *eckey = NULL;
eckey = EC_KEY_new_by_curve_name(EVP_PKEY_SM2);
if (eckey == NULL) {
return NULL;
}
EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);
EC_KEY_set_conv_form(eckey, POINT_CONVERSION_UNCOMPRESSED);
if (EC_KEY_generate_key(eckey) == 0) {
EC_KEY_free(eckey);
return NULL;
}
return eckey;
}
int main(){
EC_KEY *eckey = create_sm2();
printf("create_sm2.\n");
EC_KEY_print_fp(stdout, eckey, 0);
}