From 1047146b2351d56ea3b73dfb5d7b180d2280f0d4 Mon Sep 17 00:00:00 2001 From: Damien Stuart Date: Wed, 4 Sep 2013 15:19:43 -0400 Subject: [PATCH] Updated Perl FKO tests for lastest changes to libfko. Fixed bug where $fko->hmac() was always returning -1. --- perl/FKO/FKO.xs | 1 + perl/FKO/lib/FKO.pm | 27 +++------ perl/FKO/t/02_functions.t | 115 +++++++++++++++++++++++++------------- 3 files changed, 87 insertions(+), 56 deletions(-) diff --git a/perl/FKO/FKO.xs b/perl/FKO/FKO.xs index 3eaa9ba3..d973c668 100644 --- a/perl/FKO/FKO.xs +++ b/perl/FKO/FKO.xs @@ -197,6 +197,7 @@ _get_hmac_type(ctx, hmac_type) CODE: RETVAL = fko_get_spa_hmac_type(ctx, &hmac_type); OUTPUT: + hmac_type RETVAL int diff --git a/perl/FKO/lib/FKO.pm b/perl/FKO/lib/FKO.pm index a0c01742..15ff3a88 100644 --- a/perl/FKO/lib/FKO.pm +++ b/perl/FKO/lib/FKO.pm @@ -74,10 +74,8 @@ sub new { my $class = shift; my $data = shift; my $dc_pw = shift; - my $dc_pw_len = shift; my $enc_mode = shift; - my $hmac_pw = shift; - my $hmac_pw_len = shift; + my $hmac_pw = shift || ''; my $hmac_type = shift; my $res; @@ -88,8 +86,8 @@ sub new { # if(defined($data) and $data) { if(defined($dc_pw)) { - $ctx = _init_ctx_with_data($data, $dc_pw, $dc_pw_len, - $enc_mode, $hmac_pw, $hmac_pw_len, $hmac_type); + $ctx = _init_ctx_with_data($data, $dc_pw, length($dc_pw), + $enc_mode, $hmac_pw, length($hmac_pw), $hmac_type); } else { $ctx = _init_ctx_with_data_only($data); } @@ -321,12 +319,11 @@ sub spa_hmac { my $self = shift; my $recompute = shift || 0; my $hmac_key = shift || ''; - my $hmac_key_len = shift || 0; my $val = ''; return FKO::_set_spa_hmac($self->{_ctx}) - if($recompute and $hmac_key and $hmac_key_len); + if($recompute and $hmac_key); $self->{_err} = FKO::_get_spa_hmac($self->{_ctx}, $val); @@ -479,27 +476,23 @@ sub encoded_data { sub spa_data_final { my $self = shift; my $key = shift || ''; - my $key_len = shift || 0; my $hmac_key = shift || ''; - my $hmac_key_len = shift || 0; - return FKO::_spa_data_final($self->{_ctx}, $key, $key_len, $hmac_key, $hmac_key_len); + return FKO::_spa_data_final($self->{_ctx}, $key, length($key), $hmac_key, length($hmac_key)); } sub encrypt_spa_data { my $self = shift; my $key = shift || ''; - my $key_len = shift || 0; - return FKO::_encrypt_spa_data($self->{_ctx}, $key, $key_len) + return FKO::_encrypt_spa_data($self->{_ctx}, $key, length($key)); } sub decrypt_spa_data { my $self = shift; my $key = shift || ''; - my $key_len = shift || 0; - return FKO::_decrypt_spa_data($self->{_ctx}, $key, $key_len) + return FKO::_decrypt_spa_data($self->{_ctx}, $key, length($key)); } sub encode_spa_data { @@ -515,17 +508,15 @@ sub decode_spa_data { sub verify_hmac { my $self = shift; my $hmac_key = shift || ''; - my $hmac_key_len = shift || 0; - return FKO::_verify_hmac($self->{_ctx}, $hmac_key, $hmac_key_len) + return FKO::_verify_hmac($self->{_ctx}, $hmac_key, length($hmac_key)); } sub set_spa_hmac { my $self = shift; my $hmac_key = shift || ''; - my $hmac_key_len = shift || 0; - return FKO::_set_spa_hmac($self->{_ctx}, $hmac_key, $hmac_key_len) + return FKO::_set_spa_hmac($self->{_ctx}, $hmac_key, length($hmac_key)); } sub DESTROY { diff --git a/perl/FKO/t/02_functions.t b/perl/FKO/t/02_functions.t index 43ebb04f..68ae384c 100644 --- a/perl/FKO/t/02_functions.t +++ b/perl/FKO/t/02_functions.t @@ -10,16 +10,15 @@ # use FKO; -#use Test::More tests => 96; -use Test::More tests => 14; +use Test::More tests => 533; # Test spa data support vars # my ( - $tsd, $tsd_pw, $tsd_pw_len, $tsd_hmac_key, $tsd_hmac_key_len, - $tsd_encryption_mode, $tsd_rand, $tsd_user, $tsd_time, $tsd_ver, - $tsd_msg_type, $tsd_msg, $tsd_nat_access, $tsd_server_auth, - $tsd_client_timeout, $tsd_digest, $tsd_encoded, $tsd_digest_type, + $tsd, $tsd_pw, $tsd_hmac_key, $tsd_encryption_mode, $tsd_rand, + $tsd_user, $tsd_time, $tsd_ver, $tsd_msg_type, $tsd_msg, + $tsd_nat_access, $tsd_server_auth, $tsd_client_timeout, + $tsd_digest, $tsd_encoded, $tsd_digest_type, $tsd_hmac_digest_type, $tsd_encryption_type ); @@ -35,8 +34,8 @@ my $def_tsd_msg = '0.0.0.0,tcp/22'; my $def_encryption_type = FKO::FKO_ENCRYPTION_RIJNDAEL; my $def_digest_type = FKO::FKO_DIGEST_SHA256; my $def_msg_type = FKO::FKO_ACCESS_MSG; -my $def_hmac_digest_type = FKO::FKO_HMAC_SHA256; -my $def_encrption_mode = FKO::FKO_ENC_MODE_ECB; +my $def_hmac_digest_type = FKO::FKO_HMAC_UNKNOWN; +my $def_encryption_mode = FKO::FKO_ENC_MODE_CBC; my $test_hmac_key = '0987654321test this is only a test'; @@ -77,6 +76,12 @@ ok($tsd_digest_type == $def_digest_type, 'default digest type'); $tsd_msg_type = $f1->spa_message_type(); ok($tsd_msg_type == $def_msg_type, 'default message type'); +$tsd_hmac_digest_type = $f1->hmac_type(); +ok($tsd_hmac_digest_type == $def_hmac_digest_type, 'HMAC digest type'); + +$tsd_encryption_mode = $f1->encryption_mode(); +ok($tsd_encryption_mode == $def_encryption_mode, 'Encryption mode'); + # 10-11 - set and verify username # $err = $f1->username($tuser); @@ -89,18 +94,15 @@ $err = $f1->spa_message($def_tsd_msg); ok($err == 0, 'set spa message'); ok($f1->spa_message() eq $def_tsd_msg, 'set spa message value'); -##--DSS -# Set the hmac digest stuff -$f1->hmac_type($test_hmac_type); -$f1->encryption_mode($test_encryption_mode); +# Set the hmac digest stuff (none here) +$thmac_key = ''; # 14 - Finalize the spa data (encode fields , compute digest, encrypt, # and encode all) # -$err = $f1->spa_data_final($tuser_pw, length($tuser_pw), $thmac_key, length($thmac_key)); +$err = $f1->spa_data_final($tuser_pw, $thmac_key); ok($err == 0, 'f1 spa data final'); -if(0) { # 15-16 - Get some of the current spa data for later tests. # $tsd = $f1->spa_data(); @@ -110,8 +112,8 @@ ok($tsd_digest, 'f1 get spa digest'); # 17 - create a new object based on the spa data produced by f1. # -my $f2 = FKO->new($tsd, $tuser_pw, length($tuser_pw), $f1->encryption_mode(), - $thmac_key, length($thmac_key), $test_hmac_type); +my $f2 = FKO->new($tsd, $tuser_pw, $f1->encryption_mode(), + $thmac_key, $def_hmac_digest_type); ok( $f2 ); # 18-31 - Ensure the f2 fields match the f1 fields @@ -126,32 +128,47 @@ ok($err == 0, 'f1 set digest to sha1'); is($f1->digest_type(), FKO::FKO_DIGEST_SHA1, 'verify set digest sha1'); ok($f1->timestamp(5) == 0, 'reset timestamp 1'); isnt($f1->timestamp(), $f2->timestamp(), 'verify new timestamp 1'); -ok($f1->spa_data_final('testme') == 0, 'f1 recompute spa data 1'); -my $f3 = FKO->new($f1->spa_data(), 'testme'); +ok($f1->spa_data_final($tuser_pw, $thmac_key) == 0, + 'f1 recompute spa data 1'); + +my $f3 = FKO->new($f1->spa_data(), $tuser_pw, $f1->encryption_mode(), + $thmac_key, $def_hmac_digest_type); ok($f3, 'create fko object f3'); # 38-51 - Compare f1 and f3 # compare_fko($f1, $f3, 'f1-f3'); -# 52-57 - Change digest_type and timestamp in f1 and recompute, then -# make a new fko object based on f1's spa_data. +# 52-57 - Change digest_type and timestamp in f1 and recompute, add an +# HMAC key, then make a new fko object based on f1's spa_data. # -$err = $f2->digest_type(FKO::FKO_DIGEST_MD5); -ok($err == 0, 'f1 set digest to md5'); -is($f2->digest_type(), FKO::FKO_DIGEST_MD5, 'verify set digest sha1'); -my $tts = $f2->timestamp(); -ok($f2->timestamp(10) == 0, 'reset timestamp 2'); -isnt($f2->timestamp(), $tts, 'verify new timestamp 2'); -ok($f2->spa_data_final('metest') == 0, 'f2 recompute spa data 1'); +$tuser_pw = 'metest'; +$thmac_key = 'This is a bogus hmac key - 1234567890'; +$tsd_hmac_digest_type = FKO::FKO_HMAC_SHA512; -my $f4 = FKO->new($f2->spa_data(), 'metest'); +$err = $f1->digest_type(FKO::FKO_DIGEST_MD5); +ok($err == 0, 'f1 set digest to md5'); +is($f1->digest_type(), FKO::FKO_DIGEST_MD5, 'verify set digest md5'); + +$err = $f1->hmac_type($tsd_hmac_digest_type); +ok($err == 0, 'f1 set set HMAC digest to sha512'); +is($f1->hmac_type(), $tsd_hmac_digest_type, 'verify set HMAC digest sha512'); + +my $tts = $f1->timestamp(); +ok($f1->timestamp(10) == 0, 'reset timestamp 2'); +isnt($f1->timestamp(), $tts, 'verify new timestamp 2'); + +ok($f1->spa_data_final($tuser_pw, $thmac_key) == 0, + 'f2 recompute spa data 1'); + +my $f4 = FKO->new($f1->spa_data(), $tuser_pw, $f1->encryption_mode(), + $thmac_key, $tsd_hmac_digest_type); ok($f4, 'create fko object f4'); # 58-71 - Compare f1 and f4 # -compare_fko($f1, $f3, 'f2-f4'); +compare_fko($f1, $f4, 'f1-f4'); # Clean up what we have so far # @@ -162,17 +179,17 @@ $f4->destroy(); ### General function tests. -# 72 - A fresh object to work with. +# A fresh object to work with. # $f1 = FKO->new(); ok($f1, 'Create f1 #2'); -# 73-74 - Force rand value. +# Force rand value. # ok($f1->rand_value('0123456789012345') == 0, 'force rand value'); is($f1->rand_value(), '0123456789012345', 'verify force rand_value'); -# 75-88 - Iterate over setting message type +# Iterate over setting message type # my @msg_types = ( FKO::FKO_COMMAND_MSG, @@ -189,27 +206,45 @@ foreach my $mt ( @msg_types ) { is($f1->spa_message_type(), $mt, "verify msg_type is $mt"); } -# 89-90 - SPA message +# SPA message # ok($f1->spa_message('1.1.1.1,udp/111') == 0, 'set spa message'); is($f1->spa_message(), '1.1.1.1,udp/111', 'verify spa message'); -# 91-92 - Nat Access +# Nat Access # ok($f1->spa_nat_access('1.2.1.1,211') == 0, 'set nat_access message'); is($f1->spa_nat_access(), '1.2.1.1,211', 'verify nat_access message'); -# 93-94 - Server Auth +# Server Auth # ok($f1->spa_server_auth('crypt,bubba') == 0, 'set server_auth message'); is($f1->spa_server_auth(), 'crypt,bubba', 'verify server_auth message'); -# 95-96 - Client Timeout +# Client Timeout # ok($f1->spa_client_timeout(666) == 0, 'set client_timeout'); is($f1->spa_client_timeout(), 666, 'verify client_timeout'); -#--DSS +# Now iterate over the various digest types and hmac digest types and +# Generate spa data for each. +# +# @DIGEST_TYPES, (1-5) +# @HMAC_DIGEST_TYPES, (1-5) +# @ENCRYPTION_TYPES, (1) +# @ENCRYPTION_MODES, (1-8) +foreach my $hmac_type (1..5) { + next if($hmac_type < 1); + ok($f1->hmac_type($hmac_type) == 0, "set HMAC type: $hmac_type"); + foreach my $digest_type (1..5) { + next if($digest_type < 1); + ok($f1->digest_type($digest_type) == 0, "set digest type: $digest_type"); + foreach my $enc_mode (1..8) { + ok($f1->encryption_mode($enc_mode) == 0, "set encryption mode: $enc_mode"); + ok($f1->spa_data_final($tuser_pw, $test_hmac_key) == 0, + "spad_data_final (HAMC:$hmac_type, DIGEST:$digest_type), ENC_MODE: $enc_mode"); + } + } } ############################################################################## @@ -233,7 +268,11 @@ sub compare_fko { is($fko1->spa_client_timeout(), $fko2->spa_client_timeout(), "$tn spa_client_timeout compare"); is($fko1->spa_digest(), $fko2->spa_digest(), "$tn spa_digest compare"); is($fko1->encoded_data(), $fko2->encoded_data(), "$tn encoded_data compare"); - is($fko1->spa_data(), $fko2->spa_data(), "$tn spa_data compare"); + is($fko1->hmac_type(), $fko2->hmac_type(), "$tn hmac_type compare"); + is($fko1->encryption_mode(), $fko2->encryption_mode(), "$tn encryption_mode compare"); + # Using fko->new_with_data() does not recreate the hmac and append it to + # spa data so we don't inlcude it in this check. + #is($fko1->spa_data(), $fko2->spa_data(), "$tn spa_data compare"); } sub create