diff options
31 files changed, 97 insertions, 153 deletions
diff --git a/checks/block.cpp b/checks/block.cpp index 3cd41e63a..c90e3e499 100644 --- a/checks/block.cpp +++ b/checks/block.cpp @@ -89,7 +89,7 @@ void ECB_Encryption_ErrorCheck::end_msg() } if(position) - throw Exception("ECB: input was not in full blocks"); + throw Encoding_Error("ECB: input was not in full blocks"); } Filter* lookup_block(const std::string& algname, const std::string& key) diff --git a/checks/pk.cpp b/checks/pk.cpp index e6f4d4f48..9c243f1cf 100644 --- a/checks/pk.cpp +++ b/checks/pk.cpp @@ -172,7 +172,7 @@ u32bit validate_rsa_enc_pkcs8(const std::string& algo, RandomNumberGenerator& rng) { if(str.size() != 4 && str.size() != 5) - throw Exception("Invalid input from pk_valid.dat"); + throw std::runtime_error("Invalid input from pk_valid.dat"); #if defined(BOTAN_HAS_RSA) @@ -210,7 +210,7 @@ u32bit validate_rsa_enc(const std::string& algo, RandomNumberGenerator& rng) { if(str.size() != 6) - throw Exception("Invalid input from pk_valid.dat"); + throw std::runtime_error("Invalid input from pk_valid.dat"); #if defined(BOTAN_HAS_RSA) @@ -238,7 +238,7 @@ u32bit validate_elg_enc(const std::string& algo, RandomNumberGenerator& rng) { if(str.size() != 6 && str.size() != 7) - throw Exception("Invalid input from pk_valid.dat"); + throw std::runtime_error("Invalid input from pk_valid.dat"); #if defined(BOTAN_HAS_ELGAMAL) @@ -270,7 +270,7 @@ u32bit validate_rsa_sig(const std::string& algo, RandomNumberGenerator& rng) { if(str.size() != 6) - throw Exception("Invalid input from pk_valid.dat"); + throw std::runtime_error("Invalid input from pk_valid.dat"); #if defined(BOTAN_HAS_RSA) @@ -296,7 +296,7 @@ u32bit validate_rsa_ver(const std::string& algo, const std::vector<std::string>& str) { if(str.size() != 5) /* is actually 4, parse() adds an extra empty one */ - throw Exception("Invalid input from pk_valid.dat"); + throw std::runtime_error("Invalid input from pk_valid.dat"); #if defined(BOTAN_HAS_RSA) RSA_PublicKey key(to_bigint(str[1]), to_bigint(str[0])); @@ -320,7 +320,7 @@ u32bit validate_rsa_ver_x509(const std::string& algo, const std::vector<std::string>& str) { if(str.size() != 5) /* is actually 3, parse() adds extra empty ones */ - throw Exception("Invalid input from pk_valid.dat"); + throw std::runtime_error("Invalid input from pk_valid.dat"); #if defined(BOTAN_HAS_RSA) DataSource_Memory keysource(reinterpret_cast<const byte*>(str[0].c_str()), @@ -352,7 +352,7 @@ u32bit validate_rw_ver(const std::string& algo, const std::vector<std::string>& str) { if(str.size() != 5) - throw Exception("Invalid input from pk_valid.dat"); + throw std::runtime_error("Invalid input from pk_valid.dat"); @@ -379,7 +379,7 @@ u32bit validate_rw_sig(const std::string& algo, RandomNumberGenerator& rng) { if(str.size() != 6) - throw Exception("Invalid input from pk_valid.dat"); + throw std::runtime_error("Invalid input from pk_valid.dat"); #if defined(BOTAN_HAS_RW) @@ -405,7 +405,7 @@ u32bit validate_dsa_sig(const std::string& algo, RandomNumberGenerator& rng) { if(str.size() != 4 && str.size() != 5) - throw Exception("Invalid input from pk_valid.dat"); + throw std::runtime_error("Invalid input from pk_valid.dat"); std::string pass; if(str.size() == 5) pass = str[4]; @@ -442,7 +442,7 @@ u32bit validate_dsa_ver(const std::string& algo, const std::vector<std::string>& str) { if(str.size() != 5) /* is actually 3, parse() adds extra empty ones */ - throw Exception("Invalid input from pk_valid.dat"); + throw std::runtime_error("Invalid input from pk_valid.dat"); DataSource_Memory keysource(reinterpret_cast<const byte*>(str[0].c_str()), str[0].length()); @@ -476,7 +476,7 @@ u32bit validate_nr_sig(const std::string& algo, RandomNumberGenerator& rng) { if(str.size() != 8) - throw Exception("Invalid input from pk_valid.dat"); + throw std::runtime_error("Invalid input from pk_valid.dat"); #if defined(BOTAN_HAS_NYBERG_RUEPPEL) @@ -503,7 +503,7 @@ u32bit validate_dh(const std::string& algo, RandomNumberGenerator& rng) { if(str.size() != 5 && str.size() != 6) - throw Exception("Invalid input from pk_valid.dat"); + throw std::runtime_error("Invalid input from pk_valid.dat"); #if defined(BOTAN_HAS_DIFFIE_HELLMAN) @@ -534,7 +534,7 @@ u32bit validate_dlies(const std::string& algo, RandomNumberGenerator& rng) { if(str.size() != 6) - throw Exception("Invalid input from pk_valid.dat"); + throw std::runtime_error("Invalid input from pk_valid.dat"); #if defined(BOTAN_HAS_DLIES) DL_Group domain(to_bigint(str[0]), to_bigint(str[1])); @@ -547,7 +547,7 @@ u32bit validate_dlies(const std::string& algo, std::vector<std::string> options = split_on(opt_str, '/'); if(options.size() != 3) - throw Exception("DLIES needs three options: " + opt_str); + throw std::runtime_error("DLIES needs three options: " + opt_str); MessageAuthenticationCode* mac = get_mac(options[1]); u32bit mac_key_len = to_u32bit(options[2]); diff --git a/doc/examples/pqg_gen.cpp b/doc/examples/pqg_gen.cpp index 4104e8ecb..e797233f9 100644 --- a/doc/examples/pqg_gen.cpp +++ b/doc/examples/pqg_gen.cpp @@ -31,7 +31,7 @@ int main() std::ifstream in("PQGGen.rsp"); if(!in) - throw Exception("Can't open response file"); + throw std::runtime_error("Can't open response file"); std::map<std::string, std::string> inputs; @@ -46,7 +46,7 @@ int main() std::vector<std::string> name_and_val = split_on(line, '='); if(name_and_val.size() != 2) - throw Decoding_Error("Unexpected input: " + line); + throw std::runtime_error("Unexpected input: " + line); name_and_val[0].erase(name_and_val[0].size()-1); name_and_val[1].erase(0, 1); diff --git a/src/alloc/mem_pool/mem_pool.cpp b/src/alloc/mem_pool/mem_pool.cpp index 2945a4cef..4180d2602 100644 --- a/src/alloc/mem_pool/mem_pool.cpp +++ b/src/alloc/mem_pool/mem_pool.cpp @@ -15,19 +15,6 @@ namespace Botan { -namespace { - -/* -* Memory Allocation Exception -*/ -struct Memory_Exhaustion : public std::bad_alloc - { - const char* what() const throw() - { return "Ran out of memory, allocation failed"; } - }; - -} - /* * Memory_Block Constructor */ diff --git a/src/alloc/system_alloc/defalloc.cpp b/src/alloc/system_alloc/defalloc.cpp index faaeb3c58..311057462 100644 --- a/src/alloc/system_alloc/defalloc.cpp +++ b/src/alloc/system_alloc/defalloc.cpp @@ -94,7 +94,7 @@ Allocator* Allocator::get(bool locking) if(alloc) return alloc; - throw Exception("Couldn't find an allocator to use in get_allocator"); + throw Internal_Error("Couldn't find an allocator to use in get_allocator"); } } diff --git a/src/block/lion/lion.cpp b/src/block/lion/lion.cpp index e590a78ce..d8dfd1fcb 100644 --- a/src/block/lion/lion.cpp +++ b/src/block/lion/lion.cpp @@ -116,8 +116,9 @@ Lion::Lion(HashFunction* hash_in, StreamCipher* sc_in, u32bit block_len) : { if(2*LEFT_SIZE + 1 > BLOCK_SIZE) throw Invalid_Argument(name() + ": Chosen block size is too small"); + if(!cipher->valid_keylength(LEFT_SIZE)) - throw Exception(name() + ": This stream/hash combination is invalid"); + throw Invalid_Argument(name() + ": This stream/hash combo is invalid"); key1.resize(LEFT_SIZE); key2.resize(LEFT_SIZE); diff --git a/src/cert/x509/x509_ext.cpp b/src/cert/x509/x509_ext.cpp index 9a03c9d23..69b21d8b3 100644 --- a/src/cert/x509/x509_ext.cpp +++ b/src/cert/x509/x509_ext.cpp @@ -473,7 +473,7 @@ MemoryVector<byte> Certificate_Policies::encode_inner() const { // FIXME #if 1 - throw Exception("Certificate_Policies::encode_inner: Bugged"); + throw Internal_Error("Certificate_Policies::encode_inner: Bugged"); #else std::vector<Policy_Information> policies; diff --git a/src/cms/cms_dalg.cpp b/src/cms/cms_dalg.cpp index 7ed793f4f..1fc4e2faa 100644 --- a/src/cms/cms_dalg.cpp +++ b/src/cms/cms_dalg.cpp @@ -195,7 +195,7 @@ void CMS_Decoder::decode_layer() else if(type == OIDS::lookup("CMS.SignedData")) { #if 1 - throw Exception("FIXME: not implemented"); + throw Internal_Error("FIXME: not implemented"); #else u32bit version; @@ -258,11 +258,11 @@ void CMS_Decoder::decode_layer() } else if(type == OIDS::lookup("CMS.EnvelopedData")) { - throw Exception("FIXME: not implemented"); + throw Internal_Error("FIXME: not implemented"); } else if(type == OIDS::lookup("CMS.AuthenticatedData")) { - throw Exception("FIXME: not implemented"); + throw Internal_Error("FIXME: not implemented"); } else throw Decoding_Error("CMS: Unknown content ID " + type.as_string()); diff --git a/src/cms/cms_ealg.cpp b/src/cms/cms_ealg.cpp index 2970e8e79..5efa33254 100644 --- a/src/cms/cms_ealg.cpp +++ b/src/cms/cms_ealg.cpp @@ -168,7 +168,7 @@ void CMS_Encoder::encrypt_kari(RandomNumberGenerator&, X509_PublicKey*, const std::string&) { - throw Exception("FIXME: unimplemented"); + throw Internal_Error("FIXME: unimplemented"); #if 0 SymmetricKey cek = setup_key(rng, cipher); @@ -198,7 +198,7 @@ void CMS_Encoder::encrypt(RandomNumberGenerator& rng, const SymmetricKey& kek, const std::string& user_cipher) { - throw Exception("FIXME: untested"); + throw Internal_Error("FIXME: untested"); const std::string cipher = choose_algo(user_cipher, "TripleDES"); SymmetricKey cek = setup_key(rng, cipher); @@ -232,7 +232,7 @@ void CMS_Encoder::encrypt(RandomNumberGenerator&, const std::string& user_cipher) { const std::string cipher = choose_algo(user_cipher, "TripleDES"); - throw Exception("FIXME: unimplemented"); + throw Internal_Error("FIXME: unimplemented"); /* SymmetricKey cek = setup_key(key); @@ -375,7 +375,7 @@ void CMS_Encoder::authenticate(const X509_Certificate&, const std::string& mac_algo) { const std::string mac = choose_algo(mac_algo, "HMAC(SHA-1)"); - throw Exception("FIXME: unimplemented"); + throw Internal_Error("FIXME: unimplemented"); } /* @@ -385,7 +385,7 @@ void CMS_Encoder::authenticate(const SymmetricKey&, const std::string& mac_algo) { const std::string mac = choose_algo(mac_algo, "HMAC(SHA-1)"); - throw Exception("FIXME: unimplemented"); + throw Internal_Error("FIXME: unimplemented"); } /* @@ -395,7 +395,7 @@ void CMS_Encoder::authenticate(const std::string&, const std::string& mac_algo) { const std::string mac = choose_algo(mac_algo, "HMAC(SHA-1)"); - throw Exception("FIXME: unimplemented"); + throw Internal_Error("FIXME: unimplemented"); } } diff --git a/src/constructs/cryptobox/cryptobox.cpp b/src/constructs/cryptobox/cryptobox.cpp index ba7553c55..20435fa59 100644 --- a/src/constructs/cryptobox/cryptobox.cpp +++ b/src/constructs/cryptobox/cryptobox.cpp @@ -135,7 +135,7 @@ std::string decrypt(const byte input[], u32bit input_len, if(!same_mem(computed_mac, ciphertext + VERSION_CODE_LEN + PBKDF_SALT_LEN, MAC_OUTPUT_LEN)) - throw Integrity_Failure("CryptoBox integrity failure"); + throw Decoding_Error("CryptoBox integrity failure"); return pipe.read_all_as_string(0); } diff --git a/src/filters/bzip2/bzip2.cpp b/src/filters/bzip2/bzip2.cpp index 4cdca5355..9dcee8fdf 100644 --- a/src/filters/bzip2/bzip2.cpp +++ b/src/filters/bzip2/bzip2.cpp @@ -96,7 +96,7 @@ void Bzip_Compression::start_msg() clear(); bz = new Bzip_Stream; if(BZ2_bzCompressInit(&(bz->stream), level, 0, 0) != BZ_OK) - throw Exception("Bzip_Compression: Memory allocation error"); + throw Memory_Exhaustion(); } /* @@ -196,13 +196,15 @@ void Bzip_Decompression::write(const byte input_arr[], u32bit length) if(rc != BZ_OK && rc != BZ_STREAM_END) { clear(); + if(rc == BZ_DATA_ERROR) throw Decoding_Error("Bzip_Decompression: Data integrity error"); - if(rc == BZ_DATA_ERROR_MAGIC) + else if(rc == BZ_DATA_ERROR_MAGIC) throw Decoding_Error("Bzip_Decompression: Invalid input"); - if(rc == BZ_MEM_ERROR) - throw Exception("Bzip_Decompression: Memory allocation error"); - throw Exception("Bzip_Decompression: Unknown decompress error"); + else if(rc == BZ_MEM_ERROR) + throw Memory_Exhaustion(); + else + throw std::runtime_error("Bzip2 decompression: Unknown error"); } send(buffer, buffer.size() - bz->stream.avail_out); @@ -228,7 +230,7 @@ void Bzip_Decompression::start_msg() bz = new Bzip_Stream; if(BZ2_bzDecompressInit(&(bz->stream), 0, small_mem) != BZ_OK) - throw Exception("Bzip_Decompression: Memory allocation error"); + throw Memory_Exhaustion(); no_writes = true; } @@ -252,7 +254,7 @@ void Bzip_Decompression::end_msg() if(rc != BZ_OK && rc != BZ_STREAM_END) { clear(); - throw Exception("Bzip_Decompression: Error finalizing decompression"); + throw Decoding_Error("Bzip_Decompression: Error finalizing"); } send(buffer, buffer.size() - bz->stream.avail_out); diff --git a/src/filters/modes/cbc/cbc.cpp b/src/filters/modes/cbc/cbc.cpp index 059b239e8..7722fad2f 100644 --- a/src/filters/modes/cbc/cbc.cpp +++ b/src/filters/modes/cbc/cbc.cpp @@ -79,7 +79,7 @@ void CBC_Encryption::buffered_final(const byte input[], u32bit length) if(length % cipher->BLOCK_SIZE == 0) buffered_block(input, length); else if(length != 0) - throw Exception(name() + ": Did not pad to full blocksize"); + throw Encoding_Error(name() + ": Did not pad to full blocksize"); } void CBC_Encryption::write(const byte input[], u32bit input_length) diff --git a/src/filters/modes/cts/cts.cpp b/src/filters/modes/cts/cts.cpp index 3a15a1d68..b27b9b3c5 100644 --- a/src/filters/modes/cts/cts.cpp +++ b/src/filters/modes/cts/cts.cpp @@ -101,7 +101,8 @@ void CTS_Encryption::write(const byte input[], u32bit length) void CTS_Encryption::end_msg() { if(position < cipher->BLOCK_SIZE + 1) - throw Exception("CTS_Encryption: insufficient data to encrypt"); + throw Encoding_Error(name() + ": insufficient data to encrypt"); + xor_buf(state, buffer, cipher->BLOCK_SIZE); cipher->encrypt(state); SecureVector<byte> cn = state; diff --git a/src/filters/modes/eax/eax_dec.cpp b/src/filters/modes/eax/eax_dec.cpp index f41327ffc..998773697 100644 --- a/src/filters/modes/eax/eax_dec.cpp +++ b/src/filters/modes/eax/eax_dec.cpp @@ -98,13 +98,13 @@ void EAX_Decryption::do_write(const byte input[], u32bit length) void EAX_Decryption::end_msg() { if((queue_end - queue_start) != TAG_SIZE) - throw Integrity_Failure(name() + ": Message authentication failure"); + throw Decoding_Error(name() + ": Message authentication failure"); SecureVector<byte> data_mac = cmac->final(); for(u32bit j = 0; j != TAG_SIZE; ++j) if(queue[queue_start+j] != (data_mac[j] ^ nonce_mac[j] ^ header_mac[j])) - throw Integrity_Failure(name() + ": Message authentication failure"); + throw Decoding_Error(name() + ": Message authentication failure"); queue_start = queue_end = 0; } diff --git a/src/filters/modes/ecb/ecb.cpp b/src/filters/modes/ecb/ecb.cpp index c13d0fd37..2ce6576e3 100644 --- a/src/filters/modes/ecb/ecb.cpp +++ b/src/filters/modes/ecb/ecb.cpp @@ -103,7 +103,7 @@ void ECB_Encryption::buffered_final(const byte input[], u32bit input_length) if(input_length % cipher->BLOCK_SIZE == 0) buffered_block(input, input_length); else if(input_length != 0) - throw Exception(name() + ": Did not pad to full blocksize"); + throw Encoding_Error(name() + ": Did not pad to full blocksize"); } /* diff --git a/src/filters/modes/xts/xts.cpp b/src/filters/modes/xts/xts.cpp index afeea24b5..cfea0b34b 100644 --- a/src/filters/modes/xts/xts.cpp +++ b/src/filters/modes/xts/xts.cpp @@ -168,7 +168,7 @@ void XTS_Encryption::buffered_block(const byte input[], u32bit length) void XTS_Encryption::buffered_final(const byte input[], u32bit length) { if(length <= cipher->BLOCK_SIZE) - throw Exception("XTS_Encryption: insufficient data to encrypt"); + throw Encoding_Error("XTS_Encryption: insufficient data to encrypt"); if(length % cipher->BLOCK_SIZE == 0) { @@ -323,7 +323,7 @@ void XTS_Decryption::buffered_block(const byte input[], u32bit input_length) void XTS_Decryption::buffered_final(const byte input[], u32bit input_length) { if(input_length <= cipher->BLOCK_SIZE) - throw Exception("XTS_Decryption: insufficient data to decrypt"); + throw Decoding_Error("XTS_Decryption: insufficient data to decrypt"); if(input_length % cipher->BLOCK_SIZE == 0) { diff --git a/src/filters/pipe_rw.cpp b/src/filters/pipe_rw.cpp index 756a1cf09..de77c6b43 100644 --- a/src/filters/pipe_rw.cpp +++ b/src/filters/pipe_rw.cpp @@ -34,7 +34,7 @@ Pipe::message_id Pipe::get_message_no(const std::string& func_name, void Pipe::write(const byte input[], u32bit length) { if(!inside_msg) - throw Exception("Cannot write to a Pipe while it is not processing"); + throw Invalid_State("Cannot write to a Pipe while it is not processing"); pipe->write(input, length); } diff --git a/src/filters/pk_filts/pk_filts.cpp b/src/filters/pk_filts/pk_filts.cpp index d604436e0..05b45227f 100644 --- a/src/filters/pk_filts/pk_filts.cpp +++ b/src/filters/pk_filts/pk_filts.cpp @@ -73,7 +73,7 @@ void PK_Verifier_Filter::write(const byte input[], u32bit length) void PK_Verifier_Filter::end_msg() { if(signature.empty()) - throw Exception("PK_Verifier_Filter: No signature to check against"); + throw Invalid_State("PK_Verifier_Filter: No signature to check against"); bool is_valid = verifier->check_signature(signature, signature.size()); send((is_valid ? 1 : 0)); } diff --git a/src/filters/zlib/zlib.cpp b/src/filters/zlib/zlib.cpp index 246e32924..171caa73f 100644 --- a/src/filters/zlib/zlib.cpp +++ b/src/filters/zlib/zlib.cpp @@ -95,7 +95,7 @@ void Zlib_Compression::start_msg() clear(); zlib = new Zlib_Stream; if(deflateInit(&(zlib->stream), level) != Z_OK) - throw Exception("Zlib_Compression: Memory allocation error"); + throw Memory_Exhaustion(); } /* @@ -187,7 +187,7 @@ void Zlib_Decompression::start_msg() clear(); zlib = new Zlib_Stream; if(inflateInit(&(zlib->stream)) != Z_OK) - throw Exception("Zlib_Decompression: Memory allocation error"); + throw Memory_Exhaustion(); } /* @@ -215,11 +215,12 @@ void Zlib_Decompression::write(const byte input_arr[], u32bit length) clear(); if(rc == Z_DATA_ERROR) throw Decoding_Error("Zlib_Decompression: Data integrity error"); - if(rc == Z_NEED_DICT) + else if(rc == Z_NEED_DICT) throw Decoding_Error("Zlib_Decompression: Need preset dictionary"); - if(rc == Z_MEM_ERROR) - throw Exception("Zlib_Decompression: Memory allocation error"); - throw Exception("Zlib_Decompression: Unknown decompress error"); + else if(rc == Z_MEM_ERROR) + throw Memory_Exhaustion(); + else + throw std::runtime_error("Zlib decompression: Unknown error"); } send(buffer.begin(), buffer.size() - zlib->stream.avail_out); @@ -258,7 +259,7 @@ void Zlib_Decompression::end_msg() if(rc != Z_OK && rc != Z_STREAM_END) { clear(); - throw Exception("Zlib_Decompression: Error finalizing decompression"); + throw Decoding_Error("Zlib_Decompression: Error finalizing"); } send(buffer.begin(), buffer.size() - zlib->stream.avail_out); diff --git a/src/math/gfpmath/point_gfp.cpp b/src/math/gfpmath/point_gfp.cpp index f95add9f7..4203a3db3 100644 --- a/src/math/gfpmath/point_gfp.cpp +++ b/src/math/gfpmath/point_gfp.cpp @@ -921,7 +921,7 @@ SecureVector<byte> EC2OSP(const PointGFp& point, byte format) } else { - throw Format_Error("illegal point encoding format specification"); + throw Invalid_Argument("illegal point encoding format specification"); } return result; } @@ -1078,7 +1078,7 @@ PointGFp OS2ECP(MemoryRegion<byte> const& os, const CurveGFp& curve) } break; default: - throw Format_Error("encountered illegal format specification while decoding point"); + throw Invalid_Argument("encountered illegal format specification while decoding point"); } z = GFpElement(curve.get_p(), BigInt(1)); //assert((x.is_trf_to_mres() && x.is_use_montgm()) || !x.is_trf_to_mres()); diff --git a/src/math/numbertheory/powm_mnt.cpp b/src/math/numbertheory/powm_mnt.cpp index e356387c0..2d18ccdef 100644 --- a/src/math/numbertheory/powm_mnt.cpp +++ b/src/math/numbertheory/powm_mnt.cpp @@ -156,10 +156,9 @@ BigInt Montgomery_Exponentiator::execute() const Montgomery_Exponentiator::Montgomery_Exponentiator(const BigInt& mod, Power_Mod::Usage_Hints hints) { - if(!mod.is_positive()) - throw Exception("Montgomery_Exponentiator: modulus must be positive"); - if(mod.is_even()) - throw Exception("Montgomery_Exponentiator: modulus must be odd"); + // Montgomery reduction only works for positive odd moduli + if(!mod.is_positive() || mod.is_even()) + throw Invalid_Argument("Montgomery_Exponentiator: invalid modulus"); window_bits = 0; this->hints = hints; diff --git a/src/mutex/pthreads/mux_pthr.cpp b/src/mutex/pthreads/mux_pthr.cpp index 75a116fe8..165132239 100644 --- a/src/mutex/pthreads/mux_pthr.cpp +++ b/src/mutex/pthreads/mux_pthr.cpp @@ -28,19 +28,19 @@ Mutex* Pthread_Mutex_Factory::make() void lock() { if(pthread_mutex_lock(&mutex) != 0) - throw Exception("Pthread_Mutex::lock: Error occured"); + throw Invalid_State("Pthread_Mutex::lock: Error occured"); } void unlock() { if(pthread_mutex_unlock(&mutex) != 0) - throw Exception("Pthread_Mutex::unlock: Error occured"); + throw Invalid_State("Pthread_Mutex::unlock: Error occured"); } Pthread_Mutex() { if(pthread_mutex_init(&mutex, 0) != 0) - throw Exception("Pthread_Mutex: initialization failed"); + throw Invalid_State("Pthread_Mutex: initialization failed"); } ~Pthread_Mutex() diff --git a/src/pk_pad/eme1/eme1.cpp b/src/pk_pad/eme1/eme1.cpp index 13f68f8e4..d99ffaf58 100644 --- a/src/pk_pad/eme1/eme1.cpp +++ b/src/pk_pad/eme1/eme1.cpp @@ -21,7 +21,7 @@ SecureVector<byte> EME1::pad(const byte in[], u32bit in_length, key_length /= 8; if(in_length > key_length - 2*HASH_LENGTH - 1) - throw Exception("EME1: Input is too large"); + throw Invalid_Argument("EME1: Input is too large"); SecureVector<byte> out(key_length); diff --git a/src/pubkey/dl_group/dl_group.cpp b/src/pubkey/dl_group/dl_group.cpp index a2e239783..7940e69b2 100644 --- a/src/pubkey/dl_group/dl_group.cpp +++ b/src/pubkey/dl_group/dl_group.cpp @@ -193,7 +193,7 @@ const BigInt& DL_Group::get_q() const { init_check(); if(q == 0) - throw Format_Error("DLP group has no q prime specified"); + throw Invalid_State("DLP group has no q prime specified"); return q; } @@ -325,7 +325,7 @@ BigInt DL_Group::make_dsa_generator(const BigInt& p, const BigInt& q) } if(g == 1) - throw Exception("DL_Group: Couldn't create a suitable generator"); + throw Internal_Error("DL_Group: Couldn't create a suitable generator"); return g; } diff --git a/src/pubkey/dlies/dlies.cpp b/src/pubkey/dlies/dlies.cpp index 3a3ab52ee..6ef3292e1 100644 --- a/src/pubkey/dlies/dlies.cpp +++ b/src/pubkey/dlies/dlies.cpp @@ -127,7 +127,7 @@ SecureVector<byte> DLIES_Decryptor::dec(const byte msg[], u32bit length) const mac->update(0); SecureVector<byte> T2 = mac->final(); if(T != T2) - throw Integrity_Failure("DLIES: message authentication failed"); + throw Decoding_Error("DLIES: message authentication failed"); xor_buf(C, K.begin() + mac_keylen, C.size()); diff --git a/src/pubkey/dsa/dsa.cpp b/src/pubkey/dsa/dsa.cpp index b0688ae0d..5be3e1d48 100644 --- a/src/pubkey/dsa/dsa.cpp +++ b/src/pubkey/dsa/dsa.cpp @@ -97,6 +97,8 @@ SecureVector<byte> DSA_PrivateKey::sign(const byte in[], u32bit length, { const BigInt& q = group_q(); + rng.add_entropy(in, length); + BigInt k; do k.randomize(rng, q.bits()); diff --git a/src/pubkey/pubkey.cpp b/src/pubkey/pubkey.cpp index 44e31159c..9b5c9180b 100644 --- a/src/pubkey/pubkey.cpp +++ b/src/pubkey/pubkey.cpp @@ -73,7 +73,7 @@ PK_Encryptor_MR_with_EME::enc(const byte msg[], message.set(msg, length); if(8*(message.size() - 1) + high_bit(message[0]) > key.max_input_bits()) - throw Exception("PK_Encryptor_MR_with_EME: Input is too large"); + throw Invalid_Argument("PK_Encryptor_MR_with_EME: Input is too large"); return key.encrypt(message, message.size(), rng); } @@ -113,11 +113,11 @@ SecureVector<byte> PK_Decryptor_MR_with_EME::dec(const byte msg[], } catch(Invalid_Argument) { - throw Exception("PK_Decryptor_MR_with_EME: Input is invalid"); + throw Decoding_Error("PK_Decryptor_MR_with_EME: Input is invalid"); } catch(Decoding_Error) { - throw Exception("PK_Decryptor_MR_with_EME: Input is invalid"); + throw Decoding_Error("PK_Decryptor_MR_with_EME: Input is invalid"); } } diff --git a/src/s2k/pbkdf1/pbkdf1.cpp b/src/s2k/pbkdf1/pbkdf1.cpp index 04e3aa453..fcc5b9a97 100644 --- a/src/s2k/pbkdf1/pbkdf1.cpp +++ b/src/s2k/pbkdf1/pbkdf1.cpp @@ -18,10 +18,10 @@ OctetString PKCS5_PBKDF1::derive(u32bit key_len, u32bit iterations) const { if(iterations == 0) - throw Invalid_Argument("PKCS#5 PBKDF1: Invalid iteration count"); + throw Invalid_Argument("PKCS5_PBKDF1: Invalid iteration count"); if(key_len > hash->OUTPUT_LENGTH) - throw Exception("PKCS#5 PBKDF1: Requested output length too long"); + throw Invalid_Argument("PKCS5_PBKDF1: Requested output length too long"); hash->update(passphrase); hash->update(salt, salt_size); diff --git a/src/stream/stream_cipher.h b/src/stream/stream_cipher.h index 29c16c8b5..cb6fb3481 100644 --- a/src/stream/stream_cipher.h +++ b/src/stream/stream_cipher.h @@ -42,8 +42,8 @@ class BOTAN_DLL StreamCipher : public SymmetricAlgorithm virtual void set_iv(const byte[], u32bit iv_len) { if(iv_len) - throw Exception("The stream cipher " + name() + - " does not support resyncronization"); + throw Invalid_Argument("The stream cipher " + name() + + " does not support resyncronization"); } /** diff --git a/src/utils/exceptn.h b/src/utils/exceptn.h index d106de1c0..86efebc7c 100644 --- a/src/utils/exceptn.h +++ b/src/utils/exceptn.h @@ -1,6 +1,6 @@ /* * Exceptions -* (C) 1999-2007 Jack Lloyd +* (C) 1999-2009 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -16,32 +16,8 @@ namespace Botan { -/* -* Exception Base Class -*/ -class BOTAN_DLL Exception : public std::exception - { - public: - Exception(const std::string& m = "Unknown error") : - msg("Botan: " + m) - {} - - const char* what() const throw() { return msg.c_str(); } - - virtual ~Exception() throw() {} - private: - std::string msg; - }; - -/* -* Invalid_Argument Exception -*/ -struct BOTAN_DLL Invalid_Argument : public Exception - { - Invalid_Argument(const std::string& err = "") : - Exception(err) - {} - }; +typedef std::runtime_error Exception; +typedef std::invalid_argument Invalid_Argument; /* * Invalid_State Exception @@ -54,16 +30,6 @@ struct BOTAN_DLL Invalid_State : public Exception }; /* -* Format_Error Exception -*/ -struct BOTAN_DLL Format_Error : public Exception - { - Format_Error(const std::string& err = "") : - Exception(err) - {} - }; - -/* * Lookup_Error Exception */ struct BOTAN_DLL Lookup_Error : public Exception @@ -150,29 +116,29 @@ struct BOTAN_DLL Algorithm_Not_Found : public Lookup_Error /* * Invalid_Algorithm_Name Exception */ -struct BOTAN_DLL Invalid_Algorithm_Name : public Format_Error +struct BOTAN_DLL Invalid_Algorithm_Name : public Invalid_Argument { Invalid_Algorithm_Name(const std::string& name): - Format_Error("Invalid algorithm name: " + name) + Invalid_Argument("Invalid algorithm name: " + name) {} }; /* * Encoding_Error Exception */ -struct BOTAN_DLL Encoding_Error : public Format_Error +struct BOTAN_DLL Encoding_Error : public Invalid_Argument { Encoding_Error(const std::string& name) : - Format_Error("Encoding error: " + name) {} + Invalid_Argument("Encoding error: " + name) {} }; /* * Decoding_Error Exception */ -struct BOTAN_DLL Decoding_Error : public Format_Error +struct BOTAN_DLL Decoding_Error : public Invalid_Argument { Decoding_Error(const std::string& name) : - Format_Error("Decoding error: " + name) {} + Invalid_Argument("Decoding error: " + name) {} }; /* @@ -195,37 +161,22 @@ struct BOTAN_DLL Stream_IO_Error : public Exception }; /* -* Configuration Error Exception -*/ -struct BOTAN_DLL Config_Error : public Format_Error - { - Config_Error(const std::string& err) : - Format_Error("Config error: " + err) - {} - - Config_Error(const std::string& err, u32bit line) : - Format_Error("Config error at line " + to_string(line) + ": " + err) - {} - }; - -/* -* Integrity Failure Exception +* Self Test Failure Exception */ -struct BOTAN_DLL Integrity_Failure : public Internal_Error +struct BOTAN_DLL Self_Test_Failure : public Internal_Error { - Integrity_Failure(const std::string& err) : - Internal_Error("Integrity failure: " + err) + Self_Test_Failure(const std::string& err) : + Internal_Error("Self test failed: " + err) {} }; /* -* Self Test Failure Exception +* Memory Allocation Exception */ -struct BOTAN_DLL Self_Test_Failure : public Internal_Error +struct BOTAN_DLL Memory_Exhaustion : public std::bad_alloc { - Self_Test_Failure(const std::string& err) : - Internal_Error("Self test failed: " + err) - {} + const char* what() const throw() + { return "Ran out of memory, allocation failed"; } }; } diff --git a/src/utils/parsing.cpp b/src/utils/parsing.cpp index 58a8e0b38..4b99ac1ec 100644 --- a/src/utils/parsing.cpp +++ b/src/utils/parsing.cpp @@ -168,7 +168,7 @@ std::vector<std::string> split_on(const std::string& str, char delim) } if(substr == "") - throw Format_Error("Unable to split string: " + str); + throw Invalid_Argument("Unable to split string: " + str); elems.push_back(substr); return elems; |