| 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 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use base 'Crypt::RSA::Errorhandler'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | ▶ | use Crypt::Random qw(makerandom_octet); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 1 | ▶ | use Crypt::RSA::DataFormat qw(bitsize octet_len os2ip i2osp); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 1 | ▶ | use Crypt::RSA::Primitives; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 1 | ▶ | use Crypt::RSA::Debug qw(debug); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 1 | ▶ | use Math::Pari qw(floor); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 1 | ▶ | use Sort::Versions qw(versioncmp); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 1 | $Crypt::RSA::ES::PKCS1v15::VERSION = '1.99'; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 25 | 1 | my ($class, %params) = @_; | |||||||||||||||||||
| 26 | 1 | my $self = bless { primitives => new Crypt::RSA::Primitives, | |||||||||||||||||||
| 27 | VERSION => $Crypt::RSA::ES::PKCS1v15::VERSION, | ||||||||||||||||||||
| 28 | }, $class; | ||||||||||||||||||||
| 29 | 1 | ▶ | if ($params{Version}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | # do versioning here. | ||||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | 1 | return $self; | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 2 | ▶ | sub encrypt { | ||||||||||||||||||
|
Subroutine
encrypt: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||||||||||||
| 37 | 2 | my ($self, %params) = @_; | |||||||||||||||||||
| 38 | 2 | ▶ | my $key = $params{Key}; my $M = $params{Message} || $params{Plaintext}; | ||||||||||||||||||
|
Condition33%1/3
$params{'Message'} || $params{'Plaintext'}
MC/DC0%0/2
$params{'Message'} || $params{'Plaintext'}
$params{'Message'}
$params{'Plaintext'}
Truth table33%1/3
A || B
A= $params{'Message'}B= $params{'Plaintext'}
| |||||||||||||||||||||
| 39 | 2 | ▶ | return $self->error ("No Message or Plaintext parameter", \$key, \%params) unless $M; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | 2 | ▶ | return $self->error ($key->errstr, \$M, $key, \%params) unless $key->check; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 2 | my $k = octet_len ($key->n); debug ("octet_len of modulus: $k"); | |||||||||||||||||||
| 42 | 2 | ▶ | my $em = $self->encode ($M, $k-1) || | ||||||||||||||||||
|
Condition50%1/2
$self->encode($M, $k - 1) || (return $self->error($self->errstr, \$M, $key, \%params))
MC/DC0%0/1
$self->encode($M, $k - 1) || (return $self->error($self->errstr, \$M, $key, \%params))
$self->encode($M, $k - 1)
Truth table50%1/2
A
A= $self->encode($M, $k - 1)
| |||||||||||||||||||||
| 43 | return $self->error ($self->errstr, \$M, $key, \%params); | ||||||||||||||||||||
| 44 | 2 | debug ("encoded: $em"); | |||||||||||||||||||
| 45 | 2 | my $m = os2ip ($em); | |||||||||||||||||||
| 46 | 2 | my $c = $self->{primitives}->core_encrypt (Plaintext => $m, Key => $key); | |||||||||||||||||||
| 47 | 2 | my $ec = i2osp ($c, $k); debug ("cyphertext: $ec"); | |||||||||||||||||||
| 48 | 2 | return $ec; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 2 | ▶ | sub decrypt { | ||||||||||||||||||
|
Subroutine
decrypt: called CC=9 SCAR=25.5
Pod
documented
| |||||||||||||||||||||
| 53 | 2 | my ($self, %params) = @_; | |||||||||||||||||||
| 54 | 2 | ▶ | my $key = $params{Key}; my $C = $params{Cyphertext} || $params{Ciphertext}; | ||||||||||||||||||
|
Condition33%1/3
$params{'Cyphertext'} || $params{'Ciphertext'}
MC/DC0%0/2
$params{'Cyphertext'} || $params{'Ciphertext'}
$params{'Cyphertext'}
$params{'Ciphertext'}
Truth table33%1/3
A || B
A= $params{'Cyphertext'}B= $params{'Ciphertext'}
| |||||||||||||||||||||
| 55 | 2 | ▶ | return $self->error ("No Cyphertext or Ciphertext parameter", \$key, \%params) unless $C; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 2 | ▶ | return $self->error ($key->errstr, $key, \%params) unless $key->check; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 2 | my $k = octet_len ($key->n); | |||||||||||||||||||
| 58 | 2 | my $c = os2ip ($C); | |||||||||||||||||||
| 59 | 2 | debug ("bitsize(c): " . bitsize($c)); | |||||||||||||||||||
| 60 | 2 | debug ("bitsize(n): " . bitsize($key->n)); | |||||||||||||||||||
| 61 | 2 | ▶ | if (bitsize($c) > bitsize($key->n)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | 0 | return $self->error ("Decryption error.", $key, \%params) | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | 2 | ▶ | 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))
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)
| |||||||||||||||||||||
| 65 | return $self->error ("Decryption error.", $key, \%params); | ||||||||||||||||||||
| 66 | 2 | ▶ | my $em = i2osp ($m, $k-1) || | ||||||||||||||||||
|
Condition50%1/2
i2osp($m, $k - 1) || (return $self->error('Decryption error.', $key, \%params))
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)
| |||||||||||||||||||||
| 67 | return $self->error ("Decryption error.", $key, \%params); | ||||||||||||||||||||
| 68 | 2 | my $M; $self->errstrrst; # reset the errstr | |||||||||||||||||||
| 69 | 2 | ▶ | unless ($M = $self->decode ($em)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 0 | ▶ | return $self->error ("Decryption error.", $key, \%params) if $self->errstr(); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 0 | return $M; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | 2 | return $M; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 2 | ▶ | sub encode { | ||||||||||||||||||
|
Subroutine
encode: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||
| 78 | 2 | my ($self, $M, $emlen) = @_; | |||||||||||||||||||
| 79 | 2 | ▶ | $M = $M || ""; my $mlen = length($M); | ||||||||||||||||||
|
Condition50%1/2
$M || ''
MC/DC0%0/1
$M || ''
$M
Truth table50%1/2
A
A= $M
| |||||||||||||||||||||
| 80 | 2 | ▶ | return $self->error ("Message too long.", \$M) if $mlen > $emlen-10; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 2 | my ($PS, $pslen) = ("", 0); | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 2 | $pslen = $emlen-$mlen-2; | |||||||||||||||||||
| 84 | 2 | $PS = makerandom_octet (Length => $pslen, Skip => chr(0)); | |||||||||||||||||||
| 85 | 2 | my $em = chr(2).$PS.chr(0).$M; | |||||||||||||||||||
| 86 | 2 | return $em; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 2 | ▶ | sub decode { | ||||||||||||||||||
|
Subroutine
decode: called CC=4 SCAR=14.6
Pod
undocumented
| |||||||||||||||||||||
| 91 | 2 | my ($self, $em) = @_; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 2 | ▶ | return $self->error ("Decoding error.") if length($em) < 10; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 2 | debug ("to decode: $em"); | |||||||||||||||||||
| 96 | 2 | my ($chr0, $chr2) = (chr(0), chr(2)); | |||||||||||||||||||
| 97 | 2 | my ($ps, $M); | |||||||||||||||||||
| 98 | 2 | ▶ | unless ( ($ps, $M) = $em =~ /^$chr2(.*?)$chr0(.*)$/s ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 0 | return $self->error ("Decoding error."); | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | 2 | ▶ | return $self->error ("Decoding error.") if length($ps) < 8; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 2 | return $M; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 1 | ▶ | sub encryptblock { | ||||||||||||||||||
|
Subroutine
encryptblock: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 107 | 1 | my ($self, %params) = @_; | |||||||||||||||||||
| 108 | 1 | return octet_len ($params{Key}->n) - 11; | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 1 | ▶ | sub decryptblock { | ||||||||||||||||||
|
Subroutine
decryptblock: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 113 | 1 | my ($self, %params) = @_; | |||||||||||||||||||
| 114 | 1 | return octet_len ($params{Key}->n); | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 0 | ▶ | sub version { | ||||||||||||||||||
|
Subroutine
version: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 119 | 0 | my $self = shift; | |||||||||||||||||||
| 120 | 0 | return $self->{VERSION}; | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 1 | 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<new()> | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | Constructor. | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | =head2 B<version()> | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | Returns the version number of the module. | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | =head2 B<encrypt()> | ||||||||||||||||||||
| 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<Message> | ||||||||||||||||||||
| 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<Key> | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | Public key of the recipient, a Crypt::RSA::Key::Public object. | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =back | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =head2 B<decrypt()> | ||||||||||||||||||||
| 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<Cyphertext> | ||||||||||||||||||||
| 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<Key> | ||||||||||||||||||||
| 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, E<lt>mail@vipul.netE<gt> | ||||||||||||||||||||
| 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 | |||||||||||||||||||||