Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
E2eEndpoint.cs
1using System;
2using System.IO;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
7
9{
13 public abstract class E2eEndpoint : IE2eEndpoint
14 {
15 private const string E2eCounterName = "E2EE.Counter";
16
17 private IE2eSymmetricCipher defaultSymmetricCipher;
18 private IE2eEndpoint prev = null;
19
25 {
26 this.defaultSymmetricCipher = DefaultSymmetricCipher;
27 }
28
32 public virtual void Dispose()
33 {
34 this.defaultSymmetricCipher?.Dispose();
35 this.defaultSymmetricCipher = null;
36 }
37
41 public abstract int SecurityStrength { get; }
42
47 {
48 get => this.prev;
49 set => this.prev = value;
50 }
51
55 public abstract string LocalName { get; }
56
61
65 public abstract byte[] PublicKey { get; }
66
70 public abstract string PublicKeyBase64 { get; }
71
77 public abstract IE2eEndpoint Create(int SecurityStrength);
78
84 public abstract IE2eEndpoint CreatePrivate(byte[] Secret);
85
91 public abstract IE2eEndpoint CreatePublic(byte[] PublicKey);
92
98 public IE2eEndpoint Parse(XmlElement Xml)
99 {
100 foreach (XmlAttribute Attr in Xml.Attributes)
101 {
102 switch (Attr.Name)
103 {
104 case "pub":
105 return this.CreatePublic(Convert.FromBase64String(Attr.Value));
106
107 case "d":
108 return this.CreatePrivate(Convert.FromBase64String(Attr.Value));
109 }
110 }
111
112 return null;
113 }
114
115
119 public string ToXml()
120 {
121 return this.ToXml(string.Empty);
122 }
123
128 public string ToXml(string ParentNamespace)
129 {
130 StringBuilder Xml = new StringBuilder();
131 this.ToXml(Xml, ParentNamespace);
132 return Xml.ToString();
133 }
134
140 public void ToXml(StringBuilder Xml, string ParentNamespace)
141 {
142 Xml.Append('<');
143 Xml.Append(this.LocalName);
144 Xml.Append(" pub=\"");
145 Xml.Append(this.PublicKeyBase64);
146
147 string ns = this.Namespace;
148 if (ns != ParentNamespace)
149 {
150 Xml.Append("\" xmlns=\"");
151 Xml.Append(ns);
152 }
153
154 Xml.Append("\"/>");
155 }
156
165 public abstract byte[] GetSharedSecretForEncryption(IE2eEndpoint RemoteEndpoint,
166 IE2eSymmetricCipher Cipher, out byte[] CipherText);
167
175 public abstract byte[] GetSharedSecretForDecryption(IE2eEndpoint RemoteEndpoint,
176 byte[] CipherText);
177
181 public abstract bool SharedSecretUseCipherText
182 {
183 get;
184 }
185
189 public virtual bool SupportsSignatures => true;
190
196 public abstract byte[] Sign(byte[] Data);
197
203 public abstract byte[] Sign(Stream Data);
204
211 public abstract bool Verify(byte[] Data, byte[] Signature);
212
219 public abstract bool Verify(Stream Data, byte[] Signature);
220
224 public virtual bool Safe => true;
225
229 public virtual bool Slow => false;
230
234 public virtual bool PostQuantumCryptography => false;
235
237 public override string ToString()
238 {
239 StringBuilder Xml = new StringBuilder();
240 this.ToXml(Xml, string.Empty);
241 return Xml.ToString();
242 }
243
245 public override abstract bool Equals(object obj);
246
248 public override abstract int GetHashCode();
249
253 public int Score
254 {
255 get
256 {
257 int Result = 0;
258
260 Result++;
261
262 if (this.SupportsSignatures)
263 Result++;
264
265 if (this.Safe)
266 Result++;
267
268 if (!this.Slow)
269 Result++;
270
271 if (this.PostQuantumCryptography)
272 Result += 2; // To overcome requirement of using cipher texts to derive shared secrets.
273
274 return Result;
275 }
276 }
277
282 {
283 get => this.defaultSymmetricCipher;
284 set => this.defaultSymmetricCipher = value ?? this.defaultSymmetricCipher;
285 }
286
291 public async Task<uint> GetNextCounter()
292 {
293 return (uint)await RuntimeCounters.IncrementCounter(E2eCounterName);
294 }
295 }
296}
Abstract base class for End-to-End encryption schemes.
Definition: E2eEndpoint.cs:14
abstract byte[] Sign(Stream Data)
Signs binary data using the local private key.
virtual bool Slow
If implementation is slow, compared to other options.
Definition: E2eEndpoint.cs:229
async Task< uint > GetNextCounter()
Gets the next counter value.
Definition: E2eEndpoint.cs:291
virtual bool SupportsSignatures
If signatures are supported.
Definition: E2eEndpoint.cs:189
abstract byte[] Sign(byte[] Data)
Signs binary data using the local private key.
abstract bool SharedSecretUseCipherText
If the recipient needs a cipher text to generate the same shared secret.
Definition: E2eEndpoint.cs:182
virtual IE2eSymmetricCipher DefaultSymmetricCipher
Default symmetric cipher.
Definition: E2eEndpoint.cs:282
abstract IE2eEndpoint Create(int SecurityStrength)
Creates a new key.
abstract bool Verify(Stream Data, byte[] Signature)
Verifies a signature.
virtual bool Safe
If endpoint is considered safe (i.e. there are no suspected backdoors)
Definition: E2eEndpoint.cs:224
abstract byte[] GetSharedSecretForEncryption(IE2eEndpoint RemoteEndpoint, IE2eSymmetricCipher Cipher, out byte[] CipherText)
Gets a shared secret for encryption, and optionally a corresponding cipher text.
string ToXml(string ParentNamespace)
Exports the public key information to XML.
Definition: E2eEndpoint.cs:128
virtual void Dispose()
IDisposable.Dispose
Definition: E2eEndpoint.cs:32
abstract IE2eEndpoint CreatePublic(byte[] PublicKey)
Creates a new endpoint given a public key.
abstract IE2eEndpoint CreatePrivate(byte[] Secret)
Creates a new endpoint given a private key.
abstract override bool Equals(object obj)
virtual bool PostQuantumCryptography
If post-quantum cryptography is used.
Definition: E2eEndpoint.cs:234
abstract bool Verify(byte[] Data, byte[] Signature)
Verifies a signature.
void ToXml(StringBuilder Xml, string ParentNamespace)
Exports the public key information to XML.
Definition: E2eEndpoint.cs:140
IE2eEndpoint Parse(XmlElement Xml)
Parses endpoint information from an XML element.
Definition: E2eEndpoint.cs:98
E2eEndpoint(IE2eSymmetricCipher DefaultSymmetricCipher)
Abstract base class for End-to-End encryption schemes.
Definition: E2eEndpoint.cs:24
virtual string Namespace
Namespace of the E2E encryption scheme
Definition: E2eEndpoint.cs:60
string ToXml()
Exports the public key information to XML.
Definition: E2eEndpoint.cs:119
abstract byte[] GetSharedSecretForDecryption(IE2eEndpoint RemoteEndpoint, byte[] CipherText)
Gets a shared secret for decryption.
int Score
Provides a score for the endpoint. More features, higher score.
Definition: E2eEndpoint.cs:254
abstract string PublicKeyBase64
Remote public key, as a Base64 string.
Definition: E2eEndpoint.cs:70
abstract int SecurityStrength
Security strength of End-to-End encryption scheme.
Definition: E2eEndpoint.cs:41
IE2eEndpoint Previous
Previous keys.
Definition: E2eEndpoint.cs:47
abstract string LocalName
Local name of the E2E encryption scheme
Definition: E2eEndpoint.cs:55
abstract byte[] PublicKey
Remote public key.
Definition: E2eEndpoint.cs:65
Class managing end-to-end encryption.
const string IoTHarmonizationE2ECurrent
Current namespace for End-to-End encryption.
Static class managing persistent counters.
static Task< long > IncrementCounter(CaseInsensitiveString Key)
Increments a counter.
Abstract base class for End-to-End encryption schemes.
Definition: IE2eEndpoint.cs:13
Interface for symmetric ciphers.