//
// GSM9.c
// DomesticCryptographyAlgorithm
//
// Created by zhangyazhou on 2018/12/24.
// Copyright © 2018 zhangyazhou. All rights reserved.
//
#include "GSM9.h"
//#include <stdlib.h>
#include <string.h>
//#include <stdio.h>
//#include "./e_os.h"
//# include <openssl/evp.h>
#include <openssl/err.h>
int sm9_test_verify(const char *id, unsigned char *sig, size_t siglen, const unsigned char *msg, size_t msglen, SM9PublicParameters *mpk){
int ret = 0;
ret = SM9_verify(NID_sm3, msg, msglen, sig, siglen, mpk, id, strlen(id));
if (ret < 0) {
ERR_print_errors_fp(stderr);
goto end;
}
end:
SM9PublicParameters_free(mpk);
return ret;
}
int sm9_test_sign(const char *id, const unsigned char *msg, size_t msglen)
{
int ret = 0;
SM9PublicParameters *mpk = NULL;
SM9MasterSecret *msk = NULL;
SM9PrivateKey *sk = NULL;
unsigned char sig[256];
size_t siglen = sizeof(sig);
if (!SM9_setup(NID_sm9bn256v1, NID_sm9sign, NID_sm9hash1_with_sm3, &mpk, &msk)) {
ERR_print_errors_fp(stderr);
goto end;
}
if (!(sk = SM9_extract_private_key(msk, id, strlen(id)))) {
ERR_print_errors_fp(stderr);
goto end;
}
if (!SM9_sign(NID_sm3, msg, msglen, sig, &siglen, sk)) {
ERR_print_errors_fp(stderr);
goto end;
}
// ret = SM9_verify(NID_sm3, msg, msglen, sig, siglen, mpk, id, strlen(id));
// if (ret < 0) {
// ERR_print_errors_fp(stderr);
// goto end;
// }
ret = sm9_test_verify(id, sig, siglen, msg, msglen,mpk);
if (ret < 0) {
ERR_print_errors_fp(stderr);
goto end;
}
end:
SM9MasterSecret_free(msk);
SM9PrivateKey_free(sk);
return ret;
}
int sm9_test_wrap(const char *id)
{
int ret = 0;
SM9PublicParameters *mpk = NULL;
SM9MasterSecret *msk = NULL;
SM9PrivateKey *sk = NULL;
unsigned char key[56] = {0};
unsigned char key2[56] = {0};
unsigned char C[65];
size_t Clen;
if (!SM9_setup(NID_sm9bn256v1, NID_sm9encrypt, NID_sm9hash1_with_sm3, &mpk, &msk)) {
goto end;
}
if (!(sk = SM9_extract_private_key(msk, id, strlen(id)))) {
goto end;
}
if (!SM9_wrap_key(NID_sm9kdf_with_sm3, key, sizeof(key), C, &Clen, mpk, id, strlen(id))) {
goto end;
}
if (!SM9_unwrap_key(NID_sm9kdf_with_sm3, key2, sizeof(key2), C, sizeof(C), sk)) {
goto end;
}
if (memcmp(key, key2, sizeof(key2)) != 0) {
goto end;
}
ret = 1;
end:
SM9PublicParameters_free(mpk);
SM9MasterSecret_free(msk);
SM9PrivateKey_free(sk);
return ret;
}
int sm9_test_exch(const char *idA, const char *idB)
{
int ret = 0;
SM9PublicParameters *mpk = NULL;
SM9MasterSecret *msk = NULL;
SM9PrivateKey *skA = NULL;
SM9PrivateKey *skB = NULL;
BIGNUM *rA = BN_new();
BIGNUM *rB = BN_new();
unsigned char RA[65];
unsigned char RB[65];
unsigned char gA[384];
unsigned char gB[384];
size_t RAlen = sizeof(RA);
size_t RBlen = sizeof(RB);
size_t gAlen = sizeof(gA);
size_t gBlen = sizeof(gB);
int type = NID_sm9kdf_with_sm3;
unsigned char SKA[16];
unsigned char SKB[16];
unsigned char SA[32];
unsigned char SB[32];
unsigned char S2[32];
if (!SM9_setup(NID_sm9bn256v1, NID_sm9keyagreement, NID_sm9hash1_with_sm3, &mpk, &msk)
|| !(skA = SM9_extract_private_key(msk, idA, strlen(idA)))
|| !(skB = SM9_extract_private_key(msk, idB, strlen(idB)))) {
ERR_print_errors_fp(stderr);
goto end;
}
if (!SM9_generate_key_exchange(RA, &RAlen, rA, gA, &gAlen, idB, strlen(idB), skA, 1)
|| !SM9_generate_key_exchange(RB, &RBlen, rB, gB, &gBlen, idA, strlen(idA), skB, 0)
|| !SM9_compute_share_key_B(type, SKB, sizeof(SKB), SB, S2, rB, RB, RA, gB, idA, strlen(idA), skB)
|| !SM9_compute_share_key_A(type, SKA, sizeof(SKA), SA, SB, rA, RA, RB, gA, idB, strlen(idB), skA)) {
ERR_print_errors_fp(stderr);
goto end;
}
if (memcmp(SKA, SKA, sizeof(SKA)) != 0 || memcmp(SA, S2, sizeof(SA)) != 0) {
goto end;
}
ret = 1;
end:
BN_free(rA);
BN_free(rB);
return ret;
}
unsigned char * sm9_test_dec(const unsigned char *cbuf, size_t clen, SM9PrivateKey *sk)
{
unsigned char *out_buf = NULL;
unsigned char mbuf[1024] = {0};
size_t mlen;
if (!SM9_decrypt(NID_sm9encrypt_with_sm3_xor, cbuf, clen,
mbuf, &mlen, sk)) {
ERR_print_errors_fp(stderr);
goto end;
}
printf("%s\n",mbuf);
out_buf = mbuf;
end:
SM9PrivateKey_free(sk);
return out_buf;
}
unsigned char * sm9_test_enc(const char *id, const unsigned char *data, size_t datalen)
{
unsigned char *out_buf = NULL;
SM9PublicParameters *mpk = NULL;//加密主公钥
SM9MasterSecret *msk = NULL;// 用户b的加密私钥
SM9PrivateKey *sk = NULL; //私钥
unsigned char *mbuf = NULL;
unsigned char cbuf[1024] = {0}; //存放加密的密文
size_t clen, mlen = 0;
if (!SM9_setup(NID_sm9bn256v1, NID_sm9encrypt, NID_sm9hash1_with_sm3, &mpk, &msk)) {
// NID_sm9hash1_with_sm3 随机数加密主私钥
ERR_print_errors_fp(stderr);
goto end;
}
if (!(sk = SM9_extract_private_key(msk, id, strlen(id)))) { //KGC 用加密主私钥和用户的标识生成用户的加密私钥
ERR_print_errors_fp(stderr);
goto end;
}
if (!SM9_encrypt(NID_sm9encrypt_with_sm3_xor, data, datalen,
cbuf, &clen, mpk, id, strlen(id))) {//用户主公钥加密
ERR_print_errors_fp(stderr);
goto end;
}
printf("%s\n",cbuf);
// if (!SM9_decrypt(NID_sm9encrypt_with_sm3_xor, cbuf, clen,
// mbuf, &mlen, sk)) {
// ERR_print_errors_fp(stderr);
// goto end;
// }
//
// if (mlen != datalen || memcmp(mbuf, data, datalen) != 0) {
// goto end;
// }
mbuf = sm9_test_dec(cbuf, clen, sk);// 用户主私钥解密
printf("%s\n",mbuf);
out_buf = mbuf;
end:
SM9PublicParameters_free(mpk);
SM9MasterSecret_free(msk);
// SM9PrivateKey_free(sk);
return out_buf;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于GMSSL的SM9签名,密钥交换,加密,解密 (218个子文件)
libcrypto.a 4.45MB
libssl.a 602KB
GSM9.c 6KB
128_EIA3.c 2KB
128_EEA3.c 1KB
build-public.cer 798B
.DS_Store 10KB
.DS_Store 8KB
.DS_Store 8KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
obj_mac.h 213KB
ssl.h 127KB
evp.h 80KB
ec.h 73KB
tls1.h 51KB
x509.h 48KB
asn1.h 45KB
objects.h 44KB
engine.h 39KB
x509v3.h 37KB
bio.h 37KB
asn1t.h 31KB
ec_lcl.h 30KB
rsa.h 28KB
x509_vfy.h 27KB
ts.h 27KB
cms.h 26KB
bn.h 26KB
pem.h 22KB
saf.h 21KB
ct.h 19KB
ocsp.h 18KB
crypto.h 18KB
skf.h 16KB
ui.h 16KB
skf_int.h 16KB
pkcs7.h 16KB
sm9.h 15KB
e_os.h 15KB
gmapi.h 15KB
sdf_int.h 14KB
sdf.h 14KB
gmskf.h 14KB
gmsdf.h 14KB
dh.h 14KB
sgd.h 13KB
err.h 13KB
pkcs12.h 13KB
ssl3.h 13KB
dsa.h 11KB
sm2.h 11KB
gmtls.h 11KB
dso.h 11KB
modes.h 10KB
e_os2.h 9KB
gmsaf.h 9KB
conf.h 8KB
lhash.h 8KB
des.h 7KB
sof.h 7KB
cpk.h 6KB
gmsof.h 6KB
safestack.h 6KB
constant_time_locl.h 6KB
ossl_typ.h 6KB
bb1ibe.h 6KB
bfibe.h 6KB
paillier.h 6KB
ecies.h 6KB
sms4.h 5KB
serpent.h 5KB
zuc.h 4KB
bn_gfp2.h 4KB
ffx.h 4KB
opensslv.h 4KB
opensslconf.h 4KB
speck.h 4KB
sha.h 4KB
otp.h 4KB
kdf.h 4KB
sm3.h 4KB
srp.h 4KB
ec_type1.h 4KB
dane.h 3KB
seed.h 3KB
aes.h 3KB
async.h 3KB
kdf2.h 3KB
sm2_lcl.h 3KB
camellia.h 3KB
base58.h 3KB
bn_solinas.h 3KB
fppoint.h 3KB
byteorder.h 3KB
stack.h 3KB
bn_hash.h 3KB
ec_hash.h 3KB
共 218 条
- 1
- 2
- 3
资源评论
- MoeRay2019-11-15是苹果机的代码,非C/C++
- highmountain2019-05-05没说明文档!如何应用?Xcode打开?Asia_ZhangQQ2019-05-06都已经demo给到这种地步了,还需要教你用什么编译器打开?怎么用?XCode运行下都不会吗
- seam322019-02-25感谢分享~~~
Asia_ZhangQQ
- 粉丝: 105
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 2018年对口升学信息一类第一次联考试题(卷).doc
- 见证时代变迁的高考作文题.doc
- 江苏兴化一中2018年高考第四次模拟考试语文试题整理.doc
- 建筑试题3[识图答案].doc
- 静载检测技术基础理论考试(员)C.doc
- 跨境电商人才初级认证试题以与答案.doc
- 临床的护理文书规范模拟考试标准答案09_8_7.doc
- 垃圾分类知识问答.doc
- 六年级英语上[下册]知识点总结.doc
- 六年级写字教学案.doc
- 农业区位因素教学设计.doc
- 牛津译林版2018年_2018年学年8A英语期末专题练习_首字母填空.doc
- 人版小学数学六年级(下册)第3单元圆柱与圆锥教案.doc
- 巧用多媒体有效地复习有丝分裂和减数分裂考点.doc
- 人民教育出版社五年级语文(下册)易错字易错音.doc
- 山东省医学继续教育公共课程急诊及急救答案.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功