diff options
-rw-r--r-- | aes.cpp | 4 | ||||
-rw-r--r-- | aes.h | 2 | ||||
-rw-r--r-- | bigint.cpp | 2 | ||||
-rw-r--r-- | bigint.h | 4 | ||||
-rw-r--r-- | envelope.cpp | 10 | ||||
-rwxr-xr-x | envelope/envelope | bin | 0 -> 121056 bytes | |||
-rw-r--r-- | envelope/main.cpp | 23 | ||||
-rw-r--r-- | rng.cpp | 4 | ||||
-rw-r--r-- | rng.h | 2 |
9 files changed, 40 insertions, 11 deletions
@@ -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); } @@ -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); } @@ -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); @@ -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<bool> 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="<<Base64::encode(data.substr(0,data.size()-2-encrkeylen))<<endl; #endif - return AES::decrypt(data.substr(0,data.size()-2-encrkeylen),decrkey,AES::AES_256_CBC); + try { + return AES::decrypt(data.substr(0,data.size()-2-encrkeylen),decrkey,AES::AES_256_CBC); + } catch(invalid_argument){ + throw invalid_argument("Envelope RSA private key incorrect"); + } } } diff --git a/envelope/envelope b/envelope/envelope Binary files differnew file mode 100755 index 0000000..599c263 --- /dev/null +++ b/envelope/envelope diff --git a/envelope/main.cpp b/envelope/main.cpp index f0e544e..3889c2a 100644 --- a/envelope/main.cpp +++ b/envelope/main.cpp @@ -52,7 +52,13 @@ void mode_keygen(int keylength){ } void mode_encrypt(const string &pubkeyrepr){ - RSA::Key key(RSA::importKey(pubkeyrepr)); + RSA::Key key; + try { + key=RSA::importKey(pubkeyrepr); + } catch(invalid_argument){ + cerr<<"The given public key is not a valid key!"<<endl; + exit(1); + } string data; char buf[1024]; while(cin){ @@ -65,7 +71,13 @@ void mode_encrypt(const string &pubkeyrepr){ } void mode_decrypt(const string &privkeyrepr){ - RSA::Key key(RSA::importKey(privkeyrepr)); + RSA::Key key; + try { + key=RSA::importKey(privkeyrepr); + } catch(invalid_argument){ + cerr<<"The given private key is not a valid key!"<<endl; + exit(1); + } string data; char buf[1024]; while(cin){ @@ -74,7 +86,12 @@ void mode_decrypt(const string &privkeyrepr){ if(nread==0)continue; data.append(buf,nread); } - cout<<Envelope::decrypt(Base64::decode(data),key)<<flush; + try { + cout<<Envelope::decrypt(Base64::decode(data),key)<<flush; + } catch(invalid_argument){ + cerr<<"Private key doesn't match encrypted text!"<<endl; + exit(1); + } } int main(int argc,char **argv){ @@ -23,7 +23,7 @@ inline uint64_t rotr64(uint64_t x,uint32_t n){ KeyRng::KeyRng(const char *key_,int keylen_) :keylen(keylen_),idx(0),state(0){ - assert(keylen>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(); @@ -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(); |