| | | 1 | | using System.Diagnostics; |
| | | 2 | | |
| | | 3 | | namespace NLightning.Infrastructure.Transport.Handshake.States; |
| | | 4 | | |
| | | 5 | | using Crypto.Ciphers; |
| | | 6 | | using Crypto.Functions; |
| | | 7 | | using Crypto.Primitives; |
| | | 8 | | using Domain.Crypto.Constants; |
| | | 9 | | using Domain.Utils; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// A CipherState can encrypt and decrypt data based on its variables k |
| | | 13 | | /// (a cipher key of 32 bytes) and n (an 8-byte unsigned integer nonce). |
| | | 14 | | /// </summary> |
| | | 15 | | internal sealed class CipherState : IDisposable |
| | | 16 | | { |
| | | 17 | | private const ulong MaxNonce = 1000; |
| | | 18 | | |
| | 200 | 19 | | private readonly ChaCha20Poly1305 _cipher = new(); |
| | 200 | 20 | | private readonly Hkdf _hkdf = new(); |
| | | 21 | | |
| | | 22 | | private SecureMemory? _ck; |
| | | 23 | | private SecureMemory? _k; |
| | | 24 | | private ulong _n; |
| | | 25 | | private bool _disposed; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Sets _k = key. Sets _ck = ck. Sets _n = 0. |
| | | 29 | | /// </summary> |
| | | 30 | | public void InitializeKeyAndChainingKey(ReadOnlySpan<byte> key, ReadOnlySpan<byte> chainingKey) |
| | | 31 | | { |
| | 272 | 32 | | ExceptionUtils.ThrowIfDisposed(_disposed, nameof(CipherState)); |
| | | 33 | | |
| | | 34 | | Debug.Assert(key.Length == CryptoConstants.PrivkeyLen); |
| | | 35 | | Debug.Assert(chainingKey.Length == CryptoConstants.PrivkeyLen); |
| | | 36 | | |
| | 272 | 37 | | _k ??= new SecureMemory(CryptoConstants.PrivkeyLen); |
| | 272 | 38 | | key.CopyTo(_k); |
| | | 39 | | |
| | 272 | 40 | | _ck ??= new SecureMemory(CryptoConstants.PrivkeyLen); |
| | 272 | 41 | | chainingKey.CopyTo(_ck); |
| | | 42 | | |
| | 272 | 43 | | _n = 0; |
| | 272 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Returns true if k and ck are non-empty, false otherwise. |
| | | 48 | | /// </summary> |
| | | 49 | | public bool HasKeys() |
| | | 50 | | { |
| | 16316 | 51 | | ExceptionUtils.ThrowIfDisposed(_disposed, nameof(CipherState)); |
| | | 52 | | |
| | 16316 | 53 | | return _k is not null && _ck is not null; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Sets n = nonce. This function is used for handling out-of-order transport messages. |
| | | 58 | | /// </summary> |
| | | 59 | | public void SetNonce(ulong nonce) |
| | | 60 | | { |
| | 16 | 61 | | ExceptionUtils.ThrowIfDisposed(_disposed, nameof(CipherState)); |
| | | 62 | | |
| | 16 | 63 | | _n = nonce; |
| | 16 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// If k is non-empty returns ENCRYPT(k, n++, ad, plaintext). |
| | | 68 | | /// Otherwise, copies the plaintext to the ciphertext parameter |
| | | 69 | | /// and returns the length of the plaintext. |
| | | 70 | | /// </summary> |
| | | 71 | | public int EncryptWithAd(ReadOnlySpan<byte> ad, ReadOnlySpan<byte> plaintext, Span<byte> ciphertext) |
| | | 72 | | { |
| | 132 | 73 | | ExceptionUtils.ThrowIfDisposed(_disposed, nameof(CipherState)); |
| | | 74 | | |
| | 132 | 75 | | if (_n == MaxNonce) |
| | | 76 | | { |
| | 4 | 77 | | throw new OverflowException("Nonce has reached its maximum value."); |
| | | 78 | | } |
| | | 79 | | |
| | 128 | 80 | | if (_k != null) |
| | | 81 | | { |
| | 124 | 82 | | return _cipher.Encrypt(_k, _n++, ad, plaintext, ciphertext); |
| | | 83 | | } |
| | | 84 | | |
| | 4 | 85 | | plaintext.CopyTo(ciphertext); |
| | 4 | 86 | | return plaintext.Length; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// If k is non-empty returns DECRYPT(k, n++, ad, ciphertext). |
| | | 91 | | /// Otherwise, copies the ciphertext to the plaintext parameter and returns |
| | | 92 | | /// the length of the ciphertext. If an authentication failure occurs |
| | | 93 | | /// then n is not incremented and an error is signaled to the caller. |
| | | 94 | | /// </summary> |
| | | 95 | | public int DecryptWithAd(ReadOnlySpan<byte> ad, ReadOnlySpan<byte> ciphertext, Span<byte> plaintext) |
| | | 96 | | { |
| | 128 | 97 | | ExceptionUtils.ThrowIfDisposed(_disposed, nameof(CipherState)); |
| | | 98 | | |
| | | 99 | | // If nonce reaches its maximum value, rekey |
| | 128 | 100 | | if (_n == MaxNonce) |
| | | 101 | | { |
| | 0 | 102 | | throw new OverflowException("Nonce has reached its maximum value."); |
| | | 103 | | } |
| | | 104 | | |
| | 128 | 105 | | if (_k == null) |
| | | 106 | | { |
| | 4 | 107 | | ciphertext.CopyTo(plaintext); |
| | 4 | 108 | | return ciphertext.Length; |
| | | 109 | | } |
| | | 110 | | |
| | 124 | 111 | | var bytesRead = _cipher.Decrypt(_k, _n, ad, ciphertext, plaintext); |
| | 104 | 112 | | ++_n; |
| | | 113 | | |
| | 104 | 114 | | return bytesRead; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Returns ENCRYPT(k, n, null, plaintext). |
| | | 119 | | /// </summary> |
| | | 120 | | /// <param name="plaintext">Bytes to be encrypted</param> |
| | | 121 | | /// <param name="ciphertext">Encrypted bytes</param> |
| | | 122 | | /// <returns>Number of bytes written to ciphertext</returns> |
| | | 123 | | public int Encrypt(ReadOnlySpan<byte> plaintext, Span<byte> ciphertext) |
| | | 124 | | { |
| | 8024 | 125 | | ExceptionUtils.ThrowIfDisposed(_disposed, nameof(CipherState)); |
| | | 126 | | |
| | 8024 | 127 | | if (_n == MaxNonce) |
| | | 128 | | { |
| | 12 | 129 | | Rekey(); |
| | | 130 | | } |
| | | 131 | | |
| | 8024 | 132 | | return _cipher.Encrypt(_k!, _n++, null, plaintext, ciphertext); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Returns DECRYPT(k, n, null, ciphertext). |
| | | 137 | | /// </summary> |
| | | 138 | | /// <param name="ciphertext">Bytes to be decrypted</param> |
| | | 139 | | /// <param name="plaintext">Decrypted bytes</param> |
| | | 140 | | /// <returns>Number of bytes written to plaintext</returns> |
| | | 141 | | public int Decrypt(ReadOnlySpan<byte> ciphertext, Span<byte> plaintext) |
| | | 142 | | { |
| | 8016 | 143 | | ExceptionUtils.ThrowIfDisposed(_disposed, nameof(CipherState)); |
| | | 144 | | |
| | 8016 | 145 | | if (_n == MaxNonce) |
| | | 146 | | { |
| | 8 | 147 | | Rekey(); |
| | | 148 | | } |
| | 8016 | 149 | | return _cipher.Decrypt(_k!, _n++, null, ciphertext, plaintext); |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Sets k = REKEY(k). |
| | | 154 | | /// </summary> |
| | | 155 | | public void Rekey() |
| | | 156 | | { |
| | 20 | 157 | | ExceptionUtils.ThrowIfDisposed(_disposed, nameof(CipherState)); |
| | | 158 | | |
| | 20 | 159 | | if (!HasKeys()) |
| | | 160 | | { |
| | 0 | 161 | | throw new NullReferenceException("Keys are missing"); |
| | | 162 | | } |
| | | 163 | | |
| | 20 | 164 | | _n = 0; |
| | | 165 | | |
| | 20 | 166 | | Span<byte> key = stackalloc byte[CryptoConstants.PrivkeyLen * 2]; |
| | 20 | 167 | | _hkdf.ExtractAndExpand2(_ck!, _k!, key); |
| | | 168 | | |
| | 20 | 169 | | _ck ??= new SecureMemory(CryptoConstants.PrivkeyLen); |
| | 20 | 170 | | key[..CryptoConstants.PrivkeyLen].CopyTo(_ck); |
| | | 171 | | |
| | 20 | 172 | | _k ??= new SecureMemory(CryptoConstants.PrivkeyLen); |
| | 20 | 173 | | key[CryptoConstants.PrivkeyLen..].CopyTo(_k); |
| | 20 | 174 | | } |
| | | 175 | | |
| | | 176 | | public void Dispose() |
| | | 177 | | { |
| | 152 | 178 | | if (_disposed) |
| | | 179 | | { |
| | 0 | 180 | | return; |
| | | 181 | | } |
| | | 182 | | |
| | 152 | 183 | | _ck?.Dispose(); |
| | 152 | 184 | | _k?.Dispose(); |
| | 152 | 185 | | _hkdf.Dispose(); |
| | 152 | 186 | | _cipher.Dispose(); |
| | | 187 | | |
| | 152 | 188 | | _disposed = true; |
| | 152 | 189 | | } |
| | | 190 | | } |