diff options
author | Adam Langley <agl@google.com> | 2016-07-12 18:09:33 +0300 |
---|---|---|
committer | Adam Langley <agl@google.com> | 2016-07-12 18:09:33 +0300 |
commit | 10f97f3bfcecc3fbe8e9f02e7c426f7e340f7f54 (patch) | |
tree | 3a23df61db5dc580eeb23916bf8955f5fcedc6cd /crypto | |
parent | d2b5af56cf2a8b4695821b2ff64a7bfc82f3ba5e (diff) |
Revert "Move C++ helpers into |bssl| namespace."
This reverts commit 09feb0f3d95a2bc58ce0faaf14256d3bd30f52a4.
(In order to make WebRTC happy this also needs to be reverted.)
Diffstat (limited to 'crypto')
27 files changed, 234 insertions, 299 deletions
diff --git a/crypto/asn1/asn1_test.cc b/crypto/asn1/asn1_test.cc index e3e310bd..8b024427 100644 --- a/crypto/asn1/asn1_test.cc +++ b/crypto/asn1/asn1_test.cc @@ -21,8 +21,6 @@ #include "../test/scoped_types.h" -namespace bssl { - // kTag128 is an ASN.1 structure with a universal tag with number 128. static const uint8_t kTag128[] = { 0x1f, 0x81, 0x00, 0x01, 0x00, @@ -71,12 +69,10 @@ static bool TestLargeTags() { return true; } -} // namespace bssl - int main() { CRYPTO_library_init(); - if (!bssl::TestLargeTags()) { + if (!TestLargeTags()) { return 1; } diff --git a/crypto/bio/bio_test.cc b/crypto/bio/bio_test.cc index e7c061eb..f2eb20ba 100644 --- a/crypto/bio/bio_test.cc +++ b/crypto/bio/bio_test.cc @@ -43,8 +43,6 @@ OPENSSL_MSVC_PRAGMA(warning(pop)) #include "../test/scoped_types.h" -namespace bssl { - #if !defined(OPENSSL_WINDOWS) static int closesocket(int sock) { return close(sock); @@ -341,7 +339,7 @@ static bool ReadASN1(bool should_succeed, const uint8_t *data, size_t data_len, if (!ok) { out = nullptr; } - ScopedBytes out_storage(out); + ScopedOpenSSLBytes out_storage(out); if (should_succeed != (ok == 1)) { return false; @@ -371,7 +369,7 @@ static bool TestASN1() { static const size_t kLargePayloadLen = 8000; static const uint8_t kLargePrefix[] = {0x30, 0x82, kLargePayloadLen >> 8, kLargePayloadLen & 0xff}; - ScopedBytes large(reinterpret_cast<uint8_t *>( + ScopedOpenSSLBytes large(reinterpret_cast<uint8_t *>( OPENSSL_malloc(sizeof(kLargePrefix) + kLargePayloadLen))); if (!large) { return false; @@ -412,8 +410,6 @@ static bool TestASN1() { return true; } -} // namespace bssl - int main(void) { CRYPTO_library_init(); @@ -432,10 +428,10 @@ int main(void) { } #endif - if (!bssl::TestSocketConnect() || - !bssl::TestPrintf() || - !bssl::TestZeroCopyBioPairs() || - !bssl::TestASN1()) { + if (!TestSocketConnect() || + !TestPrintf() || + !TestZeroCopyBioPairs() || + !TestASN1()) { return 1; } diff --git a/crypto/bn/bn_test.cc b/crypto/bn/bn_test.cc index 3028745c..3405cbdc 100644 --- a/crypto/bn/bn_test.cc +++ b/crypto/bn/bn_test.cc @@ -90,8 +90,6 @@ #include "../crypto/test/test_util.h" -namespace bssl { - static int HexToBIGNUM(ScopedBIGNUM *out, const char *in) { BIGNUM *raw = NULL; int ret = BN_hex2bn(&raw, in); @@ -134,8 +132,8 @@ static bool ExpectBIGNUMsEqual(FileTest *t, const char *operation, return true; } - ScopedString expected_str(BN_bn2hex(expected)); - ScopedString actual_str(BN_bn2hex(actual)); + ScopedOpenSSLString expected_str(BN_bn2hex(expected)); + ScopedOpenSSLString actual_str(BN_bn2hex(actual)); if (!expected_str || !actual_str) { return false; } @@ -999,7 +997,7 @@ static bool TestASN1() { CBB_cleanup(&cbb); return false; } - ScopedBytes delete_der(der); + ScopedOpenSSLBytes delete_der(der); if (der_len != test.der_len || memcmp(der, reinterpret_cast<const uint8_t*>(test.der), der_len) != 0) { fprintf(stderr, "Bad serialization.\n"); @@ -1265,8 +1263,6 @@ static bool TestSmallPrime(BN_CTX *ctx) { return true; } -} // namespace bssl - int main(int argc, char *argv[]) { CRYPTO_library_init(); @@ -1275,24 +1271,24 @@ int main(int argc, char *argv[]) { return 1; } - bssl::ScopedBN_CTX ctx(BN_CTX_new()); + ScopedBN_CTX ctx(BN_CTX_new()); if (!ctx) { return 1; } - if (!bssl::TestBN2BinPadded(ctx.get()) || - !bssl::TestDec2BN(ctx.get()) || - !bssl::TestHex2BN(ctx.get()) || - !bssl::TestASC2BN(ctx.get()) || - !bssl::TestMPI() || - !bssl::TestRand() || - !bssl::TestASN1() || - !bssl::TestNegativeZero(ctx.get()) || - !bssl::TestBadModulus(ctx.get()) || - !bssl::TestExpModZero() || - !bssl::TestSmallPrime(ctx.get())) { + if (!TestBN2BinPadded(ctx.get()) || + !TestDec2BN(ctx.get()) || + !TestHex2BN(ctx.get()) || + !TestASC2BN(ctx.get()) || + !TestMPI() || + !TestRand() || + !TestASN1() || + !TestNegativeZero(ctx.get()) || + !TestBadModulus(ctx.get()) || + !TestExpModZero() || + !TestSmallPrime(ctx.get())) { return 1; } - return bssl::FileTestMain(bssl::RunTest, ctx.get(), argv[1]); + return FileTestMain(RunTest, ctx.get(), argv[1]); } diff --git a/crypto/bytestring/bytestring_test.cc b/crypto/bytestring/bytestring_test.cc index 95d9b85c..31ee51c4 100644 --- a/crypto/bytestring/bytestring_test.cc +++ b/crypto/bytestring/bytestring_test.cc @@ -29,8 +29,6 @@ #include "../test/scoped_types.h" -namespace bssl { - static bool TestSkip() { static const uint8_t kData[] = {1, 2, 3}; CBS data; @@ -294,7 +292,7 @@ static bool TestCBBBasic() { return false; } - ScopedBytes scoper(buf); + ScopedOpenSSLBytes scoper(buf); return buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) == 0; } @@ -339,7 +337,7 @@ static bool TestCBBFinishChild() { CBB_cleanup(&cbb); return false; } - ScopedBytes scoper(out_buf); + ScopedOpenSSLBytes scoper(out_buf); return out_size == 1 && out_buf[0] == 0; } @@ -372,7 +370,7 @@ static bool TestCBBPrefixed() { return false; } - ScopedBytes scoper(buf); + ScopedOpenSSLBytes scoper(buf); return buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) == 0; } @@ -412,7 +410,7 @@ static bool TestCBBDiscardChild() { if (!CBB_finish(cbb.get(), &buf, &buf_len)) { return false; } - ScopedBytes scoper(buf); + ScopedOpenSSLBytes scoper(buf); static const uint8_t kExpected[] = { 0xaa, @@ -458,7 +456,7 @@ static bool TestCBBMisuse() { CBB_cleanup(&cbb); return false; } - ScopedBytes scoper(buf); + ScopedOpenSSLBytes scoper(buf); if (buf_len != 3 || memcmp(buf, "\x01\x01\x02", 3) != 0) { @@ -482,7 +480,7 @@ static bool TestCBBASN1() { CBB_cleanup(&cbb); return false; } - ScopedBytes scoper(buf); + ScopedOpenSSLBytes scoper(buf); if (buf_len != sizeof(kExpected) || memcmp(buf, kExpected, buf_len) != 0) { return false; @@ -557,7 +555,7 @@ static bool DoBerConvert(const char *name, fprintf(stderr, "%s: CBS_asn1_ber_to_der failed.\n", name); return false; } - ScopedBytes scoper(out); + ScopedOpenSSLBytes scoper(out); if (out == NULL) { if (ber_len != der_len || @@ -670,7 +668,7 @@ static bool TestImplicitString() { int ok = CBS_get_asn1_implicit_string(&in, &out, &storage, CBS_ASN1_CONTEXT_SPECIFIC | 0, CBS_ASN1_OCTETSTRING); - ScopedBytes scoper(storage); + ScopedOpenSSLBytes scoper(storage); if (static_cast<bool>(ok) != test.ok) { fprintf(stderr, "CBS_get_asn1_implicit_string unexpectedly %s\n", @@ -749,7 +747,7 @@ static bool TestASN1Uint64() { CBB_cleanup(&cbb); return false; } - ScopedBytes scoper(out); + ScopedOpenSSLBytes scoper(out); if (len != test->encoding_len || memcmp(out, test->encoding, len) != 0) { return false; } @@ -799,29 +797,27 @@ static bool TestCBBReserve() { return true; } -} // namespace bssl - int main(void) { CRYPTO_library_init(); - if (!bssl::TestSkip() || - !bssl::TestGetUint() || - !bssl::TestGetPrefixed() || - !bssl::TestGetPrefixedBad() || - !bssl::TestGetASN1() || - !bssl::TestCBBBasic() || - !bssl::TestCBBFixed() || - !bssl::TestCBBFinishChild() || - !bssl::TestCBBMisuse() || - !bssl::TestCBBPrefixed() || - !bssl::TestCBBDiscardChild() || - !bssl::TestCBBASN1() || - !bssl::TestBerConvert() || - !bssl::TestImplicitString() || - !bssl::TestASN1Uint64() || - !bssl::TestGetOptionalASN1Bool() || - !bssl::TestZero() || - !bssl::TestCBBReserve()) { + if (!TestSkip() || + !TestGetUint() || + !TestGetPrefixed() || + !TestGetPrefixedBad() || + !TestGetASN1() || + !TestCBBBasic() || + !TestCBBFixed() || + !TestCBBFinishChild() || + !TestCBBMisuse() || + !TestCBBPrefixed() || + !TestCBBDiscardChild() || + !TestCBBASN1() || + !TestBerConvert() || + !TestImplicitString() || + !TestASN1Uint64() || + !TestGetOptionalASN1Bool() || + !TestZero() || + !TestCBBReserve()) { return 1; } diff --git a/crypto/cipher/aead_test.cc b/crypto/cipher/aead_test.cc index cf0f24a6..8bad93f5 100644 --- a/crypto/cipher/aead_test.cc +++ b/crypto/cipher/aead_test.cc @@ -25,8 +25,6 @@ #include "../test/scoped_types.h" -namespace bssl { - // This program tests an AEAD against a series of test vectors from a file, // using the FileTest format. As an example, here's a valid test case: // @@ -329,7 +327,7 @@ static const struct KnownAEAD kAEADs[] = { { "", NULL, false }, }; -static int Main(int argc, char **argv) { +int main(int argc, char **argv) { CRYPTO_library_init(); if (argc != 3) { @@ -362,9 +360,3 @@ static int Main(int argc, char **argv) { return FileTestMain(TestAEAD, const_cast<EVP_AEAD*>(aead), argv[2]); } - -} // namespace bssl - -int main(int argc, char **argv) { - return bssl::Main(argc, argv); -} diff --git a/crypto/cipher/cipher_test.cc b/crypto/cipher/cipher_test.cc index 221eb67e..fa384c69 100644 --- a/crypto/cipher/cipher_test.cc +++ b/crypto/cipher/cipher_test.cc @@ -65,8 +65,6 @@ #include "../test/scoped_types.h" -namespace bssl { - static const EVP_CIPHER *GetCipher(const std::string &name) { if (name == "DES-CBC") { return EVP_des_cbc(); @@ -286,8 +284,6 @@ static bool TestCipher(FileTest *t, void *arg) { return true; } -} // namespace bssl - int main(int argc, char **argv) { CRYPTO_library_init(); @@ -296,5 +292,5 @@ int main(int argc, char **argv) { return 1; } - return bssl::FileTestMain(bssl::TestCipher, nullptr, argv[1]); + return FileTestMain(TestCipher, nullptr, argv[1]); } diff --git a/crypto/cmac/cmac_test.cc b/crypto/cmac/cmac_test.cc index 11b7ad69..2496f2a9 100644 --- a/crypto/cmac/cmac_test.cc +++ b/crypto/cmac/cmac_test.cc @@ -22,8 +22,6 @@ #include "../test/test_util.h" -namespace bssl { - static void dump(const uint8_t *got, const uint8_t *want, size_t len) { hexdump(stderr, "got :", got, len); hexdump(stderr, "want:", want, len); @@ -143,10 +141,8 @@ static int rfc_4493_test_vectors(void) { return 1; } -} // namespace bssl - int main(int argc, char **argv) { - if (!bssl::rfc_4493_test_vectors()) { + if (!rfc_4493_test_vectors()) { return 1; } diff --git a/crypto/curve25519/ed25519_test.cc b/crypto/curve25519/ed25519_test.cc index 3c08cc9a..1b6a0b61 100644 --- a/crypto/curve25519/ed25519_test.cc +++ b/crypto/curve25519/ed25519_test.cc @@ -20,8 +20,6 @@ #include "../test/file_test.h" -namespace bssl { - static bool TestSignature(FileTest *t, void *arg) { std::vector<uint8_t> private_key, public_key, message, expected_signature; if (!t->GetBytes(&private_key, "PRIV") || @@ -55,13 +53,11 @@ static bool TestSignature(FileTest *t, void *arg) { return true; } -} // namespace bssl - int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "%s <test input.txt>\n", argv[0]); return 1; } - return bssl::FileTestMain(bssl::TestSignature, nullptr, argv[1]); + return FileTestMain(TestSignature, nullptr, argv[1]); } diff --git a/crypto/curve25519/spake25519_test.cc b/crypto/curve25519/spake25519_test.cc index b8742ed9..d97a8602 100644 --- a/crypto/curve25519/spake25519_test.cc +++ b/crypto/curve25519/spake25519_test.cc @@ -22,8 +22,6 @@ #include "../test/scoped_types.h" -namespace bssl { - struct SPAKE2Run { bool Run() { ScopedSPAKE2_CTX alice(SPAKE2_CTX_new( @@ -156,15 +154,13 @@ static bool TestCorruptMessages() { return true; } -} // namespace bssl - /* TODO(agl): add tests with fixed vectors once SPAKE2 is nailed down. */ int main(int argc, char **argv) { - if (!bssl::TestSPAKE2() || - !bssl::TestWrongPassword() || - !bssl::TestWrongNames() || - !bssl::TestCorruptMessages()) { + if (!TestSPAKE2() || + !TestWrongPassword() || + !TestWrongNames() || + !TestCorruptMessages()) { return 1; } diff --git a/crypto/dh/dh_test.cc b/crypto/dh/dh_test.cc index 8fe496c9..1c244283 100644 --- a/crypto/dh/dh_test.cc +++ b/crypto/dh/dh_test.cc @@ -71,7 +71,25 @@ #include "../test/scoped_types.h" -namespace bssl { +static bool RunBasicTests(); +static bool RunRFC5114Tests(); +static bool TestBadY(); +static bool TestASN1(); + +int main(int argc, char *argv[]) { + CRYPTO_library_init(); + + if (!RunBasicTests() || + !RunRFC5114Tests() || + !TestBadY() || + !TestASN1()) { + ERR_print_errors_fp(stderr); + return 1; + } + + printf("PASS\n"); + return 0; +} static int GenerateCallback(int p, int n, BN_GENCB *arg) { char c = '*'; @@ -557,7 +575,7 @@ static bool TestASN1() { !CBB_finish(cbb.get(), &der, &der_len)) { return false; } - ScopedBytes free_der(der); + ScopedOpenSSLBytes free_der(der); if (der_len != sizeof(kParams) || memcmp(der, kParams, der_len) != 0) { return false; } @@ -599,27 +617,10 @@ static bool TestASN1() { !CBB_finish(cbb.get(), &der, &der_len)) { return false; } - ScopedBytes free_der2(der); + ScopedOpenSSLBytes free_der2(der); if (der_len != sizeof(kParamsDSA) || memcmp(der, kParamsDSA, der_len) != 0) { return false; } return true; } - -} // namespace bssl - -int main(int argc, char *argv[]) { - CRYPTO_library_init(); - - if (!bssl::RunBasicTests() || - !bssl::RunRFC5114Tests() || - !bssl::TestBadY() || - !bssl::TestASN1()) { - ERR_print_errors_fp(stderr); - return 1; - } - - printf("PASS\n"); - return 0; -} diff --git a/crypto/digest/digest_test.cc b/crypto/digest/digest_test.cc index 32d12d24..39ceaffc 100644 --- a/crypto/digest/digest_test.cc +++ b/crypto/digest/digest_test.cc @@ -26,8 +26,6 @@ #include "../test/scoped_types.h" -namespace bssl { - struct MD { // name is the name of the digest. const char* name; @@ -245,7 +243,7 @@ static int TestGetters() { return true; } -static int Main() { +int main(void) { CRYPTO_library_init(); for (size_t i = 0; i < sizeof(kTestVectors) / sizeof(kTestVectors[0]); i++) { @@ -262,9 +260,3 @@ static int Main() { printf("PASS\n"); return 0; } - -} // namespace bssl - -int main() { - return bssl::Main(); -} diff --git a/crypto/ec/ec_test.cc b/crypto/ec/ec_test.cc index 864cd499..23befeb2 100644 --- a/crypto/ec/ec_test.cc +++ b/crypto/ec/ec_test.cc @@ -26,8 +26,6 @@ #include "../test/scoped_types.h" -namespace bssl { - // kECKeyWithoutPublic is an ECPrivateKey with the optional publicKey field // omitted. static const uint8_t kECKeyWithoutPublic[] = { @@ -163,8 +161,8 @@ static bool Testd2i_ECPrivateKey() { fprintf(stderr, "Failed to get public key in affine coordinates.\n"); return false; } - ScopedString x_hex(BN_bn2hex(x.get())); - ScopedString y_hex(BN_bn2hex(y.get())); + ScopedOpenSSLString x_hex(BN_bn2hex(x.get())); + ScopedOpenSSLString y_hex(BN_bn2hex(y.get())); if (!x_hex || !y_hex) { return false; } @@ -473,17 +471,15 @@ static bool ForEachCurve(bool (*test_func)(int nid)) { return true; } -} // namespace bssl - int main(void) { CRYPTO_library_init(); - if (!bssl::Testd2i_ECPrivateKey() || - !bssl::TestZeroPadding() || - !bssl::TestSpecifiedCurve() || - !bssl::ForEachCurve(bssl::TestSetAffine) || - !bssl::ForEachCurve(bssl::TestAddingEqualPoints) || - !bssl::TestArbitraryCurve()) { + if (!Testd2i_ECPrivateKey() || + !TestZeroPadding() || + !TestSpecifiedCurve() || + !ForEachCurve(TestSetAffine) || + !ForEachCurve(TestAddingEqualPoints) || + !TestArbitraryCurve()) { fprintf(stderr, "failed\n"); return 1; } diff --git a/crypto/ecdsa/ecdsa_test.cc b/crypto/ecdsa/ecdsa_test.cc index ad2325b6..8d7827df 100644 --- a/crypto/ecdsa/ecdsa_test.cc +++ b/crypto/ecdsa/ecdsa_test.cc @@ -64,9 +64,6 @@ #include "../test/scoped_types.h" - -namespace bssl { - enum Api { kEncodedApi, kRawApi, @@ -85,7 +82,7 @@ static bool VerifyECDSASig(Api api, const uint8_t *digest, if (!ECDSA_SIG_to_bytes(&der, &der_len, ecdsa_sig)) { return false; } - ScopedBytes delete_der(der); + ScopedOpenSSLBytes delete_der(der); actual_result = ECDSA_verify(0, digest, digest_len, der, der_len, eckey); break; } @@ -331,7 +328,7 @@ static bool TestECDSA_SIG_max_len(size_t order_len) { if (!ECDSA_SIG_to_bytes(&der, &der_len, sig.get())) { return false; } - ScopedBytes delete_der(der); + ScopedOpenSSLBytes delete_der(der); size_t max_len = ECDSA_SIG_max_len(order_len); if (max_len != der_len) { @@ -343,8 +340,6 @@ static bool TestECDSA_SIG_max_len(size_t order_len) { return true; } -} // namespace bssl - static size_t BitsToBytes(size_t bits) { return (bits / 8) + (7 + (bits % 8)) / 8; } @@ -352,12 +347,12 @@ static size_t BitsToBytes(size_t bits) { int main(void) { CRYPTO_library_init(); - if (!bssl::TestBuiltin(stdout) || - !bssl::TestECDSA_SIG_max_len(BitsToBytes(224)) || - !bssl::TestECDSA_SIG_max_len(BitsToBytes(256)) || - !bssl::TestECDSA_SIG_max_len(BitsToBytes(384)) || - !bssl::TestECDSA_SIG_max_len(BitsToBytes(521)) || - !bssl::TestECDSA_SIG_max_len(BitsToBytes(10000))) { + if (!TestBuiltin(stdout) || + !TestECDSA_SIG_max_len(BitsToBytes(224)) || + !TestECDSA_SIG_max_len(BitsToBytes(256)) || + !TestECDSA_SIG_max_len(BitsToBytes(384)) || + !TestECDSA_SIG_max_len(BitsToBytes(521)) || + !TestECDSA_SIG_max_len(BitsToBytes(10000))) { printf("\nECDSA test failed\n"); ERR_print_errors_fp(stdout); return 1; diff --git a/crypto/evp/evp_extra_test.cc b/crypto/evp/evp_extra_test.cc index 2253c56b..9bc36ad8 100644 --- a/crypto/evp/evp_extra_test.cc +++ b/crypto/evp/evp_extra_test.cc @@ -30,8 +30,6 @@ #include "../test/scoped_types.h" -namespace bssl { - // kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you // should never use this key anywhere but in an example. static const uint8_t kExampleRSAKeyDER[] = { @@ -673,7 +671,7 @@ static bool Testd2i_PrivateKey(void) { return true; } -static int Main() { +int main(void) { CRYPTO_library_init(); if (!TestEVP_DigestSignInit()) { @@ -721,9 +719,3 @@ static int Main() { printf("PASS\n"); return 0; } - -} // namespace bssl - -int main() { - return bssl::Main(); -} diff --git a/crypto/evp/evp_test.cc b/crypto/evp/evp_test.cc index 9b8a6839..b01d1e47 100644 --- a/crypto/evp/evp_test.cc +++ b/crypto/evp/evp_test.cc @@ -77,8 +77,6 @@ OPENSSL_MSVC_PRAGMA(warning(pop)) #include "../test/scoped_types.h" -namespace bssl { - // evp_test dispatches between multiple test types. PrivateKey tests take a key // name parameter and single block, decode it as a PEM private key, and save it // under that key name. Decrypt, Sign, and Verify tests take a previously @@ -151,7 +149,7 @@ static bool ImportKey(FileTest *t, KeyMap *key_map, !CBB_finish(cbb.get(), &der, &der_len)) { return false; } - ScopedBytes free_der(der); + ScopedOpenSSLBytes free_der(der); std::vector<uint8_t> output = input; if (t->HasAttribute("Output") && @@ -255,8 +253,6 @@ static bool TestEVP(FileTest *t, void *arg) { return true; } -} // namespace bssl - int main(int argc, char **argv) { CRYPTO_library_init(); if (argc != 2) { @@ -264,6 +260,6 @@ int main(int argc, char **argv) { return 1; } - bssl::KeyMap map; - return bssl::FileTestMain(bssl::TestEVP, &map, argv[1]); + KeyMap map; + return FileTestMain(TestEVP, &map, argv[1]); } diff --git a/crypto/hmac/hmac_test.cc b/crypto/hmac/hmac_test.cc index ba84cc22..da390ef8 100644 --- a/crypto/hmac/hmac_test.cc +++ b/crypto/hmac/hmac_test.cc @@ -68,8 +68,6 @@ #include "../test/scoped_types.h" -namespace bssl { - static const EVP_MD *GetDigest(const std::string &name) { if (name == "MD5") { return EVP_md5(); @@ -154,8 +152,6 @@ static bool TestHMAC(FileTest *t, void *arg) { return true; } -} // namespace bssl - int main(int argc, char *argv[]) { CRYPTO_library_init(); @@ -164,5 +160,5 @@ int main(int argc, char *argv[]) { return 1; } - return bssl::FileTestMain(bssl::TestHMAC, nullptr, argv[1]); + return FileTestMain(TestHMAC, nullptr, argv[1]); } diff --git a/crypto/newhope/newhope_statistical_test.cc b/crypto/newhope/newhope_statistical_test.cc index d3a54430..44fac48a 100644 --- a/crypto/newhope/newhope_statistical_test.cc +++ b/crypto/newhope/newhope_statistical_test.cc @@ -25,8 +25,6 @@ #include "internal.h" -namespace bssl { - static const unsigned kNumTests = 1000; static bool TestNoise(void) { @@ -148,11 +146,9 @@ static bool TestKeys(void) { return true; } -} // namespace bssl - int main(void) { - if (!bssl::TestKeys() || - !bssl::TestNoise()) { + if (!TestKeys() || + !TestNoise()) { return 1; } printf("PASS\n"); diff --git a/crypto/newhope/newhope_test.cc b/crypto/newhope/newhope_test.cc index d94eee8f..6637393f 100644 --- a/crypto/newhope/newhope_test.cc +++ b/crypto/newhope/newhope_test.cc @@ -23,8 +23,6 @@ #include "internal.h" -namespace bssl { - // Set to 10 for quick execution. Tested up to 1,000,000. static const int kNumTests = 10; @@ -130,13 +128,11 @@ static bool TestInvalidAcceptMsg(void) { return true; } -} // namespace bssl - int main(void) { - for (int i = 0; i < bssl::kNumTests; i++) { - if (!bssl::TestKeys() || - !bssl::TestInvalidSK() || - !bssl::TestInvalidAcceptMsg()) { + for (int i = 0; i < kNumTests; i++) { + if (!TestKeys() || + !TestInvalidSK() || + !TestInvalidAcceptMsg()) { return 1; } } diff --git a/crypto/newhope/newhope_vectors_test.cc b/crypto/newhope/newhope_vectors_test.cc index 691d693b..fe84cd4b 100644 --- a/crypto/newhope/newhope_vectors_test.cc +++ b/crypto/newhope/newhope_vectors_test.cc @@ -24,8 +24,6 @@ #include "internal.h" -namespace bssl { - static bool TestNewhope(FileTest *t, void *arg) { ScopedNEWHOPE_POLY a(NEWHOPE_POLY_new()); ScopedNEWHOPE_POLY s(NEWHOPE_POLY_new()), sp(NEWHOPE_POLY_new()); @@ -112,8 +110,6 @@ static bool TestNewhope(FileTest *t, void *arg) { } } -} // namespace bssl - int main(int argc, char **argv) { CRYPTO_library_init(); @@ -122,5 +118,5 @@ int main(int argc, char **argv) { return 1; } - return bssl::FileTestMain(bssl::TestNewhope, nullptr, argv[1]); + return FileTestMain(TestNewhope, nullptr, argv[1]); } diff --git a/crypto/pkcs8/pkcs12_test.cc b/crypto/pkcs8/pkcs12_test.cc index 10694446..17bcd273 100644 --- a/crypto/pkcs8/pkcs12_test.cc +++ b/crypto/pkcs8/pkcs12_test.cc @@ -681,8 +681,6 @@ static const uint8_t kWindows[] = { 0xfe, 0x3a, 0x66, 0x47, 0x40, 0x49, 0x02, 0x02, 0x07, 0xd0, }; -namespace bssl { - static const char kPassword[] = "foo"; static bool Test(const char *name, const uint8_t *der, size_t der_len) { @@ -757,15 +755,13 @@ static bool TestCompat(const uint8_t *der, size_t der_len) { return true; } -} // namespace bssl - int main(int argc, char **argv) { CRYPTO_library_init(); - if (!bssl::Test("OpenSSL", kOpenSSL, sizeof(kOpenSSL)) || - !bssl::Test("NSS", kNSS, sizeof(kNSS)) || - !bssl::Test("Windows", kWindows, sizeof(kWindows)) || - !bssl::TestCompat(kWindows, sizeof(kWindows))) { + if (!Test("OpenSSL", kOpenSSL, sizeof(kOpenSSL)) || + !Test("NSS", kNSS, sizeof(kNSS)) || + !Test("Windows", kWindows, sizeof(kWindows)) || + !TestCompat(kWindows, sizeof(kWindows))) { return 1; } diff --git a/crypto/pkcs8/pkcs8_test.cc b/crypto/pkcs8/pkcs8_test.cc index e54a6997..7a88ddf4 100644 --- a/crypto/pkcs8/pkcs8_test.cc +++ b/crypto/pkcs8/pkcs8_test.cc @@ -62,9 +62,7 @@ static const uint8_t kDER[] = { 0xd6, 0x2d, }; -namespace bssl { - -static bool Test(const uint8_t *der, size_t der_len) { +static bool test(const uint8_t *der, size_t der_len) { const uint8_t *data = der; ScopedX509_SIG sig(d2i_X509_SIG(NULL, &data, der_len)); if (sig.get() == NULL || data != der + der_len) { @@ -83,10 +81,8 @@ static bool Test(const uint8_t *der, size_t der_len) { return true; } -} // namespace bssl - int main(int argc, char **argv) { - if (!bssl::Test(kDER, sizeof(kDER))) { + if (!test(kDER, sizeof(kDER))) { return 1; } diff --git a/crypto/poly1305/poly1305_test.cc b/crypto/poly1305/poly1305_test.cc index a3272250..2c25e93b 100644 --- a/crypto/poly1305/poly1305_test.cc +++ b/crypto/poly1305/poly1305_test.cc @@ -24,8 +24,6 @@ #include "../test/file_test.h" -namespace bssl { - static bool TestSIMD(FileTest *t, unsigned excess, const std::vector<uint8_t> &key, const std::vector<uint8_t> &in, @@ -121,8 +119,6 @@ static bool TestPoly1305(FileTest *t, void *arg) { return true; } -} // namespace bssl - int main(int argc, char **argv) { CRYPTO_library_init(); @@ -131,5 +127,5 @@ int main(int argc, char **argv) { return 1; } - return bssl::FileTestMain(bssl::TestPoly1305, nullptr, argv[1]); + return FileTestMain(TestPoly1305, nullptr, argv[1]); } diff --git a/crypto/rsa/rsa_test.cc b/crypto/rsa/rsa_test.cc index 5307f2cb..62177a4c 100644 --- a/crypto/rsa/rsa_test.cc +++ b/crypto/rsa/rsa_test.cc @@ -523,8 +523,6 @@ static const uint8_t kExponent1RSAKey[] = { 0xdd, 0x02, 0x01, 0x01, }; -namespace bssl { - static bool TestRSA(const uint8_t *der, size_t der_len, const uint8_t *oaep_ciphertext, size_t oaep_ciphertext_len) { @@ -857,7 +855,7 @@ static bool TestASN1() { if (!RSA_private_key_to_bytes(&der, &der_len, rsa.get())) { return false; } - ScopedBytes delete_der(der); + ScopedOpenSSLBytes delete_der(der); if (der_len != sizeof(kKey1) - 1 || memcmp(der, kKey1, der_len) != 0) { return false; } @@ -880,7 +878,7 @@ static bool TestASN1() { if (!RSA_public_key_to_bytes(&der2, &der2_len, rsa.get())) { return false; } - ScopedBytes delete_der2(der2); + ScopedOpenSSLBytes delete_der2(der2); if (der_len != der2_len || memcmp(der, der2, der_len) != 0) { return false; } @@ -924,32 +922,30 @@ static bool TestBadExponent() { return true; } -} // namespace bssl - int main(int argc, char *argv[]) { CRYPTO_library_init(); - if (!bssl::TestRSA(kKey1, sizeof(kKey1) - 1, kOAEPCiphertext1, - sizeof(kOAEPCiphertext1) - 1) || - !bssl::TestRSA(kKey2, sizeof(kKey2) - 1, kOAEPCiphertext2, - sizeof(kOAEPCiphertext2) - 1) || - !bssl::TestRSA(kKey3, sizeof(kKey3) - 1, kOAEPCiphertext3, - sizeof(kOAEPCiphertext3) - 1) || - !bssl::TestOnlyDGiven() || - !bssl::TestRecoverCRTParams() || - !bssl::TestBadKey() || - !bssl::TestMultiPrimeKey(2, kTwoPrimeKey, sizeof(kTwoPrimeKey) - 1, - kTwoPrimeEncryptedMessage, - sizeof(kTwoPrimeEncryptedMessage)) || - !bssl::TestMultiPrimeKey(3, kThreePrimeKey, sizeof(kThreePrimeKey) - 1, - kThreePrimeEncryptedMessage, - sizeof(kThreePrimeEncryptedMessage)) || - !bssl::TestMultiPrimeKey(6, kSixPrimeKey, sizeof(kSixPrimeKey) - 1, - kSixPrimeEncryptedMessage, - sizeof(kSixPrimeEncryptedMessage)) || - !bssl::TestMultiPrimeKeygen() || - !bssl::TestASN1() || - !bssl::TestBadExponent()) { + if (!TestRSA(kKey1, sizeof(kKey1) - 1, kOAEPCiphertext1, + sizeof(kOAEPCiphertext1) - 1) || + !TestRSA(kKey2, sizeof(kKey2) - 1, kOAEPCiphertext2, + sizeof(kOAEPCiphertext2) - 1) || + !TestRSA(kKey3, sizeof(kKey3) - 1, kOAEPCiphertext3, + sizeof(kOAEPCiphertext3) - 1) || + !TestOnlyDGiven() || + !TestRecoverCRTParams() || + !TestBadKey() || + !TestMultiPrimeKey(2, kTwoPrimeKey, sizeof(kTwoPrimeKey) - 1, + kTwoPrimeEncryptedMessage, + sizeof(kTwoPrimeEncryptedMessage)) || + !TestMultiPrimeKey(3, kThreePrimeKey, sizeof(kThreePrimeKey) - 1, + kThreePrimeEncryptedMessage, + sizeof(kThreePrimeEncryptedMessage)) || + !TestMultiPrimeKey(6, kSixPrimeKey, sizeof(kSixPrimeKey) - 1, + kSixPrimeEncryptedMessage, + sizeof(kSixPrimeEncryptedMessage)) || + !TestMultiPrimeKeygen() || + !TestASN1() || + !TestBadExponent()) { return 1; } diff --git a/crypto/test/file_test.cc b/crypto/test/file_test.cc index 7e99316d..d684aa09 100644 --- a/crypto/test/file_test.cc +++ b/crypto/test/file_test.cc @@ -25,8 +25,6 @@ #include <openssl/err.h> -namespace bssl { - FileTest::FileTest(const char *path) { file_ = fopen(path, "r"); if (file_ == nullptr) { @@ -302,5 +300,3 @@ int FileTestMain(bool (*run_test)(FileTest *t, void *arg), void *arg, printf("PASS\n"); return 0; } - -} // namespace bssl diff --git a/crypto/test/file_test.h b/crypto/test/file_test.h index 447b1ee6..a8591277 100644 --- a/crypto/test/file_test.h +++ b/crypto/test/file_test.h @@ -30,9 +30,6 @@ OPENSSL_MSVC_PRAGMA(warning(disable: 4702)) OPENSSL_MSVC_PRAGMA(warning(pop)) - -namespace bssl { - // File-based test framework. // // This module provides a file-based test framework. The file format is based on @@ -156,7 +153,5 @@ class FileTest { int FileTestMain(bool (*run_test)(FileTest *t, void *arg), void *arg, const char *path); -} // namespace bssl - #endif /* OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H */ diff --git a/crypto/test/scoped_types.h b/crypto/test/scoped_types.h index 7e4a48e8..2a2c3713 100644 --- a/crypto/test/scoped_types.h +++ b/crypto/test/scoped_types.h @@ -15,8 +15,6 @@ #ifndef OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H #define OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H -#include <openssl/base.h> - #include <stdint.h> #include <stdio.h> @@ -42,7 +40,28 @@ #include <openssl/stack.h> #include <openssl/x509.h> -namespace bssl { + +template<typename T, void (*func)(T*)> +struct OpenSSLDeleter { + void operator()(T *obj) { + func(obj); + } +}; + +template<typename StackType, typename T, void (*func)(T*)> +struct OpenSSLStackDeleter { + void operator()(StackType *obj) { + sk_pop_free(reinterpret_cast<_STACK*>(obj), + reinterpret_cast<void (*)(void *)>(func)); + } +}; + +template<typename T> +struct OpenSSLFree { + void operator()(T *buf) { + OPENSSL_free(buf); + } +}; struct FileCloser { void operator()(FILE *file) { @@ -50,48 +69,77 @@ struct FileCloser { } }; -using ScopedASN1_TYPE = ScopedType<ASN1_TYPE, ASN1_TYPE_free>; -using ScopedBIO = ScopedType<BIO, BIO_vfree>; -using ScopedBIGNUM = ScopedType<BIGNUM, BN_free>; -using ScopedBN_CTX = ScopedType<BN_CTX, BN_CTX_free>; -using ScopedBN_MONT_CTX = ScopedType<BN_MONT_CTX, BN_MONT_CTX_free>; -using ScopedCMAC_CTX = ScopedType<CMAC_CTX, CMAC_CTX_free>; -using ScopedDH = ScopedType<DH, DH_free>; -using ScopedECDSA_SIG = ScopedType<ECDSA_SIG, ECDSA_SIG_free>; -using ScopedEC_GROUP = ScopedType<EC_GROUP, EC_GROUP_free>; -using ScopedEC_KEY = ScopedType<EC_KEY, EC_KEY_free>; -using ScopedEC_POINT = ScopedType<EC_POINT, EC_POINT_free>; -using ScopedEVP_PKEY = ScopedType<EVP_PKEY, EVP_PKEY_free>; -using ScopedEVP_PKEY_CTX = ScopedType<EVP_PKEY_CTX, EVP_PKEY_CTX_free>; -using ScopedNEWHOPE_POLY = ScopedType<NEWHOPE_POLY, NEWHOPE_POLY_free>; -using ScopedPKCS8_PRIV_KEY_INFO = - ScopedType<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free>; -using ScopedPKCS12 = ScopedType<PKCS12, PKCS12_free>; -using ScopedSPAKE2_CTX = ScopedType<SPAKE2_CTX, SPAKE2_CTX_free>; -using ScopedRSA = ScopedType<RSA, RSA_free>; -using ScopedX509 = ScopedType<X509, X509_free>; -using ScopedX509_ALGOR = ScopedType<X509_ALGOR, X509_ALGOR_free>; -using ScopedX509_SIG = ScopedType<X509_SIG, X509_SIG_free>; -using ScopedX509_STORE_CTX = ScopedType<X509_STORE_CTX, X509_STORE_CTX_free>; - -using ScopedX509Stack = ScopedStack<STACK_OF(X509), X509, X509_free>; - -using ScopedCBB = ScopedContext<CBB, void, CBB_zero, CBB_cleanup>; -using ScopedEVP_AEAD_CTX = - ScopedContext<EVP_AEAD_CTX, void, EVP_AEAD_CTX_zero, EVP_AEAD_CTX_cleanup>; -using ScopedEVP_CIPHER_CTX = - ScopedContext<EVP_CIPHER_CTX, int, EVP_CIPHER_CTX_init, - EVP_CIPHER_CTX_cleanup>; -using ScopedEVP_MD_CTX = - ScopedContext<EVP_MD_CTX, int, EVP_MD_CTX_init, EVP_MD_CTX_cleanup>; -using ScopedHMAC_CTX = - ScopedContext<HMAC_CTX, void, HMAC_CTX_init, HMAC_CTX_cleanup>; - -using ScopedBytes = std::unique_ptr<uint8_t, Free<uint8_t>>; -using ScopedString = std::unique_ptr<char, Free<char>>; +template<typename T, void (*func)(T*)> +using ScopedOpenSSLType = std::unique_ptr<T, OpenSSLDeleter<T, func>>; -using ScopedFILE = std::unique_ptr<FILE, FileCloser>; +template<typename StackType, typename T, void (*func)(T*)> +using ScopedOpenSSLStack = + std::unique_ptr<StackType, OpenSSLStackDeleter<StackType, T, func>>; + +template<typename T, typename CleanupRet, void (*init_func)(T*), + CleanupRet (*cleanup_func)(T*)> +class ScopedOpenSSLContext { + public: + ScopedOpenSSLContext() { + init_func(&ctx_); + } + ~ScopedOpenSSLContext() { + cleanup_func(&ctx_); + } + + T *get() { return &ctx_; } + const T *get() const { return &ctx_; } + + void Reset() { + cleanup_func(&ctx_); + init_func(&ctx_); + } + + private: + T ctx_; +}; + +using ScopedASN1_TYPE = ScopedOpenSSLType<ASN1_TYPE, ASN1_TYPE_free>; +using ScopedBIO = ScopedOpenSSLType<BIO, BIO_vfree>; +using ScopedBIGNUM = ScopedOpenSSLType<BIGNUM, BN_free>; +using ScopedBN_CTX = ScopedOpenSSLType<BN_CTX, BN_CTX_free>; +using ScopedBN_MONT_CTX = ScopedOpenSSLType<BN_MONT_CTX, BN_MONT_CTX_free>; +using ScopedCMAC_CTX = ScopedOpenSSLType<CMAC_CTX, CMAC_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 ScopedEC_POINT = ScopedOpenSSLType<EC_POINT, EC_POINT_free>; +using ScopedEVP_PKEY = ScopedOpenSSLType<EVP_PKEY, EVP_PKEY_free>; +using ScopedEVP_PKEY_CTX = ScopedOpenSSLType<EVP_PKEY_CTX, EVP_PKEY_CTX_free>; +using ScopedNEWHOPE_POLY = ScopedOpenSSLType<NEWHOPE_POLY, NEWHOPE_POLY_free>; +using ScopedPKCS8_PRIV_KEY_INFO = ScopedOpenSSLType<PKCS8_PRIV_KEY_INFO, + PKCS8_PRIV_KEY_INFO_free>; +using ScopedPKCS12 = ScopedOpenSSLType<PKCS12, PKCS12_free>; +using ScopedSPAKE2_CTX = ScopedOpenSSLType<SPAKE2_CTX, SPAKE2_CTX_free>; +using ScopedRSA = ScopedOpenSSLType<RSA, RSA_free>; +using ScopedX509 = ScopedOpenSSLType<X509, X509_free>; +using ScopedX509_ALGOR = ScopedOpenSSLType<X509_ALGOR, X509_ALGOR_free>; +using ScopedX509_SIG = ScopedOpenSSLType<X509_SIG, X509_SIG_free>; +using ScopedX509_STORE_CTX = ScopedOpenSSLType<X509_STORE_CTX, X509_STORE_CTX_free>; + +using ScopedX509Stack = ScopedOpenSSLStack<STACK_OF(X509), X509, X509_free>; -} // namespace bssl +using ScopedCBB = ScopedOpenSSLContext<CBB, void, CBB_zero, CBB_cleanup>; +using ScopedEVP_AEAD_CTX = ScopedOpenSSLContext<EVP_AEAD_CTX, void, + EVP_AEAD_CTX_zero, + EVP_AEAD_CTX_cleanup>; +using ScopedEVP_CIPHER_CTX = ScopedOpenSSLContext<EVP_CIPHER_CTX, int, + EVP_CIPHER_CTX_init, + EVP_CIPHER_CTX_cleanup>; +using ScopedEVP_MD_CTX = ScopedOpenSSLContext<EVP_MD_CTX, int, EVP_MD_CTX_init, + EVP_MD_CTX_cleanup>; +using ScopedHMAC_CTX = ScopedOpenSSLContext<HMAC_CTX, void, HMAC_CTX_init, + HMAC_CTX_cleanup>; + +using ScopedOpenSSLBytes = std::unique_ptr<uint8_t, OpenSSLFree<uint8_t>>; +using ScopedOpenSSLString = std::unique_ptr<char, OpenSSLFree<char>>; + +using ScopedFILE = std::unique_ptr<FILE, FileCloser>; #endif // OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H diff --git a/crypto/x509/x509_test.cc b/crypto/x509/x509_test.cc index cb28df70..650163a8 100644 --- a/crypto/x509/x509_test.cc +++ b/crypto/x509/x509_test.cc @@ -27,8 +27,6 @@ #include "../test/scoped_types.h" -namespace bssl { - static const char kCrossSigningRootPEM[] = "-----BEGIN CERTIFICATE-----\n" "MIICcTCCAdqgAwIBAgIIagJHiPvE0MowDQYJKoZIhvcNAQELBQAwPDEaMBgGA1UE\n" @@ -459,15 +457,13 @@ static bool TestSignCtx() { return true; } -} // namespace bssl - int main(int argc, char **argv) { CRYPTO_library_init(); - if (!bssl::TestVerify() || - !bssl::TestPSS() || - !bssl::TestBadPSSParameters() || - !bssl::TestSignCtx()) { + if (!TestVerify() || + !TestPSS() || + !TestBadPSSParameters() || + !TestSignCtx()) { return 1; } |