Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ChaCha20Poly1305.cs
Go to the documentation of this file.
5
7
8[UnsupportedOSPlatform("browser")]
9[UnsupportedOSPlatform("ios")]
10[UnsupportedOSPlatform("tvos")]
11public sealed class ChaCha20Poly1305 : IDisposable
12{
14
16
23
24 public ChaCha20Poly1305(byte[] key)
25 {
27 if (key == null)
28 {
29 throw new ArgumentNullException("key");
30 }
31 CheckKeySize(key.Length);
33 }
34
35 private static void CheckKeySize(int keySizeInBytes)
36 {
37 if (keySizeInBytes != 32)
38 {
40 }
41 }
42
43 public void Encrypt(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag, byte[]? associatedData = null)
44 {
45 AeadCommon.CheckArgumentsForNull(nonce, plaintext, ciphertext, tag);
46 Encrypt((ReadOnlySpan<byte>)nonce, (ReadOnlySpan<byte>)plaintext, (Span<byte>)ciphertext, (Span<byte>)tag, (ReadOnlySpan<byte>)associatedData);
47 }
48
49 public void Encrypt(ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> plaintext, Span<byte> ciphertext, Span<byte> tag, ReadOnlySpan<byte> associatedData = default(ReadOnlySpan<byte>))
50 {
51 CheckParameters(plaintext, ciphertext, nonce, tag);
52 EncryptCore(nonce, plaintext, ciphertext, tag, associatedData);
53 }
54
55 public void Decrypt(byte[] nonce, byte[] ciphertext, byte[] tag, byte[] plaintext, byte[]? associatedData = null)
56 {
57 AeadCommon.CheckArgumentsForNull(nonce, plaintext, ciphertext, tag);
58 Decrypt((ReadOnlySpan<byte>)nonce, (ReadOnlySpan<byte>)ciphertext, (ReadOnlySpan<byte>)tag, (Span<byte>)plaintext, (ReadOnlySpan<byte>)associatedData);
59 }
60
61 public void Decrypt(ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> ciphertext, ReadOnlySpan<byte> tag, Span<byte> plaintext, ReadOnlySpan<byte> associatedData = default(ReadOnlySpan<byte>))
62 {
63 CheckParameters(plaintext, ciphertext, nonce, tag);
64 DecryptCore(nonce, ciphertext, tag, plaintext, associatedData);
65 }
66
67 private static void CheckParameters(ReadOnlySpan<byte> plaintext, ReadOnlySpan<byte> ciphertext, ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> tag)
68 {
69 if (plaintext.Length != ciphertext.Length)
70 {
72 }
73 if (nonce.Length != 12)
74 {
76 }
77 if (tag.Length != 16)
78 {
80 }
81 }
82
83 private static void ThrowIfNotSupported()
84 {
85 if (!IsSupported)
86 {
88 }
89 }
90
91 [MemberNotNull("_keyHandle")]
93 {
94 _keyHandle = global::Interop.BCrypt.BCryptImportKey(BCryptAeadHandleCache.ChaCha20Poly1305, key);
95 }
96
97 private void EncryptCore(ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> plaintext, Span<byte> ciphertext, Span<byte> tag, ReadOnlySpan<byte> associatedData = default(ReadOnlySpan<byte>))
98 {
99 AeadCommon.Encrypt(_keyHandle, nonce, associatedData, plaintext, ciphertext, tag);
100 }
101
102 private void DecryptCore(ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> ciphertext, ReadOnlySpan<byte> tag, Span<byte> plaintext, ReadOnlySpan<byte> associatedData = default(ReadOnlySpan<byte>))
103 {
104 AeadCommon.Decrypt(_keyHandle, nonce, associatedData, ciphertext, tag, plaintext, clearPlaintextOnFailure: true);
105 }
106
107 public void Dispose()
108 {
110 }
111}
static string Cryptography_AlgorithmNotSupported
Definition SR.cs:38
static string Cryptography_InvalidNonceLength
Definition SR.cs:98
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Cryptography_PlaintextCiphertextLengthMismatch
Definition SR.cs:128
static string Cryptography_InvalidKeySize
Definition SR.cs:92
static string Cryptography_InvalidTagLength
Definition SR.cs:100
Definition SR.cs:7
static void CheckArgumentsForNull(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag)
Definition AeadCommon.cs:9
static unsafe void Decrypt(SafeKeyHandle keyHandle, ReadOnlySpan< byte > nonce, ReadOnlySpan< byte > associatedData, ReadOnlySpan< byte > ciphertext, ReadOnlySpan< byte > tag, Span< byte > plaintext, bool clearPlaintextOnFailure)
Definition AeadCommon.cs:61
static unsafe void Encrypt(SafeKeyHandle keyHandle, ReadOnlySpan< byte > nonce, ReadOnlySpan< byte > associatedData, ReadOnlySpan< byte > plaintext, Span< byte > ciphertext, Span< byte > tag)
Definition AeadCommon.cs:29
void EncryptCore(ReadOnlySpan< byte > nonce, ReadOnlySpan< byte > plaintext, Span< byte > ciphertext, Span< byte > tag, ReadOnlySpan< byte > associatedData=default(ReadOnlySpan< byte >))
void DecryptCore(ReadOnlySpan< byte > nonce, ReadOnlySpan< byte > ciphertext, ReadOnlySpan< byte > tag, Span< byte > plaintext, ReadOnlySpan< byte > associatedData=default(ReadOnlySpan< byte >))
static void CheckKeySize(int keySizeInBytes)
void Encrypt(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag, byte[]? associatedData=null)
static void CheckParameters(ReadOnlySpan< byte > plaintext, ReadOnlySpan< byte > ciphertext, ReadOnlySpan< byte > nonce, ReadOnlySpan< byte > tag)
void Decrypt(byte[] nonce, byte[] ciphertext, byte[] tag, byte[] plaintext, byte[]? associatedData=null)
void Encrypt(ReadOnlySpan< byte > nonce, ReadOnlySpan< byte > plaintext, Span< byte > ciphertext, Span< byte > tag, ReadOnlySpan< byte > associatedData=default(ReadOnlySpan< byte >))
void Decrypt(ReadOnlySpan< byte > nonce, ReadOnlySpan< byte > ciphertext, ReadOnlySpan< byte > tag, Span< byte > plaintext, ReadOnlySpan< byte > associatedData=default(ReadOnlySpan< byte >))
override void Dispose(bool disposing)