private static byte[] GenerateAEADNonceExplicit(byte[] recordIV, UInt64 seqNum)
{
// Generate explicit part of defined length
byte[] nonceExplicit = new byte[recordIV.Length];
// Copy the sequence number to the end of the explicit part
byte[] seqData = BitConverter.GetBytes(seqNum);
if (BitConverter.IsLittleEndian) {
Array.Reverse(seqData);
}
int seqSrcIdx = Math.Max(0, seqData.Length-nonceExplicit.Length);
int seqDstIdx = Math.Max(0, nonceExplicit.Length-seqData.Length);
int seqLength = Math.Min(seqData.Length, nonceExplicit.Length);
Buffer.BlockCopy(seqData, seqSrcIdx, nonceExplicit, seqDstIdx, seqLength);
// XOR with the random recordIV and return
for (int i=0; i<nonceExplicit.Length; i++) {
nonceExplicit[i] ^= recordIV[i];
}
return nonceExplicit;
}