| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Crypt::RSA::Primitives; |
|
2
|
8
|
|
|
8
|
|
437
|
use strict; |
|
|
8
|
|
|
|
|
16
|
|
|
|
8
|
|
|
|
|
182
|
|
|
3
|
8
|
|
|
8
|
|
36
|
use warnings; |
|
|
8
|
|
|
|
|
16
|
|
|
|
8
|
|
|
|
|
176
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
## Crypt::RSA::Primitives -- Cryptography and encoding primitives |
|
6
|
|
|
|
|
|
|
## used by Crypt::RSA. |
|
7
|
|
|
|
|
|
|
## |
|
8
|
|
|
|
|
|
|
## Copyright (c) 2001, Vipul Ved Prakash. All rights reserved. |
|
9
|
|
|
|
|
|
|
## This code is free software; you can redistribute it and/or modify |
|
10
|
|
|
|
|
|
|
## it under the same terms as Perl itself. |
|
11
|
|
|
|
|
|
|
|
|
12
|
8
|
|
|
8
|
|
36
|
use base 'Crypt::RSA::Errorhandler'; |
|
|
8
|
|
|
|
|
15
|
|
|
|
8
|
|
|
|
|
486
|
|
|
13
|
8
|
|
|
8
|
|
2355
|
use Crypt::RSA::Debug qw(debug); |
|
|
8
|
|
|
|
|
19
|
|
|
|
8
|
|
|
|
|
369
|
|
|
14
|
8
|
|
|
8
|
|
46
|
use Math::BigInt try => 'GMP, Pari'; |
|
|
8
|
|
|
|
|
15
|
|
|
|
8
|
|
|
|
|
47
|
|
|
15
|
8
|
|
|
8
|
|
5588
|
use Carp; |
|
|
8
|
|
|
|
|
18
|
|
|
|
8
|
|
|
|
|
3921
|
|
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub new { |
|
18
|
17
|
|
|
17
|
1
|
485
|
return bless {}, shift; |
|
19
|
|
|
|
|
|
|
} |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub core_encrypt { |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# procedure: |
|
25
|
|
|
|
|
|
|
# c = (m ** e) mod n |
|
26
|
|
|
|
|
|
|
|
|
27
|
292
|
|
|
292
|
0
|
1263
|
my ($self, %params) = @_; |
|
28
|
292
|
|
|
|
|
699
|
my $key = $params{Key}; |
|
29
|
292
|
50
|
|
|
|
1122
|
$self->error ("Bad key.", \%params, $key) unless $key->check(); |
|
30
|
292
|
50
|
|
|
|
1015
|
my $plaintext = (defined $params{Message}) ? $params{Message} : $params{Plaintext}; |
|
31
|
292
|
100
|
|
|
|
931
|
$plaintext = Math::BigInt->new("$plaintext") if ref($plaintext) ne 'Math::BigInt'; |
|
32
|
292
|
|
|
|
|
1215
|
debug ("pt == $plaintext"); |
|
33
|
|
|
|
|
|
|
|
|
34
|
292
|
|
|
|
|
1280
|
my $e = $key->e; |
|
35
|
292
|
|
|
|
|
1037
|
my $n = $key->n; |
|
36
|
292
|
50
|
|
|
|
1146
|
return $self->error ("Numeric representation of plaintext is out of bound.", |
|
37
|
|
|
|
|
|
|
\$plaintext, $key, \%params) if $plaintext > $n; |
|
38
|
292
|
|
|
|
|
16320
|
my $c = $plaintext->bmodpow($e, $n); |
|
39
|
292
|
|
|
|
|
9108435
|
debug ("ct == $c"); |
|
40
|
292
|
|
|
|
|
783
|
$n = undef; |
|
41
|
292
|
|
|
|
|
609
|
$e = undef; |
|
42
|
292
|
|
|
|
|
1973
|
return $c; |
|
43
|
|
|
|
|
|
|
} |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub core_decrypt { |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# procedure: |
|
49
|
|
|
|
|
|
|
# p = (c ** d) mod n |
|
50
|
|
|
|
|
|
|
|
|
51
|
294
|
|
|
294
|
0
|
2069
|
my ($self, %params) = @_; |
|
52
|
294
|
|
|
|
|
809
|
my $key = $params{Key}; |
|
53
|
294
|
50
|
|
|
|
1452
|
$self->error ("Bad key.") unless $key->check(); |
|
54
|
|
|
|
|
|
|
|
|
55
|
294
|
50
|
|
|
|
1256
|
my $cyphertext = defined $params{Cyphertext} ? $params{Cyphertext} : $params{Ciphertext}; |
|
56
|
294
|
50
|
|
|
|
1100
|
$cyphertext = Math::BigInt->new("$cyphertext") if ref($cyphertext) ne 'Math::BigInt'; |
|
57
|
294
|
|
|
|
|
1636
|
my $n = $key->n; |
|
58
|
294
|
|
|
|
|
1412
|
my $d = $key->d; |
|
59
|
294
|
50
|
|
|
|
1424
|
return $self->error ("Decryption error.") if $cyphertext > $n; |
|
60
|
|
|
|
|
|
|
|
|
61
|
294
|
|
|
|
|
17800
|
my $pt; |
|
62
|
294
|
100
|
66
|
|
|
1549
|
if ($key->p && $key->q) { |
|
63
|
|
|
|
|
|
|
# Garner's CRT algorithm |
|
64
|
275
|
|
|
|
|
10136
|
my $p = $key->p; |
|
65
|
275
|
|
|
|
|
1097
|
my $q = $key->q; |
|
66
|
275
|
50
|
|
|
|
1254
|
$key->u ($p->copy->bmodinv($q)) unless defined $key->u; |
|
67
|
275
|
50
|
|
|
|
1204
|
$key->dp($d % ($p-1) ) unless defined $key->dp; |
|
68
|
275
|
50
|
|
|
|
1196
|
$key->dq($d % ($q-1) ) unless defined $key->dq; |
|
69
|
275
|
|
|
|
|
1113
|
my $u = $key->u; |
|
70
|
275
|
|
|
|
|
987
|
my $dp = $key->dp; |
|
71
|
275
|
|
|
|
|
1169
|
my $dq = $key->dq; |
|
72
|
275
|
|
|
|
|
998
|
my $p2 = ($cyphertext % $p)->bmodpow($dp, $p); |
|
73
|
275
|
|
|
|
|
57053481
|
my $q2 = ($cyphertext % $q)->bmodpow($dq, $q); |
|
74
|
275
|
|
|
|
|
56425344
|
$pt = $p2 + ($p * ((($q2 - $p2) * $u) % $q)); |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
else { |
|
77
|
19
|
|
|
|
|
98
|
$pt = $cyphertext->copy->bmodpow($d, $n); |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
|
|
80
|
294
|
|
|
|
|
39700869
|
debug ("ct == $cyphertext"); |
|
81
|
294
|
|
|
|
|
1187
|
debug ("pt == $pt"); |
|
82
|
294
|
|
|
|
|
2360
|
return $pt; |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub core_sign { |
|
87
|
|
|
|
|
|
|
|
|
88
|
35
|
|
|
35
|
0
|
188
|
my ($self, %params) = @_; |
|
89
|
35
|
|
33
|
|
|
166
|
$params{Cyphertext} = $params{Message} || $params{Plaintext}; |
|
90
|
35
|
|
|
|
|
1412
|
return $self->core_decrypt (%params); |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub core_verify { |
|
96
|
|
|
|
|
|
|
|
|
97
|
33
|
|
|
33
|
0
|
172
|
my ($self, %params) = @_; |
|
98
|
33
|
|
|
|
|
94
|
$params{Plaintext} = $params{Signature}; |
|
99
|
33
|
|
|
|
|
180
|
return $self->core_encrypt (%params); |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
1; |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 NAME |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Crypt::RSA::Primitives - RSA encryption, decryption, signature and verification primitives. |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
my $prim = new Crypt::RSA::Primitives; |
|
113
|
|
|
|
|
|
|
my $ctxt = $prim->core_encrypt (Key => $key, Plaintext => $string); |
|
114
|
|
|
|
|
|
|
my $ptxt = $prim->core_decrypt (Key => $key, Cyphertext => $ctxt); |
|
115
|
|
|
|
|
|
|
my $sign = $prim->core_sign (Key => $key, Message => $string); |
|
116
|
|
|
|
|
|
|
my $vrfy = $prim->core_verify (Key => $key, Signature => $sig); |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
This module implements RSA encryption, decryption, signature and |
|
121
|
|
|
|
|
|
|
verification primitives. These primitives should only be used in the |
|
122
|
|
|
|
|
|
|
context of an encryption or signing scheme. See Crypt::RSA::ES::OAEP(3), |
|
123
|
|
|
|
|
|
|
and Crypt::RSA::SS::PSS(3) for the implementation of two such schemes. |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 ERROR HANDLING |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
See B in Crypt::RSA(3) manpage. |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 AUTHOR |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Vipul Ved Prakash, Email@vipul.netE |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Crypt::RSA(3), Crypt::RSA::Key(3), Crypt::RSA::ES::OAEP(3), |
|
136
|
|
|
|
|
|
|
Crypt::RSA::SS::PSS(3) |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=cut |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|