diff options
Diffstat (limited to 'doc/examples')
-rw-r--r-- | doc/examples/ca.cpp | 2 | ||||
-rw-r--r-- | doc/examples/gen_certs.cpp | 2 | ||||
-rw-r--r-- | doc/examples/read_ssh.cpp | 15 | ||||
-rw-r--r-- | doc/examples/row_encryptor.cpp | 67 | ||||
-rw-r--r-- | doc/examples/test_es.cpp | 18 |
5 files changed, 67 insertions, 37 deletions
diff --git a/doc/examples/ca.cpp b/doc/examples/ca.cpp index b98e6620e..f5eccdde9 100644 --- a/doc/examples/ca.cpp +++ b/doc/examples/ca.cpp @@ -15,7 +15,7 @@ #include <botan/botan.h> #include <botan/x509_ca.h> -#include <botan/timer.h> +#include <botan/time.h> using namespace Botan; #include <iostream> diff --git a/doc/examples/gen_certs.cpp b/doc/examples/gen_certs.cpp index 1c95402f4..8490a167e 100644 --- a/doc/examples/gen_certs.cpp +++ b/doc/examples/gen_certs.cpp @@ -5,7 +5,7 @@ #include <botan/botan.h> #include <botan/rsa.h> -#include <botan/timer.h> +#include <botan/time.h> #include <botan/x509self.h> #include <botan/x509_ca.h> diff --git a/doc/examples/read_ssh.cpp b/doc/examples/read_ssh.cpp index a88306caa..4d9050a31 100644 --- a/doc/examples/read_ssh.cpp +++ b/doc/examples/read_ssh.cpp @@ -5,17 +5,24 @@ #include <fstream> #include <botan/x509_key.h> #include <botan/filters.h> -#include <botan/loadstor.h> #include <botan/rsa.h> #include <botan/dsa.h> using namespace Botan; +inline u32bit make_u32bit(byte i0, byte i1, byte i2, byte i3) + { + return ((static_cast<u32bit>(i0) << 24) | + (static_cast<u32bit>(i1) << 16) | + (static_cast<u32bit>(i2) << 8) | + (static_cast<u32bit>(i3))); + } + u32bit read_u32bit(Pipe& pipe) { - byte out[4] = { 0 }; - pipe.read(out, 4); - u32bit len = load_be<u32bit>(out, 0); + byte sz[4] = { 0 }; + pipe.read(sz, 4); + u32bit len = make_u32bit(sz[0], sz[1], sz[2], sz[3]); if(len > 10000) throw Decoding_Error("Huge size in read_u32bit, something went wrong"); return len; diff --git a/doc/examples/row_encryptor.cpp b/doc/examples/row_encryptor.cpp index 1dac82f89..162e962e0 100644 --- a/doc/examples/row_encryptor.cpp +++ b/doc/examples/row_encryptor.cpp @@ -19,16 +19,23 @@ class Row_Encryptor Row_Encryptor(const std::string& passphrase, RandomNumberGenerator& rng); + Row_Encryptor(const std::string& passphrase, + const MemoryRegion<byte>& salt); + std::string encrypt(const std::string& input, const MemoryRegion<byte>& salt); std::string decrypt(const std::string& input, const MemoryRegion<byte>& salt); + SecureVector<byte> get_s2k_salt() const { return s2k_salt; } private: + void init(const std::string& passphrase); + Row_Encryptor(const Row_Encryptor&) {} Row_Encryptor& operator=(const Row_Encryptor&) { return (*this); } + SecureVector<byte> s2k_salt; Pipe enc_pipe, dec_pipe; EAX_Encryption* eax_enc; // owned by enc_pipe EAX_Decryption* eax_dec; // owned by dec_pipe; @@ -37,11 +44,24 @@ class Row_Encryptor Row_Encryptor::Row_Encryptor(const std::string& passphrase, RandomNumberGenerator& rng) { + s2k_salt.resize(10); // 80 bits + rng.randomize(&s2k_salt[0], s2k_salt.size()); + init(passphrase); + } + +Row_Encryptor::Row_Encryptor(const std::string& passphrase, + const MemoryRegion<byte>& salt) + { + s2k_salt = salt; + init(passphrase); + } + +void Row_Encryptor::init(const std::string& passphrase) + { std::auto_ptr<S2K> s2k(get_s2k("PBKDF2(SHA-160)")); s2k->set_iterations(10000); - - s2k->new_random_salt(rng, 10); // 10 bytes == 80 bits + s2k->change_salt(&s2k_salt[0], s2k_salt.size()); SecureVector<byte> key = s2k->derive_key(32, passphrase).bits_of(); @@ -67,11 +87,7 @@ std::string Row_Encryptor::encrypt(const std::string& input, const MemoryRegion<byte>& salt) { eax_enc->set_iv(salt); - - enc_pipe.start_msg(); - enc_pipe.write(input); - enc_pipe.end_msg(); - + enc_pipe.process_msg(input); return enc_pipe.read_all_as_string(Pipe::LAST_MESSAGE); } @@ -79,11 +95,7 @@ std::string Row_Encryptor::decrypt(const std::string& input, const MemoryRegion<byte>& salt) { eax_dec->set_iv(salt); - - dec_pipe.start_msg(); - dec_pipe.write(input); - dec_pipe.end_msg(); - + dec_pipe.process_msg(input); return dec_pipe.read_all_as_string(Pipe::LAST_MESSAGE); } @@ -91,28 +103,25 @@ std::string Row_Encryptor::decrypt(const std::string& input, Test code follows: */ -#include <botan/loadstor.h> - int main() { Botan::LibraryInitializer init; AutoSeeded_RNG rng; + const std::string secret_passphrase = "secret passphrase"; + Row_Encryptor encryptor("secret passphrase", rng); std::vector<std::string> original_inputs; - for(u32bit i = 0; i != 15000; ++i) + for(u32bit i = 0; i != 50000; ++i) { std::ostringstream out; - // This will actually generate variable length inputs (when - // there are leading 0s, which are skipped), which is good - // since it assures performance is OK across a mix of lengths - // TODO: Maybe randomize the length slightly? + u32bit output_bytes = rng.next_byte(); - for(u32bit j = 0; j != 32; ++j) + for(u32bit j = 0; j != output_bytes; ++j) out << std::hex << (int)rng.next_byte(); original_inputs.push_back(out.str()); @@ -124,7 +133,9 @@ int main() for(u32bit i = 0; i != original_inputs.size(); ++i) { std::string input = original_inputs[i]; - store_le(i, salt); + + for(u32bit j = 0; j != 4; ++j) + salt[j] = (i >> 8) & 0xFF; encrypted_values.push_back(encryptor.encrypt(input, salt)); } @@ -132,7 +143,10 @@ int main() for(u32bit i = 0; i != encrypted_values.size(); ++i) { std::string ciphertext = encrypted_values[i]; - store_le(i, salt); // NOTE: same salt value as previous loop (index value) + + // NOTE: same salt value as previous loop (index value) + for(u32bit j = 0; j != 4; ++j) + salt[j] = (i >> 8) & 0xFF; std::string output = encryptor.decrypt(ciphertext, salt); @@ -140,4 +154,13 @@ int main() std::cout << "BOOM " << i << "\n"; } + Row_Encryptor test_s2k_salt_copy(secret_passphrase, + encryptor.get_s2k_salt()); + + salt.clear(); // all-0 + std::string test = test_s2k_salt_copy.decrypt(encrypted_values[0], salt); + if(test != original_inputs[0]) + std::cout << "S2K salt copy failed to decrypt properly\n"; + + return 0; } diff --git a/doc/examples/test_es.cpp b/doc/examples/test_es.cpp index 951d51ac7..b5510ab75 100644 --- a/doc/examples/test_es.cpp +++ b/doc/examples/test_es.cpp @@ -1,32 +1,32 @@ #include <botan/botan.h> #include <stdio.h> -#if defined(BOTAN_HAS_ENTROPY_SRC_DEVICE) - #include <botan/es_dev.h> +#if defined(BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM) + #include <botan/internal/dev_random.h> #endif #if defined(BOTAN_HAS_ENTROPY_SRC_EGD) - #include <botan/es_egd.h> + #include <botan/internal/es_egd.h> #endif #if defined(BOTAN_HAS_ENTROPY_SRC_UNIX) - #include <botan/es_unix.h> + #include <botan/internal/es_unix.h> #endif #if defined(BOTAN_HAS_ENTROPY_SRC_BEOS) - #include <botan/es_beos.h> + #include <botan/internal/es_beos.h> #endif #if defined(BOTAN_HAS_ENTROPY_SRC_CAPI) - #include <botan/es_capi.h> + #include <botan/internal/es_capi.h> #endif #if defined(BOTAN_HAS_ENTROPY_SRC_WIN32) - #include <botan/es_win32.h> + #include <botan/internal/es_win32.h> #endif #if defined(BOTAN_HAS_ENTROPY_SRC_FTW) - #include <botan/es_ftw.h> + #include <botan/internal/es_ftw.h> #endif @@ -76,7 +76,7 @@ int main() { Botan::LibraryInitializer init; -#if defined(BOTAN_HAS_ENTROPY_SRC_DEVICE) +#if defined(BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM) test_entropy_source( new Device_EntropySource( split_on("/dev/random:/dev/srandom:/dev/urandom", ':') |