| | 1 | | using System.Diagnostics; |
| | 2 | |
|
| | 3 | | namespace NLightning.Infrastructure.Transport.Handshake.States; |
| | 4 | |
|
| | 5 | | using Common.Utils; |
| | 6 | | using Crypto.Ciphers; |
| | 7 | | using Crypto.Functions; |
| | 8 | | using Crypto.Primitives; |
| | 9 | | using Domain.Crypto.Constants; |
| | 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 MAX_NONCE = 1000; |
| | 18 | |
|
| 192 | 19 | | private readonly ChaCha20Poly1305 _cipher = new(); |
| 192 | 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 | | { |
| 264 | 32 | | ExceptionUtils.ThrowIfDisposed(_disposed, nameof(CipherState)); |
| | 33 | |
|
| | 34 | | Debug.Assert(key.Length == CryptoConstants.PRIVKEY_LEN); |
| | 35 | | Debug.Assert(chainingKey.Length == CryptoConstants.PRIVKEY_LEN); |
| | 36 | |
|
| 264 | 37 | | _k ??= new SecureMemory(CryptoConstants.PRIVKEY_LEN); |
| 264 | 38 | | key.CopyTo(_k); |
| | 39 | |
|
| 264 | 40 | | _ck ??= new SecureMemory(CryptoConstants.PRIVKEY_LEN); |
| 264 | 41 | | chainingKey.CopyTo(_ck); |
| | 42 | |
|
| 264 | 43 | | _n = 0; |
| 264 | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Returns true if k and ck are non-empty, false otherwise. |
| | 48 | | /// </summary> |
| | 49 | | public bool HasKeys() |
| | 50 | | { |
| 16308 | 51 | | ExceptionUtils.ThrowIfDisposed(_disposed, nameof(CipherState)); |
| | 52 | |
|
| 16308 | 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 | | { |
| 128 | 73 | | ExceptionUtils.ThrowIfDisposed(_disposed, nameof(CipherState)); |
| | 74 | |
|
| 128 | 75 | | if (_n == MAX_NONCE) |
| | 76 | | { |
| 4 | 77 | | throw new OverflowException("Nonce has reached its maximum value."); |
| | 78 | | } |
| | 79 | |
|
| 124 | 80 | | if (_k != null) |
| | 81 | | { |
| 120 | 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 | | { |
| 124 | 97 | | ExceptionUtils.ThrowIfDisposed(_disposed, nameof(CipherState)); |
| | 98 | |
|
| | 99 | | // If nonce reaches its maximum value, rekey |
| 124 | 100 | | if (_n == MAX_NONCE) |
| | 101 | | { |
| 0 | 102 | | throw new OverflowException("Nonce has reached its maximum value."); |
| | 103 | | } |
| | 104 | |
|
| 124 | 105 | | if (_k == null) |
| | 106 | | { |
| 4 | 107 | | ciphertext.CopyTo(plaintext); |
| 4 | 108 | | return ciphertext.Length; |
| | 109 | | } |
| | 110 | |
|
| 120 | 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 == MAX_NONCE) |
| | 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 == MAX_NONCE) |
| | 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.PRIVKEY_LEN * 2]; |
| 20 | 167 | | _hkdf.ExtractAndExpand2(_ck!, _k!, key); |
| | 168 | |
|
| 20 | 169 | | _ck ??= new SecureMemory(CryptoConstants.PRIVKEY_LEN); |
| 20 | 170 | | key[..CryptoConstants.PRIVKEY_LEN].CopyTo(_ck); |
| | 171 | |
|
| 20 | 172 | | _k ??= new SecureMemory(CryptoConstants.PRIVKEY_LEN); |
| 20 | 173 | | key[CryptoConstants.PRIVKEY_LEN..].CopyTo(_k); |
| 20 | 174 | | } |
| | 175 | |
|
| | 176 | | public void Dispose() |
| | 177 | | { |
| 144 | 178 | | if (_disposed) |
| | 179 | | { |
| 0 | 180 | | return; |
| | 181 | | } |
| | 182 | |
|
| 144 | 183 | | _ck?.Dispose(); |
| 144 | 184 | | _k?.Dispose(); |
| 144 | 185 | | _hkdf.Dispose(); |
| 144 | 186 | | _cipher.Dispose(); |
| | 187 | |
|
| 144 | 188 | | _disposed = true; |
| 144 | 189 | | } |
| | 190 | | } |