speed sm2/3/4

`gmssl speed sm2 sm3 sms4`
This commit is contained in:
Zhi Guan
2017-11-25 13:22:04 +08:00
parent 4d60905e04
commit 2458fcb879
24 changed files with 618 additions and 340 deletions

View File

@@ -71,3 +71,4 @@ int SM2CiphertextValue_size(const EC_GROUP *group, int inlen)
{
return 1024;
}

View File

@@ -230,36 +230,24 @@ end:
int SM2_encrypt(int type, const unsigned char *in, size_t inlen,
unsigned char *out, size_t *outlen, EC_KEY *ec_key)
{
int ret = 0;
SM2CiphertextValue *cv = NULL;
const EVP_MD *md;
int len;
SM2CiphertextValue *cv;
if (!(md = EVP_get_digestbynid(type))) {
SM2err(SM2_F_SM2_ENCRYPT, SM2_R_INVALID_DIGEST_ALGOR);
*outlen = 0;
return 0;
}
RAND_seed(in, inlen);
if (!(cv = SM2_do_encrypt(md, in, inlen, ec_key))) {
SM2err(SM2_F_SM2_ENCRYPT, SM2_R_ENCRYPT_FAILURE);
goto end;
*outlen = 0;
return 0;
}
if (!out) {
*outlen = i2d_SM2CiphertextValue(cv, NULL) + 96;
ret = 1;
} else if (*outlen < i2d_SM2CiphertextValue(cv, NULL) + 64) {
SM2err(SM2_F_SM2_ENCRYPT, SM2_R_BUFFER_TOO_SMALL);
ret = 0;
} else {
len = i2d_SM2CiphertextValue(cv, &out);
*outlen = len;
ret = 1;
}
end:
*outlen = i2d_SM2CiphertextValue(cv, &out);
SM2CiphertextValue_free(cv);
return ret;
return 1;
}
int SM2_decrypt(int type, const unsigned char *in, size_t inlen,
@@ -273,14 +261,18 @@ int SM2_decrypt(int type, const unsigned char *in, size_t inlen,
/* check arguments */
if (!(md = EVP_get_digestbynid(type))) {
SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_DIGEST_ALGOR);
*outlen = 0;
return 0;
}
if (!in) {
SM2err(SM2_F_SM2_DECRYPT, ERR_R_PASSED_NULL_PARAMETER);
*outlen = 0;
return 0;
}
if (inlen <= 0 || inlen > INT_MAX) {
SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_INPUT_LENGTH);
*outlen = 0;
return 0;
}
@@ -300,11 +292,14 @@ int SM2_decrypt(int type, const unsigned char *in, size_t inlen,
*outlen = ASN1_STRING_length(cv->ciphertext);
ret = 1;
goto end;
} else if (*outlen < ASN1_STRING_length(cv->ciphertext)) {
}
/*
else if (*outlen < ASN1_STRING_length(cv->ciphertext)) {
SM2err(SM2_F_SM2_DECRYPT, SM2_R_BUFFER_TOO_SMALL);
ret = 0;
goto end;
}
*/
/* do decrypt */
if (!SM2_do_decrypt(md, cv, out, outlen, ec_key)) {
@@ -374,10 +369,12 @@ int SM2_do_decrypt(const EVP_MD *md, const SM2CiphertextValue *cv,
*outlen = cv->ciphertext->length;
return 1;
}
/*
if (*outlen < cv->ciphertext->length) {
SM2err(SM2_F_SM2_DO_DECRYPT, SM2_R_BUFFER_TOO_SMALL);
return 0;
}
*/
/* malloc */
point = EC_POINT_new(group);