Skip to content

Commit

Permalink
Updates c based hmac wrapper to support non byte boundary MAC.
Browse files Browse the repository at this point in the history
* #46
  • Loading branch information
Kritner committed Dec 2, 2020
1 parent 69d9efa commit 02e6963
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 02e6963

Please sign in to comment.