不使用密码学库函数,用C++简单实现SM2算法
时间: 2024-02-11 20:05:34 浏览: 164
使用C++语言实现的SM2国密算法,运用tommath库来进行公私钥的计算
SM2算法是一种国密算法,它在椭圆曲线密码学的基础上实现了数字签名、密钥交换和加密等功能。下面是一份简单的C++代码实现SM2算法的签名部分。
```c++
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
const int SM2_WORD_SIZE = 32; // 定义字节长度为32
// 定义椭圆曲线参数
const string SM2_ELLIPTIC_CURVE = "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF";
const string SM2_A = "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC";
const string SM2_B = "28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93";
const string SM2_N = "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123";
const string SM2_GX = "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE171F9B408F61B2F6";
const string SM2_GY = "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A";
// 定义点结构体
struct SM2_POINT {
string x;
string y;
};
// 定义SM2密钥对结构体
struct SM2_KEYPAIR {
string privateKey;
SM2_POINT publicKey;
};
// 定义SM2签名结构体
struct SM2_SIGNATURE {
string r;
string s;
};
// 生成随机数
string random(int len) {
string res = "";
for (int i = 1; i <= len; i++) {
int x = rand() % 256;
char c = (char)x;
res += c;
}
return res;
}
// 转换16进制字符串为10进制字符串
string hex2dec(string hex) {
string res = "";
for (int i = 0; i < hex.length(); i++) {
char c = hex[i];
if (c >= '0' && c <= '9') {
res += c;
} else if (c >= 'A' && c <= 'F') {
int x = c - 'A' + 10;
res += to_string(x);
} else if (c >= 'a' && c <= 'f') {
int x = c - 'a' + 10;
res += to_string(x);
}
}
return res;
}
// 转换10进制字符串为16进制字符串
string dec2hex(string dec) {
string res = "";
for (int i = 0; i < dec.length(); i += 2) {
int x = dec[i] - '0';
int y = dec[i + 1] - '0';
int z = x * 10 + y;
char c;
if (z >= 0 && z <= 9) {
c = z + '0';
} else {
c = z - 10 + 'A';
}
res += c;
}
return res;
}
// 模运算
string mod(string a, string b) {
int len1 = a.length(), len2 = b.length();
if (len1 < len2 || (len1 == len2 && a < b)) {
return a;
}
string res = "";
int carry = 0;
for (int i = 0; i < len1; i++) {
int x = a[i] - '0';
int y = i < len2 ? b[i] - '0' : 0;
int z = x - carry - y;
if (z < 0) {
z += 10;
carry = 1;
} else {
carry = 0;
}
res += z + '0';
}
while (res.length() > 1 && res[res.length() - 1] == '0') {
res.erase(res.length() - 1);
}
reverse(res.begin(), res.end());
return res;
}
// 快速幂算法
string pow(string a, string b, string m) {
string res = "1";
while (b != "0") {
if ((b[b.length() - 1] - '0') % 2 == 1) {
res = mod(res * a, m);
}
a = mod(a * a, m);
b = b.substr(0, b.length() - 1);
}
return res;
}
// 计算y^2 mod p
string modSqrt(string y, string p) {
string res = pow(y, (p - "1") / 2, p);
return res == "1" ? "0" : "1";
}
// 求逆元
string inverse(string a, string p) {
string t = "0", newt = "1", r = p, newr = a;
while (newr != "0") {
string q = mod(r, newr);
string temp = t;
t = newt;
newt = temp - q * newt;
temp = r;
r = newr;
newr = temp - q * newr;
}
if (r != "1") {
return "";
}
if (t < "0") {
t += p;
}
return t;
}
// 生成SM2密钥对
SM2_KEYPAIR generateKeyPair() {
srand(time(NULL));
SM2_KEYPAIR res;
res.privateKey = random(SM2_WORD_SIZE);
string k = hex2dec(res.privateKey);
SM2_POINT G;
G.x = hex2dec(SM2_GX);
G.y = hex2dec(SM2_GY);
SM2_POINT P = G;
for (int i = 1; i < k.length(); i++) {
P = add(P, G);
}
res.publicKey.x = dec2hex(P.x);
res.publicKey.y = dec2hex(P.y);
return res;
}
// 计算椭圆曲线上两点之和
SM2_POINT add(SM2_POINT P, SM2_POINT Q) {
SM2_POINT res;
string x1 = hex2dec(P.x);
string y1 = hex2dec(P.y);
string x2 = hex2dec(Q.x);
string y2 = hex2dec(Q.y);
string a = hex2dec(SM2_A);
string p = hex2dec(SM2_ELLIPTIC_CURVE);
if (P.x == Q.x && P.y == Q.y) { // P = Q
string t1 = mod(a * x1 * x1, p);
string t2 = mod(3 * x1 * x1 + a, p);
string t3 = mod(inverse(2 * y1, p) * t2, p);
string t4 = mod(t3 * t3 - 2 * x1, p);
string t5 = mod(x1 - t4, p);
string t6 = mod(t3 * (x1 - t4) - y1, p);
res.x = dec2hex(t5);
res.y = dec2hex(t6);
} else if (P.x == Q.x && P.y != Q.y) { // P = -Q
res.x = "0";
res.y = "0";
} else { // P != Q
string t1 = mod(y2 - y1, p);
string t2 = mod(x2 - x1, p);
string t3 = mod(inverse(t2, p) * t1, p);
string t4 = mod(t3 * t3 - x1 - x2, p);
string t5 = mod(x1 - t4, p);
string t6 = mod(t3 * (x1 - t4) - y1, p);
res.x = dec2hex(t5);
res.y = dec2hex(t6);
}
return res;
}
// SM2签名
SM2_SIGNATURE sm2Sign(string privateKey, string message) {
string k = random(SM2_WORD_SIZE);
string d = hex2dec(privateKey);
SM2_POINT G;
G.x = hex2dec(SM2_GX);
G.y = hex2dec(SM2_GY);
SM2_POINT P = G;
for (int i = 1; i < d.length(); i++) {
P = add(P, G);
}
string n = SM2_N;
string e = hex2dec(message);
string r, s;
while (true) {
SM2_POINT C1;
C1.x = dec2hex(P.x);
C1.y = dec2hex(P.y);
string t = mod(e + C1.x, n);
if (t == "0" || t == "1") {
continue;
}
string h = modSqrt(t, n);
if (h == "1") {
continue;
}
string x = mod(k, n);
SM2_POINT C2 = add(mul(G, x), mul(P, hex2dec(k)));
string r = mod(mod(e + C1.x, n) + mod(C2.y, n), n);
if (r == "0" || r + hex2dec(k) == n) {
continue;
}
s = mod(inverse(1 + hex2dec(privateKey) + hex2dec(k), n) * (hex2dec(k) - r * hex2dec(privateKey)), n);
if (s == "0") {
continue;
}
break;
}
SM2_SIGNATURE res;
res.r = dec2hex(r);
res.s = dec2hex(s);
return res;
}
int main() {
SM2_KEYPAIR keyPair = generateKeyPair();
string message = "Hello, SM2!";
SM2_SIGNATURE signature = sm2Sign(keyPair.privateKey, message);
cout << "r: " << signature.r << endl;
cout << "s: " << signature.s << endl;
return 0;
}
```
以上代码仅实现了SM2算法的签名部分,如果需要实现加密和密钥交换功能,还需要进一步完善代码。此外,由于SM2算法涉及到大量的高精度计算和模运算,因此在实际使用中需要注意代码的效率和精度。
阅读全文