static byte[] ComputeRecordMAC(TlsProtocolCode protocol, HMac hmac, ulong seqnum, ContentType contentType, IBufferOffsetSize fragment) { var header = new TlsBuffer(13); header.Write(seqnum); header.Write((byte)contentType); header.Write((short)protocol); header.Write((short)fragment.Size); hmac.Reset(); hmac.TransformBlock(header.Buffer, 0, header.Size); hmac.TransformBlock(fragment.Buffer, fragment.Offset, fragment.Size); return(hmac.TransformFinalBlock()); }
protected SecureBuffer Expand (DisposeContext d, HMac hmac, string label, SecureBuffer seed, int length) { var blockSize = hmac.MacSize; var iterations = (int)(length / blockSize); if ((length % blockSize) > 0) iterations++; var resMacs = d.CreateBuffer (length); var resOff = 0; var tempBuf = d.CreateBuffer (blockSize); var labelBytes = Encoding.ASCII.GetBytes (label); for (int i = 1; i <= iterations; i++) { hmac.Reset (); if (i == 1) { hmac.TransformBlock (labelBytes, 0, labelBytes.Length); hmac.TransformBlock (seed.Buffer, 0, seed.Size); } else { hmac.TransformBlock (tempBuf.Buffer, 0, blockSize); } hmac.TransformFinalBlock (tempBuf.Buffer, 0, blockSize); hmac.Reset (); hmac.TransformBlock (tempBuf.Buffer, 0, blockSize); hmac.TransformBlock (labelBytes, 0, labelBytes.Length); hmac.TransformBlock (seed.Buffer, 0, seed.Size); hmac.TransformFinalBlock (resMacs.Buffer, resOff, Min (length - resOff, blockSize)); resOff += blockSize; } return new SecureBuffer (resMacs.StealBuffer ()); }
static byte[] ComputeRecordMAC (TlsProtocolCode protocol, HMac hmac, ulong seqnum, ContentType contentType, IBufferOffsetSize fragment) { var header = new TlsBuffer (13); header.Write (seqnum); header.Write ((byte)contentType); header.Write ((short)protocol); header.Write ((short)fragment.Size); hmac.Reset (); hmac.TransformBlock (header.Buffer, 0, header.Size); hmac.TransformBlock (fragment.Buffer, fragment.Offset, fragment.Size); return hmac.TransformFinalBlock (); }