File Coverage

lib/Crypt/RSA/Primitives.pm
Criterion Covered Total %
statement 63 66 95.5
branch 9 16 56.2
condition 5 12 41.7
subroutine 13 14 92.9
pod 1 7 14.3
total 91 115 79.1


line stmt bran cond sub pod time code
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     8   126 use lib qw(lib);
  8         162  
  8         129  
14 8     8   121 use strict;
  8         153  
  8         228  
15 8     8   118 use vars qw(@ISA);
  8         75  
  8         141  
16 8     8   123 use Crypt::RSA::Errorhandler;
  8         70  
  8         128  
17 8     8   276 use Crypt::RSA::Debug qw(debug);
  8         84  
  8         756  
18 8     8   390 use Math::Pari qw(PARI Mod lift);
  8         73  
  8         173  
19 8     8   144 use Carp;
  8         73  
  8         147  
20             @ISA = qw(Crypt::RSA::Errorhandler);
21              
22             sub new {
23 13     13 1 656     return bless {}, shift;
24             } 
25              
26              
27             sub core_encrypt {
28              
29             # procedure:
30             # c = (m ** e) mod n
31              
32 750     750 0 10958     my ($self, %params) = @_;
33 750         7589     my $key = $params{Key};
34 750 50       12848     $self->error ("Bad key.", \%params, $key) unless $key->check();
35 750   33     11886     my $plaintext = $params{Message} || $params{Plaintext};
36 750         27825     debug ("pt == $plaintext");
37              
38 750         18084     my $e = $key->e; my $n = $key->n;
  750         233770  
39 750 50       11016     return $self->error ("Numeric representation of plaintext is out of bound.",
40                                       \$plaintext, $key, \%params) if $plaintext > $n;
41 750         8989     my $c = mod_exp($plaintext, $e, $n);
42 750         27882     debug ("ct == $c");
43 750         12465     $n = undef; $e = undef;
  750         13387  
44 750         14516     return $c;
45              
46             }
47              
48              
49             sub core_decrypt {
50              
51             # procedure:
52             # p = (c ** d) mod n
53              
54              
55 752     752 0 10478     my ($self, %params) = @_;
56 752         7046     my $key = $params{Key};
57 752 50       10838     $self->error ("Bad key.") unless $key->check();
58              
59 752   33     9478     my $cyphertext = $params{Cyphertext} || $params{Ciphertext};
60 752         27354     my $n = $key->n;
61 752 50       12515     return $self->error ("Decryption error.") if $cyphertext > $n;
62              
63 752         8617     my $pt;
64 752 100 66     20954     if ($key->p && $key->q) {
65 544         7104         my($p, $q, $d) = ($key->p, $key->q, $key->d);
66 544 50       8690         $key->u (mod_inverse($p, $q)) unless $key->u;
67 544 50       239901         $key->dp ($d % ($p-1)) unless $key->dp;
68 544 50       7793         $key->dq ($d % ($q-1)) unless $key->dq;
69 544         216250         my $p2 = mod_exp($cyphertext % $p, $key->dp, $p);
70 544         14458         my $q2 = mod_exp($cyphertext % $q, $key->dq, $q);
71 544         215477         $pt = $p2 + ($p * ((($q2 - $p2) * $key->u) % $q));
72                 }
73                 else {
74 208         3417         $pt = mod_exp ($cyphertext, $key->d, $n);
75                 }
76              
77 752         481109     debug ("ct == $cyphertext");
78 752         28993     debug ("pt == $pt");
79 752         17049     return $pt;
80              
81             }
82              
83              
84             sub core_sign {
85              
86 412     412 0 7852     my ($self, %params) = @_;
87 412   33     6179     $params{Cyphertext} = $params{Message} || $params{Plaintext};
88 412         226604     return $self->core_decrypt (%params);
89              
90             } 
91              
92              
93             sub core_verify {
94              
95 410     410 0 9086     my ($self, %params) = @_;
96 410         4708     $params{Plaintext} = $params{Signature};
97 410         6387     return $self->core_encrypt (%params);
98              
99             }
100              
101              
102             sub mod_inverse {
103 0     0 0 0     my($a, $n) = @_;
104 0         0     my $m = Mod(1, $n);
105 0         0     lift($m / $a);
106             }
107              
108              
109             sub mod_exp {
110 2046     2046 0 24054     my($a, $exp, $n) = @_;
111 2046         460362     my $m = Mod($a, $n);
112 2046         6589203     lift($m ** $exp);
113             }
114              
115              
116             1;
117              
118             =head1 NAME
119            
120             Crypt::RSA::Primitives - RSA encryption, decryption, signature and verification primitives.
121            
122             =head1 SYNOPSIS
123            
124             my $prim = new Crypt::RSA::Primitives;
125             my $ctxt = $prim->core_encrypt (Key => $key, Plaintext => $string);
126             my $ptxt = $prim->core_decrypt (Key => $key, Cyphertext => $ctxt);
127             my $sign = $prim->core_sign (Key => $key, Message => $string);
128             my $vrfy = $prim->core_verify (Key => $key, Signature => $sig);
129            
130             =head1 DESCRIPTION
131            
132             This module implements RSA encryption, decryption, signature and
133             verfication primitives. These primitives should only be used in the
134             context of an encryption or signing scheme. See Crypt::RSA::ES::OAEP(3),
135             and Crypt::RSA::SS::PSS(3) for the implementation of two such schemes.
136            
137             =head1 ERROR HANDLING
138            
139             See B<ERROR HANDLING> in Crypt::RSA(3) manpage.
140            
141             =head1 AUTHOR
142            
143             Vipul Ved Prakash, E<lt>mail@vipul.netE<gt>
144            
145             =head1 SEE ALSO
146            
147             Crypt::RSA(3), Crypt::RSA::Key(3), Crypt::RSA::ES::OAEP(3),
148             Crypt::RSA::SS::PSS(3)
149            
150             =cut
151              
152              
153