aboutsummaryrefslogtreecommitdiff
path: root/rsa.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'rsa.cpp')
-rw-r--r--rsa.cpp55
1 files changed, 26 insertions, 29 deletions
diff --git a/rsa.cpp b/rsa.cpp
index e1bf3a8..09ea1bc 100644
--- a/rsa.cpp
+++ b/rsa.cpp
@@ -1,3 +1,4 @@
+#include <algorithm>
#include <cstdint>
#include <cassert>
#include "base64.h"
@@ -10,55 +11,51 @@ using namespace std;
namespace RSA{
- Bigint encrypt(Bigint msg,const PublicKey &pubkey){
+ Bigint encrypt(Bigint msg,const Key &pubkey){
assert(msg>1&&msg<pubkey.mod);
return expmod(msg,pubkey.exp,pubkey.mod);
}
- Bigint decrypt(Bigint encr,const PrivateKey &privkey){
- return expmod(encr,privkey.pexp,privkey.pub.mod);
+ Bigint decrypt(Bigint encr,const Key &privkey){
+ return expmod(encr,privkey.exp,privkey.mod);
}
- PrivateKey genkey(int nbits,Rng &rng){
+ pair<Key,Key> genkeys(int nbits,Rng &rng){
pair<Bigint,Bigint> pq=genprimepair(rng,nbits);
- PrivateKey key;
- key.pub.mod=pq.first*pq.second;
- key.pub.exp=65537;
+ Key pubkey,privkey;
+ pubkey.mod=privkey.mod=pq.first*pq.second;
+ pubkey.exp=65537;
Bigint x;
- egcd((pq.first-Bigint::one)*(pq.second-Bigint::one),key.pub.exp,x,key.pexp);
- return key;
+ assert(egcd((pq.first-Bigint::one)*(pq.second-Bigint::one),pubkey.exp,x,privkey.exp)==1);
+ return make_pair(pubkey,privkey);
}
- PrivateKey genkey(int nbits){
+ pair<Key,Key> genkeys(int nbits){
CryptoRng rng;
- return genkey(nbits,rng);
+ return genkeys(nbits,rng);
}
- PrivateKey genkey(int nbits,const string &password){
+ pair<Key,Key> genkeys(int nbits,const string &password){
KeyRng rng(password);
- return genkey(nbits,rng);
+ return genkeys(nbits,rng);
}
- pair<string,string> exportkey(const PrivateKey &key){
- string modser=key.pub.mod.serialiseMantissa();
+ string exportKey(const Key &key){
+ string modser=key.mod.serialiseMantissa();
int32_t modlen=modser.size();
string modlenstr{(char)(modlen&0xff),(char)((modlen>>8)&0xff),(char)((modlen>>16)&0xff),(char)((modlen>>24)&0xff)};
- return make_pair(
- Base64::encode(modlenstr + modser + key.pub.exp.serialiseMantissa()),
- Base64::encode(modlenstr + modser + key.pexp.serialiseMantissa()));
+ return Base64::encode(modlenstr + modser + key.exp.serialiseMantissa());
}
- PrivateKey importkey(const string &pub,const string &priv){
- string pubdeser=Base64::decode(pub);
- assert(pubdeser.size()>4);
- int modlen=(uint8_t)pubdeser[0]+((uint8_t)pubdeser[1]<<8)+
- ((uint8_t)pubdeser[2]<<16)+((uint8_t)pubdeser[3]<<24);
- assert((int)pubdeser.size()-4>modlen);
- PrivateKey key;
- key.pub.mod.deserialiseMantissa(string(pubdeser.begin()+4,pubdeser.begin()+(4+modlen)));
- key.pub.exp.deserialiseMantissa(string(pubdeser.begin()+(4+modlen),pubdeser.end()));
- string privdeser=Base64::decode(priv);
- key.pexp.deserialiseMantissa(string(privdeser.begin()+(4+modlen),privdeser.end()));
+ Key importKey(const string &repr){
+ string deser=Base64::decode(repr);
+ if(deser.size()<=4)throw invalid_argument("Invalid key string length");
+ int modlen=(uint8_t)deser[0]+((uint8_t)deser[1]<<8)+
+ ((uint8_t)deser[2]<<16)+((uint8_t)deser[3]<<24);
+ if((int)deser.size()-4-modlen<=0)throw invalid_argument("Key string incomplete");
+ Key key;
+ key.mod.deserialiseMantissa(string(deser.begin()+4,deser.begin()+(4+modlen)));
+ key.exp.deserialiseMantissa(string(deser.begin()+(4+modlen),deser.end()));
return key;
}