protected virtual void CheckMac(long seqNo, byte type, byte[] recBuf, int recStart, int recEnd, byte[] calcBuf, int calcOff, int calcLen) { byte[] a = Arrays.CopyOfRange(recBuf, recStart, recEnd); byte[] b = readMac.CalculateMac(seqNo, type, calcBuf, calcOff, calcLen); if (!Arrays.ConstantTimeAreEqual(a, b)) { throw new TlsFatalAlert(20); } }
public virtual byte[] EncodePlaintext(long seqNo, byte type, byte[] plaintext, int offset, int len) { if (usesNonce) { UpdateIV(encryptCipher, forEncryption: true, seqNo); } byte[] array = new byte[len + writeMac.Size]; encryptCipher.ProcessBytes(plaintext, offset, len, array, 0); byte[] array2 = writeMac.CalculateMac(seqNo, type, plaintext, offset, len); encryptCipher.ProcessBytes(array2, 0, array2.Length, array, len); return(array); }
public virtual byte[] EncodePlaintext(long seqNo, byte type, byte[] plaintext, int offset, int len) { if (writeMac == null) { return(Arrays.CopyOfRange(plaintext, offset, offset + len)); } byte[] array = writeMac.CalculateMac(seqNo, type, plaintext, offset, len); byte[] array2 = new byte[len + array.Length]; Array.Copy(plaintext, offset, array2, 0, len); Array.Copy(array, 0, array2, len, array.Length); return(array2); }
public virtual byte[] DecodeCiphertext(long seqNo, byte type, byte[] ciphertext, int offset, int len) { if (readMac == null) { return(Arrays.CopyOfRange(ciphertext, offset, offset + len)); } int size = readMac.Size; if (len < size) { throw new TlsFatalAlert(50); } int num = len - size; byte[] a = Arrays.CopyOfRange(ciphertext, offset + num, offset + len); byte[] b = readMac.CalculateMac(seqNo, type, ciphertext, offset, num); if (!Arrays.ConstantTimeAreEqual(a, b)) { throw new TlsFatalAlert(20); } return(Arrays.CopyOfRange(ciphertext, offset, offset + num)); }
public virtual byte[] DecodeCiphertext(long seqNo, byte type, byte[] ciphertext, int offset, int len) { int blockSize = decryptCipher.GetBlockSize(); int size = mReadMac.Size; int num = blockSize; num = ((!encryptThenMac) ? Math.Max(num, size + 1) : (num + size)); if (useExplicitIV) { num += blockSize; } if (len < num) { throw new TlsFatalAlert(50); } int num2 = len; if (encryptThenMac) { num2 -= size; } if (num2 % blockSize != 0) { throw new TlsFatalAlert(21); } if (encryptThenMac) { int num3 = offset + len; byte[] b = Arrays.CopyOfRange(ciphertext, num3 - size, num3); byte[] a = mReadMac.CalculateMac(seqNo, type, ciphertext, offset, len - size); if (!Arrays.ConstantTimeAreEqual(a, b)) { throw new TlsFatalAlert(20); } } if (useExplicitIV) { decryptCipher.Init(forEncryption: false, new ParametersWithIV(null, ciphertext, offset, blockSize)); offset += blockSize; num2 -= blockSize; } for (int i = 0; i < num2; i += blockSize) { decryptCipher.ProcessBlock(ciphertext, offset + i, ciphertext, offset + i); } int num4 = CheckPaddingConstantTime(ciphertext, offset, num2, blockSize, (!encryptThenMac) ? size : 0); bool flag = num4 == 0; int num5 = num2 - num4; if (!encryptThenMac) { num5 -= size; int num6 = num5; int num7 = offset + num6; byte[] b2 = Arrays.CopyOfRange(ciphertext, num7, num7 + size); byte[] a2 = mReadMac.CalculateMacConstantTime(seqNo, type, ciphertext, offset, num6, num2 - size, randomData); flag |= !Arrays.ConstantTimeAreEqual(a2, b2); } if (flag) { throw new TlsFatalAlert(20); } return(Arrays.CopyOfRange(ciphertext, offset, offset + num5)); }
public virtual byte[] EncodePlaintext(long seqNo, byte type, byte[] plaintext, int offset, int len) { int blockSize = encryptCipher.GetBlockSize(); int size = mWriteMac.Size; ProtocolVersion serverVersion = context.ServerVersion; int num = len; if (!encryptThenMac) { num += size; } int num2 = blockSize - 1 - num % blockSize; if (!serverVersion.IsDtls && !serverVersion.IsSsl) { int max = (255 - num2) / blockSize; int num3 = ChooseExtraPadBlocks(context.SecureRandom, max); num2 += num3 * blockSize; } int num4 = len + size + num2 + 1; if (useExplicitIV) { num4 += blockSize; } byte[] array = new byte[num4]; int num5 = 0; if (useExplicitIV) { byte[] array2 = new byte[blockSize]; context.NonceRandomGenerator.NextBytes(array2); encryptCipher.Init(forEncryption: true, new ParametersWithIV(null, array2)); Array.Copy(array2, 0, array, num5, blockSize); num5 += blockSize; } int num6 = num5; Array.Copy(plaintext, offset, array, num5, len); num5 += len; if (!encryptThenMac) { byte[] array3 = mWriteMac.CalculateMac(seqNo, type, plaintext, offset, len); Array.Copy(array3, 0, array, num5, array3.Length); num5 += array3.Length; } for (int i = 0; i <= num2; i++) { array[num5++] = (byte)num2; } for (int j = num6; j < num5; j += blockSize) { encryptCipher.ProcessBlock(array, j, array, j); } if (encryptThenMac) { byte[] array4 = mWriteMac.CalculateMac(seqNo, type, array, 0, num5); Array.Copy(array4, 0, array, num5, array4.Length); num5 += array4.Length; } return(array); }