Package list libcryptx-perl / 2466727
adopt to the new libtomcrypt Karel Miko 3 years ago
5 changed file(s) with 35 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
4949 if (self->key.type != -1) { ecc_free(&self->key); self->key.type = -1; }
5050 rv = ecc_import_openssl(data, (unsigned long)data_len, &self->key);
5151 if (rv != CRYPT_OK) croak("FATAL: ecc_import_openssl failed: %s", error_to_string(rv));
52 XPUSHs(ST(0)); /* return self */
53 }
54
55 void
56 _import_old(Crypt::PK::ECC self, SV * key_data)
57 PPCODE:
58 {
59 int rv;
60 unsigned char *data=NULL;
61 STRLEN data_len=0;
62
63 data = (unsigned char *)SvPVbyte(key_data, data_len);
64 if (self->key.type != -1) { ecc_free(&self->key); self->key.type = -1; }
65 rv = ecc_import(data, (unsigned long)data_len, &self->key);
66 if (rv != CRYPT_OK) croak("FATAL: ecc_import failed: %s", error_to_string(rv));
5267 XPUSHs(ST(0)); /* return self */
5368 }
5469
215215 my ($cipher, undef, $klen, $mode) = $cipher_name =~ /^(AES|CAMELLIA|DES|DES-EDE3|SEED)(-(\d+))?-(CBC|CFB|ECB|OFB)$/i;
216216 croak "FATAL: unsupported cipher '$cipher_name'" unless $cipher && $mode;
217217 $cipher = $trans{$cipher} || $cipher;
218 $klen = 192 if $cipher eq 'DES_EDE';
219 $klen = 64 if $cipher eq 'DES';
220 $klen = 128 if $cipher eq 'SEED';
218221 $klen = $klen ? int($klen/8) : Crypt::Cipher::min_keysize($cipher);
219222 my $ilen = Crypt::Cipher::blocksize($cipher);
220223 croak "FATAL: unsupported cipher '$cipher_name'" unless $klen && $ilen;
215215
216216 if ($data =~ /-----BEGIN (EC PRIVATE|EC PUBLIC|PUBLIC) KEY-----(.*?)-----END/sg) {
217217 $data = pem_to_der($data, $password);
218 return $self->_import($data);
218 my $rv = eval { $self->_import($data) } || eval { $self->_import_old($data) };
219 return $rv if $rv;
219220 }
220221 elsif ($data =~ /-----BEGIN PRIVATE KEY-----(.*?)-----END/sg) {
221222 $data = pem_to_der($data, $password);
252253 return $self->import_key_raw($pubkey, "$2") if $pubkey && $typ =~ /^ecdsa-(.+?)-(.*)$/;
253254 }
254255 else {
255 my $rv = eval { $self->_import($data) } || eval { $self->_import_pkcs8($data, $password) } || eval { $self->_import_x509($data) };
256 my $rv = eval { $self->_import($data) } ||
257 eval { $self->_import_old($data) } ||
258 eval { $self->_import_pkcs8($data, $password) } ||
259 eval { $self->_import_x509($data) };
256260 return $rv if $rv;
257261 }
258262 croak "FATAL: invalid or unsupported EC key format";
99 our @EXPORT = qw();
1010
1111 use Carp;
12 $Carp::Internal{(__PACKAGE__)}++;
1213 use CryptX;
1314 use Crypt::Digest qw(digest_data digest_data_b64u);
1415 use Crypt::Misc qw(read_rawfile encode_b64u decode_b64u encode_b64 decode_b64 pem_to_der der_to_pem);
2122
2223 sub export_key_pem {
2324 my ($self, $type, $password, $cipher) = @_;
25 local $SIG{__DIE__} = \&CryptX::_croak;
2426 my $key = $self->export_key_der($type||'');
2527 return unless $key;
2628
3739
3840 sub export_key_jwk {
3941 my ($self, $type, $wanthash) = @_;
42 local $SIG{__DIE__} = \&CryptX::_croak;
4043 my $kh = $self->key2hash;
4144 if ($type eq 'private') {
4245 return unless $kh->{N} && $kh->{e} && $kh->{d} && $kh->{p} && $kh->{q} && $kh->{dP} && $kh->{dQ} && $kh->{qP};
7275
7376 sub export_key_jwk_thumbprint {
7477 my ($self, $hash_name) = @_;
78 local $SIG{__DIE__} = \&CryptX::_croak;
7579 $hash_name ||= 'SHA256';
7680 my $h = $self->export_key_jwk('public', 1);
7781 my $json = CryptX::_encode_json({kty=>$h->{kty}, n=>$h->{n}, e=>$h->{e}});
8084
8185 sub import_key {
8286 my ($self, $key, $password) = @_;
87 local $SIG{__DIE__} = \&CryptX::_croak;
8388 croak "FATAL: undefined key" unless $key;
8489
8590 # special case
1010 is( Crypt::Cipher::DES_EDE::blocksize, 8, '::blocksize');
1111 is( Crypt::Cipher::DES_EDE::keysize, 24, '::keysize');
1212 is( Crypt::Cipher::DES_EDE::max_keysize, 24, '::max_keysize');
13 is( Crypt::Cipher::DES_EDE::min_keysize, 24, '::min_keysize');
13 is( Crypt::Cipher::DES_EDE::min_keysize, 16, '::min_keysize');
1414 is( Crypt::Cipher::DES_EDE::default_rounds, 16, '::default_rounds');
1515
1616 is( Crypt::Cipher::DES_EDE->blocksize, 8, '->blocksize');
1717 is( Crypt::Cipher::DES_EDE->keysize, 24, '->keysize');
1818 is( Crypt::Cipher::DES_EDE->max_keysize, 24, '->max_keysize');
19 is( Crypt::Cipher::DES_EDE->min_keysize, 24, '->min_keysize');
19 is( Crypt::Cipher::DES_EDE->min_keysize, 16, '->min_keysize');
2020 is( Crypt::Cipher::DES_EDE->default_rounds, 16, '->default_rounds');
2121
2222 my $min_key = 'kkkkkkkkkkkkkkkkkkkkkkkk';
2525 is( Crypt::Cipher::blocksize('DES_EDE'), 8, 'Cipher->blocksize');
2626 is( Crypt::Cipher::keysize('DES_EDE'), 24, 'Cipher->keysize');
2727 is( Crypt::Cipher::max_keysize('DES_EDE'), 24, 'Cipher->max_keysize');
28 is( Crypt::Cipher::min_keysize('DES_EDE'), 24, 'Cipher->min_keysize');
28 is( Crypt::Cipher::min_keysize('DES_EDE'), 16, 'Cipher->min_keysize');
2929 is( Crypt::Cipher::default_rounds('DES_EDE'), 16, 'Cipher->default_rounds');
3030
3131 is( Crypt::Cipher->blocksize('DES_EDE'), 8, 'Cipher->blocksize');
3232 is( Crypt::Cipher->keysize('DES_EDE'), 24, 'Cipher->keysize');
3333 is( Crypt::Cipher->max_keysize('DES_EDE'), 24, 'Cipher->max_keysize');
34 is( Crypt::Cipher->min_keysize('DES_EDE'), 24, 'Cipher->min_keysize');
34 is( Crypt::Cipher->min_keysize('DES_EDE'), 16, 'Cipher->min_keysize');
3535 is( Crypt::Cipher->default_rounds('DES_EDE'), 16, 'Cipher->default_rounds');
3636
3737 is( Crypt::Cipher::DES_EDE->new($min_key)->blocksize, 8, 'DES_EDE->new()->blocksize');
3838 is( Crypt::Cipher::DES_EDE->new($min_key)->keysize, 24, 'DES_EDE->new()->keysize');
3939 is( Crypt::Cipher::DES_EDE->new($min_key)->max_keysize, 24, 'DES_EDE->new()->max_keysize');
40 is( Crypt::Cipher::DES_EDE->new($min_key)->min_keysize, 24, 'DES_EDE->new()->min_keysize');
40 is( Crypt::Cipher::DES_EDE->new($min_key)->min_keysize, 16, 'DES_EDE->new()->min_keysize');
4141 is( Crypt::Cipher::DES_EDE->new($min_key)->default_rounds, 16, 'DES_EDE->new()->default_rounds');
4242
4343 is( Crypt::Cipher->new('DES_EDE', $min_key)->blocksize, 8, 'Cipher->new()->blocksize');
4444 is( Crypt::Cipher->new('DES_EDE', $min_key)->keysize, 24, 'Cipher->new()->keysize');
4545 is( Crypt::Cipher->new('DES_EDE', $min_key)->max_keysize, 24, 'Cipher->new()->max_keysize');
46 is( Crypt::Cipher->new('DES_EDE', $min_key)->min_keysize, 24, 'Cipher->new()->min_keysize');
46 is( Crypt::Cipher->new('DES_EDE', $min_key)->min_keysize, 16, 'Cipher->new()->min_keysize');
4747 is( Crypt::Cipher->new('DES_EDE', $min_key)->default_rounds, 16, 'Cipher->new()->default_rounds');
4848
4949 my $block_plain = 'BBBBBBBB';