diff --git a/crypto/ec/ec_pmeth.c b/crypto/ec/ec_pmeth.c index ec02d85a..a58afc77 100644 --- a/crypto/ec/ec_pmeth.c +++ b/crypto/ec/ec_pmeth.c @@ -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__); + /* if (!(id = SM2_get_id(ec_key))) { fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__); id = "alice@pku.edu.cn"; //return 0; } + */ + //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__); return 0; } diff --git a/crypto/evp/a.c b/crypto/evp/a.c deleted file mode 100644 index cfa56294..00000000 --- a/crypto/evp/a.c +++ /dev/null @@ -1,92 +0,0 @@ - -/* crypto/evp/e_sms4.c */ -#include -#include "../cryptlib.h" - -#ifndef OPENSSL_NO_SMS4 -#include -#include -#include "evp_locl.h" -#include -#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 diff --git a/crypto/evp/a.c.BAK b/crypto/evp/a.c.BAK deleted file mode 100644 index 77975bc4..00000000 --- a/crypto/evp/a.c.BAK +++ /dev/null @@ -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) { 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(inlcipher->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; } diff --git a/crypto/sm2/sm2.h b/crypto/sm2/sm2.h index 956ed9a6..654e0fa4 100644 --- a/crypto/sm2/sm2.h +++ b/crypto/sm2/sm2.h @@ -56,6 +56,7 @@ #include #include #include +#include #include #include #include diff --git a/crypto/sm2/sm2_enc.c b/crypto/sm2/sm2_enc.c index 5fa206ed..a82606bd 100644 --- a/crypto/sm2/sm2_enc.c +++ b/crypto/sm2/sm2_enc.c @@ -417,6 +417,7 @@ end: cv = NULL; } + if (point) EC_POINT_free(point); if (n) BN_free(n); if (h) BN_free(h); if (k) BN_free(k); diff --git a/crypto/sm2/sm2_lib.c b/crypto/sm2/sm2_lib.c index a3ba906e..e75d39c4 100644 --- a/crypto/sm2/sm2_lib.c +++ b/crypto/sm2/sm2_lib.c @@ -65,6 +65,8 @@ #define EC_MAX_NBYTES ((OPENSSL_ECC_MAX_FIELD_BITS + 7)/8) +#define SM2_DEFAULT_ID "1234567812345678" + static void *sm2_data_dup(void *data) { return OPENSSL_strdup((const char *)data); } @@ -184,6 +186,7 @@ err: return ret; } +//TODO: review this function again. int SM2_compute_id_digest(unsigned char *dgst, unsigned int *dgstlen, 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))) { - goto err; + id = SM2_DEFAULT_ID; } idbits = strlen(id) * 8; diff --git a/ssl/ssl_sess.c b/ssl/ssl_sess.c index aa021444..acdd9957 100644 --- a/ssl/ssl_sess.c +++ b/ssl/ssl_sess.c @@ -451,7 +451,6 @@ int ssl_get_new_session(SSL *s, int session) } else if (s->version == GMSSL1_1_VERSION) { ss->ssl_version = GMSSL1_1_VERSION; ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; - } #endif } else { SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_UNSUPPORTED_SSL_VERSION);