deriving keys (kdf) with crypt::cbc

From: gnu valued customer (tlviewer_at_yahoo.com)
Date: 02/26/04


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



Relevant Pages

  • Re: Noob! Help required and would be really appreciated!
    ... You've read 60% of "Learning Perl"? ... On finding the keyword "New Order", it has to start building a hash. ... want to build a loop around this hash creation. ... transferred to %allOrders. ...
    (comp.lang.perl.misc)
  • Re: Passing an array to a sub routine
    ... but later you're using a hash. ... In Perl, these are very ... The LoadDataFile routine works correctly, ...
    (comp.lang.perl.misc)
  • Making Datatypes Constant and Emulating Const Correctness
    ... Being relatively new to Perl with Java and bits of C under my belt, ... I'm new to C+++, too, but remember a number of uses for the const keyword ... an array to some analizer function as a constant strongly signals that no ... some reference (e,g. a blessed hash reference) ...
    (comp.lang.perl.misc)
  • Re: taking references to functions
    ... > I'm building a diagnostic medical questionnaire using Perl Tk. ... > to create a reference to a named function but not for unnamed ones. ... Notice that %dispatch here is an actual hash variable. ...
    (perl.beginners)
  • Re: arrange form data in same order as on form
    ... In Perl, f you get your submitted name/value pairs from the ... module as a "hash", then of course the ordering has been lost by then. ... the element, now deprecated, from earlier versions of HTML). ... their scripts were easier to write, ...
    (comp.lang.perl.misc)