-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrypto.cpp
More file actions
191 lines (148 loc) · 5.03 KB
/
crypto.cpp
File metadata and controls
191 lines (148 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// Created by KevinZhao on 2019/9/24.
//
#include "base_64.h"
#include <iostream>
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/ec.h>
#include <openssl/pem.h>
#include <openssl/sgd.h>
#include <openssl/zuc.h>
#include <openssl/gmapi.h>
#include <openssl/bio.h>
using namespace std;
class Crypto {
public:
EVP_MD_CTX *mdctx;
EVP_CIPHER_CTX *strong_en, *weak_en;
EC_KEY *Key;
Crypto() {
/* Load the human readable error strings for libcrypto */
ERR_load_crypto_strings();
/* Load all digest and cipher algorithms */
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
}
~Crypto() {
/* Removes all digests and ciphers */
EVP_cleanup();
CRYPTO_cleanup_all_ex_data();
}
pair<string, string> GenKey() {
EC_KEY *keypair = NULL;
EC_GROUP *group1 = NULL;
keypair = EC_KEY_new();
if (!keypair) {
cout << "Failed to Gen Key" << endl;
exit(1);
}
group1 = EC_GROUP_new_by_curve_name(NID_sm2p256v1);//NID_sm2p256v1
if (group1 == NULL) {
cout << "Failed to Gen Key" << endl;
exit(1);
}
int ret1 = EC_KEY_set_group(keypair, group1);
if (ret1 != 1) {
cout << "Failed to Gen Key" << endl;
exit(1);
}
int ret2 = EC_KEY_generate_key(keypair);
if (ret2 != 1) {
cout << "Failed to Gen Key" << endl;
exit(1);
}
size_t pri_len;
size_t pub_len;
char *pri_key = NULL;
char *pub_key = NULL;
BIO *pri = BIO_new(BIO_s_mem());
BIO *pub = BIO_new(BIO_s_mem());
PEM_write_bio_ECPrivateKey(pri, keypair, NULL, NULL, 0, NULL, NULL);
PEM_write_bio_EC_PUBKEY(pub, keypair);
pri_len = BIO_pending(pri);
pub_len = BIO_pending(pub);
pri_key = new char[pri_len + 1];
pub_key = new char[pub_len + 1];
BIO_read(pri, pri_key, pri_len);
BIO_read(pub, pub_key, pub_len);
pri_key[pri_len] = '\0';
pub_key[pub_len] = '\0';
string public_key = pub_key;
string private_key = pri_key;
EC_KEY_free(keypair);
BIO_free_all(pub);
BIO_free_all(pri);
delete[] pri_key;
delete[] pub_key;
return std::pair<string, string>(public_key, private_key);
}
EC_KEY *CreateEC(unsigned char *key, int is_public) {
EC_KEY *ec_key = nullptr;
BIO *keybio = nullptr;
// cout << key << endl;
keybio = BIO_new_mem_buf(key, -1);
if (keybio == nullptr) {
cout << "Failed to Get Key" << endl;
exit(1);
}
if (is_public) {
ec_key = PEM_read_bio_EC_PUBKEY(keybio, nullptr, nullptr, nullptr);
} else {
ec_key = PEM_read_bio_ECPrivateKey(keybio, nullptr, nullptr, nullptr);
}
if (ec_key == nullptr) {
cout << "Failed to Get Key" << endl;
printError();
exit(1);
}
return ec_key;
}
void getHashBySM3(unsigned char *msgbuf, unsigned char *dgst, int len) {
EVP_MD_CTX *mdctx = nullptr;
unsigned int dgstlen = EVP_MAX_MD_SIZE;
mdctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(mdctx, EVP_sm3(), nullptr);
EVP_DigestUpdate(mdctx, msgbuf, len);
EVP_DigestFinal_ex(mdctx, dgst, &dgstlen);
EVP_MD_CTX_free(mdctx);
}
void initSM2() {
this->mdctx = EVP_MD_CTX_create();
EVP_SignInit_ex(mdctx, EVP_sm3(), nullptr);
}
void UpdateSignBySM2(int *msg, int len) {
EVP_DigestUpdate(mdctx, msg, len);
}
unsigned int finishSigh(unsigned char *sig, const string &private_key) {
unsigned int slen = 0;
this->Key = CreateEC((unsigned char *) private_key.c_str(), 0);
EVP_PKEY *evpkey = EVP_PKEY_new();
EVP_PKEY_set1_EC_KEY(evpkey, this->Key);
EVP_SignFinal(this->mdctx, sig, &slen, evpkey);
// cout << "slen: " << slen << endl;
EVP_MD_CTX_destroy(mdctx);
return slen;
}
int finishVerify(unsigned char *sig, int size, const string &public_key) {
int res = 0;
this->Key = CreateEC((unsigned char *) public_key.c_str(), 1);
EVP_PKEY *evpkey = EVP_PKEY_new();
EVP_PKEY_set1_EC_KEY(evpkey, this->Key);
res = EVP_VerifyFinal(this->mdctx, sig, size, evpkey);
return res;
}
void initZUC(unsigned char *strong_key, unsigned char *weak_key, unsigned char *iv) {
// const unsigned char *strong_iv = (unsigned char *) "strong_iv", *weak_iv = (unsigned char *) "weak_iv";
this->strong_en = EVP_CIPHER_CTX_new();
this->weak_en = EVP_CIPHER_CTX_new();
EVP_EncryptInit(strong_en, EVP_zuc(), strong_key, iv);
EVP_EncryptInit(weak_en, EVP_zuc(), weak_key, iv);
}
void printError() {
char errBuf[512] = {0};
ERR_error_string_n(ERR_get_error(), errBuf, sizeof(errBuf));
cout << errBuf << endl;
}
};