The unified diff between revisions [695f5995..] and [2a00be7d..] is displayed below. It can also be downloaded as a raw diff.

This diff has been restricted to the following files: 'checks/pk.cpp'

#
#
# patch "checks/pk.cpp"
#  from [f87c0201b6b773b3d29c5824810d2043c3d4b6b9]
#    to [29d3ee1e629ecb164b57caf11f80703043ec1bbe]
#
============================================================
--- checks/pk.cpp	f87c0201b6b773b3d29c5824810d2043c3d4b6b9
+++ checks/pk.cpp	29d3ee1e629ecb164b57caf11f80703043ec1bbe
@@ -1,4 +1,3 @@
-/* This file is in the public domain */
 #include <iostream>
 #include <fstream>
 #include <string>
@@ -12,35 +11,24 @@
 #include <botan/dsa.h>
 #include <botan/dh.h>

-#if !defined(BOTAN_NO_NR)
-  #include <botan/nr.h>
-#endif
+#include <botan/nr.h>
+#include <botan/rw.h>
+#include <botan/elgamal.h>
+#include <botan/dlies.h>

-#if !defined(BOTAN_NO_RW)
-  #include <botan/rw.h>
-#endif
-
-#if !defined(BOTAN_NO_ELG)
-  #include <botan/elgamal.h>
-#endif
-
-#if !defined(BOTAN_NO_DLIES)
-  #include <botan/dlies.h>
-#endif
-
 #include <botan/filters.h>
 #include <botan/look_pk.h>
 #include <botan/numthry.h>

 #include <botan/x931_rng.h>
-#include <botan/rng.h>
+#include <botan/libstate.h>
 using namespace Botan;

 #include "common.h"

 static BigInt to_bigint(const std::string& h)
    {
-   return BigInt::decode((const byte*)h.data(),
+   return BigInt::decode(reinterpret_cast<const byte*>(h.data()),
                          h.length(), BigInt::Hexadecimal);
    }

@@ -49,15 +37,14 @@ class Fixed_Output_RNG : public RandomNu
 class Fixed_Output_RNG : public RandomNumberGenerator
    {
    public:
+      bool is_seeded() const { return true; }
+
       byte random()
          {
          if(position < output.size())
             return output[position++];
-         std::cout << "Fixed_Output_RNG: Ran out of bits" << std::endl;
-         std::exit(1);
-         // Annoying: some compilers warn if a return is here (unreachable
-         // code), others warn if it's not (no return from function).
-         /* return 0; */
+
+         throw Botan::Invalid_State("Fixed_Output_RNG: out of bits");
          }
       void randomize(byte out[], u32bit len) throw()
          {
@@ -102,10 +89,7 @@ u32bit do_pk_validation_tests(const std:
    std::ifstream test_data(filename.c_str());

    if(!test_data)
-       {
-       std::cout << "Couldn't open test file " << filename << std::endl;
-       std::exit(1);
-       }
+      throw Botan::Stream_IO_Error("Couldn't open test file " + filename);

    u32bit errors = 0, alg_count = 0;
    std::string algorithm, print_algorithm;
@@ -113,10 +97,8 @@ u32bit do_pk_validation_tests(const std:
    while(!test_data.eof())
       {
       if(test_data.bad() || test_data.fail())
-         {
-         std::cout << "File I/O error." << std::endl;
-         std::exit(1);
-         }
+         throw Botan::Stream_IO_Error("File I/O error reading from " +
+                                      filename);

       std::string line;
       std::getline(test_data, line);
@@ -214,7 +196,7 @@ u32bit do_pk_validation_tests(const std:

    global_state().set_prng(new ANSI_X931_RNG);
    for(u32bit j = 0; j != 2; j++)
-      Global_RNG::seed(true, 384);
+      global_state().seed_prng(true, 384);

    do_pk_keygen_tests();
    do_x509_tests();
@@ -269,7 +251,7 @@ void validate_encryption(PK_Encryptor* e

    global_state().set_prng(new ANSI_X931_RNG);
    for(u32bit j = 0; j != 2; j++)
-      Global_RNG::seed(true, 384);
+      global_state().seed_prng(true, 384);

    validate_decryption(d, algo, out, message, failure);
    delete e;
@@ -310,7 +292,7 @@ void validate_signature(PK_Verifier* v,

    global_state().set_prng(new ANSI_X931_RNG);
    for(u32bit j = 0; j != 2; j++)
-      Global_RNG::seed(true, 384);
+      global_state().seed_prng(true, 384);

    delete v;
    delete s;
@@ -346,9 +328,11 @@ u32bit validate_rsa_enc_pkcs8(const std:
    strip_newlines(pass); /* it will have a newline thanks to the messy
                                 decoding method we use */

-   DataSource_Memory keysource((const byte*)str[0].c_str(), str[0].length());
-   PKCS8_PrivateKey* privkey = PKCS8::load_key(keysource, pass);
+   DataSource_Memory keysource(reinterpret_cast<const byte*>(str[0].c_str()),
+                               str[0].length());

+   Private_Key* privkey = PKCS8::load_key(keysource, pass);
+
    RSA_PrivateKey* rsapriv = dynamic_cast<RSA_PrivateKey*>(privkey);
    if(!rsapriv)
       throw Invalid_Argument("Bad key load for RSA key");
@@ -389,9 +373,6 @@ u32bit validate_elg_enc(const std::strin
 u32bit validate_elg_enc(const std::string& algo,
                         const std::vector<std::string>& str)
    {
-#if defined(BOTAN_NO_ELG)
-   return 0;
-#else
    if(str.size() != 6 && str.size() != 7)
       throw Exception("Invalid input from pk_valid.dat");

@@ -414,7 +395,6 @@ u32bit validate_elg_enc(const std::strin
                           decode_hex(str[4]), failure);

    return (failure ? 1 : 0);
-#endif
    }

 u32bit validate_rsa_sig(const std::string& algo,
@@ -465,9 +445,10 @@ u32bit validate_rsa_ver_x509(const std::
    if(str.size() != 5) /* is actually 3, parse() adds extra empty ones */
       throw Exception("Invalid input from pk_valid.dat");

-   DataSource_Memory keysource((const byte*)str[0].c_str(), str[0].length());
+   DataSource_Memory keysource(reinterpret_cast<const byte*>(str[0].c_str()),
+                               str[0].length());

-   X509_PublicKey* key = X509::load_key(keysource);
+   Public_Key* key = X509::load_key(keysource);

    RSA_PublicKey* rsakey = dynamic_cast<RSA_PublicKey*>(key);

@@ -492,9 +473,6 @@ u32bit validate_rw_ver(const std::string
 u32bit validate_rw_ver(const std::string& algo,
                        const std::vector<std::string>& str)
    {
-#if defined(BOTAN_NO_RW)
-   return 0;
-#else
    if(str.size() != 5)
       throw Exception("Invalid input from pk_valid.dat");

@@ -512,15 +490,11 @@ u32bit validate_rw_ver(const std::string
    delete v;

    return (passed ? 0 : 1);
-#endif
    }

 u32bit validate_rw_sig(const std::string& algo,
                        const std::vector<std::string>& str)
    {
-#if defined(BOTAN_NO_RW)
-   return 0;
-#else
    if(str.size() != 6)
       throw Exception("Invalid input from pk_valid.dat");

@@ -536,7 +510,6 @@ u32bit validate_rw_sig(const std::string
    bool failure = false;
    validate_signature(v, s, algo, str[3], str[4], str[5], failure);
    return (failure ? 1 : 0);
-#endif
    }

 u32bit validate_dsa_sig(const std::string& algo,
@@ -550,9 +523,11 @@ u32bit validate_dsa_sig(const std::strin
    strip_newlines(pass); /* it will have a newline thanks to the messy
                                 decoding method we use */

-   DataSource_Memory keysource((const byte*)str[0].c_str(), str[0].length());
-   PKCS8_PrivateKey* privkey = PKCS8::load_key(keysource, pass);
+   DataSource_Memory keysource(reinterpret_cast<const byte*>(str[0].c_str()),
+                               str[0].length());

+   Private_Key* privkey = PKCS8::load_key(keysource, pass);
+
    DSA_PrivateKey* dsapriv = dynamic_cast<DSA_PrivateKey*>(privkey);
    if(!dsapriv)
       throw Invalid_Argument("Bad key load for DSA private key");
@@ -577,9 +552,11 @@ u32bit validate_dsa_ver(const std::strin
    if(str.size() != 5) /* is actually 3, parse() adds extra empty ones */
       throw Exception("Invalid input from pk_valid.dat");

-   DataSource_Memory keysource((const byte*)str[0].c_str(), str[0].length());
-   X509_PublicKey* key = X509::load_key(keysource);
+   DataSource_Memory keysource(reinterpret_cast<const byte*>(str[0].c_str()),
+                               str[0].length());

+   Public_Key* key = X509::load_key(keysource);
+
    DSA_PublicKey* dsakey = dynamic_cast<DSA_PublicKey*>(key);

    if(!dsakey)
@@ -603,9 +580,6 @@ u32bit validate_nr_sig(const std::string
 u32bit validate_nr_sig(const std::string& algo,
                        const std::vector<std::string>& str)
    {
-#if defined(BOTAN_NO_NR)
-   return 0;
-#else
    if(str.size() != 8)
       throw Exception("Invalid input from pk_valid.dat");

@@ -621,7 +595,6 @@ u32bit validate_nr_sig(const std::string
    bool failure = false;
    validate_signature(v, s, algo, str[5], str[6], str[7], failure);
    return (failure ? 1 : 0);
-#endif
    }

 u32bit validate_dh(const std::string& algo,
@@ -652,9 +625,6 @@ u32bit validate_dlies(const std::string&
 u32bit validate_dlies(const std::string& algo,
                       const std::vector<std::string>& str)
    {
-#if defined(BOTAN_NO_DLIES)
-   return 0;
-#else
    if(str.size() != 6)
       throw Exception("Invalid input from pk_valid.dat");

@@ -682,7 +652,6 @@ u32bit validate_dlies(const std::string&
    bool failure = false;
    validate_encryption(e, d, algo, str[4], empty, str[5], failure);
    return (failure ? 1 : 0);
-#endif
    }

 void do_pk_keygen_tests()
@@ -719,9 +688,7 @@ void do_pk_keygen_tests()
    }

    IF_SIG_KEY(RSA_PrivateKey, 512);
-#if !defined(BOTAN_NO_RW)
    IF_SIG_KEY(RW_PrivateKey, 512);
-#endif

    DL_SIG_KEY(DSA_PrivateKey, "dsa/jce/512");
    DL_SIG_KEY(DSA_PrivateKey, "dsa/jce/768");
@@ -731,17 +698,13 @@ void do_pk_keygen_tests()
    DL_KEY(DH_PrivateKey, "modp/ietf/2048");
    DL_KEY(DH_PrivateKey, "dsa/jce/1024");

-#if !defined(BOTAN_NO_NR)
    DL_SIG_KEY(NR_PrivateKey, "dsa/jce/512");
    DL_SIG_KEY(NR_PrivateKey, "dsa/jce/768");
    DL_SIG_KEY(NR_PrivateKey, "dsa/jce/1024");
-#endif

-#if !defined(BOTAN_NO_ELG)
    DL_ENC_KEY(ElGamal_PrivateKey, "modp/ietf/768");
    DL_ENC_KEY(ElGamal_PrivateKey, "modp/ietf/1024");
    DL_ENC_KEY(ElGamal_PrivateKey, "dsa/jce/1024");
-#endif

    std::cout << std::endl;
    }