lib/Crypt/RSA/ES/OAEP.pm

filter: Statementstmt 97.0% 131 / 135 Branchbran 56.6% 17 / 30 Conditioncond 41.6% 5 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 20 / 20 Podpod 40.0% 4 / 10 totaltotal 82.7% 177 / 214 CC 21 SCAR 16.3
CC
21
Cov
86%
decrypt
13.5
encrypt
5.3
decode
4.0
CRAP 5.1
« lib/Crypt/RSA/ES/PKCS1v15.pm ↑ Summary lib/Crypt/RSA/Primitives.pm »
1 #!/usr/bin/perl -sw
2 ##
3 ## Crypt::RSA::ES::OAEP
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: OAEP.pm,v 1.24 2001/06/22 23:27:37 vipul Exp $
10  
11 package Crypt::RSA::ES::OAEP;
12 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use base 'Crypt::RSA::Errorhandler';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Crypt::Random qw(makerandom_octet);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Crypt::RSA::DataFormat qw(bitsize os2ip i2osp octet_xor mgf1 octet_len);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Crypt::RSA::Primitives;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Crypt::RSA::Debug qw(debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Digest::SHA1 qw(sha1);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Math::Pari qw(floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Sort::Versions qw(versioncmp);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 $Crypt::RSA::ES::OAEP::VERSION = '1.99';
24  
25 4 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
26 4 my ($class, %params) = @_;
27 4 my $self = bless { primitives => new Crypt::RSA::Primitives,
28 P => "",
29 hlen => 20,
30 VERSION => $Crypt::RSA::ES::OAEP::VERSION,
31 }, $class;
32 4 ▶ if ($params{Version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"Version"}) 2 2 4
33 2 ▶ if (versioncmp($params{Version}, '1.15') == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if (&versioncmp($params{'Version'}, '1.15') == -1) { } 1 1 2
elsif (&versioncmp($params{'Version'}, $self->{'VERSION'}) == 1) { } 1 0 1
34 1 $$self{P} = "Crypt::RSA";
35 1 $$self{VERSION} = $params{Version};
36 } elsif (versioncmp($params{Version}, $$self{VERSION}) == 1) {
37 1 croak "Required version ($params{Version}) greater than installed version ($$self{VERSION}) of $class.\n";
38 }
39 }
40 3 return $self;
41 }
42  
43  
44 337 ▶sub encrypt {
Subroutine encrypt: called CC=5 SCAR=16.7
Pod documented
45 337 my ($self, %params) = @_;
46 337 ▶ my $key = $params{Key}; my $M = $params{Message} || $params{Plaintext};
Condition33%1/3
$params{'Message'} || $params{'Plaintext'}
l!l&&r!l&&!r
33700
MC/DC0%0/2
$params{'Message'} || $params{'Plaintext'}
$params{'Message'} $params{'Plaintext'}
Truth table33%1/3
A || B
A= $params{'Message'}B= $params{'Plaintext'}
ABresult
1X1
011
000
47 337 ▶ return $self->error ("Missing Message or Plaintext parameter", \$key, \%params) unless $M;
Branch: 1 branch
BranchTrueFalseTotal
unless $M 0 337 337
48 337 ▶ return $self->error ($key->errstr, \$M, $key, \%params) unless $key->check;
Branch: 1 branch
BranchTrueFalseTotal
unless $key->check 0 337 337
49 337 my $k = octet_len ($key->n); debug ("octet_len of modulus: $k");
50 337 ▶ my $em = $self->encode ($M, $self->{P}, $k-1) ||
Condition50%1/2
$self->encode($M, $self->{'P'}, $k - 1) || (return $self->error($self->errstr, \$M, $key, \%params))
l!l
3370
MC/DC0%0/1
$self->encode($M, $self->{'P'}, $k - 1) || (return $self->error($self->errstr, \$M, $key, \%params))
$self->encode($M, $self->{'P'}, $k - 1)
Truth table50%1/2
A
A= $self->encode($M, $self->{'P'}, $k - 1)
Aresult
11
00
51 return $self->error ($self->errstr, \$M, $key, \%params);
52 337 my $m = os2ip ($em);
53 337 my $c = $self->{primitives}->core_encrypt ( Plaintext => $m, Key => $key );
54 337 my $ec = i2osp ($c, $k); debug ("ec: $ec");
55 337 return $ec;
56 }
57  
58  
59 337 ▶sub decrypt {
Subroutine decrypt: called CC=9 SCAR=26.0
Pod documented
60 337 my ($self, %params) = @_;
61 337 ▶ my $key = $params{Key}; my $C = $params{Cyphertext} || $params{Ciphertext};
Condition33%1/3
$params{'Cyphertext'} || $params{'Ciphertext'}
l!l&&r!l&&!r
33700
MC/DC0%0/2
$params{'Cyphertext'} || $params{'Ciphertext'}
$params{'Cyphertext'} $params{'Ciphertext'}
Truth table33%1/3
A || B
A= $params{'Cyphertext'}B= $params{'Ciphertext'}
ABresult
1X1
011
000
62 337 ▶ return $self->error ("Missing Cyphertext or Ciphertext parameter", \$key, \%params) unless $C;
Branch: 1 branch
BranchTrueFalseTotal
unless $C 0 337 337
63 337 ▶ return $self->error ($key->errstr, $key, \%params) unless $key->check;
Branch: 1 branch
BranchTrueFalseTotal
unless $key->check 0 337 337
64 337 my $k = octet_len ($key->n);
65 337 my $c = os2ip ($C);
66 337 ▶ if (bitsize($c) > bitsize($key->n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&bitsize($c) > &bitsize(scalar $key->n)) 0 337 337
67 0 return $self->error ("Decryption error.", $key, \%params)
68 }
69 337 ▶ my $m = $self->{primitives}->core_decrypt (Cyphertext => $c, Key => $key) ||
Condition50%1/2
$self->{'primitives'}->core_decrypt('Cyphertext', $c, 'Key', $key) || (return $self->error('Decryption error.', $key, \%params))
l!l
3370
MC/DC0%0/1
$self->{'primitives'}->core_decrypt('Cyphertext', $c, 'Key', $key) || (return $self->error('Decryption error.', $key, \%params))
$self->{'primitives'}->core_decrypt('Cyphertext', $c, 'Key', $key)
Truth table50%1/2
A
A= $self->{'primitives'}->core_decrypt('Cyphertext', $c, 'Key', $key)
Aresult
11
00
70 return $self->error ("Decryption error.", $key, \%params);
71 337 ▶ my $em = i2osp ($m, $k-1) ||
Condition50%1/2
i2osp($m, $k - 1) || (return $self->error('Decryption error.', $key, \%params))
l!l
3370
MC/DC0%0/1
i2osp($m, $k - 1) || (return $self->error('Decryption error.', $key, \%params))
i2osp($m, $k - 1)
Truth table50%1/2
A
A= i2osp($m, $k - 1)
Aresult
11
00
72 return $self->error ("Decryption error.", $key, \%params);
73 337 my $M; $self->errstrrst; # reset the errstr
74 337 ▶ unless ($M = $self->decode ($em, $$self{P})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($M = $self->decode($em, $self->{"P"})) 0 337 337
75 0 ▶ return $self->error ("Decryption error.", $key, \%params) if $self->errstr();
Branch: 1 branch
BranchTrueFalseTotal
if $self->errstr 0 0 0
76 0 return $M;
77 }
78 337 return $M;
79 }
80  
81  
82 337 ▶sub encode {
Subroutine encode: called CC=3 SCAR=11.0
Pod undocumented
83 337 my ($self, $M, $P, $emlen) = @_;
84 337 my $hlen = $$self{hlen};
85 337 my $mlen = length($M);
86 337 ▶ return $self->error ("Message too long.", \$P, \$M) if $mlen > $emlen-(2*$hlen)-1;
Branch: 1 branch
BranchTrueFalseTotal
if $mlen > $emlen - 2 * $hlen - 1 0 337 337
87 337 my ($PS, $pslen) = ("", 0);
88 337 ▶ if ($pslen = $emlen-(2*$hlen+1+$mlen)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pslen = $emlen - (2 * $hlen + 1 + $mlen)) 4 333 337
89 4 $PS = chr(0)x$pslen;
90 }
91 337 my $phash = $self->hash ($P);
92 337 my $db = $phash . $PS . chr(1) . $M;
93 337 my $seed = makerandom_octet (Length => $hlen);
94 337 my $dbmask = $self->mgf ($seed, $emlen-$hlen);
95 337 my $maskeddb = octet_xor ($db, $dbmask);
96 337 my $seedmask = $self->mgf ($maskeddb, $hlen);
97 337 my $maskedseed = octet_xor ($seed, $seedmask);
98 337 my $em = $maskedseed . $maskeddb;
99  
100 337 debug ("emlen == $emlen");
101 337 debug ("M == $M [" . length($M) . "]");
102 337 debug ("PS == $PS [$pslen]");
103 337 debug ("phash == $phash [" . length($phash) . "]");
104 337 debug ("seed == $seed [" . length($seed) . "]");
105 337 debug ("seedmask == $seedmask [" . length($seedmask) . "]");
106 337 debug ("db == $db [" . length($db) . "]");
107 337 debug ("dbmask == $dbmask [" . length($dbmask) . "]");
108 337 debug ("maskeddb == $maskeddb [" . length($maskeddb) . "]");
109 337 debug ("em == $em [" . length($em) . "]");
110  
111 337 return $em;
112 }
113  
114  
115 337 ▶sub decode {
Subroutine decode: called CC=4 SCAR=13.9
Pod undocumented
116 337 my ($self, $em, $P) = @_;
117 337 my $hlen = $$self{hlen};
118  
119 337 debug ("P == $P");
120 337 ▶ return $self->error ("Decoding error.", \$P) if length($em) < 2*$hlen+1;
Branch: 1 branch
BranchTrueFalseTotal
if length $em < 2 * $hlen + 1 0 337 337
121 337 my $maskedseed = substr $em, 0, $hlen;
122 337 my $maskeddb = substr $em, $hlen;
123 337 my $seedmask = $self->mgf ($maskeddb, $hlen);
124 337 my $seed = octet_xor ($maskedseed, $seedmask);
125 337 my $dbmask = $self->mgf ($seed, length($em) - $hlen);
126 337 my $db = octet_xor ($maskeddb, $dbmask);
127 337 my $phash = $self->hash ($P);
128  
129 337 debug ("em == $em [" . length($em) . "]");
130 337 debug ("phash == $phash [" . length($phash) . "]");
131 337 debug ("seed == $seed [" . length($seed) . "]");
132 337 debug ("seedmask == $seedmask [" . length($seedmask) . "]");
133 337 debug ("maskedseed == $maskedseed [" . length($maskedseed) . "]");
134 337 debug ("db == $db [" . length($db) . "]");
135 337 debug ("maskeddb == $maskeddb [" . length($maskeddb) . "]");
136 337 debug ("dbmask == $dbmask [" . length($dbmask) . "]");
137  
138 337 my ($phashorig) = substr $db, 0, $hlen;
139 337 debug ("phashorig == $phashorig [" . length($phashorig) . "]");
140 337 ▶ return $self->error ("Decoding error.", \$P) unless $phashorig eq $phash;
Branch: 1 branch
BranchTrueFalseTotal
unless $phashorig eq $phash 0 337 337
141 337 $db = substr $db, $hlen;
142 337 my ($chr0, $chr1) = (chr(0), chr(1));
143 337 my ($ps, $m);
144 337 debug ("db == $db [" . length($db) . "]");
145 337 ▶ unless ( ($ps, undef, $m) = $db =~ /^($chr0*)($chr1)(.*)$/s ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (($ps, undef, $m) = $db =~ /^($chr0*)($chr1)(.*)$/s) 0 337 337
146 0 return $self->error ("Decoding error.", \$P);
147 }
148  
149 337 return $m;
150 }
151  
152  
153 674 ▶sub hash {
Subroutine hash: called CC=1 SCAR=0.0
Pod undocumented
154 674 my ($self, $data) = @_;
155 674 return sha1 ($data);
156 }
157  
158  
159 1348 ▶sub mgf {
Subroutine mgf: called CC=1 SCAR=0.0
Pod undocumented
160 1348 my ($self, @data) = @_;
161 1348 return mgf1 (@data);
162 }
163  
164  
165 4 ▶sub encryptblock {
Subroutine encryptblock: called CC=1 SCAR=0.0
Pod undocumented
166 4 my ($self, %params) = @_;
167 4 return octet_len ($params{Key}->n) - 42;
168 }
169  
170  
171 3 ▶sub decryptblock {
Subroutine decryptblock: called CC=1 SCAR=0.0
Pod undocumented
172 3 my ($self, %params) = @_;
173 3 return octet_len ($params{Key}->n);
174 }
175  
176  
177 # should be able to call this as a class method.
178 3 ▶sub version {
Subroutine version: called CC=1 SCAR=0.0
Pod documented
179 3 my $self = shift;
180 3 return $self->{VERSION};
181 }
182  
183  
184 3 1;
185  
186 =head1 NAME
187  
188 Crypt::RSA::ES::OAEP - Plaintext-aware encryption with RSA.
189  
190 =head1 SYNOPSIS
191  
192 my $oaep = new Crypt::RSA::ES::OAEP;
193  
194 my $ct = $oaep->encrypt( Key => $key, Message => $message ) ||
195 die $oaep->errstr;
196  
197 my $pt = $oaep->decrypt( Key => $key, Cyphertext => $ct ) ||
198 die $oaep->errstr;
199  
200 =head1 DESCRIPTION
201  
202 This module implements Optimal Asymmetric Encryption, a plaintext-aware
203 encryption scheme based on RSA. The notion of plaintext-aware implies it's
204 computationally infeasible to obtain full or partial information about a
205 message from a cyphertext, and computationally infeasible to generate a
206 valid cyphertext without knowing the corresponding message.
207 Plaintext-aware schemes, such as OAEP, are semantically secure,
208 non-malleable and secure against chosen-ciphertext attack. For more
209 information on OAEP and plaintext-aware encryption, see [3], [9] & [13].
210  
211 =head1 METHODS
212  
213 =head2 B<new()>
214  
215 Constructor.
216  
217 =head2 B<version()>
218  
219 Returns the version number of the module.
220  
221 =head2 B<encrypt()>
222  
223 Encrypts a string with a public key and returns the encrypted string
224 on success. encrypt() takes a hash argument with the following
225 mandatory keys:
226  
227 =over 4
228  
229 =item B<Message>
230  
231 A string to be encrypted. The length of this string should not exceed k-42
232 octets, where k is the octet length of the RSA modulus. If Message is
233 longer than k-42, the method will fail and set $self->errstr to "Message
234 too long." This means the key must be at least _336_ bits long if you are
235 to use OAEP.
236  
237 =item B<Key>
238  
239 Public key of the recipient, a Crypt::RSA::Key::Public object.
240  
241 =back
242  
243 =head2 B<decrypt()>
244  
245 Decrypts cyphertext with a private key and returns plaintext on
246 success. $self->errstr is set to "Decryption Error." or appropriate
247 error on failure. decrypt() takes a hash argument with the following
248 mandatory keys:
249  
250 =over 4
251  
252 =item B<Cyphertext>
253  
254 A string encrypted with encrypt(). The length of the cyphertext must be k
255 octets, where k is the length of the RSA modulus.
256  
257 =item B<Key>
258  
259 Private key of the receiver, a Crypt::RSA::Key::Private object.
260  
261 =item B<Version>
262  
263 Version of the module that was used for creating the Cyphertext. This is
264 an optional argument. When present, decrypt() will ensure before
265 proceeding that the installed version of the module can successfully
266 decrypt the Cyphertext.
267  
268 =back
269  
270 =head1 ERROR HANDLING
271  
272 See ERROR HANDLING in Crypt::RSA(3) manpage.
273  
274 =head1 BIBLIOGRAPHY
275  
276 See BIBLIOGRAPHY in Crypt::RSA(3) manpage.
277  
278 =head1 AUTHOR
279  
280 Vipul Ved Prakash, E<lt>mail@vipul.netE<gt>
281  
282 =head1 SEE ALSO
283  
284 Crypt::RSA(3), Crypt::RSA::Primitives(3), Crypt::RSA::Keys(3),
285 Crypt::RSA::SSA::PSS(3)
286  
287 =cut
288  
289  
« lib/Crypt/RSA/ES/PKCS1v15.pm ↑ Summary lib/Crypt/RSA/Primitives.pm »