-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
603 lines (500 loc) · 13.1 KB
/
Copy pathmain.cpp
File metadata and controls
603 lines (500 loc) · 13.1 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#include <stdio.h>
#include <stdint.h>
#include <time.h>
namespace RsaImport
{
typedef int8_t INT8;
typedef uint8_t UINT8;
typedef int16_t INT16;
typedef uint16_t UINT16;
typedef int32_t INT32;
typedef uint32_t UINT32;
typedef int64_t INT64;
typedef uint64_t UINT64;
struct UINT128
{
UINT64 hi;
UINT64 lo;
};
struct RSAI_ENCRYPT_DATA
{
UINT64 hi;
UINT64 lo;
};
struct RSAI_PUBLIC
{
UINT128 n;
UINT128 e;
};
struct RSAI_PRIVATE
{
UINT128 n;
UINT128 d;
UINT64 p;
UINT64 q;
UINT64 dp;
UINT64 dq;
UINT64 qinv;
};
static UINT128 MakeU128(UINT64 hi, UINT64 lo)
{
UINT128 x;
x.hi = hi;
x.lo = lo;
return x;
}
static UINT128 MakeU128U64(UINT64 x)
{
UINT128 r;
r.hi = 0;
r.lo = x;
return r;
}
static int U128IsZero(UINT128 a)
{
return (a.hi == 0 && a.lo == 0);
}
static int U128Eq(UINT128 a, UINT128 b)
{
return a.hi == b.hi && a.lo == b.lo;
}
static int U128Lt(UINT128 a, UINT128 b)
{
if (a.hi != b.hi) return a.hi < b.hi;
return a.lo < b.lo;
}
static int U128Ge(UINT128 a, UINT128 b)
{
return !U128Lt(a, b);
}
static UINT128 U128Add(UINT128 a, UINT128 b)
{
UINT128 r;
r.lo = a.lo + b.lo;
r.hi = a.hi + b.hi + (r.lo < a.lo ? 1ULL : 0ULL);
return r;
}
static UINT128 U128Sub(UINT128 a, UINT128 b)
{
UINT128 r;
r.lo = a.lo - b.lo;
r.hi = a.hi - b.hi - (a.lo < b.lo ? 1ULL : 0ULL);
return r;
}
static UINT128 U128Shl1(UINT128 a)
{
UINT128 r;
r.hi = (a.hi << 1) | (a.lo >> 63);
r.lo = (a.lo << 1);
return r;
}
static int U128GetBit(UINT128 a, int bit)
{
if (bit < 64) return (int)((a.lo >> bit) & 1ULL);
return (int)((a.hi >> (bit - 64)) & 1ULL);
}
static void PrintU128Hex(UINT128 x)
{
printf("0x%016llx%016llx",
(unsigned long long)x.hi,
(unsigned long long)x.lo);
}
static UINT128 Mul64Wide(UINT64 a, UINT64 b)
{
UINT64 a0 = (UINT32)a;
UINT64 a1 = a >> 32;
UINT64 b0 = (UINT32)b;
UINT64 b1 = b >> 32;
UINT64 p00 = a0 * b0;
UINT64 p01 = a0 * b1;
UINT64 p10 = a1 * b0;
UINT64 p11 = a1 * b1;
UINT64 mid = (p00 >> 32) + (UINT32)p01 + (UINT32)p10;
UINT128 r;
r.hi = p11 + (p01 >> 32) + (p10 >> 32) + (mid >> 32);
r.lo = (mid << 32) | (UINT32)p00;
return r;
}
static UINT64 GcdU64(UINT64 a, UINT64 b)
{
while (b)
{
UINT64 t = a % b;
a = b;
b = t;
}
return a;
}
static UINT64 AddModU64(UINT64 a, UINT64 b, UINT64 mod)
{
if (a >= mod) a %= mod;
if (b >= mod) b %= mod;
if (a >= mod - b) return a - (mod - b);
return a + b;
}
static UINT64 MulModU64(UINT64 a, UINT64 b, UINT64 mod)
{
UINT64 r = 0;
while (b)
{
if (b & 1ULL) r = AddModU64(r, a, mod);
b >>= 1;
if (b) a = AddModU64(a, a, mod);
}
return r;
}
static UINT64 PowModU64(UINT64 a, UINT64 e, UINT64 mod)
{
UINT64 r = 1 % mod;
while (e)
{
if (e & 1ULL) r = MulModU64(r, a, mod);
e >>= 1;
if (e) a = MulModU64(a, a, mod);
}
return r;
}
static int IsPrimeU64(UINT64 n)
{
static const UINT64 testPrimes[] = {
2ULL, 3ULL, 5ULL, 7ULL, 11ULL, 13ULL, 17ULL, 19ULL, 23ULL, 29ULL, 31ULL, 37ULL
};
static const UINT64 bases[] = {
2ULL, 325ULL, 9375ULL, 28178ULL, 450775ULL, 9780504ULL, 1795265022ULL
};
UINT64 d;
int s, i, j;
if (n < 2) return 0;
for (i = 0; i < (int)(sizeof(testPrimes) / sizeof(testPrimes[0])); ++i)
{
UINT64 p = testPrimes[i];
if (n == p) return 1;
if ((n % p) == 0) return 0;
}
d = n - 1;
s = 0;
while ((d & 1ULL) == 0)
{
d >>= 1;
++s;
}
for (i = 0; i < (int)(sizeof(bases) / sizeof(bases[0])); ++i)
{
UINT64 a = bases[i] % n;
UINT64 x;
if (a == 0) continue;
x = PowModU64(a, d, n);
if (x == 1 || x == n - 1) continue;
for (j = 1; j < s; ++j)
{
x = MulModU64(x, x, n);
if (x == n - 1) break;
}
if (j == s) return 0;
}
return 1;
}
static UINT64 g_rng_state = 0x9e3779b97f4a7c15ULL;
static UINT64 RandomU64(void)
{
UINT64 z = (g_rng_state += 0x9e3779b97f4a7c15ULL);
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
z = z ^ (z >> 31);
return z;
}
static void SeedU64(UINT64 seed)
{
if (seed == 0)
seed = (UINT64)time(NULL) ^ 0x123456789abcdef1ULL;
g_rng_state = seed;
RandomU64();
RandomU64();
RandomU64();
}
static UINT64 GeneratePrime63Bit(void)
{
for (;;)
{
UINT64 x = RandomU64();
x &= 0x7fffffffffffffffULL;
x |= 0x4000000000000000ULL;
x |= 1ULL;
if (!IsPrimeU64(x)) continue;
if (GcdU64(65537ULL, x - 1) != 1ULL) continue;
return x;
}
}
static UINT128 U128DivMod(UINT128 a, UINT128 b, UINT128* rem_out)
{
UINT128 q = MakeU128U64(0);
UINT128 r = MakeU128U64(0);
int i;
for (i = 127; i >= 0; --i)
{
r = U128Shl1(r);
if (U128GetBit(a, i))
{
r.lo |= 1ULL;
}
if (U128Ge(r, b))
{
r = U128Sub(r, b);
if (i >= 64) q.hi |= (1ULL << (i - 64));
else q.lo |= (1ULL << i);
}
}
if (rem_out) *rem_out = r;
return q;
}
static UINT128 U128Mod(UINT128 a, UINT128 mod)
{
UINT128 r;
U128DivMod(a, mod, &r);
return r;
}
static UINT128 U128Gcd(UINT128 a, UINT128 b)
{
while (!U128IsZero(b))
{
UINT128 r = U128Mod(a, b);
a = b;
b = r;
}
return a;
}
struct SIGNED128
{
int neg;
UINT128 mag;
};
static SIGNED128 MakeS128(int neg, UINT128 mag)
{
SIGNED128 r;
r.neg = (neg && !U128IsZero(mag)) ? 1 : 0;
r.mag = mag;
return r;
}
static SIGNED128 S128Neg(SIGNED128 a)
{
if (!U128IsZero(a.mag)) a.neg = !a.neg;
return a;
}
static SIGNED128 S128Add(SIGNED128 a, SIGNED128 b)
{
SIGNED128 r;
if (a.neg == b.neg)
{
r.neg = a.neg;
r.mag = U128Add(a.mag, b.mag);
if (U128IsZero(r.mag)) r.neg = 0;
return r;
}
if (U128Ge(a.mag, b.mag))
{
r.neg = a.neg;
r.mag = U128Sub(a.mag, b.mag);
}
else
{
r.neg = b.neg;
r.mag = U128Sub(b.mag, a.mag);
}
if (U128IsZero(r.mag)) r.neg = 0;
return r;
}
static SIGNED128 S128Sub(SIGNED128 a, SIGNED128 b)
{
return S128Add(a, S128Neg(b));
}
static SIGNED128 S128MulU128Small(SIGNED128 a, UINT128 b)
{
SIGNED128 r;
UINT128 acc = MakeU128U64(0);
UINT128 cur = a.mag;
int i;
for (i = 0; i < 128; ++i)
{
if (U128GetBit(b, i))
{
acc = U128Add(acc, cur);
}
if (i != 127) cur = U128Shl1(cur);
}
r.neg = a.neg;
r.mag = acc;
if (U128IsZero(r.mag)) r.neg = 0;
return r;
}
static UINT128 U128ModInv(UINT128 a, UINT128 mod)
{
UINT128 old_r = mod;
UINT128 r = a;
SIGNED128 old_t = MakeS128(0, MakeU128U64(0));
SIGNED128 t = MakeS128(0, MakeU128U64(1));
while (!U128IsZero(r))
{
UINT128 rem;
UINT128 q = U128DivMod(old_r, r, &rem);
UINT128 tmp_r = r;
SIGNED128 tmp_t = t;
r = rem;
old_r = tmp_r;
t = S128Sub(old_t, S128MulU128Small(t, q));
old_t = tmp_t;
}
if (!(old_r.hi == 0 && old_r.lo == 1))
{
return MakeU128U64(0);
}
if (old_t.neg)
{
UINT128 m = U128Mod(old_t.mag, mod);
if (U128IsZero(m)) return MakeU128U64(0);
return U128Sub(mod, m);
}
return U128Mod(old_t.mag, mod);
}
static UINT128 U128ModAdd(UINT128 a, UINT128 b, UINT128 mod)
{
UINT128 s = U128Add(a, b);
if (U128Lt(s, a) || U128Ge(s, mod))
{
s = U128Sub(s, mod);
}
return s;
}
static UINT128 U128ModMul(UINT128 a, UINT128 b, UINT128 mod)
{
UINT128 r = MakeU128U64(0);
int i;
a = U128Mod(a, mod);
for (i = 0; i < 128; ++i)
{
if (U128GetBit(b, i))
{
r = U128ModAdd(r, a, mod);
}
if (i != 127)
{
a = U128ModAdd(a, a, mod);
}
}
return r;
}
static UINT128 PowModU128(UINT128 base, UINT128 exp, UINT128 mod)
{
UINT128 r = MakeU128U64(1);
int i;
base = U128Mod(base, mod);
for (i = 0; i < 128; ++i)
{
if (U128GetBit(exp, i))
{
r = U128ModMul(r, base, mod);
}
if (i != 127)
{
base = U128ModMul(base, base, mod);
}
}
return r;
}
static UINT64 ModInvPrimeU64(UINT64 a, UINT64 p)
{
return PowModU64(a, p - 2, p);
}
static int GenerateKeyPairInternal(RSAI_PRIVATE* pri, RSAI_PUBLIC* pub, UINT64 seed)
{
UINT64 p, q;
UINT128 n, lambda, e, d;
UINT128 g;
UINT64 gcd_pq1;
UINT128 full_u64_max = MakeU128U64(0xffffffffffffffffULL);
if (!pri || !pub) return 0;
SeedU64(seed);
e = MakeU128U64(65537ULL);
for (;;)
{
p = GeneratePrime63Bit();
q = GeneratePrime63Bit();
if (p == q) continue;
n = Mul64Wide(p, q);
if (U128Lt(n, full_u64_max) || U128Eq(n, full_u64_max))
{
continue;
}
gcd_pq1 = GcdU64(p - 1, q - 1);
lambda = Mul64Wide((p - 1) / gcd_pq1, (q - 1));
g = U128Gcd(e, lambda);
if (!(g.hi == 0 && g.lo == 1)) continue;
d = U128ModInv(e, lambda);
if (U128IsZero(d)) continue;
pub->n = n;
pub->e = e;
pri->n = n;
pri->d = d;
pri->p = p;
pri->q = q;
pri->dp = (UINT64)(d.lo % (p - 1));
pri->dq = (UINT64)(d.lo % (q - 1));
pri->qinv = ModInvPrimeU64(q % p, p);
return 1;
}
}
bool GenerateKeyPair(RSAI_PRIVATE* PrivateKeyOut, RSAI_PUBLIC* PublicKeyOut)
{
UINT64 seed = ((UINT64)time(NULL) << 32) ^ 0xA5A5A5A55A5A5A5AULL ^ (UINT64)(uintptr_t)PrivateKeyOut;
return GenerateKeyPairInternal(PrivateKeyOut, PublicKeyOut, seed) ? true : false;
}
RSAI_ENCRYPT_DATA Encrypt64(UINT64 Data, const RSAI_PRIVATE& PrivateKey)
{
UINT128 m = MakeU128U64(Data);
UINT128 c = PowModU128(m, PrivateKey.d, PrivateKey.n);
RSAI_ENCRYPT_DATA out;
out.hi = c.hi;
out.lo = c.lo;
return out;
}
UINT64 Decrypt64(RSAI_ENCRYPT_DATA Data, const RSAI_PUBLIC& PublicKey)
{
UINT128 c;
UINT128 m;
c.hi = Data.hi;
c.lo = Data.lo;
m = PowModU128(c, PublicKey.e, PublicKey.n);
if (m.hi != 0)
return 0;
return m.lo;
}
}
int main()
{
RsaImport::RSAI_PRIVATE pri;
RsaImport::RSAI_PUBLIC pub;
RsaImport::RSAI_ENCRYPT_DATA enc;
RsaImport::UINT64 plain = 1234567890123456789ULL;
RsaImport::UINT64 dec;
if (!RsaImport::GenerateKeyPair(&pri, &pub))
{
printf("GenerateKeyPair failed\n");
return 1;
}
printf("public n = ");
RsaImport::PrintU128Hex(pub.n);
printf("\n");
printf("public e = ");
RsaImport::PrintU128Hex(pub.e);
printf("\n");
printf("private d = ");
RsaImport::PrintU128Hex(pri.d);
printf("\n");
enc = RsaImport::Encrypt64(plain, pri);
printf("plain = %llu\n", (unsigned long long)plain);
printf("cipher = 0x%016llx%016llx\n",
(unsigned long long)enc.hi,
(unsigned long long)enc.lo);
dec = RsaImport::Decrypt64(enc, pub);
printf("decrypt = %llu\n", (unsigned long long)dec);
return 0;
}