public IList <T> GetValues <T>(ITime[] times) { if (typeof(T) != typeof(double)) { throw new NotSupportedException("Only doubles supported"); } if (buffer == null) { buffer = new SmartBuffer(); } IList <T> valuesList = new List <T>(); foreach (ITime time in times) { for (int timeIndex = 0; timeIndex < TimeSet.Times.Count; timeIndex++) { if (TimeSet.Times[timeIndex].Equals(time)) { double[] valuesAtTime = buffer.GetValuesAt(timeIndex); for (int elementIndex = 0; elementIndex < ElementSet.ElementCount; elementIndex++) { valuesList.Add((T)(object)valuesAtTime[ComputeIndex(timeIndex, elementIndex)]); } break; } } } return(valuesList); }
/// <summary> /// /// </summary> /// <param name="cipher">[Cipher][Tag]</param> /// <param name="key"></param> /// <param name="nonce"></param> /// <param name="aad"></param> /// <returns>[Plain]</returns> protected override SmartBuffer DecryptChunk(ReadOnlyMemory <byte> cipher, ReadOnlySpan <byte> key, ReadOnlySpan <byte> nonce, ReadOnlySpan <byte> aad = default) { var rt = SmartBuffer.Rent(cipher.Length - LEN_TAG); var aead = this.CreateCipher(key, nonce, aad); using (MemoryStream src = MemoryStreamManager.GetStream(), work = MemoryStreamManager.GetStream()) { cipher.Slice(0, cipher.Length - LEN_TAG).CopyToStream(src); src.Position = 0; try { aead.Decrypt(src, src, work, cipher.Slice(cipher.Length - LEN_TAG).Span); src.Position = 0; rt.SignificantLength += rt.Memory.CopyFromStream(src); } catch (Exception ex) { Console.WriteLine($"AeadChaChaPoly1305 DecryptChunk failed. {ex.Message}"); _logger?.LogWarning($"AeadChaChaPoly1305 DecryptChunk failed. {ex.Message}"); rt.SignificantLength = 0; } return(rt); } }
/// <summary> /// Read the client. /// </summary> /// <param name="cancellationToken"></param> /// <returns></returns> public async ValueTask <ReadResult> Read(CancellationToken cancellationToken) { var received = SmartBuffer.Rent(_bufferSize); received.SignificantLength = await Client.ReadAsync(received.Memory, cancellationToken); _logger?.LogInformation($"PipeReader Received {received.SignificantLength} bytes from [{Client.EndPoint.ToString()}]."); if (0 >= received.SignificantLength) { received.Dispose(); return(new ReadResult(Failed, null, received.SignificantLength)); } if (FilterEnabled && FilterChain.Count > 0) { var result = ExecuteFilter(received.SignificantMemory, cancellationToken); received.Dispose(); received = result.Buffer; if (!result.Continue) { received?.Dispose(); return(new ReadResult(BrokeByFilter, null, 0)); } } int read = null != received ? received.SignificantLength : 0; _logger?.LogInformation($"{read} bytes left after [OnReading] filtering."); return(new ReadResult(Succeeded, received, read)); }
private void CreateBufferAndTimeSet() { _buffer = new SmartBuffer(); timeSet = null; _timeBufferTimeSet = new TimeSet(); UpdateTimeHorizonFromDecoratedOutputItem(); }
protected override SmartBuffer EncryptChunk(ReadOnlyMemory <byte> raw, ReadOnlySpan <byte> key, ReadOnlySpan <byte> nonce, ReadOnlySpan <byte> add = default) { AesGcm aes = null; if (!_cipherPool.TryPop(out aes)) { aes = new AesGcm(key); } SmartBuffer cipherPacket = SmartBuffer.Rent(raw.Length + LEN_TAG); var cipherSpan = cipherPacket.Memory.Span; try { aes.Encrypt(nonce, raw.Span, cipherSpan.Slice(0, raw.Length), cipherSpan.Slice(raw.Length, LEN_TAG), add); cipherPacket.SignificantLength = raw.Length + LEN_TAG; } catch (Exception ex) { cipherPacket.SignificantLength = 0; _logger?.LogError(ex, "AeadAesGcm EncryptChunk failed."); } finally { _cipherPool.Push(aes); } return(cipherPacket); }
protected override SmartBuffer DecryptChunk(ReadOnlyMemory <byte> cipher, ReadOnlySpan <byte> key, ReadOnlySpan <byte> nonce, ReadOnlySpan <byte> add = default) { AesGcm aes = null; if (!_cipherPool.TryPop(out aes)) { aes = new AesGcm(key); } SmartBuffer plainPacket = SmartBuffer.Rent(cipher.Length); var plainSpan = plainPacket.Memory.Span; try { aes.Decrypt(nonce, cipher.Span.Slice(0, cipher.Length - LEN_TAG), cipher.Span.Slice(cipher.Length - LEN_TAG), plainSpan.Slice(0, cipher.Length - LEN_TAG), add); plainPacket.SignificantLength = cipher.Length - LEN_TAG; } catch (Exception ex) { plainPacket.SignificantLength = 0; _logger?.LogError(ex, "AeadAesGcm DecryptChunk failed."); } finally { _cipherPool.Push(aes); } return(plainPacket); }
PipeFilterResult ExecuteFilter_BeforeWriting(IClient client, SmartBuffer memory, SortedSet <PipeFilter> filters, CancellationToken cancellationToken) { SmartBuffer prevFilterMemory = memory; bool @continue = true; foreach (var filter in filters.Reverse()) { try { var result = filter.BeforeWriting(new PipeFilterContext(client, prevFilterMemory.SignificanMemory)); prevFilterMemory.Dispose(); prevFilterMemory = result.Buffer; @continue = result.Continue; if (!result.Continue) { break; } if (cancellationToken.IsCancellationRequested) { break; } } catch (Exception ex) { @continue = false; _logger?.LogError(ex, $"ExecuteFilter_BeforeWriting [{client.EndPoint.ToString()}]."); } } return(new PipeFilterResult(client, prevFilterMemory, @continue)); }
/// <summary> /// Restores the buffer with a previously saved buffer state /// </summary> /// <param name="bufferStateID">ID for the state to restore</param> public void RestoreBufferState(string bufferStateID) { /*PEROFMANCE1 * smartBuffer = (SmartBuffer) _bufferStates[bufferStateID]; */ this.smartBuffer = new SmartBuffer((SmartBuffer)_bufferStates[bufferStateID]); }
PipeFilterResult DoFilter_BeforeWriting(ref SmartBuffer buffer, SortedSet <PipeFilter> filters, CancellationToken cancellationToken) { PipeFilterResult rt = default; foreach (var f in filters) { _logger?.LogInformation($"DefaultPipe DoFilter_BeforeWriting {f.ToString()}..."); PipeFilterContext ctx = new PipeFilterContext(f.Client, buffer.Memory, buffer.SignificantLength); try { rt = f.BeforeWriting(ctx); if (!rt.Continue || cancellationToken.IsCancellationRequested) { buffer.Dispose(); rt.Buffer?.Dispose(); break; } buffer.Dispose(); buffer = rt.Buffer; //next filter } catch (Exception ex) { _logger?.LogError(ex, $"DefaultPipe DoFilter_BeforeWriting {f.ToString()} error."); buffer.Dispose(); rt = new PipeFilterResult(ctx.Client, null, false); break; } } return(rt); }
void Initialize_TcpDecrypt_Crumb() { if (null == _tcp_decrypt_crumb) { _tcp_decrypt_crumb = SmartBuffer.Rent(LEN_TCP_MAX_CHUNK + LEN_TCP_OVERHEAD_PER_CHUNK + 100); } }
public virtual SmartBuffer EncryptUdp(ReadOnlyMemory <byte> plain) { if (plain.IsEmpty) { _logger?.LogInformation($"ShadowosocksAeadCipher EncryptUdp plain.IsEmpty."); return(null); } var cipherPacket = SmartBuffer.Rent(1500 + LEN_TAG + _keySize_SaltSize.Item2); var cipherPacketStream = new MemoryWriter(cipherPacket.Memory); byte[] salt = new byte[_keySize_SaltSize.Item2], key = new byte[_keySize_SaltSize.Item1]; RandomNumberGenerator.Fill(salt); DeriveSubKey(_masterKeyBytes, salt, SubkeyInfoBytes, key, key.Length); //[encrypted payload][tag] using (var payload = this.EncryptChunk(plain, key.AsSpan(), _nonceZero.AsSpan())) { cipherPacketStream.Write(salt.AsMemory()); cipherPacketStream.Write(payload.SignificantMemory); cipherPacket.SignificantLength = cipherPacketStream.Position; //[salt][encrypted payload][tag] } return(cipherPacket); }
public virtual SmartBuffer EncryptTcp(ReadOnlyMemory <byte> plain) { if (plain.IsEmpty) { _logger?.LogInformation($"ShadowosocksAeadCipher EncryptTcp plain.IsEmpty."); return(null); } int cipherLen = CalcTcpCipherStreamLength(plain.Length); SmartBuffer cihperBuffer = SmartBuffer.Rent(cipherLen); var cipherStream = new MemoryWriter(cihperBuffer.Memory); if (!_tcpCtx加密.HaveSalt()) { _tcpCtx加密.NewSalt(); cipherStream.Write(_tcpCtx加密.Salt.AsMemory()); _logger?.LogInformation($"ShadowosocksAeadCipher EncryptTcp new salt wrote. { _tcpCtx加密.Salt.ToHexString()}"); DeriveSubKey(_masterKeyBytes, _tcpCtx加密.Salt, SubkeyInfoBytes, _tcpCtx加密.Key, _tcpCtx加密.Key.Length); _logger?.LogInformation($"ShadowosocksAeadCipher EncryptTcp new subkey {_tcpCtx加密.Key.ToHexString()}."); } int remain = plain.Length; int chunkLen = 0, encrypted = 0; do { chunkLen = Math.Min(remain, LEN_TCP_MAX_CHUNK); //payload length. { byte[] payloadLenBytes = BitConverter.GetBytes((ushort)(System.Net.IPAddress.HostToNetworkOrder((short)chunkLen))); _logger?.LogInformation($"ShadowosocksAeadCipher EncryptTcp chunklen={chunkLen}, payloadLenBytes={payloadLenBytes.ToHexString()}."); using (var payloadLenC = this.EncryptChunk(payloadLenBytes, _tcpCtx加密.Key, _tcpCtx加密.Nonce)) { //[encrypted payload length][length tag] cipherStream.Write(payloadLenC.Memory.Slice(0, payloadLenC.SignificantLength)); _tcpCtx加密.IncreaseNonce(); } } //payload. { var payload = plain.Slice(plain.Length - remain, chunkLen); using (var payloadC = this.EncryptChunk(payload, _tcpCtx加密.Key, _tcpCtx加密.Nonce)) { //[encrypted payload][payload tag] cipherStream.Write(payloadC.Memory.Slice(0, payloadC.SignificantLength)); _tcpCtx加密.IncreaseNonce(); } } //------------------------------- encrypted += chunkLen; _logger?.LogInformation($"ShadowosocksAeadCipher EncryptTcp encrypted {encrypted} bytes."); remain -= chunkLen; } while (remain > 0); cihperBuffer.SignificantLength = cipherStream.Position; return(cihperBuffer); }
public override ClientFilterResult AfterReading(ClientFilterContext ctx) { _logger?.LogInformation($"TestPipeFilter AfterReading data={ctx.Memory.ToArray().ToHexString()}"); var newBuff = SmartBuffer.Rent(ctx.Memory.Length - 4); ctx.Memory.Slice(4).CopyTo(newBuff.Memory); newBuff.SignificantLength = ctx.Memory.Length - 4; return(new ClientFilterResult(ctx.Client, newBuff, true)); }
private string _fullPath; //path to the .dll #region ILinkableComponent Members public void AddLink(ILink link) { _links.Add(link.ID, link); SmartBuffer sb = new SmartBuffer(); sb = CreateBuffer(link.SourceElementSet.ToString()); _buffervalues.Add(link.ID, sb); //AddToBuffer(link.SourceElementSet.ToString()); }
/// <summary> /// The last time in the buffer /// </summary> /// <returns>the latest time in the buffer</returns> public ITimeStamp GetLastBufferedTime() { ITime time = SmartBuffer.GetTimeAt(SmartBuffer.TimesCount - 1); if (time is ITimeSpan) { return(new TimeStamp(((ITimeSpan)time).End)); } else { return((ITimeStamp)time); } }
private void CheckBuffer() { if (_buffer == null) { _buffer = new SmartBuffer(); ITimeSpaceValueSet outputItemValues = _adaptee.Values; IList <ITime> outputItemTimes = _adaptee.TimeSet.Times; for (int t = 0; t < outputItemTimes.Count; t++) { ITime time = outputItemTimes[t]; _buffer.AddValues(time, outputItemValues.GetElementValuesForTime(0)); } } }
public override ClientFilterResult BeforeWriting(ClientFilterContext ctx) { _logger?.LogInformation($"TestPipeFilter BeforeWriting data={ctx.Memory.ToArray().ToHexString()}"); var newBuff = SmartBuffer.Rent(ctx.Memory.Length + 4); var p = newBuff.Memory.Span; p[0] = 0x12; p[1] = 0x34; p[4] = 0xAB; p[3] = 0xCD; ctx.Memory.CopyTo(newBuff.Memory.Slice(4)); newBuff.SignificantLength = ctx.Memory.Length + 4; return(new ClientFilterResult(ctx.Client, newBuff, true)); }
private void CheckBuffer() { if (buffer == null) { buffer = new SmartBuffer(); IList outputItemValues = DecoratedOutputItem.Values; IList <ITime> outputItemTimes = DecoratedOutputItem.TimeSet.Times; int i = 0; foreach (ITime time in outputItemTimes) { buffer.AddValues(time, new double[] { (double)outputItemValues[i++] }); } } }
public override ClientFilterResult BeforeWriting(ClientFilterContext ctx) { SmartBuffer toTarget = SmartBuffer.Rent(ctx.Memory.Length); if (ShadowsocksAddress.TryResolveLength(ctx.Memory, out int targetAddrLen)) { ctx.Memory.Slice(targetAddrLen).CopyTo(toTarget.Memory); toTarget.SignificantLength = ctx.Memory.Length - targetAddrLen; return(new ClientFilterResult(ctx.Client, toTarget, true)); } else { return(new ClientFilterResult(ctx.Client, toTarget, false)); } }
public ShadowosocksAeadCipher(string password, ValueTuple <int, int> key_salt_size, ILogger logger = null) : base(password) { _keySize_SaltSize = key_salt_size; _logger = logger; _hkdf = new HkdfBytesGenerator(new Sha1Digest()); _masterKeyBytes = MasterKeyGenerator.PasswordToKey(password, _keySize_SaltSize.Item1); _logger?.LogInformation($"ShadowosocksAeadCipher ctor " + $"_keySize_SaltSize={_keySize_SaltSize.Item1},{_keySize_SaltSize.Item2}"); _tcpCtx加密 = new TcpCipherContext(_keySize_SaltSize.Item1, _keySize_SaltSize.Item2); _tcpCtx解密 = new TcpCipherContext(_keySize_SaltSize.Item1, _keySize_SaltSize.Item2); _tcp_decrypt_crumb = SmartBuffer.Rent(LEN_TCP_OVERHEAD_PER_CHUNK * 2); }
public override ClientFilterResult BeforeWriting(ClientFilterContext ctx) { if (!ctx.Memory.IsEmpty) { SmartBuffer toRemote = SmartBuffer.Rent(1500); ctx.Memory.Slice(3).CopyTo(toRemote.Memory); toRemote.SignificantLength = ctx.Memory.Length - 3; return(new ClientFilterResult(ctx.Client, toRemote, true)); } else { _logger?.LogError($"LocalUdpRelayPackingFilter BeforeWriting filterContext.Memory.IsEmpty"); } return(new ClientFilterResult(this.Client, null, false)); }
/// <summary> /// Write to the client. /// </summary> /// <param name="data"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async ValueTask <WriteResult> Write(ReadOnlyMemory <byte> data, CancellationToken cancellationToken) { if (data.IsEmpty) { _logger?.LogWarning($"PipeWriter Try to write empty memory. [{Client.EndPoint.ToString()}]."); return(new WriteResult(Failed, 0)); } ReadOnlyMemory <byte> toWrite = data; SmartBuffer filterResultBuffer = null; if (FilterEnabled && FilterChain.Count > 0) { var result = ExecuteFilter(data, cancellationToken); if (!result.Continue) { result.Buffer?.Dispose(); _logger?.LogInformation($"Pipe broke by filter [{Client.EndPoint.ToString()}]."); return(new WriteResult(BrokeByFilter, 0)); } else { filterResultBuffer = result.Buffer; toWrite = null != filterResultBuffer ? filterResultBuffer.SignificantMemory : ReadOnlyMemory <byte> .Empty; } } _logger?.LogInformation($"{toWrite.Length} bytes left after [OnWriting] filtering."); if (!toWrite.IsEmpty) { int written = await Client.WriteAsync(toWrite, cancellationToken); filterResultBuffer?.Dispose(); if (0 >= written) { return(new WriteResult(Failed, written)); } else { return(new WriteResult(Succeeded, written)); } } else { return(new WriteResult(Succeeded, 0)); } }
public override ClientFilterResult OnReading(ClientFilterContext ctx) { SmartBuffer bufferPlain = null; if (null != _cipher) { if (!ctx.Memory.IsEmpty)//TODO 1 { bufferPlain = _cipher.DecryptTcp(ctx.Memory); } else { _logger?.LogError($"AeadCipherTcpFilter OnReading filterContext.Memory.IsEmpty"); } } return(new ClientFilterResult(this.Client, bufferPlain, true));//TODO 2 }
public override ClientFilterResult AfterReading(ClientFilterContext ctx) { if (!ctx.Memory.IsEmpty) { SmartBuffer toApplication = SmartBuffer.Rent(1500); ctx.Memory.CopyTo(toApplication.Memory.Slice(2 + 1)); var p = toApplication.Memory.Span; p.Slice(0, 3).Fill(0x0); toApplication.SignificantLength = ctx.Memory.Length + 2 + 1; return(new ClientFilterResult(ctx.Client, toApplication, true)); } else { _logger?.LogError($"LocalUdpRelayPackingFilter AfterReading filterContext.Memory.IsEmpty"); } return(new ClientFilterResult(this.Client, null, false)); }
public void TestMemoryPool1() { var buf1 = SmartBuffer.Rent(16); System.Security.Cryptography.RandomNumberGenerator.Fill(buf1.Memory.Span); Trace.TraceInformation(BitConverter.ToString(buf1.Memory.ToArray()).Replace("-", "")); buf1.Dispose(); var buf2 = SmartBuffer.Rent(10); Trace.TraceInformation(BitConverter.ToString(buf2.Memory.ToArray()).Replace("-", "")); buf2.Dispose(); var buf3 = SmartBuffer.Rent(20); Trace.TraceInformation(BitConverter.ToString(buf3.Memory.ToArray()).Replace("-", "")); buf3.Dispose(); }
protected override SmartBuffer EncryptChunk(ReadOnlyMemory <byte> raw, ReadOnlySpan <byte> key, ReadOnlySpan <byte> nonce, ReadOnlySpan <byte> aad = default) { using (var aes = new AesGcm(key)) { SmartBuffer cipherPacket = SmartBuffer.Rent(raw.Length + LEN_TAG); var cipherSpan = cipherPacket.Memory.Span; try { aes.Encrypt(nonce, raw.Span, cipherSpan.Slice(0, raw.Length), cipherSpan.Slice(raw.Length, LEN_TAG), aad); cipherPacket.SignificantLength = raw.Length + LEN_TAG; } catch (Exception ex) { cipherPacket.SignificantLength = 0; _logger?.LogWarning($"AeadAesGcm EncryptChunk failed. {ex.Message}"); } return(cipherPacket); } }
protected override SmartBuffer DecryptChunk(ReadOnlyMemory <byte> cipher, ReadOnlySpan <byte> key, ReadOnlySpan <byte> nonce, ReadOnlySpan <byte> aad = default) { using (var aes = new AesGcm(key)) { SmartBuffer plainPacket = SmartBuffer.Rent(cipher.Length); var plainSpan = plainPacket.Memory.Span; try { aes.Decrypt(nonce, cipher.Span.Slice(0, cipher.Length - LEN_TAG), cipher.Span.Slice(cipher.Length - LEN_TAG), plainSpan.Slice(0, cipher.Length - LEN_TAG), aad); plainPacket.SignificantLength = cipher.Length - LEN_TAG; } catch (Exception ex) { plainPacket.SignificantLength = 0; _logger?.LogWarning($"AeadAesGcm DecryptChunk failed. {ex.Message}"); } return(plainPacket); } }
/// <summary> /// Update the associated buffer with the last values calculated by the engine /// </summary> public virtual void UpdateBuffer() { if ((link.SourceQuantity != null) && (link.SourceElementSet != null)) { if (this.Engine is Oatc.OpenMI.Sdk.Wrapper.IAdvancedEngine) { TimeValueSet timeValueSet = ((IAdvancedEngine)this.Engine).GetValues(link.SourceQuantity.ID, link.SourceElementSet.ID); if (timeValueSet.Time != null) { if (_useSpatialMapping) { this.smartBuffer.AddValues(timeValueSet.Time, elementMapper.MapValues(timeValueSet.ValueSet)); } else { this.smartBuffer.AddValues(timeValueSet.Time, timeValueSet.ValueSet); } } } else // the engine is IEngine or IRunEngine { ITime time = this.Engine.GetCurrentTime(); IValueSet valueSet = this.Engine.GetValues(link.SourceQuantity.ID, link.SourceElementSet.ID); if (_useSpatialMapping) { this.smartBuffer.AddValues(time, elementMapper.MapValues(valueSet)); } else { this.smartBuffer.AddValues(time, valueSet); } } } SmartBuffer.ClearBefore(link.TargetComponent.EarliestInputTime); }
public override ClientFilterResult AfterReading(ClientFilterContext ctx) { SmartBuffer toSsLocal = SmartBuffer.Rent(1500);//TODO what if exceeds 1500? fragments or not? if (ShadowsocksAddress.TrySerailizeTo( (byte)(AddressFamily.InterNetworkV6 == ctx.Client.EndPoint.AddressFamily ? 0x4 : 0x1), ctx.Client.EndPoint.Address.GetAddressBytes(), (ushort)ctx.Client.EndPoint.Port, toSsLocal.Memory, out int written)) { toSsLocal.SignificantLength = written; int payloadToCopy = Math.Min(toSsLocal.FreeSpace, ctx.Memory.Length); ctx.Memory.Slice(0, payloadToCopy).CopyTo(toSsLocal.FreeMemory); toSsLocal.SignificantLength += payloadToCopy; return(new ClientFilterResult(ctx.Client, toSsLocal, true));; } return(new ClientFilterResult(ctx.Client, null, false));; }
/// <summary> /// /// </summary> /// <param name="raw">[Plain]</param> /// <param name="key"></param> /// <param name="nonce"></param> /// <param name="aad"></param> /// <returns>[Cipher][Tag]</returns> protected override SmartBuffer EncryptChunk(ReadOnlyMemory <byte> raw, ReadOnlySpan <byte> key, ReadOnlySpan <byte> nonce, ReadOnlySpan <byte> aad = default) { var aead = this.CreateCipher(key, nonce, aad); using (MemoryStream src = MemoryStreamManager.GetStream(), work = MemoryStreamManager.GetStream()) { raw.CopyToStream(src); src.Position = 0; var tag = aead.Encrypt(src, src, work); src.Position = 0; var rt = SmartBuffer.Rent((int)raw.Length + tag.Length); rt.SignificantLength = rt.Memory.CopyFromStream(src); tag.AsMemory().CopyTo(rt.FreeMemory); rt.SignificantLength += tag.Length; return(rt); } }