| 1 | #!/usr/bin/perl -sw | ||||||||||||||||||||
| 2 | ## | ||||||||||||||||||||
| 3 | ## Crypt::RSA::SS: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.6 2001/06/22 23:27:38 vipul Exp $ | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | package Crypt::RSA::SS::PKCS1v15; | ||||||||||||||||||||
| 12 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 2 | ▶ | use base 'Crypt::RSA::Errorhandler'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 2 | ▶ | use Crypt::RSA::DataFormat qw(octet_len os2ip i2osp h2osp); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 2 | ▶ | use Crypt::RSA::Primitives; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 2 | ▶ | use Crypt::RSA::Debug qw(debug); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 2 | ▶ | use Digest::SHA1 qw(sha1); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 2 | ▶ | use Digest::MD5 qw(md5); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 2 | ▶ | use Digest::MD2 qw(md2); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 2 | ▶ | use Math::Pari qw(floor); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 2 | $Crypt::RSA::SS::PKCS1v15::VERSION = '1.99'; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 4 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 4 | my ($class, %params) = @_; | |||||||||||||||||||
| 27 | my $self = bless { | ||||||||||||||||||||
| 28 | primitives => new Crypt::RSA::Primitives, | ||||||||||||||||||||
| 29 | 4 | ▶ | digest => $params{Digest} || 'SHA1', | ||||||||||||||||||
|
Condition100%2/2
$params{'Digest'} || 'SHA1'
MC/DC100%1/1
$params{'Digest'} || 'SHA1'
$params{'Digest'}
Truth table100%2/2
A
A= $params{'Digest'}
| |||||||||||||||||||||
| 30 | encoding => { | ||||||||||||||||||||
| 31 | MD2 => "0x 30 20 30 0C 06 08 2A 86 48 | ||||||||||||||||||||
| 32 | 86 F7 0D 02 02 05 00 04 10", | ||||||||||||||||||||
| 33 | MD5 => "0x 30 20 30 0C 06 08 2A 86 48 | ||||||||||||||||||||
| 34 | 86 F7 0D 02 05 05 00 04 10", | ||||||||||||||||||||
| 35 | SHA1 => "0x 30 21 30 09 06 05 2B 0E 03 | ||||||||||||||||||||
| 36 | 02 1A 05 00 04 14", | ||||||||||||||||||||
| 37 | }, | ||||||||||||||||||||
| 38 | VERSION => $Crypt::RSA::SS::PKCS1v15::VERSION, | ||||||||||||||||||||
| 39 | }, $class; | ||||||||||||||||||||
| 40 | 4 | ▶ | if ($params{Version}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | # do versioning here | ||||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | 4 | return $self; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 204 | ▶ | sub sign { | ||||||||||||||||||
|
Subroutine
sign: called CC=7 SCAR=23.7
Pod
documented
| |||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 204 | my ($self, %params) = @_; | |||||||||||||||||||
| 51 | 204 | ▶ | 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'}
| |||||||||||||||||||||
| 52 | 204 | ▶ | return $self->error ("No Message or Plaintext parameter", \$key, \%params) unless $M; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | 204 | ▶ | return $self->error ("No Key parameter", \$M, \%params) unless $key; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 204 | my $k = octet_len ($key->n); | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 204 | my $em; | |||||||||||||||||||
| 57 | 204 | ▶ | unless ($em = $self->encode ($M, $k-1)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 0 | ▶ | return $self->error ($self->errstr, \$key, \%params, \$M) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | if $self->errstr eq "Message too long."; | ||||||||||||||||||||
| 60 | 0 | ▶ | return $self->error ("Modulus too short.", \$key, \%params, \$M) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | if $self->errstr eq "Intended encoded message length too short"; | ||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 204 | my $m = os2ip ($em); | |||||||||||||||||||
| 65 | 204 | my $sig = $self->{primitives}->core_sign (Key => $key, Message => $m); | |||||||||||||||||||
| 66 | 204 | return i2osp ($sig, $k); | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 203 | ▶ | sub verify { | ||||||||||||||||||
|
Subroutine
verify: called CC=12 SCAR=30.1
Pod
documented
| |||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 203 | my ($self, %params) = @_; | |||||||||||||||||||
| 74 | 203 | ▶ | 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'}
| |||||||||||||||||||||
| 75 | 203 | my $S = $params{Signature}; | |||||||||||||||||||
| 76 | 203 | ▶ | return $self->error ("No Message or Plaintext parameter", \$key, \%params) unless $M; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 203 | ▶ | return $self->error ("No Key parameter", \$M, \$S, \%params) unless $key; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 203 | ▶ | return $self->error ("No Signature parameter", \$key, \$M, \%params) unless $S; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 203 | my $k = octet_len ($key->n); | |||||||||||||||||||
| 80 | 203 | ▶ | return $self->error ("Invalid signature.", \$key, \$M, \%params) if length($S) != $k; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 203 | my $s = os2ip ($S); | |||||||||||||||||||
| 82 | 203 | ▶ | my $m = $self->{primitives}->core_verify (Key => $key, Signature => $s) || | ||||||||||||||||||
|
Condition33%1/3
$self->{'primitives'}->core_verify('Key', $key, 'Signature', $s) || $self->error('Invalid signature.', \$M, $key, \%params)
MC/DC0%0/2
$self->{'primitives'}->core_verify('Key', $key, 'Signature', $s) || $self->error('Invalid signature.', \$M, $key, \%params)
$self->{'primitives'}->core_verify('Key', $key, 'Signature', $s)
$self->error('Invalid signature.', \$M, $key, \%params)
Truth table33%1/3
A || B
A= $self->{'primitives'}->core_verify('Key', $key, 'Signature', $s)B= $self->error('Invalid signature.', \$M, $key, \%params)
| |||||||||||||||||||||
| 83 | $self->error ("Invalid signature.", \$M, $key, \%params); | ||||||||||||||||||||
| 84 | 203 | ▶ | my $em = i2osp ($m, $k-1) || | ||||||||||||||||||
|
Condition100%2/2
i2osp($m, $k - 1) || (return $self->error('Invalid signature.', \$M, \$S, $key, \%params))
MC/DC100%1/1
i2osp($m, $k - 1) || (return $self->error('Invalid signature.', \$M, \$S, $key, \%params))
i2osp($m, $k - 1)
Truth table100%2/2
A
A= i2osp($m, $k - 1)
| |||||||||||||||||||||
| 85 | return $self->error ("Invalid signature.", \$M, \$S, $key, \%params); | ||||||||||||||||||||
| 86 | 103 | my $em1; | |||||||||||||||||||
| 87 | 103 | ▶ | unless ($em1 = $self->encode ($M, $k-1)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | 0 | ▶ | return $self->error ($self->errstr, \$key, \%params, \$M) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | if $self->errstr eq "Message too long."; | ||||||||||||||||||||
| 90 | 0 | ▶ | return $self->error ("Modulus too short.", \$key, \%params, \$M) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | if $self->errstr eq "Intended encoded message length too short."; | ||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 103 | debug ("em: $em"); debug ("em1: $em1"); | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 103 | ▶ | return 1 if $em eq $em1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | return $self->error ("Invalid signature.", \$M, \$key, \%params); | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 307 | ▶ | sub encode { | ||||||||||||||||||
|
Subroutine
encode: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 307 | my ($self, $M, $emlen) = @_; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 307 | my $H; | |||||||||||||||||||
| 107 | 307 | ▶ | if ($self->{digest} eq "SHA1") { $H = sha1 ($M) } | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 108 | 2 | elsif ($self->{digest} eq "MD5" ) { $H = md5 ($M) } | |||||||||||||||||||
| 109 | 2 | elsif ($self->{digest} eq "MD2" ) { $H = md2 ($M) } | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 307 | my $alg = h2osp($self->{encoding}->{$self->{digest}}); | |||||||||||||||||||
| 112 | 307 | my $T = $alg . $H; | |||||||||||||||||||
| 113 | 307 | ▶ | $self->error ("Intended encoded message length too short.", \$M) if $emlen < length($T) + 10; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 307 | my $pslen = $emlen - length($T) - 2; | |||||||||||||||||||
| 115 | 307 | my $PS = chr(0xff) x $pslen; | |||||||||||||||||||
| 116 | 307 | my $em = chr(1) . $PS . chr(0) . $T; | |||||||||||||||||||
| 117 | 307 | return $em; | |||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 0 | ▶ | sub version { | ||||||||||||||||||
|
Subroutine
version: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 123 | 0 | my $self = shift; | |||||||||||||||||||
| 124 | 0 | return $self->{VERSION}; | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 0 | ▶ | sub signblock { | ||||||||||||||||||
|
Subroutine
signblock: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 129 | 0 | return -1; | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 3 | ▶ | sub verifyblock { | ||||||||||||||||||
|
Subroutine
verifyblock: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 134 | 3 | my ($self, %params) = @_; | |||||||||||||||||||
| 135 | 3 | return octet_len($params{Key}->n); | |||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 2 | 1; | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =head1 NAME | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | Crypt::RSA::SS::PKCS1v15 - PKCS #1 v1.5 signatures. | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =head1 SYNOPSIS | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | my $pkcs = new Crypt::RSA::SS::PKCS1v15 ( | ||||||||||||||||||||
| 148 | Digest => 'MD5' | ||||||||||||||||||||
| 149 | ); | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | my $signature = $pkcs->sign ( | ||||||||||||||||||||
| 152 | Message => $message, | ||||||||||||||||||||
| 153 | Key => $private, | ||||||||||||||||||||
| 154 | ) || die $pss->errstr; | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | my $verify = $pkcs->verify ( | ||||||||||||||||||||
| 157 | Message => $message, | ||||||||||||||||||||
| 158 | Key => $key, | ||||||||||||||||||||
| 159 | Signature => $signature, | ||||||||||||||||||||
| 160 | ) || die $pss->errstr; | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =head1 DESCRIPTION | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | This module implements PKCS #1 v1.5 signatures based on RSA. See [13] | ||||||||||||||||||||
| 166 | for details on the scheme. | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | =head1 METHODS | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | =head2 B<new()> | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | Constructor. Takes a hash as argument with the following key: | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =over 4 | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =item B<Digest> | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | Name of the Message Digest algorithm. Three Digest algorithms are | ||||||||||||||||||||
| 179 | supported: MD2, MD5 and SHA1. Digest defaults to SHA1. | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | =back | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | =head2 B<version()> | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | Returns the version number of the module. | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | =head2 B<sign()> | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | Computes a PKCS #1 v1.5 signature on a message with the private key of the | ||||||||||||||||||||
| 191 | signer. sign() takes a hash argument with the following mandatory keys: | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | =over 4 | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | =item B<Message> | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | Message to be signed, a string of arbitrary length. | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | =item B<Key> | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | Private key of the signer, a Crypt::RSA::Key::Private object. | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | =back | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | =head2 B<verify()> | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | Verifies a signature generated with sign(). Returns a true value on | ||||||||||||||||||||
| 208 | success and false on failure. $self->errstr is set to "Invalid signature." | ||||||||||||||||||||
| 209 | or appropriate error on failure. verify() takes a hash argument with the | ||||||||||||||||||||
| 210 | following mandatory keys: | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | =over 4 | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | =item B<Key> | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | Public key of the signer, a Crypt::RSA::Key::Public object. | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | =item B<Message> | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | The original signed message, a string of arbitrary length. | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | =item B<Signature> | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | Signature computed with sign(), a string. | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | =back | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | =head1 ERROR HANDLING | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | See ERROR HANDLING in Crypt::RSA(3) manpage. | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | =head1 BIBLIOGRAPHY | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | See BIBLIOGRAPHY in Crypt::RSA(3) manpage. | ||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | =head1 AUTHOR | ||||||||||||||||||||
| 237 | |||||||||||||||||||||
| 238 | Vipul Ved Prakash, E<lt>mail@vipul.netE<gt> | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | =head1 SEE ALSO | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | Crypt::RSA(3), Crypt::RSA::Primitives(3), Crypt::RSA::Keys(3), | ||||||||||||||||||||
| 243 | Crypt::RSA::EME::OAEP(3) | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | =cut | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | |||||||||||||||||||||