forked from zhaozg/lua-openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlbn.c
More file actions
804 lines (756 loc) · 15.5 KB
/
lbn.c
File metadata and controls
804 lines (756 loc) · 15.5 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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/***
big-number library for Lua 5.x based on OpenSSL BIGNUM
This module provides a complete wrapper for OpenSSL's BIGNUM operations,
enabling arbitrary precision arithmetic similar to BIGNUM.
@author Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
@license This code is hereby placed in the public domain.
@warning verson 11 Nov 2010 22:56:45
@module bn
@usage
bn = require('openssl').bn
*/
#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <stdlib.h>
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#define BN_is_negative(a) ((a)->neg != 0)
#endif
#include "lauxlib.h"
#include "lua.h"
#include "lualib.h"
#include "private.h"
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
#define SHLIB_VERSION_NUMBER OPENSSL_VERSION_STR
#endif
#define MYNAME "bn"
#define MYVERSION \
MYNAME " library for " LUA_VERSION " / Nov 2010 / " \
"based on OpenSSL " SHLIB_VERSION_NUMBER
#define MYTYPE "openssl.bn"
static void
error(lua_State *L, const char *message)
{
luaL_error(L, "(bn) %s %s", message, ERR_reason_error_string(ERR_get_error()));
}
static BIGNUM *
Bnew(lua_State *L)
{
BIGNUM *x = BN_new();
if (x == NULL) error(L, "BN_new failed");
PUSH_BN(x);
return x;
}
static BIGNUM *
Bget(lua_State *L, int i)
{
switch (lua_type(L, i)) {
case LUA_TNUMBER:
case LUA_TSTRING: {
BIGNUM *x = Bnew(L);
const char *s = lua_tostring(L, i);
if (s[0] == 'X' || s[0] == 'x')
BN_hex2bn(&x, s + 1);
else
BN_dec2bn(&x, s);
lua_replace(L, i);
return x;
}
default:
return *((void **)luaL_checkudata(L, i, MYTYPE));
}
}
/**
* bits(x)
* @function bits
* @param x big number
* @return integer number of bits
* @usage bits = bn.bits(x)
*/
static int
Bbits(lua_State *L) /** bits(x) */
{
BIGNUM *a = Bget(L, 1);
lua_pushinteger(L, BN_num_bits(a));
return 1;
}
/**
* tostring(x)
* @function tostring
* @param x big number
* @return string decimal representation
* @usage s = bn.tostring(x)
*/
static int
Btostring(lua_State *L) /** tostring(x) */
{
BIGNUM *a = Bget(L, 1);
char *s = BN_bn2dec(a);
lua_pushstring(L, s);
OPENSSL_free(s);
return 1;
}
/**
* tohex(x)
* @function tohex
* @param x big number
* @return string hexadecimal representation
* @usage s = bn.tohex(x)
*/
static int
Btohex(lua_State *L) /** tohex(x) */
{
BIGNUM *a = Bget(L, 1);
char *s = BN_bn2hex(a);
lua_pushstring(L, s);
OPENSSL_free(s);
return 1;
}
/**
* totext(x[, n])
* @function totext
* @param x big number
* @param[opt] n number of bytes
* @return string binary representation
* @usage t = bn.totext(x)
*/
static int
Btotext(lua_State *L) /** totext(x) */
{
BIGNUM *a = Bget(L, 1);
int n = luaL_optinteger(L, 2, BN_num_bytes(a));
void *s = malloc(n);
if (s == NULL) return 0;
n = BN_bn2binpad(a, s, n);
if (n > 0)
lua_pushlstring(L, s, n);
else
lua_pushnil(L);
free(s);
return 1;
}
/**
* tonumber(x)
* @function tonumber
* @param x big number
* @return number
* @usage n = bn.tonumber(x)
*/
static int
Btonumber(lua_State *L) /** tonumber(x) */
{
Btostring(L);
lua_pushnumber(L, lua_tonumber(L, -1));
return 1;
}
/**
* iszero(x)
* @function iszero
* @param x big number
* @return boolean true if x == 0
* @usage ok = bn.iszero(x)
*/
static int
Biszero(lua_State *L) /** iszero(x) */
{
BIGNUM *a = Bget(L, 1);
lua_pushboolean(L, BN_is_zero(a));
return 1;
}
/**
* isone(x)
* @function isone
* @param x big number
* @return boolean true if x == 1
* @usage ok = bn.isone(x)
*/
static int
Bisone(lua_State *L) /** isone(x) */
{
BIGNUM *a = Bget(L, 1);
lua_pushboolean(L, BN_is_one(a));
return 1;
}
/**
* isodd(x)
* @function isodd
* @param x big number
* @return boolean true if x is odd
* @usage ok = bn.isodd(x)
*/
static int
Bisodd(lua_State *L) /** isodd(x) */
{
BIGNUM *a = Bget(L, 1);
lua_pushboolean(L, BN_is_odd(a));
return 1;
}
/**
* isneg(x)
* @function isneg
* @param x big number
* @return boolean true if x < 0
* @usage ok = bn.isneg(x)
*/
static int
Bisneg(lua_State *L) /** isneg(x) */
{
BIGNUM *a = Bget(L, 1);
lua_pushboolean(L, BN_is_negative(a));
return 1;
}
/**
* number(x)
* @function number
* @param x number or string
* @return big number
* @usage x = bn.number(123)
*/
static int
Bnumber(lua_State *L) /** number(x) */
{
Bget(L, 1);
lua_settop(L, 1);
return 1;
}
/**
* text(t)
* @function text
* @param t string binary representation
* @return big number
* @usage x = bn.text(t)
*/
static int
Btext(lua_State *L) /** text(t) */
{
size_t l;
const char *s = luaL_checklstring(L, 1, &l);
BIGNUM *x = Bnew(L);
BN_bin2bn((const unsigned char *)s, l, x);
return 1;
}
/**
* compare(x, y)
* @function compare
* @param x big number
* @param y big number
* @return integer (-1, 0, 1)
* @usage cmp = bn.compare(x, y)
*/
static int
Bcompare(lua_State *L) /** compare(x,y) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *b = Bget(L, 2);
lua_pushinteger(L, BN_cmp(a, b));
return 1;
}
static int
Beq(lua_State *L)
{
BIGNUM *a = Bget(L, 1);
BIGNUM *b = Bget(L, 2);
lua_pushboolean(L, BN_cmp(a, b) == 0);
return 1;
}
static int
Blt(lua_State *L)
{
BIGNUM *a = Bget(L, 1);
BIGNUM *b = Bget(L, 2);
lua_pushboolean(L, BN_cmp(a, b) < 0);
return 1;
}
/**
* sqr(x)
* @function sqr
* @param x big number
* @return big number x^2
* @usage y = bn.sqr(x)
*/
static int
Bsqr(lua_State *L) /** sqr(x) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *c = Bnew(L);
BN_CTX *ctx = BN_CTX_new();
BN_sqr(c, a, ctx);
BN_CTX_free(ctx);
return 1;
}
/**
* neg(x)
* @function neg
* @param x big number
* @return big number -x
* @usage y = bn.neg(x)
*/
static int
Bneg(lua_State *L) /** neg(x) */
{
BIGNUM *a = BN_new();
BIGNUM *b = Bget(L, 1);
BIGNUM *c = Bnew(L);
BN_set_word(a, 0);
BN_sub(c, a, b);
BN_free(a);
return 1;
}
/**
* abs(x)
* @function abs
* @param x big number
* @return big number |x|
* @usage y = bn.abs(x)
*/
static int
Babs(lua_State *L) /** abs(x) */
{
BIGNUM *b = Bget(L, 1);
if (BN_is_negative(b)) {
BIGNUM *a = BN_new();
BIGNUM *c = Bnew(L);
BN_set_word(a, 0);
BN_sub(c, a, b);
BN_free(a);
} else
lua_settop(L, 1);
return 1;
}
/**
* add(x, y)
* @function add
* @param x big number
* @param y big number
* @return big number x + y
* @usage z = bn.add(x, y)
*/
static int
Badd(lua_State *L) /** add(x,y) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *b = Bget(L, 2);
BIGNUM *c = Bnew(L);
BN_add(c, a, b);
return 1;
}
/**
* sub(x, y)
* @function sub
* @param x big number
* @param y big number
* @return big number x - y
* @usage z = bn.sub(x, y)
*/
static int
Bsub(lua_State *L) /** sub(x,y) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *b = Bget(L, 2);
BIGNUM *c = Bnew(L);
BN_sub(c, a, b);
return 1;
}
/**
* mul(x, y)
* @function mul
* @param x big number
* @param y big number
* @return big number x * y
* @usage z = bn.mul(x, y)
*/
static int
Bmul(lua_State *L) /** mul(x,y) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *b = Bget(L, 2);
BIGNUM *c = Bnew(L);
BN_CTX *ctx = BN_CTX_new();
BN_mul(c, a, b, ctx);
BN_CTX_free(ctx);
return 1;
}
/**
* div(x, y)
* @function div
* @param x big number
* @param y big number
* @return big number x // y
* @usage q = bn.div(x, y)
*/
static int
Bdiv(lua_State *L) /** div(x,y) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *b = Bget(L, 2);
BIGNUM *q = Bnew(L);
BIGNUM *r = NULL;
BN_CTX *ctx = BN_CTX_new();
BN_div(q, r, a, b, ctx);
BN_CTX_free(ctx);
return 1;
}
/**
* mod(x, y)
* @function mod
* @param x big number
* @param y big number
* @return big number x % y
* @usage r = bn.mod(x, y)
*/
static int
Bmod(lua_State *L) /** mod(x,y) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *b = Bget(L, 2);
BIGNUM *q = NULL;
BIGNUM *r = Bnew(L);
BN_CTX *ctx = BN_CTX_new();
BN_div(q, r, a, b, ctx);
BN_CTX_free(ctx);
return 1;
}
/**
* rmod(x, y)
* @function rmod
* @param x big number
* @param y big number
* @return big number x mod y (always positive)
* @usage r = bn.rmod(x, y)
*/
static int
Brmod(lua_State *L) /** rmod(x,y) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *b = Bget(L, 2);
BIGNUM *r = Bnew(L);
BN_CTX *ctx = BN_CTX_new();
BN_nnmod(r, a, b, ctx);
BN_CTX_free(ctx);
return 1;
}
/**
* divmod(x, y)
* @function divmod
* @param x big number
* @param y big number
* @return q, r quotient and remainder
* @usage q, r = bn.divmod(x, y)
*/
static int
Bdivmod(lua_State *L) /** divmod(x,y) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *b = Bget(L, 2);
BIGNUM *q = Bnew(L);
BIGNUM *r = Bnew(L);
BN_CTX *ctx = BN_CTX_new();
BN_div(q, r, a, b, ctx);
BN_CTX_free(ctx);
return 2;
}
/**
* gcd(x, y)
* @function gcd
* @param x big number
* @param y big number
* @return big number gcd(x, y)
* @usage g = bn.gcd(x, y)
*/
static int
Bgcd(lua_State *L) /** gcd(x,y) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *b = Bget(L, 2);
BIGNUM *c = Bnew(L);
BN_CTX *ctx = BN_CTX_new();
BN_gcd(c, a, b, ctx);
BN_CTX_free(ctx);
return 1;
}
/**
* pow(x, y)
* @function pow
* @param x big number
* @param y big number
* @return big number x^y
* @usage z = bn.pow(x, y)
*/
static int
Bpow(lua_State *L) /** pow(x,y) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *b = Bget(L, 2);
BIGNUM *c = Bnew(L);
BN_CTX *ctx = BN_CTX_new();
BN_exp(c, a, b, ctx);
BN_CTX_free(ctx);
return 1;
}
/**
* addmod(x, y, m)
* @function addmod
* @param x big number
* @param y big number
* @param m modulus
* @return big number (x + y) % m
* @usage z = bn.addmod(x, y, m)
*/
static int
Baddmod(lua_State *L) /** addmod(x,y,m) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *b = Bget(L, 2);
BIGNUM *m = Bget(L, 3);
BIGNUM *c = Bnew(L);
BN_CTX *ctx = BN_CTX_new();
BN_mod_add(c, a, b, m, ctx);
BN_CTX_free(ctx);
return 1;
}
/**
* submod(x, y, m)
* @function submod
* @param x big number
* @param y big number
* @param m modulus
* @return big number (x - y) % m
* @usage z = bn.submod(x, y, m)
*/
static int
Bsubmod(lua_State *L) /** submod(x,y,m) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *b = Bget(L, 2);
BIGNUM *m = Bget(L, 3);
BIGNUM *c = Bnew(L);
BN_CTX *ctx = BN_CTX_new();
BN_mod_sub(c, a, b, m, ctx);
BN_CTX_free(ctx);
return 1;
}
/**
* mulmod(x, y, m)
* @function mulmod
* @param x big number
* @param y big number
* @param m modulus
* @return big number (x * y) % m
* @usage z = bn.mulmod(x, y, m)
*/
static int
Bmulmod(lua_State *L) /** mulmod(x,y,m) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *b = Bget(L, 2);
BIGNUM *m = Bget(L, 3);
BIGNUM *c = Bnew(L);
BN_CTX *ctx = BN_CTX_new();
BN_mod_mul(c, a, b, m, ctx);
BN_CTX_free(ctx);
return 1;
}
/**
* powmod(x, y, m)
* @function powmod
* @param x big number
* @param y big number
* @param m modulus
* @return big number (x^y) % m
* @usage z = bn.powmod(x, y, m)
*/
static int
Bpowmod(lua_State *L) /** powmod(x,y,m) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *b = Bget(L, 2);
BIGNUM *m = Bget(L, 3);
BIGNUM *c = Bnew(L);
BN_CTX *ctx = BN_CTX_new();
BN_mod_exp(c, a, b, m, ctx);
BN_CTX_free(ctx);
return 1;
}
/**
* sqrmod(x, m)
* @function sqrmod
* @param x big number
* @param m modulus
* @return big number (x^2) % m
* @usage z = bn.sqrmod(x, m)
*/
static int
Bsqrmod(lua_State *L) /** sqrmod(x) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *m = Bget(L, 2);
BIGNUM *c = Bnew(L);
BN_CTX *ctx = BN_CTX_new();
BN_mod_sqr(c, a, m, ctx);
BN_CTX_free(ctx);
return 1;
}
/**
* invmod(x, m)
* @function invmod
* @param x big number
* @param m modulus
* @return big number modular inverse
* @usage y = bn.invmod(x, m)
*/
static int
Binvmod(lua_State *L) /** invmod(x) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *m = Bget(L, 2);
BIGNUM *c = Bnew(L);
BN_CTX *ctx = BN_CTX_new();
BN_mod_inverse(c, a, m, ctx);
BN_CTX_free(ctx);
return 1;
}
/**
* sqrtmod(x, m)
* @function sqrtmod
* @param x big number
* @param m modulus
* @return big number modular square root
* @usage y = bn.sqrtmod(x, m)
*/
static int
Bsqrtmod(lua_State *L) /** sqrtmod(x) */
{
BIGNUM *a = Bget(L, 1);
BIGNUM *m = Bget(L, 2);
BIGNUM *c = Bnew(L);
BN_CTX *ctx = BN_CTX_new();
BN_mod_sqrt(c, a, m, ctx);
BN_CTX_free(ctx);
return 1;
}
/**
* random([bits])
* @function random
* @param[opt=32] bits number of bits (default 32)
* @return big number random value
* @usage x = bn.random(128)
*/
static int
Brandom(lua_State *L) /** random(bits) */
{
int bits = luaL_optint(L, 1, 32);
BIGNUM *x = Bnew(L);
BN_rand(x, bits, -1, 0);
return 1;
}
/**
* aprime([bits])
* @function aprime
* @param[opt=32] bits number of bits (default 32)
* @return big number probable prime
* @usage p = bn.aprime(128)
*/
static int
Baprime(lua_State *L) /** aprime(bits) */
{
int bits = luaL_optint(L, 1, 32);
BIGNUM *x = Bnew(L);
if (BN_generate_prime_ex(x, bits, 0, NULL, NULL, NULL))
return 1;
else
lua_pop(L, 1);
return 0;
}
/**
* isprime(x[, checks])
* @function isprime
* @param x big number
* @param[opt] checks number of checks
* @return boolean true if x is prime
* @usage ok = bn.isprime(x)
*/
static int
Bisprime(lua_State *L) /** isprime(x,[checks]) */
{
BIGNUM *a = Bget(L, 1);
BN_CTX *ctx = BN_CTX_new();
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
BN_GENCB *cb = BN_GENCB_new();
lua_pushboolean(L, BN_check_prime(a, ctx, cb));
BN_GENCB_free(cb);
#else
int checks = luaL_optint(L, 2, BN_prime_checks);
lua_pushboolean(L, BN_is_prime_fasttest_ex(a, checks, ctx, 1, NULL));
#endif
BN_CTX_free(ctx);
return 1;
}
static int
Bgc(lua_State *L)
{
BIGNUM *a = Bget(L, 1);
BN_free(a);
return 0;
}
static const luaL_Reg R[] = {
{ "__add", Badd }, /** __add(x,y) */
{ "__div", Bdiv }, /** __div(x,y) */
{ "__eq", Beq }, /** __eq(x,y) */
{ "__gc", Bgc },
{ "__lt", Blt }, /** __lt(x,y) */
{ "__mod", Bmod }, /** __mod(x,y) */
{ "__mul", Bmul }, /** __mul(x,y) */
{ "__pow", Bpow }, /** __pow(x,y) */
{ "__sub", Bsub }, /** __sub(x,y) */
{ "__tostring", Btostring }, /** __tostring(x) */
{ "__unm", Bneg }, /** __unm(x) */
{ "abs", Babs },
{ "add", Badd },
{ "addmod", Baddmod },
{ "aprime", Baprime },
{ "bits", Bbits },
{ "compare", Bcompare },
{ "div", Bdiv },
{ "divmod", Bdivmod },
{ "gcd", Bgcd },
{ "invmod", Binvmod },
{ "isneg", Bisneg },
{ "isodd", Bisodd },
{ "isone", Bisone },
{ "isprime", Bisprime },
{ "iszero", Biszero },
{ "mod", Bmod },
{ "mul", Bmul },
{ "mulmod", Bmulmod },
{ "neg", Bneg },
{ "number", Bnumber },
{ "pow", Bpow },
{ "powmod", Bpowmod },
{ "random", Brandom },
{ "rmod", Brmod },
{ "sqr", Bsqr },
{ "sqrmod", Bsqrmod },
{ "sqrtmod", Bsqrtmod },
{ "sub", Bsub },
{ "submod", Bsubmod },
{ "text", Btext },
{ "tohex", Btohex },
{ "totext", Btotext },
{ "tonumber", Btonumber },
{ "tostring", Btostring },
{ NULL, NULL }
};
int
luaopen_bn(lua_State *L)
{
luaL_newmetatable(L, MYTYPE);
luaL_setfuncs(L, R, 0);
lua_pushliteral(L, "version"); /** version */
lua_pushliteral(L, MYVERSION);
lua_settable(L, -3);
lua_pushliteral(L, "__index");
lua_pushvalue(L, -2);
lua_settable(L, -3);
return 1;
}