Convert ec_test to C++

Change-Id: I5e25ddbc87370b58d9b6fc410f51e259947df8dd
Reviewed-on: https://boringssl-review.googlesource.com/4468
Reviewed-by: Adam Langley <agl@google.com>
This commit is contained in:
David Benjamin 2015-04-20 16:19:00 -04:00 committed by Adam Langley
parent 7af16eb49f
commit 67be048e1a
4 changed files with 42 additions and 63 deletions

View File

@ -25,7 +25,7 @@ add_executable(
add_executable( add_executable(
ec_test ec_test
ec_test.c ec_test.cc
) )
target_link_libraries(example_mul crypto) target_link_libraries(example_mul crypto)

View File

@ -15,11 +15,15 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <vector>
#include <openssl/crypto.h> #include <openssl/crypto.h>
#include <openssl/ec_key.h> #include <openssl/ec_key.h>
#include <openssl/err.h> #include <openssl/err.h>
#include <openssl/mem.h> #include <openssl/mem.h>
#include "../test/scoped_types.h"
#include "../test/stl_compat.h"
#include "internal.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, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
}; };
int test_d2i_ECPrivateKey(void) { bool Testd2i_ECPrivateKey(void) {
int len, ret = 0; const uint8_t *inp = kECKeyWithoutPublic;
uint8_t *out = NULL, *outp; ScopedEC_KEY key(d2i_ECPrivateKey(NULL, &inp, sizeof(kECKeyWithoutPublic)));
const uint8_t *inp;
EC_KEY *key = NULL;
BIGNUM *x = NULL, *y = NULL;
const EC_POINT *public;
char *x_hex = NULL, *y_hex = NULL;
inp = kECKeyWithoutPublic; if (!key || inp != kECKeyWithoutPublic + sizeof(kECKeyWithoutPublic)) {
key = d2i_ECPrivateKey(NULL, &inp, sizeof(kECKeyWithoutPublic));
if (key == NULL || inp != kECKeyWithoutPublic + sizeof(kECKeyWithoutPublic)) {
fprintf(stderr, "Failed to parse private key.\n"); fprintf(stderr, "Failed to parse private key.\n");
ERR_print_errors_fp(stderr); ERR_print_errors_fp(stderr);
goto out; return false;
} }
len = i2d_ECPrivateKey(key, NULL); int len = i2d_ECPrivateKey(key.get(), NULL);
out = malloc(len); std::vector<uint8_t> out(len);
outp = out; uint8_t *outp = bssl::vector_data(&out);
if (len != i2d_ECPrivateKey(key, &outp)) { if (len != i2d_ECPrivateKey(key.get(), &outp)) {
fprintf(stderr, "Failed to serialize private key.\n"); fprintf(stderr, "Failed to serialize private key.\n");
ERR_print_errors_fp(stderr); 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"); fprintf(stderr, "Serialisation of key doesn't match original.\n");
goto out; return false;
} }
public = EC_KEY_get0_public_key(key); const EC_POINT *pub_key = EC_KEY_get0_public_key(key.get());
if (public == NULL) { if (pub_key == NULL) {
fprintf(stderr, "Public key missing.\n"); fprintf(stderr, "Public key missing.\n");
goto out; return false;
} }
x = BN_new(); ScopedBIGNUM x(BN_new());
y = BN_new(); ScopedBIGNUM y(BN_new());
if (x == NULL || y == NULL) { if (!x || !y) {
goto out; return false;
} }
if (!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(key), public, x, y, if (!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(key.get()),
NULL)) { pub_key, x.get(), y.get(), NULL)) {
fprintf(stderr, "Failed to get public key in affine coordinates.\n"); fprintf(stderr, "Failed to get public key in affine coordinates.\n");
goto out; return false;
} }
x_hex = BN_bn2hex(x); ScopedOpenSSLString x_hex(BN_bn2hex(x.get()));
y_hex = BN_bn2hex(y); ScopedOpenSSLString y_hex(BN_bn2hex(y.get()));
if (0 != strcmp(x_hex, "c81561ecf2e54edefe6617db1c7a34a70744ddb261f269b83dacfcd2ade5a681") || if (0 != strcmp(
0 != strcmp(y_hex, "e0e2afa3f9b6abe4c698ef6495f1be49a3196c5056acb3763fe4507eec596e88")) { x_hex.get(),
fprintf(stderr, "Incorrect public key: %s %s\n", x_hex, y_hex); "c81561ecf2e54edefe6617db1c7a34a70744ddb261f269b83dacfcd2ade5a681") ||
goto out; 0 != strcmp(
y_hex.get(),
"e0e2afa3f9b6abe4c698ef6495f1be49a3196c5056acb3763fe4507eec596e88")) {
fprintf(stderr, "Incorrect public key: %s %s\n", x_hex.get(), y_hex.get());
return false;
} }
ret = 1; return true;
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;
} }
int main(void) { int main(void) {
CRYPTO_library_init(); CRYPTO_library_init();
ERR_load_crypto_strings(); ERR_load_crypto_strings();
if (!test_d2i_ECPrivateKey()) { if (!Testd2i_ECPrivateKey()) {
fprintf(stderr, "failed\n"); fprintf(stderr, "failed\n");
return 1; return 1;
} }

View File

@ -93,6 +93,7 @@ using ScopedHMAC_CTX = ScopedOpenSSLContext<HMAC_CTX, void, HMAC_CTX_init,
HMAC_CTX_cleanup>; HMAC_CTX_cleanup>;
using ScopedOpenSSLBytes = bssl::unique_ptr<uint8_t, OpenSSLFree<uint8_t>>; using ScopedOpenSSLBytes = bssl::unique_ptr<uint8_t, OpenSSLFree<uint8_t>>;
using ScopedOpenSSLString = bssl::unique_ptr<char, OpenSSLFree<char>>;
#endif // OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H #endif // OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H

View File

@ -430,9 +430,8 @@ static bool CipherGetRFCName(std::string *out, uint16_t value) {
if (cipher == NULL) { if (cipher == NULL) {
return false; return false;
} }
char *rfc_name = SSL_CIPHER_get_rfc_name(cipher); ScopedOpenSSLString rfc_name(SSL_CIPHER_get_rfc_name(cipher));
out->assign(rfc_name); out->assign(rfc_name.get());
OPENSSL_free(rfc_name);
return true; return true;
} }