Codebase list libcryptx-perl / ce15e24
strip trailing spaces Karel Miko 10 years ago
83 changed file(s) with 434 addition(s) and 434 deletion(s). Raw diff Collapse all Expand all
5252 #### send ($ciphertext, $tag, $nonce, $header) to other party
5353
5454 my $plaintext = ccm_decrypt_verify('AES', $key, $nonce, $header, $ciphertext, $tag);
55
55
5656 =head1 DESCRIPTION
5757
5858 CCM is a encrypt+authenticate mode that is centered around using AES (or any 16-byte cipher) as aprimitive.
7171 =head2 ccm_encrypt_authenticate
7272
7373 my ($ciphertext, $tag) = ccm_encrypt_authenticate($cipher, $key, $nonce, $header, $tag_len, $plaintext);
74
74
7575 # $cipher .. 'AES' or name of any other cipher with 16-byte block len
7676 # $key ..... AES key of proper length (128/192/256bits)
7777 # $nonce ... unique nonce/salt (no need to keep it secret)
8080 =head2 ccm_decrypt_verify
8181
8282 my $plaintext = ccm_decrypt_verify($cipher, $key, $nonce, $header, $ciphertext, $tag);
83
83
8484 # on error returns undef
8585
8686 =head1 SEE ALSO
2929 my $nonce = shift;
3030 my $header = shift;
3131 my $plaintext = shift;
32
32
3333 my $m = Crypt::AuthEnc::EAX->new($cipher_name, $key, $nonce);
3434 $m->header_add($header) if defined $header;
3535 my $ct = $m->encrypt_add($plaintext);
6464
6565 ### example 1
6666 use Crypt::AuthEnc::EAX;
67
67
6868 # encrypt + authenticate
6969 my $ae = Crypt::AuthEnc::EAX->new("AES", $key, $nonce);
7070 $ae->header_add('headerdata part1');
8989
9090 ### example 2
9191 use Crypt::AuthEnc::EAX qw(eax_encrypt_authenticate eax_decrypt_verify);
92
92
9393 my ($ciphertext, $tag) = eax_encrypt_authenticate('AES', $key, $nonce, $header, $plaintext);
9494 my $plaintext = eax_decrypt_verify('AES', $key, $nonce, $header, $ciphertext, $tag);
9595
9696 =head1 DESCRIPTION
9797
98 EAX is a mode that requires a cipher, CTR and OMAC support and provides encryption and authentication.
98 EAX is a mode that requires a cipher, CTR and OMAC support and provides encryption and authentication.
9999 It is initialized with a random nonce that can be shared publicly, a header which can be fixed and public,
100100 and a random secret symmetric key.
101101
112112 =head2 eax_encrypt_authenticate
113113
114114 my ($ciphertext, $tag) = eax_encrypt_authenticate($cipher, $key, $nonce, $header, $plaintext);
115
115
116116 # $cipher .. 'AES' or name of any other cipher with 16-byte block len
117117 # $key ..... AES key of proper length (128/192/256bits)
118118 # $nonce ... unique nonce/salt (no need to keep it secret)
121121 =head2 eax_decrypt_verify
122122
123123 my $plaintext = eax_decrypt_verify($cipher, $key, $nonce, $header, $ciphertext, $tag);
124
124
125125 # on error returns undef
126126
127127 =head1 METHODS
131131 my $ae = Crypt::AuthEnc::EAX->new($cipher, $key, $nonce);
132132 #or
133133 my $ae = Crypt::AuthEnc::EAX->new($cipher, $key, $nonce, $header);
134
134
135135 # $cipher .. 'AES' or name of any other cipher with 16-byte block len
136136 # $key ..... AES key of proper length (128/192/256bits)
137137 # $nonce ... unique nonce/salt (no need to keep it secret)
1919 my $iv = shift;
2020 my $adata = shift;
2121 my $plaintext = shift;
22
22
2323 my $m = Crypt::AuthEnc::GCM->new($cipher_name, $key);
2424 $m->iv_add($iv);
2525 $m->adata_add(defined $adata ? $adata : ''); #XXX-TODO if no aad we have to pass empty string
5454 =head1 SYNOPSIS
5555
5656 ### OO interface
57
57
5858 # encrypt and authenticate
5959 my $ae = Crypt::AuthEnc::GCM->new("AES", $key);
6060 $ae->iv_add('data_iv1');
7979
8080 ### functional interface
8181 use Crypt::AuthEnc::GCM qw(gcm_encrypt_authenticate gcm_decrypt_verify);
82
82
8383 my ($ciphertext, $tag) = gcm_encrypt_authenticate('AES', $key, $iv, $adata, $plaintext);
8484 my $plaintext = gcm_decrypt_verify('AES', $key, $iv, $adata, $ciphertext, $tag);
85
85
8686 =head1 DESCRIPTION
8787
88 Galois/Counter Mode (GCM) - provides encryption and authentication.
88 Galois/Counter Mode (GCM) - provides encryption and authentication.
8989
9090 =head1 EXPORT
9191
100100 =head2 gcm_encrypt_authenticate
101101
102102 my ($ciphertext, $tag) = gcm_encrypt_authenticate($cipher, $key, $iv, $adata, $plaintext);
103
103
104104 # $cipher .. 'AES' or name of any other cipher with 16-byte block len
105105 # $key ..... AES key of proper length (128/192/256bits)
106106 # $iv ...... initial vector
109109 =head2 gcm_decrypt_verify
110110
111111 my $plaintext = gcm_decrypt_verify($cipher, $key, $iv, $adata, $ciphertext, $tag);
112
112
113113 # on error returns undef
114114
115115 =head1 METHODS
117117 =head2 new
118118
119119 my $ae = Crypt::AuthEnc::GCM->new($cipher, $key);
120
120
121121 # $cipher .. 'AES' or name of any other cipher
122122 # $key ..... encryption key of proper length
123123
1919 my $nonce = shift;
2020 my $aad = shift;
2121 my $plaintext = shift;
22
22
2323 my $m = Crypt::AuthEnc::OCB->new($cipher_name, $key, $nonce);
2424 $m->adata_add($aad) if defined $aad;
2525 my $ct = $m->encrypt_last($plaintext);
5353
5454 ### OO interface
5555 use Crypt::AuthEnc::OCB;
56
56
5757 my $ae = Crypt::AuthEnc::OCB->new("AES", $key, $nonce);
5858 $ae->adata_add('aad1');
5959 $ae->adata_add('aad2');
7474
7575 ### functional interface
7676 use Crypt::AuthEnc::OCB qw(ocb_encrypt_authenticate ocb_decrypt_verify);
77
77
7878 my ($ciphertext, $tag) = ocb_encrypt_authenticate('AES', $key, $nonce, $aad, $plaintext);
7979 my $plaintext = ocb_decrypt_verify('AES', $key, $nonce, $aad, $ciphertext, $tag);
8080
9595 =head2 ocb_encrypt_authenticate
9696
9797 my ($ciphertext, $tag) = ocb_encrypt_authenticate($cipher, $key, $nonce, $aad, $plaintext);
98
98
9999 # $cipher .. 'AES' or name of any other cipher with 16-byte block len
100100 # $key ..... AES key of proper length (128/192/256bits)
101101 # $nonce ... unique nonce/salt (no need to keep it secret)
104104 =head2 ocb_decrypt_verify
105105
106106 my $plaintext = ocb_decrypt_verify($cipher, $key, $nonce, $aad, $ciphertext, $tag);
107
107
108108 # on error returns undef
109109
110110 =head1 METHODS
112112 =head2 new
113113
114114 my $ae = Crypt::AuthEnc::OCB->new($cipher, $key, $nonce);
115
115
116116 # $cipher .. 'AES' or name of any other cipher with 16-byte block len
117117 # $key ..... AES key of proper length (128/192/256bits)
118118 # $nonce ... unique nonce/salt (no need to keep it secret)
124124 =head2 encrypt_add
125125
126126 $ciphertext = $ae->encrypt_add($data); #can be called multiple times
127
127
128128 #BEWARE: size of $data has to be multiple of blocklen (16 for AES)
129129
130130 =head2 encrypt_last
138138 =head2 decrypt_add
139139
140140 $plaintext = $ae->decrypt_add($ciphertext); #can be called multiple times
141
141
142142 #BEWARE: size of $ciphertext has to be multiple of blocklen (16 for AES)
143143
144144 =head2 encrypt_last
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('AES');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::AES;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::AES', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('Anubis');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::Anubis;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::Anubis', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('Blowfish');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::Blowfish;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::Blowfish', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('CAST5');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::CAST5;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::CAST5', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('Camellia');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::Camellia;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::Camellia', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('DES');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::DES;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::DES', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('DES_EDE');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::DES_EDE;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::DES_EDE', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('KASUMI');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::KASUMI;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::KASUMI', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('Khazad');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::Khazad;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::Khazad', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('MULTI2');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::MULTI2;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::MULTI2', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('Noekeon');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::Noekeon;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::Noekeon', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('RC2');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::RC2;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::RC2', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('RC5');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::RC5;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::RC5', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('RC6');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::RC6;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::RC6', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('SAFERP');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::SAFERP;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::SAFERP', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('SAFER_K128');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::SAFER_K128;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::SAFER_K128', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('SAFER_K64');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::SAFER_K64;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::SAFER_K64', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('SAFER_SK128');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::SAFER_SK128;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::SAFER_SK128', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('SAFER_SK64');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::SAFER_SK64;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::SAFER_SK64', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('SEED');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::SEED;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::SEED', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('Skipjack');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::Skipjack;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::Skipjack', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('Twofish');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::Twofish;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::Twofish', -key=>$key, -iv=>$iv );
2525
2626 ### example 1
2727 use Crypt::Mode::CBC;
28
28
2929 my $key = '...'; # length has to be valid key size for this cipher
3030 my $iv = '...'; # 16 bytes
3131 my $cbc = Crypt::Mode::CBC->new('XTEA');
3434 ### example 2
3535 use Crypt::CBC;
3636 use Crypt::Cipher::XTEA;
37
37
3838 my $key = '...'; # length has to be valid key size for this cipher
3939 my $iv = '...'; # 16 bytes
4040 my $cbc = Crypt::CBC->new( -cipher=>'Cipher::XTEA', -key=>$key, -iv=>$iv );
1414 # - encrypt
1515 # - decrypt
1616 #functions, not methods:
17 # - _block_length_by_name
17 # - _block_length_by_name
1818 # - _min_key_length_by_name
1919 # - _max_key_length_by_name
2020 # - _default_rounds_by_name
2525 DES_EDE => '3des',
2626 SAFERP => 'safer+',
2727 SAFER_K128 => 'safer-k128',
28 SAFER_K64 => 'safer-k64',
28 SAFER_K64 => 'safer-k64',
2929 SAFER_SK128 => 'safer-sk128',
3030 SAFER_SK64 => 'safer-sk64',
3131 );
4242 return _new($cipher_name, @_);
4343 }
4444
45 sub blocksize {
45 sub blocksize {
4646 my $self = shift;
4747 return $self->_blocksize if ref($self);
4848 $self = _trans_cipher_name(shift) if $self eq __PACKAGE__;
4949 return _block_length_by_name(_trans_cipher_name($self));
5050 }
51
52 sub keysize {
51
52 sub keysize {
5353 max_keysize(@_);
5454 }
5555
9292
9393 #### example 1
9494 use Crypt::Cipher;
95
95
9696 $key = 'K' x 32; # 256 bits
9797 $c = Crypt::Cipher->new('AES', $key);
9898 $blocksize = $c->blocksize;
9999 $ciphertext = $c->encrypt('plain text block'); #encrypt 1 block
100100 $plaintext = $c->decrypt($ciphertext); #decrypt 1 block
101
101
102102 #### example 2 (compatibility with Crypt::CBC)
103103 use Crypt::CBC;
104104 use Crypt::Cipher;
105
105
106106 my $key = '...'; # length has to be valid key size for this cipher
107107 my $cipher = Crypt::Cipher('AES', $key);
108108 my $cbc = Crypt::CBC->new( -cipher=>$cipher );
109109 my $ciphertext = $cbc->encrypt("secret data");
110
110
111111 =head1 DESCRIPTION
112112
113113 Provides an interface to various symetric cipher algorithms.
114114
115 B<BEWARE:> This module implements just elementary one-block (usually 8 or 16 byte) encryption/encryption
115 B<BEWARE:> This module implements just elementary one-block (usually 8 or 16 byte) encryption/encryption
116116 operation - if you want to encrypt/decrypt generic data of "any length" you have to use some of the cipher
117117 block modes - check for example L<Crypt::Mode::CBC|Crypt::Mode::CBC>.
118118
124124
125125 ## basic scenario
126126 $d = Crypt::Cipher->new($name, $key);
127 # $name = one of 'AES', 'Anubis', 'Blowfish', 'CAST5', 'Camellia', 'DES', 'DES_EDE',
128 # 'KASUMI', 'Khazad', 'MULTI2', 'Noekeon', 'RC2', 'RC5', 'RC6',
127 # $name = one of 'AES', 'Anubis', 'Blowfish', 'CAST5', 'Camellia', 'DES', 'DES_EDE',
128 # 'KASUMI', 'Khazad', 'MULTI2', 'Noekeon', 'RC2', 'RC5', 'RC6',
129129 # 'SAFERP', 'SAFER_K128', 'SAFER_K64', 'SAFER_SK128', 'SAFER_SK64',
130130 # 'SEED', 'Skipjack', 'Twofish', 'XTEA'
131131 # simply any <CNAME> for which there exists Crypt::Cipher::<NAME>
132132 # $key = binary key (keysize should comply with selected cipher requirements)
133
133
134134 ## some of the ciphers (e.g. MULTI2, RC5, SAFER) allows to set number of rounds
135135 $d = Crypt::Cipher->new('MULTI2', $key, $rounds);
136136 # $rounds = positive integer (should comply with selected cipher requirements)
190190 Crypt::Cipher->default_rounds('AES');
191191 #or
192192 Crypt::Cipher::default_rounds('AES');
193
193
194194 =head1 SEE ALSO
195195
196196 =over 4
3737 =head1 SYNOPSIS
3838
3939 ### Functional interface:
40 use Crypt::Digest::CHAES qw( chaes chaes_hex chaes_b64 chaes_b64u
40 use Crypt::Digest::CHAES qw( chaes chaes_hex chaes_b64 chaes_b64u
4141 chaes_file chaes_file_hex chaes_file_b64 chaes_file_b64u );
4242
4343 # calculate digest from string/buffer
3737 =head1 SYNOPSIS
3838
3939 ### Functional interface:
40 use Crypt::Digest::MD2 qw( md2 md2_hex md2_b64 md2_b64u
40 use Crypt::Digest::MD2 qw( md2 md2_hex md2_b64 md2_b64u
4141 md2_file md2_file_hex md2_file_b64 md2_file_b64u );
4242
4343 # calculate digest from string/buffer
3737 =head1 SYNOPSIS
3838
3939 ### Functional interface:
40 use Crypt::Digest::MD4 qw( md4 md4_hex md4_b64 md4_b64u
40 use Crypt::Digest::MD4 qw( md4 md4_hex md4_b64 md4_b64u
4141 md4_file md4_file_hex md4_file_b64 md4_file_b64u );
4242
4343 # calculate digest from string/buffer
3737 =head1 SYNOPSIS
3838
3939 ### Functional interface:
40 use Crypt::Digest::MD5 qw( md5 md5_hex md5_b64 md5_b64u
40 use Crypt::Digest::MD5 qw( md5 md5_hex md5_b64 md5_b64u
4141 md5_file md5_file_hex md5_file_b64 md5_file_b64u );
4242
4343 # calculate digest from string/buffer
3737 =head1 SYNOPSIS
3838
3939 ### Functional interface:
40 use Crypt::Digest::RIPEMD128 qw( ripemd128 ripemd128_hex ripemd128_b64 ripemd128_b64u
40 use Crypt::Digest::RIPEMD128 qw( ripemd128 ripemd128_hex ripemd128_b64 ripemd128_b64u
4141 ripemd128_file ripemd128_file_hex ripemd128_file_b64 ripemd128_file_b64u );
4242
4343 # calculate digest from string/buffer
3737 =head1 SYNOPSIS
3838
3939 ### Functional interface:
40 use Crypt::Digest::RIPEMD160 qw( ripemd160 ripemd160_hex ripemd160_b64 ripemd160_b64u
40 use Crypt::Digest::RIPEMD160 qw( ripemd160 ripemd160_hex ripemd160_b64 ripemd160_b64u
4141 ripemd160_file ripemd160_file_hex ripemd160_file_b64 ripemd160_file_b64u );
4242
4343 # calculate digest from string/buffer
3737 =head1 SYNOPSIS
3838
3939 ### Functional interface:
40 use Crypt::Digest::RIPEMD256 qw( ripemd256 ripemd256_hex ripemd256_b64 ripemd256_b64u
40 use Crypt::Digest::RIPEMD256 qw( ripemd256 ripemd256_hex ripemd256_b64 ripemd256_b64u
4141 ripemd256_file ripemd256_file_hex ripemd256_file_b64 ripemd256_file_b64u );
4242
4343 # calculate digest from string/buffer
3737 =head1 SYNOPSIS
3838
3939 ### Functional interface:
40 use Crypt::Digest::RIPEMD320 qw( ripemd320 ripemd320_hex ripemd320_b64 ripemd320_b64u
40 use Crypt::Digest::RIPEMD320 qw( ripemd320 ripemd320_hex ripemd320_b64 ripemd320_b64u
4141 ripemd320_file ripemd320_file_hex ripemd320_file_b64 ripemd320_file_b64u );
4242
4343 # calculate digest from string/buffer
3737 =head1 SYNOPSIS
3838
3939 ### Functional interface:
40 use Crypt::Digest::SHA1 qw( sha1 sha1_hex sha1_b64 sha1_b64u
40 use Crypt::Digest::SHA1 qw( sha1 sha1_hex sha1_b64 sha1_b64u
4141 sha1_file sha1_file_hex sha1_file_b64 sha1_file_b64u );
4242
4343 # calculate digest from string/buffer
3737 =head1 SYNOPSIS
3838
3939 ### Functional interface:
40 use Crypt::Digest::SHA224 qw( sha224 sha224_hex sha224_b64 sha224_b64u
40 use Crypt::Digest::SHA224 qw( sha224 sha224_hex sha224_b64 sha224_b64u
4141 sha224_file sha224_file_hex sha224_file_b64 sha224_file_b64u );
4242
4343 # calculate digest from string/buffer
3737 =head1 SYNOPSIS
3838
3939 ### Functional interface:
40 use Crypt::Digest::SHA256 qw( sha256 sha256_hex sha256_b64 sha256_b64u
40 use Crypt::Digest::SHA256 qw( sha256 sha256_hex sha256_b64 sha256_b64u
4141 sha256_file sha256_file_hex sha256_file_b64 sha256_file_b64u );
4242
4343 # calculate digest from string/buffer
3737 =head1 SYNOPSIS
3838
3939 ### Functional interface:
40 use Crypt::Digest::SHA384 qw( sha384 sha384_hex sha384_b64 sha384_b64u
40 use Crypt::Digest::SHA384 qw( sha384 sha384_hex sha384_b64 sha384_b64u
4141 sha384_file sha384_file_hex sha384_file_b64 sha384_file_b64u );
4242
4343 # calculate digest from string/buffer
3737 =head1 SYNOPSIS
3838
3939 ### Functional interface:
40 use Crypt::Digest::SHA512 qw( sha512 sha512_hex sha512_b64 sha512_b64u
40 use Crypt::Digest::SHA512 qw( sha512 sha512_hex sha512_b64 sha512_b64u
4141 sha512_file sha512_file_hex sha512_file_b64 sha512_file_b64u );
4242
4343 # calculate digest from string/buffer
3737 =head1 SYNOPSIS
3838
3939 ### Functional interface:
40 use Crypt::Digest::Tiger192 qw( tiger192 tiger192_hex tiger192_b64 tiger192_b64u
40 use Crypt::Digest::Tiger192 qw( tiger192 tiger192_hex tiger192_b64 tiger192_b64u
4141 tiger192_file tiger192_file_hex tiger192_file_b64 tiger192_file_b64u );
4242
4343 # calculate digest from string/buffer
3737 =head1 SYNOPSIS
3838
3939 ### Functional interface:
40 use Crypt::Digest::Whirlpool qw( whirlpool whirlpool_hex whirlpool_b64 whirlpool_b64u
40 use Crypt::Digest::Whirlpool qw( whirlpool whirlpool_hex whirlpool_b64 whirlpool_b64u
4141 whirlpool_file whirlpool_file_hex whirlpool_file_b64 whirlpool_file_b64u );
4242
4343 # calculate digest from string/buffer
5656 }
5757 else {
5858 my $pkg = shift;
59 unshift @_, ($pkg eq 'Crypt::Digest' ? _trans_digest_name(shift) : _trans_digest_name($pkg));
59 unshift @_, ($pkg eq 'Crypt::Digest' ? _trans_digest_name(shift) : _trans_digest_name($pkg));
6060 ###return _hashsize_by_name(@_);
6161 goto \&_hashsize_by_name; # keep the real caller for croak()
6262 }
6464
6565 sub addfile {
6666 my ($self, $file) = @_;
67
67
6868 my $handle;
6969 if (ref(\$file) eq 'SCALAR') { #filename
7070 open($handle, "<", $file) || croak "FATAL: cannot open '$file': $!";
7171 binmode($handle);
7272 }
73 else { #handle
73 else { #handle
7474 $handle = $file
75 }
75 }
7676 croak "FATAL: invalid handle" unless defined $handle;
77
77
7878 my $n;
7979 my $buf = "";
8080 while (($n = read($handle, $buf, 32*1024))) {
8181 $self->add($buf)
8282 }
8383 croak "FATAL: read failed: $!" unless defined $n;
84
84
8585 return $self;
8686 }
8787
119119 ### Functional interface:
120120 use Crypt::Digest qw( digest_data digest_data_hex digest_data_b64 digest_data_b64u
121121 digest_file digest_file_hex digest_file_b64 digest_file_b64u );
122
122
123123 # calculate digest from string/buffer
124124 $digest_raw = digest_data('SHA1', 'data string');
125125 $digest_hex = digest_data_hex('SHA1', 'data string');
126 $digest_b64 = digest_data_b64('SHA1', 'data string');
127 $digest_b64u = digest_data_b64u('SHA1', 'data string');
126 $digest_b64 = digest_data_b64('SHA1', 'data string');
127 $digest_b64u = digest_data_b64u('SHA1', 'data string');
128128 # calculate digest from file
129129 $digest_raw = digest_file('SHA1', 'filename.dat');
130130 $digest_hex = digest_file_hex('SHA1', 'filename.dat');
133133 # calculate digest from filehandle
134134 $digest_raw = digest_file('SHA1', *FILEHANDLE);
135135 $digest_hex = digest_file_hex('SHA1', *FILEHANDLE);
136 $digest_b64 = digest_file_b64('SHA1', *FILEHANDLE);
137 $digest_b64u = digest_file_b64u('SHA1', *FILEHANDLE);
136 $digest_b64 = digest_file_b64('SHA1', *FILEHANDLE);
137 $digest_b64u = digest_file_b64u('SHA1', *FILEHANDLE);
138138
139139 ### OO interface:
140140 use Crypt::Digest;
141
141
142142 $d = Crypt::Digest->new('SHA1');
143143 $d->add('any data');
144144 $d->addfile('filename.dat');
147147 $result_hex = $d->hexdigest; # hexadecimal form
148148 $result_b64 = $d->b64digest; # Base64 form
149149 $result_b64u = $d->b64udigest; # Base64 URL Safe form
150
150
151151 =head1 DESCRIPTION
152152
153153 Provides an interface to various hash/digest algorithms.
160160
161161 use Crypt::Digest qw( digest_data digest_data_hex digest_data_b64 digest_data_b64u
162162 digest_file digest_file_hex digest_file_b64 digest_file_b64u );
163
163
164164 Or all of them at once:
165165
166166 use Crypt::Digest ':all';
167
167
168168 =head1 FUNCTIONS
169169
170170 Please note that all functions take as its first argument the algoritm name, supported values are:
171171
172 'CHAES', 'MD2', 'MD4', 'MD5', 'RIPEMD128', 'RIPEMD160',
172 'CHAES', 'MD2', 'MD4', 'MD5', 'RIPEMD128', 'RIPEMD160',
173173 'RIPEMD256', 'RIPEMD320', 'SHA1', 'SHA224', 'SHA256',
174174 'SHA384', 'SHA512', 'Tiger192', 'Whirlpool'
175
175
176176 (simply any <FUNCNAME> for which there is Crypt::Digest::<FUNCNAME> module)
177177
178178 =head2 digest_data
180180 Logically joins all arguments into a single string, and returns its SHA1 digest encoded as a binary string.
181181
182182 $digest_raw = digest_data('SHA1', 'data string');
183 #or
183 #or
184184 $digest_raw = digest_data('SHA1', 'any data', 'more data', 'even more data');
185185
186186 =head2 digest_data_hex
188188 Logically joins all arguments into a single string, and returns its SHA1 digest encoded as a hexadecimal string.
189189
190190 $digest_hex = digest_data_hex('SHA1', 'data string');
191 #or
191 #or
192192 $digest_hex = digest_data_hex('SHA1', 'any data', 'more data', 'even more data');
193193
194194 =head2 digest_data_b64
196196 Logically joins all arguments into a single string, and returns its SHA1 digest encoded as a Base64 string, B<with> trailing '=' padding.
197197
198198 $digest_b64 = digest_data_b64('SHA1', 'data string');
199 #or
199 #or
200200 $digest_b64 = digest_data_b64('SHA1', 'any data', 'more data', 'even more data');
201201
202202 =head2 digest_data_b64u
204204 Logically joins all arguments into a single string, and returns its SHA1 digest encoded as a Base64 URL Safe string (see RFC 4648 section 5).
205205
206206 $digest_b64url = digest_data_b64u('SHA1', 'data string');
207 #or
207 #or
208208 $digest_b64url = digest_data_b64u('SHA1', 'any data', 'more data', 'even more data');
209209
210210 =head2 digest_file
233233 #or
234234 $digest_b64 = digest_file_b64('SHA1', *FILEHANDLE);
235235
236 =head2 digest_file_b64u
236 =head2 digest_file_b64u
237237
238238 Reads file (defined by filename or filehandle) content, and returns its digest encoded as a Base64 URL Safe string (see RFC 4648 section 5).
239239
248248 Constructor, returns a reference to the digest object.
249249
250250 $d = Crypt::Digest->new($name);
251 # $name could be: 'CHAES', 'MD2', 'MD4', 'MD5', 'RIPEMD128', 'RIPEMD160',
252 # 'RIPEMD256', 'RIPEMD320', 'SHA1', 'SHA224', 'SHA256',
251 # $name could be: 'CHAES', 'MD2', 'MD4', 'MD5', 'RIPEMD128', 'RIPEMD160',
252 # 'RIPEMD256', 'RIPEMD320', 'SHA1', 'SHA224', 'SHA256',
253253 # 'SHA384', 'SHA512', 'Tiger192', 'Whirlpool'
254254 #
255255 # simply any <FUNCNAME> for which there is Crypt::Digest::<FUNCNAME> module
274274 $d->add('any data');
275275 #or
276276 $d->add('any data', 'more data', 'even more data');
277
277
278278 Note that all the following cases are equivalent:
279279
280280 # case 1
281281 $d->add('aa', 'bb', 'cc');
282
282
283283 # case 2
284284 $d->add('aa');
285285 $d->add('bb');
7171
7272 ### HKDF & co.
7373 $derived_key3 = hkdf($keying_material, $salt, $hash_name, $len, $info);
74 $prk = hkdf_extract($keying_material, $salt, $hash_name);
74 $prk = hkdf_extract($keying_material, $salt, $hash_name);
7575 $okm1 = hkdf_expand($prk, $hash_name, $len, $info);
7676
7777 =head1 DESCRIPTION
9999 $derived_key = pbkdf1($password, $salt, $iteration_count);
100100 #or
101101 $derived_key = pbkdf1($password, $salt);
102
102
103103 # $password ......... input keying material (password)
104104 # $salt ............. salt/nonce (expected length: 8)
105105 # $iteration_count .. optional, DEFAULT: 5000
115115 $derived_key = pbkdf2($password, $salt, $iteration_count);
116116 #or
117117 $derived_key = pbkdf2($password, $salt);
118
118
119119 # $password ......... input keying material (password)
120120 # $salt ............. salt/nonce
121121 # $iteration_count .. optional, DEFAULT: 5000
143143 $prk = hkdf_extract($password, $salt, $hash_name);
144144 #or
145145 $prk = hkdf_extract($password, $salt, $hash_name);
146
146
147147 # $password ... input keying material (password)
148148 # $salt ....... salt/nonce, if undef defaults to HashLen zero octets
149149 # $hash_name .. optional, DEFAULT: 'SHA256'
1515
1616 sub addfile {
1717 my ($self, $file) = @_;
18
18
1919 my $handle;
2020 if (ref(\$file) eq 'SCALAR') {
2121 #filename
2525 else {
2626 #handle
2727 $handle = $file
28 }
28 }
2929 die "FATAL: invalid handle" unless defined $handle;
30
30
3131 my $n;
3232 my $buf = "";
3333 while (($n = read($handle, $buf, 32*1024))) {
3434 $self->_add_single($buf)
3535 }
3636 die "FATAL: read failed: $!" unless defined $n;
37
37
3838 return $self;
3939 }
4040
5656 # 1 PKCS5 padding, Crypt::CBC's "standard" - DEFAULT
5757 # 2 Crypt::CBC's "oneandzeroes"
5858 # $cipher_rounds ... optional num of rounds for given cipher
59
59
6060 =head2 encrypt
6161
6262 my $ciphertext = $m->encrypt($plaintext, $key, $iv);
4949 my $m = Crypt::Mode::CFB->new('AES', $cipher_rounds);
5050
5151 # $cipher_rounds ... optional num of rounds for given cipher
52
52
5353 =head2 encrypt
5454
5555 my $ciphertext = $m->encrypt($plaintext, $key, $iv);
4949 my $m = Crypt::Mode::CTR->new($cipher_name, $ctr_mode, $ctr_width);
5050 #or
5151 my $m = Crypt::Mode::CTR->new($cipher_name, $ctr_mode, $ctr_width, $cipher_rounds);
52
52
5353 # $ctr_mode .... 0 little-endian counter (DEFAULT)
5454 # 1 big-endian counter
5555 # 2 little-endian + RFC3686 incrementing
5656 # 3 big-endian + RFC3686 incrementing
5757 # $ctr_width ... counter width in bytes (DEFAULT = full block width)
5858 # $cipher_rounds ... optional num of rounds for given cipher
59
59
6060 =head2 encrypt
6161
6262 my $ciphertext = $m->encrypt($plaintext, $key, $iv);
5757 # 1 PKCS5 padding, Crypt::CBC's "standard" - DEFAULT
5858 # 2 Crypt::CBC's "oneandzeroes"
5959 # $cipher_rounds ... optional num of rounds for given cipher
60
60
6161 =head2 encrypt
6262
6363 my $ciphertext = $m->encrypt($plaintext, $key);
4949 my $m = Crypt::Mode::OFB->new('AES', $cipher_rounds);
5050
5151 # $cipher_rounds ... optional num of rounds for given cipher
52
52
5353 =head2 encrypt
5454
5555 my $ciphertext = $m->encrypt($plaintext, $key, $iv);
3030 return $self;
3131 }
3232
33 sub finish {
33 sub finish {
3434 shift->_finish(@_);
3535 }
3636
1212 use Carp;
1313 use MIME::Base64 qw(encode_base64 decode_base64);
1414
15 sub new {
15 sub new {
1616 my ($class, $f) = @_;
1717 my $self = _new();
1818 $self->import_key($f) if $f;
4545
4646 sub encrypt {
4747 my ($self, $data, $hash_name) = @_;
48 $hash_name = Crypt::Digest::_trans_digest_name($hash_name||'SHA1');
48 $hash_name = Crypt::Digest::_trans_digest_name($hash_name||'SHA1');
4949 return $self->_encrypt($data, $hash_name);
5050 }
5151
5252 sub decrypt {
53 my ($self, $data) = @_;
53 my ($self, $data) = @_;
5454 return $self->_decrypt($data);
5555 }
5656
7474 }
7575
7676 sub verify_hash {
77 my ($self, $sig, $data_hash) = @_;
77 my ($self, $sig, $data_hash) = @_;
7878 return $self->_verify($sig, $data_hash);
7979 }
8080
9090 sub dh_decrypt {
9191 my $key = shift;
9292 $key = __PACKAGE__->new($key) unless ref $key;
93 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
93 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
9494 return $key->decrypt(@_);
9595 }
9696
9797 sub dh_sign_message {
9898 my $key = shift;
9999 $key = __PACKAGE__->new($key) unless ref $key;
100 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
100 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
101101 return $key->sign_message(@_);
102102 }
103103
104104 sub dh_verify_message {
105105 my $key = shift;
106106 $key = __PACKAGE__->new($key) unless ref $key;
107 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
107 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
108108 return $key->verify_message(@_);
109109 }
110110
111111 sub dh_sign_hash {
112112 my $key = shift;
113113 $key = __PACKAGE__->new($key) unless ref $key;
114 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
114 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
115115 return $key->sign_hash(@_);
116116 }
117117
118118 sub dh_verify_hash {
119119 my $key = shift;
120120 $key = __PACKAGE__->new($key) unless ref $key;
121 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
121 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
122122 return $key->verify_hash(@_);
123123 }
124124
154154 =head1 SYNOPSIS
155155
156156 ### OO interface
157
157
158158 #Encryption: Alice
159 my $pub = Crypt::PK::DH->new('Bob_pub_dh1.key');
159 my $pub = Crypt::PK::DH->new('Bob_pub_dh1.key');
160160 my $ct = $pub->encrypt("secret message");
161161 #
162162 #Encryption: Bob (received ciphertext $ct)
163163 my $priv = Crypt::PK::DH->new('Bob_priv_dh1.key');
164164 my $pt = $priv->decrypt($ct);
165
165
166166 #Signature: Alice
167167 my $priv = Crypt::PK::DH->new('Alice_priv_dh1.key');
168168 my $sig = $priv->sign_message($message);
170170 #Signature: Bob (received $message + $sig)
171171 my $pub = Crypt::PK::DH->new('Alice_pub_dh1.key');
172172 $pub->verify_message($sig, $message) or die "ERROR";
173
173
174174 #Shared secret
175175 my $priv = Crypt::PK::DH->new('Alice_priv_dh1.key');
176 my $pub = Crypt::PK::DH->new('Bob_pub_dh1.key');
176 my $pub = Crypt::PK::DH->new('Bob_pub_dh1.key');
177177 my $shared_secret = $priv->shared_secret($pub);
178178
179179 #Key generation
181181 $pk->generate_key(128);
182182 my $private = $pk->export_key('private');
183183 my $public = $pk->export_key('public');
184
184
185185 ### Functional interface
186
186
187187 #Encryption: Alice
188188 my $ct = dh_encrypt('Bob_pub_dh1.key', "secret message");
189189 #Encryption: Bob (received ciphertext $ct)
190190 my $pt = dh_decrypt('Bob_priv_dh1.key', $ct);
191
191
192192 #Signature: Alice
193193 my $sig = dh_sign_message('Alice_priv_dh1.key', $message);
194194 #Signature: Bob (received $message + $sig)
195195 dh_verify_message('Alice_pub_dh1.key', $sig, $message) or die "ERROR";
196
196
197197 #Shared secret
198198 my $shared_secret = dh_shared_secret('Alice_priv_dh1.key', 'Bob_pub_dh1.key');
199199
208208 my $ct = dh_encrypt(\$buffer_containing_pub_key, $message);
209209 #or
210210 my $ct = dh_encrypt($pub_key_filename, $message, $hash_name);
211
211
212212 #NOTE: $hash_name can be 'SHA1' (DEFAULT), 'SHA256' or any other hash supported by Crypt::Digest
213
213
214214 Encryption works similar to the L<Crypt::PK::ECC> encryption whereas shared DH key is computed, and
215215 the hash of the shared key XOR'ed against the plaintext forms the ciphertext.
216216
264264
265265 #on Alice side
266266 my $shared_secret = dh_shared_secret('Alice_priv_dh1.key', 'Bob_pub_dh1.key');
267
267
268268 #on Bob side
269269 my $shared_secret = dh_shared_secret('Bob_priv_dh1.key', 'Alice_pub_dh1.key');
270270
315315 my $ct = $pk->encrypt($message);
316316 #or
317317 my $ct = $pk->encrypt($message, $hash_name);
318
318
319319 #NOTE: $hash_name can be 'SHA1' (DEFAULT), 'SHA256' or any other hash supported by Crypt::Digest
320320
321321 =head2 decrypt
378378 =head2 key2hash
379379
380380 my $hash = $pk->key2hash;
381
381
382382 # returns hash like this (or undef if no key loaded):
383383 {
384384 type => 0, # integer: 1 .. private, 0 .. public
220220 my $ct = dsa_encrypt(\$buffer_containing_pub_key, $message);
221221 #or
222222 my $ct = dsa_encrypt($pub_key_filename, $message, $hash_name);
223
223
224224 #NOTE: $hash_name can be 'SHA1' (DEFAULT), 'SHA256' or any other hash supported by Crypt::Digest
225225
226226 Encryption works similar to the L<Crypt::PK::ECC> encryption whereas shared DSA key is computed, and
328328 my $ct = $pk->encrypt($message);
329329 #or
330330 my $ct = $pk->encrypt($message, $hash_name);
331
331
332332 #NOTE: $hash_name can be 'SHA1' (DEFAULT), 'SHA256' or any other hash supported by Crypt::Digest
333333
334334 =head2 decrypt
379379 =head2 key2hash
380380
381381 my $hash = $pk->key2hash;
382
382
383383 # returns hash like this (or undef if no key loaded):
384384 {
385385 type => 1, # integer: 1 .. private, 0 .. public
386386 size => 256, # integer: key size in bytes
387 # all the rest are hex strings
387 # all the rest are hex strings
388388 p => "AAF839A764E04D80824B79FA1F0496C093...", #prime modulus
389389 q => "D05C4CB45F29D353442F1FEC43A6BE2BE8...", #prime divisor
390390 g => "847E8896D12C9BF18FE283AE7AD58ED7F3...", #generator of a subgroup of order q in GF(p)
1212 use Carp;
1313 use MIME::Base64 qw(encode_base64 decode_base64);
1414
15 sub new {
15 sub new {
1616 my ($class, $f) = @_;
1717 my $self = _new();
1818 $self->import_key($f) if $f;
4949
5050 sub encrypt {
5151 my ($self, $data, $hash_name) = @_;
52 $hash_name = Crypt::Digest::_trans_digest_name($hash_name||'SHA1');
52 $hash_name = Crypt::Digest::_trans_digest_name($hash_name||'SHA1');
5353 return $self->_encrypt($data, $hash_name);
5454 }
5555
5656 sub decrypt {
57 my ($self, $data) = @_;
57 my ($self, $data) = @_;
5858 return $self->_decrypt($data);
5959 }
6060
7878 }
7979
8080 sub verify_hash {
81 my ($self, $sig, $data_hash) = @_;
81 my ($self, $sig, $data_hash) = @_;
8282 return $self->_verify($sig, $data_hash);
8383 }
8484
9494 sub ecc_decrypt {
9595 my $key = shift;
9696 $key = __PACKAGE__->new($key) unless ref $key;
97 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
97 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
9898 return $key->decrypt(@_);
9999 }
100100
101101 sub ecc_sign_message {
102102 my $key = shift;
103103 $key = __PACKAGE__->new($key) unless ref $key;
104 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
104 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
105105 return $key->sign_message(@_);
106106 }
107107
108108 sub ecc_verify_message {
109109 my $key = shift;
110110 $key = __PACKAGE__->new($key) unless ref $key;
111 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
111 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
112112 return $key->verify_message(@_);
113113 }
114114
115115 sub ecc_sign_hash {
116116 my $key = shift;
117117 $key = __PACKAGE__->new($key) unless ref $key;
118 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
118 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
119119 return $key->sign_hash(@_);
120120 }
121121
122122 sub ecc_verify_hash {
123123 my $key = shift;
124124 $key = __PACKAGE__->new($key) unless ref $key;
125 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
125 carp "FATAL: invalid 'key' param" unless ref($key) eq __PACKAGE__;
126126 return $key->verify_hash(@_);
127127 }
128128
158158 =head1 SYNOPSIS
159159
160160 ### OO interface
161
161
162162 #Encryption: Alice
163 my $pub = Crypt::PK::ECC->new('Bob_pub_ecc1.der');
163 my $pub = Crypt::PK::ECC->new('Bob_pub_ecc1.der');
164164 my $ct = $pub->encrypt("secret message");
165165 #
166166 #Encryption: Bob (received ciphertext $ct)
167167 my $priv = Crypt::PK::ECC->new('Bob_priv_ecc1.der');
168168 my $pt = $priv->decrypt($ct);
169
169
170170 #Signature: Alice
171171 my $priv = Crypt::PK::ECC->new('Alice_priv_ecc1.der');
172172 my $sig = $priv->sign_message($message);
174174 #Signature: Bob (received $message + $sig)
175175 my $pub = Crypt::PK::ECC->new('Alice_pub_ecc1.der');
176176 $pub->verify_message($sig, $message) or die "ERROR";
177
177
178178 #Shared secret
179179 my $priv = Crypt::PK::ECC->new('Alice_priv_ecc1.der');
180 my $pub = Crypt::PK::ECC->new('Bob_pub_ecc1.der');
180 my $pub = Crypt::PK::ECC->new('Bob_pub_ecc1.der');
181181 my $shared_secret = $priv->shared_secret($pub);
182182
183183 #Key generation
188188 my $public_ansi_x963 = $pk->export_key_x963();
189189
190190 ### Functional interface
191
191
192192 #Encryption: Alice
193193 my $ct = ecc_encrypt('Bob_pub_ecc1.der', "secret message");
194194 #Encryption: Bob (received ciphertext $ct)
195195 my $pt = ecc_decrypt('Bob_priv_ecc1.der', $ct);
196
196
197197 #Signature: Alice
198198 my $sig = ecc_sign_message('Alice_priv_ecc1.der', $message);
199199 #Signature: Bob (received $message + $sig)
200200 ecc_verify_message('Alice_pub_ecc1.der', $sig, $message) or die "ERROR";
201
201
202202 #Shared secret
203203 my $shared_secret = ecc_shared_secret('Alice_priv_ecc1.der', 'Bob_pub_ecc1.der');
204204
205205 =head1 DESCRIPTION
206206
207 The module provides a set of core ECC functions as well that are designed to be the Elliptic Curve analogy of
207 The module provides a set of core ECC functions as well that are designed to be the Elliptic Curve analogy of
208208 all of the Diffie-Hellman routines (ECDH).
209209
210210 =head1 FUNCTIONS
218218 my $ct = ecc_encrypt(\$buffer_containing_pub_key, $message);
219219 #or
220220 my $ct = ecc_encrypt($pub_key_filename, $message, $hash_name);
221
221
222222 #NOTE: $hash_name can be 'SHA1' (DEFAULT), 'SHA256' or any other hash supported by Crypt::Digest
223223
224224 ECCDH Encryption is performed by producing a random key, hashing it, and XOR'ing the digest against the plaintext.
273273
274274 #on Alice side
275275 my $shared_secret = ecc_shared_secret('Alice_priv_ecc1.der', 'Bob_pub_ecc1.der');
276
276
277277 #on Bob side
278278 my $shared_secret = ecc_shared_secret('Bob_priv_ecc1.der', 'Alice_pub_ecc1.der');
279279
302302 # 32 => use curve P-256 recommended by FIPS 186-3
303303 # 48 => use curve P-384 recommended by FIPS 186-3
304304 # 65 => use curve P-521 recommended by FIPS 186-3
305
305
306306 See L<http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf> and L<http://www.secg.org/collateral/sec2_final.pdf>
307307
308308 =head2 import_key
337337 my $ct = $pk->encrypt($message);
338338 #or
339339 my $ct = $pk->encrypt($message, $hash_name);
340
340
341341 #NOTE: $hash_name can be 'SHA1' (DEFAULT), 'SHA256' or any other hash supported by Crypt::Digest
342342
343343 =head2 decrypt
400400 =head2 key2hash
401401
402402 my $hash = $pk->key2hash;
403
403
404404 # returns hash like this (or undef if no key loaded):
405405 {
406406 type => 1, # integer: 1 .. private, 0 .. public
437437 =head2 key2hash
438438
439439 my $hash = $pk->key2hash;
440
440
441441 # returns hash like this (or undef if no key loaded):
442442 {
443443 type => 1, # integer: 1 .. private, 0 .. public
444444 size => 256, # integer: key size in bytes
445 # all the rest are hex strings
445 # all the rest are hex strings
446446 e => "10001", #public exponent
447447 d => "9ED5C3D3F866E06957CA0E9478A273C39BBDA4EEAC5B...", #private exponent
448448 N => "D0A5CCCAE03DF9C2F5C4C8C0CE840D62CDE279990DC6...", #modulus
1111 use base 'Crypt::PRNG';
1212
1313 {
14 ### stolen from Bytes::Random::Secure
14 ### stolen from Bytes::Random::Secure
1515 my $RNG_object = undef;
1616 my $fetch_RNG = sub { # Lazily, instantiate the RNG object, but only once.
1717 $RNG_object = Crypt::PRNG::Fortuna->new unless defined $RNG_object && ref($RNG_object) ne 'SCALAR';
1818 return $RNG_object;
19 };
19 };
2020 sub rand { return $fetch_RNG->()->double(@_) }
2121 sub irand { return $fetch_RNG->()->int32(@_) }
2222 sub random_bytes { return $fetch_RNG->()->bytes(@_) }
5858 $prng = Crypt::PRNG::Fortuna->new;
5959 #or
6060 $prng = Crypt::PRNG::Fortuna->new("some data used for seeding PRNG");
61
61
6262 $octets = $prng->bytes(45);
6363 $hex_string = $prng->bytes_hex(45);
6464 $base64_string = $prng->bytes_b64(45);
1111 use base 'Crypt::PRNG';
1212
1313 {
14 ### stolen from Bytes::Random::Secure
14 ### stolen from Bytes::Random::Secure
1515 my $RNG_object = undef;
1616 my $fetch_RNG = sub { # Lazily, instantiate the RNG object, but only once.
1717 $RNG_object = Crypt::PRNG::RC4->new unless defined $RNG_object && ref($RNG_object) ne 'SCALAR';
1818 return $RNG_object;
19 };
19 };
2020 sub rand { return $fetch_RNG->()->double(@_) }
2121 sub irand { return $fetch_RNG->()->int32(@_) }
2222 sub random_bytes { return $fetch_RNG->()->bytes(@_) }
5757 $prng = Crypt::PRNG::RC4->new;
5858 #or
5959 $prng = Crypt::PRNG::RC4->new("some data used for seeding PRNG");
60
60
6161 $octets = $prng->bytes(45);
6262 $hex_string = $prng->bytes_hex(45);
6363 $base64_string = $prng->bytes_b64(45);
1111 use base 'Crypt::PRNG';
1212
1313 {
14 ### stolen from Bytes::Random::Secure
14 ### stolen from Bytes::Random::Secure
1515 my $RNG_object = undef;
1616 my $fetch_RNG = sub { # Lazily, instantiate the RNG object, but only once.
1717 $RNG_object = Crypt::PRNG::Sober128->new unless defined $RNG_object && ref($RNG_object) ne 'SCALAR';
1818 return $RNG_object;
19 };
19 };
2020 sub rand { return $fetch_RNG->()->double(@_) }
2121 sub irand { return $fetch_RNG->()->int32(@_) }
2222 sub random_bytes { return $fetch_RNG->()->bytes(@_) }
5757 $prng = Crypt::PRNG::Sober128->new;
5858 #or
5959 $prng = Crypt::PRNG::Sober128->new("some data used for seeding PRNG");
60
60
6161 $octets = $prng->bytes(45);
6262 $hex_string = $prng->bytes_hex(45);
6363 $base64_string = $prng->bytes_b64(45);
1111 use base 'Crypt::PRNG';
1212
1313 {
14 ### stolen from Bytes::Random::Secure
14 ### stolen from Bytes::Random::Secure
1515 my $RNG_object = undef;
1616 my $fetch_RNG = sub { # Lazily, instantiate the RNG object, but only once.
1717 $RNG_object = Crypt::PRNG::Yarrow->new unless defined $RNG_object && ref($RNG_object) ne 'SCALAR';
1818 return $RNG_object;
19 };
19 };
2020 sub rand { return $fetch_RNG->()->double(@_) }
2121 sub irand { return $fetch_RNG->()->int32(@_) }
2222 sub random_bytes { return $fetch_RNG->()->bytes(@_) }
5757 $prng = Crypt::PRNG::Yarrow->new;
5858 #or
5959 $prng = Crypt::PRNG::Yarrow->new("some data used for seeding PRNG");
60
60
6161 $octets = $prng->bytes(45);
6262 $hex_string = $prng->bytes_hex(45);
6363 $base64_string = $prng->bytes_b64(45);
4343
4444 sub string_from {
4545 my ($self, $chars, $len) = @_;
46
46
4747 $len = 20 unless defined $len;
4848 return unless $len>0;
49
49
5050 my @ch = split(//, $chars);
5151 my $max_index = scalar(@ch)-1;
52
52
5353 my $mask;
54 for my $n (1..31) {
54 for my $n (1..31) {
5555 $mask = (1<<$n) - 1;
5656 last if $mask >= $max_index;
5757 }
7878 my $fetch_RNG = sub { # Lazily, instantiate the RNG object, but only once.
7979 $RNG_object = Crypt::PRNG->new unless defined $RNG_object && ref($RNG_object) ne 'SCALAR';
8080 return $RNG_object;
81 };
81 };
8282 sub rand { return $fetch_RNG->()->double(@_) }
8383 sub irand { return $fetch_RNG->()->int32() }
8484 sub random_bytes { return $fetch_RNG->()->bytes(@_) }
121121 $prng = Crypt::PRNG->new("RC4");
122122 #or
123123 $prng = Crypt::PRNG->new("RC4", "some data used for seeding PRNG");
124
124
125125 $octets = $prng->bytes(45);
126126 $hex_string = $prng->bytes_hex(45);
127127 $base64_string = $prng->bytes_b64(45);
216216 If called without parameter it uses 32bytes random data taken from C</dev/random> (UNIX) or C<CryptGenRandom> (Win32).
217217
218218 B<BEWARE:> you probably do not need this function at all as the module does automatic seeding on initialization as well as reseeding after fork and thread creation.
219
219
220220 =head2 bytes
221221
222222 $octets = $prng->bytes($length);
258258 =head2 double
259259
260260 $n = $prng->double;
261 #or
261 #or
262262 $n = $prng->double($limit);
263263
264264 See L<rand|/rand>
2424
2525 =item * Ciphers - see L<Crypt::Cipher> and related modules
2626
27 L<Crypt::Cipher::AES>, L<Crypt::Cipher::Anubis>, L<Crypt::Cipher::Blowfish>, L<Crypt::Cipher::Camellia>, L<Crypt::Cipher::CAST5>, L<Crypt::Cipher::DES>,
28 L<Crypt::Cipher::DES_EDE>, L<Crypt::Cipher::KASUMI>, L<Crypt::Cipher::Khazad>, L<Crypt::Cipher::MULTI2>, L<Crypt::Cipher::Noekeon>, L<Crypt::Cipher::RC2>,
29 L<Crypt::Cipher::RC5>, L<Crypt::Cipher::RC6>, L<Crypt::Cipher::SAFERP>, L<Crypt::Cipher::SAFER_K128>, L<Crypt::Cipher::SAFER_K64>, L<Crypt::Cipher::SAFER_SK128>,
27 L<Crypt::Cipher::AES>, L<Crypt::Cipher::Anubis>, L<Crypt::Cipher::Blowfish>, L<Crypt::Cipher::Camellia>, L<Crypt::Cipher::CAST5>, L<Crypt::Cipher::DES>,
28 L<Crypt::Cipher::DES_EDE>, L<Crypt::Cipher::KASUMI>, L<Crypt::Cipher::Khazad>, L<Crypt::Cipher::MULTI2>, L<Crypt::Cipher::Noekeon>, L<Crypt::Cipher::RC2>,
29 L<Crypt::Cipher::RC5>, L<Crypt::Cipher::RC6>, L<Crypt::Cipher::SAFERP>, L<Crypt::Cipher::SAFER_K128>, L<Crypt::Cipher::SAFER_K64>, L<Crypt::Cipher::SAFER_SK128>,
3030 L<Crypt::Cipher::SAFER_SK64>, L<Crypt::Cipher::SEED>, L<Crypt::Cipher::Skipjack>, L<Crypt::Cipher::Twofish>, L<Crypt::Cipher::XTEA>
3131
3232 =item * Block cipher modes
3939
4040 =item * Hash Functions - see L<Crypt::Digest> and related modules
4141
42 L<Crypt::Digest::CHAES>, L<Crypt::Digest::MD2>, L<Crypt::Digest::MD4>, L<Crypt::Digest::MD5>, L<Crypt::Digest::RIPEMD128>, L<Crypt::Digest::RIPEMD160>,
43 L<Crypt::Digest::RIPEMD256>, L<Crypt::Digest::RIPEMD320>, L<Crypt::Digest::SHA1>, L<Crypt::Digest::SHA224>, L<Crypt::Digest::SHA256>, L<Crypt::Digest::SHA384>,
42 L<Crypt::Digest::CHAES>, L<Crypt::Digest::MD2>, L<Crypt::Digest::MD4>, L<Crypt::Digest::MD5>, L<Crypt::Digest::RIPEMD128>, L<Crypt::Digest::RIPEMD160>,
43 L<Crypt::Digest::RIPEMD256>, L<Crypt::Digest::RIPEMD320>, L<Crypt::Digest::SHA1>, L<Crypt::Digest::SHA224>, L<Crypt::Digest::SHA256>, L<Crypt::Digest::SHA384>,
4444 L<Crypt::Digest::SHA512>, L<Crypt::Digest::Tiger192>, L<Crypt::Digest::Whirlpool>
4545
4646 =item * Message Authentication Codes
205205 if(register_hash(&sha384_desc)==-1) { croak("FATAL: cannot register_hash sha384"); }
206206 if(register_hash(&sha512_desc)==-1) { croak("FATAL: cannot register_hash sha512"); }
207207 if(register_hash(&tiger_desc)==-1) { croak("FATAL: cannot register_hash tiger"); }
208 if(register_hash(&whirlpool_desc)==-1) { croak("FATAL: cannot register_hash whirlpool"); }
208 if(register_hash(&whirlpool_desc)==-1) { croak("FATAL: cannot register_hash whirlpool"); }
209209 /* --- */
210210 if(chc_register(find_cipher("aes"))==-1) { croak("FATAL: chc_register failed"); }
211211 /* --- */
244244 INCLUDE: CryptX_Mac_Pelican.xs.inc
245245 INCLUDE: CryptX_Mac_PMAC.xs.inc
246246 INCLUDE: CryptX_Mac_XCBC.xs.inc
247
247
248248 INCLUDE: CryptX_Mode_CBC.xs.inc
249249 INCLUDE: CryptX_Mode_ECB.xs.inc
250250 INCLUDE: CryptX_Mode_CFB.xs.inc
88 int rv, id;
99 unsigned char tag[MAXBLOCKSIZE];
1010 SV *ct;
11
11
1212 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1313 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
1414 if (!SvPOK(header)) croak("FATAL: header must be string/buffer scalar");
1515 if (!SvPOK(plaintext)) croak("FATAL: plaintext must be string/buffer scalar");
1616 k = (unsigned char *) SvPVbyte(key, k_len);
17 n = (unsigned char *) SvPVbyte(nonce, n_len);
17 n = (unsigned char *) SvPVbyte(nonce, n_len);
1818 h = (unsigned char *) SvPVbyte(header, h_len);
1919 pt = (unsigned char *) SvPVbyte(plaintext, pt_len);
2020
2121 id = find_cipher(cipher_name);
2222 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
23
23
2424 ct = NEWSV(0, pt_len);
2525 SvPOK_only(ct);
2626 SvCUR_set(ct, pt_len);
27
27
2828 if(tag_len<4 || tag_len>16) tag_len = 16;
29
29
3030 rv = ccm_memory(id, k, (unsigned long)k_len, NULL, n, (unsigned long)n_len, h, (unsigned long)h_len,
3131 pt, (unsigned long)pt_len, (unsigned char *)SvPV_nolen(ct), tag, &tag_len, CCM_ENCRYPT);
3232 if (rv != CRYPT_OK) croak("FATAL: ccm_memory failed: %s", error_to_string(rv));
33
33
3434 XPUSHs(sv_2mortal(ct));
3535 XPUSHs(sv_2mortal(newSVpvn((char*)tag,tag_len)));
3636
4343 unsigned char *ct,
4444 unsigned char *tag, unsigned long *taglen,
4545 int direction); */
46
46
4747 }
4848
4949 void
5454 unsigned char *k, *n, *h, *ct, *t;
5555 int rv, id;
5656 unsigned char xtag[MAXBLOCKSIZE];
57 unsigned long xtag_len;
57 unsigned long xtag_len;
5858 SV *pt;
59
59
6060 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
6161 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
6262 if (!SvPOK(header)) croak("FATAL: header must be string/buffer scalar");
6363 if (!SvPOK(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar");
6464 if (!SvPOK(tag)) croak("FATAL: tag must be string/buffer scalar");
6565 k = (unsigned char *) SvPVbyte(key, k_len);
66 n = (unsigned char *) SvPVbyte(nonce, n_len);
66 n = (unsigned char *) SvPVbyte(nonce, n_len);
6767 h = (unsigned char *) SvPVbyte(header, h_len);
6868 ct = (unsigned char *) SvPVbyte(ciphertext, ct_len);
6969 t = (unsigned char *) SvPVbyte(tag, t_len);
7070
7171 id = find_cipher(cipher_name);
7272 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
73
73
7474 pt = NEWSV(0, ct_len);
7575 SvPOK_only(pt);
7676 SvCUR_set(pt, ct_len);
77
77
7878 xtag_len = (unsigned long)t_len;
79
79
8080 rv = ccm_memory(id, k, (unsigned long)k_len, NULL, n, (unsigned long)n_len, h, (unsigned long)h_len,
8181 (unsigned char *)SvPV_nolen(pt), (unsigned long)ct_len, ct, xtag, &xtag_len, CCM_DECRYPT);
8282 if (rv != CRYPT_OK) croak("FATAL: ccm_memory failed: %s", error_to_string(rv));
83
83
8484 if (t_len!=xtag_len) {
8585 XPUSHs(sv_2mortal(newSVpvn(NULL,0))); /* undef */
8686 }
8787 else if (memNE(t, xtag, xtag_len)) {
8888 XPUSHs(sv_2mortal(newSVpvn(NULL,0))); /* undef */
8989 }
90 else {
90 else {
9191 XPUSHs(sv_2mortal(pt));
9292 }
9393 }
1010 unsigned char *h=NULL;
1111 STRLEN h_len=0;
1212 int id;
13
13
1414 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1515 k = (unsigned char *) SvPVbyte(key, k_len);
1616 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
17 n = (unsigned char *) SvPVbyte(nonce, n_len);
17 n = (unsigned char *) SvPVbyte(nonce, n_len);
1818 if(SvOK(header)) { /* header is optional param */
1919 if(!SvPOK(header)) croak("FATAL: header must be string/buffer scalar");
2020 h = (unsigned char *) SvPVbyte(header, h_len);
6565 SvCUR_set(RETVAL, in_data_len);
6666 out_data = (unsigned char *)SvPV_nolen(RETVAL);
6767 rv = eax_encrypt(&self->state, in_data, out_data, (unsigned long)in_data_len);
68 if (rv != CRYPT_OK) croak("FATAL: eax_encrypt failed: %s", error_to_string(rv));
68 if (rv != CRYPT_OK) croak("FATAL: eax_encrypt failed: %s", error_to_string(rv));
6969 }
7070 }
7171 OUTPUT:
7878 int rv;
7979 STRLEN in_data_len;
8080 unsigned char *in_data, *out_data;
81
82 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
81
82 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
8383 if (in_data_len==0) {
8484 RETVAL = newSVpvn("", 0);
8585 }
8989 SvCUR_set(RETVAL, in_data_len);
9090 out_data = (unsigned char *)SvPV_nolen(RETVAL);
9191 rv = eax_decrypt(&self->state, in_data, out_data, (unsigned long)in_data_len);
92 if (rv != CRYPT_OK) croak("FATAL: eax_decrypt failed: %s", error_to_string(rv));
92 if (rv != CRYPT_OK) croak("FATAL: eax_decrypt failed: %s", error_to_string(rv));
9393 }
9494 }
9595 OUTPUT:
102102 int rv;
103103 unsigned char tag[MAXBLOCKSIZE];
104104 unsigned long tag_len = sizeof(tag);
105
105
106106 rv = eax_done(&self->state, tag, &tag_len);
107107 if (rv != CRYPT_OK) croak("FATAL: eax_done failed: %s", error_to_string(rv));
108108 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
117117 unsigned long tag_len = sizeof(tag);
118118 STRLEN expected_tag_len;
119119 unsigned char *expected_tag;
120
120
121121 rv = eax_done(&self->state, tag, &tag_len);
122122 if (rv != CRYPT_OK) croak("FATAL: eax_done failed: %s", error_to_string(rv));
123123 if (items == 1) {
134134 }
135135 else {
136136 XPUSHs(sv_2mortal(newSViv(1))); /* true */
137 }
137 }
138138 }
139139 }
140140
66 STRLEN k_len=0;
77 unsigned char *k=NULL;
88 int id;
9
9
1010 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1111 k = (unsigned char *) SvPVbyte(key, k_len);
1212
4444 {
4545 int rv;
4646 rv = gcm_reset(&self->state);
47 if (rv != CRYPT_OK) croak("FATAL: gcm_reset failed: %s", error_to_string(rv));
47 if (rv != CRYPT_OK) croak("FATAL: gcm_reset failed: %s", error_to_string(rv));
4848 RETVAL = rv;
4949 }
5050 OUTPUT:
6262 if (in_data_len==0) {
6363 RETVAL = newSVpvn("", 0);
6464 }
65 else
65 else
6666 {
6767 RETVAL = NEWSV(0, in_data_len);
6868 SvPOK_only(RETVAL);
6969 SvCUR_set(RETVAL, in_data_len);
7070 out_data = (unsigned char *)SvPV_nolen(RETVAL);
7171 rv = gcm_process(&self->state, in_data, (unsigned long)in_data_len, out_data, GCM_ENCRYPT);
72 if (rv != CRYPT_OK) croak("FATAL: encrypt_add/gcm_process failed: %s", error_to_string(rv));
72 if (rv != CRYPT_OK) croak("FATAL: encrypt_add/gcm_process failed: %s", error_to_string(rv));
7373 }
7474 }
7575 OUTPUT:
8585
8686 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
8787 rv = gcm_add_iv(&self->state, in_data, (unsigned long)in_data_len);
88 if (rv != CRYPT_OK) croak("FATAL: gcm_add_iv failed: %s", error_to_string(rv));
88 if (rv != CRYPT_OK) croak("FATAL: gcm_add_iv failed: %s", error_to_string(rv));
8989 RETVAL = rv;
9090 }
9191 OUTPUT:
101101
102102 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
103103 rv = gcm_add_aad(&self->state, in_data, (unsigned long)in_data_len);
104 if (rv != CRYPT_OK) croak("FATAL: gcm_add_aad failed: %s", error_to_string(rv));
104 if (rv != CRYPT_OK) croak("FATAL: gcm_add_aad failed: %s", error_to_string(rv));
105105 RETVAL = rv;
106106 }
107107 OUTPUT:
114114 int rv;
115115 STRLEN in_data_len;
116116 unsigned char *in_data, *out_data;
117
118 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
117
118 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
119119 if (in_data_len==0) {
120120 RETVAL = newSVpvn("", 0);
121121 }
139139 int rv;
140140 unsigned char tag[MAXBLOCKSIZE];
141141 unsigned long tag_len = sizeof(tag);
142
142
143143 rv = gcm_done(&self->state, tag, &tag_len);
144144 if (rv != CRYPT_OK) croak("FATAL: gcm_done failed: %s", error_to_string(rv));
145145 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
154154 unsigned long tag_len = sizeof(tag);
155155 STRLEN expected_tag_len;
156156 unsigned char *expected_tag;
157
157
158158 rv = gcm_done(&self->state, tag, &tag_len);
159159 if (rv != CRYPT_OK) croak("FATAL: gcm_done failed: %s", error_to_string(rv));
160160 if (items == 1) {
171171 }
172172 else {
173173 XPUSHs(sv_2mortal(newSViv(1))); /* true */
174 }
174 }
175175 }
176176 }
88 unsigned char *n=NULL;
99 STRLEN n_len=0;
1010 int id;
11
11
1212 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1313 k = (unsigned char *) SvPVbyte(key, k_len);
1414 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
5353
5454 if (in_data_len>0) {
5555 rv = ocb3_add_aad(&self->state, in_data, (unsigned long)in_data_len);
56 if (rv != CRYPT_OK) croak("FATAL: ocb3_add_aad failed: %s", error_to_string(rv));
56 if (rv != CRYPT_OK) croak("FATAL: ocb3_add_aad failed: %s", error_to_string(rv));
5757 }
5858 }
5959
7979 croak ("FATAL: sizeof(data) should be multiple of blocksize (%d)", (&self->state)->block_len);
8080
8181 rv = ocb3_encrypt(&self->state, in_data, (unsigned long)in_data_len, out_data);
82 if (rv != CRYPT_OK) croak("FATAL: ocb3_encrypt failed: %s", error_to_string(rv));
82 if (rv != CRYPT_OK) croak("FATAL: ocb3_encrypt failed: %s", error_to_string(rv));
8383 }
8484 }
8585 OUTPUT:
9393 STRLEN in_data_len;
9494 unsigned char *in_data, *out_data;
9595
96 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
96 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
9797 if (in_data_len>0) {
9898 RETVAL = NEWSV(0, in_data_len);
9999 SvPOK_only(RETVAL);
105105 out_data = NULL;
106106 }
107107 rv = ocb3_encrypt_last(&self->state, in_data, (unsigned long)in_data_len, out_data);
108 if (rv != CRYPT_OK) croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
108 if (rv != CRYPT_OK) croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
109109 }
110110 OUTPUT:
111111 RETVAL
117117 int rv;
118118 STRLEN in_data_len;
119119 unsigned char *in_data, *out_data;
120
121 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
120
121 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
122122 if (in_data_len==0) {
123123 RETVAL = newSVpvn("", 0);
124124 }
132132 croak ("FATAL: sizeof(data) should be multiple of blocksize (%d)", (&self->state)->block_len);
133133
134134 rv = ocb3_decrypt(&self->state, in_data, (unsigned long)in_data_len, out_data);
135 if (rv != CRYPT_OK) croak("FATAL: ocb3_decrypt failed: %s", error_to_string(rv));
135 if (rv != CRYPT_OK) croak("FATAL: ocb3_decrypt failed: %s", error_to_string(rv));
136136 }
137137 }
138138 OUTPUT:
146146 STRLEN in_data_len;
147147 unsigned char *in_data, *out_data;
148148
149 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
149 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
150150 if (in_data_len>0) {
151151 RETVAL = NEWSV(0, in_data_len);
152152 SvPOK_only(RETVAL);
158158 out_data = NULL;
159159 }
160160 rv = ocb3_decrypt_last(&self->state, in_data, (unsigned long)in_data_len, out_data);
161 if (rv != CRYPT_OK) croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
161 if (rv != CRYPT_OK) croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
162162 }
163163 OUTPUT:
164164 RETVAL
170170 int rv;
171171 unsigned char tag[MAXBLOCKSIZE];
172172 unsigned long tag_len = sizeof(tag);
173
173
174174 rv = ocb3_done(&self->state, tag, &tag_len);
175 if (rv != CRYPT_OK) croak("FATAL: ocb3_done_encrypt failed: %s", error_to_string(rv));
175 if (rv != CRYPT_OK) croak("FATAL: ocb3_done_encrypt failed: %s", error_to_string(rv));
176176
177177 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
178178 }
186186 unsigned long tag_len = sizeof(tag);
187187 STRLEN expected_tag_len;
188188 unsigned char *expected_tag;
189
190 rv = ocb3_done(&self->state, tag, &tag_len);
189
190 rv = ocb3_done(&self->state, tag, &tag_len);
191191 if (rv != CRYPT_OK) croak("FATAL: ocb3_done_decrypt failed: %s", error_to_string(rv));
192192 if (items == 1) {
193193 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
203203 }
204204 else {
205205 XPUSHs(sv_2mortal(newSViv(1))); /* true */
206 }
206 }
207207 }
208208 }
209209
1010 unsigned char *key_data=NULL;
1111 int rv;
1212 int id;
13
13
1414 if (!SvPOK (key)) croak("FATAL: key must be string scalar");
1515 key_data = (unsigned char *)SvPVbyte(key, key_len);
16
16
1717 id = find_cipher(cipher_name);
1818 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1919
124124 CODE:
125125 {
126126 int rv, id;
127
127
128128 id = find_cipher(cipher_name);
129129 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
130
130
131131 rv = cipher_descriptor[id].block_length;
132132 if (!rv) XSRETURN_UNDEF;
133133 RETVAL = rv;
141141 CODE:
142142 {
143143 int rv, id;
144
144
145145 id = find_cipher(cipher_name);
146146 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
147
147
148148 rv = cipher_descriptor[id].min_key_length;
149149 if (!rv) XSRETURN_UNDEF;
150150 RETVAL = rv;
158158 CODE:
159159 {
160160 int rv, id;
161
161
162162 id = find_cipher(cipher_name);
163163 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
164
164
165165 rv = cipher_descriptor[id].max_key_length;
166166 if (!rv) XSRETURN_UNDEF;
167167 RETVAL = rv;
175175 CODE:
176176 {
177177 int rv, id;
178
178
179179 id = find_cipher(cipher_name);
180180 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
181
181
182182 rv = cipher_descriptor[id].default_rounds;
183183 if (!rv) XSRETURN_UNDEF;
184184 RETVAL = rv;
5353 STRLEN inlen;
5454 int rv, i;
5555 unsigned char *in;
56
56
5757 for(i=1; i<items; i++) {
5858 in = (unsigned char *)SvPVbyte(ST(i), inlen);
5959 if (inlen>0) {
7171 {
7272 unsigned char hash[MAXBLOCKSIZE];
7373 int rv;
74
74
7575 rv = self->desc->done(&self->state, hash);
7676 if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv));
7777 RETVAL = newSVpvn((char *) hash, self->desc->hashsize);
8888 unsigned long i;
8989 unsigned char hash[MAXBLOCKSIZE];
9090 char hash_hex[MAXBLOCKSIZE*2 + 1];
91
91
9292 rv = self->desc->done(&self->state, hash);
9393 if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv));
94
94
9595 hash_hex[0] = '\0';
9696 for(i=0; i<self->desc->hashsize; i++)
9797 sprintf(&hash_hex[2*i], "%02x", hash[i]);
109109 unsigned long outlen;
110110 unsigned char hash[MAXBLOCKSIZE];
111111 char hash_base64[MAXBLOCKSIZE*2 + 1];
112
112
113113 rv = self->desc->done(&self->state, hash);
114114 if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv));
115
115
116116 outlen = sizeof(hash_base64);
117117 rv = base64_encode(hash, self->desc->hashsize, (unsigned char *)hash_base64, &outlen);
118118 if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
130130 unsigned long outlen;
131131 unsigned char hash[MAXBLOCKSIZE];
132132 char hash_base64[MAXBLOCKSIZE*2 + 1];
133
133
134134 rv = self->desc->done(&self->state, hash);
135135 if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv));
136
136
137137 outlen = sizeof(hash_base64);
138138 rv = base64url_encode(hash, self->desc->hashsize, (unsigned char *)hash_base64, &outlen);
139139 if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
156156 CODE:
157157 {
158158 int rv, id;
159
159
160160 id = find_hash(digest_name);
161161 if(id==-1) croak("FATAL: find_digest failed for '%s'", digest_name);
162
162
163163 rv = hash_descriptor[id].hashsize;
164164 if (!rv) croak("FATAL: invalid hashsize for '%s'", digest_name);;
165165 RETVAL = rv;
44 CODE:
55 {
66 /*
7 int pkcs_5_alg1(const unsigned char *password, unsigned long password_len,
8 const unsigned char *salt,
7 int pkcs_5_alg1(const unsigned char *password, unsigned long password_len,
8 const unsigned char *salt,
99 int iteration_count, int hash_idx,
1010 unsigned char *out, unsigned long *outlen)
1111 */
1919
2020 id = find_hash(hash_name);
2121 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
22
22
2323 password_ptr = (unsigned char *)SvPVbyte(password, password_len);
2424 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
2525 if (salt_len < 8) croak("FATAL: salt_len has to be 8");
26
26
2727 output_len = len;
2828 Newz(0, output, output_len, unsigned char);
2929 if (!output) croak("FATAL: Newz failed [%ld]", output_len);
30
30
3131 rv = pkcs_5_alg1(password_ptr, (unsigned long)password_len, salt_ptr, iteration_count, id, output, &output_len);
3232 if (rv != CRYPT_OK) croak("FATAL: pkcs_5_alg1 process failed: %s", error_to_string(rv));
33
33
3434 RETVAL = newSVpvn((char *)output, output_len);
3535 Safefree(output);
3636 }
4242 CODE:
4343 {
4444 /*
45 int pkcs_5_alg2(const unsigned char *password, unsigned long password_len,
45 int pkcs_5_alg2(const unsigned char *password, unsigned long password_len,
4646 const unsigned char *salt, unsigned long salt_len,
4747 int iteration_count, int hash_idx,
4848 unsigned char *out, unsigned long *outlen)
5757
5858 id = find_hash(hash_name);
5959 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
60
60
6161 password_ptr = (unsigned char *)SvPVbyte(password, password_len);
6262 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
63
63
6464 output_len = len;
6565 Newz(0, output, output_len, unsigned char);
6666 if (!output) croak("FATAL: Newz failed [%ld]", output_len);
67
67
6868 rv = pkcs_5_alg2(password_ptr, (unsigned long)password_len, salt_ptr, (unsigned long)salt_len, iteration_count, id, output, &output_len);
6969 if (rv != CRYPT_OK) croak("FATAL: pkcs_5_alg2 process failed: %s", error_to_string(rv));
70
70
7171 RETVAL = newSVpvn((char *)output, output_len);
7272 Safefree(output);
7373 }
9393
9494 id = find_hash(hash_name);
9595 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
96
96
9797 in_ptr = (unsigned char *)SvPVbyte(in, in_len);
9898 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
99
99
100100 output_len = sizeof(output);
101101 rv = hkdf_extract(id, salt_ptr, (unsigned long)salt_len, in_ptr, (unsigned long)in_len, output, &output_len);
102102 if (rv != CRYPT_OK) croak("FATAL: hkdf_extract process failed: %s", error_to_string(rv));
103
103
104104 RETVAL = newSVpvn((char *)output, output_len);
105105 }
106106 OUTPUT:
124124
125125 id = find_hash(hash_name);
126126 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
127
127
128128 in_ptr = (unsigned char *)SvPVbyte(in, in_len);
129129 info_ptr = (unsigned char *)SvPVbyte(info, info_len);
130
130
131131 Newz(0, output, output_len, unsigned char);
132132 if (!output) croak("FATAL: Newz failed [%ld]", output_len);
133
133
134134 rv = hkdf_expand(id, info_ptr, (unsigned long)info_len, in_ptr, (unsigned long)in_len, output, output_len);
135135 if (rv != CRYPT_OK) croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv));
136
136
137137 RETVAL = newSVpvn((char *)output, output_len);
138138 Safefree(output);
139139 }
161161
162162 id = find_hash(hash_name);
163163 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
164
164
165165 in_ptr = (unsigned char *)SvPVbyte(in, in_len);
166166 info_ptr = (unsigned char *)SvPVbyte(info, info_len);
167167 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
168
168
169169 Newz(0, output, output_len, unsigned char);
170170 if (!output) croak("FATAL: Newz failed [%ld]", output_len);
171
171
172172 rv = hkdf(id, salt_ptr, (unsigned long)salt_len, info_ptr, (unsigned long)info_len, in_ptr, (unsigned long)in_len, output, output_len);
173173 if (rv != CRYPT_OK) croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv));
174
174
175175 RETVAL = newSVpvn((char *)output, output_len);
176176 Safefree(output);
177177 }
77 {
88 STRLEN k_len=0;
99 unsigned char *k=NULL;
10 int rv;
10 int rv;
1111 int id;
1212
1313 id = find_cipher(cipher_name);
4747 STRLEN in_data_len;
4848 unsigned char *in_data;
4949
50 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
50 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
5151 if (in_data_len>0) {
5252 rv = f9_process(&self->state, in_data, (unsigned long)in_data_len);
5353 if (rv != CRYPT_OK) croak("FATAL: f9_process failed: %s", error_to_string(rv));
8585 if (rv != CRYPT_OK) croak("FATAL: f9_done failed: %s", error_to_string(rv));
8686 outlen = sizeof(mac_base64);
8787 rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen);
88 if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
88 if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
8989 RETVAL = newSVpvn(mac_base64, outlen);
9090 }
9191 OUTPUT:
106106 if (rv != CRYPT_OK) croak("FATAL: f9_done failed: %s", error_to_string(rv));
107107 outlen = sizeof(mac_base64);
108108 rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen);
109 if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
109 if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
110110 RETVAL = newSVpvn(mac_base64, outlen);
111111 }
112112 OUTPUT:
77 {
88 STRLEN k_len=0;
99 unsigned char *k=NULL;
10 int rv;
10 int rv;
1111 int id;
1212
1313 id = find_hash(hash_name);
4747 STRLEN in_data_len;
4848 unsigned char *in_data;
4949
50 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
50 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
5151 if (in_data_len>0) {
5252 rv = hmac_process(&self->state, in_data, (unsigned long)in_data_len);
5353 if (rv != CRYPT_OK) croak("FATAL: hmac_process failed: %s", error_to_string(rv));
8585 if (rv != CRYPT_OK) croak("FATAL: hmac_done failed: %s", error_to_string(rv));
8686 outlen = sizeof(mac_base64);
8787 rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen);
88 if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
88 if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
8989 RETVAL = newSVpvn(mac_base64, outlen);
9090 }
9191 OUTPUT:
106106 if (rv != CRYPT_OK) croak("FATAL: hmac_done failed: %s", error_to_string(rv));
107107 outlen = sizeof(mac_base64);
108108 rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen);
109 if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
109 if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
110110 RETVAL = newSVpvn(mac_base64, outlen);
111111 }
112112 OUTPUT:
77 {
88 STRLEN k_len=0;
99 unsigned char *k=NULL;
10 int rv;
10 int rv;
1111 int id;
1212
1313 id = find_cipher(cipher_name);
4747 STRLEN in_data_len;
4848 unsigned char *in_data;
4949
50 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
50 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
5151 if (in_data_len>0) {
5252 rv = omac_process(&self->state, in_data, (unsigned long)in_data_len);
5353 if (rv != CRYPT_OK) croak("FATAL: omac_process failed: %s", error_to_string(rv));
8585 if (rv != CRYPT_OK) croak("FATAL: omac_done failed: %s", error_to_string(rv));
8686 outlen = sizeof(mac_base64);
8787 rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen);
88 if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
88 if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
8989 RETVAL = newSVpvn(mac_base64, outlen);
9090 }
9191 OUTPUT:
106106 if (rv != CRYPT_OK) croak("FATAL: omac_done failed: %s", error_to_string(rv));
107107 outlen = sizeof(mac_base64);
108108 rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen);
109 if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
109 if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
110110 RETVAL = newSVpvn(mac_base64, outlen);
111111 }
112112 OUTPUT:
77 {
88 STRLEN k_len=0;
99 unsigned char *k=NULL;
10 int rv;
10 int rv;
1111 int id;
1212
1313 id = find_cipher(cipher_name);
4747 STRLEN in_data_len;
4848 unsigned char *in_data;
4949
50 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
50 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
5151 if (in_data_len>0) {
5252 rv = pmac_process(&self->state, in_data, (unsigned long)in_data_len);
5353 if (rv != CRYPT_OK) croak("FATAL: pmac_process failed: %s", error_to_string(rv));
8585 if (rv != CRYPT_OK) croak("FATAL: pmac_done failed: %s", error_to_string(rv));
8686 outlen = sizeof(mac_base64);
8787 rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen);
88 if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
88 if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
8989 RETVAL = newSVpvn(mac_base64, outlen);
9090 }
9191 OUTPUT:
106106 if (rv != CRYPT_OK) croak("FATAL: pmac_done failed: %s", error_to_string(rv));
107107 outlen = sizeof(mac_base64);
108108 rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen);
109 if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
109 if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
110110 RETVAL = newSVpvn(mac_base64, outlen);
111111 }
112112 OUTPUT:
77 {
88 STRLEN k_len=0;
99 unsigned char *k=NULL;
10 int rv;
10 int rv;
1111
1212 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1313 k = (unsigned char *) SvPVbyte(key, k_len);
1515 Newz(0, RETVAL, 1, struct pelican_struct);
1616 if (!RETVAL) croak("FATAL: Newz failed");
1717
18 rv = pelican_init(&RETVAL->state, k, (unsigned long)k_len);
18 rv = pelican_init(&RETVAL->state, k, (unsigned long)k_len);
1919 if (rv != CRYPT_OK) croak("FATAL: pelican_init failed: %s", error_to_string(rv));
2020 }
2121 OUTPUT:
4343 STRLEN in_data_len;
4444 unsigned char *in_data;
4545
46 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
46 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
4747 if (in_data_len>0) {
4848 rv = pelican_process(&self->state, in_data, (unsigned long)in_data_len);
4949 if (rv != CRYPT_OK) croak("FATAL: pelican_process failed: %s", error_to_string(rv));
8181 if (rv != CRYPT_OK) croak("FATAL: pelican_done failed: %s", error_to_string(rv));
8282 outlen = sizeof(mac_base64);
8383 rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen);
84 if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
84 if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
8585 RETVAL = newSVpvn(mac_base64, outlen);
8686 }
8787 OUTPUT:
102102 if (rv != CRYPT_OK) croak("FATAL: pelican_done failed: %s", error_to_string(rv));
103103 outlen = sizeof(mac_base64);
104104 rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen);
105 if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
105 if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
106106 RETVAL = newSVpvn(mac_base64, outlen);
107107 }
108108 OUTPUT:
77 {
88 STRLEN k_len=0;
99 unsigned char *k=NULL;
10 int rv;
10 int rv;
1111 int id;
1212
1313 id = find_cipher(cipher_name);
4747 STRLEN in_data_len;
4848 unsigned char *in_data;
4949
50 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
50 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
5151 if (in_data_len>0) {
5252 rv = xcbc_process(&self->state, in_data, (unsigned long)in_data_len);
5353 if (rv != CRYPT_OK) croak("FATAL: xcbc_process failed: %s", error_to_string(rv));
8585 if (rv != CRYPT_OK) croak("FATAL: xcbc_done failed: %s", error_to_string(rv));
8686 outlen = sizeof(mac_base64);
8787 rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen);
88 if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
88 if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
8989 RETVAL = newSVpvn(mac_base64, outlen);
9090 }
9191 OUTPUT:
106106 if (rv != CRYPT_OK) croak("FATAL: xcbc_done failed: %s", error_to_string(rv));
107107 outlen = sizeof(mac_base64);
108108 rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen);
109 if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
109 if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
110110 RETVAL = newSVpvn(mac_base64, outlen);
111111 }
112112 OUTPUT:
9393 in_data_len = 0;
9494 }
9595 } /* padlen > 0 */
96
96
9797 i = (unsigned long)(in_data_len % blen);
9898 if (in_data_len>0 && i>0) { /* save tail of data into pad */
9999 Copy(in_data+in_data_start+in_data_len-i, self->pad, i, unsigned char);
100100 self->padlen = i;
101101 in_data_len -= i;
102102 }
103
103
104104 if (in_data_len>0) {
105105 i = (unsigned long)(has_tmp_block ? in_data_len + blen : in_data_len);
106106 RETVAL = NEWSV(0, i);
107107 SvPOK_only(RETVAL);
108108 SvCUR_set(RETVAL, i);
109109 out_data = (unsigned char *)SvPV_nolen(RETVAL);
110
110
111111 if (has_tmp_block) {
112112 Copy(tmp_block, out_data, blen, unsigned char);
113113 out_data += blen;
132132 {
133133 unsigned char tmp_block[MAXBLOCKSIZE];
134134 int rv, blen, i, j;
135
135
136136 blen = (&self->state)->blocklen;
137137 if (self->padlen<0 || self->padlen>=blen) croak("FATAL: invalid padlen");
138
138
139139 if(self->padding_mode == 1) { /* pkcs5|7 padding */
140140 i = blen - self->padlen;
141141 if (i == 0) i = blen;
147147 self->pad[self->padlen] = 0x80;
148148 for(j=self->padlen+1; j<blen; j++) self->pad[j] = 0;
149149 rv = cbc_encrypt(self->pad, tmp_block, blen, &self->state);
150 if (rv != CRYPT_OK) croak("FATAL: cbc_encrypt failed: %s", error_to_string(rv));
150 if (rv != CRYPT_OK) croak("FATAL: cbc_encrypt failed: %s", error_to_string(rv));
151151 }
152152 else {
153153 if (self->padlen>0) croak("FATAL: cbc_encrypt, input data length not multiple of %d", blen);
154154 blen = 0;
155155 }
156
156
157157 RETVAL = newSVpvn((char*)tmp_block, blen);
158158 }
159159 OUTPUT:
178178 RETVAL = newSVpvn("", 0);
179179 }
180180 else {
181
181
182182 if(self->padlen == blen) {
183183 rv = cbc_decrypt(self->pad, tmp_block, blen, &self->state);
184184 if (rv != CRYPT_OK) croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv));
205205 in_data_len = 0;
206206 }
207207 } /* padlen > 0 */
208
208
209209 /* here: a/ padlen==1..16 && in_data_len==0; b/ padlen==0 && in_data_len>0 */
210210 if (in_data_len>0) {
211211 i = (unsigned long)(in_data_len % blen);
215215 in_data_len -= i;
216216 }
217217 }
218
218
219219 if (in_data_len>0) {
220220 if(self->padlen == 0 && self->padding_mode !=0) {
221221 /* in case of padding keep full pad if no more data */
256256 _finish_dec(Crypt::Mode::CBC self)
257257 CODE:
258258 {
259 unsigned char tmp_block[MAXBLOCKSIZE];
259 unsigned char tmp_block[MAXBLOCKSIZE];
260260 unsigned char i;
261261 int rv, rv_len, blen;
262
263 rv_len = 0;
262
263 rv_len = 0;
264264 if (self->padlen > 0) {
265265 blen = (&self->state)->blocklen;
266266 if (self->padlen != blen) croak("FATAL: cipher text length has to be multiple of %d (%d)", blen, self->padlen);
8686 in_data_len = 0;
8787 }
8888 } /* padlen > 0 */
89
89
9090 i = (unsigned long)(in_data_len % blen);
9191 if (in_data_len>0 && i>0) { /* save tail of data into pad */
9292 Copy(in_data+in_data_start+in_data_len-i, self->pad, i, unsigned char);
9393 self->padlen = i;
9494 in_data_len -= i;
9595 }
96
96
9797 if (in_data_len>0) {
9898 i = (unsigned long)(has_tmp_block ? in_data_len + blen : in_data_len);
9999 RETVAL = NEWSV(0, i);
100100 SvPOK_only(RETVAL);
101101 SvCUR_set(RETVAL, i);
102102 out_data = (unsigned char *)SvPV_nolen(RETVAL);
103
103
104104 if (has_tmp_block) {
105105 Copy(tmp_block, out_data, blen, unsigned char);
106106 out_data += blen;
125125 {
126126 unsigned char tmp_block[MAXBLOCKSIZE];
127127 int rv, blen, i, j;
128
128
129129 blen = (&self->state)->blocklen;
130130 if (self->padlen<0 || self->padlen>=blen) croak("FATAL: invalid padlen");
131
131
132132 if(self->padding_mode == 1) { /* pkcs5|7 padding */
133133 i = blen - self->padlen;
134134 if (i == 0) i = blen;
140140 self->pad[self->padlen] = 0x80;
141141 for(j=self->padlen+1; j<blen; j++) self->pad[j] = 0;
142142 rv = ecb_encrypt(self->pad, tmp_block, blen, &self->state);
143 if (rv != CRYPT_OK) croak("FATAL: ecb_encrypt failed: %s", error_to_string(rv));
143 if (rv != CRYPT_OK) croak("FATAL: ecb_encrypt failed: %s", error_to_string(rv));
144144 }
145145 else {
146146 if (self->padlen>0) croak("FATAL: ecb_encrypt, input data length not multiple of %d", blen);
147147 blen = 0;
148148 }
149
149
150150 RETVAL = newSVpvn((char*)tmp_block, blen);
151151 }
152152 OUTPUT:
171171 RETVAL = newSVpvn("", 0);
172172 }
173173 else {
174
174
175175 if(self->padlen == blen) {
176176 rv = ecb_decrypt(self->pad, tmp_block, blen, &self->state);
177177 if (rv != CRYPT_OK) croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv));
198198 in_data_len = 0;
199199 }
200200 } /* padlen > 0 */
201
201
202202 /* here: a/ padlen==1..16 && in_data_len==0; b/ padlen==0 && in_data_len>0 */
203203 if (in_data_len>0) {
204204 i = (unsigned long)(in_data_len % blen);
208208 in_data_len -= i;
209209 }
210210 }
211
211
212212 if (in_data_len>0) {
213213 if(self->padlen == 0 && self->padding_mode !=0) {
214214 /* in case of padding keep full pad if no more data */
249249 _finish_dec(Crypt::Mode::ECB self)
250250 CODE:
251251 {
252 unsigned char tmp_block[MAXBLOCKSIZE];
252 unsigned char tmp_block[MAXBLOCKSIZE];
253253 unsigned char i;
254254 int rv, rv_len, blen;
255
256 rv_len = 0;
255
256 rv_len = 0;
257257 if (self->padlen > 0) {
258258 blen = (&self->state)->blocklen;
259259 if (self->padlen != blen) croak("FATAL: cipher text length has to be multiple of %d (%d)", blen, self->padlen);
33 _new()
44 CODE:
55 {
6 int rv;
6 int rv;
77 Newz(0, RETVAL, 1, struct dh_struct);
88 if (!RETVAL) croak("FATAL: Newz failed");
99 RETVAL->key.type = -1;
3434 int rv;
3535 unsigned char *data=NULL;
3636 STRLEN data_len=0;
37
37
3838 data = (unsigned char *)SvPVbyte(key_data, data_len);
3939 dh_free(&self->key);
4040 rv = dh_import(data, (unsigned long)data_len, &self->key);
7272 /* =====> x */
7373 siz = (self->key.x) ? ltc_mp.unsigned_size(self->key.x) : 0;
7474 if (siz>10000) {
75 croak("FATAL: key2hash failed - 'x' too big number");
75 croak("FATAL: key2hash failed - 'x' too big number");
7676 }
7777 if (siz>0) {
7878 mp_tohex(self->key.x, buf);
8484 /* =====> y */
8585 siz = (self->key.y) ? ltc_mp.unsigned_size(self->key.y) : 0;
8686 if (siz>10000) {
87 croak("FATAL: key2hash failed - 'y' too big number");
87 croak("FATAL: key2hash failed - 'y' too big number");
8888 }
8989 if (siz>0) {
9090 mp_tohex(self->key.y, buf);
111111 int rv;
112112 unsigned char out[4096];
113113 unsigned long int out_len = 4096;
114
114
115115 RETVAL = newSVpvn(NULL, 0); /* undef */
116116 if (strnEQ(type, "private", 7)) {
117117 rv = dh_export(out, &out_len, PK_PRIVATE, &self->key);
139139 STRLEN data_len=0;
140140 unsigned char buffer[1024];
141141 unsigned long buffer_len = 1024;
142
143 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
144
142
143 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
144
145145 hash_id = find_hash(hash_name);
146146 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
147147 rv = dh_encrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
162162 STRLEN data_len=0;
163163 unsigned char buffer[1024];
164164 unsigned long buffer_len = 1024;
165
165
166166 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
167167
168168 rv = dh_decrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &self->key);
181181 STRLEN data_len=0;
182182 unsigned char buffer[1024];
183183 unsigned long buffer_len = 1024;
184
185 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
186
184
185 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
186
187187 rv = dh_sign_hash(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
188188 &self->yarrow_prng_state, self->yarrow_prng_index,
189189 &self->key);
202202 STRLEN data_len=0;
203203 unsigned char *sig_ptr=NULL;
204204 STRLEN sig_len=0;
205
205
206206 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
207207 sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len);
208
208
209209 RETVAL = 1;
210210 rv = dh_verify_hash(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &stat, &self->key);
211211 if (rv != CRYPT_OK || stat != 1) RETVAL = 0;
33 _new()
44 CODE:
55 {
6 int rv;
6 int rv;
77 Newz(0, RETVAL, 1, struct dsa_struct);
88 if (!RETVAL) croak("FATAL: Newz failed");
99 RETVAL->key.type = -1;
3434 int rv;
3535 unsigned char *data=NULL;
3636 STRLEN data_len=0;
37
37
3838 data = (unsigned char *)SvPVbyte(key_data, data_len);
3939 dsa_free(&self->key);
4040 rv = dsa_import(data, (unsigned long)data_len, &self->key);
8080 /* =====> g */
8181 siz = (self->key.g) ? ltc_mp.unsigned_size(self->key.g) : 0;
8282 if (siz>10000) {
83 croak("FATAL: key2hash failed - 'g' too big number");
83 croak("FATAL: key2hash failed - 'g' too big number");
8484 }
8585 if (siz>0) {
8686 mp_tohex(self->key.g, buf);
9292 /* =====> q */
9393 siz = (self->key.q) ? ltc_mp.unsigned_size(self->key.q) : 0;
9494 if (siz>10000) {
95 croak("FATAL: key2hash failed - 'q' too big number");
95 croak("FATAL: key2hash failed - 'q' too big number");
9696 }
9797 if (siz>0) {
9898 mp_tohex(self->key.q, buf);
104104 /* =====> p */
105105 siz = (self->key.p) ? ltc_mp.unsigned_size(self->key.p) : 0;
106106 if (siz>10000) {
107 croak("FATAL: key2hash failed - 'p' too big number");
107 croak("FATAL: key2hash failed - 'p' too big number");
108108 }
109109 if (siz>0) {
110110 mp_tohex(self->key.p, buf);
116116 /* =====> x */
117117 siz = (self->key.x) ? ltc_mp.unsigned_size(self->key.x) : 0;
118118 if (siz>10000) {
119 croak("FATAL: key2hash failed - 'x' too big number");
119 croak("FATAL: key2hash failed - 'x' too big number");
120120 }
121121 if (siz>0) {
122122 mp_tohex(self->key.x, buf);
128128 /* =====> y */
129129 siz = (self->key.y) ? ltc_mp.unsigned_size(self->key.y) : 0;
130130 if (siz>10000) {
131 croak("FATAL: key2hash failed - 'y' too big number");
131 croak("FATAL: key2hash failed - 'y' too big number");
132132 }
133133 if (siz>0) {
134134 mp_tohex(self->key.y, buf);
152152 int rv;
153153 unsigned char out[4096];
154154 unsigned long int out_len = 4096;
155
155
156156 RETVAL = newSVpvn(NULL, 0); /* undef */
157157 if (strnEQ(type, "private", 7)) {
158158 rv = dsa_export(out, &out_len, PK_PRIVATE, &self->key);
180180 STRLEN data_len=0;
181181 unsigned char buffer[1024];
182182 unsigned long buffer_len = 1024;
183
184 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
185
183
184 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
185
186186 hash_id = find_hash(hash_name);
187187 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
188188 rv = dsa_encrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
203203 STRLEN data_len=0;
204204 unsigned char buffer[1024];
205205 unsigned long buffer_len = 1024;
206
206
207207 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
208208
209209 rv = dsa_decrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &self->key);
222222 STRLEN data_len=0;
223223 unsigned char buffer[1024];
224224 unsigned long buffer_len = 1024;
225
226 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
227
225
226 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
227
228228 rv = dsa_sign_hash(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
229229 &self->yarrow_prng_state, self->yarrow_prng_index,
230230 &self->key);
243243 STRLEN data_len=0;
244244 unsigned char *sig_ptr=NULL;
245245 STRLEN sig_len=0;
246
246
247247 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
248248 sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len);
249
249
250250 RETVAL = 1;
251251 rv = dsa_verify_hash(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &stat, &self->key);
252252 if (rv != CRYPT_OK || stat != 1) RETVAL = 0;
33 _new()
44 CODE:
55 {
6 int rv;
6 int rv;
77 Newz(0, RETVAL, 1, struct ecc_struct);
88 if (!RETVAL) croak("FATAL: Newz failed");
99 RETVAL->key.type = -1;
3434 int rv;
3535 unsigned char *data=NULL;
3636 STRLEN data_len=0;
37
37
3838 data = (unsigned char *)SvPVbyte(key_data, data_len);
3939 ecc_free(&self->key);
4040 rv = ecc_import(data, (unsigned long)data_len, &self->key);
5151 int rv;
5252 unsigned char *data=NULL;
5353 STRLEN data_len=0;
54
54
5555 data = (unsigned char *)SvPVbyte(key_data, data_len);
5656 ecc_free(&self->key);
5757 rv = ecc_ansi_x963_import(data, (unsigned long)data_len, &self->key);
8989 /* =====> k */
9090 siz = (self->key.k) ? ltc_mp.unsigned_size(self->key.k) : 0;
9191 if (siz>10000) {
92 croak("FATAL: key2hash failed - 'k' too big number");
92 croak("FATAL: key2hash failed - 'k' too big number");
9393 }
9494 if (siz>0) {
9595 mp_tohex(self->key.k, buf);
101101 /* =====> pub_x */
102102 siz = (self->key.pubkey.x) ? ltc_mp.unsigned_size(self->key.pubkey.x) : 0;
103103 if (siz>10000) {
104 croak("FATAL: key2hash failed - 'pub_x' too big number");
104 croak("FATAL: key2hash failed - 'pub_x' too big number");
105105 }
106106 if (siz>0) {
107107 mp_tohex(self->key.pubkey.x, buf);
113113 /* =====> pub_y */
114114 siz = (self->key.pubkey.y) ? ltc_mp.unsigned_size(self->key.pubkey.y) : 0;
115115 if (siz>10000) {
116 croak("FATAL: key2hash failed - 'pub_y' too big number");
116 croak("FATAL: key2hash failed - 'pub_y' too big number");
117117 }
118118 if (siz>0) {
119119 mp_tohex(self->key.pubkey.y, buf);
125125 /* =====> pub_z */
126126 siz = (self->key.pubkey.z) ? ltc_mp.unsigned_size(self->key.pubkey.z) : 0;
127127 if (siz>10000) {
128 croak("FATAL: key2hash failed - 'pub_z' too big number");
128 croak("FATAL: key2hash failed - 'pub_z' too big number");
129129 }
130130 if (siz>0) {
131131 mp_tohex(self->key.pubkey.z, buf);
159159 int rv;
160160 unsigned char out[4096];
161161 unsigned long int out_len = 4096;
162
162
163163 RETVAL = newSVpvn(NULL, 0); /* undef */
164164 if (strnEQ(type, "private", 7)) {
165165 rv = ecc_export(out, &out_len, PK_PRIVATE, &self->key);
185185 int rv;
186186 unsigned char out[4096];
187187 unsigned long int out_len = 4096;
188
188
189189 rv = ecc_ansi_x963_export(&self->key, out, &out_len);
190190 if (rv != CRYPT_OK) croak("FATAL: ecc_ansi_x963_export failed: %s", error_to_string(rv));
191191 RETVAL = newSVpvn((char*)out, out_len);
202202 STRLEN data_len=0;
203203 unsigned char buffer[1024];
204204 unsigned long buffer_len = 1024;
205
205
206206 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
207
207
208208 hash_id = find_hash(hash_name);
209209 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
210210 rv = ecc_encrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
225225 STRLEN data_len=0;
226226 unsigned char buffer[1024];
227227 unsigned long buffer_len = 1024;
228
228
229229 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
230230
231231 rv = ecc_decrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &self->key);
244244 STRLEN data_len=0;
245245 unsigned char buffer[1024];
246246 unsigned long buffer_len = 1024;
247
247
248248 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
249
249
250250 rv = ecc_sign_hash(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
251251 &self->yarrow_prng_state, self->yarrow_prng_index,
252252 &self->key);
265265 STRLEN data_len=0;
266266 unsigned char *sig_ptr=NULL;
267267 STRLEN sig_len=0;
268
268
269269 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
270270 sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len);
271
271
272272 RETVAL = 1;
273273 rv = ecc_verify_hash(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &stat, &self->key);
274274 if (rv != CRYPT_OK || stat != 1) RETVAL = 0;
33 _new()
44 CODE:
55 {
6 int rv;
6 int rv;
77 Newz(0, RETVAL, 1, struct rsa_struct);
88 if (!RETVAL) croak("FATAL: Newz failed");
99 RETVAL->key.type = -1;
3535 int rv;
3636 unsigned char *data=NULL;
3737 STRLEN data_len=0;
38
38
3939 data = (unsigned char *)SvPVbyte(key_data, data_len);
4040 rsa_free(&self->key);
4141 rv = rsa_import(data, (unsigned long)data_len, &self->key);
7373 /* =====> e */
7474 siz = (self->key.e) ? ltc_mp.unsigned_size(self->key.e) : 0;
7575 if (siz>10000) {
76 croak("FATAL: key2hash failed - 'e' too big number");
76 croak("FATAL: key2hash failed - 'e' too big number");
7777 }
7878 if (siz>0) {
7979 mp_tohex(self->key.e, buf);
8585 /* =====> d */
8686 siz = (self->key.d) ? ltc_mp.unsigned_size(self->key.d) : 0;
8787 if (siz>10000) {
88 croak("FATAL: key2hash failed - 'd' too big number");
88 croak("FATAL: key2hash failed - 'd' too big number");
8989 }
9090 if (siz>0) {
9191 mp_tohex(self->key.d, buf);
9797 /* =====> N */
9898 siz = (self->key.N) ? ltc_mp.unsigned_size(self->key.N) : 0;
9999 if (siz>10000) {
100 croak("FATAL: key2hash failed - 'N' too big number");
100 croak("FATAL: key2hash failed - 'N' too big number");
101101 }
102102 if (siz>0) {
103103 mp_tohex(self->key.N, buf);
109109 /* =====> q */
110110 siz = (self->key.q) ? ltc_mp.unsigned_size(self->key.q) : 0;
111111 if (siz>10000) {
112 croak("FATAL: key2hash failed - 'q' too big number");
112 croak("FATAL: key2hash failed - 'q' too big number");
113113 }
114114 if (siz>0) {
115115 mp_tohex(self->key.q, buf);
121121 /* =====> p */
122122 siz = (self->key.p) ? ltc_mp.unsigned_size(self->key.p) : 0;
123123 if (siz>10000) {
124 croak("FATAL: key2hash failed - 'p' too big number");
124 croak("FATAL: key2hash failed - 'p' too big number");
125125 }
126126 if (siz>0) {
127127 mp_tohex(self->key.p, buf);
133133 /* =====> qP */
134134 siz = (self->key.qP) ? ltc_mp.unsigned_size(self->key.qP) : 0;
135135 if (siz>10000) {
136 croak("FATAL: key2hash failed - 'qP' too big number");
136 croak("FATAL: key2hash failed - 'qP' too big number");
137137 }
138138 if (siz>0) {
139139 mp_tohex(self->key.qP, buf);
145145 /* =====> dP */
146146 siz = (self->key.dP) ? ltc_mp.unsigned_size(self->key.dP) : 0;
147147 if (siz>10000) {
148 croak("FATAL: key2hash failed - 'dP' too big number");
148 croak("FATAL: key2hash failed - 'dP' too big number");
149149 }
150150 if (siz>0) {
151151 mp_tohex(self->key.dP, buf);
157157 /* =====> dQ */
158158 siz = (self->key.dQ) ? ltc_mp.unsigned_size(self->key.dQ) : 0;
159159 if (siz>10000) {
160 croak("FATAL: key2hash failed - 'dQ' too big number");
160 croak("FATAL: key2hash failed - 'dQ' too big number");
161161 }
162162 if (siz>0) {
163163 mp_tohex(self->key.dQ, buf);
181181 int rv;
182182 unsigned char out[4096];
183183 unsigned long int out_len = 4096;
184
184
185185 RETVAL = newSVpvn(NULL, 0); /* undef */
186186 if (strnEQ(type, "private", 7)) {
187187 rv = rsa_export(out, &out_len, PK_PRIVATE, &self->key);
211211 STRLEN data_len=0;
212212 unsigned char buffer[1024];
213213 unsigned long buffer_len = 1024;
214
214
215215 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
216
216
217217 RETVAL = newSVpvn(NULL, 0); /* undef */
218218 if (strnEQ(padding, "oaep", 4)) {
219219 hash_id = find_hash(oaep_hash);
256256 STRLEN data_len=0;
257257 unsigned char buffer[1024];
258258 unsigned long buffer_len = 1024;
259
259
260260 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
261
261
262262 RETVAL = newSVpvn(NULL, 0); /* undef */
263263 if (strnEQ(padding, "oaep", 4)) {
264264 hash_id = find_hash(oaep_hash);
299299 STRLEN data_len=0;
300300 unsigned char buffer[1024];
301301 unsigned long buffer_len = 1024;
302
302
303303 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
304
304
305305 RETVAL = newSVpvn(NULL, 0); /* undef */
306306 hash_id = find_hash(hash_name);
307307 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
335335 STRLEN data_len=0;
336336 unsigned char *sig_ptr=NULL;
337337 STRLEN sig_len=0;
338
338
339339 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
340340 sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len);
341
341
342342 RETVAL = 1;
343343 hash_id = find_hash(hash_name);
344344 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
1010
1111 Newz(0, RETVAL, 1, struct prng_struct);
1212 if (!RETVAL) croak("FATAL: Newz failed");
13
13
1414 id = find_prng(prng_name);
1515 if(id==-1) croak("FATAL: find_prng failed for '%s'", prng_name);
1616 RETVAL->id = id;
2525 rv = RETVAL->desc->add_entropy(ent, (unsigned long)ent_len, &RETVAL->state);
2626 if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv));
2727 }
28 else {
28 else {
2929 if (rng_get_bytes(entropy_buf, 32, NULL) != 32) croak("FATAL: rng_get_bytes failed: %s", error_to_string(rv));
3030 rv = RETVAL->desc->add_entropy(entropy_buf, 32, &RETVAL->state);
3131 if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv));
7272 int rv_len;
7373 unsigned char *rdata;
7474 unsigned char entropy_buf[32];
75
75
7676 if (self->last_pid != curpid) {
7777 rng_get_bytes(entropy_buf, 32, NULL);
7878 self->desc->add_entropy(entropy_buf, 32, &self->state);
7979 self->desc->ready(&self->state);
8080 self->last_pid = curpid;
8181 }
82
82
8383 RETVAL = NEWSV(0, output_len);
8484 SvPOK_only(RETVAL);
8585 SvCUR_set(RETVAL, output_len);