OpenSSL sm2 签名源码讲解

签名原理https://blog.csdn.net/Asia_ZhangQQ/article/details/96854514这个文章有详细说明,这里就不再说了,直接上源码

1.格式转换,OpenSSL EVP签名用到公私钥格式EVP_PKEY,先转成对应格式.如果是用EVP直接生成公私钥那就直接用EVP_PKEY 忽略掉这一步.

EVP_PKEY *pKey = EVP_PKEY_new();

 sm2CreateEVP_PKEY(privateKey, 0, &pKey);

具体string 转EVP_PKEY,或者EC_KEY转EVP_PKEY后期再写个文章

2.EVP_PKEY_set_alias_type(pKey, EVP_PKEY_SM2);

设置公私钥类型为sm2曲线类型

The B structure will default to using ECDSA for signatures when it is

created. It should be set to B by calling:

3.签名初始化

EVP_MD_CTX *mctx = EVP_MD_CTX_new();

EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pKey, NULL);

When calling the EVP_DigestSignInit() or EVP_DigestVerifyInit() functions, a

pre-allocated B should be assigned to the B. This is

done by calling:

EVP_MD_CTX_set_pkey_ctx(mctx, pctx);

设置signId,默认1234567812345678(java bc)可

你可能感兴趣的:(sm2,sm3,sm4,sm9,zuc,等加解密带源码,ios,android,java)