aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--aes.cpp4
-rw-r--r--aes.h2
-rw-r--r--bigint.cpp2
-rw-r--r--bigint.h4
-rw-r--r--envelope.cpp10
-rwxr-xr-xenvelope/envelopebin0 -> 121056 bytes
-rw-r--r--envelope/main.cpp23
-rw-r--r--rng.cpp4
-rw-r--r--rng.h2
9 files changed, 40 insertions, 11 deletions
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<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
new file mode 100755
index 0000000..599c263
--- /dev/null
+++ b/envelope/envelope
Binary files differ
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){
diff --git a/rng.cpp b/rng.cpp
index e06b8be..fc86993 100644
--- a/rng.cpp
+++ b/rng.cpp
@@ -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();
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();