aboutsummaryrefslogtreecommitdiff
path: root/crypto/sm2/sm2_crypt.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/sm2/sm2_crypt.c')
-rw-r--r--crypto/sm2/sm2_crypt.c17
1 files changed, 15 insertions, 2 deletions
diff --git a/crypto/sm2/sm2_crypt.c b/crypto/sm2/sm2_crypt.c
index b1c2ef6b630a..32bfa9797fa7 100644
--- a/crypto/sm2/sm2_crypt.c
+++ b/crypto/sm2/sm2_crypt.c
@@ -1,5 +1,5 @@
/*
- * Copyright 2017-2024 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2017-2026 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 2017 Ribose Inc. All Rights Reserved.
* Ported from Ribose contributions from Botan.
*
@@ -248,12 +248,23 @@ again:
goto done;
}
- ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
+ ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, NULL);
/* Ensure cast to size_t is safe */
if (ciphertext_leni < 0) {
ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
goto done;
}
+
+ if (*ciphertext_len < (size_t)ciphertext_leni) {
+ ERR_raise(ERR_LIB_SM2, SM2_R_BUFFER_TOO_SMALL);
+ goto done;
+ }
+
+ ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);
+ if (ciphertext_leni < 0) {
+ ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
+ goto done;
+ }
*ciphertext_len = (size_t)ciphertext_leni;
rc = 1;
@@ -266,6 +277,7 @@ done:
OPENSSL_free(x2y2);
OPENSSL_free(C3);
EVP_MD_CTX_free(hash);
+ BN_CTX_end(ctx);
BN_CTX_free(ctx);
EC_POINT_free(kG);
EC_POINT_free(kP);
@@ -406,6 +418,7 @@ done:
OPENSSL_free(x2y2);
OPENSSL_free(computed_C3);
EC_POINT_free(C1);
+ BN_CTX_end(ctx);
BN_CTX_free(ctx);
SM2_Ciphertext_free(sm2_ctext);
EVP_MD_CTX_free(hash);