From 67be048e1a3272fe5202c5571dc23d16cfa2ed04 Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Mon, 20 Apr 2015 16:19:00 -0400 Subject: [PATCH] Convert ec_test to C++ Change-Id: I5e25ddbc87370b58d9b6fc410f51e259947df8dd Reviewed-on: https://boringssl-review.googlesource.com/4468 Reviewed-by: Adam Langley --- crypto/ec/CMakeLists.txt | 2 +- crypto/ec/{ec_test.c => ec_test.cc} | 97 +++++++++++------------------ crypto/test/scoped_types.h | 1 + ssl/ssl_test.cc | 5 +- 4 files changed, 42 insertions(+), 63 deletions(-) rename crypto/ec/{ec_test.c => ec_test.cc} (56%) diff --git a/crypto/ec/CMakeLists.txt b/crypto/ec/CMakeLists.txt index fd3fb569..a218c0d4 100644 --- a/crypto/ec/CMakeLists.txt +++ b/crypto/ec/CMakeLists.txt @@ -25,7 +25,7 @@ add_executable( add_executable( ec_test - ec_test.c + ec_test.cc ) target_link_libraries(example_mul crypto) diff --git a/crypto/ec/ec_test.c b/crypto/ec/ec_test.cc similarity index 56% rename from crypto/ec/ec_test.c rename to crypto/ec/ec_test.cc index 269005bc..9b49a551 100644 --- a/crypto/ec/ec_test.c +++ b/crypto/ec/ec_test.cc @@ -15,11 +15,15 @@ #include #include +#include + #include #include #include #include +#include "../test/scoped_types.h" +#include "../test/stl_compat.h" #include "internal.h" @@ -30,91 +34,66 @@ static const uint8_t kECKeyWithoutPublic[] = { 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, }; -int test_d2i_ECPrivateKey(void) { - int len, ret = 0; - uint8_t *out = NULL, *outp; - const uint8_t *inp; - EC_KEY *key = NULL; - BIGNUM *x = NULL, *y = NULL; - const EC_POINT *public; - char *x_hex = NULL, *y_hex = NULL; +bool Testd2i_ECPrivateKey(void) { + const uint8_t *inp = kECKeyWithoutPublic; + ScopedEC_KEY key(d2i_ECPrivateKey(NULL, &inp, sizeof(kECKeyWithoutPublic))); - inp = kECKeyWithoutPublic; - key = d2i_ECPrivateKey(NULL, &inp, sizeof(kECKeyWithoutPublic)); - - if (key == NULL || inp != kECKeyWithoutPublic + sizeof(kECKeyWithoutPublic)) { + if (!key || inp != kECKeyWithoutPublic + sizeof(kECKeyWithoutPublic)) { fprintf(stderr, "Failed to parse private key.\n"); ERR_print_errors_fp(stderr); - goto out; + return false; } - len = i2d_ECPrivateKey(key, NULL); - out = malloc(len); - outp = out; - if (len != i2d_ECPrivateKey(key, &outp)) { + int len = i2d_ECPrivateKey(key.get(), NULL); + std::vector out(len); + uint8_t *outp = bssl::vector_data(&out); + if (len != i2d_ECPrivateKey(key.get(), &outp)) { fprintf(stderr, "Failed to serialize private key.\n"); ERR_print_errors_fp(stderr); - goto out; + return false; } - if (0 != memcmp(out, kECKeyWithoutPublic, len)) { + if (0 != memcmp(bssl::vector_data(&out), kECKeyWithoutPublic, len)) { fprintf(stderr, "Serialisation of key doesn't match original.\n"); - goto out; + return false; } - public = EC_KEY_get0_public_key(key); - if (public == NULL) { + const EC_POINT *pub_key = EC_KEY_get0_public_key(key.get()); + if (pub_key == NULL) { fprintf(stderr, "Public key missing.\n"); - goto out; + return false; } - x = BN_new(); - y = BN_new(); - if (x == NULL || y == NULL) { - goto out; + ScopedBIGNUM x(BN_new()); + ScopedBIGNUM y(BN_new()); + if (!x || !y) { + return false; } - if (!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(key), public, x, y, - NULL)) { + if (!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(key.get()), + pub_key, x.get(), y.get(), NULL)) { fprintf(stderr, "Failed to get public key in affine coordinates.\n"); - goto out; + return false; } - x_hex = BN_bn2hex(x); - y_hex = BN_bn2hex(y); - if (0 != strcmp(x_hex, "c81561ecf2e54edefe6617db1c7a34a70744ddb261f269b83dacfcd2ade5a681") || - 0 != strcmp(y_hex, "e0e2afa3f9b6abe4c698ef6495f1be49a3196c5056acb3763fe4507eec596e88")) { - fprintf(stderr, "Incorrect public key: %s %s\n", x_hex, y_hex); - goto out; + ScopedOpenSSLString x_hex(BN_bn2hex(x.get())); + ScopedOpenSSLString y_hex(BN_bn2hex(y.get())); + if (0 != strcmp( + x_hex.get(), + "c81561ecf2e54edefe6617db1c7a34a70744ddb261f269b83dacfcd2ade5a681") || + 0 != strcmp( + y_hex.get(), + "e0e2afa3f9b6abe4c698ef6495f1be49a3196c5056acb3763fe4507eec596e88")) { + fprintf(stderr, "Incorrect public key: %s %s\n", x_hex.get(), y_hex.get()); + return false; } - ret = 1; - -out: - if (key != NULL) { - EC_KEY_free(key); - } - if (out != NULL) { - free(out); - } - if (x != NULL) { - BN_free(x); - } - if (y != NULL) { - BN_free(y); - } - if (x_hex != NULL) { - OPENSSL_free(x_hex); - } - if (y_hex != NULL) { - OPENSSL_free(y_hex); - } - return ret; + return true; } int main(void) { CRYPTO_library_init(); ERR_load_crypto_strings(); - if (!test_d2i_ECPrivateKey()) { + if (!Testd2i_ECPrivateKey()) { fprintf(stderr, "failed\n"); return 1; } diff --git a/crypto/test/scoped_types.h b/crypto/test/scoped_types.h index 69c64231..bdb3ca7f 100644 --- a/crypto/test/scoped_types.h +++ b/crypto/test/scoped_types.h @@ -93,6 +93,7 @@ using ScopedHMAC_CTX = ScopedOpenSSLContext; using ScopedOpenSSLBytes = bssl::unique_ptr>; +using ScopedOpenSSLString = bssl::unique_ptr>; #endif // OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H diff --git a/ssl/ssl_test.cc b/ssl/ssl_test.cc index a0dcfda5..7886304e 100644 --- a/ssl/ssl_test.cc +++ b/ssl/ssl_test.cc @@ -430,9 +430,8 @@ static bool CipherGetRFCName(std::string *out, uint16_t value) { if (cipher == NULL) { return false; } - char *rfc_name = SSL_CIPHER_get_rfc_name(cipher); - out->assign(rfc_name); - OPENSSL_free(rfc_name); + ScopedOpenSSLString rfc_name(SSL_CIPHER_get_rfc_name(cipher)); + out->assign(rfc_name.get()); return true; }