Please, I would like your input on the following HKDF-Expand implementation in c# for v4.
public byte[] ExpandPrk(byte[] prk, HMAC hmac, byte[] info, int length)
{
if (hmac == null) hmac = new HMACSHA512(); // if hmac is null then hmac will be initialized to a new instance of HMACSHA512
if (info == null) info = new byte[0]; // if info is null then it will initialized the variable with zeros
if (length == null) length = 32; // if length is null then it will initialized with the number 32(256 bits) which will be the length of opm
uint hashLength = (uint) hmac.HashSize / 8; // based on output of selected hash
hmac.Key = prk;
int n = (int)System.Math.Ceiling((Single)length / hashLength); // N = ceil(L/HashLen) number of loops needed to get the required length
byte[] tn = new byte[n * hashLength];
using (MemoryStream ms = new System.IO.MemoryStream())
{
byte[] tempBuffer = new byte[0]; // tempBuffer will keep the bytes from the previous (prev) computed hash which
// will be used with the next hash computation
for (int i = 1; i <= n; i++)
{
ms.Write(tempBuffer, 0, tempBuffer.Length);
if (info.Length > 0) ms.Write(info, 0, info.Length);
ms.WriteByte((byte)(0x01 * i)); // adds i information to the info data
tempBuffer = hmac.ComputeHash(ms.ToArray());
Array.Copy(tempBuffer, 0, tn, (i - 1) * hashLength, hashLength); // keeps adding to tn until requested length is satisfied
ms.SetLength(0); //reset stream
}
}
byte[] okm = new byte[length]; // okm = output key material
Array.Copy(tn, okm, okm.Length); // copy only the requested size as result
return okm; // okm now contains the new key with requested length
}