From cbea7bc041e5c14670042e3e52f0786f9561af05 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sun, 9 Oct 2016 21:43:38 +0200 Subject: Better error handling (exceptions) --- aes.cpp | 4 +++- aes.h | 2 +- bigint.cpp | 2 +- bigint.h | 4 +++- envelope.cpp | 10 ++++++++-- envelope/envelope | Bin 0 -> 121056 bytes envelope/main.cpp | 23 ++++++++++++++++++++--- rng.cpp | 4 ++-- rng.h | 2 ++ 9 files changed, 40 insertions(+), 11 deletions(-) create mode 100755 envelope/envelope diff --git a/aes.cpp b/aes.cpp index 29fc7fc..7430717 100644 --- a/aes.cpp +++ b/aes.cpp @@ -248,7 +248,9 @@ namespace AES{ case AES_256_CBC: increment=2; break; default: assert(false); } - assert((int)key.size()==4*(4+2*increment)); + if((int)key.size()!=4*(4+2*increment)){ + throw invalid_argument("Invalid AES key length"); + } return decryptCBC(data,key,10+2*increment); } diff --git a/aes.h b/aes.h index f531395..9274d21 100644 --- a/aes.h +++ b/aes.h @@ -12,7 +12,7 @@ namespace AES{ std::string encrypt(const std::string &data,const std::string &key,Algorithm algo); - //throws invalid_argument for an invalid ciphertext (length not a multiple of block size, or padding malformed) + //throws invalid_argument for an invalid ciphertext (length not a multiple of block size, or padding malformed) or an invalid key (invalid length) std::string decrypt(const std::string &data,const std::string &key,Algorithm algo); } diff --git a/bigint.cpp b/bigint.cpp index 8cd06ab..c25a2c1 100644 --- a/bigint.cpp +++ b/bigint.cpp @@ -521,7 +521,7 @@ string Bigint::serialiseMantissa() const { } void Bigint::deserialiseMantissa(const string &s){ - assert(s.size()%sizeof(digit_t)==0); + if(s.size()%sizeof(digit_t)!=0)throw invalid_argument("Not a serialised Bigint"); sign=1; int sz=s.size()/sizeof(digit_t); digits.resize(sz); diff --git a/bigint.h b/bigint.h index a4c29d6..a3c5be3 100644 --- a/bigint.h +++ b/bigint.h @@ -93,7 +93,9 @@ public: bool odd() const; std::string serialiseMantissa() const; //stores everything but the sign - void deserialiseMantissa(const std::string&); //restores non-negative number + + //restores non-negative number; can throw invalid_argument + void deserialiseMantissa(const std::string&); std::vector bits() const; friend std::istream& operator>>(std::istream&,Bigint&); diff --git a/envelope.cpp b/envelope.cpp index c881815..33ac8c4 100644 --- a/envelope.cpp +++ b/envelope.cpp @@ -105,7 +105,9 @@ namespace Envelope{ bytes.push_back(res.lowdigits()&0xff); res>>=8; } - assert(res==0); + if(res!=0){ + throw invalid_argument("Envelope RSA private key incorrect"); + } string decrkey(bytes.size(),'\0'); for(int i=0;i<(int)bytes.size();i++)decrkey[bytes.size()-1-i]=bytes[i]; #ifdef DEBUG @@ -115,7 +117,11 @@ namespace Envelope{ #ifdef DEBUG cerr<<"payload="<0); + if(keylen<=0)throw invalid_argument("KeyRng: Key should not be empty"); assert(key_); key=new uint8_t[keylen]; memcpy(key,key_,keylen); @@ -32,7 +32,7 @@ KeyRng::KeyRng(const char *key_,int keylen_) KeyRng::KeyRng(const string &key_) :keylen(key_.size()),idx(0),state(0){ - assert(keylen>0); + if(keylen==0)throw invalid_argument("KeyRng: Key should not be empty"); key=new uint8_t[keylen]; memcpy(key,key_.data(),keylen); stir(); diff --git a/rng.h b/rng.h index e843bbd..9ce2abd 100644 --- a/rng.h +++ b/rng.h @@ -18,8 +18,10 @@ class KeyRng : public Rng{ void stir(); public: + //throws invalid_argument if keylen<=0 KeyRng(const char *key,int keylen); explicit KeyRng(const std::string &key); + KeyRng(const Rng&)=delete; //just keep it at one KeyRng please ~KeyRng(); -- cgit v1.2.3-54-g00ecf