Handle evp_tests assumption of EVP_PKEY_FLAG_AUTOARGLEN

Without actually using EVP_PKEY_FLAG_AUTOARGLEN

Reviewed-by: Rich Salz <rsalz@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/4793)
This commit is contained in:
Jack Lloyd 2018-02-09 12:21:56 -05:00 committed by Richard Levitte
parent dceb99a5fb
commit 4e66475063
5 changed files with 43 additions and 8 deletions

View file

@ -221,8 +221,14 @@ static int pkey_ecies_encrypt(EVP_PKEY_CTX *ctx,
# if defined(OPENSSL_NO_SM2)
ret = -1;
# else
ret = SM2_encrypt(ec, EVP_get_digestbynid(md_type),
in, inlen, out, outlen);
if (out == NULL) {
*outlen = SM2_ciphertext_size(ec, EVP_get_digestbynid(md_type), inlen);
ret = 1;
}
else {
ret = SM2_encrypt(ec, EVP_get_digestbynid(md_type),
in, inlen, out, outlen);
}
# endif
} else {
/* standard ECIES not implemented */
@ -252,8 +258,14 @@ static int pkey_ecies_decrypt(EVP_PKEY_CTX *ctx,
# if defined(OPENSSL_NO_SM2)
ret = -1;
# else
ret = SM2_decrypt(ec, EVP_get_digestbynid(md_type),
in, inlen, out, outlen);
if (out == NULL) {
*outlen = SM2_plaintext_size(ec, EVP_get_digestbynid(md_type), inlen);
ret = 1;
}
else {
ret = SM2_decrypt(ec, EVP_get_digestbynid(md_type),
in, inlen, out, outlen);
}
# endif
} else {
/* standard ECIES not implemented */
@ -510,7 +522,7 @@ static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
const EVP_PKEY_METHOD ec_pkey_meth = {
EVP_PKEY_EC,
EVP_PKEY_FLAG_AUTOARGLEN,
0,
pkey_ec_init,
pkey_ec_copy,
pkey_ec_cleanup,

View file

@ -57,10 +57,23 @@ static size_t EC_field_size(const EC_GROUP *group)
return field_size;
}
size_t SM2_plaintext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len)
{
const size_t field_size = EC_field_size(EC_KEY_get0_group(key));
const size_t md_size = EVP_MD_size(digest);
const size_t overhead = 10 + 2 * field_size + md_size;
if(msg_len <= overhead)
return 0;
return msg_len - overhead;
}
size_t SM2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len)
{
return 10 + 2 * EC_field_size(EC_KEY_get0_group(key)) +
EVP_MD_size(digest) + msg_len;
const size_t field_size = EC_field_size(EC_KEY_get0_group(key));
const size_t md_size = EVP_MD_size(digest);
return 10 + 2 * field_size + md_size + msg_len;
}
int SM2_encrypt(const EC_KEY *key,

View file

@ -54,6 +54,10 @@ size_t SM2_ciphertext_size(const EC_KEY *key,
const EVP_MD *digest,
size_t msg_len);
size_t SM2_plaintext_size(const EC_KEY *key,
const EVP_MD *digest,
size_t msg_len);
int SM2_encrypt(const EC_KEY *key,
const EVP_MD *digest,
const uint8_t *msg,

View file

@ -139,6 +139,7 @@ static int test_sm2(const EC_GROUP *group,
unsigned char *expected = OPENSSL_hexstr2buf(ctext_hex, NULL);
size_t ctext_len = 0;
size_t ptext_len = 0;
uint8_t *ctext = NULL;
uint8_t *recovered = NULL;
size_t recovered_len = msg_len;
@ -172,7 +173,11 @@ static int test_sm2(const EC_GROUP *group,
if (rc == 0)
goto done;
recovered = OPENSSL_zalloc(msg_len);
ptext_len = SM2_plaintext_size(key, digest, ctext_len);
TEST_int_eq(ptext_len, msg_len);
recovered = OPENSSL_zalloc(ptext_len);
if (recovered == NULL)
goto done;
rc = SM2_decrypt(key, digest, ctext, ctext_len, recovered, &recovered_len);

View file

@ -4523,3 +4523,4 @@ SM2_verify 4464 1_1_1 EXIST::FUNCTION:
SM2_do_verify 4465 1_1_1 EXIST::FUNCTION:
SM2_sign 4466 1_1_1 EXIST::FUNCTION:
ERR_load_SM2_strings 4467 1_1_1 EXIST::FUNCTION:
SM2_plaintext_size 4468 1_1_1 EXIST::FUNCTION: