|
| 1 | +#if !NET |
| 2 | +#nullable enable |
| 3 | +using System.Security.Cryptography; |
| 4 | + |
| 5 | +using Org.BouncyCastle.Asn1; |
| 6 | +using Org.BouncyCastle.Asn1.Sec; |
| 7 | +using Org.BouncyCastle.Crypto; |
| 8 | +using Org.BouncyCastle.Crypto.Digests; |
| 9 | +using Org.BouncyCastle.Crypto.Parameters; |
| 10 | +using Org.BouncyCastle.Crypto.Signers; |
| 11 | + |
| 12 | +using Renci.SshNet.Common; |
| 13 | + |
| 14 | +namespace Renci.SshNet.Security |
| 15 | +{ |
| 16 | + public partial class EcdsaKey |
| 17 | + { |
| 18 | + private sealed class BouncyCastleImpl : Impl |
| 19 | + { |
| 20 | + private readonly ECPublicKeyParameters _publicKeyParameters; |
| 21 | + private readonly ECPrivateKeyParameters? _privateKeyParameters; |
| 22 | + private readonly DsaDigestSigner _signer; |
| 23 | + |
| 24 | + public BouncyCastleImpl(string curve_oid, byte[] qx, byte[] qy, byte[]? privatekey) |
| 25 | + { |
| 26 | + DerObjectIdentifier oid; |
| 27 | + IDigest digest; |
| 28 | + switch (curve_oid) |
| 29 | + { |
| 30 | + case ECDSA_P256_OID_VALUE: |
| 31 | + oid = SecObjectIdentifiers.SecP256r1; |
| 32 | + digest = new Sha256Digest(); |
| 33 | + KeyLength = 256; |
| 34 | + break; |
| 35 | + case ECDSA_P384_OID_VALUE: |
| 36 | + oid = SecObjectIdentifiers.SecP384r1; |
| 37 | + digest = new Sha384Digest(); |
| 38 | + KeyLength = 384; |
| 39 | + break; |
| 40 | + case ECDSA_P521_OID_VALUE: |
| 41 | + oid = SecObjectIdentifiers.SecP521r1; |
| 42 | + digest = new Sha512Digest(); |
| 43 | + KeyLength = 521; |
| 44 | + break; |
| 45 | + default: |
| 46 | + throw new SshException("Unexpected OID: " + curve_oid); |
| 47 | + } |
| 48 | + |
| 49 | + _signer = new DsaDigestSigner(new ECDsaSigner(), digest, PlainDsaEncoding.Instance); |
| 50 | + |
| 51 | + var x9ECParameters = SecNamedCurves.GetByOid(oid); |
| 52 | + var domainParameter = new ECNamedDomainParameters(oid, x9ECParameters); |
| 53 | + |
| 54 | + if (privatekey != null) |
| 55 | + { |
| 56 | + _privateKeyParameters = new ECPrivateKeyParameters( |
| 57 | + new Org.BouncyCastle.Math.BigInteger(1, privatekey), |
| 58 | + domainParameter); |
| 59 | + |
| 60 | + _publicKeyParameters = new ECPublicKeyParameters( |
| 61 | + domainParameter.G.Multiply(_privateKeyParameters.D).Normalize(), |
| 62 | + domainParameter); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + _publicKeyParameters = new ECPublicKeyParameters( |
| 67 | + x9ECParameters.Curve.CreatePoint( |
| 68 | + new Org.BouncyCastle.Math.BigInteger(1, qx), |
| 69 | + new Org.BouncyCastle.Math.BigInteger(1, qy)), |
| 70 | + domainParameter); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public override byte[]? PrivateKey { get; } |
| 75 | + |
| 76 | + public override ECDsa? Ecdsa { get; } |
| 77 | + |
| 78 | + public override int KeyLength { get; } |
| 79 | + |
| 80 | + public override byte[] Sign(byte[] input) |
| 81 | + { |
| 82 | + _signer.Init(forSigning: true, _privateKeyParameters); |
| 83 | + _signer.BlockUpdate(input, 0, input.Length); |
| 84 | + |
| 85 | + return _signer.GenerateSignature(); |
| 86 | + } |
| 87 | + |
| 88 | + public override bool Verify(byte[] input, byte[] signature) |
| 89 | + { |
| 90 | + _signer.Init(forSigning: false, _publicKeyParameters); |
| 91 | + _signer.BlockUpdate(input, 0, input.Length); |
| 92 | + |
| 93 | + return _signer.VerifySignature(signature); |
| 94 | + } |
| 95 | + |
| 96 | + public override void Export(out byte[] qx, out byte[] qy) |
| 97 | + { |
| 98 | + qx = _publicKeyParameters.Q.XCoord.GetEncoded(); |
| 99 | + qy = _publicKeyParameters.Q.YCoord.GetEncoded(); |
| 100 | + } |
| 101 | + |
| 102 | + protected override void Dispose(bool disposing) |
| 103 | + { |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | +#endif |
0 commit comments