|
| 1 | +using System; |
| 2 | +using System.Buffers.Binary; |
| 3 | +using System.Diagnostics; |
| 4 | + |
| 5 | +using Org.BouncyCastle.Crypto.Engines; |
| 6 | +using Org.BouncyCastle.Crypto.Macs; |
| 7 | +using Org.BouncyCastle.Crypto.Parameters; |
| 8 | +using Org.BouncyCastle.Utilities; |
| 9 | + |
| 10 | +using Renci.SshNet.Common; |
| 11 | +using Renci.SshNet.Messages.Transport; |
| 12 | + |
| 13 | +namespace Renci.SshNet.Security.Cryptography.Ciphers |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// ChaCha20Poly1305 cipher implementation. |
| 17 | + /// <see href="https://datatracker.ietf.org/doc/html/draft-josefsson-ssh-chacha20-poly1305-openssh-00"/>. |
| 18 | + /// </summary> |
| 19 | + internal sealed class ChaCha20Poly1305Cipher : SymmetricCipher |
| 20 | + { |
| 21 | + private readonly ChaCha7539Engine _aadCipher = new ChaCha7539Engine(); |
| 22 | + private readonly ChaCha7539Engine _cipher = new ChaCha7539Engine(); |
| 23 | + private readonly Poly1305 _mac = new Poly1305(); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Gets the minimun block size. |
| 27 | + /// </summary> |
| 28 | + public override byte MinimumSize |
| 29 | + { |
| 30 | + get |
| 31 | + { |
| 32 | + return 16; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Gets the tag size in bytes. |
| 38 | + /// Poly1305 [Poly1305], also by Daniel Bernstein, is a one-time Carter- |
| 39 | + /// Wegman MAC that computes a 128 bit integrity tag given a message |
| 40 | + /// <see href="https://datatracker.ietf.org/doc/html/draft-josefsson-ssh-chacha20-poly1305-openssh-00#section-1"/>. |
| 41 | + /// </summary> |
| 42 | + public override int TagSize |
| 43 | + { |
| 44 | + get |
| 45 | + { |
| 46 | + return 16; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Initializes a new instance of the <see cref="ChaCha20Poly1305Cipher"/> class. |
| 52 | + /// </summary> |
| 53 | + /// <param name="key">The key.</param> |
| 54 | + public ChaCha20Poly1305Cipher(byte[] key) |
| 55 | + : base(key) |
| 56 | + { |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Encrypts the specified input. |
| 61 | + /// </summary> |
| 62 | + /// <param name="input"> |
| 63 | + /// The input data with below format: |
| 64 | + /// <code> |
| 65 | + /// [outbound sequence field][packet length field][padding length field sz][payload][random paddings] |
| 66 | + /// [----4 bytes----(offset)][------4 bytes------][----------------Plain Text---------------(length)] |
| 67 | + /// </code> |
| 68 | + /// </param> |
| 69 | + /// <param name="offset">The zero-based offset in <paramref name="input"/> at which to begin encrypting.</param> |
| 70 | + /// <param name="length">The number of bytes to encrypt from <paramref name="input"/>.</param> |
| 71 | + /// <returns> |
| 72 | + /// The encrypted data with below format: |
| 73 | + /// <code> |
| 74 | + /// [packet length field][padding length field sz][payload][random paddings][Authenticated TAG] |
| 75 | + /// [------4 bytes------][------------------Cipher Text--------------------][-------TAG-------] |
| 76 | + /// </code> |
| 77 | + /// </returns> |
| 78 | + public override byte[] Encrypt(byte[] input, int offset, int length) |
| 79 | + { |
| 80 | + var output = new byte[length + TagSize]; |
| 81 | + |
| 82 | + _aadCipher.ProcessBytes(input, offset, 4, output, 0); |
| 83 | + _cipher.ProcessBytes(input, offset + 4, length - 4, output, 4); |
| 84 | + |
| 85 | + _mac.BlockUpdate(output, 0, length); |
| 86 | + _ = _mac.DoFinal(output, length); |
| 87 | + |
| 88 | + return output; |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Decrypts the first block which is packet length field. |
| 93 | + /// </summary> |
| 94 | + /// <param name="input">The encrypted packet length field.</param> |
| 95 | + /// <returns>The decrypted packet length field.</returns> |
| 96 | + public override byte[] Decrypt(byte[] input) |
| 97 | + { |
| 98 | + var output = new byte[input.Length]; |
| 99 | + _aadCipher.ProcessBytes(input, 0, input.Length, output, 0); |
| 100 | + |
| 101 | + return output; |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Decrypts the specified input. |
| 106 | + /// </summary> |
| 107 | + /// <param name="input"> |
| 108 | + /// The input data with below format: |
| 109 | + /// <code> |
| 110 | + /// [inbound sequence field][packet length field][padding length field sz][payload][random paddings][Authenticated TAG] |
| 111 | + /// [--------4 bytes-------][--4 bytes--(offset)][--------------Cipher Text----------------(length)][-------TAG-------] |
| 112 | + /// </code> |
| 113 | + /// </param> |
| 114 | + /// <param name="offset">The zero-based offset in <paramref name="input"/> at which to begin decrypting and authenticating.</param> |
| 115 | + /// <param name="length">The number of bytes to decrypt and authenticate from <paramref name="input"/>.</param> |
| 116 | + /// <returns> |
| 117 | + /// The decrypted data with below format: |
| 118 | + /// <code> |
| 119 | + /// [padding length field sz][payload][random paddings] |
| 120 | + /// [--------------------Plain Text-------------------] |
| 121 | + /// </code> |
| 122 | + /// </returns> |
| 123 | + public override byte[] Decrypt(byte[] input, int offset, int length) |
| 124 | + { |
| 125 | + Debug.Assert(offset == 8, "The offset must be 8"); |
| 126 | + |
| 127 | + var tag = new byte[TagSize]; |
| 128 | + _mac.BlockUpdate(input, offset - 4, length + 4); |
| 129 | + _ = _mac.DoFinal(tag, 0); |
| 130 | + if (!Arrays.FixedTimeEquals(TagSize, tag, 0, input, offset + length)) |
| 131 | + { |
| 132 | + throw new SshConnectionException("MAC error", DisconnectReason.MacError); |
| 133 | + } |
| 134 | + |
| 135 | + var output = new byte[length]; |
| 136 | + _cipher.ProcessBytes(input, offset, length, output, 0); |
| 137 | + |
| 138 | + return output; |
| 139 | + } |
| 140 | + |
| 141 | + internal override void SetSequenceNumber(uint sequenceNumber) |
| 142 | + { |
| 143 | + var iv = new byte[12]; |
| 144 | + BinaryPrimitives.WriteUInt64BigEndian(iv.AsSpan(4), sequenceNumber); |
| 145 | + |
| 146 | + // ChaCha20 encryption and decryption is completely |
| 147 | + // symmetrical, so the 'forEncryption' is |
| 148 | + // irrelevant. (Like 90% of stream ciphers) |
| 149 | + _aadCipher.Init(forEncryption: true, new ParametersWithIV(new KeyParameter(Key, 32, 32), iv)); |
| 150 | + _cipher.Init(forEncryption: true, new ParametersWithIV(new KeyParameter(Key, 0, 32), iv)); |
| 151 | + |
| 152 | + var keyStream = new byte[64]; |
| 153 | + _cipher.ProcessBytes(keyStream, 0, keyStream.Length, keyStream, 0); |
| 154 | + _mac.Init(new KeyParameter(keyStream, 0, 32)); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments