|
8 | 8 | #include <openssl/ec.h> |
9 | 9 | #include <openssl/bn.h> |
10 | 10 | #include <openssl/crypto.h> |
11 | | -#include <openssl/sm2.h> |
12 | 11 |
|
13 | 12 | #include "dde-sm2.h" |
14 | 13 |
|
@@ -86,6 +85,166 @@ static char* get_private_key(EC_KEY *key) { |
86 | 85 | return ret; |
87 | 86 | } |
88 | 87 |
|
| 88 | +/*openssl sm2 cipher evp using*/ |
| 89 | +static int openssl_evp_sm2_encrypt(EC_KEY *ec_key, |
| 90 | + const unsigned char *plain_text, size_t plain_len, |
| 91 | + unsigned char *cipher_text, size_t *cipher_len) |
| 92 | +{ |
| 93 | + int ret = 0; |
| 94 | + BIO *bp = NULL; |
| 95 | + EVP_PKEY* public_evp_key = NULL; |
| 96 | + EVP_PKEY_CTX *ctx = NULL; |
| 97 | + |
| 98 | + /*Check the user input.*/ |
| 99 | + if (plain_text == NULL || plain_len == 0) { |
| 100 | + ret = -1; |
| 101 | + return ret; |
| 102 | + } |
| 103 | + |
| 104 | + //OpenSSL_add_all_algorithms(); |
| 105 | + bp = BIO_new(BIO_s_mem()); |
| 106 | + if (bp == NULL) { |
| 107 | + printf("BIO_new is failed.\n"); |
| 108 | + ret = -1; |
| 109 | + return ret; |
| 110 | + } |
| 111 | + |
| 112 | + if (ec_key == NULL) { |
| 113 | + ret = -1; |
| 114 | + printf("open_public_key failed to PEM_read_bio_EC_PUBKEY Failed, ret=%d\n", ret); |
| 115 | + goto finish; |
| 116 | + } |
| 117 | + public_evp_key = EVP_PKEY_new(); |
| 118 | + if (public_evp_key == NULL) { |
| 119 | + ret = -1; |
| 120 | + printf("open_public_key EVP_PKEY_new failed\n"); |
| 121 | + goto finish; |
| 122 | + } |
| 123 | + ret = EVP_PKEY_set1_EC_KEY(public_evp_key, ec_key); |
| 124 | + if (ret != 1) { |
| 125 | + ret = -1; |
| 126 | + printf("EVP_PKEY_set1_EC_KEY failed\n"); |
| 127 | + goto finish; |
| 128 | + } |
| 129 | + ret = EVP_PKEY_set_alias_type(public_evp_key, EVP_PKEY_SM2); |
| 130 | + if (ret != 1) { |
| 131 | + printf("EVP_PKEY_set_alias_type to EVP_PKEY_SM2 failed! ret = %d\n", ret); |
| 132 | + ret = -1; |
| 133 | + goto finish; |
| 134 | + } |
| 135 | + /*modifying a EVP_PKEY to use a different set of algorithms than the default.*/ |
| 136 | + |
| 137 | + /*do cipher.*/ |
| 138 | + ctx = EVP_PKEY_CTX_new(public_evp_key, NULL); |
| 139 | + if (ctx == NULL) { |
| 140 | + ret = -1; |
| 141 | + printf("EVP_PKEY_CTX_new failed\n"); |
| 142 | + goto finish; |
| 143 | + } |
| 144 | + ret = EVP_PKEY_encrypt_init(ctx); |
| 145 | + if (ret < 0) { |
| 146 | + printf("sm2_pubkey_encrypt failed to EVP_PKEY_encrypt_init. ret = %d\n", ret); |
| 147 | + goto finish; |
| 148 | + } |
| 149 | + ret = EVP_PKEY_encrypt(ctx, cipher_text, cipher_len, plain_text, plain_len); |
| 150 | + if (ret < 0) { |
| 151 | + printf("sm2_pubkey_encrypt failed to EVP_PKEY_encrypt. ret = %d\n", ret); |
| 152 | + goto finish; |
| 153 | + } |
| 154 | + ret = 0; |
| 155 | + |
| 156 | +finish: |
| 157 | + if (public_evp_key != NULL) |
| 158 | + EVP_PKEY_free(public_evp_key); |
| 159 | + if (ctx != NULL) |
| 160 | + EVP_PKEY_CTX_free(ctx); |
| 161 | + if (bp != NULL) |
| 162 | + BIO_free(bp); |
| 163 | + |
| 164 | + return ret; |
| 165 | +} |
| 166 | + |
| 167 | +/*openssl sm2 decrypt evp using*/ |
| 168 | +static int openssl_evp_sm2_decrypt(EC_KEY* ec_key, |
| 169 | + const unsigned char *cipher_text, size_t cipher_len, |
| 170 | + unsigned char *plain_text, size_t *plain_len) |
| 171 | +{ |
| 172 | + int ret = 0; |
| 173 | + size_t out_len = 0; |
| 174 | + BIO *bp = NULL; |
| 175 | + EVP_PKEY* private_evp_key = NULL; |
| 176 | + EVP_PKEY_CTX *ctx = NULL; |
| 177 | + |
| 178 | + /*Check the user input.*/ |
| 179 | + if (cipher_len == 0 || cipher_text == NULL) { |
| 180 | + ret = -1; |
| 181 | + return ret; |
| 182 | + } |
| 183 | + |
| 184 | + //OpenSSL_add_all_algorithms(); |
| 185 | + bp = BIO_new(BIO_s_mem()); |
| 186 | + if (bp == NULL) { |
| 187 | + printf("BIO_new is failed.\n"); |
| 188 | + ret = -1; |
| 189 | + return ret; |
| 190 | + } |
| 191 | + |
| 192 | + if (ec_key == NULL) { |
| 193 | + ret = -1; |
| 194 | + printf("open_private_key failed to PEM_read_bio_ECPrivateKey Failed, ret=%d\n", ret); |
| 195 | + goto finish; |
| 196 | + } |
| 197 | + private_evp_key = EVP_PKEY_new(); |
| 198 | + if (private_evp_key == NULL) { |
| 199 | + ret = -1; |
| 200 | + printf("open_public_key EVP_PKEY_new failed\n"); |
| 201 | + goto finish; |
| 202 | + } |
| 203 | + ret = EVP_PKEY_set1_EC_KEY(private_evp_key, ec_key); |
| 204 | + if (ret != 1) { |
| 205 | + ret = -1; |
| 206 | + printf("EVP_PKEY_set1_EC_KEY failed\n"); |
| 207 | + goto finish; |
| 208 | + } |
| 209 | + ret = EVP_PKEY_set_alias_type(private_evp_key, EVP_PKEY_SM2); |
| 210 | + if (ret != 1) { |
| 211 | + printf("EVP_PKEY_set_alias_type to EVP_PKEY_SM2 failed! ret = %d\n", ret); |
| 212 | + ret = -1; |
| 213 | + goto finish; |
| 214 | + } |
| 215 | + /*modifying a EVP_PKEY to use a different set of algorithms than the default.*/ |
| 216 | + |
| 217 | + /*do cipher.*/ |
| 218 | + ctx = EVP_PKEY_CTX_new(private_evp_key, NULL); |
| 219 | + if (ctx == NULL) { |
| 220 | + ret = -1; |
| 221 | + printf("EVP_PKEY_CTX_new failed\n"); |
| 222 | + goto finish; |
| 223 | + } |
| 224 | + ret = EVP_PKEY_decrypt_init(ctx); |
| 225 | + if (ret < 0) { |
| 226 | + printf("sm2 private_key decrypt failed to EVP_PKEY_decrypt_init. ret = %d\n", ret); |
| 227 | + goto finish; |
| 228 | + } |
| 229 | + |
| 230 | + ret = EVP_PKEY_decrypt(ctx, plain_text, plain_len, cipher_text, cipher_len); |
| 231 | + if (ret < 0) { |
| 232 | + printf("sm2_prikey_decrypt failed to EVP_PKEY_decrypt. ret = %d\n", ret); |
| 233 | + goto finish; |
| 234 | + } |
| 235 | + ret = 0; |
| 236 | +finish: |
| 237 | + if (private_evp_key != NULL) |
| 238 | + EVP_PKEY_free(private_evp_key); |
| 239 | + if (ctx != NULL) |
| 240 | + EVP_PKEY_CTX_free(ctx); |
| 241 | + if (bp != NULL) |
| 242 | + BIO_free(bp); |
| 243 | + |
| 244 | + return ret; |
| 245 | +} |
| 246 | + |
| 247 | + |
89 | 248 | sm2_context* new_sm2_context() { |
90 | 249 | EC_KEY* key = gen_ec_key(); |
91 | 250 | if (key == NULL) { |
@@ -125,34 +284,34 @@ const char* get_sm2_private_key(sm2_context* context) { |
125 | 284 | return context->private_key; |
126 | 285 | } |
127 | 286 |
|
128 | | -int get_ciphertext_size(const sm2_context *context, size_t plen) { |
| 287 | +int get_ciphertext_size(const sm2_context *context, const uint8_t *ptext, size_t plen) { |
129 | 288 | size_t ret = 0; |
130 | | - if (1 == sm2_ciphertext_size(context->key, EVP_sm3(), plen, &ret)) { |
| 289 | + if (0 == openssl_evp_sm2_encrypt(context->key, ptext, plen, NULL, &ret)) { |
131 | 290 | return (int)ret; |
132 | 291 | } |
133 | 292 |
|
134 | 293 | return -1; |
135 | 294 | } |
136 | 295 |
|
137 | | -int get_plaintext_size(const uint8_t *ctext, size_t clen) { |
| 296 | +int get_plaintext_size(const sm2_context *context, const uint8_t *ctext, size_t clen) { |
138 | 297 | size_t ret = 0; |
139 | | - if (1 == sm2_plaintext_size(ctext, clen, &ret)) { |
| 298 | + if (0 == openssl_evp_sm2_decrypt(context->key, ctext, clen, NULL, &ret)) { |
140 | 299 | return (int)ret; |
141 | 300 | } |
142 | 301 |
|
143 | 302 | return -1; |
144 | 303 | } |
145 | 304 |
|
146 | 305 | int encrypt(const sm2_context* context, const uint8_t *ptext, size_t psize, uint8_t *ctext, size_t csize) { |
147 | | - if (1 == sm2_encrypt(context->key, EVP_sm3(), ptext, psize, ctext, &csize)) { |
| 306 | + if (0 == openssl_evp_sm2_encrypt(context->key, ptext, psize, ctext, &csize)) { |
148 | 307 | return (int)csize; |
149 | 308 | } |
150 | 309 |
|
151 | 310 | return -1; |
152 | 311 | } |
153 | 312 |
|
154 | 313 | int decrypt(const sm2_context* context, const uint8_t *ctext, size_t clen, uint8_t *ptext, size_t psize) { |
155 | | - if (1 == sm2_decrypt(context->key, EVP_sm3(), ctext, clen, ptext, &psize)) { |
| 314 | + if (0 == openssl_evp_sm2_decrypt(context->key, ctext, clen, ptext, &psize)) { |
156 | 315 | return (int)psize; |
157 | 316 | } |
158 | 317 |
|
|
0 commit comments