| 1 | #!/usr/bin/perl -sw | ||||||||||||||||||||||||||||
| 2 | ## | ||||||||||||||||||||||||||||
| 3 | ## Crypt::RSA::Primitives -- Cryptography and encoding primitives | ||||||||||||||||||||||||||||
| 4 | ## used by Crypt::RSA. | ||||||||||||||||||||||||||||
| 5 | ## | ||||||||||||||||||||||||||||
| 6 | ## Copyright (c) 2001, Vipul Ved Prakash. All rights reserved. | ||||||||||||||||||||||||||||
| 7 | ## This code is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 8 | ## it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 9 | ## | ||||||||||||||||||||||||||||
| 10 | ## $Id: Primitives.pm,v 1.14 2001/06/22 23:27:35 vipul Exp $ | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | package Crypt::RSA::Primitives; | ||||||||||||||||||||||||||||
| 13 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 8 | ▶ | use base 'Crypt::RSA::Errorhandler'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 8 | ▶ | use Crypt::RSA::Debug qw(debug); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 8 | ▶ | use Math::Pari qw(PARI Mod lift); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 13 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 13 | return bless {}, shift; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 750 | ▶ | sub core_encrypt { | ||||||||||||||||||||||||||
|
Subroutine
core_encrypt: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # procedure: | ||||||||||||||||||||||||||||
| 27 | # c = (m ** e) mod n | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 750 | my ($self, %params) = @_; | |||||||||||||||||||||||||||
| 30 | 750 | my $key = $params{Key}; | |||||||||||||||||||||||||||
| 31 | 750 | ▶ | $self->error ("Bad key.", \%params, $key) unless $key->check(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 750 | ▶ | my $plaintext = $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'}
| |||||||||||||||||||||||||||||
| 33 | 750 | debug ("pt == $plaintext"); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 750 | my $e = $key->e; my $n = $key->n; | |||||||||||||||||||||||||||
| 36 | 750 | ▶ | return $self->error ("Numeric representation of plaintext is out of bound.", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | \$plaintext, $key, \%params) if $plaintext > $n; | ||||||||||||||||||||||||||||
| 38 | 750 | my $c = mod_exp($plaintext, $e, $n); | |||||||||||||||||||||||||||
| 39 | 750 | debug ("ct == $c"); | |||||||||||||||||||||||||||
| 40 | 750 | $n = undef; $e = undef; | |||||||||||||||||||||||||||
| 41 | 750 | return $c; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 752 | ▶ | sub core_decrypt { | ||||||||||||||||||||||||||
|
Subroutine
core_decrypt: called CC=9 SCAR=22.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # procedure: | ||||||||||||||||||||||||||||
| 49 | # p = (c ** d) mod n | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 752 | my ($self, %params) = @_; | |||||||||||||||||||||||||||
| 53 | 752 | my $key = $params{Key}; | |||||||||||||||||||||||||||
| 54 | 752 | ▶ | $self->error ("Bad key.") unless $key->check(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 752 | ▶ | my $cyphertext = $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'}
| |||||||||||||||||||||||||||||
| 57 | 752 | my $n = $key->n; | |||||||||||||||||||||||||||
| 58 | 752 | ▶ | return $self->error ("Decryption error.") if $cyphertext > $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 752 | my $pt; | |||||||||||||||||||||||||||
| 61 | 752 | ▶ | if ($key->p && $key->q) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$key->p and $key->q
MC/DC50%1/2
$key->p and $key->q
$key->p
$key->q
Truth table66%2/3
A and B
A= $key->pB= $key->q
| |||||||||||||||||||||||||||||
| 62 | 544 | my($p, $q, $d) = ($key->p, $key->q, $key->d); | |||||||||||||||||||||||||||
| 63 | 544 | ▶ | $key->u (mod_inverse($p, $q)) unless $key->u; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 544 | ▶ | $key->dp ($d % ($p-1)) unless $key->dp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 544 | ▶ | $key->dq ($d % ($q-1)) unless $key->dq; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 544 | my $p2 = mod_exp($cyphertext % $p, $key->dp, $p); | |||||||||||||||||||||||||||
| 67 | 544 | my $q2 = mod_exp($cyphertext % $q, $key->dq, $q); | |||||||||||||||||||||||||||
| 68 | 544 | $pt = $p2 + ($p * ((($q2 - $p2) * $key->u) % $q)); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||||||||||
| 71 | 208 | $pt = mod_exp ($cyphertext, $key->d, $n); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 752 | debug ("ct == $cyphertext"); | |||||||||||||||||||||||||||
| 75 | 752 | debug ("pt == $pt"); | |||||||||||||||||||||||||||
| 76 | 752 | return $pt; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 412 | ▶ | sub core_sign { | ||||||||||||||||||||||||||
|
Subroutine
core_sign: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 412 | my ($self, %params) = @_; | |||||||||||||||||||||||||||
| 84 | 412 | ▶ | $params{Cyphertext} = $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'}
| |||||||||||||||||||||||||||||
| 85 | 412 | return $self->core_decrypt (%params); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 410 | ▶ | sub core_verify { | ||||||||||||||||||||||||||
|
Subroutine
core_verify: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 410 | my ($self, %params) = @_; | |||||||||||||||||||||||||||
| 93 | 410 | $params{Plaintext} = $params{Signature}; | |||||||||||||||||||||||||||
| 94 | 410 | return $self->core_encrypt (%params); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub mod_inverse { | ||||||||||||||||||||||||||
|
Subroutine
mod_inverse: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 100 | 0 | my($a, $n) = @_; | |||||||||||||||||||||||||||
| 101 | 0 | my $m = Mod(1, $n); | |||||||||||||||||||||||||||
| 102 | 0 | lift($m / $a); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 2046 | ▶ | sub mod_exp { | ||||||||||||||||||||||||||
|
Subroutine
mod_exp: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 107 | 2046 | my($a, $exp, $n) = @_; | |||||||||||||||||||||||||||
| 108 | 2046 | my $m = Mod($a, $n); | |||||||||||||||||||||||||||
| 109 | 2046 | lift($m ** $exp); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 8 | 1; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head1 NAME | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Crypt::RSA::Primitives - RSA encryption, decryption, signature and verification primitives. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | my $prim = new Crypt::RSA::Primitives; | ||||||||||||||||||||||||||||
| 122 | my $ctxt = $prim->core_encrypt (Key => $key, Plaintext => $string); | ||||||||||||||||||||||||||||
| 123 | my $ptxt = $prim->core_decrypt (Key => $key, Cyphertext => $ctxt); | ||||||||||||||||||||||||||||
| 124 | my $sign = $prim->core_sign (Key => $key, Message => $string); | ||||||||||||||||||||||||||||
| 125 | my $vrfy = $prim->core_verify (Key => $key, Signature => $sig); | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | This module implements RSA encryption, decryption, signature and | ||||||||||||||||||||||||||||
| 130 | verfication primitives. These primitives should only be used in the | ||||||||||||||||||||||||||||
| 131 | context of an encryption or signing scheme. See Crypt::RSA::ES::OAEP(3), | ||||||||||||||||||||||||||||
| 132 | and Crypt::RSA::SS::PSS(3) for the implementation of two such schemes. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 ERROR HANDLING | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | See B<ERROR HANDLING> in Crypt::RSA(3) manpage. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Vipul Ved Prakash, E<lt>mail@vipul.netE<gt> | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Crypt::RSA(3), Crypt::RSA::Key(3), Crypt::RSA::ES::OAEP(3), | ||||||||||||||||||||||||||||
| 145 | Crypt::RSA::SS::PSS(3) | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =cut | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||