Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
E2eSymmetricCipher.cs
1using System;
2using System.IO;
4using System.Text;
5using System.Threading.Tasks;
6using System.Xml;
10
12{
17 {
21 protected readonly static RandomNumberGenerator rnd = RandomNumberGenerator.Create();
22
26 public abstract string LocalName { get; }
27
32
36 public virtual bool AuthenticatedEncryption => false;
37
41 public virtual void Dispose()
42 {
43 }
44
52 {
53 return Algorithm switch
54 {
55 SymmetricCipherAlgorithms.Aes256 => new Aes256(),
56 SymmetricCipherAlgorithms.ChaCha20 => new ChaCha20(),
57 SymmetricCipherAlgorithms.AeadChaCha20Poly1305 => new AeadChaCha20Poly1305(),
58 _ => throw new ArgumentException("Unrecognized algorithm: " + Algorithm.ToString(), nameof(Algorithm)),
59 };
60 }
61
67 public abstract bool Supported(XmlElement E2e);
68
73 public abstract IE2eSymmetricCipher CreteNew();
74
85 public abstract byte[] GetIV(string Id, string Type, string From, string To, uint Counter);
86
92 protected virtual long GetEncryptedLength(long ContentLength)
93 {
94 return ContentLength;
95 }
96
106 public virtual byte[] Encrypt(byte[] Data, byte[] Key, byte[] IV, byte[] AssociatedData,
107 E2eBufferFillAlgorithm FillAlgorithm)
108 {
109 int c = Data.Length;
110 int d = 0;
111 int i = c;
112
113 do
114 {
115 i >>= 7;
116 d++;
117 }
118 while (i != 0);
119
120 int ContentLen = c + d;
121 int BlockLen = (int)this.GetEncryptedLength(ContentLen);
122 byte[] Encrypted = new byte[BlockLen];
123 int j = 0;
124
125 i = c;
126
127 do
128 {
129 Encrypted[j] = (byte)(i & 127);
130 i >>= 7;
131 if (i != 0)
132 Encrypted[j] |= 0x80;
133
134 j++;
135 }
136 while (i != 0);
137
138 Buffer.BlockCopy(Data, 0, Encrypted, j, c);
139
140 if (ContentLen < BlockLen)
141 {
142 switch (FillAlgorithm)
143 {
144 case E2eBufferFillAlgorithm.Random:
145 default:
146 c = BlockLen - ContentLen;
147 byte[] Bin = new byte[c];
148
149 lock (rnd)
150 {
151 rnd.GetBytes(Bin);
152 }
153
154 Buffer.BlockCopy(Bin, 0, Encrypted, ContentLen, c);
155 break;
156
157 case E2eBufferFillAlgorithm.Zeroes:
158 break;
159 }
160 }
161
162 return Encrypted;
163 }
164
173 public virtual byte[] Decrypt(byte[] Data, byte[] Key, byte[] IV, byte[] AssociatedData)
174 {
175 int c = 0;
176 int i = 0;
177 int Offset = 0;
178 byte b;
179
180 do
181 {
182 b = Data[i++];
183
184 c |= (b & 127) << Offset;
185 Offset += 7;
186 }
187 while (b >= 0x80);
188
189 if (c < 0 || c + i > Data.Length)
190 return null;
191
192 byte[] Decrypted = new byte[c];
193
194 Buffer.BlockCopy(Data, i, Decrypted, 0, c);
195
196 return Decrypted;
197 }
198
207 public virtual async Task Encrypt(Stream Data, Stream Encrypted, byte[] Key, byte[] IV, byte[] AssociatedData)
208 {
209 long c = Data.Length;
210 int d = 0;
211 long i = c;
212
213 do
214 {
215 i >>= 7;
216 d++;
217 }
218 while (i != 0);
219
220 long ContentLen = c + d;
221 long BlockLen = this.GetEncryptedLength(ContentLen);
222 byte b;
223
224 i = c;
225
226 do
227 {
228 b = (byte)(i & 127);
229 i >>= 7;
230 if (i != 0)
231 b |= 0x80;
232
233 Encrypted.WriteByte(b);
234 }
235 while (i != 0);
236
237 await Data.CopyToAsync(Encrypted);
238
239 if (ContentLen < BlockLen)
240 {
241 c = BlockLen - ContentLen;
242 byte[] Bin = new byte[c];
243
244 lock (rnd)
245 {
246 rnd.GetBytes(Bin);
247 }
248
249 await Encrypted.WriteAsync(Bin, 0, (int)c);
250 }
251 }
252
261 public virtual async Task<Stream> Decrypt(Stream Data, byte[] Key, byte[] IV, byte[] AssociatedData)
262 {
263 int c = 0;
264 int i;
265 int Offset = 0;
266
267 do
268 {
269 i = Data.ReadByte();
270 if (i < 0)
271 return null;
272
273 c |= (i & 127) << Offset;
274 Offset += 7;
275 }
276 while (i >= 0x80);
277
278 if (c < 0 || c + Data.Position > Data.Length)
279 return null;
280
281 TemporaryStream Decrypted = new TemporaryStream();
282 try
283 {
284 await Crypto.CopyAsync(Data, Decrypted, c);
285 return Decrypted;
286 }
287 catch (Exception)
288 {
289 Decrypted.Dispose();
290 return null;
291 }
292 }
293
298 public abstract byte[] GenerateKey();
299
313 public virtual byte[] Encrypt(string Id, string Type, string From, string To, uint Counter, byte[] Data, IE2eEndpoint Sender, IE2eEndpoint Receiver)
314 {
315 byte[] Encrypted;
316 byte[] Key;
317 byte[] IV = this.GetIV(Id, Type, From, To, Counter);
318 byte[] AssociatedData = this.AuthenticatedEncryption ? Encoding.UTF8.GetBytes(From) : null;
319 byte[] Signature;
320 byte[] Block;
321 int i, j, k, l;
322 int c = 8;
323
324 Key = Sender.GetSharedSecretForEncryption(Receiver, this, out byte[] KeyCipherText);
325 if (KeyCipherText is null)
326 {
327 l = 0;
328
329 if (Sender.SharedSecretUseCipherText)
330 c += 2;
331 }
332 else
333 {
334 if (!Sender.SharedSecretUseCipherText)
335 throw new InvalidOperationException("Shared secret ciphertexts not supported.");
336
337 l = KeyCipherText.Length;
338 c += l + 2;
339 }
340
341 Encrypted = this.Encrypt(Data, Key, IV, AssociatedData, E2eBufferFillAlgorithm.Random);
342 i = Encrypted.Length;
343 j = 0;
344 c += i;
345
346 if (Sender.SupportsSignatures)
347 {
348 Signature = Sender.Sign(Data);
349 k = Signature.Length;
350 c += k + 1;
351 if (k >= 128)
352 c++;
353 }
354 else
355 {
356 k = 0;
357 Signature = null;
358 }
359
360 Block = new byte[c];
361
362 if (k > 0)
363 {
364 if (k < 128)
365 Block[j++] = (byte)k;
366 else
367 {
368 Block[j++] = (byte)(k | 128);
369 Block[j++] = (byte)(k >> 7);
370 }
371 }
372
373 if (Sender.SharedSecretUseCipherText)
374 {
375 Block[j++] = (byte)l;
376 Block[j++] = (byte)(l >> 8);
377 }
378
379 Block[j++] = (byte)i;
380 Block[j++] = (byte)(i >> 8);
381 Block[j++] = (byte)(i >> 16);
382 Block[j++] = (byte)(i >> 24);
383
384 Block[j++] = (byte)Counter;
385 Block[j++] = (byte)(Counter >> 8);
386 Block[j++] = (byte)(Counter >> 16);
387 Block[j++] = (byte)(Counter >> 24);
388
389 if (k > 0)
390 {
391 Buffer.BlockCopy(Signature, 0, Block, j, k);
392 j += k;
393 }
394
395 if (l > 0)
396 {
397 Buffer.BlockCopy(KeyCipherText, 0, Block, j, l);
398 j += l;
399 }
400
401 Buffer.BlockCopy(Encrypted, 0, Block, j, i);
402
403 return Block;
404 }
405
417 public virtual byte[] Decrypt(string Id, string Type, string From, string To, byte[] Data, IE2eEndpoint Sender, IE2eEndpoint Receiver)
418 {
419 if (Data.Length < 8)
420 return null;
421
422 int SignatureLen;
423 int KeyLen;
424 int DataLen;
425 uint Counter;
426 int i = 0;
427
428 if (Receiver.SupportsSignatures)
429 {
430 SignatureLen = Data[i++];
431 if ((SignatureLen & 128) != 0)
432 {
433 SignatureLen &= 127;
434 SignatureLen |= Data[i++] << 7;
435 }
436 }
437 else
438 SignatureLen = 0;
439
440 if (Receiver.SharedSecretUseCipherText)
441 {
442 KeyLen = Data[i++];
443 KeyLen |= Data[i++] << 8;
444 }
445 else
446 KeyLen = 0;
447
448 if (i + 4 > Data.Length)
449 return null;
450
451 DataLen = Data[i++];
452 DataLen |= Data[i++] << 8;
453 DataLen |= Data[i++] << 16;
454 DataLen |= Data[i++] << 24;
455
456 Counter = Data[i++];
457 Counter |= (uint)(Data[i++] << 8);
458 Counter |= (uint)(Data[i++] << 16);
459 Counter |= (uint)(Data[i++] << 24);
460
461 if (Data.Length != i + SignatureLen + KeyLen + DataLen)
462 return null;
463
464 byte[] Signature = new byte[SignatureLen];
465 byte[] KeyCipherText = KeyLen > 0 ? new byte[KeyLen] : null;
466 byte[] Encrypted = new byte[DataLen];
467 byte[] Key;
468
469 Buffer.BlockCopy(Data, i, Signature, 0, SignatureLen);
470 i += SignatureLen;
471
472 if (KeyLen > 0)
473 {
474 Buffer.BlockCopy(Data, i, KeyCipherText, 0, KeyLen);
475 i += KeyLen;
476 }
477
478 Buffer.BlockCopy(Data, i, Encrypted, 0, DataLen);
479
480 Key = Receiver.GetSharedSecretForDecryption(Sender, KeyCipherText);
481
482 byte[] Decrypted;
483 byte[] IV = this.GetIV(Id, Type, From, To, Counter);
484 byte[] AssociatedData = this.AuthenticatedEncryption ? Encoding.UTF8.GetBytes(From) : null;
485
486 try
487 {
488 Decrypted = this.Decrypt(Encrypted, Key, IV, AssociatedData);
489
490 if (!(Decrypted is null) &&
491 ((Sender.SupportsSignatures && Sender.Verify(Decrypted, Signature)) ||
492 (!Sender.SupportsSignatures && SignatureLen == 0)))
493 {
494 return Decrypted;
495 }
496 }
497 catch (Exception)
498 {
499 // Invalid key
500 }
501
502 if (!(Receiver.Previous is null))
503 {
504 try
505 {
506 Key = Receiver.Previous.GetSharedSecretForDecryption(Sender, KeyCipherText);
507
508 if (!(Key is null))
509 {
510 Decrypted = this.Decrypt(Encrypted, Key, IV, AssociatedData);
511
512 if (!(Decrypted is null) &&
513 ((Sender.SupportsSignatures && Sender.Verify(Decrypted, Signature)) ||
514 (!Sender.SupportsSignatures && SignatureLen == 0)))
515 {
516 return Decrypted;
517 }
518 }
519 }
520 catch (Exception)
521 {
522 // Invalid key
523 }
524 }
525
526 return null;
527 }
528
542 public virtual async Task Encrypt(string Id, string Type, string From, string To, uint Counter, Stream Data, Stream Encrypted, IE2eEndpoint Sender, IE2eEndpoint Receiver)
543 {
544 using TemporaryStream TempEncrypted = new TemporaryStream();
545
546 byte[] Key;
547 byte[] IV = this.GetIV(Id, Type, From, To, Counter);
548 byte[] AssociatedData = this.AuthenticatedEncryption ? Encoding.UTF8.GetBytes(From) : null;
549 byte[] Signature;
550 long i;
551 int k, l;
552
553 Key = Sender.GetSharedSecretForEncryption(Receiver, this, out byte[] KeyCipherText);
554 if (KeyCipherText is null)
555 l = 0;
556 else
557 {
558 if (!Sender.SharedSecretUseCipherText)
559 throw new InvalidOperationException("Shared secret ciphertexts not supported.");
560
561 l = KeyCipherText.Length;
562 }
563
564 await this.Encrypt(Data, TempEncrypted, Key, IV, AssociatedData);
565 i = TempEncrypted.Length;
566
567 if (i > uint.MaxValue)
568 throw new NotSupportedException("Too large.");
569
570 if (Sender.SupportsSignatures)
571 {
572 Data.Position = 0;
573 Signature = Sender.Sign(Data);
574 k = Signature.Length;
575 }
576 else
577 {
578 k = 0;
579 Signature = null;
580 }
581
582 if (k > 0)
583 {
584 if (k < 128)
585 Encrypted.WriteByte((byte)k);
586 else
587 {
588 Encrypted.WriteByte((byte)(k | 128));
589 Encrypted.WriteByte((byte)(k >> 7));
590 }
591 }
592
593 if (Receiver.SharedSecretUseCipherText)
594 {
595 Encrypted.WriteByte((byte)l);
596 Encrypted.WriteByte((byte)(l >> 8));
597 }
598
599 Encrypted.WriteByte((byte)i);
600 Encrypted.WriteByte((byte)(i >> 8));
601 Encrypted.WriteByte((byte)(i >> 16));
602 Encrypted.WriteByte((byte)(i >> 24));
603
604 Encrypted.WriteByte((byte)Counter);
605 Encrypted.WriteByte((byte)(Counter >> 8));
606 Encrypted.WriteByte((byte)(Counter >> 16));
607 Encrypted.WriteByte((byte)(Counter >> 24));
608
609 if (k > 0)
610 await Encrypted.WriteAsync(Signature, 0, k);
611
612 if (l > 0)
613 await Encrypted.WriteAsync(KeyCipherText, 0, l);
614
615 TempEncrypted.Position = 0;
616 await TempEncrypted.CopyToAsync(Encrypted);
617 }
618
630 public virtual async Task<Stream> Decrypt(string Id, string Type, string From, string To, Stream Data, IE2eEndpoint Sender, IE2eEndpoint Receiver)
631 {
632 if (Data.Length < 8)
633 return null;
634
635 int SignatureLen;
636 int KeyLen;
637 int DataLen;
638 uint Counter;
639
640 if (Receiver.SupportsSignatures)
641 {
642 SignatureLen = Data.ReadByte();
643 if ((SignatureLen & 128) != 0)
644 {
645 SignatureLen &= 127;
646 SignatureLen |= Data.ReadByte() << 7;
647 }
648 }
649 else
650 SignatureLen = 0;
651
652 if (Receiver.SharedSecretUseCipherText)
653 {
654 KeyLen = Data.ReadByte();
655 KeyLen |= Data.ReadByte() << 8;
656 }
657 else
658 KeyLen = 0;
659
660 if (Data.Position + 4 > Data.Length)
661 return null;
662
663 DataLen = Data.ReadByte();
664 DataLen |= Data.ReadByte() << 8;
665 DataLen |= Data.ReadByte() << 16;
666 DataLen |= Data.ReadByte() << 24;
667
668 Counter = (byte)Data.ReadByte();
669 Counter |= (uint)(Data.ReadByte() << 8);
670 Counter |= (uint)(Data.ReadByte() << 16);
671 Counter |= (uint)(Data.ReadByte() << 24);
672
673 if (Data.Length != Data.Position + SignatureLen + KeyLen + DataLen)
674 return null;
675
676 byte[] Signature = new byte[SignatureLen];
677 byte[] KeyCipherText = KeyLen > 0 ? new byte[KeyLen] : null;
678 byte[] Key;
679
680 if (await Data.TryReadAllAsync(Signature, 0, SignatureLen) != SignatureLen)
681 return null;
682
683 if (KeyLen > 0)
684 {
685 if (await Data.TryReadAllAsync(KeyCipherText, 0, KeyLen) != KeyLen)
686 return null;
687 }
688
689 using TemporaryStream Encrypted = new TemporaryStream();
690
691 await Crypto.CopyAsync(Data, Encrypted, DataLen);
692
693 Key = Receiver.GetSharedSecretForDecryption(Sender, KeyCipherText);
694
695 byte[] IV = this.GetIV(Id, Type, From, To, Counter);
696 byte[] AssociatedData = this.AuthenticatedEncryption ? Encoding.UTF8.GetBytes(From) : null;
697 Stream Decrypted = null;
698
699 try
700 {
701 Encrypted.Position = 0;
702 Decrypted = await this.Decrypt(Encrypted, Key, IV, AssociatedData);
703
704 if (!(Decrypted is null))
705 {
706 Decrypted.Position = 0;
707
708 if ((Sender.SupportsSignatures && Sender.Verify(Decrypted, Signature)) ||
709 (!Sender.SupportsSignatures && SignatureLen == 0))
710 {
711 return Decrypted;
712 }
713
714 Decrypted.Dispose();
715 Decrypted = null;
716 }
717 }
718 catch (Exception)
719 {
720 // Invalid key
721
722 Decrypted?.Dispose();
723 Decrypted = null;
724 }
725
726 if (!(Receiver.Previous is null))
727 {
728 try
729 {
730 Key = Receiver.Previous.GetSharedSecretForDecryption(Sender, KeyCipherText);
731
732 if (!(Key is null))
733 {
734 Encrypted.Position = 0;
735 Decrypted = await this.Decrypt(Encrypted, Key, IV, AssociatedData);
736
737 if (!(Decrypted is null))
738 {
739 Decrypted.Position = 0;
740
741 if ((Sender.SupportsSignatures && Sender.Verify(Decrypted, Signature)) ||
742 (!Sender.SupportsSignatures && SignatureLen == 0))
743 {
744 return Decrypted;
745 }
746
747 Decrypted.Dispose();
748 Decrypted = null;
749 }
750 }
751 }
752 catch (Exception)
753 {
754 // Invalid key
755
756 Decrypted?.Dispose();
757 Decrypted = null;
758 }
759 }
760
761 return null;
762 }
763
764
779 public virtual bool Encrypt(string Id, string Type, string From, string To, uint Counter, byte[] Data,
780 StringBuilder Xml, IE2eEndpoint Sender, IE2eEndpoint Receiver)
781 {
782 byte[] Encrypted;
783 byte[] Key;
784 byte[] IV = this.GetIV(Id, Type, From, To, Counter);
785 byte[] AssociatedData = this.AuthenticatedEncryption ? Encoding.UTF8.GetBytes(From) : null;
786
787 Xml.Append('<');
788 Xml.Append(this.LocalName);
789 Xml.Append(" xmlns=\"");
790 Xml.Append(this.Namespace);
791 Xml.Append("\" r=\"");
793 {
794 Xml.Append(Sender.Namespace);
795 Xml.Append('#');
796 }
797 Xml.Append(Sender.LocalName);
798 Xml.Append("\" c=\"");
799 Xml.Append(Counter.ToString());
800
801 Key = Sender.GetSharedSecretForEncryption(Receiver, this, out byte[] KeyCipherText);
802
803 if (!(KeyCipherText is null))
804 {
805 if (!Sender.SharedSecretUseCipherText)
806 throw new InvalidOperationException("Shared secret ciphertexts not supported.");
807
808 Xml.Append("\" k=\"");
809 Xml.Append(Convert.ToBase64String(KeyCipherText));
810 }
811
812 Encrypted = this.Encrypt(Data, Key, IV, AssociatedData, E2eBufferFillAlgorithm.Random);
813
814 if (Sender.SupportsSignatures)
815 {
816 byte[] Signature = Sender.Sign(Data);
817
818 Xml.Append("\" s=\"");
819 Xml.Append(Convert.ToBase64String(Signature));
820 }
821
822 Xml.Append("\">");
823 Xml.Append(Convert.ToBase64String(Encrypted));
824 Xml.Append("</");
825 Xml.Append(this.LocalName);
826 Xml.Append('>');
827
828 return true;
829 }
830
842 public virtual string Decrypt(string Id, string Type, string From, string To, XmlElement Xml,
843 IE2eEndpoint Sender, IE2eEndpoint Receiver)
844 {
845 byte[] KeyCipherText = null;
846 byte[] Signature = null;
847 uint? Counter = null;
848
849 foreach (XmlAttribute Attr in Xml.Attributes)
850 {
851 switch (Attr.Name)
852 {
853 case "c":
854 if (!uint.TryParse(Attr.Value, out uint i))
855 return null;
856
857 Counter = i;
858 break;
859
860 case "s":
861 Signature = Convert.FromBase64String(Attr.Value);
862 break;
863
864 case "k":
865 KeyCipherText = Convert.FromBase64String(Attr.Value);
866 break;
867 }
868 }
869
870 if (!Counter.HasValue)
871 return null;
872
873 byte[] Encrypted = Convert.FromBase64String(Xml.InnerText);
874 byte[] Decrypted;
875 byte[] Key;
876
877 Key = Receiver.GetSharedSecretForDecryption(Sender, KeyCipherText);
878
879 byte[] IV = this.GetIV(Id, Type, From, To, Counter.Value);
880 byte[] AssociatedData = this.AuthenticatedEncryption ? Encoding.UTF8.GetBytes(From) : null;
881
882 try
883 {
884 Decrypted = this.Decrypt(Encrypted, Key, IV, AssociatedData);
885
886 if (!(Decrypted is null) &&
887 ((Sender.SupportsSignatures && Sender.Verify(Decrypted, Signature)) ||
888 (!Sender.SupportsSignatures && Signature is null)))
889 {
890 return Encoding.UTF8.GetString(Decrypted);
891 }
892 }
893 catch (Exception)
894 {
895 // Invalid key
896 }
897
898 if (!(Receiver.Previous is null))
899 {
900 try
901 {
902 Key = Receiver.Previous.GetSharedSecretForDecryption(Sender, KeyCipherText);
903
904 if (!(Key is null))
905 {
906 Decrypted = this.Decrypt(Encrypted, Key, IV, AssociatedData);
907
908 if (!(Decrypted is null) &&
909 ((Sender.SupportsSignatures && Sender.Verify(Decrypted, Signature)) ||
910 (!Sender.SupportsSignatures && Signature is null)))
911 {
912 return Encoding.UTF8.GetString(Decrypted);
913 }
914 }
915 }
916 catch (Exception)
917 {
918 // Invalid key
919 }
920 }
921
922 return null;
923 }
924 }
925}
Class managing end-to-end encryption.
const string IoTHarmonizationE2ECurrent
Current namespace for End-to-End encryption.
Implements support for the AEAD-ChaCha20-Poly1305 cipher in hybrid End-to-End encryption schemes.
Implements support for the AES-256 cipher in hybrid End-to-End encryption schemes.
Definition: Aes256.cs:17
Implements support for the ChaCha20 cipher in hybrid End-to-End encryption schemes.
Definition: ChaCha20.cs:17
abstract byte[] GetIV(string Id, string Type, string From, string To, uint Counter)
Gets an Initiation Vector from stanza attributes.
abstract bool Supported(XmlElement E2e)
If the symmetric cipher is supported by a remote endpoint.
static IE2eSymmetricCipher Create(SymmetricCipherAlgorithms Algorithm)
Creates an instance of a symmetric cipher algorithm.
virtual bool Encrypt(string Id, string Type, string From, string To, uint Counter, byte[] Data, StringBuilder Xml, IE2eEndpoint Sender, IE2eEndpoint Receiver)
Encrypts Binary data
virtual async Task Encrypt(string Id, string Type, string From, string To, uint Counter, Stream Data, Stream Encrypted, IE2eEndpoint Sender, IE2eEndpoint Receiver)
Encrypts binary data
virtual string Decrypt(string Id, string Type, string From, string To, XmlElement Xml, IE2eEndpoint Sender, IE2eEndpoint Receiver)
Decrypts XML data
abstract byte[] GenerateKey()
Generates a new key. Used when the asymmetric cipher cannot calculate a shared secret.
virtual byte[] Encrypt(string Id, string Type, string From, string To, uint Counter, byte[] Data, IE2eEndpoint Sender, IE2eEndpoint Receiver)
Encrypts binary data
virtual long GetEncryptedLength(long ContentLength)
Calculates the minimum size of encrypted data, given the size of the content.
virtual bool AuthenticatedEncryption
If Authenticated Encryption with Associated Data is used
abstract IE2eSymmetricCipher CreteNew()
Creates a new symmetric cipher object with the same settings as the current object.
static readonly RandomNumberGenerator rnd
Random number generator.
virtual byte[] Decrypt(byte[] Data, byte[] Key, byte[] IV, byte[] AssociatedData)
Decrypts binary data
abstract string LocalName
Local name of the E2E symmetric cipher
virtual async Task Encrypt(Stream Data, Stream Encrypted, byte[] Key, byte[] IV, byte[] AssociatedData)
Encrypts binary data
virtual byte[] Decrypt(string Id, string Type, string From, string To, byte[] Data, IE2eEndpoint Sender, IE2eEndpoint Receiver)
Decrypts binary data
virtual async Task< Stream > Decrypt(Stream Data, byte[] Key, byte[] IV, byte[] AssociatedData)
Decrypts binary data
virtual string Namespace
Namespace of the E2E symmetric cipher
virtual byte[] Encrypt(byte[] Data, byte[] Key, byte[] IV, byte[] AssociatedData, E2eBufferFillAlgorithm FillAlgorithm)
Encrypts binary data
virtual async Task< Stream > Decrypt(string Id, string Type, string From, string To, Stream Data, IE2eEndpoint Sender, IE2eEndpoint Receiver)
Decrypts binary data
Manages a temporary stream. Contents is kept in-memory, if below a memory threshold,...
override void Dispose(bool disposing)
Releases the unmanaged resources used by the System.IO.Stream and optionally releases the managed res...
Helper methods for encrypting and decrypting streams of data.
Definition: Crypto.cs:14
static async Task< bool > CopyAsync(Stream From, Stream To, long DataLen)
Copies DataLen number of bytes from From to To .
Definition: Crypto.cs:82
Abstract base class for End-to-End encryption schemes.
Definition: IE2eEndpoint.cs:13
bool SupportsSignatures
If signatures are supported.
byte[] Sign(byte[] Data)
Signs binary data using the local private key.
bool Verify(byte[] Data, byte[] Signature)
Verifies a signature.
string Namespace
Namespace of the E2E endpoint
Definition: IE2eEndpoint.cs:27
bool SharedSecretUseCipherText
If the recipient needs a cipher text to generate the same shared secret.
byte[] GetSharedSecretForDecryption(IE2eEndpoint RemoteEndpoint, byte[] CipherText)
Gets a shared secret for decryption.
byte[] GetSharedSecretForEncryption(IE2eEndpoint RemoteEndpoint, IE2eSymmetricCipher Cipher, out byte[] CipherText)
Gets a shared secret for encryption, and optionally a corresponding cipher text.
string LocalName
Local name of the E2E endpoint
Definition: IE2eEndpoint.cs:22
IE2eEndpoint Previous
Previous keys.
Definition: IE2eEndpoint.cs:32
Interface for symmetric ciphers.
Definition: ImplTypes.g.cs:58
SymmetricCipherAlgorithms
Enumeration of symmetric cipher algorithms available in the library.
E2eBufferFillAlgorithm
How buffers are filler before E2E Encryption is performed.