using CryptX::_croak
Karel Miko
6 years ago
8 | 8 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
9 | 9 | our @EXPORT = qw(); |
10 | 10 | |
11 | use Carp; $Carp::Internal{(__PACKAGE__)}++; | |
11 | use Carp; | |
12 | $Carp::Internal{(__PACKAGE__)}++; | |
12 | 13 | use CryptX; |
13 | 14 | use Crypt::Cipher; |
14 | 15 |
8 | 8 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
9 | 9 | our @EXPORT = qw(); |
10 | 10 | |
11 | use Carp; | |
12 | $Carp::Internal{(__PACKAGE__)}++; | |
11 | 13 | use CryptX; |
12 | 14 | |
13 | sub new { my $class = shift; _new(@_) } | |
15 | sub new { | |
16 | my $class = shift; | |
17 | local $SIG{__DIE__} = \&CryptX::_croak; | |
18 | return _new(@_); | |
19 | } | |
14 | 20 | |
15 | 21 | sub chacha20poly1305_encrypt_authenticate { |
16 | 22 | my $key = shift; |
18 | 24 | my $adata = shift; |
19 | 25 | my $plaintext = shift; |
20 | 26 | |
27 | local $SIG{__DIE__} = \&CryptX::_croak; | |
21 | 28 | my $m = Crypt::AuthEnc::ChaCha20Poly1305->new($key, $iv); |
22 | 29 | $m->adata_add(defined $adata ? $adata : ''); #XXX-TODO if no aad we have to pass empty string |
23 | 30 | my $ct = $m->encrypt_add($plaintext); |
32 | 39 | my $ciphertext = shift; |
33 | 40 | my $tag = shift; |
34 | 41 | |
42 | local $SIG{__DIE__} = \&CryptX::_croak; | |
35 | 43 | my $m = Crypt::AuthEnc::ChaCha20Poly1305->new($key, $iv); |
36 | 44 | $m->adata_add(defined $adata ? $adata : ''); #XXX-TODO if no aad we have to pass empty string |
37 | 45 | my $ct = $m->decrypt_add($ciphertext); |
8 | 8 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
9 | 9 | our @EXPORT = qw(); |
10 | 10 | |
11 | use Carp; | |
12 | $Carp::Internal{(__PACKAGE__)}++; | |
11 | 13 | use CryptX; |
12 | 14 | use Crypt::Cipher; |
13 | 15 | |
21 | 23 | # - decrypt_done |
22 | 24 | # - adata_add |
23 | 25 | |
24 | sub new { my $class = shift; _new(Crypt::Cipher::_trans_cipher_name(shift), @_) } | |
26 | sub new { | |
27 | my $class = shift; | |
28 | local $SIG{__DIE__} = \&CryptX::_croak; | |
29 | return _new(Crypt::Cipher::_trans_cipher_name(shift), @_); | |
30 | } | |
25 | 31 | |
26 | 32 | sub eax_encrypt_authenticate { |
27 | 33 | my $cipher_name = shift; |
30 | 36 | my $adata = shift; |
31 | 37 | my $plaintext = shift; |
32 | 38 | |
39 | local $SIG{__DIE__} = \&CryptX::_croak; | |
33 | 40 | my $m = Crypt::AuthEnc::EAX->new($cipher_name, $key, $iv); |
34 | 41 | $m->adata_add($adata) if defined $adata; |
35 | 42 | my $ct = $m->encrypt_add($plaintext); |
45 | 52 | my $ciphertext = shift; |
46 | 53 | my $tag = shift; |
47 | 54 | |
55 | local $SIG{__DIE__} = \&CryptX::_croak; | |
48 | 56 | my $m = Crypt::AuthEnc::EAX->new($cipher_name, $key, $iv); |
49 | 57 | $m->adata_add($adata) if defined $adata; |
50 | 58 | my $ct = $m->decrypt_add($ciphertext); |
99 | 107 | =head1 DESCRIPTION |
100 | 108 | |
101 | 109 | EAX is a mode that requires a cipher, CTR and OMAC support and provides encryption and authentication. |
102 | It is initialized with a random IV that can be shared publicly, additional authenticated data which can | |
110 | It is initialized with a random IV that can be shared publicly, additional authenticated data which can | |
103 | 111 | be fixed and public, and a random secret symmetric key. |
104 | 112 | |
105 | 113 | =head1 EXPORT |
8 | 8 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
9 | 9 | our @EXPORT = qw(); |
10 | 10 | |
11 | use Carp; | |
12 | $Carp::Internal{(__PACKAGE__)}++; | |
11 | 13 | use CryptX; |
12 | 14 | use Crypt::Cipher; |
13 | 15 | |
14 | 16 | sub new { |
15 | 17 | my ($class, $cipher, $key, $iv) = @_; |
18 | local $SIG{__DIE__} = \&CryptX::_croak; | |
16 | 19 | my $self = _new(Crypt::Cipher::_trans_cipher_name($cipher), $key); |
17 | 20 | # for backwards compatibility the $iv is optional |
18 | 21 | $self->iv_add($iv) if defined $iv; |
26 | 29 | my $adata = shift; |
27 | 30 | my $plaintext = shift; |
28 | 31 | |
32 | local $SIG{__DIE__} = \&CryptX::_croak; | |
29 | 33 | my $m = Crypt::AuthEnc::GCM->new($cipher_name, $key); |
30 | 34 | $m->iv_add($iv); |
31 | 35 | $m->adata_add(defined $adata ? $adata : ''); #XXX-TODO if no aad we have to pass empty string |
42 | 46 | my $ciphertext = shift; |
43 | 47 | my $tag = shift; |
44 | 48 | |
49 | local $SIG{__DIE__} = \&CryptX::_croak; | |
45 | 50 | my $m = Crypt::AuthEnc::GCM->new($cipher_name, $key); |
46 | 51 | $m->iv_add($iv); |
47 | 52 | $m->adata_add(defined $adata ? $adata : ''); #XXX-TODO if no aad we have to pass empty string |
8 | 8 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
9 | 9 | our @EXPORT = qw(); |
10 | 10 | |
11 | use Carp; | |
12 | $Carp::Internal{(__PACKAGE__)}++; | |
11 | 13 | use CryptX; |
12 | 14 | use Crypt::Cipher; |
13 | 15 | |
14 | sub new { my $class = shift; _new(Crypt::Cipher::_trans_cipher_name(shift), @_) } | |
16 | sub new { | |
17 | my $class = shift; | |
18 | local $SIG{__DIE__} = \&CryptX::_croak; | |
19 | return _new(Crypt::Cipher::_trans_cipher_name(shift), @_); | |
20 | } | |
15 | 21 | |
16 | 22 | sub ocb_encrypt_authenticate { |
17 | 23 | my $cipher_name = shift; |
21 | 27 | my $tag_len = shift; |
22 | 28 | my $plaintext = shift; |
23 | 29 | |
30 | local $SIG{__DIE__} = \&CryptX::_croak; | |
24 | 31 | my $m = Crypt::AuthEnc::OCB->new($cipher_name, $key, $nonce, $tag_len); |
25 | 32 | $m->adata_add($adata) if defined $adata; |
26 | 33 | my $ct = $m->encrypt_last($plaintext); |
36 | 43 | my $ciphertext = shift; |
37 | 44 | my $tag = shift; |
38 | 45 | |
46 | local $SIG{__DIE__} = \&CryptX::_croak; | |
39 | 47 | my $m = Crypt::AuthEnc::OCB->new($cipher_name, $key, $nonce, length($tag)); |
40 | 48 | $m->adata_add($adata) if defined $adata; |
41 | 49 | my $ct = $m->decrypt_last($ciphertext); |
2 | 2 | use strict; |
3 | 3 | use warnings; |
4 | 4 | our $VERSION = '0.054_004'; |
5 | ||
5 | 6 | use Carp; |
7 | $Carp::Internal{(__PACKAGE__)}++; | |
6 | 8 | use CryptX; |
7 | 9 | |
8 | 10 | sub addfile { |
2 | 2 | use strict; |
3 | 3 | use warnings; |
4 | 4 | our $VERSION = '0.054_004'; |
5 | ||
5 | 6 | use Carp; |
7 | $Carp::Internal{(__PACKAGE__)}++; | |
6 | 8 | use CryptX; |
7 | 9 | |
8 | 10 | sub addfile { |
12 | 12 | our @EXPORT = qw(); |
13 | 13 | |
14 | 14 | use Carp; |
15 | $Carp::Internal{(__PACKAGE__)}++; | |
16 | use CryptX; | |
15 | 17 | use Crypt::Checksum::Adler32; |
16 | 18 | use Crypt::Checksum::CRC32; |
17 | 19 | |
18 | sub adler32_data { Crypt::Checksum::Adler32->new->add(@_)->digest } | |
19 | sub adler32_data_hex { Crypt::Checksum::Adler32->new->add(@_)->hexdigest } | |
20 | sub adler32_data_int { unpack("N", Crypt::Checksum::Adler32->new->add(@_)->digest) } | |
21 | sub adler32_file { Crypt::Checksum::Adler32->new->addfile(@_)->digest } | |
22 | sub adler32_file_hex { Crypt::Checksum::Adler32->new->addfile(@_)->hexdigest } | |
23 | sub adler32_file_int { unpack("N", Crypt::Checksum::Adler32->new->addfile(@_)->digest) } | |
24 | sub crc32_data { Crypt::Checksum::CRC32->new->add(@_)->digest } | |
25 | sub crc32_data_hex { Crypt::Checksum::CRC32->new->add(@_)->hexdigest } | |
26 | sub crc32_data_int { unpack("N", Crypt::Checksum::CRC32->new->add(@_)->digest) } | |
27 | sub crc32_file { Crypt::Checksum::CRC32->new->addfile(@_)->digest } | |
28 | sub crc32_file_hex { Crypt::Checksum::CRC32->new->addfile(@_)->hexdigest } | |
29 | sub crc32_file_int { unpack("N", Crypt::Checksum::CRC32->new->addfile(@_)->digest) } | |
20 | sub adler32_data { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::Adler32->new->add(@_)->digest } | |
21 | sub adler32_data_hex { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::Adler32->new->add(@_)->hexdigest } | |
22 | sub adler32_data_int { local $SIG{__DIE__} = \&CryptX::_croak; unpack("N", Crypt::Checksum::Adler32->new->add(@_)->digest) } | |
23 | sub adler32_file { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::Adler32->new->addfile(@_)->digest } | |
24 | sub adler32_file_hex { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::Adler32->new->addfile(@_)->hexdigest } | |
25 | sub adler32_file_int { local $SIG{__DIE__} = \&CryptX::_croak; unpack("N", Crypt::Checksum::Adler32->new->addfile(@_)->digest) } | |
26 | sub crc32_data { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->add(@_)->digest } | |
27 | sub crc32_data_hex { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->add(@_)->hexdigest } | |
28 | sub crc32_data_int { local $SIG{__DIE__} = \&CryptX::_croak; unpack("N", Crypt::Checksum::CRC32->new->add(@_)->digest) } | |
29 | sub crc32_file { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->addfile(@_)->digest } | |
30 | sub crc32_file_hex { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->addfile(@_)->hexdigest } | |
31 | sub crc32_file_int { local $SIG{__DIE__} = \&CryptX::_croak; unpack("N", Crypt::Checksum::CRC32->new->addfile(@_)->digest) } | |
30 | 32 | |
31 | 33 | 1; |
32 | 34 | |
39 | 41 | =head1 SYNOPSIS |
40 | 42 | |
41 | 43 | use Crypt::Checksum ':all'; |
42 | ||
44 | ||
43 | 45 | # calculate Adler32 checksum from string/buffer |
44 | 46 | $checksum_raw = adler32_data($data); |
45 | 47 | $checksum_hex = adler32_data_hex($data); |
63 | 65 | # calculate CRC32 checksum from filehandle |
64 | 66 | $checksum_raw = crc32_file(*FILEHANDLE); |
65 | 67 | $checksum_hex = crc32_file_hex(*FILEHANDLE); |
66 | ||
68 | ||
67 | 69 | =head1 DESCRIPTION |
68 | 70 | |
69 | 71 | Calculating CRC32 and Adler32 checksums (functional interface); |
2 | 2 | use strict; |
3 | 3 | use warnings; |
4 | 4 | our $VERSION = '0.054_004'; |
5 | ||
6 | use Carp; | |
7 | $Carp::Internal{(__PACKAGE__)}++; | |
5 | 8 | use CryptX; |
6 | 9 | |
7 | 10 | ### the following methods/functions are implemented in XS: |
40 | 43 | sub new { |
41 | 44 | my $pkg = shift; |
42 | 45 | my $cipher_name = $pkg eq __PACKAGE__ ? _trans_cipher_name(shift) : _trans_cipher_name($pkg); |
46 | local $SIG{__DIE__} = \&CryptX::_croak; | |
43 | 47 | return _new($cipher_name, @_); |
44 | 48 | } |
45 | 49 | |
47 | 51 | my $self = shift; |
48 | 52 | return $self->_blocksize if ref($self); |
49 | 53 | $self = _trans_cipher_name(shift) if $self eq __PACKAGE__; |
54 | local $SIG{__DIE__} = \&CryptX::_croak; | |
50 | 55 | return _block_length_by_name(_trans_cipher_name($self)); |
51 | } | |
52 | ||
53 | sub keysize { | |
54 | max_keysize(@_); | |
55 | 56 | } |
56 | 57 | |
57 | 58 | sub max_keysize |
60 | 61 | return unless defined $self; |
61 | 62 | return $self->_max_keysize if ref($self); |
62 | 63 | $self = _trans_cipher_name(shift) if $self eq __PACKAGE__; |
64 | local $SIG{__DIE__} = \&CryptX::_croak; | |
63 | 65 | return _max_key_length_by_name(_trans_cipher_name($self)); |
64 | 66 | } |
65 | 67 | |
68 | 70 | return unless defined $self; |
69 | 71 | return $self->_min_keysize if ref($self); |
70 | 72 | $self = _trans_cipher_name(shift) if $self eq __PACKAGE__; |
73 | local $SIG{__DIE__} = \&CryptX::_croak; | |
71 | 74 | return _min_key_length_by_name(_trans_cipher_name($self)); |
75 | } | |
76 | ||
77 | sub keysize { | |
78 | goto &max_keysize; | |
72 | 79 | } |
73 | 80 | |
74 | 81 | sub default_rounds { |
76 | 83 | return unless defined $self; |
77 | 84 | return $self->_default_rounds if ref($self); |
78 | 85 | $self = _trans_cipher_name(shift) if $self eq __PACKAGE__; |
86 | local $SIG{__DIE__} = \&CryptX::_croak; | |
79 | 87 | return _default_rounds_by_name(_trans_cipher_name($self)); |
80 | 88 | } |
81 | 89 |
8 | 8 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
9 | 9 | our @EXPORT = qw(); |
10 | 10 | |
11 | use Carp; $Carp::Internal{(__PACKAGE__)}++; | |
11 | use Carp; | |
12 | $Carp::Internal{(__PACKAGE__)}++; | |
12 | 13 | use CryptX; |
13 | 14 | |
14 | 15 | ### the following methods/functions are implemented in XS: |
8 | 8 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
9 | 9 | our @EXPORT = qw(); |
10 | 10 | |
11 | use Carp; | |
12 | $Carp::Internal{(__PACKAGE__)}++; | |
11 | 13 | use CryptX; |
12 | 14 | use Crypt::Digest; |
13 | 15 | |
16 | 18 | $iteration_count ||= 5000; |
17 | 19 | $hash_name = Crypt::Digest::_trans_digest_name($hash_name||'SHA256'); |
18 | 20 | $len ||= 32; |
21 | local $SIG{__DIE__} = \&CryptX::_croak; | |
19 | 22 | return _pkcs_5_alg1($password, $salt, $iteration_count, $hash_name, $len); |
20 | 23 | } |
21 | 24 | |
24 | 27 | $iteration_count ||= 5000; |
25 | 28 | $hash_name = Crypt::Digest::_trans_digest_name($hash_name||'SHA256'); |
26 | 29 | $len ||= 32; |
30 | local $SIG{__DIE__} = \&CryptX::_croak; | |
27 | 31 | return _pkcs_5_alg2($password, $salt, $iteration_count, $hash_name, $len); |
28 | 32 | } |
29 | 33 | |
33 | 37 | my ($keying_material, $salt, $hash_name) = @_; |
34 | 38 | $hash_name = Crypt::Digest::_trans_digest_name($hash_name||'SHA256'); |
35 | 39 | $salt = pack("H*", "00" x Crypt::Digest->hashsize($hash_name)) unless defined $salt; # according to rfc5869 defaults to HashLen zero octets |
40 | local $SIG{__DIE__} = \&CryptX::_croak; | |
36 | 41 | return _hkdf_extract($hash_name, $salt, $keying_material); |
37 | 42 | } |
38 | 43 | |
43 | 48 | $len ||= 32; |
44 | 49 | $info ||= ''; |
45 | 50 | $hash_name = Crypt::Digest::_trans_digest_name($hash_name||'SHA256'); |
51 | local $SIG{__DIE__} = \&CryptX::_croak; | |
46 | 52 | return _hkdf_expand($hash_name, $info, $keying_material, $len); |
47 | 53 | } |
48 | 54 | |
53 | 59 | $info ||= ''; |
54 | 60 | $hash_name = Crypt::Digest::_trans_digest_name($hash_name||'SHA256'); |
55 | 61 | $salt = pack("H*", "00" x Crypt::Digest->hashsize($hash_name)) unless defined $salt; # according to rfc5869 defaults to HashLen zero octets |
62 | local $SIG{__DIE__} = \&CryptX::_croak; | |
56 | 63 | return _hkdf($hash_name, $salt, $info, $keying_material, $len); |
57 | 64 | } |
58 | 65 |
8 | 8 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
9 | 9 | our @EXPORT = qw(); |
10 | 10 | |
11 | use Carp; | |
12 | $Carp::Internal{(__PACKAGE__)}++; | |
13 | use CryptX; | |
14 | ||
11 | 15 | sub add { |
12 | 16 | my $self = shift; |
17 | local $SIG{__DIE__} = \&CryptX::_croak; | |
13 | 18 | $self->_add_single($_) for (@_); |
14 | 19 | return $self; |
15 | 20 | } |
31 | 36 | |
32 | 37 | my $n; |
33 | 38 | my $buf = ""; |
39 | local $SIG{__DIE__} = \&CryptX::_croak; | |
34 | 40 | while (($n = read($handle, $buf, 32*1024))) { |
35 | 41 | $self->_add_single($buf) |
36 | 42 | } |
44 | 50 | 1; |
45 | 51 | |
46 | 52 | __END__ |
47 | ||
53 | ||
48 | 54 | =head1 NAME |
49 | 55 | |
50 | 56 | Crypt::Mac - [internal only] |
353 | 353 | $pk->generate_key($param_hash) |
354 | 354 | # $param_hash is { g => $g, p => $p } |
355 | 355 | # where $g is the generator (base) in a hex string and $p is the prime in a hex string |
356 | ||
356 | ||
357 | 357 | $pk->generate_key(\$dh_param) |
358 | 358 | # $dh_param is the content of DER or PEM file with DH params |
359 | 359 | # e.g. openssl dhparam 2048 |
279 | 279 | $pk->generate_key(\$dsa_param) |
280 | 280 | # $dsa_param is the content of DER or PEM file with DSA params |
281 | 281 | # e.g. openssl dsaparam 2048 |
282 | ||
282 | ||
283 | 283 | =head2 import_key |
284 | 284 | |
285 | 285 | Loads private or public key in DER or PEM format. |
52 | 52 | my @ch = split(//, $chars); |
53 | 53 | my $max_index = $#ch; |
54 | 54 | return if $max_index > 65535; |
55 | ||
55 | ||
56 | 56 | my $mask; |
57 | 57 | for my $n (1..31) { |
58 | 58 | $mask = (1<<$n) - 1; |
31 | 31 | } |
32 | 32 | |
33 | 33 | sub _croak { |
34 | die @_ if ref $_[0]; | |
35 | if ($_[-1] =~ /\n$/s) { | |
36 | my $arg = pop @_; | |
37 | $arg =~ s/(.*)( at .*? line .*?\n$)/$1/s; | |
38 | push @_, $arg; | |
34 | die @_ if ref $_[0] || !$_[-1]; | |
35 | if ($_[-1] =~ /^(.*)( at .+ line .+\n$)/s) { | |
36 | pop @_; | |
37 | push @_, $1; | |
39 | 38 | } |
40 | 39 | die Carp::shortmess @_; |
41 | 40 | } |