| | | 1 | | using System; |
| | | 2 | | using Renci.SshNet.Common; |
| | | 3 | | |
| | | 4 | | namespace Renci.SshNet.Security.Cryptography.Ciphers |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Implements RSA cipher algorithm. |
| | | 8 | | /// </summary> |
| | | 9 | | public class RsaCipher : AsymmetricCipher |
| | | 10 | | { |
| | | 11 | | private readonly RsaKey _key; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Initializes a new instance of the <see cref="RsaCipher"/> class. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="key">The RSA key.</param> |
| | 2768 | 17 | | public RsaCipher(RsaKey key) |
| | 2768 | 18 | | { |
| | 2768 | 19 | | if (key is null) |
| | 0 | 20 | | { |
| | 0 | 21 | | throw new ArgumentNullException(nameof(key)); |
| | | 22 | | } |
| | | 23 | | |
| | 2768 | 24 | | _key = key; |
| | 2768 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Encrypts the specified data. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="input">The data.</param> |
| | | 31 | | /// <param name="offset">The zero-based offset in <paramref name="input"/> at which to begin encrypting.</param> |
| | | 32 | | /// <param name="length">The number of bytes to encrypt from <paramref name="input"/>.</param> |
| | | 33 | | /// <returns>Encrypted data.</returns> |
| | | 34 | | public override byte[] Encrypt(byte[] input, int offset, int length) |
| | 696 | 35 | | { |
| | | 36 | | // Calculate signature |
| | 696 | 37 | | var bitLength = _key.Modulus.BitLength; |
| | | 38 | | |
| | 696 | 39 | | var paddedBlock = new byte[(bitLength / 8) + (bitLength % 8 > 0 ? 1 : 0) - 1]; |
| | | 40 | | |
| | 696 | 41 | | paddedBlock[0] = 0x01; |
| | 239440 | 42 | | for (var i = 1; i < paddedBlock.Length - length - 1; i++) |
| | 119024 | 43 | | { |
| | 119024 | 44 | | paddedBlock[i] = 0xFF; |
| | 119024 | 45 | | } |
| | | 46 | | |
| | 696 | 47 | | Buffer.BlockCopy(input, offset, paddedBlock, paddedBlock.Length - length, length); |
| | | 48 | | |
| | 696 | 49 | | return Transform(paddedBlock); |
| | 696 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Decrypts the specified data. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <param name="input">The data.</param> |
| | | 56 | | /// <returns> |
| | | 57 | | /// The decrypted data. |
| | | 58 | | /// </returns> |
| | | 59 | | /// <exception cref="NotSupportedException">Only block type 01 or 02 are supported.</exception> |
| | | 60 | | /// <exception cref="NotSupportedException">Thrown when decrypted block type is not supported.</exception> |
| | | 61 | | public override byte[] Decrypt(byte[] input) |
| | 1210 | 62 | | { |
| | 1210 | 63 | | return Decrypt(input, 0, input.Length); |
| | 1210 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Decrypts the specified input. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="input">The input.</param> |
| | | 70 | | /// <param name="offset">The zero-based offset in <paramref name="input"/> at which to begin decrypting.</param> |
| | | 71 | | /// <param name="length">The number of bytes to decrypt from <paramref name="input"/>.</param> |
| | | 72 | | /// <returns> |
| | | 73 | | /// The decrypted data. |
| | | 74 | | /// </returns> |
| | | 75 | | /// <exception cref="NotSupportedException">Only block type 01 or 02 are supported.</exception> |
| | | 76 | | /// <exception cref="NotSupportedException">Thrown when decrypted block type is not supported.</exception> |
| | | 77 | | public override byte[] Decrypt(byte[] input, int offset, int length) |
| | 1210 | 78 | | { |
| | 1210 | 79 | | var paddedBlock = Transform(input, offset, length); |
| | | 80 | | |
| | 1210 | 81 | | if (paddedBlock[0] is not 1 and not 2) |
| | 0 | 82 | | { |
| | 0 | 83 | | throw new NotSupportedException("Only block type 01 or 02 are supported."); |
| | | 84 | | } |
| | | 85 | | |
| | 1210 | 86 | | var position = 1; |
| | | 87 | | |
| | 360206 | 88 | | while (position < paddedBlock.Length && paddedBlock[position] != 0) |
| | 358996 | 89 | | { |
| | 358996 | 90 | | position++; |
| | 358996 | 91 | | } |
| | | 92 | | |
| | 1210 | 93 | | position++; |
| | | 94 | | |
| | 1210 | 95 | | var result = new byte[paddedBlock.Length - position]; |
| | 1210 | 96 | | Buffer.BlockCopy(paddedBlock, position, result, 0, result.Length); |
| | 1210 | 97 | | return result; |
| | 1210 | 98 | | } |
| | | 99 | | |
| | | 100 | | private byte[] Transform(byte[] data) |
| | 696 | 101 | | { |
| | 696 | 102 | | return Transform(data, 0, data.Length); |
| | 696 | 103 | | } |
| | | 104 | | |
| | | 105 | | private byte[] Transform(byte[] data, int offset, int length) |
| | 1906 | 106 | | { |
| | 1906 | 107 | | Array.Reverse(data, offset, length); |
| | | 108 | | |
| | 1906 | 109 | | var inputBytes = new byte[length + 1]; |
| | 1906 | 110 | | Buffer.BlockCopy(data, offset, inputBytes, 0, length); |
| | | 111 | | |
| | 1906 | 112 | | var input = new BigInteger(inputBytes); |
| | | 113 | | |
| | | 114 | | BigInteger result; |
| | | 115 | | |
| | 1906 | 116 | | var isPrivate = !_key.D.IsZero; |
| | | 117 | | |
| | 1906 | 118 | | if (isPrivate) |
| | 696 | 119 | | { |
| | 696 | 120 | | var random = BigInteger.One; |
| | 696 | 121 | | var max = _key.Modulus - 1; |
| | 696 | 122 | | var bitLength = _key.Modulus.BitLength; |
| | | 123 | | |
| | 696 | 124 | | if (max < BigInteger.One) |
| | 0 | 125 | | { |
| | 0 | 126 | | throw new SshException("Invalid RSA key."); |
| | | 127 | | } |
| | | 128 | | |
| | 1392 | 129 | | while (random <= BigInteger.One || random >= max) |
| | 696 | 130 | | { |
| | 696 | 131 | | random = BigInteger.Random(bitLength); |
| | 696 | 132 | | } |
| | | 133 | | |
| | 696 | 134 | | var blindedInput = BigInteger.PositiveMod(BigInteger.ModPow(random, _key.Exponent, _key.Modulus) * input |
| | | 135 | | |
| | | 136 | | // mP = ((input Mod p) ^ dP)) Mod p |
| | 696 | 137 | | var mP = BigInteger.ModPow(blindedInput % _key.P, _key.DP, _key.P); |
| | | 138 | | |
| | | 139 | | // mQ = ((input Mod q) ^ dQ)) Mod q |
| | 696 | 140 | | var mQ = BigInteger.ModPow(blindedInput % _key.Q, _key.DQ, _key.Q); |
| | | 141 | | |
| | 696 | 142 | | var h = BigInteger.PositiveMod((mP - mQ) * _key.InverseQ, _key.P); |
| | | 143 | | |
| | 696 | 144 | | var m = (h * _key.Q) + mQ; |
| | | 145 | | |
| | 696 | 146 | | var rInv = BigInteger.ModInverse(random, _key.Modulus); |
| | | 147 | | |
| | 696 | 148 | | result = BigInteger.PositiveMod(m * rInv, _key.Modulus); |
| | 696 | 149 | | } |
| | | 150 | | else |
| | 1210 | 151 | | { |
| | 1210 | 152 | | result = BigInteger.ModPow(input, _key.Exponent, _key.Modulus); |
| | 1210 | 153 | | } |
| | | 154 | | |
| | 1906 | 155 | | return result.ToByteArray().Reverse(); |
| | 1906 | 156 | | } |
| | | 157 | | } |
| | | 158 | | } |