From 02e6963673585328b4e15bddafedda2a61b1cc95 Mon Sep 17 00:00:00 2001 From: Russell Hammett Date: Wed, 2 Dec 2020 07:09:33 -0500 Subject: [PATCH] Updates c based hmac wrapper to support non byte boundary MAC. * https://github.com/usnistgov/ACVP-Server/issues/46 --- .../NIST.CVP.Crypto/HMAC/FastHmac/FastHmacSha.cs | 12 ++++++++---- .../NIST.CVP.Crypto/HMAC/FastHmac/FastHmacSha3.cs | 14 +++++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/gen-val/src/crypto/src/NIST.CVP.Crypto/HMAC/FastHmac/FastHmacSha.cs b/gen-val/src/crypto/src/NIST.CVP.Crypto/HMAC/FastHmac/FastHmacSha.cs index bffe7edd7b..afa3badfcb 100644 --- a/gen-val/src/crypto/src/NIST.CVP.Crypto/HMAC/FastHmac/FastHmacSha.cs +++ b/gen-val/src/crypto/src/NIST.CVP.Crypto/HMAC/FastHmac/FastHmacSha.cs @@ -4,6 +4,7 @@ using NIST.CVP.Crypto.Common.MAC; using NIST.CVP.Crypto.Common.MAC.HMAC; using NIST.CVP.Math; +using NIST.CVP.Math.Helpers; namespace NIST.CVP.Crypto.HMAC.FastHmac { @@ -36,12 +37,15 @@ public MacResult Generate(BitString key, BitString message, int macLength = 0) hashLength = 512256; } + var macLengthInBytes = (uint) macLength.CeilingDivide(BitString.BITSINBYTE); + // C expects securityStrength, keyLen and macLen to all be in BYTES, but msgLen is in BITS - void osxAction(IntPtr digPtr) => HmacDllLoader.hmac_SHA_osx(hashLength, key.GetPaddedBytes(), (uint)key.BitLength/8, message.GetPaddedBytes(), (uint)message.BitLength, digPtr, (uint)macLength/8); - void winAction(IntPtr digPtr) => HmacDllLoader.hmac_SHA_win(hashLength, key.GetPaddedBytes(), (uint)key.BitLength/8, message.GetPaddedBytes(), (uint)message.BitLength, digPtr, (uint)macLength/8); - void linuxAction(IntPtr digPtr) => HmacDllLoader.hmac_SHA_linux(hashLength, key.GetPaddedBytes(), (uint)key.BitLength/8, message.GetPaddedBytes(), (uint)message.BitLength, digPtr, (uint)macLength/8); + void osxAction(IntPtr digPtr) => HmacDllLoader.hmac_SHA_osx(hashLength, key.GetPaddedBytes(), (uint)key.BitLength/8, message.GetPaddedBytes(), (uint)message.BitLength, digPtr, macLengthInBytes); + void winAction(IntPtr digPtr) => HmacDllLoader.hmac_SHA_win(hashLength, key.GetPaddedBytes(), (uint)key.BitLength/8, message.GetPaddedBytes(), (uint)message.BitLength, digPtr, macLengthInBytes); + void linuxAction(IntPtr digPtr) => HmacDllLoader.hmac_SHA_linux(hashLength, key.GetPaddedBytes(), (uint)key.BitLength/8, message.GetPaddedBytes(), (uint)message.BitLength, digPtr, macLengthInBytes); - var mac = new BitString(DllHelper.PickOS(osxAction, winAction, linuxAction, macLength/8)); + var mac = new BitString(DllHelper.PickOS(osxAction, winAction, linuxAction, (int)macLengthInBytes)) + .GetMostSignificantBits(macLength); return new MacResult(mac); } } diff --git a/gen-val/src/crypto/src/NIST.CVP.Crypto/HMAC/FastHmac/FastHmacSha3.cs b/gen-val/src/crypto/src/NIST.CVP.Crypto/HMAC/FastHmac/FastHmacSha3.cs index 1c25918c42..cba47d0a8b 100644 --- a/gen-val/src/crypto/src/NIST.CVP.Crypto/HMAC/FastHmac/FastHmacSha3.cs +++ b/gen-val/src/crypto/src/NIST.CVP.Crypto/HMAC/FastHmac/FastHmacSha3.cs @@ -4,6 +4,7 @@ using NIST.CVP.Crypto.Common.MAC; using NIST.CVP.Crypto.Common.MAC.HMAC; using NIST.CVP.Math; +using NIST.CVP.Math.Helpers; namespace NIST.CVP.Crypto.HMAC.FastHmac { @@ -27,13 +28,16 @@ public MacResult Generate(BitString key, BitString message, int macLength = 0) { macLength = OutputLength; } - + + var macLengthInBytes = (uint) macLength.CeilingDivide(BitString.BITSINBYTE); + // C expects securityStrength, keyLen and macLen to all be in BYTES, but msgLen is in BITS - void osxAction(IntPtr digPtr) => HmacDllLoader.hmac_SHA3_osx((uint)_hashFunction.OutputLen/8, paddedKey.ToBytes(), (uint)key.BitLength/8, paddedMsg.ToBytes(), (uint)message.BitLength, digPtr, (uint)macLength/8); - void winAction(IntPtr digPtr) => HmacDllLoader.hmac_SHA3_win((uint)_hashFunction.OutputLen/8, paddedKey.ToBytes(), (uint)key.BitLength/8, paddedMsg.ToBytes(), (uint)message.BitLength, digPtr, (uint)macLength/8); - void linuxAction(IntPtr digPtr) => HmacDllLoader.hmac_SHA3_linux((uint)_hashFunction.OutputLen/8, paddedKey.ToBytes(), (uint)key.BitLength/8, paddedMsg.ToBytes(), (uint)message.BitLength, digPtr, (uint)macLength/8); + void osxAction(IntPtr digPtr) => HmacDllLoader.hmac_SHA3_osx((uint)_hashFunction.OutputLen/8, paddedKey.ToBytes(), (uint)key.BitLength/8, paddedMsg.ToBytes(), (uint)message.BitLength, digPtr, macLengthInBytes); + void winAction(IntPtr digPtr) => HmacDllLoader.hmac_SHA3_win((uint)_hashFunction.OutputLen/8, paddedKey.ToBytes(), (uint)key.BitLength/8, paddedMsg.ToBytes(), (uint)message.BitLength, digPtr, macLengthInBytes); + void linuxAction(IntPtr digPtr) => HmacDllLoader.hmac_SHA3_linux((uint)_hashFunction.OutputLen/8, paddedKey.ToBytes(), (uint)key.BitLength/8, paddedMsg.ToBytes(), (uint)message.BitLength, digPtr, macLengthInBytes); - var mac = new BitString(DllHelper.PickOS(osxAction, winAction, linuxAction, macLength/8)); + var mac = new BitString(DllHelper.PickOS(osxAction, winAction, linuxAction, (int)macLengthInBytes)) + .GetMostSignificantBits(macLength); return new MacResult(mac); } }