File: Bignum.xs

package info (click to toggle)
libcrypt-openssl-bignum-perl 0.09-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 164 kB
  • sloc: perl: 131; makefile: 3
file content (440 lines) | stat: -rwxr-xr-x 9,954 bytes parent folder | download | duplicates (2)
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
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include <openssl/err.h>
#include <openssl/bn.h>

#define checkOpenSslCall( result ) if( ! ( result ) ) \
  croak( "OpenSSL error: %s", ERR_reason_error_string( ERR_get_error() ) );

typedef BIGNUM *Crypt__OpenSSL__Bignum;
typedef BN_CTX *Crypt__OpenSSL__Bignum__CTX;

SV* new_obj( void* obj )
{
    SV * tmp = sv_newmortal();
    sv_setref_pv(tmp, "Crypt::OpenSSL::Bignum", (void*)obj);
    return tmp;
}

BIGNUM* sv2bn( SV* sv )
{
    if (SvROK(sv) && sv_derived_from(sv, "Crypt::OpenSSL::Bignum")) {
        return INT2PTR(Crypt__OpenSSL__Bignum, SvIV((SV*)SvRV(sv)));
    }
    else Perl_croak(aTHX_ "argument is not a Crypt::OpenSSL::Bignum object");
}

MODULE = Crypt::OpenSSL::Bignum      PACKAGE = Crypt::OpenSSL::Bignum   PREFIX = BN_

BOOT:
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
    OPENSSL_init_crypto(0, NULL);
#else
    ERR_load_crypto_strings();
#endif

void
DESTROY(Crypt::OpenSSL::Bignum self)
    CODE:
        BN_clear_free( self );

Crypt::OpenSSL::Bignum
new_from_word(CLASS, p_word)
    unsigned long p_word;
  PREINIT:
    BIGNUM* bn;
  CODE:
    checkOpenSslCall( bn = BN_new() );
    checkOpenSslCall( BN_set_word( bn, p_word ) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

Crypt::OpenSSL::Bignum
new_from_decimal(CLASS, p_dec_string)
    char* p_dec_string;
  PREINIT:
    BIGNUM* bn;
  CODE:
    bn = NULL;
    checkOpenSslCall( BN_dec2bn( &bn, p_dec_string ) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

Crypt::OpenSSL::Bignum
new_from_hex(CLASS, p_hex_string)
    char* p_hex_string;
  PREINIT:
    BIGNUM* bn;
  CODE:
    bn = NULL;
    checkOpenSslCall( BN_hex2bn( &bn, p_hex_string ) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

Crypt::OpenSSL::Bignum
new_from_bin(CLASS, p_bin_string_SV)
    SV* p_bin_string_SV;
  PREINIT:
    BIGNUM* bn;
    unsigned char* bin;
    STRLEN bin_length;
  CODE:
    bin = (unsigned char*) SvPV( p_bin_string_SV, bin_length );
    checkOpenSslCall( bn = BN_bin2bn( bin, bin_length, NULL ) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

Crypt::OpenSSL::Bignum
BN_new(CLASS)
  PREINIT:
    BIGNUM* bn;
  CODE:
    checkOpenSslCall( bn = BN_new() );
    checkOpenSslCall( BN_set_word( bn, 0 ) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

Crypt::OpenSSL::Bignum
BN_zero(CLASS)
  PREINIT:
    BIGNUM *bn;
  CODE:
    checkOpenSslCall( bn = BN_new() );
    checkOpenSslCall( BN_set_word( bn, 0 ) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

Crypt::OpenSSL::Bignum
BN_one(CLASS)
  PREINIT:
    BIGNUM *bn;
  CODE:
    checkOpenSslCall( bn = BN_new() );
    checkOpenSslCall( BN_one( bn ) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

Crypt::OpenSSL::Bignum
BN_rand(CLASS, int bits, int top, int bottom)
  PREINIT:
    BIGNUM* bn;
  CODE:
    checkOpenSslCall( bn = BN_new() );
    checkOpenSslCall( BN_rand( bn, bits, top, bottom) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

Crypt::OpenSSL::Bignum
BN_pseudo_rand(CLASS, int bits, int top, int bottom)
  PREINIT:
    BIGNUM* bn;
  CODE:
    checkOpenSslCall( bn = BN_new() );
    checkOpenSslCall( BN_pseudo_rand( bn, bits, top, bottom) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

Crypt::OpenSSL::Bignum
BN_rand_range(CLASS, Crypt::OpenSSL::Bignum r)
  PREINIT:
    BIGNUM* bn;
  CODE:
    checkOpenSslCall( bn = BN_new() );
    checkOpenSslCall( BN_rand_range( bn, r) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

void
BN_bless_pointer(CLASS, void *pointer)
  PPCODE:
    ST(0) = new_obj(pointer);
    XSRETURN(1);

char*
BN_to_decimal(Crypt::OpenSSL::Bignum self)
  CODE:
    checkOpenSslCall( RETVAL = BN_bn2dec( self ) );
  OUTPUT:
    RETVAL
  CLEANUP:
    OPENSSL_free( RETVAL );

char*
BN_to_hex(Crypt::OpenSSL::Bignum self)
  CODE:
    checkOpenSslCall( RETVAL = BN_bn2hex( self ) );
  OUTPUT:
    RETVAL
  CLEANUP:
    OPENSSL_free( RETVAL );

SV*
BN_to_bin(Crypt::OpenSSL::Bignum self)
  PREINIT:
    unsigned char* bin;
    int length;
  CODE:
    length = BN_num_bytes( self );
    if (length>0) {
      RETVAL = NEWSV(0, length);
      SvPOK_only(RETVAL);
      SvCUR_set(RETVAL, length);
      bin = (unsigned char *)SvPV_nolen(RETVAL);
      BN_bn2bin( self, bin );
    }
    else {
      RETVAL = newSVpvn("", 0);
    }
  OUTPUT:
    RETVAL

unsigned long
BN_get_word(Crypt::OpenSSL::Bignum self)

int
BN_is_zero(Crypt::OpenSSL::Bignum self)

int
BN_is_one(Crypt::OpenSSL::Bignum self)

int
BN_is_odd(Crypt::OpenSSL::Bignum self)

void
BN_add(Crypt::OpenSSL::Bignum self, Crypt::OpenSSL::Bignum b, ...)
  PREINIT:
    BIGNUM *bn;
  PPCODE:
    if( items > 3 )
      croak( "usage: $bn->add( $bn2[, $target] )" );
    bn = ( items < 3 ) ? BN_new() : sv2bn( ST(2) );
    checkOpenSslCall( BN_add( bn, self, b ) );
    ST(0) = ( (items < 3 ) ? new_obj( bn ) : ST(2) );
    XSRETURN(1);

void
BN_sub(Crypt::OpenSSL::Bignum self, Crypt::OpenSSL::Bignum b, ...)
  PREINIT:
    BIGNUM *bn;
  PPCODE:
    if( items > 3 )
      croak( "usage: $bn->sub( $bn2[, $target] )" );
    bn = ( items < 3 ) ? BN_new() : sv2bn( ST(2) );
    checkOpenSslCall( BN_sub( bn, self, b ) );
    ST(0) = ( (items < 3 ) ? new_obj( bn ) : ST(2) );
    XSRETURN(1);

void
BN_mul(self, b, ctx, ...)
    Crypt::OpenSSL::Bignum self;
    Crypt::OpenSSL::Bignum b;
    Crypt::OpenSSL::Bignum::CTX ctx;
  PREINIT:
    BIGNUM* bn;
  PPCODE:
    if( items > 4 )
      croak( "usage: $bn->mul( $bn2, $ctx, [, $target] )" );
    bn = ( items < 4 ) ? BN_new() : sv2bn( ST(3) );
    checkOpenSslCall( BN_mul( bn, self, b, ctx ) );
    ST(0) = ( (items < 4 ) ? new_obj( bn ) : ST(3) );
    XSRETURN(1);

void
BN_div(self, b, ctx, ...)
    Crypt::OpenSSL::Bignum self;
    Crypt::OpenSSL::Bignum b;
    Crypt::OpenSSL::Bignum::CTX ctx;
  PREINIT:
    BIGNUM* quotient;
    BIGNUM* remainder;
  PPCODE:
    if( items > 5 )
      croak( "usage: $bn->div( $bn2, $ctx, [, $quotient [, $remainder ] ] )" );
    quotient = ( items < 4 ) ? BN_new() : sv2bn( ST(3) );
    remainder = ( items < 5 ) ? BN_new() : sv2bn( ST(4) );
    checkOpenSslCall( BN_div( quotient, remainder, self, b, ctx ) );
    ST(0) = ( (items < 4 ) ? new_obj( quotient ) : ST(3) );
    ST(1) = ( (items < 5 ) ? new_obj( remainder ) : ST(4) );
    XSRETURN(2);

Crypt::OpenSSL::Bignum
BN_sqr(Crypt::OpenSSL::Bignum self, Crypt::OpenSSL::Bignum::CTX ctx)
  PREINIT:
    BIGNUM* bn;
  CODE:
    checkOpenSslCall( bn = BN_new() );
    checkOpenSslCall( BN_sqr( bn, self, ctx ) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

void
BN_mod(self, b, ctx, ...)
    Crypt::OpenSSL::Bignum self;
    Crypt::OpenSSL::Bignum b;
    Crypt::OpenSSL::Bignum::CTX ctx;
  PREINIT:
    BIGNUM* bn;
  PPCODE:
    if( items > 4 )
      croak( "usage: $bn->mod( $bn2, $ctx, [, $target] )" );
    bn = ( items < 4 ) ? BN_new() : sv2bn( ST(3) );
    checkOpenSslCall( BN_mod( bn, self, b, ctx ) );
    ST(0) = ( (items < 4 ) ? new_obj( bn ) : ST(3) );
    XSRETURN(1);

Crypt::OpenSSL::Bignum
BN_mod_mul(self, b, m, ctx)
    Crypt::OpenSSL::Bignum self;
    Crypt::OpenSSL::Bignum b;
    Crypt::OpenSSL::Bignum m;
    Crypt::OpenSSL::Bignum::CTX ctx;
  PREINIT:
    BIGNUM* bn;
  CODE:
    checkOpenSslCall( bn = BN_new() );
    checkOpenSslCall( BN_mod_mul( bn, self, b, m, ctx ) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

Crypt::OpenSSL::Bignum
BN_exp(self, exp, ctx)
    Crypt::OpenSSL::Bignum self;
    Crypt::OpenSSL::Bignum exp;
    Crypt::OpenSSL::Bignum::CTX ctx;
  PREINIT:
    BIGNUM* bn;
  CODE:
    checkOpenSslCall( bn = BN_new() );
    checkOpenSslCall( BN_exp( bn, self, exp, ctx ) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

Crypt::OpenSSL::Bignum
BN_mod_exp(self, exp, mod, ctx)
    Crypt::OpenSSL::Bignum self;
    Crypt::OpenSSL::Bignum exp;
    Crypt::OpenSSL::Bignum mod;
    Crypt::OpenSSL::Bignum::CTX ctx;
  PREINIT:
    BIGNUM* bn;
  CODE:
    checkOpenSslCall( bn = BN_new() );
    checkOpenSslCall( BN_mod_exp( bn, self, exp, mod, ctx ) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

Crypt::OpenSSL::Bignum
BN_mod_inverse(self, n, ctx)
    Crypt::OpenSSL::Bignum self;
    Crypt::OpenSSL::Bignum n;
    Crypt::OpenSSL::Bignum::CTX ctx;
  PREINIT:
    BIGNUM* bn;
  CODE:
    checkOpenSslCall( bn = BN_new() );
    checkOpenSslCall( BN_mod_inverse( bn, self, n, ctx ) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

Crypt::OpenSSL::Bignum
BN_gcd(self, b, ctx)
    Crypt::OpenSSL::Bignum self;
    Crypt::OpenSSL::Bignum b;
    Crypt::OpenSSL::Bignum::CTX ctx;
  PREINIT:
    BIGNUM* bn;
  CODE:
    checkOpenSslCall( bn = BN_new() );
    checkOpenSslCall( BN_gcd( bn, self, b, ctx ) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

int
BN_equals(Crypt::OpenSSL::Bignum self, Crypt::OpenSSL::Bignum b)
  CODE:
    RETVAL = BN_cmp(self, b) == 0 ? 1 : 0;
  OUTPUT:
    RETVAL

int
BN_cmp(Crypt::OpenSSL::Bignum self, Crypt::OpenSSL::Bignum b)

int
BN_num_bits(Crypt::OpenSSL::Bignum self)

int
BN_num_bytes(Crypt::OpenSSL::Bignum self)

Crypt::OpenSSL::Bignum
BN_rshift(Crypt::OpenSSL::Bignum self, int n)
  PREINIT:
    BIGNUM* bn;
  CODE:
    checkOpenSslCall( bn = BN_new() );
    checkOpenSslCall( BN_rshift( bn, self, n ) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

Crypt::OpenSSL::Bignum
BN_lshift(Crypt::OpenSSL::Bignum self, int n)
  PREINIT:
    BIGNUM* bn;
  CODE:
    checkOpenSslCall( bn = BN_new() );
    checkOpenSslCall( BN_lshift( bn, self, n ) );
    RETVAL = bn;
  OUTPUT:
    RETVAL

int
BN_ucmp(Crypt::OpenSSL::Bignum self, Crypt::OpenSSL::Bignum b)

void
BN_swap(Crypt::OpenSSL::Bignum self, Crypt::OpenSSL::Bignum b)

Crypt::OpenSSL::Bignum
BN_copy(Crypt::OpenSSL::Bignum self)
  CODE:
    checkOpenSslCall( RETVAL = BN_dup(self) );
  OUTPUT:
    RETVAL

IV
BN_pointer_copy(Crypt::OpenSSL::Bignum self)
  CODE:
    checkOpenSslCall( RETVAL = PTR2IV(BN_dup(self)) );
  OUTPUT:
    RETVAL

MODULE = Crypt::OpenSSL::Bignum  PACKAGE = Crypt::OpenSSL::Bignum::CTX

Crypt::OpenSSL::Bignum::CTX
new(CLASS)
    CODE:
        RETVAL = BN_CTX_new();
    OUTPUT:
        RETVAL

void
DESTROY(Crypt::OpenSSL::Bignum::CTX self)
    CODE:
        BN_CTX_free(self);