forked from sshnet/SSH.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyExchangeECDH.BclImpl.cs
More file actions
77 lines (62 loc) · 2.18 KB
/
KeyExchangeECDH.BclImpl.cs
File metadata and controls
77 lines (62 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#if NET
using System;
using System.Security.Cryptography;
namespace Renci.SshNet.Security
{
internal abstract partial class KeyExchangeECDH
{
private sealed class BclImpl : Impl
{
private readonly ECCurve _curve;
private readonly ECDiffieHellman _clientECDH;
public BclImpl(ECCurve curve)
{
_curve = curve;
_clientECDH = ECDiffieHellman.Create();
}
public override byte[] GenerateClientPublicKey()
{
_clientECDH.GenerateKey(_curve);
var q = _clientECDH.PublicKey.ExportParameters().Q;
return EncodeECPoint(q);
}
public override byte[] CalculateAgreement(byte[] serverPublicKey)
{
var q = DecodeECPoint(serverPublicKey);
var parameters = new ECParameters
{
Curve = _curve,
Q = q,
};
using var serverECDH = ECDiffieHellman.Create(parameters);
return _clientECDH.DeriveRawSecretAgreement(serverECDH.PublicKey);
}
private static byte[] EncodeECPoint(ECPoint point)
{
var q = new byte[1 + point.X.Length + point.Y.Length];
q[0] = 0x04;
Buffer.BlockCopy(point.X, 0, q, 1, point.X.Length);
Buffer.BlockCopy(point.Y, 0, q, point.X.Length + 1, point.Y.Length);
return q;
}
private static ECPoint DecodeECPoint(byte[] q)
{
var cordSize = (q.Length - 1) / 2;
var x = new byte[cordSize];
var y = new byte[cordSize];
Buffer.BlockCopy(q, 1, x, 0, x.Length);
Buffer.BlockCopy(q, cordSize + 1, y, 0, y.Length);
return new ECPoint { X = x, Y = y };
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
_clientECDH.Dispose();
}
}
}
}
}
#endif