# 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 */
# 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 */
const EVP_PKEY_METHOD ec_pkey_meth = {
EVP_PKEY_EC,
- EVP_PKEY_FLAG_AUTOARGLEN,
+ 0,
pkey_ec_init,
pkey_ec_copy,
pkey_ec_cleanup,
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,
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,
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;
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);
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: