| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | using Renci.SshNet.Common; |
| | | 4 | | using Renci.SshNet.Security.Chaos.NaCl; |
| | | 5 | | using Renci.SshNet.Security.Cryptography; |
| | | 6 | | |
| | | 7 | | namespace Renci.SshNet.Security |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Contains ED25519 private and public key. |
| | | 11 | | /// </summary> |
| | | 12 | | public class ED25519Key : Key, IDisposable |
| | | 13 | | { |
| | | 14 | | #pragma warning disable IDE1006 // Naming Styles |
| | | 15 | | #pragma warning disable SX1309 // Field names should begin with underscore |
| | 10 | 16 | | private readonly byte[] privateKey = new byte[Ed25519.ExpandedPrivateKeySizeInBytes]; |
| | | 17 | | #pragma warning restore SX1309 // Field names should begin with underscore |
| | | 18 | | #pragma warning restore IDE1006 // Naming Styles |
| | | 19 | | private ED25519DigitalSignature _digitalSignature; |
| | 10 | 20 | | private byte[] _publicKey = new byte[Ed25519.PublicKeySizeInBytes]; |
| | | 21 | | private bool _isDisposed; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the name of the key. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <returns> |
| | | 27 | | /// The name of the key. |
| | | 28 | | /// </returns> |
| | | 29 | | public override string ToString() |
| | 7 | 30 | | { |
| | 7 | 31 | | return "ssh-ed25519"; |
| | 7 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets or sets the public. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <value> |
| | | 38 | | /// The public. |
| | | 39 | | /// </value> |
| | | 40 | | public override BigInteger[] Public |
| | | 41 | | { |
| | | 42 | | get |
| | 4 | 43 | | { |
| | 4 | 44 | | return new BigInteger[] { _publicKey.ToBigInteger2() }; |
| | 4 | 45 | | } |
| | | 46 | | set |
| | 3 | 47 | | { |
| | 3 | 48 | | _publicKey = value[0].ToByteArray().Reverse().TrimLeadingZeros().Pad(Ed25519.PublicKeySizeInBytes); |
| | 3 | 49 | | } |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets the length of the key. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <value> |
| | | 56 | | /// The length of the key. |
| | | 57 | | /// </value> |
| | | 58 | | public override int KeyLength |
| | | 59 | | { |
| | | 60 | | get |
| | 3 | 61 | | { |
| | 3 | 62 | | return PublicKey.Length * 8; |
| | 3 | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets the digital signature. |
| | | 68 | | /// </summary> |
| | | 69 | | protected internal override DigitalSignature DigitalSignature |
| | | 70 | | { |
| | | 71 | | get |
| | 10 | 72 | | { |
| | 10 | 73 | | _digitalSignature ??= new ED25519DigitalSignature(this); |
| | 10 | 74 | | return _digitalSignature; |
| | 10 | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Gets the PublicKey Bytes. |
| | | 80 | | /// </summary> |
| | | 81 | | public byte[] PublicKey |
| | | 82 | | { |
| | | 83 | | get |
| | 6 | 84 | | { |
| | 6 | 85 | | return _publicKey; |
| | 6 | 86 | | } |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Gets the PrivateKey Bytes. |
| | | 91 | | /// </summary> |
| | | 92 | | public byte[] PrivateKey |
| | | 93 | | { |
| | | 94 | | get |
| | 1 | 95 | | { |
| | 1 | 96 | | return privateKey; |
| | 1 | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Initializes a new instance of the <see cref="ED25519Key"/> class. |
| | | 102 | | /// </summary> |
| | 3 | 103 | | public ED25519Key() |
| | 3 | 104 | | { |
| | 3 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Initializes a new instance of the <see cref="ED25519Key"/> class. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <param name="pk">pk data.</param> |
| | 0 | 111 | | public ED25519Key(byte[] pk) |
| | 0 | 112 | | { |
| | 0 | 113 | | _publicKey = pk.TrimLeadingZeros().Pad(Ed25519.PublicKeySizeInBytes); |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Initializes a new instance of the <see cref="ED25519Key"/> class. |
| | | 118 | | /// </summary> |
| | | 119 | | /// <param name="pk">pk data.</param> |
| | | 120 | | /// <param name="sk">sk data.</param> |
| | 7 | 121 | | public ED25519Key(byte[] pk, byte[] sk) |
| | 7 | 122 | | { |
| | 7 | 123 | | _publicKey = pk.TrimLeadingZeros().Pad(Ed25519.PublicKeySizeInBytes); |
| | 7 | 124 | | var seed = new byte[Ed25519.PrivateKeySeedSizeInBytes]; |
| | 7 | 125 | | Buffer.BlockCopy(sk, 0, seed, 0, seed.Length); |
| | 7 | 126 | | Ed25519.KeyPairFromSeed(out _publicKey, out privateKey, seed); |
| | 7 | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 131 | | /// </summary> |
| | | 132 | | public void Dispose() |
| | 0 | 133 | | { |
| | 0 | 134 | | Dispose(disposing: true); |
| | 0 | 135 | | GC.SuppressFinalize(this); |
| | 0 | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 140 | | /// </summary> |
| | | 141 | | /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor |
| | | 142 | | protected virtual void Dispose(bool disposing) |
| | 10 | 143 | | { |
| | 10 | 144 | | if (_isDisposed) |
| | 0 | 145 | | { |
| | 0 | 146 | | return; |
| | | 147 | | } |
| | | 148 | | |
| | 10 | 149 | | if (disposing) |
| | 0 | 150 | | { |
| | 0 | 151 | | _isDisposed = true; |
| | 0 | 152 | | } |
| | 10 | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Finalizes an instance of the <see cref="ED25519Key"/> class. |
| | | 157 | | /// </summary> |
| | | 158 | | ~ED25519Key() |
| | 20 | 159 | | { |
| | 10 | 160 | | Dispose(disposing: false); |
| | 20 | 161 | | } |
| | | 162 | | } |
| | | 163 | | } |