SM2实现(利⽤实现(利⽤openssl的的evp))
参考博客
加解密加解密
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "openssl/ec.h"
#include "openssl/evp.h"
/**************************************************
* Main function return value:
0: main function executes successfully
-1: an error occurs
**************************************************/
int main(void)
{
int ret = -1, i;
EVP_PKEY_CTX *pctx = NULL, *ectx = NULL;
EVP_PKEY *pkey = NULL;
unsigned char message[16] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };
size_t message_len = sizeof(message);
unsigned char *ciphertext = NULL, *plaintext = NULL;
size_t ciphertext_len, plaintext_len;
EC_KEY *key_pair = NULL;
const BIGNUM *priv_key = NULL;
char *priv_key_str = NULL;
const EC_GROUP *group = NULL;
const EC_POINT *pub_key = NULL;
BN_CTX *ctx = NULL;
BIGNUM *x_coordinate = NULL, *y_coordinate = NULL;
char *x_coordinate_str = NULL, *y_coordinate_str = NULL;
/* create SM2 Ellipse Curve parameters and key pair */
if ( !(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) )
{
goto clean_up;
}
if ( (EVP_PKEY_paramgen_init(pctx)) != 1 )
{
goto clean_up;
}
if ( (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_sm2)) <= 0 )
{
goto clean_up;
}
if ( (EVP_PKEY_keygen_init(pctx)) != 1 )
{
goto clean_up;
}
if ( (EVP_PKEY_keygen(pctx, &pkey)) != 1 )
{
goto clean_up;
}
/* print SM2 key pair */
if ( !(key_pair = EVP_PKEY_get0_EC_KEY(pkey)) )
{
goto clean_up;
}
if ( !(priv_key = EC_KEY_get0_private_key(key_pair)) )
{
goto clean_up;
}
if ( !(priv_key_str = BN_bn2hex(priv_key)) )
{
goto clean_up;
}
printf("SM2 private key (in hex form):\n");
评论0
最新资源