| | | 1 | | #if CRYPTO_NATIVE |
| | | 2 | | using System.Buffers; |
| | | 3 | | using System.Buffers.Binary; |
| | | 4 | | using NLightning.Infrastructure.Crypto.Providers.Native.Constants; |
| | | 5 | | |
| | | 6 | | namespace NLightning.Infrastructure.Crypto.Providers.Native.Ciphers; |
| | | 7 | | |
| | | 8 | | using Converters; |
| | | 9 | | |
| | | 10 | | public static class HChaCha20 |
| | | 11 | | { |
| | 2 | 12 | | private static readonly uint[] s_hChacha20Constant = [0x61707865, 0x3320646E, 0x79622D32, 0x6B206574]; |
| | | 13 | | |
| | | 14 | | public static void CreateInitialState(ReadOnlySpan<byte> key, ReadOnlySpan<byte> nonce, Span<uint> state) |
| | | 15 | | { |
| | 4 | 16 | | if (state.Length != XChaCha20Constants.StateSize) |
| | 0 | 17 | | throw new ArgumentException("State must be 16 bytes long", nameof(state)); |
| | | 18 | | |
| | | 19 | | // set HChaCha20 constant |
| | 4 | 20 | | s_hChacha20Constant.CopyTo(state); |
| | | 21 | | |
| | | 22 | | // set key |
| | 4 | 23 | | Span<uint> keyState = stackalloc uint[8]; |
| | 4 | 24 | | ToUint32LittleEndian(key, keyState); |
| | 4 | 25 | | keyState.CopyTo(state[4..]); |
| | | 26 | | |
| | | 27 | | // set nonce |
| | 4 | 28 | | Span<uint> nonceState = stackalloc uint[4]; |
| | 4 | 29 | | ToUint32LittleEndian(nonce, nonceState); |
| | 4 | 30 | | nonceState.CopyTo(state[^4..]); |
| | 4 | 31 | | } |
| | | 32 | | |
| | | 33 | | public static void PerformRounds(Span<uint> state) |
| | | 34 | | { |
| | 88 | 35 | | for (var i = 0; i < 10; i++) |
| | | 36 | | { |
| | 40 | 37 | | ChaCha20.QuarterRound(ref state[0], ref state[4], ref state[8], ref state[12]); |
| | 40 | 38 | | ChaCha20.QuarterRound(ref state[1], ref state[5], ref state[9], ref state[13]); |
| | 40 | 39 | | ChaCha20.QuarterRound(ref state[2], ref state[6], ref state[10], ref state[14]); |
| | 40 | 40 | | ChaCha20.QuarterRound(ref state[3], ref state[7], ref state[11], ref state[15]); |
| | 40 | 41 | | ChaCha20.QuarterRound(ref state[0], ref state[5], ref state[10], ref state[15]); |
| | 40 | 42 | | ChaCha20.QuarterRound(ref state[1], ref state[6], ref state[11], ref state[12]); |
| | 40 | 43 | | ChaCha20.QuarterRound(ref state[2], ref state[7], ref state[8], ref state[13]); |
| | 40 | 44 | | ChaCha20.QuarterRound(ref state[3], ref state[4], ref state[9], ref state[14]); |
| | | 45 | | } |
| | 4 | 46 | | } |
| | | 47 | | |
| | | 48 | | public static void CreateSubkey(ReadOnlySpan<byte> key, ReadOnlySpan<byte> nonce, Span<byte> subkey) |
| | | 49 | | { |
| | 4 | 50 | | Span<uint> state = stackalloc uint[XChaCha20Constants.StateSize]; |
| | 4 | 51 | | CreateInitialState(key, nonce, state); |
| | 4 | 52 | | PerformRounds(state); |
| | | 53 | | |
| | 4 | 54 | | FromUint32LittleEndian([state[0], state[1], state[2], state[3], state[12], state[13], state[14], state[15]], |
| | 4 | 55 | | subkey); |
| | 4 | 56 | | } |
| | | 57 | | |
| | | 58 | | private static void ToUint32LittleEndian(ReadOnlySpan<byte> buffer, Span<uint> output) |
| | | 59 | | { |
| | 8 | 60 | | var temp = ArrayPool<byte>.Shared.Rent(4); |
| | | 61 | | try |
| | | 62 | | { |
| | 8 | 63 | | var pos = 0; |
| | | 64 | | |
| | 8 | 65 | | using var ms = new MemoryStream(buffer.ToArray()); |
| | 56 | 66 | | while (pos != output.Length) |
| | | 67 | | { |
| | 48 | 68 | | ms.ReadExactly(temp, 0, 4); |
| | 48 | 69 | | output[pos] = EndianBitConverter.ToUInt32LittleEndian(temp[..4]); |
| | 48 | 70 | | pos += 1; |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | finally |
| | | 74 | | { |
| | 8 | 75 | | ArrayPool<byte>.Shared.Return(temp, true); |
| | 8 | 76 | | } |
| | 8 | 77 | | } |
| | | 78 | | |
| | | 79 | | private static void FromUint32LittleEndian(ReadOnlySpan<uint> input, Span<byte> output) |
| | | 80 | | { |
| | 72 | 81 | | for (var i = 0; i < input.Length; i++) |
| | | 82 | | { |
| | 32 | 83 | | var u = input[i]; |
| | 32 | 84 | | var temp = EndianBitConverter.GetBytesLittleEndian(u); |
| | 32 | 85 | | BinaryPrimitives.WriteUInt32LittleEndian(temp, u); |
| | 32 | 86 | | temp.CopyTo(output[(i * 4)..]); |
| | | 87 | | } |
| | 4 | 88 | | } |
| | | 89 | | } |
| | | 90 | | #endif |