HMAC-256 KeyedHashAlgorithm

Hello,

I’m using spider 2

netfm-4.3 & visual studio 2015 community

I tried to sign buffer with HMAC-256 algorithm .

bellow is my code and using key,

  
     byte[] key =new byte[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
        byte[] data=new byte[]{1,2,3,4}
       byte[] HmacSha256(byte[] data, byte[] key)
        {
            try
            {
                hmac = new KeyedHashAlgorithm(KeyedHashAlgorithmType.HMACSHA256);
                hmac.Key = CryptoKey.ImportKey(hmac.Session, key, CryptoKey.KeyClass.Secret, CryptoKey.KeyType.AES, true);
                byte[] HashArray= hmac.ComputeHash(data);
                return HashArray;
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
                return null;
            }
        }

sometimes this code work’s but most of the time I receive an exception

while trying to create new KeyedHashAlgorithm(KeyedHashAlgorithmType.HMACSHA256);

what I’m doing wrong?

please advice

Thank’s lior

Welcome to the community!

What kind of exception do you get?

@ liorbennaim - You’re not necessarily doing anything wrong. I belive NETMF is pretty broken; I once had to use AES, and NETMF was throwing hashing exceptions absolutely randomly, sometimes even on the code that does nothing with AES!.. So eventually I stoppped using NETMF resources and reimplemented AES myself using RLP.

@ 10 x Architect

I succeeded to reproduce the failure
try to run the following code with netmf 4.3
after about 40 iterations of init hmac key, it will throw you exception
(only hard reset will release it )


public static void Main()
{
byte[] data = { 1, 2, 3, 4 };
byte[] key = { 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf };
while (true)
{
    int i = 0;
    key[0]++;
    Debug.Print(key[0].ToString());
    initHmac(key);
    while (i < 20)
    {
        i++;
        byte[] sig = HmacSha256(data);
        Debug.Print(i.ToString());
        Thread.Sleep(100);
    } 
}
}
        static private byte[] HmacSha256(byte[] data)
        {
            try
            {
                byte[] HashArray = hmac.ComputeHash(data);
                return HashArray;
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
                return null;
            }
        }
        static public void initHmac(byte[] key)
        {
            //CryptoKey k = CryptoKey.ImportKey(ss, key, CryptoKey.KeyClass.Secret, CryptoKey.KeyType.AES, false);
            //hmac = new KeyedHashAlgorithm(KeyedHashAlgorithmType.HMACSHA256,k);
            try
            {
                if (CryptoSession == null)
                    CryptoSession = new Session("AesCryptoServiceProvider");
                if (hmac == null)
                    hmac = new KeyedHashAlgorithm(KeyedHashAlgorithmType.HMACSHA256, CryptoKey.ImportKey(CryptoSession, key, CryptoKey.KeyClass.Secret, CryptoKey.KeyType.AES, false));
                else
                    hmac.Key = CryptoKey.ImportKey(CryptoSession, key, CryptoKey.KeyClass.Secret, CryptoKey.KeyType.AES, false);
            }
            catch (Exception)
            {
                hmac = null;
                Debug.GC(true);
                Thread.Sleep(3500);
            }

        }

@ Simon from Vilnius - was your test on 2016 sdk? Can you provide an example so we can look into it please?

Sorry, I can’t :frowning: I’m still at 2014R2. Issues were totally random that I couldn’t even reliably reproduce-this is why I didn’t report anything. All went away once I remade AES myself…

@ liorbennaim -
There is max 40 objects so if hmac is static you should to call hmac.Session.Dispose();