Convert ecdsa_test to C++.
Change-Id: I3bcfc9d29fb3b5eed3d578eb4879b815458f20c0
Reviewed-on: https://boringssl-review.googlesource.com/4351
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/ecdsa/CMakeLists.txt b/crypto/ecdsa/CMakeLists.txt
index 95adbad..c8645d1 100644
--- a/crypto/ecdsa/CMakeLists.txt
+++ b/crypto/ecdsa/CMakeLists.txt
@@ -13,7 +13,7 @@
add_executable(
ecdsa_test
- ecdsa_test.c
+ ecdsa_test.cc
)
target_link_libraries(ecdsa_test crypto)
diff --git a/crypto/ecdsa/ecdsa_test.c b/crypto/ecdsa/ecdsa_test.c
deleted file mode 100644
index 12d02d6..0000000
--- a/crypto/ecdsa/ecdsa_test.c
+++ /dev/null
@@ -1,415 +0,0 @@
-/* ====================================================================
- * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. All advertising materials mentioning features or use of this
- * software must display the following acknowledgment:
- * "This product includes software developed by the OpenSSL Project
- * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
- *
- * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
- * endorse or promote products derived from this software without
- * prior written permission. For written permission, please contact
- * openssl-core@OpenSSL.org.
- *
- * 5. Products derived from this software may not be called "OpenSSL"
- * nor may "OpenSSL" appear in their names without prior written
- * permission of the OpenSSL Project.
- *
- * 6. Redistributions of any form whatsoever must retain the following
- * acknowledgment:
- * "This product includes software developed by the OpenSSL Project
- * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
- *
- * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
- * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- * ====================================================================
- *
- * This product includes cryptographic software written by Eric Young
- * (eay@cryptsoft.com). This product includes software written by Tim
- * Hudson (tjh@cryptsoft.com). */
-
-#include <openssl/ecdsa.h>
-
-#include <openssl/bn.h>
-#include <openssl/crypto.h>
-#include <openssl/ec.h>
-#include <openssl/err.h>
-#include <openssl/mem.h>
-#include <openssl/obj.h>
-#include <openssl/rand.h>
-
-enum api_t {
- encoded_api,
- raw_api,
-};
-
-/* verify_ecdsa_sig returns 1 on success, 0 on failure. */
-static int verify_ecdsa_sig(enum api_t api, const uint8_t *digest,
- size_t digest_len, const ECDSA_SIG *ecdsa_sig,
- EC_KEY *eckey, int expected_result) {
- int actual_result;
-
- switch (api) {
- case encoded_api: {
- int sig_len = i2d_ECDSA_SIG(ecdsa_sig, NULL);
- if (sig_len <= 0) {
- return 0;
- }
- uint8_t *signature = OPENSSL_malloc(sig_len);
- if (signature == NULL) {
- return 0;
- }
- uint8_t *sig_ptr = signature;
- sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr);
- if (sig_len <= 0) {
- OPENSSL_free(signature);
- return 0;
- }
- actual_result = ECDSA_verify(0, digest, digest_len, signature, sig_len,
- eckey);
- OPENSSL_free(signature);
- break;
- }
-
- case raw_api:
- actual_result = ECDSA_do_verify(digest, digest_len, ecdsa_sig, eckey);
- break;
-
- default:
- return 0;
- }
- return expected_result == actual_result;
-}
-
-/* test_tampered_sig verifies that signature verification fails when a valid
- * signature is tampered with. |ecdsa_sig| must be a valid signature, which
- * will be modified. test_tampered_sig returns 1 on success, 0 on failure. */
-static int test_tampered_sig(FILE *out, enum api_t api, const uint8_t *digest,
- size_t digest_len, ECDSA_SIG *ecdsa_sig,
- EC_KEY *eckey, const BIGNUM *order) {
- int ret = 0;
-
- /* Modify a single byte of the signature: to ensure we don't
- * garble the ASN1 structure, we read the raw signature and
- * modify a byte in one of the bignums directly. */
-
- /* Store the two BIGNUMs in raw_buf. */
- size_t r_len = BN_num_bytes(ecdsa_sig->r);
- size_t s_len = BN_num_bytes(ecdsa_sig->s);
- size_t bn_len = BN_num_bytes(order);
- if (r_len > bn_len || s_len > bn_len) {
- return 0;
- }
- size_t buf_len = 2 * bn_len;
- uint8_t *raw_buf = OPENSSL_malloc(buf_len);
- if (raw_buf == NULL) {
- return 0;
- }
- /* Pad the bignums with leading zeroes. */
- if (!BN_bn2bin_padded(raw_buf, bn_len, ecdsa_sig->r) ||
- !BN_bn2bin_padded(raw_buf + bn_len, bn_len, ecdsa_sig->s)) {
- goto err;
- }
-
- /* Modify a single byte in the buffer. */
- size_t offset = raw_buf[10] % buf_len;
- uint8_t dirt = raw_buf[11] ? raw_buf[11] : 1;
- raw_buf[offset] ^= dirt;
- /* Now read the BIGNUMs back in from raw_buf. */
- if (BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL ||
- BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL ||
- !verify_ecdsa_sig(api, digest, digest_len, ecdsa_sig, eckey, 0)) {
- goto err;
- }
-
- /* Sanity check: Undo the modification and verify signature. */
- raw_buf[offset] ^= dirt;
- if (BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL ||
- BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL ||
- !verify_ecdsa_sig(api, digest, digest_len, ecdsa_sig, eckey, 1)) {
- goto err;
- }
-
- ret = 1;
-err:
- if (raw_buf) {
- OPENSSL_free(raw_buf);
- }
- return ret;
-}
-
-static int test_builtin(FILE *out) {
- size_t n = 0;
- EC_KEY *eckey = NULL, *wrong_eckey = NULL;
- EC_GROUP *group;
- BIGNUM *order = NULL;
- ECDSA_SIG *ecdsa_sig = NULL;
- uint8_t digest[20], wrong_digest[20];
- uint8_t *signature = NULL;
- int nid, ret = 0;
-
- /* fill digest values with some random data */
- if (!RAND_bytes(digest, 20) || !RAND_bytes(wrong_digest, 20)) {
- fprintf(out, "ERROR: unable to get random data\n");
- goto builtin_err;
- }
-
- order = BN_new();
- if (order == NULL) {
- goto builtin_err;
- }
-
- /* Create and verify ecdsa signatures with every available curve. */
- fputs("\ntesting ECDSA_sign(), ECDSA_verify(), ECDSA_do_sign(), and "
- "ECDSA_do_verify() with some internal curves:\n", out);
-
- static const struct
- {
- int nid;
- const char *name;
- } kCurves[] = {
- { NID_secp224r1, "secp224r1" },
- { NID_X9_62_prime256v1, "secp256r1" },
- { NID_secp384r1, "secp384r1" },
- { NID_secp521r1, "secp521r1" },
- { NID_undef, NULL }
- };
-
- /* now create and verify a signature for every curve */
- for (n = 0; kCurves[n].nid != NID_undef; n++) {
-
- nid = kCurves[n].nid;
- /* create new ecdsa key (== EC_KEY) */
- eckey = EC_KEY_new();
- if (eckey == NULL) {
- goto builtin_err;
- }
- group = EC_GROUP_new_by_curve_name(nid);
- if (group == NULL) {
- goto builtin_err;
- }
- if (!EC_KEY_set_group(eckey, group)) {
- goto builtin_err;
- }
- EC_GROUP_free(group);
- if (!EC_GROUP_get_order(EC_KEY_get0_group(eckey), order, NULL)) {
- goto builtin_err;
- }
- if (BN_num_bits(order) < 160) {
- /* Too small to test. */
- EC_KEY_free(eckey);
- eckey = NULL;
- continue;
- }
-
- fprintf(out, "%s: ", kCurves[n].name);
- /* create key */
- if (!EC_KEY_generate_key(eckey)) {
- fprintf(out, " failed\n");
- goto builtin_err;
- }
- /* create second key */
- wrong_eckey = EC_KEY_new();
- if (wrong_eckey == NULL) {
- goto builtin_err;
- }
- group = EC_GROUP_new_by_curve_name(nid);
- if (group == NULL) {
- goto builtin_err;
- }
- if (!EC_KEY_set_group(wrong_eckey, group)) {
- goto builtin_err;
- }
- EC_GROUP_free(group);
- if (!EC_KEY_generate_key(wrong_eckey)) {
- fprintf(out, " failed\n");
- goto builtin_err;
- }
-
- fprintf(out, ".");
- fflush(out);
- /* check key */
- if (!EC_KEY_check_key(eckey)) {
- fprintf(out, " failed\n");
- goto builtin_err;
- }
- fprintf(out, ".");
- fflush(out);
-
- /* Test ASN.1-encoded signatures. */
- /* Create a signature. */
- unsigned sig_len = ECDSA_size(eckey);
- signature = OPENSSL_malloc(sig_len);
- if (signature == NULL) {
- goto builtin_err;
- }
- if (!ECDSA_sign(0, digest, 20, signature, &sig_len, eckey)) {
- fprintf(out, " failed\n");
- goto builtin_err;
- }
- fprintf(out, ".");
- fflush(out);
- /* verify signature */
- if (!ECDSA_verify(0, digest, 20, signature, sig_len, eckey)) {
- fprintf(out, " failed\n");
- goto builtin_err;
- }
- fprintf(out, ".");
- fflush(out);
- /* verify signature with the wrong key */
- if (ECDSA_verify(0, digest, 20, signature, sig_len, wrong_eckey)) {
- fprintf(out, " failed\n");
- goto builtin_err;
- }
- fprintf(out, ".");
- fflush(out);
- /* wrong digest */
- if (ECDSA_verify(0, wrong_digest, 20, signature, sig_len, eckey)) {
- fprintf(out, " failed\n");
- goto builtin_err;
- }
- fprintf(out, ".");
- fflush(out);
- /* wrong length */
- if (ECDSA_verify(0, digest, 20, signature, sig_len - 1, eckey)) {
- fprintf(out, " failed\n");
- goto builtin_err;
- }
- fprintf(out, ".");
- fflush(out);
- /* Verify a tampered signature. */
- const uint8_t *sig_ptr = signature;
- ecdsa_sig = d2i_ECDSA_SIG(NULL, &sig_ptr, sig_len);
- if (ecdsa_sig == NULL) {
- fprintf(out, " failed\n");
- goto builtin_err;
- }
- if (!test_tampered_sig(out, encoded_api, digest, 20, ecdsa_sig, eckey,
- order)) {
- fprintf(out, " failed\n");
- goto builtin_err;
- }
- fprintf(out, ".");
- fflush(out);
- ECDSA_SIG_free(ecdsa_sig);
- ecdsa_sig = NULL;
- OPENSSL_free(signature);
- signature = NULL;
-
- /* Test ECDSA_SIG signing and verification. */
- /* Create a signature. */
- ecdsa_sig = ECDSA_do_sign(digest, 20, eckey);
- if (!ecdsa_sig) {
- fprintf(out, " failed\n");
- goto builtin_err;
- }
- fprintf(out, ".");
- fflush(out);
- /* Verify the signature using the correct key. */
- if (!ECDSA_do_verify(digest, 20, ecdsa_sig, eckey)) {
- fprintf(out, " failed\n");
- goto builtin_err;
- }
- fprintf(out, ".");
- fflush(out);
- /* Verify the signature with the wrong key. */
- if (ECDSA_do_verify(digest, 20, ecdsa_sig, wrong_eckey)) {
- fprintf(out, " failed\n");
- goto builtin_err;
- }
- fprintf(out, ".");
- fflush(out);
- /* Verify the signature using the wrong digest. */
- if (ECDSA_do_verify(wrong_digest, 20, ecdsa_sig, eckey)) {
- fprintf(out, " failed\n");
- goto builtin_err;
- }
- fprintf(out, ".");
- fflush(out);
- /* Verify a tampered signature. */
- if (!test_tampered_sig(out, raw_api, digest, 20, ecdsa_sig, eckey, order)) {
- fprintf(out, " failed\n");
- goto builtin_err;
- }
- fprintf(out, ".");
- fflush(out);
-
- fprintf(out, " ok\n");
- /* cleanup */
- /* clean bogus errors */
- ERR_clear_error();
- EC_KEY_free(eckey);
- eckey = NULL;
- EC_KEY_free(wrong_eckey);
- wrong_eckey = NULL;
- ECDSA_SIG_free(ecdsa_sig);
- ecdsa_sig = NULL;
- }
-
- ret = 1;
-builtin_err:
- if (eckey) {
- EC_KEY_free(eckey);
- }
- if (order) {
- BN_free(order);
- }
- if (wrong_eckey) {
- EC_KEY_free(wrong_eckey);
- }
- if (ecdsa_sig) {
- ECDSA_SIG_free(ecdsa_sig);
- }
- if (signature) {
- OPENSSL_free(signature);
- }
-
- return ret;
-}
-
-int main(void) {
- int ret = 1;
-
- CRYPTO_library_init();
- ERR_load_crypto_strings();
-
- if (!test_builtin(stdout)) {
- goto err;
- }
-
- ret = 0;
-
-err:
- if (ret) {
- printf("\nECDSA test failed\n");
- } else {
- printf("\nPASS\n");
- }
- if (ret) {
- ERR_print_errors_fp(stdout);
- }
-
- return ret;
-}
diff --git a/crypto/ecdsa/ecdsa_test.cc b/crypto/ecdsa/ecdsa_test.cc
new file mode 100644
index 0000000..a6bd7a1
--- /dev/null
+++ b/crypto/ecdsa/ecdsa_test.cc
@@ -0,0 +1,340 @@
+/* ====================================================================
+ * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. All advertising materials mentioning features or use of this
+ * software must display the following acknowledgment:
+ * "This product includes software developed by the OpenSSL Project
+ * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
+ *
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
+ * endorse or promote products derived from this software without
+ * prior written permission. For written permission, please contact
+ * openssl-core@OpenSSL.org.
+ *
+ * 5. Products derived from this software may not be called "OpenSSL"
+ * nor may "OpenSSL" appear in their names without prior written
+ * permission of the OpenSSL Project.
+ *
+ * 6. Redistributions of any form whatsoever must retain the following
+ * acknowledgment:
+ * "This product includes software developed by the OpenSSL Project
+ * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This product includes cryptographic software written by Eric Young
+ * (eay@cryptsoft.com). This product includes software written by Tim
+ * Hudson (tjh@cryptsoft.com). */
+
+#include <openssl/ecdsa.h>
+
+#include <vector>
+
+#include <openssl/bn.h>
+#include <openssl/crypto.h>
+#include <openssl/ec.h>
+#include <openssl/err.h>
+#include <openssl/mem.h>
+#include <openssl/obj.h>
+#include <openssl/rand.h>
+
+#include "../test/scoped_types.h"
+#include "../test/stl_compat.h"
+
+enum Api {
+ kEncodedApi,
+ kRawApi,
+};
+
+// VerifyECDSASig returns true on success, false on failure.
+static bool VerifyECDSASig(Api api, const uint8_t *digest,
+ size_t digest_len, const ECDSA_SIG *ecdsa_sig,
+ EC_KEY *eckey, int expected_result) {
+ int actual_result;
+
+ switch (api) {
+ case kEncodedApi: {
+ int sig_len = i2d_ECDSA_SIG(ecdsa_sig, NULL);
+ if (sig_len <= 0) {
+ return false;
+ }
+ std::vector<uint8_t> signature(static_cast<size_t>(sig_len));
+ uint8_t *sig_ptr = bssl::vector_data(&signature);
+ sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr);
+ if (sig_len <= 0) {
+ return false;
+ }
+ actual_result = ECDSA_verify(0, digest, digest_len, bssl::vector_data(&signature),
+ signature.size(), eckey);
+ break;
+ }
+
+ case kRawApi:
+ actual_result = ECDSA_do_verify(digest, digest_len, ecdsa_sig, eckey);
+ break;
+
+ default:
+ return false;
+ }
+ return expected_result == actual_result;
+}
+
+// TestTamperedSig verifies that signature verification fails when a valid
+// signature is tampered with. |ecdsa_sig| must be a valid signature, which will
+// be modified. TestTamperedSig returns true on success, false on failure.
+static bool TestTamperedSig(FILE *out, Api api, const uint8_t *digest,
+ size_t digest_len, ECDSA_SIG *ecdsa_sig,
+ EC_KEY *eckey, const BIGNUM *order) {
+ // Modify a single byte of the signature: to ensure we don't
+ // garble the ASN1 structure, we read the raw signature and
+ // modify a byte in one of the bignums directly.
+
+ // Store the two BIGNUMs in raw_buf.
+ size_t r_len = BN_num_bytes(ecdsa_sig->r);
+ size_t s_len = BN_num_bytes(ecdsa_sig->s);
+ size_t bn_len = BN_num_bytes(order);
+ if (r_len > bn_len || s_len > bn_len) {
+ return false;
+ }
+ size_t buf_len = 2 * bn_len;
+ std::vector<uint8_t> raw_buf(buf_len);
+ // Pad the bignums with leading zeroes.
+ if (!BN_bn2bin_padded(bssl::vector_data(&raw_buf), bn_len, ecdsa_sig->r) ||
+ !BN_bn2bin_padded(bssl::vector_data(&raw_buf) + bn_len, bn_len,
+ ecdsa_sig->s)) {
+ return false;
+ }
+
+ // Modify a single byte in the buffer.
+ size_t offset = raw_buf[10] % buf_len;
+ uint8_t dirt = raw_buf[11] ? raw_buf[11] : 1;
+ raw_buf[offset] ^= dirt;
+ // Now read the BIGNUMs back in from raw_buf.
+ if (BN_bin2bn(bssl::vector_data(&raw_buf), bn_len, ecdsa_sig->r) == NULL ||
+ BN_bin2bn(bssl::vector_data(&raw_buf) + bn_len, bn_len,
+ ecdsa_sig->s) == NULL ||
+ !VerifyECDSASig(api, digest, digest_len, ecdsa_sig, eckey, 0)) {
+ return false;
+ }
+
+ // Sanity check: Undo the modification and verify signature.
+ raw_buf[offset] ^= dirt;
+ if (BN_bin2bn(bssl::vector_data(&raw_buf), bn_len, ecdsa_sig->r) == NULL ||
+ BN_bin2bn(bssl::vector_data(&raw_buf) + bn_len, bn_len,
+ ecdsa_sig->s) == NULL ||
+ !VerifyECDSASig(api, digest, digest_len, ecdsa_sig, eckey, 1)) {
+ return false;
+ }
+
+ return true;
+}
+
+static bool TestBuiltin(FILE *out) {
+ // Fill digest values with some random data.
+ uint8_t digest[20], wrong_digest[20];
+ if (!RAND_bytes(digest, 20) || !RAND_bytes(wrong_digest, 20)) {
+ fprintf(out, "ERROR: unable to get random data\n");
+ return false;
+ }
+
+ static const struct {
+ int nid;
+ const char *name;
+ } kCurves[] = {
+ { NID_secp224r1, "secp224r1" },
+ { NID_X9_62_prime256v1, "secp256r1" },
+ { NID_secp384r1, "secp384r1" },
+ { NID_secp521r1, "secp521r1" },
+ { NID_undef, NULL }
+ };
+
+ // Create and verify ECDSA signatures with every available curve.
+ fputs("\ntesting ECDSA_sign(), ECDSA_verify(), ECDSA_do_sign(), and "
+ "ECDSA_do_verify() with some internal curves:\n", out);
+
+ for (size_t n = 0; kCurves[n].nid != NID_undef; n++) {
+ fprintf(out, "%s: ", kCurves[n].name);
+
+ int nid = kCurves[n].nid;
+ ScopedEC_GROUP group(EC_GROUP_new_by_curve_name(nid));
+ if (!group) {
+ fprintf(out, " failed\n");
+ return false;
+ }
+ ScopedBIGNUM order(BN_new());
+ if (!order || !EC_GROUP_get_order(group.get(), order.get(), NULL)) {
+ fprintf(out, " failed\n");
+ return false;
+ }
+ if (BN_num_bits(order.get()) < 160) {
+ // Too small to test.
+ fprintf(out, " skipped\n");
+ continue;
+ }
+
+ // Create a new ECDSA key.
+ ScopedEC_KEY eckey(EC_KEY_new());
+ if (!eckey || !EC_KEY_set_group(eckey.get(), group.get()) ||
+ !EC_KEY_generate_key(eckey.get())) {
+ fprintf(out, " failed\n");
+ return false;
+ }
+ // Create a second key.
+ ScopedEC_KEY wrong_eckey(EC_KEY_new());
+ if (!wrong_eckey || !EC_KEY_set_group(wrong_eckey.get(), group.get()) ||
+ !EC_KEY_generate_key(wrong_eckey.get())) {
+ fprintf(out, " failed\n");
+ return false;
+ }
+
+ fprintf(out, ".");
+ fflush(out);
+
+ // Check the key.
+ if (!EC_KEY_check_key(eckey.get())) {
+ fprintf(out, " failed\n");
+ return false;
+ }
+ fprintf(out, ".");
+ fflush(out);
+
+ // Test ASN.1-encoded signatures.
+ // Create a signature.
+ unsigned sig_len = ECDSA_size(eckey.get());
+ std::vector<uint8_t> signature(sig_len);
+ if (!ECDSA_sign(0, digest, 20, bssl::vector_data(&signature), &sig_len,
+ eckey.get())) {
+ fprintf(out, " failed\n");
+ return false;
+ }
+ signature.resize(sig_len);
+ fprintf(out, ".");
+ fflush(out);
+ // Verify the signature.
+ if (!ECDSA_verify(0, digest, 20, bssl::vector_data(&signature),
+ signature.size(), eckey.get())) {
+ fprintf(out, " failed\n");
+ return false;
+ }
+ fprintf(out, ".");
+ fflush(out);
+ // Verify the signature with the wrong key.
+ if (ECDSA_verify(0, digest, 20, bssl::vector_data(&signature),
+ signature.size(), wrong_eckey.get())) {
+ fprintf(out, " failed\n");
+ return false;
+ }
+ fprintf(out, ".");
+ fflush(out);
+ // Verify the signature using the wrong digest.
+ if (ECDSA_verify(0, wrong_digest, 20, bssl::vector_data(&signature),
+ signature.size(), eckey.get())) {
+ fprintf(out, " failed\n");
+ return false;
+ }
+ fprintf(out, ".");
+ fflush(out);
+ // Verify a truncated signature.
+ if (ECDSA_verify(0, digest, 20, bssl::vector_data(&signature),
+ signature.size() - 1, eckey.get())) {
+ fprintf(out, " failed\n");
+ return false;
+ }
+ fprintf(out, ".");
+ fflush(out);
+ // Verify a tampered signature.
+ const uint8_t *sig_ptr = bssl::vector_data(&signature);
+ ScopedECDSA_SIG ecdsa_sig(d2i_ECDSA_SIG(NULL, &sig_ptr, signature.size()));
+ if (!ecdsa_sig ||
+ !TestTamperedSig(out, kEncodedApi, digest, 20, ecdsa_sig.get(),
+ eckey.get(), order.get())) {
+ fprintf(out, " failed\n");
+ return false;
+ }
+ fprintf(out, ".");
+ fflush(out);
+
+ // Test ECDSA_SIG signing and verification.
+ // Create a signature.
+ ecdsa_sig.reset(ECDSA_do_sign(digest, 20, eckey.get()));
+ if (!ecdsa_sig) {
+ fprintf(out, " failed\n");
+ return false;
+ }
+ fprintf(out, ".");
+ fflush(out);
+ // Verify the signature using the correct key.
+ if (!ECDSA_do_verify(digest, 20, ecdsa_sig.get(), eckey.get())) {
+ fprintf(out, " failed\n");
+ return false;
+ }
+ fprintf(out, ".");
+ fflush(out);
+ // Verify the signature with the wrong key.
+ if (ECDSA_do_verify(digest, 20, ecdsa_sig.get(), wrong_eckey.get())) {
+ fprintf(out, " failed\n");
+ return false;
+ }
+ fprintf(out, ".");
+ fflush(out);
+ // Verify the signature using the wrong digest.
+ if (ECDSA_do_verify(wrong_digest, 20, ecdsa_sig.get(), eckey.get())) {
+ fprintf(out, " failed\n");
+ return false;
+ }
+ fprintf(out, ".");
+ fflush(out);
+ // Verify a tampered signature.
+ if (!TestTamperedSig(out, kRawApi, digest, 20, ecdsa_sig.get(), eckey.get(),
+ order.get())) {
+ fprintf(out, " failed\n");
+ return false;
+ }
+ fprintf(out, ".");
+ fflush(out);
+
+ fprintf(out, " ok\n");
+ // Clear bogus errors.
+ ERR_clear_error();
+ }
+
+ return true;
+}
+
+int main(void) {
+ CRYPTO_library_init();
+ ERR_load_crypto_strings();
+
+ if (!TestBuiltin(stdout)) {
+ printf("\nECDSA test failed\n");
+ ERR_print_errors_fp(stdout);
+ return 1;
+ }
+
+ printf("\nPASS\n");
+ return 0;
+}
diff --git a/crypto/test/scoped_types.h b/crypto/test/scoped_types.h
index bb8359c..d4e6951 100644
--- a/crypto/test/scoped_types.h
+++ b/crypto/test/scoped_types.h
@@ -20,6 +20,9 @@
#include <openssl/bio.h>
#include <openssl/bn.h>
#include <openssl/dh.h>
+#include <openssl/ec.h>
+#include <openssl/ec_key.h>
+#include <openssl/ecdsa.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/mem.h>
@@ -74,6 +77,9 @@
using ScopedBN_CTX = ScopedOpenSSLType<BN_CTX, BN_CTX_free>;
using ScopedBN_MONT_CTX = ScopedOpenSSLType<BN_MONT_CTX, BN_MONT_CTX_free>;
using ScopedDH = ScopedOpenSSLType<DH, DH_free>;
+using ScopedECDSA_SIG = ScopedOpenSSLType<ECDSA_SIG, ECDSA_SIG_free>;
+using ScopedEC_GROUP = ScopedOpenSSLType<EC_GROUP, EC_GROUP_free>;
+using ScopedEC_KEY = ScopedOpenSSLType<EC_KEY, EC_KEY_free>;
using ScopedEVP_PKEY = ScopedOpenSSLType<EVP_PKEY, EVP_PKEY_free>;
using ScopedPKCS8_PRIV_KEY_INFO = ScopedOpenSSLType<PKCS8_PRIV_KEY_INFO,
PKCS8_PRIV_KEY_INFO_free>;