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;
3using System.Security.Cryptography;
4using System.Text;
5using System.Threading.Tasks;
6using System.Xml;
9
11{
16 {
20 protected readonly static RandomNumberGenerator rnd = RandomNumberGenerator.Create();
21
25 public abstract string LocalName
26 {
27 get;
28 }
29
34
38 public virtual bool AuthenticatedEncryption => false;
39
43 public virtual void Dispose()
44 {
45 }
46
54 {
55 switch (Algorithm)
56 {
57 case SymmetricCipherAlgorithms.Aes256: return new Aes256();
58 case SymmetricCipherAlgorithms.ChaCha20: return new ChaCha20();
59 case SymmetricCipherAlgorithms.AeadChaCha20Poly1305: return new AeadChaCha20Poly1305();
60 default: throw new ArgumentException("Unrecognized algorithm: " + Algorithm.ToString(), nameof(Algorithm));
61 }
62 }
63
68 public abstract IE2eSymmetricCipher CreteNew();
69
80 public abstract byte[] GetIV(string Id, string Type, string From, string To, uint Counter);
81
87 protected virtual long GetEncryptedLength(long ContentLength)
88 {
89 return ContentLength;
90 }
91
101 public virtual byte[] Encrypt(byte[] Data, byte[] Key, byte[] IV, byte[] AssociatedData,
102 E2eBufferFillAlgorithm FillAlgorithm)
103 {
104 int c = Data.Length;
105 int d = 0;
106 int i = c;
107
108 do
109 {
110 i >>= 7;
111 d++;
112 }
113 while (i != 0);
114
115 int ContentLen = c + d;
116 int BlockLen = (int)this.GetEncryptedLength(ContentLen);
117 byte[] Encrypted = new byte[BlockLen];
118 int j = 0;
119
120 i = c;
121
122 do
123 {
124 Encrypted[j] = (byte)(i & 127);
125 i >>= 7;
126 if (i != 0)
127 Encrypted[j] |= 0x80;
128
129 j++;
130 }
131 while (i != 0);
132
133 Array.Copy(Data, 0, Encrypted, j, c);
134
135 if (ContentLen < BlockLen)
136 {
137 switch (FillAlgorithm)
138 {
139 case E2eBufferFillAlgorithm.Random:
140 default:
141 c = BlockLen - ContentLen;
142 byte[] Bin = new byte[c];
143
144 lock (rnd)
145 {
146 rnd.GetBytes(Bin);
147 }
148
149 Array.Copy(Bin, 0, Encrypted, ContentLen, c);
150 break;
151
152 case E2eBufferFillAlgorithm.Zeroes:
153 break;
154 }
155 }
156
157 return Encrypted;
158 }
159
168 public virtual byte[] Decrypt(byte[] Data, byte[] Key, byte[] IV, byte[] AssociatedData)
169 {
170 int c = 0;
171 int i = 0;
172 int Offset = 0;
173 byte b;
174
175 do
176 {
177 b = Data[i++];
178
179 c |= (b & 127) << Offset;
180 Offset += 7;
181 }
182 while (b >= 0x80);
183
184 if (c < 0 || c + i > Data.Length)
185 return null;
186
187 byte[] Decrypted = new byte[c];
188
189 Array.Copy(Data, i, Decrypted, 0, c);
190
191 return Decrypted;
192 }
193
202 public virtual async Task Encrypt(Stream Data, Stream Encrypted, byte[] Key, byte[] IV, byte[] AssociatedData)
203 {
204 long c = Data.Length;
205 int d = 0;
206 long i = c;
207
208 do
209 {
210 i >>= 7;
211 d++;
212 }
213 while (i != 0);
214
215 long ContentLen = c + d;
216 long BlockLen = this.GetEncryptedLength(ContentLen);
217 byte b;
218
219 i = c;
220
221 do
222 {
223 b = (byte)(i & 127);
224 i >>= 7;
225 if (i != 0)
226 b |= 0x80;
227
228 Encrypted.WriteByte(b);
229 }
230 while (i != 0);
231
232 await Data.CopyToAsync(Encrypted);
233
234 if (ContentLen < BlockLen)
235 {
236 c = BlockLen - ContentLen;
237 byte[] Bin = new byte[c];
238
239 lock (rnd)
240 {
241 rnd.GetBytes(Bin);
242 }
243
244 await Encrypted.WriteAsync(Bin, 0, (int)c);
245 }
246 }
247
256 public virtual async Task<Stream> Decrypt(Stream Data, byte[] Key, byte[] IV, byte[] AssociatedData)
257 {
258 int c = 0;
259 int i;
260 int Offset = 0;
261
262 do
263 {
264 i = Data.ReadByte();
265 if (i < 0)
266 return null;
267
268 c |= (i & 127) << Offset;
269 Offset += 7;
270 }
271 while (i >= 0x80);
272
273 if (c < 0 || c + Data.Position > Data.Length)
274 return null;
275
276 TemporaryStream Decrypted = new TemporaryStream();
277 try
278 {
279 await Crypto.CopyAsync(Data, Decrypted, c);
280 return Decrypted;
281 }
282 catch (Exception)
283 {
284 Decrypted.Dispose();
285 return null;
286 }
287 }
288
293 public abstract byte[] GenerateKey();
294
308 public virtual byte[] Encrypt(string Id, string Type, string From, string To, uint Counter, byte[] Data, IE2eEndpoint Sender, IE2eEndpoint Receiver)
309 {
310 byte[] Encrypted;
311 byte[] EncryptedKey;
312 byte[] Key;
313 byte[] IV = this.GetIV(Id, Type, From, To, Counter);
314 byte[] AssociatedData = this.AuthenticatedEncryption ? Encoding.UTF8.GetBytes(From) : null;
315 byte[] Signature;
316 byte[] Block;
317 int i, j, k, l;
318 int c = 8;
319
320 if (Sender.SupportsSharedSecrets)
321 {
322 Key = Sender.GetSharedSecret(Receiver);
323 EncryptedKey = null;
324 l = 0;
325 }
326 else
327 {
328 Key = this.GenerateKey();
329 EncryptedKey = Receiver.EncryptSecret(Key);
330 l = EncryptedKey.Length;
331 c += l + 2;
332 }
333
334 Encrypted = this.Encrypt(Data, Key, IV, AssociatedData, E2eBufferFillAlgorithm.Random);
335 i = Encrypted.Length;
336 j = 0;
337 c += i;
338
339 if (Sender.SupportsSignatures)
340 {
341 Signature = Sender.Sign(Data);
342 k = Signature.Length;
343 c += k + 1;
344 if (k >= 128)
345 c++;
346 }
347 else
348 {
349 k = 0;
350 Signature = null;
351 }
352
353 Block = new byte[c];
354
355 if (k > 0)
356 {
357 if (k < 128)
358 Block[j++] = (byte)k;
359 else
360 {
361 Block[j++] = (byte)(k | 128);
362 Block[j++] = (byte)(k >> 7);
363 }
364 }
365
366 if (l > 0)
367 {
368 Block[j++] = (byte)l;
369 Block[j++] = (byte)(l >> 8);
370 }
371
372 Block[j++] = (byte)i;
373 Block[j++] = (byte)(i >> 8);
374 Block[j++] = (byte)(i >> 16);
375 Block[j++] = (byte)(i >> 24);
376
377 Block[j++] = (byte)Counter;
378 Block[j++] = (byte)(Counter >> 8);
379 Block[j++] = (byte)(Counter >> 16);
380 Block[j++] = (byte)(Counter >> 24);
381
382 if (k > 0)
383 {
384 Array.Copy(Signature, 0, Block, j, k);
385 j += k;
386 }
387
388 if (l > 0)
389 {
390 Array.Copy(EncryptedKey, 0, Block, j, l);
391 j += l;
392 }
393
394 Array.Copy(Encrypted, 0, Block, j, i);
395
396 return Block;
397 }
398
410 public virtual byte[] Decrypt(string Id, string Type, string From, string To, byte[] Data, IE2eEndpoint Sender, IE2eEndpoint Receiver)
411 {
412 if (Data.Length < 8)
413 return null;
414
415 int SignatureLen;
416 int KeyLen;
417 int DataLen;
418 uint Counter;
419 int i = 0;
420
421 if (Receiver.SupportsSignatures)
422 {
423 SignatureLen = Data[i++];
424 if ((SignatureLen & 128) != 0)
425 {
426 SignatureLen &= 127;
427 SignatureLen |= Data[i++] << 7;
428 }
429 }
430 else
431 SignatureLen = 0;
432
433 if (Receiver.SupportsSharedSecrets)
434 KeyLen = 0;
435 else
436 {
437 KeyLen = Data[i++];
438 KeyLen |= Data[i++] << 8;
439 }
440
441 if (i + 4 > Data.Length)
442 return null;
443
444 DataLen = Data[i++];
445 DataLen |= Data[i++] << 8;
446 DataLen |= Data[i++] << 16;
447 DataLen |= Data[i++] << 24;
448
449 Counter = Data[i++];
450 Counter |= (uint)(Data[i++] << 8);
451 Counter |= (uint)(Data[i++] << 16);
452 Counter |= (uint)(Data[i++] << 24);
453
454 if (Data.Length != i + SignatureLen + KeyLen + DataLen)
455 return null;
456
457 byte[] Signature = new byte[SignatureLen];
458 byte[] EncryptedKey = KeyLen > 0 ? new byte[KeyLen] : null;
459 byte[] Encrypted = new byte[DataLen];
460 byte[] Key;
461
462 Array.Copy(Data, i, Signature, 0, SignatureLen);
463 i += SignatureLen;
464
465 if (KeyLen > 0)
466 {
467 Array.Copy(Data, i, EncryptedKey, 0, KeyLen);
468 i += KeyLen;
469 }
470
471 Array.Copy(Data, i, Encrypted, 0, DataLen);
472
473 if (EncryptedKey is null)
474 Key = Receiver.GetSharedSecret(Sender);
475 else
476 Key = Receiver.DecryptSecret(EncryptedKey);
477
478 byte[] Decrypted;
479 byte[] IV = this.GetIV(Id, Type, From, To, Counter);
480 byte[] AssociatedData = this.AuthenticatedEncryption ? Encoding.UTF8.GetBytes(From) : null;
481
482 try
483 {
484 Decrypted = this.Decrypt(Encrypted, Key, IV, AssociatedData);
485
486 if (!(Decrypted is null) &&
487 ((Sender.SupportsSignatures && Sender.Verify(Decrypted, Signature)) ||
488 (!Sender.SupportsSignatures && SignatureLen == 0)))
489 {
490 return Decrypted;
491 }
492 }
493 catch (Exception)
494 {
495 // Invalid key
496 }
497
498 if (!(Receiver.Previous is null))
499 {
500 try
501 {
502 if (EncryptedKey is null)
503 Key = Receiver.Previous.GetSharedSecret(Sender);
504 else
505 Key = Receiver.Previous.DecryptSecret(EncryptedKey);
506
507 if (!(Key is null))
508 {
509 Decrypted = this.Decrypt(Encrypted, Key, IV, AssociatedData);
510
511 if (!(Decrypted is null) &&
512 ((Sender.SupportsSignatures && Sender.Verify(Decrypted, Signature)) ||
513 (!Sender.SupportsSignatures && SignatureLen == 0)))
514 {
515 return Decrypted;
516 }
517 }
518 }
519 catch (Exception)
520 {
521 // Invalid key
522 }
523 }
524
525 return null;
526 }
527
541 public virtual async Task Encrypt(string Id, string Type, string From, string To, uint Counter, Stream Data, Stream Encrypted, IE2eEndpoint Sender, IE2eEndpoint Receiver)
542 {
543 using (TemporaryStream TempEncrypted = new TemporaryStream())
544 {
545 byte[] EncryptedKey;
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 if (Sender.SupportsSharedSecrets)
554 {
555 Key = Sender.GetSharedSecret(Receiver);
556 EncryptedKey = null;
557 l = 0;
558 }
559 else
560 {
561 Key = this.GenerateKey();
562 EncryptedKey = Receiver.EncryptSecret(Key);
563 l = EncryptedKey.Length;
564 }
565
566 await this.Encrypt(Data, TempEncrypted, Key, IV, AssociatedData);
567 i = TempEncrypted.Length;
568
569 if (i > uint.MaxValue)
570 throw new NotSupportedException("Too large.");
571
572 if (Sender.SupportsSignatures)
573 {
574 Data.Position = 0;
575 Signature = Sender.Sign(Data);
576 k = Signature.Length;
577 }
578 else
579 {
580 k = 0;
581 Signature = null;
582 }
583
584 if (k > 0)
585 {
586 if (k < 128)
587 Encrypted.WriteByte((byte)k);
588 else
589 {
590 Encrypted.WriteByte((byte)(k | 128));
591 Encrypted.WriteByte((byte)(k >> 7));
592 }
593 }
594
595 if (l > 0)
596 {
597 Encrypted.WriteByte((byte)l);
598 Encrypted.WriteByte((byte)(l >> 8));
599 }
600
601 Encrypted.WriteByte((byte)i);
602 Encrypted.WriteByte((byte)(i >> 8));
603 Encrypted.WriteByte((byte)(i >> 16));
604 Encrypted.WriteByte((byte)(i >> 24));
605
606 Encrypted.WriteByte((byte)Counter);
607 Encrypted.WriteByte((byte)(Counter >> 8));
608 Encrypted.WriteByte((byte)(Counter >> 16));
609 Encrypted.WriteByte((byte)(Counter >> 24));
610
611 if (k > 0)
612 await Encrypted.WriteAsync(Signature, 0, k);
613
614 if (l > 0)
615 await Encrypted.WriteAsync(EncryptedKey, 0, l);
616
617 TempEncrypted.Position = 0;
618 await TempEncrypted.CopyToAsync(Encrypted);
619 }
620 }
621
633 public virtual async Task<Stream> Decrypt(string Id, string Type, string From, string To, Stream Data, IE2eEndpoint Sender, IE2eEndpoint Receiver)
634 {
635 if (Data.Length < 8)
636 return null;
637
638 int SignatureLen;
639 int KeyLen;
640 int DataLen;
641 uint Counter;
642
643 if (Receiver.SupportsSignatures)
644 {
645 SignatureLen = Data.ReadByte();
646 if ((SignatureLen & 128) != 0)
647 {
648 SignatureLen &= 127;
649 SignatureLen |= Data.ReadByte() << 7;
650 }
651 }
652 else
653 SignatureLen = 0;
654
655 if (Receiver.SupportsSharedSecrets)
656 KeyLen = 0;
657 else
658 {
659 KeyLen = Data.ReadByte();
660 KeyLen |= Data.ReadByte() << 8;
661 }
662
663 if (Data.Position + 4 > Data.Length)
664 return null;
665
666 DataLen = Data.ReadByte();
667 DataLen |= Data.ReadByte() << 8;
668 DataLen |= Data.ReadByte() << 16;
669 DataLen |= Data.ReadByte() << 24;
670
671 Counter = (byte)Data.ReadByte();
672 Counter |= (uint)(Data.ReadByte() << 8);
673 Counter |= (uint)(Data.ReadByte() << 16);
674 Counter |= (uint)(Data.ReadByte() << 24);
675
676 if (Data.Length != Data.Position + SignatureLen + KeyLen + DataLen)
677 return null;
678
679 byte[] Signature = new byte[SignatureLen];
680 byte[] EncryptedKey = KeyLen > 0 ? new byte[KeyLen] : null;
681 byte[] Key;
682
683 if (await Data.ReadAsync(Signature, 0, SignatureLen) != SignatureLen)
684 return null;
685
686 if (KeyLen > 0)
687 {
688 if (await Data.ReadAsync(EncryptedKey, 0, KeyLen) != KeyLen)
689 return null;
690 }
691
692 using (TemporaryStream Encrypted = new TemporaryStream())
693 {
694 await Crypto.CopyAsync(Data, Encrypted, DataLen);
695
696 if (EncryptedKey is null)
697 Key = Receiver.GetSharedSecret(Sender);
698 else
699 Key = Receiver.DecryptSecret(EncryptedKey);
700
701 byte[] IV = this.GetIV(Id, Type, From, To, Counter);
702 byte[] AssociatedData = this.AuthenticatedEncryption ? Encoding.UTF8.GetBytes(From) : null;
703 Stream Decrypted = null;
704
705 try
706 {
707 Encrypted.Position = 0;
708 Decrypted = await this.Decrypt(Encrypted, Key, IV, AssociatedData);
709
710 if (!(Decrypted is null))
711 {
712 Decrypted.Position = 0;
713
714 if ((Sender.SupportsSignatures && Sender.Verify(Decrypted, Signature)) ||
715 (!Sender.SupportsSignatures && SignatureLen == 0))
716 {
717 return Decrypted;
718 }
719
720 Decrypted.Dispose();
721 Decrypted = null;
722 }
723 }
724 catch (Exception)
725 {
726 // Invalid key
727
728 Decrypted?.Dispose();
729 Decrypted = null;
730 }
731
732 if (!(Receiver.Previous is null))
733 {
734 try
735 {
736 if (EncryptedKey is null)
737 Key = Receiver.Previous.GetSharedSecret(Sender);
738 else
739 Key = Receiver.Previous.DecryptSecret(EncryptedKey);
740
741 if (!(Key is null))
742 {
743 Encrypted.Position = 0;
744 Decrypted = await this.Decrypt(Encrypted, Key, IV, AssociatedData);
745
746 if (!(Decrypted is null))
747 {
748 Decrypted.Position = 0;
749
750 if ((Sender.SupportsSignatures && Sender.Verify(Decrypted, Signature)) ||
751 (!Sender.SupportsSignatures && SignatureLen == 0))
752 {
753 return Decrypted;
754 }
755
756 Decrypted.Dispose();
757 Decrypted = null;
758 }
759 }
760 }
761 catch (Exception)
762 {
763 // Invalid key
764
765 Decrypted?.Dispose();
766 Decrypted = null;
767 }
768 }
769
770 return null;
771 }
772 }
773
774
789 public virtual bool Encrypt(string Id, string Type, string From, string To, uint Counter, byte[] Data,
790 StringBuilder Xml, IE2eEndpoint Sender, IE2eEndpoint Receiver)
791 {
792 byte[] Encrypted;
793 byte[] Key;
794 byte[] IV = this.GetIV(Id, Type, From, To, Counter);
795 byte[] AssociatedData = this.AuthenticatedEncryption ? Encoding.UTF8.GetBytes(From) : null;
796
797 Xml.Append('<');
798 Xml.Append(this.LocalName);
799 Xml.Append(" xmlns=\"");
800 Xml.Append(this.Namespace);
801 Xml.Append("\" r=\"");
803 {
804 Xml.Append(Sender.Namespace);
805 Xml.Append('#');
806 }
807 Xml.Append(Sender.LocalName);
808 Xml.Append("\" c=\"");
809 Xml.Append(Counter.ToString());
810
811 if (Sender.SupportsSharedSecrets)
812 Key = Sender.GetSharedSecret(Receiver);
813 else
814 {
815 Key = this.GenerateKey();
816
817 byte[] EncryptedKey = Receiver.EncryptSecret(Key);
818
819 Xml.Append("\" k=\"");
820 Xml.Append(Convert.ToBase64String(EncryptedKey));
821 }
822
823 Encrypted = this.Encrypt(Data, Key, IV, AssociatedData, E2eBufferFillAlgorithm.Random);
824
825 if (Sender.SupportsSignatures)
826 {
827 byte[] Signature = Sender.Sign(Data);
828
829 Xml.Append("\" s=\"");
830 Xml.Append(Convert.ToBase64String(Signature));
831 }
832
833 Xml.Append("\">");
834 Xml.Append(Convert.ToBase64String(Encrypted));
835 Xml.Append("</");
836 Xml.Append(this.LocalName);
837 Xml.Append('>');
838
839 return true;
840 }
841
853 public virtual string Decrypt(string Id, string Type, string From, string To, XmlElement Xml,
854 IE2eEndpoint Sender, IE2eEndpoint Receiver)
855 {
856 byte[] EncryptedKey = null;
857 byte[] Signature = null;
858 uint? Counter = null;
859
860 foreach (XmlAttribute Attr in Xml.Attributes)
861 {
862 switch (Attr.Name)
863 {
864 case "c":
865 if (!uint.TryParse(Attr.Value, out uint i))
866 return null;
867
868 Counter = i;
869 break;
870
871 case "s":
872 Signature = Convert.FromBase64String(Attr.Value);
873 break;
874
875 case "k":
876 EncryptedKey = Convert.FromBase64String(Attr.Value);
877 break;
878 }
879 }
880
881 if (!Counter.HasValue)
882 return null;
883
884 byte[] Encrypted = Convert.FromBase64String(Xml.InnerText);
885 byte[] Decrypted;
886 byte[] Key;
887
888 if (EncryptedKey is null)
889 Key = Receiver.GetSharedSecret(Sender);
890 else
891 Key = Receiver.DecryptSecret(EncryptedKey);
892
893 byte[] IV = this.GetIV(Id, Type, From, To, Counter.Value);
894 byte[] AssociatedData = this.AuthenticatedEncryption ? Encoding.UTF8.GetBytes(From) : null;
895
896 try
897 {
898 Decrypted = this.Decrypt(Encrypted, Key, IV, AssociatedData);
899
900 if (!(Decrypted is null) &&
901 ((Sender.SupportsSignatures && Sender.Verify(Decrypted, Signature)) ||
902 (!Sender.SupportsSignatures && Signature is null)))
903 {
904 return Encoding.UTF8.GetString(Decrypted);
905 }
906 }
907 catch (Exception)
908 {
909 // Invalid key
910 }
911
912 if (!(Receiver.Previous is null))
913 {
914 try
915 {
916 if (EncryptedKey is null)
917 Key = Receiver.Previous.GetSharedSecret(Sender);
918 else
919 Key = Receiver.Previous.DecryptSecret(EncryptedKey);
920
921 if (!(Key is null))
922 {
923 Decrypted = this.Decrypt(Encrypted, Key, IV, AssociatedData);
924
925 if (!(Decrypted is null) &&
926 ((Sender.SupportsSignatures && Sender.Verify(Decrypted, Signature)) ||
927 (!Sender.SupportsSignatures && Signature is null)))
928 {
929 return Encoding.UTF8.GetString(Decrypted);
930 }
931 }
932 }
933 catch (Exception)
934 {
935 // Invalid key
936 }
937 }
938
939 return null;
940 }
941 }
942}
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:15
Implements support for the ChaCha20 cipher in hybrid End-to-End encryption schemes.
Definition: ChaCha20.cs:15
abstract byte[] GetIV(string Id, string Type, string From, string To, uint Counter)
Gets an Initiation Vector from stanza attributes.
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 async Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
Asynchronously reads the bytes from the current stream and writes them to another stream,...
override void Dispose(bool disposing)
Releases the unmanaged resources used by the System.IO.Stream and optionally releases the managed res...
override long Length
When overridden in a derived class, gets the length in bytes of the stream.
Helper methods for encrypting and decrypting streams of data.
Definition: Crypto.cs:12
static async Task< bool > CopyAsync(Stream From, Stream To, long DataLen)
Copies DataLen number of bytes from From to To .
Definition: Crypto.cs:71
Abstract base class for End-to-End encryption schemes.
Definition: IE2eEndpoint.cs:12
bool SupportsSignatures
If signatures are supported.
byte[] Sign(byte[] Data)
Signs binary data using the local private key.
byte[] GetSharedSecret(IE2eEndpoint RemoteEndpoint)
Gets a shared secret
bool SupportsSharedSecrets
If shared secrets can be calculated from the endpoints keys.
bool Verify(byte[] Data, byte[] Signature)
Verifies a signature.
string Namespace
Namespace of the E2E endpoint
Definition: IE2eEndpoint.cs:33
byte[] EncryptSecret(byte[] Secret)
Encrypts a secret. Used if shared secrets cannot be calculated.
string LocalName
Local name of the E2E endpoint
Definition: IE2eEndpoint.cs:25
byte[] DecryptSecret(byte[] Secret)
Decrypts a secret. Used if shared secrets cannot be calculated.
IE2eEndpoint Previous
Previous keys.
Definition: IE2eEndpoint.cs:41
Interface for symmetric ciphers.
SymmetricCipherAlgorithms
Enumeration of symmetric cipher algorithms available in the library.
E2eBufferFillAlgorithm
How buffers are filler before E2E Encryption is performed.