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