From be3c356661507dc5d6dea152a8f8342e4ce0d387 Mon Sep 17 00:00:00 2001 From: Michael Rash Date: Fri, 13 May 2016 09:25:06 -0700 Subject: [PATCH] [libfko] account for SHA3_256 and SHA3_512 digest lengths matching SHA256 and SHA512 --- lib/fko_decode.c | 43 +++++++++++++++++++++++++++++++++- test/tests/basic_operations.pl | 10 ++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/fko_decode.c b/lib/fko_decode.c index 1afdba65..b259e172 100644 --- a/lib/fko_decode.c +++ b/lib/fko_decode.c @@ -107,6 +107,13 @@ verify_digest(char *tbuf, int t_size, fko_ctx_t ctx) sha512_base64(tbuf, (unsigned char*)ctx->encoded_msg, ctx->encoded_msg_len); break; + /* Note that we check SHA3_256 and SHA3_512 below because the + * digest lengths for these are the same as SHA256 and SHA512 + * respectively, and setting the digest type for an incoming + * decrypted SPA packet is done initially by looking at the + * length. + */ + default: /* Invalid or unsupported digest */ return(FKO_ERROR_INVALID_DIGEST_TYPE); } @@ -115,7 +122,39 @@ verify_digest(char *tbuf, int t_size, fko_ctx_t ctx) * digest in the message data. */ if(constant_runtime_cmp(ctx->digest, tbuf, t_size) != 0) - return(FKO_ERROR_DIGEST_VERIFICATION_FAILED); + { + /* Could potentially also have been SHA3_256 or SHA3_512 */ + if(ctx->digest_type == FKO_DIGEST_SHA256) + { + sha3_256_base64(tbuf, (unsigned char*)ctx->encoded_msg, ctx->encoded_msg_len); + if(constant_runtime_cmp(ctx->digest, tbuf, t_size) != 0) + { + return(FKO_ERROR_DIGEST_VERIFICATION_FAILED); + } + else + { + ctx->digest_type = FKO_DIGEST_SHA3_256; + ctx->digest_len = SHA3_256_B64_LEN; + } + + } + else if(ctx->digest_type == FKO_DIGEST_SHA512) + { + sha3_512_base64(tbuf, (unsigned char*)ctx->encoded_msg, ctx->encoded_msg_len); + if(constant_runtime_cmp(ctx->digest, tbuf, t_size) != 0) + { + return(FKO_ERROR_DIGEST_VERIFICATION_FAILED); + } + else + { + ctx->digest_type = FKO_DIGEST_SHA3_512; + ctx->digest_len = SHA3_512_B64_LEN; + } + + } + else + return(FKO_ERROR_DIGEST_VERIFICATION_FAILED); + } return FKO_SUCCESS; } @@ -135,6 +174,7 @@ is_valid_digest_len(int t_size, fko_ctx_t ctx) ctx->digest_len = SHA1_B64_LEN; break; + /* Could also match SHA3_256_B64_LEN, handled in verify_digest() */ case SHA256_B64_LEN: ctx->digest_type = FKO_DIGEST_SHA256; ctx->digest_len = SHA256_B64_LEN; @@ -145,6 +185,7 @@ is_valid_digest_len(int t_size, fko_ctx_t ctx) ctx->digest_len = SHA384_B64_LEN; break; + /* Could also match SHA3_512_B64_LEN, handled in verify_digest() */ case SHA512_B64_LEN: ctx->digest_type = FKO_DIGEST_SHA512; ctx->digest_len = SHA512_B64_LEN; diff --git a/test/tests/basic_operations.pl b/test/tests/basic_operations.pl index 455662c3..65a5bc7e 100644 --- a/test/tests/basic_operations.pl +++ b/test/tests/basic_operations.pl @@ -1176,6 +1176,16 @@ 'vars' => {'KEY' => 'testtest', 'DIGEST_TYPE' => 'SHA512'}}], 'positive_output_matches' => [qr/Digest\sType\:\s.*SHA512/], }, + { + 'category' => 'basic operations', + 'subcategory' => 'client rc file', + 'detail' => 'digest SHA3_256', + 'function' => \&client_rc_file, + 'cmdline' => $client_rewrite_rc_args, + 'write_rc_file' => [{'name' => 'default', + 'vars' => {'KEY' => 'testtest', 'DIGEST_TYPE' => 'SHA3_256'}}], + 'positive_output_matches' => [qr/Digest\sType\:\s.*SHA3_256/], + }, { 'category' => 'basic operations', 'subcategory' => 'client rc file',