public bool DecryptAfterReceive( ReadOnlySpan <byte> associatedData, ReadOnlySpan <byte> ciphertext, Span <byte> plaintext) { // decrypt the ciphertext with the receive nonce if (!_algorithm.Decrypt( _receiveKey, _receiveNonce, associatedData, ciphertext, plaintext)) { // abort the connection if decryption fails _sendKey.Dispose(); _receiveKey.Dispose(); return(false); } // increment the counter field of the receive nonce if (!Nonce.TryIncrement(ref _receiveNonce)) { // abort the connection when the counter field of the // receive nonce reaches the maximum value _sendKey.Dispose(); _receiveKey.Dispose(); } return(true); }
private static void EncryptBeforeSend(ReadOnlySpan <byte> associatedData, Nonce sendNonce, ReadOnlySpan <byte> plaintext, Span <byte> ciphertext) { _algorithm.Encrypt(_sendKey, sendNonce, associatedData, plaintext, ciphertext); if (!Nonce.TryIncrement(ref _sendSequenceNumber)) { _sendKey.Dispose(); } }
private static bool DecryptAfterReceive(Nonce receiveNonce, ReadOnlySpan <byte> ciphertext, Span <byte> plaintext) { if (!_algorithm.Decrypt(_sendKey, receiveNonce, _associatedData, ciphertext, plaintext)) { _sendKey.Dispose(); return(false); } if (!Nonce.TryIncrement(ref _receiveSequenceNumber)) { _sendKey.Dispose(); return(false); } return(true); }
public void EncryptBeforeSend( ReadOnlySpan <byte> associatedData, ReadOnlySpan <byte> plaintext, Span <byte> ciphertext) { // encrypt the plaintext with the send nonce _algorithm.Encrypt( _sendKey, _sendNonce, associatedData, plaintext, ciphertext); // increment the counter field of the send nonce if (!Nonce.TryIncrement(ref _sendNonce)) { // abort the connection when the counter field of the // send nonce reaches the maximum value _sendKey.Dispose(); _receiveKey.Dispose(); } }
public void EncryptBeforeSend( ReadOnlySpan <byte> associatedData, ReadOnlySpan <byte> plaintext, Span <byte> ciphertext) { // encrypt the plaintext with the send sequence number XORed // with the send IV as the nonce _algorithm.Encrypt( _sendKey, _sendSequenceNumber ^ _sendIV, associatedData, plaintext, ciphertext); // increment the send sequence number if (!Nonce.TryIncrement(ref _sendSequenceNumber)) { // abort the connection when the send sequence number // reaches the maximum value _sendKey.Dispose(); _receiveKey.Dispose(); } }