From d6cc28665d2025afba1f3701a486927f0b3a51da Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sat, 8 Oct 2016 22:03:52 +0200 Subject: Lots of renaming (mainly {Public,Private}Key -> Key) --- envelope.cpp | 24 +++++++++++++----------- envelope.h | 4 ++-- rsa.cpp | 55 ++++++++++++++++++++++++++----------------------------- rsa.h | 19 ++++++++----------- 4 files changed, 49 insertions(+), 53 deletions(-) diff --git a/envelope.cpp b/envelope.cpp index 4674121..5270dae 100644 --- a/envelope.cpp +++ b/envelope.cpp @@ -17,24 +17,25 @@ namespace Envelope{ return true; //fine } - string encrypt(const string &data,const RSA::PublicKey &pubkey){ + string encrypt(const string &data,const RSA::Key &pubkey){ const int keylen=8; //256-bit CryptoRng crng; - string key(4*keylen,'\0'); + string aeskey(4*keylen,'\0'); do { - for(int i=0;i1&&msg genkeys(int nbits,Rng &rng){ pair 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 genkeys(int nbits){ CryptoRng rng; - return genkey(nbits,rng); + return genkeys(nbits,rng); } - PrivateKey genkey(int nbits,const string &password){ + pair genkeys(int nbits,const string &password){ KeyRng rng(password); - return genkey(nbits,rng); + return genkeys(nbits,rng); } - pair 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; } diff --git a/rsa.h b/rsa.h index f931ab3..bed8400 100644 --- a/rsa.h +++ b/rsa.h @@ -6,22 +6,19 @@ namespace RSA{ - struct PublicKey{ + struct Key{ Bigint mod,exp; }; - struct PrivateKey{ - PublicKey pub; - Bigint pexp; - }; + Bigint encrypt(Bigint msg,const Key &key); + Bigint decrypt(Bigint msg,const Key &key); - Bigint encrypt(Bigint msg,const PublicKey &key); - Bigint decrypt(Bigint msg,const PrivateKey &key); + std::pair genkeys(int nbits); //nbits is target number of bits of modulus + std::pair genkeys(int nbits,const std::string &password); //generates keys seeded by password - PrivateKey genkey(int nbits); //nbits is target number of bits of modulus - PrivateKey genkey(int nbits,const std::string &password); //generates key seeded by password + std::string exportKey(const Key&); - std::pair exportkey(const PrivateKey&); //returns {pub,priv} - PrivateKey importkey(const std::string &pub,const std::string &priv); + //throws invalid_argument if the imported key is of invalid format + Key importKey(const std::string &repr); } -- cgit v1.2.3-70-g09d2