summaryrefslogtreecommitdiffstats
path: root/crypto/sm2
diff options
context:
space:
mode:
authorMatt Caswell <matt@openssl.org>2018-05-31 14:49:47 +0200
committerMatt Caswell <matt@openssl.org>2018-06-04 12:59:56 +0200
commite14d6cf691c9d8ad49df280b580b6836a67c6a19 (patch)
treecbf7b07a1abd22da9e711c3656ed599f5f7b7715 /crypto/sm2
parentFurther work on SM2 error codes (diff)
downloadopenssl-e14d6cf691c9d8ad49df280b580b6836a67c6a19.tar.xz
openssl-e14d6cf691c9d8ad49df280b580b6836a67c6a19.zip
Improve use of the test framework in the SM2 internal tests
Also general clean up of those tests Reviewed-by: Richard Levitte <levitte@openssl.org> (Merged from https://github.com/openssl/openssl/pull/6386)
Diffstat (limited to 'crypto/sm2')
-rw-r--r--crypto/sm2/sm2_crypt.c38
1 files changed, 29 insertions, 9 deletions
diff --git a/crypto/sm2/sm2_crypt.c b/crypto/sm2/sm2_crypt.c
index ab2d18ee2e..2a0fdceea7 100644
--- a/crypto/sm2/sm2_crypt.c
+++ b/crypto/sm2/sm2_crypt.c
@@ -59,23 +59,43 @@ 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)
+int SM2_plaintext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
+ size_t *pt_size)
{
const size_t field_size = EC_field_size(EC_KEY_get0_group(key));
- const size_t md_size = EVP_MD_size(digest);
+ const int md_size = EVP_MD_size(digest);
+ size_t overhead;
- const size_t overhead = 10 + 2 * field_size + md_size;
- if(msg_len <= overhead)
- return 0;
+ if (md_size < 0) {
+ SM2err(SM2_F_SM2_ENCRYPT, SM2_R_INVALID_DIGEST);
+ return 0;
+ }
+ if (field_size == 0) {
+ SM2err(SM2_F_SM2_ENCRYPT, SM2_R_INVALID_FIELD);
+ return 0;
+ }
+
+ overhead = 10 + 2 * field_size + (size_t)md_size;
+ if(msg_len <= overhead) {
+ SM2err(SM2_F_SM2_ENCRYPT, SM2_R_INVALID_ENCODING);
+ return 0;
+ }
- return msg_len - overhead;
+ *pt_size = msg_len - overhead;
+ return 1;
}
-size_t SM2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len)
+int SM2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
+ size_t *ct_size)
{
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;
+ const int md_size = EVP_MD_size(digest);
+
+ if (field_size == 0 || md_size < 0)
+ return 0;
+
+ *ct_size = 10 + 2 * field_size + (size_t)md_size + msg_len;
+ return 1;
}
int SM2_encrypt(const EC_KEY *key,