目录
说明
GmSSL地址:https://github.com/guanzhi/GmSSL
GmSSL是由北京大学自主开发的国产商用密码开源库,实现了对国密算法、标准和安全通信协议的全面功能覆盖,支持包括移动端在内的主流操作系统和处理器,支持密码钥匙、密码卡等典型国产密码硬件,提供功能丰富的命令行工具及多种编译语言编程接口。
主要特性
- 超轻量:GmSSL 3 大幅度降低了内存需求和二进制代码体积,不依赖动态内存,可以用于无操作系统的低功耗嵌入式环境(MCU、SOC等),开发者也可以更容易地将国密算法和SSL协议嵌入到现有的项目中。
- 更合规:GmSSL 3 可以配置为仅包含国密算法和国密协议(TLCP协议),依赖GmSSL 的密码应用更容易满足密码产品型号检测的要求,避免由于混杂非国密算法、不安全算法等导致的安全问题和合规问题。
- 更安全:TLS 1.3在安全性和通信延迟上相对之前的TLS协议有巨大的提升,GmSSL 3 支持TLS 1.3协议和RFC 8998的国密套件。GmSSL 3 默认支持密钥的加密保护,提升了密码算法的抗侧信道攻击能力。
- 跨平台:GmSSL 3 更容易跨平台,构建系统不再依赖Perl,默认的CMake构建系统可以容易地和Visual Studio、Android NDK等默认编译工具配合使用,开发者也可以手工编写Makefile在特殊环境中编译、剪裁。
源码编译
cmake下载地址:Download CMake
cmake生成解决方案
生成后如下图:
使用VS2020打开,选择gmssl项目->右键->生成
gmssl属性页
效果
项目
附加包含目录
附加库目录
附加依赖项
代码
- #define _CRT_SECURE_NO_DEPRECATE
-
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <gmssl/asn1.h>
- #include <gmssl/rand.h>
- #include <gmssl/error.h>
- #include <gmssl/sm2.h>
- #include <gmssl/sm2_z256.h>
- #include <gmssl/pkcs8.h>
- #include <fstream>
- #include <sstream>
- #include <vector>
-
- const std::vector<uint8_t> hexStrToByteVector(const std::string& hexStr)
- {
- std::vector<uint8_t> byte_vector;
- byte_vector.reserve(hexStr.size() / 2);
- for (std::string::const_iterator it = hexStr.begin(); it != hexStr.end(); it += 2)
- {
- byte_vector.push_back(
- static_cast<char>(strtol(std::string(it, it + 2).c_str(), nullptr, 16)));
- }
- return byte_vector;
- }
-
- int main(void)
- {
- //std::ifstream file("puk.pem"); //
- //std::string line;
- //if (file.is_open()) {
- // while (getline(file, line)) {
- // std::cout << line << std::endl;
- // }
- // file.close();
- //} else {
- // std::cout << "无法打开文件" << std::endl;
- //}
-
- FILE* publicKeyFile = fopen("puk.pem", "r");
- SM2_KEY sm2_key;
- int ret = 0;
- if (sm2_public_key_info_from_pem(&sm2_key, publicKeyFile) != 1)
- {
- error_print();
- std::cout << "sm2_public_key_info_from_pem失败" << std::endl;
- fclose(publicKeyFile);
- return -1;
- }
- fclose(publicKeyFile);
-
- sm2_key_print(stderr, 0, 0, "SM2_KEY", &sm2_key);
- std::cout << std::endl;
-
- //std::string message="lxw123abc!@#$%^&*()天天代码码天天SM验签测试";
- std::string messageHex = "6C787731323361626321402324255E262A2829E5A4A9E5A4A9E4BBA3E7A081E7A081E5A4A9E5A4A9534DE9AA8CE7ADBEE6B58BE8AF95";
- std::vector<uint8_t> message = hexStrToByteVector(messageHex);
-
- std::cout << "messageHex:" << std::endl << messageHex<< std::endl<<std::endl;
-
- std::string signatureHex = "3044022025c74f0d8d0bf00f26f30a7e330425479967110a1b816d42189c9197515e832d022054284f43b00d76a30d2aaf70499728e081bc0053638349bfe763da4210809b63";
- std::vector<uint8_t> signature = hexStrToByteVector(signatureHex);
-
- std::cout << "signatureHex:" << std::endl << signatureHex<< std::endl << std::endl;
-
- SM2_SIGNATURE sig;
- const uint8_t* cp = signature.data();
- size_t len = signature.size();
-
- if (sm2_signature_from_der(&sig, &cp, &len) != 1
- || asn1_length_is_zero(len) != 1) {
- error_print();
- std::cout << "sm2_signature_from_der失败" << std::endl;
- return -1;
- }
-
- SM2_SIGN_CTX sign_ctx;
- SM2_VERIFY_CTX vrfy_ctx;
-
- /* int ret=sm2_verify_init(&vrfy_ctx, &sm2_key, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH);
- ret = sm2_verify_update(&vrfy_ctx, message.data(), message.size());
- ret = sm2_verify_finish(&vrfy_ctx, signature.data(), signature.size());*/
-
- if (sm2_verify_init(&vrfy_ctx, &sm2_key, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1
- || sm2_verify_update(&vrfy_ctx, message.data(), message.size()) != 1
- || (ret = sm2_verify_finish(&vrfy_ctx, signature.data(), signature.size())) != 1) {
- std::cout << "验签失败" << std::endl;
- error_print();
- return -1;
- }
- else {
- std::cout << "验签通过" << std::endl;
- }
-
- getchar();
-
- return 0;
- }
puk.pem
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEpcOpKYODXlH4slAdbjwX3h10cqi1
4rJPAcXl+kPyl4JyuiiJjnh5nr4IQrIJ5tMmctx2slfuSkT/diodLIx+GQ==
-----END PUBLIC KEY-----
C# SM2 加签、验签工具:http://iyenn.com/rec/1658238.html
下载
天天代码码天天
微信公众号
.NET 人工智能实践


评论记录:
回复评论: