| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Crypt::RSA::ES::PKCS1v15; |
|
2
|
1
|
|
|
1
|
|
18704
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
21
|
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
25
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
## Crypt::RSA::ES::PKCS1v15 |
|
6
|
|
|
|
|
|
|
## |
|
7
|
|
|
|
|
|
|
## Copyright (c) 2001, Vipul Ved Prakash. All rights reserved. |
|
8
|
|
|
|
|
|
|
## This code is free software; you can redistribute it and/or modify |
|
9
|
|
|
|
|
|
|
## it under the same terms as Perl itself. |
|
10
|
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
5
|
use base 'Crypt::RSA::Errorhandler'; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
499
|
|
|
12
|
1
|
|
|
1
|
|
804
|
use Bytes::Random::Secure qw/random_bytes random_string_from/; |
|
|
1
|
|
|
|
|
10446
|
|
|
|
1
|
|
|
|
|
58
|
|
|
13
|
1
|
|
|
1
|
|
505
|
use Crypt::RSA::DataFormat qw(bitsize octet_len os2ip i2osp); |
|
|
1
|
|
|
|
|
4
|
|
|
|
1
|
|
|
|
|
95
|
|
|
14
|
1
|
|
|
1
|
|
816
|
use Crypt::RSA::Primitives; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
38
|
|
|
15
|
1
|
|
|
1
|
|
7
|
use Crypt::RSA::Debug qw(debug); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
52
|
|
|
16
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
1437
|
|
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$Crypt::RSA::ES::PKCS1v15::VERSION = '1.99'; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $nonzerobag = join('', map { chr($_) } (1..255)); |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub new { |
|
23
|
1
|
|
|
1
|
1
|
616
|
my ($class, %params) = @_; |
|
24
|
1
|
|
|
|
|
11
|
my $self = bless { primitives => new Crypt::RSA::Primitives, |
|
25
|
|
|
|
|
|
|
VERSION => $Crypt::RSA::ES::PKCS1v15::VERSION, |
|
26
|
|
|
|
|
|
|
}, $class; |
|
27
|
1
|
50
|
|
|
|
6
|
if ($params{Version}) { |
|
28
|
|
|
|
|
|
|
# do versioning here. |
|
29
|
|
|
|
|
|
|
} |
|
30
|
1
|
|
|
|
|
52
|
return $self; |
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub encrypt { |
|
35
|
2
|
|
|
2
|
1
|
587
|
my ($self, %params) = @_; |
|
36
|
2
|
|
33
|
|
|
5
|
my $key = $params{Key}; my $M = $params{Message} || $params{Plaintext}; |
|
|
2
|
|
|
|
|
8
|
|
|
37
|
2
|
50
|
|
|
|
6
|
return $self->error ("No Message or Plaintext parameter", \$key, \%params) unless $M; |
|
38
|
2
|
50
|
|
|
|
13
|
return $self->error ($key->errstr, \$M, $key, \%params) unless $key->check; |
|
39
|
2
|
|
|
|
|
11
|
my $k = octet_len ($key->n); debug ("octet_len of modulus: $k"); |
|
|
2
|
|
|
|
|
14
|
|
|
40
|
2
|
|
50
|
|
|
10
|
my $em = $self->encode ($M, $k-1) || |
|
41
|
|
|
|
|
|
|
return $self->error ($self->errstr, \$M, $key, \%params); |
|
42
|
2
|
|
|
|
|
27
|
debug ("encoded: $em"); |
|
43
|
2
|
|
|
|
|
13
|
my $m = os2ip ($em); |
|
44
|
2
|
|
|
|
|
3794
|
my $c = $self->{primitives}->core_encrypt (Plaintext => $m, Key => $key); |
|
45
|
2
|
|
|
|
|
14
|
my $ec = i2osp ($c, $k); debug ("cyphertext: $ec"); |
|
|
2
|
|
|
|
|
17
|
|
|
46
|
2
|
|
|
|
|
20
|
return $ec; |
|
47
|
|
|
|
|
|
|
} |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub decrypt { |
|
51
|
2
|
|
|
2
|
1
|
23
|
my ($self, %params) = @_; |
|
52
|
2
|
|
33
|
|
|
6
|
my $key = $params{Key}; my $C = $params{Cyphertext} || $params{Ciphertext}; |
|
|
2
|
|
|
|
|
9
|
|
|
53
|
2
|
50
|
|
|
|
9
|
return $self->error ("No Cyphertext or Ciphertext parameter", \$key, \%params) unless $C; |
|
54
|
2
|
50
|
|
|
|
16
|
return $self->error ($key->errstr, $key, \%params) unless $key->check; |
|
55
|
2
|
|
|
|
|
14
|
my $k = octet_len ($key->n); |
|
56
|
2
|
|
|
|
|
11
|
my $c = os2ip ($C); |
|
57
|
2
|
|
|
|
|
3525
|
debug ("bitsize(c): " . bitsize($c)); |
|
58
|
2
|
|
|
|
|
24
|
debug ("bitsize(n): " . bitsize($key->n)); |
|
59
|
2
|
50
|
|
|
|
6
|
if (bitsize($c) > bitsize($key->n)) { |
|
60
|
0
|
|
|
|
|
0
|
return $self->error ("Decryption error.", $key, \%params) |
|
61
|
|
|
|
|
|
|
} |
|
62
|
2
|
|
50
|
|
|
21
|
my $m = $self->{primitives}->core_decrypt (Cyphertext => $c, Key => $key) || |
|
63
|
|
|
|
|
|
|
return $self->error ("Decryption error.", $key, \%params); |
|
64
|
2
|
|
50
|
|
|
87
|
my $em = i2osp ($m, $k-1) || |
|
65
|
|
|
|
|
|
|
return $self->error ("Decryption error.", $key, \%params); |
|
66
|
2
|
|
|
|
|
4
|
my $M; $self->errstrrst; # reset the errstr |
|
|
2
|
|
|
|
|
22
|
|
|
67
|
2
|
50
|
|
|
|
10
|
unless ($M = $self->decode ($em)) { |
|
68
|
0
|
0
|
|
|
|
0
|
return $self->error ("Decryption error.", $key, \%params) if $self->errstr(); |
|
69
|
0
|
|
|
|
|
0
|
return $M; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
2
|
|
|
|
|
22
|
return $M; |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub encode { |
|
76
|
2
|
|
|
2
|
0
|
5
|
my ($self, $M, $emlen) = @_; |
|
77
|
2
|
|
50
|
|
|
5
|
$M = $M || ""; my $mlen = length($M); |
|
|
2
|
|
|
|
|
4
|
|
|
78
|
2
|
50
|
|
|
|
10
|
return $self->error ("Message too long.", \$M) if $mlen > $emlen-10; |
|
79
|
2
|
|
|
|
|
8
|
my ($PS, $pslen) = ("", 0); |
|
80
|
|
|
|
|
|
|
|
|
81
|
2
|
|
|
|
|
4
|
$pslen = $emlen-$mlen-2; |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
#$PS = ''; |
|
84
|
|
|
|
|
|
|
#while (length($PS) < $pslen) { |
|
85
|
|
|
|
|
|
|
# $PS .= random_bytes( $pslen - length($PS) ); |
|
86
|
|
|
|
|
|
|
# $PS =~ s/\x00//g; |
|
87
|
|
|
|
|
|
|
#} |
|
88
|
|
|
|
|
|
|
|
|
89
|
2
|
|
|
|
|
14
|
$PS = random_string_from($nonzerobag, $pslen); |
|
90
|
|
|
|
|
|
|
|
|
91
|
2
|
|
|
|
|
130653261
|
my $em = chr(2).$PS.chr(0).$M; |
|
92
|
2
|
|
|
|
|
12
|
return $em; |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub decode { |
|
97
|
2
|
|
|
2
|
0
|
5
|
my ($self, $em) = @_; |
|
98
|
|
|
|
|
|
|
|
|
99
|
2
|
50
|
|
|
|
7
|
return $self->error ("Decoding error.") if length($em) < 10; |
|
100
|
|
|
|
|
|
|
|
|
101
|
2
|
|
|
|
|
18
|
debug ("to decode: $em"); |
|
102
|
2
|
|
|
|
|
6
|
my ($chr0, $chr2) = (chr(0), chr(2)); |
|
103
|
2
|
|
|
|
|
3
|
my ($ps, $M); |
|
104
|
2
|
50
|
|
|
|
51
|
unless ( ($ps, $M) = $em =~ /^$chr2(.*?)$chr0(.*)$/s ) { |
|
105
|
0
|
|
|
|
|
0
|
return $self->error ("Decoding error."); |
|
106
|
|
|
|
|
|
|
} |
|
107
|
2
|
50
|
|
|
|
8
|
return $self->error ("Decoding error.") if length($ps) < 8; |
|
108
|
2
|
|
|
|
|
8
|
return $M; |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub encryptblock { |
|
113
|
1
|
|
|
1
|
0
|
10
|
my ($self, %params) = @_; |
|
114
|
1
|
|
|
|
|
6
|
return octet_len ($params{Key}->n) - 11; |
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub decryptblock { |
|
119
|
1
|
|
|
1
|
0
|
4
|
my ($self, %params) = @_; |
|
120
|
1
|
|
|
|
|
7
|
return octet_len ($params{Key}->n); |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub version { |
|
125
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
126
|
0
|
|
|
|
|
|
return $self->{VERSION}; |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
1; |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 NAME |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Crypt::RSA::ES::PKCS1v15 - PKCS #1 v1.5 padded encryption scheme based on RSA. |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
my $pkcs = new Crypt::RSA::ES::PKCS1v15; |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
my $ct = $pkcs->encrypt( Key => $key, Message => $message ) || |
|
141
|
|
|
|
|
|
|
die $pkcs->errstr; |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
my $pt = $pkcs->decrypt( Key => $key, Cyphertext => $ct ) || |
|
144
|
|
|
|
|
|
|
die $pkcs->errstr; |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
This module implements PKCS #1 v1.5 padded encryption scheme based on RSA. |
|
149
|
|
|
|
|
|
|
See [13] for details on the encryption scheme. |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 METHODS |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head2 B |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Constructor. |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 B |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Returns the version number of the module. |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 B |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Encrypts a string with a public key and returns the encrypted string |
|
164
|
|
|
|
|
|
|
on success. encrypt() takes a hash argument with the following |
|
165
|
|
|
|
|
|
|
mandatory keys: |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=over 4 |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=item B |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
A string to be encrypted. The length of this string should not exceed k-10 |
|
172
|
|
|
|
|
|
|
octets, where k is the octet length of the RSA modulus. If Message is |
|
173
|
|
|
|
|
|
|
longer than k-10, the method will fail and set $self->errstr to "Message |
|
174
|
|
|
|
|
|
|
too long." |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=item B |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Public key of the recipient, a Crypt::RSA::Key::Public object. |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=back |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 B |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Decrypts cyphertext with a private key and returns plaintext on |
|
185
|
|
|
|
|
|
|
success. $self->errstr is set to "Decryption Error." or appropriate |
|
186
|
|
|
|
|
|
|
error on failure. decrypt() takes a hash argument with the following |
|
187
|
|
|
|
|
|
|
mandatory keys: |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=over 4 |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=item B |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
A string encrypted with encrypt(). The length of the cyphertext must be k |
|
194
|
|
|
|
|
|
|
octets, where k is the length of the RSA modulus. |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=item B |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Private key of the receiver, a Crypt::RSA::Key::Private object. |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=back |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head1 ERROR HANDLING |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
See ERROR HANDLING in Crypt::RSA(3) manpage. |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 BIBLIOGRAPHY |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
See BIBLIOGRAPHY in Crypt::RSA(3) manpage. |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 AUTHOR |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Vipul Ved Prakash, Email@vipul.netE |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
Crypt::RSA(3), Crypt::RSA::Primitives(3), Crypt::RSA::Keys(3), |
|
217
|
|
|
|
|
|
|
Crypt::RSA::SSA::PSS(3) |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=cut |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
|