deriving keys (kdf) with crypt::cbc
From: gnu valued customer (tlviewer_at_yahoo.com)
Date: 02/26/04
- Next message: John E. Pannell: "Re: Using GD::Graph::xylines, xylinespoints..."
- Previous message: Jean-Louis MOREL: "Re: OpenGL perl module on Windows?"
- Next in thread: gnu valued customer: "Re: deriving keys (kdf) with crypt::cbc"
- Reply: gnu valued customer: "Re: deriving keys (kdf) with crypt::cbc"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 26 Feb 2004 05:31:48 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello Perl coders,
Using Win2k sp3
Perl 5.8.1
Apache 2.0.48
OpenSSL 0.9.7c
editing with UltraEdit 8.20a
I'm proposing a module in the namespace
Crypt::CBC::KDF, to help simpify interoperations
between Perl and certain crypto libraries (CL).
KDF - key derivation function
The module should accomodate a variety of hashing
functions:
sha1 (160 bits)
md5 (128 bits)
Digest::SHA (by Mark Shelor)
sha192
sha256
Chime in with any CLs
or RFCs that you want included that I might leave out.
First few that came to mind:
TLS - rfc 2246
OpenSSL - (Blowfish and DES_ECE3)
Win32 CryptoAPI - (AES and 3DES)
(add yours)
KDF examples of interest involve
key lengths greater than 16 and are never
only a simple digest.
In the process of interoperating 3DES ciphering
between the Win32 CryptoAPI and Perl, I came
up with Perl code that will derive a DES_EDE3 key
from a password such that the cipher text is identical
to that returned by the Win32 CryptAPI, when starting
with the same passphrase. Pardon me for that last
sentence.
# sample InterOp code
# see the MSDN SDK for example CryptAPI C code
use Crypt::CBC;
use warnings;
# classic 160 bit one-way hash
use Digest::SHA1 qw(sha1); #
use MIME::Base64;
# Blowfish:56 Rijndael:(16,24,32) CAST5_PP:16 Twofish:16
my $skey = KeyDerive("124-Kelp", 24 );
print unpack("H*", $skey), "\n";
# derived key is
# 5ab48b8def0bdca77f16f8c4f4781823e92ecc1c4d40f762
#
my $cipher = Crypt::CBC->new(
{'key' => $skey,
'cipher' => 'DES_EDE3',
'iv' => 'AAAAAAAA',
'regenerate_key' => 0, # we control the key
'padding' => 'standard', # pkcs5
'prepend_iv' => 0
});
my $all = 'MS says open source is cancer.' ;
my $ciphertext = $cipher->encrypt($all);
print "len ct=", length($ciphertext), "\n";
print encode_base64($ciphertext), "\n";
# YEiDFLljDZGXx+0TutG15wGqwPKk+Czv0sKppPKs+o0=
exit(0);
# helper subs
##################
sub KeyDerive {
my ($key, $klen) = @_;
$hash = sha1( $key);
my $inner = join('',hmac( chr(0x36), $hash));
my $outer = join('',hmac( chr(0x5C), $hash ));
return substr( sha1($inner) . sha1($outer), 0, $klen);
}
sub hmac {
($mask, $in ) = @_;
my @hmac = split //, $mask x 64;
my @in = split //, $in;
for my $n (0 .. length($in)-1) {
$hmac[$n] = $hmac[$n] ^ $in[$n];
}
#
return @hmac;
}
__END__
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (MingW32)
iD8DBQFAPYPD6ol16hqWbsURAufwAKDeWyUfvci23glKQtobR20Cqv81pACghT9V
E60sjld6WywYUczFOyMWObE=
=fCw9
-----END PGP SIGNATURE-----
Mark Pryor
tlviewer:AIM
- Next message: John E. Pannell: "Re: Using GD::Graph::xylines, xylinespoints..."
- Previous message: Jean-Louis MOREL: "Re: OpenGL perl module on Windows?"
- Next in thread: gnu valued customer: "Re: deriving keys (kdf) with crypt::cbc"
- Reply: gnu valued customer: "Re: deriving keys (kdf) with crypt::cbc"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|