Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
EllipticCurveEndpoint.cs
1using System;
2using System.IO;
6
8{
12 public abstract class EllipticCurveEndpoint : E2eEndpoint
13 {
14 private static readonly Cache<string, byte[]> sharedSecrets = new Cache<string, byte[]>(int.MaxValue, TimeSpan.MaxValue, TimeSpan.FromDays(1), true);
15
19 protected readonly byte[] publicKey;
20 private readonly EllipticCurve curve;
21 private readonly bool hasPrivateKey;
22 private readonly string publicKeyBase64;
23
31 {
32 this.curve = Curve;
33 this.publicKey = Curve.PublicKey;
34 this.hasPrivateKey = true;
35 this.publicKeyBase64 = Convert.ToBase64String(this.publicKey);
36 }
37
46 {
47 this.publicKey = PublicKey;
48 this.curve = ReferenceCurve;
49 this.hasPrivateKey = false;
50 this.publicKeyBase64 = Convert.ToBase64String(this.publicKey);
51 }
52
56 public bool HasPrivateKey => this.hasPrivateKey;
57
61 public override byte[] PublicKey => this.publicKey;
62
66 public override string PublicKeyBase64 => this.publicKeyBase64;
67
71 public string CurveName => this.curve.CurveName;
72
76 public EllipticCurve Curve => this.curve;
77
81 public EllipticCurve PrevCurve => (this.Previous as EllipticCurveEndpoint)?.Curve;
82
88 public override byte[] GetSharedSecret(IE2eEndpoint RemoteEndpoint)
89 {
90 return GetSharedKey(this, RemoteEndpoint);
91 }
92
96 public static byte[] GetSharedKey(EllipticCurveEndpoint LocalKey, IE2eEndpoint RemoteKey)
97 {
98 string Key = LocalKey.PublicKeyBase64 + ";" + RemoteKey.PublicKeyBase64;
99
100 if (sharedSecrets.TryGetValue(Key, out byte[] SharedKey))
101 return SharedKey;
102
103 SharedKey = LocalKey.curve.GetSharedKey(RemoteKey.PublicKey, Hashes.ComputeSHA256Hash);
104 sharedSecrets[Key] = SharedKey;
105
106 return SharedKey;
107 }
108
114 public override byte[] Sign(byte[] Data)
115 {
116 if (!this.hasPrivateKey)
117 throw new InvalidOperationException("Signing requires private key.");
118
119 byte[] Signature = this.curve.Sign(Data);
120
121 return Signature;
122 }
123
129 public override byte[] Sign(Stream Data)
130 {
131 if (!this.hasPrivateKey)
132 throw new InvalidOperationException("Signing requires private key.");
133
134 byte[] Signature = this.curve.Sign(Data);
135
136 return Signature;
137 }
138
146 public bool Verify(byte[] Data, byte[] PublicKey, byte[] Signature)
147 {
148 return this.curve.Verify(Data, PublicKey, Signature);
149 }
150
158 public bool Verify(Stream Data, byte[] PublicKey, byte[] Signature)
159 {
160 return this.curve.Verify(Data, PublicKey, Signature);
161 }
162
169 public override bool Verify(byte[] Data, byte[] Signature)
170 {
171 return this.Verify(Data, this.publicKey, Signature);
172 }
173
180 public override bool Verify(Stream Data, byte[] Signature)
181 {
182 return this.Verify(Data, this.publicKey, Signature);
183 }
184
186 public override bool Equals(object obj)
187 {
188 return obj is EllipticCurveEndpoint EcEndpoint &&
189 this.curve.CurveName.Equals(EcEndpoint.curve.CurveName) &&
190 this.publicKeyBase64.Equals(EcEndpoint.publicKeyBase64);
191 }
192
194 public override int GetHashCode()
195 {
196 int Result = this.curve.CurveName.GetHashCode();
197 Result ^= Result << 5 ^ this.publicKeyBase64.GetHashCode();
198
199 return Result;
200 }
201
202 }
203}
Abstract base class for End-to-End encryption schemes.
Definition: E2eEndpoint.cs:12
virtual IE2eSymmetricCipher DefaultSymmetricCipher
Default symmetric cipher.
Definition: E2eEndpoint.cs:265
Abstract base class for Elliptic Curve endpoints.
override byte[] GetSharedSecret(IE2eEndpoint RemoteEndpoint)
Gets a shared secret
override byte[] Sign(Stream Data)
Signs binary data using the local private key.
EllipticCurveEndpoint(EllipticCurve Curve, IE2eSymmetricCipher DefaultSymmetricCipher)
Abstract base class for Elliptic Curve / AES-256 hybrid ciphers.s
bool Verify(byte[] Data, byte[] PublicKey, byte[] Signature)
Verifies a signature.
override string PublicKeyBase64
Remote public key, as a Base64 string.
override bool Verify(Stream Data, byte[] Signature)
Verifies a signature.
override bool Verify(byte[] Data, byte[] Signature)
Verifies a signature.
EllipticCurveEndpoint(byte[] PublicKey, EllipticCurve ReferenceCurve, IE2eSymmetricCipher DefaultSymmetricCipher)
Abstract base class for Elliptic Curve / AES-256 hybrid ciphers.s
bool HasPrivateKey
If the key contains a private key.
static byte[] GetSharedKey(EllipticCurveEndpoint LocalKey, IE2eEndpoint RemoteKey)
Shared secret, for underlying AES cipher.
bool Verify(Stream Data, byte[] PublicKey, byte[] Signature)
Verifies a signature.
override byte[] Sign(byte[] Data)
Signs binary data using the local private key.
Implements an in-memory cache.
Definition: Cache.cs:15
Abstract base class for elliptic curves.
virtual byte[] PublicKey
Encoded public key
virtual byte[] GetSharedKey(byte[] RemotePublicKey, HashFunctionArray HashFunction)
Gets a shared key using the Elliptic Curve Diffie-Hellman (ECDH) algorithm.
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static byte[] ComputeSHA256Hash(byte[] Data)
Computes the SHA-256 hash of a block of binary data.
Definition: Hashes.cs:348
Abstract base class for End-to-End encryption schemes.
Definition: IE2eEndpoint.cs:12
byte[] PublicKey
Remote public key.
Definition: IE2eEndpoint.cs:50
string PublicKeyBase64
Remote public key, as a Base64 string.
Definition: IE2eEndpoint.cs:58
Interface for symmetric ciphers.