Crypto/RSA.pm

filter: Statementstmt 100% 133 / 133 Branchbran 92.6% 63 / 68 Conditioncond 60.8% 14 / 23 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 100% 24 / 24 Podpod 76.9% 10 / 13 totaltotal 90.5% 249 / 275 CC 43 SCAR 17.0
CC
43
Cov
94%
reinit
11.0
decrypt
9.0
verify
7.0
CRAP 5.4
↑ Summary Crypto/Symmetric.pm »
1 ##############################################################################
2 #
3 # Data::Tools::Crypto::RSA perl module
4 # Copyright (c) 2013-2026 Vladi Belperchinov-Shabanski "Cade"
5 # <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org>
6 # http://cade.noxrun.com/
7 #
8 # GPL
9 #
10 ##############################################################################
11 #
12 # freeze/thaw API actually uses JSON as safer option than Storable
13 #
14 # encryption is hybrid: a fresh symmetric key is generated for each message,
15 # the message is encrypted with it and the key itself is wrapped with RSA.
16 # RSA alone can only encrypt keysize-2*hashsize-2 bytes (190 for a 2048-bit
17 # key with SHA256), hybrid encryption removes that limit entirely and adds
18 # authentication of the payload. note that anyone holding the public key can
19 # still produce a well-formed message, RSA encryption alone proves no origin
20 #
21 # new() and reinit() always take the PEM text of the key itself, never a file
22 # name -- loading a key from a file is left to the calling code. the text is
23 # handed to Crypt::PK::RSA by reference, so it is never taken for a file name
24 #
25 # options are honoured or refused, never silently ignored:
26 #
27 # HASH => 'SHA256' hash used for OAEP wrapping and for PSS signatures
28 #
29 # OAEP must carry the 32 byte symmetric key, so the key modulus must be at
30 # least 32 + 2*hashsize + 2 bytes. PSS signing needs less, so OAEP decides:
31 #
32 # HASH hash size min modulus smallest usual key
33 # SHA1 20 bytes 74 bytes ( 592 bits) 1024 bits
34 # SHA224 28 bytes 90 bytes ( 720 bits) 1024 bits
35 # SHA256 (default) 32 bytes 98 bytes ( 784 bits) 1024 bits
36 # SHA3_256 32 bytes 98 bytes ( 784 bits) 1024 bits
37 # SHA384 48 bytes 130 bytes (1040 bits) 1536 bits
38 # SHA512 64 bytes 162 bytes (1296 bits) 1536 bits
39 # SHA3_512 64 bytes 162 bytes (1296 bits) 1536 bits
40 #
41 # a key too small for the chosen hash is refused by reinit(). these are the
42 # minimums which work at all, 2048 bits or more is what should be used
43 #
44 # sign()/verify() work on the message itself and are independent of the
45 # encrypt/decrypt pair -- signing proves origin, encryption alone does not
46 #
47 # a public key can encrypt() and verify(), both need only the modulus and the
48 # public exponent. decrypt() and sign() need the private exponent and boom if
49 # the object was given a public key, that is a configuration error and not a
50 # data-dependent one, which is what the undef return value means everywhere else
51 #
52 ##############################################################################
53 package Data::Tools::Crypto::RSA;
54 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 1 ▶use parent 'Data::Tools::Crypto';
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 ▶use Exception::Sink;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use Crypt::PK::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use Crypt::Digest;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use Crypt::PRNG qw( random_bytes );
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use Data::Tools::Crypto::Symmetric;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use Encode qw( encode_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use Data::Tools qw( str_hex str_unhex );
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 1 our $VERSION = '1.53';
68  
69 1 my $SKEY_LEN = 32; # symmetric key size, as required by Crypto::Symmetric
70  
71 sub new
72 40 ▶{
Subroutine new: called CC=2 SCAR=7.1
Pod documented
73 40 my $class = shift;
74 40 my $key = shift;
75 40 my %opt = @_;
76  
77 40 ▶ $class = ref( $class ) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0400
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
78 40 my $self = {};
79 40 bless $self, $class;
80  
81 40 $self->reinit( $key, %opt );
82  
83 28 return $self;
84 }
85  
86 # sub DESTROY
87 # {
88 # my $self = shift;
89 # }
90  
91 ##############################################################################
92  
93 1 my %KNOWN_OPT = map { $_ => 1 } qw( HASH );
94  
95 sub reinit
96 50 ▶{
Subroutine reinit: called CC=11 SCAR=24.0
Pod documented
97 50 my $self = shift;
98 50 my $key = shift;
99 50 my %opt = @_;
100  
101 50 %$self = (); # clear state
102  
103 50 ▶ boom( "key parameter is not defined" ) unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 4 46 50
104 46 ▶ boom( "key parameter must be PEM text, not a [" . ref( $key ) . "] reference" ) if ref( $key );
Branch: 1 branch
BranchTrueFalseTotal
if ref $key 2 44 46
105 44 ▶ boom( "key parameter cannot be downgraded from UTF-8" ) if utf8::is_utf8( $key ) and ! utf8::downgrade( $key, 1 );
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($key) and not utf8::downgrade($key, 1) 1 43 44
Condition100%3/3
utf8::is_utf8($key) and not utf8::downgrade($key, 1)
!ll&&!rl&&r
4211
MC/DC100%2/2
utf8::is_utf8($key) and not utf8::downgrade($key, 1)
utf8::is_utf8($key) not utf8::downgrade($key, 1)
Truth table100%3/3
A and not(B)
A= utf8::is_utf8($key)B= not utf8::downgrade($key, 1)
ABresult
0X0
100
111
106  
107 43 my @unknown = grep { ! $KNOWN_OPT{ $_ } } keys %opt;
108 43 ▶ boom( "unknown option(s) [" . join( ',', sort @unknown ) . "]" ) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 3 40 43
109  
110 40 ▶ my $hash = $opt{ 'HASH' } || 'SHA256';
Condition100%2/2
$opt{'HASH'} || 'SHA256'
l!l
1426
MC/DC100%1/1
$opt{'HASH'} || 'SHA256'
$opt{'HASH'}
Truth table100%2/2
A
A= $opt{'HASH'}
Aresult
11
00
111  
112 40 my $hlen = eval { Crypt::Digest::hashsize( $hash ) };
113 40 ▶ boom( "invalid or unknown hash [$hash]" ) unless $hlen;
Branch: 1 branch
BranchTrueFalseTotal
unless $hlen 2 38 40
114  
115 # passed by reference, so it is always read as key data and never as a file name
116 38 my $rsa = eval { Crypt::PK::RSA->new( \$key ) };
117 38 ▶ boom( "invalid or unsupported RSA key" ) unless $rsa;
Branch: 1 branch
BranchTrueFalseTotal
unless $rsa 2 36 38
118  
119 # OAEP can carry keysize-2*hashsize-2 bytes and must fit the symmetric key,
120 # otherwise every encrypt() would fail later with nothing to explain why
121 36 my $ksize = $rsa->size();
122 36 my $budget = $ksize - 2 * $hlen - 2;
123 36 ▶ boom( "RSA key too small for [$hash], modulus is [$ksize] bytes, "
Branch: 1 branch
BranchTrueFalseTotal
if $budget < $SKEY_LEN 3 33 36
124 . "needs at least [" . ( $SKEY_LEN + 2 * $hlen + 2 ) . "]" ) if $budget < $SKEY_LEN;
125  
126 33 $self->{ 'RSA' } = $rsa;
127 33 $self->{ 'HASH' } = $hash;
128 33 ▶ $self->{ 'PRIVATE' } = $rsa->is_private() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rsa->is_private ? : 26 7 33
129  
130 33 1;
131 }
132  
133 ##############################################################################
134  
135 sub encrypt
136 1331 ▶{
Subroutine encrypt: called CC=5 SCAR=16.3
Pod documented
137 1331 my $self = shift;
138 1331 my $ptext = shift; # plaintext
139  
140 1331 ▶ boom( "object state is undefined! use reinit() to setup first" ) unless $self->{ 'RSA' };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"RSA"} 2 1329 1331
141 1329 ▶ boom( "plaintext not defined" ) unless defined( $ptext );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ptext 1 1328 1329
142  
143 # fresh symmetric key per message, wrapped with RSA. the payload itself never
144 # passes through RSA, so there is no limit on its size
145 1328 my $skey = random_bytes( $SKEY_LEN );
146  
147 1328 my $wrapped = eval { $self->{ 'RSA' }->encrypt( $skey, 'oaep', $self->{ 'HASH' } ); };
148 1328 ▶ return undef if $@ or ! defined $wrapped;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not defined $wrapped 0 1328 1328
Condition33%1/3
$@ or not defined $wrapped
l!l&&r!l&&!r
001328
MC/DC0%0/2
$@ or not defined $wrapped
$@ not defined $wrapped
Truth table33%1/3
A or not(B)
A= $@B= not defined $wrapped
ABresult
1X1
011
000
149  
150 # utf8 handling and authentication are done by Crypto::Symmetric
151 1328 my $sym = Data::Tools::Crypto::Symmetric->new( $skey );
152  
153 1328 return $wrapped . $sym->encrypt( $ptext );
154 }
155  
156 sub decrypt
157 840 ▶{
Subroutine decrypt: called CC=9 SCAR=22.0
Pod documented
158 840 my $self = shift;
159 840 my $ctext = shift; # cryptotext
160  
161 840 ▶ boom( "object state is undefined! use reinit() to setup first" ) unless $self->{ 'RSA' };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"RSA"} 4 836 840
162 836 ▶ boom( "cryptotext not defined" ) unless defined( $ctext );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ctext 1 835 836
163 835 ▶ boom( "cryptotext must be binary, utf8 scalars are not supported" ) if utf8::is_utf8( $ctext );
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($ctext) 1 834 835
164 834 ▶ boom( "private key required to decrypt, this object holds a public key" ) unless $self->{ 'PRIVATE' };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"PRIVATE"} 3 831 834
165  
166 # the wrapped key is always exactly the modulus size
167 831 my $wlen = $self->{ 'RSA' }->size();
168  
169 831 ▶ return undef if length( $ctext ) <= $wlen;
Branch: 1 branch
BranchTrueFalseTotal
if length $ctext <= $wlen 7 824 831
170  
171 824 my $wrapped = substr( $ctext, 0, $wlen );
172 824 $ctext = substr( $ctext, $wlen );
173  
174 824 my $skey = eval { $self->{ 'RSA' }->decrypt( $wrapped, 'oaep', $self->{ 'HASH' } ); };
175 824 ▶ return undef if $@ or ! defined $skey or length( $skey ) != $SKEY_LEN;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not defined $skey or length $skey != $SKEY_LEN 14 810 824
Condition66%4/6
$@ or not defined $skey or length $skey != $SKEY_LEN
l!l&&r!l&&!r
140810
$@ or not defined $skey
l!l&&r!l&&!r
140810
MC/DC33%1/3
$@ or not defined $skey or length $skey != $SKEY_LEN
$@ not defined $skey length $skey != $SKEY_LEN
Truth table50%2/4
A or not(B) or C
A= $@B= not defined $skeyC= length $skey != $SKEY_LEN
ABCresult
1XX1
01X1
0011
0000
176  
177 810 my $sym = Data::Tools::Crypto::Symmetric->new( $skey );
178  
179 810 return $sym->decrypt( $ctext );
180 }
181  
182 ##############################################################################
183  
184 # signing is over the message bytes plus the same utf8 marker that encrypt()
185 # uses, so that a character string and its utf8 byte form never share a signature
186  
187 sub __sign_body
188 51 ▶{
Subroutine __sign_body: called CC=2 SCAR=6.9
189 51 my $ptext = shift;
190  
191 51 ▶ return 'u' . encode_utf8( $ptext ) if utf8::is_utf8( $ptext );
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($ptext) 8 43 51
192 43 return 'b' . $ptext;
193 }
194  
195 sub sign
196 26 ▶{
Subroutine sign: called CC=6 SCAR=18.1
Pod documented
197 26 my $self = shift;
198 26 my $ptext = shift; # plaintext
199  
200 26 ▶ boom( "object state is undefined! use reinit() to setup first" ) unless $self->{ 'RSA' };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"RSA"} 1 25 26
201 25 ▶ boom( "plaintext not defined" ) unless defined( $ptext );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ptext 1 24 25
202 24 ▶ boom( "private key required to sign, this object holds a public key" ) unless $self->{ 'PRIVATE' };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"PRIVATE"} 5 19 24
203  
204 19 my $sig = eval { $self->{ 'RSA' }->sign_message( __sign_body( $ptext ), $self->{ 'HASH' }, 'pss' ); };
205 19 ▶ return undef if $@ or ! defined $sig;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not defined $sig 0 19 19
Condition33%1/3
$@ or not defined $sig
l!l&&r!l&&!r
0019
MC/DC0%0/2
$@ or not defined $sig
$@ not defined $sig
Truth table33%1/3
A or not(B)
A= $@B= not defined $sig
ABresult
1X1
011
000
206  
207 19 return $sig;
208 }
209  
210 # true when the object holds a private key, so the caller can tell in advance
211 # whether decrypt() and sign() are available to it
212  
213 sub is_private
214 4 ▶{
Subroutine is_private: called CC=2 SCAR=6.9
Pod documented
215 4 my $self = shift;
216  
217 4 ▶ boom( "object state is undefined! use reinit() to setup first" ) unless $self->{ 'RSA' };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"RSA"} 1 3 4
218  
219 3 return $self->{ 'PRIVATE' };
220 }
221  
222 sub verify
223 39 ▶{
Subroutine verify: called CC=7 SCAR=19.5
Pod documented
224 39 my $self = shift;
225 39 my $ptext = shift; # plaintext
226 39 my $sig = shift; # signature, as returned by sign()
227  
228 39 ▶ boom( "object state is undefined! use reinit() to setup first" ) unless $self->{ 'RSA' };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"RSA"} 1 38 39
229 38 ▶ boom( "plaintext not defined" ) unless defined( $ptext );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ptext 1 37 38
230 37 ▶ boom( "signature not defined" ) unless defined( $sig );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sig 4 33 37
231 33 ▶ boom( "signature must be binary, utf8 scalars are not supported" ) if utf8::is_utf8( $sig );
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($sig) 1 32 33
232  
233 32 my $res = eval { $self->{ 'RSA' }->verify_message( $sig, __sign_body( $ptext ), $self->{ 'HASH' }, 'pss' ); };
234 32 ▶ return 0 if $@ or ! $res;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not $res 15 17 32
Condition66%2/3
$@ or not $res
l!l&&r!l&&!r
01517
MC/DC50%1/2
$@ or not $res
$@ not $res
Truth table66%2/3
A or not(B)
A= $@B= not $res
ABresult
1X1
011
000
235  
236 17 return 1;
237 }
238  
239 #-----------------------------------------------------------------------------
240  
241 # encoded signatures. undef is passed through both ways, so a failed sign()
242 # stays undef and an undefined signature booms in verify() as it does there
243  
244 sub sign_hex
245 5 ▶{
Subroutine sign_hex: called CC=2 SCAR=7.1
Pod documented
246 5 my $self = shift;
247  
248 5 my $sig = $self->sign( $_[0] );
249 4 ▶ return defined $sig ? str_hex( $sig ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $sig ? : 4 0 4
250 }
251  
252 sub verify_hex
253 7 ▶{
Subroutine verify_hex: called CC=2 SCAR=6.9
Pod undocumented
254 7 my $self = shift;
255  
256 7 ▶ return $self->verify( $_[0], defined $_[1] ? str_unhex( $_[1] ) : undef );
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 6 1 7
257 }
258  
259 sub sign_base64
260 3 ▶{
Subroutine sign_base64: called CC=2 SCAR=7.1
Pod documented
261 3 my $self = shift;
262  
263 3 my $sig = $self->sign( $_[0] );
264 2 ▶ return defined $sig ? MIME::Base64::encode_base64( $sig, '' ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $sig ? : 2 0 2
265 }
266  
267 sub verify_base64
268 6 ▶{
Subroutine verify_base64: called CC=2 SCAR=6.9
Pod undocumented
269 6 my $self = shift;
270  
271 6 ▶ return $self->verify( $_[0], defined $_[1] ? MIME::Base64::decode_base64( $_[1] ) : undef );
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 5 1 6
272 }
273  
274 sub sign_base64url
275 3 ▶{
Subroutine sign_base64url: called CC=2 SCAR=7.1
Pod documented
276 3 my $self = shift;
277  
278 3 my $sig = $self->sign( $_[0] );
279 2 ▶ return defined $sig ? MIME::Base64::encode_base64url( $sig ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $sig ? : 2 0 2
280 }
281  
282 sub verify_base64url
283 6 ▶{
Subroutine verify_base64url: called CC=2 SCAR=6.9
Pod undocumented
284 6 my $self = shift;
285  
286 6 ▶ return $self->verify( $_[0], defined $_[1] ? MIME::Base64::decode_base64url( $_[1] ) : undef );
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 5 1 6
287 }
288  
289 ##############################################################################
290  
291 =pod
292  
293  
294 =head1 NAME
295  
296 Data::Tools::Crypto::RSA - hybrid public key encryption and digital signatures with an RSA key pair
297  
298 =head1 SYNOPSIS
299  
300 use Data::Tools qw( file_text_load );
301 use Data::Tools::Crypto::RSA;
302 use Exception::Sink;
303  
304 # the key is always the PEM text itself, never a file name
305  
306 my $pem = file_text_load( 'private.pem' );
307 my $crypto = Data::Tools::Crypto::RSA->new( $pem );
308  
309 # --------------------------------------------------------------------------
310  
311 # anyone with the public key can encrypt, only the private key can decrypt
312  
313 my $ctext = $crypto->encrypt( $ptext );
314 my $ptext = $crypto->decrypt( $ctext );
315  
316 boom( "data cannot be decrypted" ) unless defined $ptext;
317  
318 # --------------------------------------------------------------------------
319  
320 # only the private key can sign, anyone with the public key can verify
321  
322 my $sig = $crypto->sign( $message );
323  
324 boom( "message is not authentic" ) unless $crypto->verify( $message, $sig );
325  
326 # the same, with the signature in text-safe encodings
327  
328 my $sig_hex = $crypto->sign_hex( $message );
329 my $sig_b64 = $crypto->sign_base64( $message );
330 my $sig_b64u = $crypto->sign_base64url( $message );
331  
332 boom( "message is not authentic" ) unless $crypto->verify_base64url( $message, $sig_b64u );
333  
334 # --------------------------------------------------------------------------
335  
336 # a different hash can be selected, it applies to both wrapping and signing
337  
338 my $crypto = Data::Tools::Crypto::RSA->new( $pem, HASH => 'SHA512' );
339  
340 # --------------------------------------------------------------------------
341  
342 # the whole encoding and freeze/thaw API comes from
343 # Data::Tools::Crypto, see there for the full list
344  
345 my $sealed = $crypto->freeze_base64url( { name => 'test' } );
346 my $data = $crypto->thaw_base64url( $sealed );
347  
348 # --------------------------------------------------------------------------
349  
350 =head1 DESCRIPTION
351  
352 Data::Tools::Crypto::RSA encrypts with RSA-OAEP and signs with RSA-PSS.
353  
354 Encryption is hybrid. RSA by itself can only encrypt a few hundred bytes, so
355 it is not used on the data at all: a fresh symmetric key is generated for each
356 message, the message is encrypted with Data::Tools::Crypto::Symmetric and only
357 that key is wrapped with RSA. There is no limit on the size of the data, and
358 the payload is authenticated exactly as it is there.
359  
360 =head1 PUBLIC AND PRIVATE KEYS
361  
362 A private key contains the public key, so an object holding a private key can
363 do everything. A public key can only do the two public operations:
364  
365 private key public key
366 encrypt() yes yes
367 verify() yes yes
368 decrypt() yes no
369 sign() yes no
370  
371 This is why a service which only sends encrypted data, or only verifies
372 signatures, should be given the public key alone -- it works exactly the same
373 and the private key never reaches that host.
374  
375 Asking a public key object to decrypt() or sign() is a configuration error,
376 not a data error, and raises an exception saying so.
377  
378 =head1 SIGNATURES
379  
380 Encryption does not prove who sent anything. Anyone holding the public key can
381 produce a well formed message, because that is precisely what the public key
382 is for. Only sign()/verify() establish origin, and they are independent of the
383 encrypt/decrypt pair -- use both when both properties are needed.
384  
385 =head1 METHODS
386  
387 =head2 new( $pem_text, %options )
388  
389 Returns a new object. $pem_text is the PEM text of a public or a private key.
390 It is never taken for a file name, reading a key from a file is left to the
391 calling code. Options:
392  
393 HASH => 'SHA256' hash for OAEP wrapping and PSS signatures, default SHA256
394  
395 Unknown options are refused rather than silently ignored, as is a key too
396 small to be usable -- see KEY SIZE below.
397  
398 =head2 reinit( $pem_text, %options )
399  
400 Sets a new key on an existing object. All previous state is cleared first, so
401 if the key or an option is rejected the object is left without a key and is
402 not usable until a later reinit() succeeds. Catching the exception and
403 handling the unusable object is left to the caller.
404  
405 =head2 encrypt( $plaintext )
406  
407 Returns the cryptotext, which is always binary. There is no limit on the size
408 of the plaintext, the cryptotext is the key modulus size plus 29 bytes longer
409 than the plaintext.
410  
411 =head2 decrypt( $cryptotext )
412  
413 Returns the plaintext, or undef if the cryptotext cannot be decrypted, which
414 covers a wrong key, modified data, truncated data and random input alike.
415 Raises an exception if the object holds a public key.
416  
417 =head2 sign( $message )
418  
419 Returns an RSA-PSS signature, which is always binary and is the key modulus
420 size. Raises an exception if the object holds a public key.
421  
422 =head2 verify( $message, $signature )
423  
424 Returns true if the signature was made over this exact message by the private
425 key matching this object's key, false otherwise. An unusable or malformed
426 signature is simply false, it does not raise an exception.
427  
428 =head2 sign_hex(), sign_base64(), sign_base64url() and their verify pairs
429  
430 As sign()/verify() but with the signature HEX, BASE64 or URL-safe BASE64
431 encoded, so it can be stored or passed as plain text. BASE64 is on a single
432 line with no newlines added. Only the signature is encoded, the message is
433 signed and verified exactly as with sign()/verify():
434  
435 my $sig = $crypto->sign_hex( $message );
436 my $ok = $crypto->verify_hex( $message, $sig );
437  
438 An encoded signature which does not decode to a valid signature is simply
439 false, as in verify().
440  
441 =head2 is_private()
442  
443 True if the object holds a private key, so the caller can tell in advance
444 whether decrypt() and sign() are available to it. Like every other method it
445 raises an exception on an object whose reinit() failed, rather than answering
446 false.
447  
448 =head1 KEY SIZE
449  
450 OAEP can carry keysize minus twice the hash size minus two bytes, and that
451 must fit the 32 byte symmetric key, so the key modulus must be at least
452  
453 32 + 2 * hash size + 2 bytes
454  
455 PSS signatures need less than that, so the OAEP limit is the one which
456 decides. The minimum key size for each HASH option:
457  
458 HASH hash size min modulus smallest usual key
459 SHA1 20 bytes 74 bytes ( 592 bits) 1024 bits
460 SHA224 28 bytes 90 bytes ( 720 bits) 1024 bits
461 SHA256 (default) 32 bytes 98 bytes ( 784 bits) 1024 bits
462 SHA3_256 32 bytes 98 bytes ( 784 bits) 1024 bits
463 SHA384 48 bytes 130 bytes (1040 bits) 1536 bits
464 SHA512 64 bytes 162 bytes (1296 bits) 1536 bits
465 SHA3_512 64 bytes 162 bytes (1296 bits) 1536 bits
466  
467 So a 1024 bit key works with SHA256 but is refused with SHA384 or SHA512.
468 A key too small for the chosen hash is refused when it is loaded, rather than
469 failing on every encrypt() later.
470  
471 These are the smallest keys which work at all, not a recommendation: 1024 bit
472 RSA is no longer considered secure, use 2048 bits or more.
473  
474 =head1 UTF8 HANDLING
475  
476 encrypt()/decrypt() are transparent, exactly as described in
477 Data::Tools::Crypto::Symmetric, which does the work.
478  
479 sign()/verify() cover the same distinction, so a character string and its utf8
480 byte form do not share a signature. Signatures are always binary, passing a
481 character string to verify() raises an exception.
482  
483 =head1 ERRORS
484  
485 The two kinds of failure are reported differently and never confused:
486  
487 * a caller or configuration error raises an exception with boom(), i.e. a
488 key given as a reference instead of PEM text, malformed or unsupported
489 key material, a key too small, an invalid
490 or unknown hash, an unknown option, an undefined argument, a private
491 operation on a public key, or any use of an object whose reinit() failed
492  
493 * data which simply does not decrypt or verify returns undef from decrypt()
494 and false from verify(), which are normal results the caller must check
495  
496 =head1 DATA FORMAT
497  
498 RSA wrapped symmetric key (key modulus size) . Crypto::Symmetric cryptotext
499  
500 =head1 REQUIRED MODULES
501  
502 Data::Tools::Crypto::RSA uses:
503  
504 * Data::Tools::Crypto
505 * Data::Tools::Crypto::Symmetric
506 * Crypt::PK::RSA (CryptX)
507 * Crypt::Digest (CryptX)
508 * Crypt::PRNG (CryptX)
509 * Data::Tools
510 * Encode
511 * Exception::Sink
512 * MIME::Base64
513  
514 =head1 GITHUB REPOSITORY
515  
516 https://github.com/cade-vs/perl-data-tools-crypto
517  
518 git clone https://github.com/cade-vs/perl-data-tools-crypto.git
519  
520 =head1 AUTHOR
521  
522 Vladi Belperchinov-Shabanski "Cade"
523 <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org>
524 http://cade.noxrun.com/
525  
526  
527 =cut
528  
529 ##############################################################################
530  
531 1 1;
↑ Summary Crypto/Symmetric.pm »