From 0fb25e59e825cdd996384a4571f5ac3a308b4784 Mon Sep 17 00:00:00 2001 From: Karel Miko Date: Mon, 4 Jun 2018 16:52:29 +0200 Subject: [PATCH] adopt to the new libtomcrypt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Petr Písař: Ported from CryptX-0.61: commit 2466727cd0603184442ba7c8251e64674c6731a7 Author: Karel Miko Date: Mon Jun 4 16:52:29 2018 +0200 adopt to the new libtomcrypt ECC support removed. Signed-off-by: Petr Písař --- inc/CryptX_PK_ECC.xs.inc | 15 +++++++++++++++ lib/Crypt/Misc.pm | 3 +++ lib/Crypt/PK/RSA.pm | 5 +++++ t/cipher_des_ede.t | 12 ++++++------ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/inc/CryptX_PK_ECC.xs.inc b/inc/CryptX_PK_ECC.xs.inc index 2ecbd3c..a3afa69 100644 --- a/inc/CryptX_PK_ECC.xs.inc +++ b/inc/CryptX_PK_ECC.xs.inc @@ -45,6 +45,21 @@ _import(Crypt::PK::ECC self, SV * key_data) XPUSHs(ST(0)); /* return self */ } +void +_import_old(Crypt::PK::ECC self, SV * key_data) + PPCODE: + { + int rv; + unsigned char *data=NULL; + STRLEN data_len=0; + + data = (unsigned char *)SvPVbyte(key_data, data_len); + if (self->key.type != -1) { ecc_free(&self->key); self->key.type = -1; } + rv = ecc_import(data, (unsigned long)data_len, &self->key); + if (rv != CRYPT_OK) croak("FATAL: ecc_import failed: %s", error_to_string(rv)); + XPUSHs(ST(0)); /* return self */ + } + void _import_pkcs8(Crypt::PK::ECC self, SV * key_data, SV * passwd) PPCODE: diff --git a/lib/Crypt/Misc.pm b/lib/Crypt/Misc.pm index 9bd0223..523490c 100644 --- a/lib/Crypt/Misc.pm +++ b/lib/Crypt/Misc.pm @@ -252,6 +252,9 @@ sub _name2mode { my ($cipher, undef, $klen, $mode) = $cipher_name =~ /^(AES|CAMELLIA|DES|DES-EDE3|SEED)(-(\d+))?-(CBC|CFB|ECB|OFB)$/i; croak "FATAL: unsupported cipher '$cipher_name'" unless $cipher && $mode; $cipher = $trans{$cipher} || $cipher; + $klen = 192 if $cipher eq 'DES_EDE'; + $klen = 64 if $cipher eq 'DES'; + $klen = 128 if $cipher eq 'SEED'; $klen = $klen ? int($klen/8) : Crypt::Cipher::min_keysize($cipher); my $ilen = Crypt::Cipher::blocksize($cipher); croak "FATAL: unsupported cipher '$cipher_name'" unless $klen && $ilen; diff --git a/lib/Crypt/PK/RSA.pm b/lib/Crypt/PK/RSA.pm index 69e1c1f..a0be518 100644 --- a/lib/Crypt/PK/RSA.pm +++ b/lib/Crypt/PK/RSA.pm @@ -10,6 +10,7 @@ our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw(); use Carp; +$Carp::Internal{(__PACKAGE__)}++; use CryptX qw(_encode_json _decode_json); use Crypt::Digest qw(digest_data digest_data_b64u); use Crypt::Misc qw(read_rawfile encode_b64u decode_b64u encode_b64 decode_b64 pem_to_der der_to_pem); @@ -24,6 +25,7 @@ sub new { sub export_key_pem { my ($self, $type, $password, $cipher) = @_; + local $SIG{__DIE__} = \&CryptX::_croak; my $key = $self->export_key_der($type||''); return unless $key; @@ -40,6 +42,7 @@ sub export_key_pem { sub export_key_jwk { my ($self, $type, $wanthash) = @_; + local $SIG{__DIE__} = \&CryptX::_croak; my $kh = $self->key2hash; if ($type eq 'private') { return unless $kh->{N} && $kh->{e} && $kh->{d} && $kh->{p} && $kh->{q} && $kh->{dP} && $kh->{dQ} && $kh->{qP}; @@ -75,6 +78,7 @@ sub export_key_jwk { sub export_key_jwk_thumbprint { my ($self, $hash_name) = @_; + local $SIG{__DIE__} = \&CryptX::_croak; $hash_name ||= 'SHA256'; my $h = $self->export_key_jwk('public', 1); my $json = _encode_json({kty=>$h->{kty}, n=>$h->{n}, e=>$h->{e}}); @@ -83,6 +87,7 @@ sub export_key_jwk_thumbprint { sub import_key { my ($self, $key, $password) = @_; + local $SIG{__DIE__} = \&CryptX::_croak; croak "FATAL: undefined key" unless $key; # special case diff --git a/t/cipher_des_ede.t b/t/cipher_des_ede.t index cfd8d68..b8134db 100644 --- a/t/cipher_des_ede.t +++ b/t/cipher_des_ede.t @@ -11,13 +11,13 @@ use Crypt::Cipher::DES_EDE; is( Crypt::Cipher::DES_EDE::blocksize, 8, '::blocksize'); is( Crypt::Cipher::DES_EDE::keysize, 24, '::keysize'); is( Crypt::Cipher::DES_EDE::max_keysize, 24, '::max_keysize'); -is( Crypt::Cipher::DES_EDE::min_keysize, 24, '::min_keysize'); +is( Crypt::Cipher::DES_EDE::min_keysize, 16, '::min_keysize'); is( Crypt::Cipher::DES_EDE::default_rounds, 16, '::default_rounds'); is( Crypt::Cipher::DES_EDE->blocksize, 8, '->blocksize'); is( Crypt::Cipher::DES_EDE->keysize, 24, '->keysize'); is( Crypt::Cipher::DES_EDE->max_keysize, 24, '->max_keysize'); -is( Crypt::Cipher::DES_EDE->min_keysize, 24, '->min_keysize'); +is( Crypt::Cipher::DES_EDE->min_keysize, 16, '->min_keysize'); is( Crypt::Cipher::DES_EDE->default_rounds, 16, '->default_rounds'); my $min_key = 'kkkkkkkkkkkkkkkkkkkkkkkk'; @@ -26,25 +26,25 @@ my $max_key = 'KKKKKKKKKKKKKKKKKKKKKKKK'; is( Crypt::Cipher::blocksize('DES_EDE'), 8, 'Cipher->blocksize'); is( Crypt::Cipher::keysize('DES_EDE'), 24, 'Cipher->keysize'); is( Crypt::Cipher::max_keysize('DES_EDE'), 24, 'Cipher->max_keysize'); -is( Crypt::Cipher::min_keysize('DES_EDE'), 24, 'Cipher->min_keysize'); +is( Crypt::Cipher::min_keysize('DES_EDE'), 16, 'Cipher->min_keysize'); is( Crypt::Cipher::default_rounds('DES_EDE'), 16, 'Cipher->default_rounds'); is( Crypt::Cipher->blocksize('DES_EDE'), 8, 'Cipher->blocksize'); is( Crypt::Cipher->keysize('DES_EDE'), 24, 'Cipher->keysize'); is( Crypt::Cipher->max_keysize('DES_EDE'), 24, 'Cipher->max_keysize'); -is( Crypt::Cipher->min_keysize('DES_EDE'), 24, 'Cipher->min_keysize'); +is( Crypt::Cipher->min_keysize('DES_EDE'), 16, 'Cipher->min_keysize'); is( Crypt::Cipher->default_rounds('DES_EDE'), 16, 'Cipher->default_rounds'); is( Crypt::Cipher::DES_EDE->new($min_key)->blocksize, 8, 'DES_EDE->new()->blocksize'); is( Crypt::Cipher::DES_EDE->new($min_key)->keysize, 24, 'DES_EDE->new()->keysize'); is( Crypt::Cipher::DES_EDE->new($min_key)->max_keysize, 24, 'DES_EDE->new()->max_keysize'); -is( Crypt::Cipher::DES_EDE->new($min_key)->min_keysize, 24, 'DES_EDE->new()->min_keysize'); +is( Crypt::Cipher::DES_EDE->new($min_key)->min_keysize, 16, 'DES_EDE->new()->min_keysize'); is( Crypt::Cipher::DES_EDE->new($min_key)->default_rounds, 16, 'DES_EDE->new()->default_rounds'); is( Crypt::Cipher->new('DES_EDE', $min_key)->blocksize, 8, 'Cipher->new()->blocksize'); is( Crypt::Cipher->new('DES_EDE', $min_key)->keysize, 24, 'Cipher->new()->keysize'); is( Crypt::Cipher->new('DES_EDE', $min_key)->max_keysize, 24, 'Cipher->new()->max_keysize'); -is( Crypt::Cipher->new('DES_EDE', $min_key)->min_keysize, 24, 'Cipher->new()->min_keysize'); +is( Crypt::Cipher->new('DES_EDE', $min_key)->min_keysize, 16, 'Cipher->new()->min_keysize'); is( Crypt::Cipher->new('DES_EDE', $min_key)->default_rounds, 16, 'Cipher->new()->default_rounds'); my $block_plain = 'BBBBBBBB'; -- 2.17.2