/*public void RemoveRange(int index, int count) * { * for (var i = 0; i < count; i++) * Utils.ClearArray(_messages[index + i]); * * _messages.RemoveRange(index, count); * }*/ public void ClearMessages() { foreach (var arr in _messages) { Utils.ClearArray(arr); } _messages.Clear(); HasServerHelloDone = false; _hasFinished = false; _hasHelloRequest = false; }
public static ulong[] GetH(ICryptoTransform key) { var bytes = new byte[16]; key.TransformBlock(bytes, 0, 16, bytes, 0); int offset = 0; var h1 = Utils.ReadUInt64(bytes, ref offset); var h2 = Utils.ReadUInt64(bytes, ref offset); Utils.ClearArray(bytes); return(Construct(h1, h2)); }
public void RemoveFirst() { if ((HandshakeType)_messages[0][0] == HandshakeType.Finished) { _hasFinished = false; } // Ignore HasServerHelloDone and _hasHelloRequest Utils.ClearArray(_messages[0]); _messages.RemoveAt(0); }
public static byte[] SignRsaPKCS1(RSACryptoServiceProvider key, byte[] hash) { // NOTE: The X509Certificate2 must be initialized with the X509KeyStorageFlags.Exportable flag var parameters = key.ExportParameters(true); var dp = Utils.BigIntegerFromBigEndian(parameters.DP, 0, parameters.DP.Length); var dq = Utils.BigIntegerFromBigEndian(parameters.DQ, 0, parameters.DQ.Length); var qinv = Utils.BigIntegerFromBigEndian(parameters.InverseQ, 0, parameters.InverseQ.Length); var p = Utils.BigIntegerFromBigEndian(parameters.P, 0, parameters.P.Length); var q = Utils.BigIntegerFromBigEndian(parameters.Q, 0, parameters.Q.Length); var data = new byte[parameters.D.Length - 1]; data[0] = 1; for (var i = 1; i < data.Length - hash.Length - 1; i++) { data[i] = 0xff; } data[data.Length - hash.Length - 1] = 0; Buffer.BlockCopy(hash, 0, data, data.Length - hash.Length, hash.Length); var m = Utils.BigIntegerFromBigEndian(data, 0, data.Length); var m1 = BigInteger.ModPow(m, dp, p); var m2 = BigInteger.ModPow(m, dq, q); var h = qinv * (m1 - m2) % p; if (h.Sign == -1) { h += p; } var signature = Utils.BigEndianFromBigInteger(m2 + h * q); Utils.ClearArray(parameters.D); Utils.ClearArray(parameters.DP); Utils.ClearArray(parameters.DQ); Utils.ClearArray(parameters.InverseQ); Utils.ClearArray(parameters.P); Utils.ClearArray(parameters.Q); return(signature); }
public BigInt GenPriv(RandomNumberGenerator rng) { EllipticCurve.BigInt priv = null; do { if (priv != null) { priv.Clear(); } var bytes = new byte[curveByteLen]; rng.GetBytes(bytes); var byteMask = (1 << (curveLen & 7)) - 1; if (byteMask != 0) { bytes[0] &= (byte)byteMask; } priv = new EllipticCurve.BigInt(bytes, 0, bytes.Length); Utils.ClearArray(bytes); } while (priv >= p || priv.IsZero()); return(priv); }
public void Dispose() { if (ReadAesECB != null) { ReadAesECB.Dispose(); } if (WriteAesECB != null) { WriteAesECB.Dispose(); } if (ReadAes != null) { ReadAes.Clear(); } if (WriteAes != null) { WriteAes.Clear(); } if (ReadMac != null) { ReadMac.Clear(); } if (WriteMac != null) { WriteMac.Clear(); } if (ReadIv != null) { Utils.ClearArray(ReadIv); } if (WriteIv != null) { Utils.ClearArray(WriteIv); } if (ReadGCMTable != null) { Utils.ClearArray(ReadGCMTable); } if (WriteGCMTable != null) { Utils.ClearArray(WriteGCMTable); } if (MasterSecret != null) { Utils.ClearArray(MasterSecret); } if (ClientRandom != null) { Utils.ClearArray(ClientRandom); } if (ServerRandom != null) { Utils.ClearArray(ServerRandom); } if (ClientVerifyData != null) { Utils.ClearArray(ClientVerifyData); } if (ServerVerifyData != null) { Utils.ClearArray(ServerVerifyData); } }
public int AddBytes(byte[] buffer, int offset, int length, IgnoreHelloRequestsSettings ignoreHelloRequests = IgnoreHelloRequestsSettings.IncludeHelloRequests) { var numAdded = 0; var end = offset + length; while (true) { if (_headerBufferLen == 0) { while (offset + 4 <= end) { // We can read at least the header int start = offset; offset++; var messageLen = Utils.ReadUInt24(buffer, ref offset); offset += messageLen; if (offset <= end) { // Whole message fits in buffer, this is the common case var message = new byte[4 + messageLen]; Buffer.BlockCopy(buffer, start, message, 0, 4 + messageLen); if ((!_hasHelloRequest && (ignoreHelloRequests == IgnoreHelloRequestsSettings.IncludeHelloRequests || (ignoreHelloRequests == IgnoreHelloRequestsSettings.IgnoreHelloRequestsUntilFinished && _hasFinished))) || !IsHelloRequest(message)) { _messages.Add(message); CheckType((HandshakeType)message[0]); numAdded++; } } else { // The header fits in the buffer, but not the entire message _headerBuffer = new byte[4]; _headerBufferLen = 4; Buffer.BlockCopy(buffer, start, _headerBuffer, 0, 4); _buffer = new byte[messageLen]; _bufferLen = messageLen - (offset - end); Buffer.BlockCopy(buffer, start + 4, _buffer, 0, _bufferLen); } } if (offset < end) { // Else, the whole header does not fit in the buffer _headerBuffer = new byte[4]; _headerBufferLen = end - offset; Buffer.BlockCopy(buffer, offset, _headerBuffer, 0, _headerBufferLen); } return(numAdded); } else { // We have previously buffered up a part of a message that needs to be completed if (_headerBufferLen < 4) { var toCopy = Math.Min(end - offset, 4 - _headerBufferLen); Buffer.BlockCopy(buffer, offset, _headerBuffer, _headerBufferLen, toCopy); _headerBufferLen += toCopy; offset += toCopy; if (_headerBufferLen < 4) { return(numAdded); } } // Now header buffer is complete, so we can fetch message len and fill rest of message buffer as much as possible var tmpOffset = 1; var messageLen = Utils.ReadUInt24(_headerBuffer, ref tmpOffset); var bytesToCopy = Math.Min(end - offset, messageLen - _bufferLen); if (_buffer == null) { _buffer = new byte[messageLen]; } Buffer.BlockCopy(buffer, offset, _buffer, _bufferLen, bytesToCopy); offset += bytesToCopy; _bufferLen += bytesToCopy; if (_bufferLen != messageLen) { return(numAdded); } // Now we have a complete message to insert to the queue var message = new byte[4 + messageLen]; Buffer.BlockCopy(_headerBuffer, 0, message, 0, 4); Buffer.BlockCopy(_buffer, 0, message, 4, messageLen); if ((!_hasHelloRequest && (ignoreHelloRequests == IgnoreHelloRequestsSettings.IncludeHelloRequests || (ignoreHelloRequests == IgnoreHelloRequestsSettings.IgnoreHelloRequestsUntilFinished && _hasFinished))) || !IsHelloRequest(message)) { _messages.Add(message); CheckType((HandshakeType)message[0]); numAdded++; } _headerBuffer = null; _headerBufferLen = 0; Utils.ClearArray(_buffer); _buffer = null; _bufferLen = 0; } } }