-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA.cpp
394 lines (298 loc) · 6.71 KB
/
RSA.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "RSA.h"
#include "BigInt.h"
#include <cstdlib>
#include <cmath>
#include <limits>
#include <iostream>
#include <ctime>
// Author Cynthia Sturton
namespace RSAUtil
{
#define A_MAX 25
#define RAND_LIMIT 0xFFFF
RSA::RSA(int p1, int q1){
RSA::e = 0;
RSA::d = 0;
RSA::p = p1;
RSA::q = q1;
srand(time(0));
RSA::n = BigInt(RSA::p)*BigInt(RSA::q);
RSA::phi = BigInt(((RSA::p)-1))*BigInt(((RSA::q)-1));
}
RSA::RSA(int p1){
bool isP;
RSA::e = 0;
RSA::d = 0;
RSA::p = p1;
srand(time(0));
//Find q that is prime and not equal to p. Check that p!=q first
do{
RSA::q = int(((double)std::rand()/RAND_MAX)*RAND_LIMIT);
//set the low bit and high bit.
RSA::q = RSA::q | 0x10001;
isP = isPrime(RSA::q);
}while((RSA::p==RSA::q) || !isP);
RSA::n = BigInt(RSA::p)*BigInt(RSA::q);
RSA::phi = BigInt(((RSA::p)-1))*BigInt(((RSA::q)-1));
}
RSA::RSA()
{
bool isP;
RSA::e = 0;
RSA::d = 0;
//find p & q, s.t. p!=q && p and q are both prime.
srand(time(0));
do{
RSA::p = int(((double)std::rand()/RAND_MAX)*RAND_LIMIT);
// Set low bit (for oddness) and high bit (to make sure it is large enough).
RSA::p = RSA::p | 0x10001;
isP = isPrime(RSA::p);
}while(!isP);
//Find q that is prime and not equal to p. Check that p!=q first since
//that is the easier check.
do{
RSA::q = int(((double)std::rand()/RAND_MAX)*RAND_LIMIT);
//set the low bit and high bit.
RSA::q = RSA::q | 0x10001;
isP = isPrime(RSA::q);
}while((RSA::p==RSA::q) || !isP);
RSA::n = BigInt(RSA::p)*BigInt(RSA::q);
RSA::phi = BigInt(((RSA::p)-1))*BigInt(((RSA::q)-1));
}
RSA::~RSA()
{
}
void RSA::setPublicKey(unsigned int pubKey){
RSA::e = pubKey;
}
// The 2 functions below added by Raghunathan Srinivasan
void RSA::setN(BigInt B)
{
RSA::n = B;
}
// end of func
// overloaded function created by Raghu
void RSA::setPublicKey(BigInt B)
{
RSA::e = B;
}
// end of code addition
int RSA::getP() const{
return RSA::p;
}
int RSA::getQ() const{
return RSA::q;
}
BigInt RSA::getPublicKey(){
//If e has not been set, calculate e, o/w just return it.
if(RSA::e.isZero()){
calcE();
}
return RSA::e;
}
BigInt RSA::getPrivateKey(){
//If d has not been set, calculate d, o/w just return it.
if(RSA::d.isZero()){
calcD();
}
return RSA::d;
}
BigInt RSA::getPHI() const{
return RSA::phi;
}
BigInt RSA::getModulus() const{
return RSA::n;
}
//calculates m^e mod n
BigInt RSA::encrypt(BigInt msg){
BigInt cipher;
if(RSA::e.isZero()){
calcE();
}
cipher = RSAUtil::modPow(msg, RSA::e, RSA::n);
return cipher;
}
//calculates c^d mod n
BigInt RSA::decrypt(BigInt cipher){
BigInt message;
if(RSA::d.isZero()){
calcD();
}
//Do decryption
message = RSAUtil::modPow(cipher, RSA::d, RSA::n);
return message;
}
/** test code by raghu */
/* end of test code*/
void RSA::calcE(){
//Find e such that 1 < e < PHI, and e is relatively prime to PHI
BigInt r;
unsigned int high, low;
bool done = false;
BigInt tempPhi;
tempPhi = RSA::phi;
while(!done){
//need to generate a 32-34 bit random number.
//generate 32 bit random num.
//add 33rd bit. either 0,1,or 2.
low = int(((double)std::rand()/RAND_MAX)*0xFFFFFFFF);
high = int(((double)std::rand()/RAND_MAX)*0x02);
r = BigInt(high,low);
//Make sure r is in the middle 2/3 of PHI.
if((r>(RSA::phi/6)) && r<((RSA::phi/6)*5) ){
r |= 0x01;
done = (gcd(RSA::phi, r) == 1);
}
}//end while loop.
RSA::e = r;
}
void RSA::calcD(){
//Find d such that de = 1 (mod PHI). d exists if e and PHI are relatively prime.
BigInt response;
if(RSA::e.isZero()){
calcE();
}
response = modInverse(RSA::e, RSA::phi);
RSA::d = response;
}
/******* Test code added by Raghu *******/
/* end of test code by raghu ****/
//Composite testing.
bool isPrime(int p){
bool isP;
//Check if it is divisible by a small prime.
isP = isPrimeDiv(p);
//If it isn't, check for primality using Miller-Rabin algorithm.
if(isP){
isP = isPrimeMR(p);
}
return isP;
}
bool isPrimeMR(int p){
unsigned int a, b, m, j, tempPow2, pow2;
bool maybePrime = true;
BigInt z;
//Easy check.
if(p == 2){
return true;
}
//Check for even numbers.
if(!(p & 0x1)){
return false;
}
//Calculate m, such that p = 1 + (pow2*m), where pow2 is the largest power
//of 2 that divides p-1.
pow2 = 1;
tempPow2 = 2;
b = 0;
while((p-1)%tempPow2 == 0){
b++;
pow2 = tempPow2;
//multiply by 2.
tempPow2 = tempPow2 << 1;
}
m = (p-1)/pow2;
int iter = 0;
while(iter < 5 && maybePrime){
//a must be less than p.
do{
a = int(((double)std::rand()/RAND_MAX)* A_MAX);
}while(a >= p);
if(a==0 || a==1){
a = 2;
}
j = 0;
z = RSAUtil::modPow(BigInt(a), BigInt(m), BigInt(p));
if(z==1 || z==(p-1)){
//p passes. it may be prime.
maybePrime = true;
}else{
j++;
while(j<b && !(z==(p-1)) && !(z==1)){
z = modPow(z, (unsigned int)2, (unsigned int)p);
j++;
}
if(z == 1){
//p is not prime.
maybePrime = false;
}
else if(j == b && !(z == (p-1))){
//p is not prime.
maybePrime = false;
}
if(z == (p-1)){
//p may be prime.
maybePrime = true;
}
}
iter ++;
}//end while loop.
return maybePrime;
}
bool isPrimeDiv(int p){
// Test all primes < 256.
//use a wheel to generate 1st 2000 primes.
bool response = true;
int primes[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251};
int plength = sizeof(primes)/sizeof(int);
for(int i = 0; i < plength; i++){
if(p%primes[i] == 0){
response = false;
break;
}
}
return response;
}
int gcd(int i, int j){
if(j == 0){
return i;
}
else{
return gcd(j, i%j);
}
}
BigInt gcd(BigInt i, BigInt j){
if(j==0){
return i;
}
else{
return gcd(j, i%j);
}
}
//extended Euclidean algorithm. Find b s.t. ab = 1 mod m
BigInt modInverse(BigInt a, BigInt m){
bool neg = false;
BigInt b;
BigInt u1,u2,u3,v1,v2,v3,t1,t2,t3,q;
u1 = 1;
u2 = 0;
u3 = m;
v1 = 0;
v2 = 1;
v3 = a;
while(!((u3%v3).isZero())){
q = (u3/v3);
t1 = u1 - (q*v1);
t2 = u2 - (q*v2);
t3 = u3 - (q*v3);
u1 = v1;
u2 = v2;
u3 = v3;
v1 = t1;
v2 = t2;
v3 = t3;
}
//v2 is neg
if (v2[BIGINT_SIZE-1]==1){
//take 2's comp
v2.flip();
v2 = v2+1;
neg = true;
}
b = v2%m;
if(neg){
b = m-b;
}
return b;
}
}