blib/lib/Crypt/Perl/RSA/Generate.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 83 CC 6 SCAR 29.5
CC
6
Cov
0%
create
42.0
_get_random_prime
2.0
BEGIN
2.0
CRAP 19.1
1 package Crypt::Perl::RSA::Generate;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Crypt::Perl::RSA::Generate - RSA key generation
8  
9 =head1 SYNOPSIS
10  
11 use Crypt::Perl::RSA::Generate ();
12  
13 #$prkey is a Crypt::Perl::RSA::PrivateKey instance.
14 my $prkey = Crypt::Perl::RSA::Generate::create(2048);
15  
16 =head1 DISCUSSION
17  
18 Unfortunately, this is quite slow in Perl—too slow, in fact, if you
19 don’t have either L<Math::BigInt::GMP> or L<Math::BigInt::Pari>.
20 The logic here will still run under pure Perl, but it’ll take too long
21 to be practical.
22  
23 The current L<Math::ProvablePrime> backend is slated to be replaced
24 with L<Math::Prime::Util>; once that happens, pure-Perl operation should
25 be much more feasible.
26  
27 =head1 ALTERNATIVES
28  
29 =over 4
30  
31 =item L<Crypt::OpenSSL::RSA> - probably the fastest way to generate RSA
32 keys in perl. (It relies on XS, so this project can’t use it.)
33  
34 =item Use the C<openssl> binary L<OpenSSL|http://openssl.org> directly,
35 e.g., C<my $rsa_pem = qx/openssl genrsa/>. Most *NIX systems can do this.
36  
37 =back
38  
39 NOTE: As of December 2016, L<Crypt::PK::RSA> is NOT suitable for key
40 generation because it can only generate keys with up to a 512-bit modulus.
41  
42 =cut
43  
44 use strict;
45 use warnings;
46  
47 use Math::ProvablePrime ();
48  
49 use Crypt::Perl::BigInt ();
50 use Crypt::Perl::RSA::PrivateKey ();
51 use Crypt::Perl::X ();
52  
53 use constant PUBLIC_EXPONENTS => ( 65537, 3 );
54  
55 sub create {
56 my ($mod_bits, $exp) = @_;
57  
58 die Crypt::Perl::X::create('Generic', "Need modulus length!") if !$mod_bits;
59  
60 $exp ||= (PUBLIC_EXPONENTS())[0];
61  
62 if (!grep { $exp eq $_ } PUBLIC_EXPONENTS()) {
63 my @allowed = PUBLIC_EXPONENTS();
64 die Crypt::Perl::X::create('Generic', "Invalid public exponent ($exp); should be one of: [@allowed]");
65 }
66  
67 my $qs = $mod_bits >> 1;
68 (ref $exp) or $exp = Crypt::Perl::BigInt->new($exp);
69  
70 while (1) {
71 my ($p, $q, $p1, $q1);
72  
73 #Create a random number, ($mod_bits - $qs) bits long.
74 {
75 $p = _get_random_prime($mod_bits - $qs);
76 $p1 = $p->copy()->bdec();
77  
78 #($p - 1) needs not to be a multiple of $exp
79 redo if $p1->copy()->bmod($exp)->is_zero();
80 }
81  
82 {
83 $q = _get_random_prime($qs);
84 $q1 = $q->copy()->bdec();
85  
86 #Same restriction as on $p applies to $q.
87 #Let’s also make sure these are two different numbers!
88 redo if $q1->copy()->bmod($exp)->is_zero() || $q->beq($p);
89 }
90  
91 #$p should be > $q
92 if ($p->blt($q)) {
93 my $t = $p;
94 $p = $q;
95 $q = $t;
96  
97 $t = $p1;
98 $p1 = $q1;
99 $q1 = $t;
100 }
101  
102 my $phi = $p1->copy()->bmul($q1);
103  
104 my $d = $exp->copy()->bmodinv($phi);
105  
106 my $obj = Crypt::Perl::RSA::PrivateKey->new(
107 {
108 version => 0,
109 modulus => $p->copy()->bmul($q),
110 publicExponent => $exp,
111 privateExponent => $d,
112 prime1 => $p,
113 prime2 => $q,
114 exponent1 => $d->copy()->bmod($p1),
115 exponent2 => $d->copy()->bmod($q1),
116 coefficient => $q->copy()->bmodinv($p),
117 },
118 );
119  
120 return $obj;
121 }
122 }
123  
124 sub _get_random_prime {
125 return Crypt::Perl::BigInt->new( Math::ProvablePrime::find(@_) );
126 }
127  
128 1;