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