mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-07 00:46:17 +08:00
add SM2 default ID, fixed the SM2_do_encrypt() memory leak
This commit is contained in:
@@ -670,13 +670,16 @@ static int pkey_sm2_ctrl_digestinit(EVP_PKEY_CTX *pk_ctx, EVP_MD_CTX *md_ctx)
|
|||||||
|
|
||||||
fprintf(stderr, "%s() called\n", __FUNCTION__);
|
fprintf(stderr, "%s() called\n", __FUNCTION__);
|
||||||
|
|
||||||
|
/*
|
||||||
if (!(id = SM2_get_id(ec_key))) {
|
if (!(id = SM2_get_id(ec_key))) {
|
||||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||||
id = "alice@pku.edu.cn";
|
id = "alice@pku.edu.cn";
|
||||||
//return 0;
|
//return 0;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
//FIXME: check this function
|
//FIXME: check this function
|
||||||
if (!SM2_compute_id_digest(zid, &zidlen, md, id, strlen(id), ec_key)) {
|
if (!SM2_compute_id_digest(zid, &zidlen, md, ec_key)) {
|
||||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,92 +0,0 @@
|
|||||||
|
|
||||||
/* crypto/evp/e_sms4.c */
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "../cryptlib.h"
|
|
||||||
|
|
||||||
#ifndef OPENSSL_NO_SMS4
|
|
||||||
#include <openssl/evp.h>
|
|
||||||
#include <openssl/objects.h>
|
|
||||||
#include "evp_locl.h"
|
|
||||||
#include <openssl/sms4.h>
|
|
||||||
#define SMS4_IV_LENGTH SMS4_BLOCK_SIZE
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
sms4_key_t ks;
|
|
||||||
} EVP_SMS4_KEY;
|
|
||||||
|
|
||||||
static int sms4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
|
||||||
const unsigned char *iv, int enc)
|
|
||||||
{
|
|
||||||
if (!enc) {
|
|
||||||
if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_OFB_MODE)
|
|
||||||
enc = 1;
|
|
||||||
else if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CFB_MODE)
|
|
||||||
enc = 1; //encrypt key == decrypt key
|
|
||||||
}
|
|
||||||
|
|
||||||
if (enc)
|
|
||||||
sms4_set_encrypt_key(ctx->cipher_data, key);
|
|
||||||
else sms4_set_decrypt_key(ctx->cipher_data, key);
|
|
||||||
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
static int
|
|
||||||
sms4_cbc_cipher(EVP_CIPHER_CTX * ctx, unsigned char *out, const unsigned char *in, size_t inl)
|
|
||||||
{
|
|
||||||
while (inl >= ((size_t) 1 << (sizeof(long) * 8 - 2))) {
|
|
||||||
sms4_cbc_encrypt(in, out, (long)((size_t) 1 << (sizeof(long) * 8 - 2)), &((EVP_SMS4_KEY *) ctx->cipher_data)->ks, ctx->iv, ctx->encrypt);
|
|
||||||
inl -= ((size_t) 1 << (sizeof(long) * 8 - 2));
|
|
||||||
in += ((size_t) 1 << (sizeof(long) * 8 - 2));
|
|
||||||
out += ((size_t) 1 << (sizeof(long) * 8 - 2));
|
|
||||||
} if (inl)
|
|
||||||
sms4_cbc_encrypt(in, out, (long)inl, &((EVP_SMS4_KEY *) ctx->cipher_data)->ks, ctx->iv, ctx->encrypt);
|
|
||||||
return 1;
|
|
||||||
} static int sms4_cfb128_cipher(EVP_CIPHER_CTX * ctx, unsigned char *out, const unsigned char *in, size_t inl){
|
|
||||||
size_t chunk = ((size_t) 1 << (sizeof(long) * 8 - 2));
|
|
||||||
if (128 == 1)
|
|
||||||
chunk >>= 3;
|
|
||||||
if (inl < chunk)
|
|
||||||
chunk = inl;
|
|
||||||
while (inl && inl >= chunk) {
|
|
||||||
sms4_cfb128_encrypt(in, out, (long)((128 == 1) && !(ctx->flags & 0x2000) ? inl * 8 : inl), &((EVP_SMS4_KEY *) ctx->cipher_data)->ks, ctx->iv, &ctx->num, ctx->encrypt);
|
|
||||||
inl -= chunk;
|
|
||||||
in += chunk;
|
|
||||||
out += chunk;
|
|
||||||
if (inl < chunk)
|
|
||||||
chunk = inl;
|
|
||||||
} return 1;
|
|
||||||
} static int sms4_ecb_cipher(EVP_CIPHER_CTX * ctx, unsigned char *out, const unsigned char *in, size_t inl){
|
|
||||||
size_t i , bl;
|
|
||||||
bl = ctx->cipher->block_size;
|
|
||||||
if (inl < bl)
|
|
||||||
return 1;
|
|
||||||
inl -= bl;
|
|
||||||
for (i = 0; i <= inl; i += bl)
|
|
||||||
sms4_ecb_encrypt(in + i, out + i, &((EVP_SMS4_KEY *) ctx->cipher_data)->ks, ctx->encrypt);
|
|
||||||
return 1;
|
|
||||||
} static int sms4_ofb_cipher(EVP_CIPHER_CTX * ctx, unsigned char *out, const unsigned char *in, size_t inl){
|
|
||||||
while (inl >= ((size_t) 1 << (sizeof(long) * 8 - 2))) {
|
|
||||||
sms4_ofb128_encrypt(in, out, (long)((size_t) 1 << (sizeof(long) * 8 - 2)), &((EVP_SMS4_KEY *) ctx->cipher_data)->ks, ctx->iv, &ctx->num);
|
|
||||||
inl -= ((size_t) 1 << (sizeof(long) * 8 - 2));
|
|
||||||
in += ((size_t) 1 << (sizeof(long) * 8 - 2));
|
|
||||||
out += ((size_t) 1 << (sizeof(long) * 8 - 2));
|
|
||||||
} if (inl)
|
|
||||||
sms4_ofb128_encrypt(in, out, (long)inl, &((EVP_SMS4_KEY *) ctx->cipher_data)->ks, ctx->iv, &ctx->num);
|
|
||||||
return 1;
|
|
||||||
} static const EVP_CIPHER sms4_cbc = {978, 16, 16, 16, 0 | 0x2, sms4_init_key, sms4_cbc_cipher, ((void *)0), sizeof(EVP_SMS4_KEY), ((void *)0), ((void *)0), ((void *)0), ((void *)0)};
|
|
||||||
const EVP_CIPHER *EVP_sms4_cbc(void){
|
|
||||||
return &sms4_cbc;
|
|
||||||
} static const EVP_CIPHER sms4_cfb128 = {982, 1, 16, 16, 0 | 0x3, sms4_init_key, sms4_cfb128_cipher, ((void *)0), sizeof(EVP_SMS4_KEY), ((void *)0), ((void *)0), ((void *)0), ((void *)0)};
|
|
||||||
const EVP_CIPHER *EVP_sms4_cfb128(void){
|
|
||||||
return &sms4_cfb128;
|
|
||||||
} static const EVP_CIPHER sms4_ofb = {981, 1, 16, 16, 0 | 0x4, sms4_init_key, sms4_ofb_cipher, ((void *)0), sizeof(EVP_SMS4_KEY), ((void *)0), ((void *)0), ((void *)0), ((void *)0)};
|
|
||||||
const EVP_CIPHER *EVP_sms4_ofb(void){
|
|
||||||
return &sms4_ofb;
|
|
||||||
} static const EVP_CIPHER sms4_ecb = {977, 16, 16, 0, 0 | 0x1, sms4_init_key, sms4_ecb_cipher, ((void *)0), sizeof(EVP_SMS4_KEY), ((void *)0), ((void *)0), ((void *)0), ((void *)0)};
|
|
||||||
const EVP_CIPHER *
|
|
||||||
EVP_sms4_ecb(void)
|
|
||||||
{
|
|
||||||
return &sms4_ecb;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
|
|
||||||
static int sms4_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) {
|
|
||||||
while(inl>=((size_t)1<<(sizeof(long)*8-2))) { sms4_cbc_encrypt(in, out, (long)((size_t)1<<(sizeof(long)*8-2)), &((EVP_SMS4_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt); inl-=((size_t)1<<(sizeof(long)*8-2)); in +=((size_t)1<<(sizeof(long)*8-2)); out+=((size_t)1<<(sizeof(long)*8-2)); } if (inl) sms4_cbc_encrypt(in, out, (long)inl, &((EVP_SMS4_KEY *)ctx->cipher_data)->ks, ctx->iv, ctx->encrypt); return 1;} static int sms4_cfb128_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) { size_t chunk=((size_t)1<<(sizeof(long)*8-2)); if (128==1) chunk>>=3; if (inl<chunk) chunk=inl; while(inl && inl>=chunk) { sms4_cfb128_encrypt(in, out, (long)((128==1) && !(ctx->flags & 0x2000) ?inl*8:inl), &((EVP_SMS4_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num, ctx->encrypt); inl-=chunk; in +=chunk; out+=chunk; if(inl<chunk) chunk=inl; } return 1;} static int sms4_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) { size_t i, bl; bl = ctx->cipher->block_size; if(inl < bl) return 1; inl -= bl; for(i=0; i <= inl; i+=bl) sms4_ecb_encrypt(in + i, out + i, &((EVP_SMS4_KEY *)ctx->cipher_data)->ks, ctx->encrypt); return 1;} static int sms4_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) { while(inl>=((size_t)1<<(sizeof(long)*8-2))) { sms4_ofb128_encrypt(in, out, (long)((size_t)1<<(sizeof(long)*8-2)), &((EVP_SMS4_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num); inl-=((size_t)1<<(sizeof(long)*8-2)); in +=((size_t)1<<(sizeof(long)*8-2)); out+=((size_t)1<<(sizeof(long)*8-2)); } if (inl) sms4_ofb128_encrypt(in, out, (long)inl, &((EVP_SMS4_KEY *)ctx->cipher_data)->ks, ctx->iv, &ctx->num); return 1;} static const EVP_CIPHER sms4_cbc = { 978, 16, 16, 16, 0 | 0x2, sms4_init_key, sms4_cbc_cipher, ((void*)0), sizeof(EVP_SMS4_KEY), ((void*)0), ((void*)0), ((void*)0), ((void*)0) }; const EVP_CIPHER *EVP_sms4_cbc(void) { return &sms4_cbc; } static const EVP_CIPHER sms4_cfb128 = { 982, 1, 16, 16, 0 | 0x3, sms4_init_key, sms4_cfb128_cipher, ((void*)0), sizeof(EVP_SMS4_KEY), ((void*)0), ((void*)0), ((void*)0), ((void*)0) }; const EVP_CIPHER *EVP_sms4_cfb128(void) { return &sms4_cfb128; } static const EVP_CIPHER sms4_ofb = { 981, 1, 16, 16, 0 | 0x4, sms4_init_key, sms4_ofb_cipher, ((void*)0), sizeof(EVP_SMS4_KEY), ((void*)0), ((void*)0), ((void*)0), ((void*)0) }; const EVP_CIPHER *EVP_sms4_ofb(void) { return &sms4_ofb; } static const EVP_CIPHER sms4_ecb = { 977, 16, 16, 0, 0 | 0x1, sms4_init_key, sms4_ecb_cipher, ((void*)0), sizeof(EVP_SMS4_KEY), ((void*)0), ((void*)0), ((void*)0), ((void*)0) }; const EVP_CIPHER *EVP_sms4_ecb(void) { return &sms4_ecb; }
|
|
||||||
@@ -56,6 +56,7 @@
|
|||||||
#include <openssl/ec.h>
|
#include <openssl/ec.h>
|
||||||
#include <openssl/err.h>
|
#include <openssl/err.h>
|
||||||
#include <openssl/evp.h>
|
#include <openssl/evp.h>
|
||||||
|
#include <openssl/kdf.h>
|
||||||
#include <openssl/x509.h>
|
#include <openssl/x509.h>
|
||||||
#include <openssl/asn1.h>
|
#include <openssl/asn1.h>
|
||||||
#include <openssl/ecdsa.h>
|
#include <openssl/ecdsa.h>
|
||||||
|
|||||||
@@ -417,6 +417,7 @@ end:
|
|||||||
cv = NULL;
|
cv = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (point) EC_POINT_free(point);
|
||||||
if (n) BN_free(n);
|
if (n) BN_free(n);
|
||||||
if (h) BN_free(h);
|
if (h) BN_free(h);
|
||||||
if (k) BN_free(k);
|
if (k) BN_free(k);
|
||||||
|
|||||||
@@ -65,6 +65,8 @@
|
|||||||
#define EC_MAX_NBYTES ((OPENSSL_ECC_MAX_FIELD_BITS + 7)/8)
|
#define EC_MAX_NBYTES ((OPENSSL_ECC_MAX_FIELD_BITS + 7)/8)
|
||||||
|
|
||||||
|
|
||||||
|
#define SM2_DEFAULT_ID "1234567812345678"
|
||||||
|
|
||||||
static void *sm2_data_dup(void *data) {
|
static void *sm2_data_dup(void *data) {
|
||||||
return OPENSSL_strdup((const char *)data);
|
return OPENSSL_strdup((const char *)data);
|
||||||
}
|
}
|
||||||
@@ -184,6 +186,7 @@ err:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: review this function again.
|
||||||
int SM2_compute_id_digest(unsigned char *dgst, unsigned int *dgstlen,
|
int SM2_compute_id_digest(unsigned char *dgst, unsigned int *dgstlen,
|
||||||
const EVP_MD *md, EC_KEY *ec_key)
|
const EVP_MD *md, EC_KEY *ec_key)
|
||||||
{
|
{
|
||||||
@@ -199,7 +202,7 @@ int SM2_compute_id_digest(unsigned char *dgst, unsigned int *dgstlen,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!(id = SM2_get_id(ec_key))) {
|
if (!(id = SM2_get_id(ec_key))) {
|
||||||
goto err;
|
id = SM2_DEFAULT_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
idbits = strlen(id) * 8;
|
idbits = strlen(id) * 8;
|
||||||
|
|||||||
@@ -451,7 +451,6 @@ int ssl_get_new_session(SSL *s, int session)
|
|||||||
} else if (s->version == GMSSL1_1_VERSION) {
|
} else if (s->version == GMSSL1_1_VERSION) {
|
||||||
ss->ssl_version = GMSSL1_1_VERSION;
|
ss->ssl_version = GMSSL1_1_VERSION;
|
||||||
ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
|
ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_UNSUPPORTED_SSL_VERSION);
|
SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_UNSUPPORTED_SSL_VERSION);
|
||||||
|
|||||||
Reference in New Issue
Block a user