diff options
-rw-r--r-- | src/lib/constructs/fpe_fe1/fpe_fe1.cpp | 12 | ||||
-rw-r--r-- | src/lib/constructs/fpe_fe1/fpe_fe1.h | 4 | ||||
-rw-r--r-- | src/lib/pk_pad/emsa1/emsa1.cpp | 24 | ||||
-rw-r--r-- | src/lib/pk_pad/emsa1/emsa1.h | 23 | ||||
-rw-r--r-- | src/lib/pk_pad/emsa1_bsi/emsa1_bsi.cpp (renamed from src/lib/pk_pad/emsa_raw_bsi/emsa1_bsi.cpp) | 2 | ||||
-rw-r--r-- | src/lib/pk_pad/emsa1_bsi/emsa1_bsi.h (renamed from src/lib/pk_pad/emsa_raw_bsi/emsa1_bsi.h) | 6 | ||||
-rw-r--r-- | src/lib/pk_pad/emsa1_bsi/info.txt (renamed from src/lib/pk_pad/emsa_raw_bsi/info.txt) | 0 | ||||
-rw-r--r-- | src/lib/rng/x931_rng/x931_rng.cpp | 102 | ||||
-rw-r--r-- | src/lib/rng/x931_rng/x931_rng.h | 10 | ||||
-rw-r--r-- | src/lib/utils/types.h | 1 |
10 files changed, 69 insertions, 115 deletions
diff --git a/src/lib/constructs/fpe_fe1/fpe_fe1.cpp b/src/lib/constructs/fpe_fe1/fpe_fe1.cpp index b22d3a8df..da0ef1081 100644 --- a/src/lib/constructs/fpe_fe1/fpe_fe1.cpp +++ b/src/lib/constructs/fpe_fe1/fpe_fe1.cpp @@ -1,8 +1,5 @@ /* -* Format Preserving Encryption using the scheme FE1 from the paper -* "Format-Preserving Encryption" by Bellare, Rogaway, et al -* (http://eprint.iacr.org/2009/251) -* +* Format Preserving Encryption (FE1 scheme) * (C) 2009 Jack Lloyd * * Distributed under the terms of the Botan license @@ -13,6 +10,7 @@ #include <botan/hmac.h> #include <botan/sha2_32.h> #include <stdexcept> +#include <memory> namespace Botan { @@ -86,12 +84,10 @@ class FPE_Encryptor const BigInt& n, const std::vector<byte>& tweak); - ~FPE_Encryptor() { delete mac; } - BigInt operator()(size_t i, const BigInt& R); private: - MessageAuthenticationCode* mac; + std::unique_ptr<MessageAuthenticationCode> mac; std::vector<byte> mac_n_t; }; @@ -99,7 +95,7 @@ FPE_Encryptor::FPE_Encryptor(const SymmetricKey& key, const BigInt& n, const std::vector<byte>& tweak) { - mac = new HMAC(new SHA_256); + mac.reset(new HMAC(new SHA_256)); mac->set_key(key); std::vector<byte> n_bin = BigInt::encode(n); diff --git a/src/lib/constructs/fpe_fe1/fpe_fe1.h b/src/lib/constructs/fpe_fe1/fpe_fe1.h index 66e7f1cfa..555f97d3f 100644 --- a/src/lib/constructs/fpe_fe1/fpe_fe1.h +++ b/src/lib/constructs/fpe_fe1/fpe_fe1.h @@ -16,6 +16,10 @@ namespace Botan { namespace FPE { /** +* Format Preserving Encryption using the scheme FE1 from the paper +* "Format-Preserving Encryption" by Bellare, Rogaway, et al +* (http://eprint.iacr.org/2009/251) +* * Encrypt X from and onto the group Z_n using key and tweak * @param n the modulus * @param X the plaintext as a BigInt diff --git a/src/lib/pk_pad/emsa1/emsa1.cpp b/src/lib/pk_pad/emsa1/emsa1.cpp index 2358023f8..2da38f12f 100644 --- a/src/lib/pk_pad/emsa1/emsa1.cpp +++ b/src/lib/pk_pad/emsa1/emsa1.cpp @@ -40,42 +40,30 @@ secure_vector<byte> emsa1_encoding(const secure_vector<byte>& msg, } -/* -* EMSA1 Update Operation -*/ void EMSA1::update(const byte input[], size_t length) { - hash->update(input, length); + m_hash->update(input, length); } -/* -* Return the raw (unencoded) data -*/ secure_vector<byte> EMSA1::raw_data() { - return hash->final(); + return m_hash->final(); } -/* -* EMSA1 Encode Operation -*/ secure_vector<byte> EMSA1::encoding_of(const secure_vector<byte>& msg, - size_t output_bits, - RandomNumberGenerator&) + size_t output_bits, + RandomNumberGenerator&) { - if(msg.size() != hash->output_length()) + if(msg.size() != hash_output_length()) throw Encoding_Error("EMSA1::encoding_of: Invalid size for input"); return emsa1_encoding(msg, output_bits); } -/* -* EMSA1 Decode/Verify Operation -*/ bool EMSA1::verify(const secure_vector<byte>& coded, const secure_vector<byte>& raw, size_t key_bits) { try { - if(raw.size() != hash->output_length()) + if(raw.size() != m_hash->output_length()) throw Encoding_Error("EMSA1::encoding_of: Invalid size for input"); secure_vector<byte> our_coding = emsa1_encoding(raw, key_bits); diff --git a/src/lib/pk_pad/emsa1/emsa1.h b/src/lib/pk_pad/emsa1/emsa1.h index f84ca5ae7..8df53f789 100644 --- a/src/lib/pk_pad/emsa1/emsa1.h +++ b/src/lib/pk_pad/emsa1/emsa1.h @@ -21,26 +21,25 @@ class BOTAN_DLL EMSA1 : public EMSA { public: /** - * @param h the hash object to use + * @param hash the hash function to use */ - EMSA1(HashFunction* h) : hash(h) {} - ~EMSA1() { delete hash; } + EMSA1(HashFunction* hash) : m_hash(hash) {} + protected: - /** - * @return const pointer to the underlying hash - */ - const HashFunction* hash_ptr() const { return hash; } + size_t hash_output_length() const { return m_hash->output_length(); } private: void update(const byte[], size_t); secure_vector<byte> raw_data(); - secure_vector<byte> encoding_of(const secure_vector<byte>&, size_t, - RandomNumberGenerator& rng); + secure_vector<byte> encoding_of(const secure_vector<byte>& msg, + size_t output_bits, + RandomNumberGenerator& rng); - bool verify(const secure_vector<byte>&, const secure_vector<byte>&, - size_t); + bool verify(const secure_vector<byte>& coded, + const secure_vector<byte>& raw, + size_t key_bits); - HashFunction* hash; + std::unique_ptr<HashFunction> m_hash; }; } diff --git a/src/lib/pk_pad/emsa_raw_bsi/emsa1_bsi.cpp b/src/lib/pk_pad/emsa1_bsi/emsa1_bsi.cpp index 9096edfbf..235dfb91b 100644 --- a/src/lib/pk_pad/emsa_raw_bsi/emsa1_bsi.cpp +++ b/src/lib/pk_pad/emsa1_bsi/emsa1_bsi.cpp @@ -17,7 +17,7 @@ secure_vector<byte> EMSA1_BSI::encoding_of(const secure_vector<byte>& msg, size_t output_bits, RandomNumberGenerator&) { - if(msg.size() != hash_ptr()->output_length()) + if(msg.size() != hash_output_length()) throw Encoding_Error("EMSA1_BSI::encoding_of: Invalid size for input"); if(8*msg.size() <= output_bits) diff --git a/src/lib/pk_pad/emsa_raw_bsi/emsa1_bsi.h b/src/lib/pk_pad/emsa1_bsi/emsa1_bsi.h index 1b90f48df..a2b0c7432 100644 --- a/src/lib/pk_pad/emsa_raw_bsi/emsa1_bsi.h +++ b/src/lib/pk_pad/emsa1_bsi/emsa1_bsi.h @@ -14,9 +14,9 @@ namespace Botan { /** -EMSA1_BSI is a variant of EMSA1 specified by the BSI. It accepts only -hash values which are less or equal than the maximum key length. The -implementation comes from InSiTo +* EMSA1_BSI is a variant of EMSA1 specified by the BSI. It accepts +* only hash values which are less or equal than the maximum key +* length. The implementation comes from InSiTo */ class BOTAN_DLL EMSA1_BSI : public EMSA1 { diff --git a/src/lib/pk_pad/emsa_raw_bsi/info.txt b/src/lib/pk_pad/emsa1_bsi/info.txt index 021c99720..021c99720 100644 --- a/src/lib/pk_pad/emsa_raw_bsi/info.txt +++ b/src/lib/pk_pad/emsa1_bsi/info.txt diff --git a/src/lib/rng/x931_rng/x931_rng.cpp b/src/lib/rng/x931_rng/x931_rng.cpp index b36f87106..dbf09b367 100644 --- a/src/lib/rng/x931_rng/x931_rng.cpp +++ b/src/lib/rng/x931_rng/x931_rng.cpp @@ -1,6 +1,6 @@ /* * ANSI X9.31 RNG -* (C) 1999-2009 Jack Lloyd +* (C) 1999-2009,2014 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -11,9 +11,6 @@ namespace Botan { -/* -* Generate a buffer of random bytes -*/ void ANSI_X931_RNG::randomize(byte out[], size_t length) { if(!is_seeded()) @@ -21,15 +18,15 @@ void ANSI_X931_RNG::randomize(byte out[], size_t length) while(length) { - if(position == R.size()) + if(m_R_pos == m_R.size()) update_buffer(); - const size_t copied = std::min<size_t>(length, R.size() - position); + const size_t copied = std::min<size_t>(length, m_R.size() - m_R_pos); - copy_mem(out, &R[position], copied); + copy_mem(out, &m_R[m_R_pos], copied); out += copied; length -= copied; - position += copied; + m_R_pos += copied; } } @@ -38,18 +35,18 @@ void ANSI_X931_RNG::randomize(byte out[], size_t length) */ void ANSI_X931_RNG::update_buffer() { - const size_t BLOCK_SIZE = cipher->block_size(); + const size_t BLOCK_SIZE = m_cipher->block_size(); - secure_vector<byte> DT = prng->random_vec(BLOCK_SIZE); - cipher->encrypt(DT); + secure_vector<byte> DT = m_prng->random_vec(BLOCK_SIZE); + m_cipher->encrypt(DT); - xor_buf(&R[0], &V[0], &DT[0], BLOCK_SIZE); - cipher->encrypt(R); + xor_buf(&m_R[0], &m_V[0], &DT[0], BLOCK_SIZE); + m_cipher->encrypt(m_R); - xor_buf(&V[0], &R[0], &DT[0], BLOCK_SIZE); - cipher->encrypt(V); + xor_buf(&m_V[0], &m_R[0], &DT[0], BLOCK_SIZE); + m_cipher->encrypt(m_V); - position = 0; + m_R_pos = 0; } /* @@ -57,90 +54,59 @@ void ANSI_X931_RNG::update_buffer() */ void ANSI_X931_RNG::rekey() { - const size_t BLOCK_SIZE = cipher->block_size(); + const size_t BLOCK_SIZE = m_cipher->block_size(); - if(prng->is_seeded()) + if(m_prng->is_seeded()) { - cipher->set_key(prng->random_vec(cipher->maximum_keylength())); + m_cipher->set_key(m_prng->random_vec(m_cipher->maximum_keylength())); - if(V.size() != BLOCK_SIZE) - V.resize(BLOCK_SIZE); - prng->randomize(&V[0], V.size()); + if(m_V.size() != BLOCK_SIZE) + m_V.resize(BLOCK_SIZE); + m_prng->randomize(&m_V[0], m_V.size()); update_buffer(); } } -/* -* Reseed the internal state -*/ void ANSI_X931_RNG::reseed(size_t poll_bits) { - prng->reseed(poll_bits); + m_prng->reseed(poll_bits); rekey(); } -/* -* Add some entropy to the underlying PRNG -*/ void ANSI_X931_RNG::add_entropy(const byte input[], size_t length) { - prng->add_entropy(input, length); + m_prng->add_entropy(input, length); rekey(); } -/* -* Check if the the PRNG is seeded -*/ bool ANSI_X931_RNG::is_seeded() const { - return (V.size() > 0); + return (m_V.size() > 0); } -/* -* Clear memory of sensitive data -*/ void ANSI_X931_RNG::clear() { - cipher->clear(); - prng->clear(); - zeroise(R); - V.clear(); + m_cipher->clear(); + m_prng->clear(); + zeroise(m_R); + m_V.clear(); - position = 0; + m_R_pos = 0; } -/* -* Return the name of this type -*/ std::string ANSI_X931_RNG::name() const { - return "X9.31(" + cipher->name() + ")"; - } - -/* -* ANSI X931 RNG Constructor -*/ -ANSI_X931_RNG::ANSI_X931_RNG(BlockCipher* cipher_in, - RandomNumberGenerator* prng_in) - { - if(!prng_in || !cipher_in) - throw Invalid_Argument("ANSI_X931_RNG constructor: NULL arguments"); - - cipher = cipher_in; - prng = prng_in; - - R.resize(cipher->block_size()); - position = 0; + return "X9.31(" + m_cipher->name() + ")"; } -/* -* ANSI X931 RNG Destructor -*/ -ANSI_X931_RNG::~ANSI_X931_RNG() +ANSI_X931_RNG::ANSI_X931_RNG(BlockCipher* cipher, + RandomNumberGenerator* prng) : + m_cipher(cipher), + m_prng(prng), + m_R(m_cipher->block_size()), + m_R_pos(0) { - delete cipher; - delete prng; } } diff --git a/src/lib/rng/x931_rng/x931_rng.h b/src/lib/rng/x931_rng/x931_rng.h index 8052cedc3..63ade67ff 100644 --- a/src/lib/rng/x931_rng/x931_rng.h +++ b/src/lib/rng/x931_rng/x931_rng.h @@ -34,15 +34,15 @@ class BOTAN_DLL ANSI_X931_RNG : public RandomNumberGenerator */ ANSI_X931_RNG(BlockCipher* cipher, RandomNumberGenerator* rng); - ~ANSI_X931_RNG(); + private: void rekey(); void update_buffer(); - BlockCipher* cipher; - RandomNumberGenerator* prng; - secure_vector<byte> V, R; - size_t position; + std::unique_ptr<BlockCipher> m_cipher; + std::unique_ptr<RandomNumberGenerator> m_prng; + secure_vector<byte> m_V, m_R; + size_t m_R_pos; }; } diff --git a/src/lib/utils/types.h b/src/lib/utils/types.h index 1fd54e060..253aaa66a 100644 --- a/src/lib/utils/types.h +++ b/src/lib/utils/types.h @@ -12,6 +12,7 @@ #include <botan/assert.h> #include <cstddef> #include <cstdint> +#include <memory> /** * The primary namespace for the botan library |