aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-10-23 18:57:02 +0200
committertomsmeding <tom.smeding@gmail.com>2016-10-23 18:57:02 +0200
commitc5163d30258178ac66e05ac491935ab710913917 (patch)
tree5ddffd5600e79e619786040ba22d25b5c67dc2d7
parent79ae4ca5d81d7f809cfe282c2b607a27e00f60e5 (diff)
Don't assert out when 65537|phi
-rw-r--r--rsa.cpp26
1 files changed, 15 insertions, 11 deletions
diff --git a/rsa.cpp b/rsa.cpp
index 34b47f9..4adfa2c 100644
--- a/rsa.cpp
+++ b/rsa.cpp
@@ -22,17 +22,21 @@ namespace RSA{
}
pair<Key,Key> genkeys(int nbits,Rng &rng){
- pair<Bigint,Bigint> pq=genprimepair(rng,nbits);
- Key pubkey,privkey;
- pubkey.mod=privkey.mod=pq.first*pq.second;
- pubkey.exp=65537;
- Bigint x;
- Bigint phi((pq.first-Bigint::one)*(pq.second-Bigint::one));
- assert(egcd(phi,pubkey.exp,x,privkey.exp)==1);
- privkey.exp=privkey.exp.divmod(phi).second;
- // cerr<<"pubkey = {"<<pubkey.mod<<" , "<<pubkey.exp<<'}'<<endl;
- // cerr<<"privkey = {"<<privkey.mod<<" , "<<privkey.exp<<'}'<<endl;
- return make_pair(pubkey,privkey);
+ while(true){ //retry loop for if invalid primes were generated
+ pair<Bigint,Bigint> pq=genprimepair(rng,nbits);
+ Key pubkey,privkey;
+ pubkey.mod=privkey.mod=pq.first*pq.second;
+ pubkey.exp=65537;
+ Bigint x;
+ Bigint phi((pq.first-Bigint::one)*(pq.second-Bigint::one));
+ if(egcd(phi,pubkey.exp,x,privkey.exp)!=1){
+ continue; //p-1 or q-1 is divisible by pubkey.exp=65537
+ }
+ privkey.exp=privkey.exp.divmod(phi).second;
+ // cerr<<"pubkey = {"<<pubkey.mod<<" , "<<pubkey.exp<<'}'<<endl;
+ // cerr<<"privkey = {"<<privkey.mod<<" , "<<privkey.exp<<'}'<<endl;
+ return make_pair(pubkey,privkey);
+ }
}
pair<Key,Key> genkeys(int nbits){