From a8ee54d459a42d98fdfe1e9ff4f0c011c2f41e10 Mon Sep 17 00:00:00 2001 From: lloyd Date: Sun, 26 Oct 2008 03:07:18 +0000 Subject: Move rng.{cpp,h} from core to rng/ topdir Add a new class AutoSeeded_RNG that is a RandomNumberGenerator that wraps up the logic formerly in RandomNumberGenerator::make_rng. make_rng in fact now just returns a new AutoSeeded_RNG object. AutoSeeded_RNG is a bit more convenient because - No need to use auto_ptr - No need to dereference (same syntax everywhere - it's an underestimated advantage imo) Also move the code from timer/timer_base to timer/ --- doc/examples/ca.cpp | 8 +++----- doc/examples/cms_dec.cpp | 5 ++--- doc/examples/cms_enc.cpp | 7 +++---- doc/examples/dh.cpp | 7 +++---- doc/examples/dsa_kgen.cpp | 9 ++++----- doc/examples/dsa_sign.cpp | 7 +++---- doc/examples/eax_test.cpp | 3 --- doc/examples/ecdsa.cpp | 7 +++---- doc/examples/encrypt.cpp | 5 ++--- doc/examples/factor.cpp | 5 ++--- doc/examples/make_prime.cpp | 11 ++++++----- doc/examples/passhash.cpp | 5 ++--- doc/examples/pkcs10.cpp | 10 +++++----- doc/examples/pqg_gen.cpp | 6 +++--- doc/examples/ressol.cpp | 9 +++++---- doc/examples/row_encryptor.cpp | 6 +++--- doc/examples/rsa_dec.cpp | 5 ++--- doc/examples/rsa_enc.cpp | 7 +++---- doc/examples/rsa_kgen.cpp | 7 +++---- doc/examples/rsa_manykey.cpp | 4 ++-- doc/examples/self_sig.cpp | 9 ++++----- 21 files changed, 63 insertions(+), 79 deletions(-) (limited to 'doc/examples') diff --git a/doc/examples/ca.cpp b/doc/examples/ca.cpp index d0fd32d17..e4fb9eb02 100644 --- a/doc/examples/ca.cpp +++ b/doc/examples/ca.cpp @@ -14,7 +14,6 @@ */ #include -#include #include #include using namespace Botan; @@ -38,13 +37,12 @@ int main(int argc, char* argv[]) const std::string arg_ca_key = argv[3]; const std::string arg_req_file = argv[4]; - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; X509_Certificate ca_cert(arg_ca_cert); std::auto_ptr privkey( - PKCS8::load_key(arg_ca_key, *rng, arg_passphrase) + PKCS8::load_key(arg_ca_key, rng, arg_passphrase) ); X509_CA ca(ca_cert, *privkey); @@ -59,7 +57,7 @@ int main(int argc, char* argv[]) X509_Time start_time(system_time()); X509_Time end_time(system_time() + 365 * 60 * 60 * 24); - X509_Certificate new_cert = ca.sign_request(req, *rng, + X509_Certificate new_cert = ca.sign_request(req, rng, start_time, end_time); // send the new cert back to the requestor diff --git a/doc/examples/cms_dec.cpp b/doc/examples/cms_dec.cpp index 08d43e7b8..f35d63fa6 100644 --- a/doc/examples/cms_dec.cpp +++ b/doc/examples/cms_dec.cpp @@ -17,11 +17,10 @@ int main(int argc, char* argv[]) LibraryInitializer init; try { - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; X509_Certificate mycert("mycert.pem"); - PKCS8_PrivateKey* mykey = PKCS8::load_key("mykey.pem", *rng, "cut"); + PKCS8_PrivateKey* mykey = PKCS8::load_key("mykey.pem", rng, "cut"); X509_Certificate yourcert("yourcert.pem"); X509_Certificate cacert("cacert.pem"); diff --git a/doc/examples/cms_enc.cpp b/doc/examples/cms_enc.cpp index 50babc650..48b3c5ddd 100644 --- a/doc/examples/cms_enc.cpp +++ b/doc/examples/cms_enc.cpp @@ -18,8 +18,7 @@ int main() X509_Certificate cacert("cacert.pem"); X509_Certificate int_ca("int_ca.pem"); - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; X509_Store store; store.add_cert(mycert); @@ -34,10 +33,10 @@ int main() encoder.compress("Zlib"); encoder.digest(); - encoder.encrypt(*rng, mycert); + encoder.encrypt(rng, mycert); /* - PKCS8_PrivateKey* mykey = PKCS8::load_key("mykey.pem", *rng, "cut"); + PKCS8_PrivateKey* mykey = PKCS8::load_key("mykey.pem", rng, "cut"); encoder.sign(store, *mykey); */ diff --git a/doc/examples/dh.cpp b/doc/examples/dh.cpp index f2a43e7f6..af0c19fec 100644 --- a/doc/examples/dh.cpp +++ b/doc/examples/dh.cpp @@ -17,15 +17,14 @@ int main() { try { - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; // Alice creates a DH key and sends (the public part) to Bob - DH_PrivateKey private_a(*rng, DL_Group("modp/ietf/1024")); + DH_PrivateKey private_a(rng, DL_Group("modp/ietf/1024")); DH_PublicKey public_a = private_a; // Bob gets this // Bob creates a key with a matching group - DH_PrivateKey private_b(*rng, public_a.get_domain()); + DH_PrivateKey private_b(rng, public_a.get_domain()); // Bob sends the key back to Alice DH_PublicKey public_b = private_b; // Alice gets this diff --git a/doc/examples/dsa_kgen.cpp b/doc/examples/dsa_kgen.cpp index 258ad6cf6..fe70f93fb 100644 --- a/doc/examples/dsa_kgen.cpp +++ b/doc/examples/dsa_kgen.cpp @@ -44,18 +44,17 @@ int main(int argc, char* argv[]) try { - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; - DL_Group group(*rng, DL_Group::DSA_Kosherizer, 2048, 256); + DL_Group group(rng, DL_Group::DSA_Kosherizer, 2048, 256); - DSA_PrivateKey key(*rng, group); + DSA_PrivateKey key(rng, group); pub << X509::PEM_encode(key); if(argc == 1) priv << PKCS8::PEM_encode(key); else - priv << PKCS8::PEM_encode(key, *rng, argv[1]); + priv << PKCS8::PEM_encode(key, rng, argv[1]); } catch(std::exception& e) { diff --git a/doc/examples/dsa_sign.cpp b/doc/examples/dsa_sign.cpp index b45bd9d70..1ef81d424 100644 --- a/doc/examples/dsa_sign.cpp +++ b/doc/examples/dsa_sign.cpp @@ -48,11 +48,10 @@ int main(int argc, char* argv[]) return 1; } - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; std::auto_ptr key( - PKCS8::load_key(argv[1], *rng, passphrase) + PKCS8::load_key(argv[1], rng, passphrase) ); DSA_PrivateKey* dsakey = dynamic_cast(key.get()); @@ -71,7 +70,7 @@ int main(int argc, char* argv[]) signer.update(buf, got); Pipe pipe(new Base64_Encoder); - pipe.process_msg(signer.signature(*rng)); + pipe.process_msg(signer.signature(rng)); sigfile << pipe.read_all_as_string() << std::endl; } catch(std::exception& e) diff --git a/doc/examples/eax_test.cpp b/doc/examples/eax_test.cpp index f0e6b8d33..ea20bd0a6 100644 --- a/doc/examples/eax_test.cpp +++ b/doc/examples/eax_test.cpp @@ -48,9 +48,6 @@ void eax_test(const std::string& algo, plaintext_str.c_str(), ciphertext.c_str()); */ - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); - SymmetricKey key(key_str); InitializationVector iv(nonce_str); diff --git a/doc/examples/ecdsa.cpp b/doc/examples/ecdsa.cpp index db4a94f3f..ce199936d 100644 --- a/doc/examples/ecdsa.cpp +++ b/doc/examples/ecdsa.cpp @@ -12,12 +12,11 @@ int main() { try { - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; EC_Domain_Params params = get_EC_Dom_Pars_by_oid("1.3.132.0.8"); - ECDSA_PrivateKey ecdsa(*rng, params); + ECDSA_PrivateKey ecdsa(rng, params); ECDSA_PublicKey ecdsa_pub = ecdsa; @@ -34,7 +33,7 @@ int main() signer->update((const byte*)message, strlen(message)); - SecureVector sig = signer->signature(*rng); + SecureVector sig = signer->signature(rng); std::cout << sig.size() << "\n"; diff --git a/doc/examples/encrypt.cpp b/doc/examples/encrypt.cpp index 959461b63..8f61306be 100644 --- a/doc/examples/encrypt.cpp +++ b/doc/examples/encrypt.cpp @@ -119,12 +119,11 @@ int main(int argc, char* argv[]) const u32bit key_len = max_keylength_of(algo); const u32bit iv_len = block_size_of(algo); - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; std::auto_ptr s2k(get_s2k("PBKDF2(SHA-1)")); s2k->set_iterations(8192); - s2k->new_random_salt(*rng, 8); + s2k->new_random_salt(rng, 8); SymmetricKey bc_key = s2k->derive_key(key_len, "BLK" + passphrase); InitializationVector iv = s2k->derive_key(iv_len, "IVL" + passphrase); diff --git a/doc/examples/factor.cpp b/doc/examples/factor.cpp index cf4d395d0..70dde1050 100644 --- a/doc/examples/factor.cpp +++ b/doc/examples/factor.cpp @@ -123,10 +123,9 @@ int main(int argc, char* argv[]) { BigInt n(argv[1]); - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; - std::vector factors = factorize(n, *rng); + std::vector factors = factorize(n, rng); std::sort(factors.begin(), factors.end()); std::cout << n << ": "; diff --git a/doc/examples/make_prime.cpp b/doc/examples/make_prime.cpp index 4d82907f9..4135bd197 100644 --- a/doc/examples/make_prime.cpp +++ b/doc/examples/make_prime.cpp @@ -1,4 +1,5 @@ #include +#include using namespace Botan; @@ -9,7 +10,7 @@ using namespace Botan; int main() { - RandomNumberGenerator* rng = RandomNumberGenerator::make_rng(); + AutoSeeded_RNG rng; std::set primes; @@ -23,14 +24,14 @@ int main() u32bit bits = 18; - if(rng->next_byte() % 128 == 0) - bits -= rng->next_byte() % (bits-2); + if(rng.next_byte() % 128 == 0) + bits -= rng.next_byte() % (bits-2); bit_count[bits]++; //std::cout << "random_prime(" << bits << ")\n"; - BigInt p = random_prime(*rng, bits); + BigInt p = random_prime(rng, bits); if(p.bits() != bits) { @@ -39,7 +40,7 @@ int main() return 1; } - primes.insert(random_prime(*rng, bits)); + primes.insert(random_prime(rng, bits)); if(primes.size() != start_cnt) std::cout << primes.size() << "\n"; diff --git a/doc/examples/passhash.cpp b/doc/examples/passhash.cpp index 78ced1c66..3cba3738f 100644 --- a/doc/examples/passhash.cpp +++ b/doc/examples/passhash.cpp @@ -25,11 +25,10 @@ int main(int argc, char* argv[]) if(argc == 2) { - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; std::cout << "H('" << argv[1] << "') = " - << password_hash(argv[1], *rng) << '\n'; + << password_hash(argv[1], rng) << '\n'; } else { diff --git a/doc/examples/pkcs10.cpp b/doc/examples/pkcs10.cpp index 3983d5891..18390da7c 100644 --- a/doc/examples/pkcs10.cpp +++ b/doc/examples/pkcs10.cpp @@ -8,6 +8,7 @@ Written by Jack Lloyd (lloyd@randombit.net), April 7, 2003 This file is in the public domain */ #include +#include #include #include #include @@ -28,16 +29,15 @@ int main(int argc, char* argv[]) try { - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; - RSA_PrivateKey priv_key(*rng, 1024); + RSA_PrivateKey priv_key(rng, 1024); // If you want a DSA key instead of RSA, comment out the above line and // uncomment this one: //DSA_PrivateKey priv_key(DL_Group("dsa/jce/1024")); std::ofstream key_file("private.pem"); - key_file << PKCS8::PEM_encode(priv_key, *rng, argv[1]); + key_file << PKCS8::PEM_encode(priv_key, rng, argv[1]); X509_Cert_Options opts; @@ -57,7 +57,7 @@ int main(int argc, char* argv[]) opts.xmpp = "someid@xmpp.org"; - PKCS10_Request req = X509::create_cert_req(opts, priv_key, *rng); + PKCS10_Request req = X509::create_cert_req(opts, priv_key, rng); std::ofstream req_file("req.pem"); req_file << req.PEM_encode(); diff --git a/doc/examples/pqg_gen.cpp b/doc/examples/pqg_gen.cpp index 8683cb2df..5cb3703d6 100644 --- a/doc/examples/pqg_gen.cpp +++ b/doc/examples/pqg_gen.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -20,8 +21,7 @@ int main() try { LibraryInitializer init("use_engines"); - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; std::ifstream in("PQGGen.rsp"); if(!in) @@ -51,7 +51,7 @@ int main() if(name == "H") { - bool result = check(*rng, inputs); + bool result = check(rng, inputs); std::cout << "." << std::flush; if(result == false) { diff --git a/doc/examples/ressol.cpp b/doc/examples/ressol.cpp index ff49ef19d..47a6550b6 100644 --- a/doc/examples/ressol.cpp +++ b/doc/examples/ressol.cpp @@ -1,4 +1,5 @@ #include +#include using namespace Botan; @@ -47,7 +48,7 @@ void test_ressol(const BigInt& p, RandomNumberGenerator& rng) int main() { - RandomNumberGenerator* rng = RandomNumberGenerator::make_rng(); + AutoSeeded_RNG rng; #if 0 std::cout << ressol(8, 17) << "\n"; @@ -58,8 +59,8 @@ int main() for(int j = 16; j != 1024; ++j) { std::cout << "Round " << j << "\n"; - BigInt p = random_prime(*rng, j); - test_ressol(p, *rng); + BigInt p = random_prime(rng, j); + test_ressol(p, rng); //printf("%d\n", j); @@ -70,7 +71,7 @@ int main() { std::cout << "PRIME[" << j << "] == " << PRIMES[j] << std::endl; //printf("%d - ", PRIMES[j]); - test_ressol(PRIMES[j], *rng); + test_ressol(PRIMES[j], rng); //printf("\n"); } */ diff --git a/doc/examples/row_encryptor.cpp b/doc/examples/row_encryptor.cpp index dc15ee6c4..f78332335 100644 --- a/doc/examples/row_encryptor.cpp +++ b/doc/examples/row_encryptor.cpp @@ -88,9 +88,9 @@ int main() { LibraryInitializer init; - std::auto_ptr rng(RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; - Row_Encryptor encryptor("secret passphrase", *rng); + Row_Encryptor encryptor("secret passphrase", rng); std::vector original_inputs; @@ -104,7 +104,7 @@ int main() // TODO: Maybe randomize the length slightly? for(u32bit j = 0; j != 32; ++j) - out << std::hex << (int)rng->next_byte(); + out << std::hex << (int)rng.next_byte(); original_inputs.push_back(out.str()); } diff --git a/doc/examples/rsa_dec.cpp b/doc/examples/rsa_dec.cpp index ca93756f2..7459f41b6 100644 --- a/doc/examples/rsa_dec.cpp +++ b/doc/examples/rsa_dec.cpp @@ -34,11 +34,10 @@ int main(int argc, char* argv[]) try { - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; std::auto_ptr key( - PKCS8::load_key(argv[1], *rng, argv[3])); + PKCS8::load_key(argv[1], rng, argv[3])); RSA_PrivateKey* rsakey = dynamic_cast(key.get()); if(!rsakey) diff --git a/doc/examples/rsa_enc.cpp b/doc/examples/rsa_enc.cpp index aebe42e72..4f37af6d6 100644 --- a/doc/examples/rsa_enc.cpp +++ b/doc/examples/rsa_enc.cpp @@ -73,8 +73,7 @@ int main(int argc, char* argv[]) return 1; } - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; std::auto_ptr encryptor(get_pk_encryptor(*rsakey, "EME1(SHA-1)")); @@ -90,7 +89,7 @@ int main(int argc, char* argv[]) statistically indepedent. Practically speaking I don't think this is a problem. */ - SymmetricKey masterkey(*rng, + SymmetricKey masterkey(rng, std::min(32U, encryptor->maximum_input_size())); SymmetricKey cast_key = derive_key("CAST", masterkey, 16); @@ -98,7 +97,7 @@ int main(int argc, char* argv[]) SymmetricKey iv = derive_key("IV", masterkey, 8); SecureVector encrypted_key = - encryptor->encrypt(masterkey.bits_of(), *rng); + encryptor->encrypt(masterkey.bits_of(), rng); ciphertext << b64_encode(encrypted_key) << std::endl; diff --git a/doc/examples/rsa_kgen.cpp b/doc/examples/rsa_kgen.cpp index c3942971b..ed8e786fe 100644 --- a/doc/examples/rsa_kgen.cpp +++ b/doc/examples/rsa_kgen.cpp @@ -45,16 +45,15 @@ int main(int argc, char* argv[]) try { - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; - RSA_PrivateKey key(*rng, bits); + RSA_PrivateKey key(rng, bits); pub << X509::PEM_encode(key); if(argc == 2) priv << PKCS8::PEM_encode(key); else - priv << PKCS8::PEM_encode(key, *rng, argv[2]); + priv << PKCS8::PEM_encode(key, rng, argv[2]); } catch(std::exception& e) { diff --git a/doc/examples/rsa_manykey.cpp b/doc/examples/rsa_manykey.cpp index 95be8c568..9d27634de 100644 --- a/doc/examples/rsa_manykey.cpp +++ b/doc/examples/rsa_manykey.cpp @@ -15,13 +15,13 @@ using namespace Botan; int main() { - std::auto_ptr rng(RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; for(u32bit j = 512; j <= 8192; j += 256) { std::cout << j << "..."; - RSA_PrivateKey key(*rng, j); + RSA_PrivateKey key(rng, j); std::ofstream priv(("rsa/" + to_string(j) + ".pem").c_str()); priv << PKCS8::PEM_encode(key); diff --git a/doc/examples/self_sig.cpp b/doc/examples/self_sig.cpp index 42a58b485..a4b0c928c 100644 --- a/doc/examples/self_sig.cpp +++ b/doc/examples/self_sig.cpp @@ -42,13 +42,12 @@ int main(int argc, char* argv[]) try { - std::auto_ptr rng( - RandomNumberGenerator::make_rng()); + AutoSeeded_RNG rng; - RSA_PrivateKey key(*rng, 1024); + RSA_PrivateKey key(rng, 1024); std::ofstream priv_key("private.pem"); - priv_key << PKCS8::PEM_encode(key, *rng, argv[1]); + priv_key << PKCS8::PEM_encode(key, rng, argv[1]); X509_Cert_Options opts; @@ -63,7 +62,7 @@ int main(int argc, char* argv[]) if(do_CA) opts.CA_key(); - X509_Certificate cert = X509::create_self_signed_cert(opts, key, *rng); + X509_Certificate cert = X509::create_self_signed_cert(opts, key, rng); std::ofstream cert_file("cert.pem"); cert_file << cert.PEM_encode(); -- cgit v1.2.3