Subject: | Patch to add more functions |
Attached is a patch file for Crypt-OpenSSL-Bignum 0.04 to add support
and documentation for some more Bignum functions.
new(), rand(), pseudo_rand(), rand_range(),
num_bits(), num_bytes(), rshift(), lshift(), ucmp(), swap().
I hope Ian can roll this patch in to the next release.
Cheers.
Subject: | Crypt-OpenSSL-Bignum-0.04-mikem.patch |
Only in Crypt-OpenSSL-Bignum-0.04-mikem/Bignum: CTX.pm~
Only in Crypt-OpenSSL-Bignum-0.04-mikem: Bignum.bs
Only in Crypt-OpenSSL-Bignum-0.04-mikem: Bignum.c
Only in Crypt-OpenSSL-Bignum-0.04-mikem: Bignum.o
diff -ur /usr/local/src/Crypt-OpenSSL-Bignum-0.04/Bignum.pm Crypt-OpenSSL-Bignum-0.04-mikem/Bignum.pm
--- /usr/local/src/Crypt-OpenSSL-Bignum-0.04/Bignum.pm 2007-05-21 05:07:39.000000000 +1000
+++ Crypt-OpenSSL-Bignum-0.04-mikem/Bignum.pm 2012-06-20 09:17:22.532037027 +1000
@@ -227,6 +227,62 @@
allocated by this BIGNUM object if and when it is done with it. See
also bless_pointer.
+=item new
+
+Returns a new Bignum.
+
+=item rand(rnd, bits, top, bottom)
+
+generates a cryptographically strong pseudo-random number of bits bits in
+length and stores it in rnd. If top is -1, the most significant bit of the
+random number can be zero. If top is 0, it is set to 1, and if top is 1, the
+two most significant bits of the number will be set to 1, so that the product
+of two such random numbers will always have 2*bits length. If bottom is true,
+the number will be odd.
+
+=item pseudo_rand(rnd, bits, top, bottom)
+
+does the same, but pseudo-random numbers generated by this function are not
+necessarily unpredictable. They can be used for non-cryptographic purposes and
+for certain purposes in cryptographic protocols, but usually not for key
+generation etc.
+
+=item rand_range(rnd, range)
+
+generates a cryptographically strong pseudo-random number rnd in the range 0
+<lt>= rnd < range. BN_pseudo_rand_range() does the same, but is based on
+BN_pseudo_rand(), and hence numbers generated by it are not necessarily
+unpredictable.
+
+=item num_bits(a)
+
+returns the number of significant bits in a word. If we take 0x00000432 as an
+example, it returns 11, not 16, not 32. Basically, except for a zero, it
+returns floor(log2(w))+1.
+
+=item num_bytes(a)
+
+returns the size of a BIGNUM in bytes
+
+=item rshift(r, a, n)
+
+shifts a right by n bits and places the result in r ("r=a/2^n"). BN_rshift1()
+ shifts a right by one and places the result in r ("r=a/2").
+
+=item lshift(r, a, n)
+
+shifts a left by n bits and places the result in r
+ ("r=a*2^n"). BN_lshift1() shifts a left by one and places the result in
+ r ("r=2*a").
+
+=item ucmp(a, b)
+
+returns -1 if a < b, 0 if a == b and 1 if a > b, using the absolute values of a and b.
+
+=item swap(a, b)
+
+exchanges the values of a and b.
+
=back
=head1 AUTHOR
Only in Crypt-OpenSSL-Bignum-0.04-mikem: Bignum.pm~
diff -ur /usr/local/src/Crypt-OpenSSL-Bignum-0.04/Bignum.xs Crypt-OpenSSL-Bignum-0.04-mikem/Bignum.xs
--- /usr/local/src/Crypt-OpenSSL-Bignum-0.04/Bignum.xs 2003-04-28 06:02:21.000000000 +1000
+++ Crypt-OpenSSL-Bignum-0.04-mikem/Bignum.xs 2012-06-20 09:15:40.516040641 +1000
@@ -2,6 +2,7 @@
#include "perl.h"
#include "XSUB.h"
+#include <openssl/err.h>
#include <openssl/ssl.h>
#include <openssl/bn.h>
@@ -245,7 +246,7 @@
BIGNUM* bn;
PPCODE:
if( items > 4 )
- croak( "usage: $bn->add( $bn2, $ctx, [, $target] )" );
+ croak( "usage: $bn->mod( $bn2, $ctx, [, $target] )" );
bn = ( items < 4 ) ? BN_new() : sv2bn( ST(3) );
checkOpenSslCall( BN_mod( bn, a, b, ctx ) );
ST(0) = ( (items < 4 ) ? proto_obj( bn ) : ST(3) );
@@ -370,6 +371,67 @@
OUTPUT:
RETVAL
+# mikem
+
+BIGNUM*
+BN_new(p_proto)
+ SV* p_proto;
+ CODE:
+ RETVAL = BN_new();
+ OUTPUT:
+ RETVAL
+
+int
+BN_rand(rnd, bits, top, bottom)
+ BIGNUM *rnd;
+ int bits;
+ int top;
+ int bottom;
+
+int
+BN_pseudo_rand(rnd, bits, top, bottom)
+ BIGNUM *rnd;
+ int bits;
+ int top;
+ int bottom;
+
+int
+BN_rand_range(rnd, range)
+ BIGNUM *rnd;
+ BIGNUM *range;
+
+int
+BN_num_bits(a)
+ BIGNUM *a;
+
+int
+BN_num_bytes(a)
+ BIGNUM *a;
+
+int
+BN_rshift(r, a, n)
+ BIGNUM *r;
+ BIGNUM *a;
+ int n;
+
+int
+BN_lshift(r, a, n)
+ BIGNUM *r;
+ BIGNUM *a;
+ int n;
+
+int
+BN_ucmp(a, b)
+ BIGNUM *a;
+ BIGNUM *b;
+
+void
+BN_swap(a, b)
+ BIGNUM *a;
+ BIGNUM *b;
+
+# End mikem
+
MODULE = Crypt::OpenSSL::Bignum PACKAGE = Crypt::OpenSSL::Bignum::CTX PREFIX=BN_CTX_
BN_CTX*
Only in Crypt-OpenSSL-Bignum-0.04-mikem: Bignum.xs~
Only in Crypt-OpenSSL-Bignum-0.04-mikem: blib
diff -ur /usr/local/src/Crypt-OpenSSL-Bignum-0.04/Changes Crypt-OpenSSL-Bignum-0.04-mikem/Changes
--- /usr/local/src/Crypt-OpenSSL-Bignum-0.04/Changes 2007-05-21 05:09:12.000000000 +1000
+++ Crypt-OpenSSL-Bignum-0.04-mikem/Changes 2012-06-20 09:25:47.937019039 +1000
@@ -1,5 +1,10 @@
Revision history for Perl extension Crypt::OpenSSL::Bignum.
+????? ?????
+ - mikem fixed a usage typo in mod()
+ - mikem added new functions new(), rand(), pseudo_rand(), rand_range(),
+ num_bits(), num_bytes(), rshift(), lshift(), ucmp(), swap().
+
0.04 Sun May 20 2007 13:08:23
- Add a LICENSE file.
- Add -DOPENSSL_NO_KRB5 to DEFINE to keep redhat happy.
Only in Crypt-OpenSSL-Bignum-0.04-mikem: Changes~
Only in Crypt-OpenSSL-Bignum-0.04-mikem: Makefile
Only in Crypt-OpenSSL-Bignum-0.04-mikem: MYMETA.json
Only in Crypt-OpenSSL-Bignum-0.04-mikem: MYMETA.yml
Only in Crypt-OpenSSL-Bignum-0.04-mikem: pm_to_blib
diff -ur /usr/local/src/Crypt-OpenSSL-Bignum-0.04/test.pl Crypt-OpenSSL-Bignum-0.04-mikem/test.pl
--- /usr/local/src/Crypt-OpenSSL-Bignum-0.04/test.pl 2003-02-17 13:48:26.000000000 +1000
+++ Crypt-OpenSSL-Bignum-0.04-mikem/test.pl 2012-06-20 09:20:20.755030728 +1000
@@ -6,7 +6,7 @@
# change 'tests => 1' to 'tests => last_test_to_print';
use Test;
-BEGIN { plan tests => 52 };
+BEGIN { plan tests => 62 };
use Crypt::OpenSSL::Bignum;
use Crypt::OpenSSL::Bignum::CTX;
@@ -124,3 +124,21 @@
ok( 4 == $bn3->mod_exp( $bn6, $bn25, $ctx )->get_word() );
ok( 36 == $bn6->sqr( $ctx )->get_word() );
ok( 12 == $bn23->mod_inverse( $bn25, $ctx )->get_word() );
+
+# mikem:
+my $rand = Crypt::OpenSSL::Bignum->new();
+ok($rand);
+ok($rand->rand(32, 0, 0));
+ok($rand->pseudo_rand(32, 0, 0));
+my $range = Crypt::OpenSSL::Bignum->new_from_decimal('1000');
+ok($rand->rand_range($range));
+ok($range->num_bits() == 10);
+ok($range->num_bytes() == 2);
+my $n = Crypt::OpenSSL::Bignum->new_from_decimal('2');
+ok($range->rshift($range, 2));
+$n = Crypt::OpenSSL::Bignum->new_from_decimal('-250');
+ok($range->cmp($n) == 1);
+ok($range->ucmp($n) == 0);
+$range->swap($rand);
+ok($range->lshift($range, 2));
+
Only in Crypt-OpenSSL-Bignum-0.04-mikem: test.pl~