Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
AesCcm.cs
Go to the documentation of this file.
5
7
8[UnsupportedOSPlatform("browser")]
9[UnsupportedOSPlatform("ios")]
10[UnsupportedOSPlatform("tvos")]
11public sealed class AesCcm : IDisposable
12{
14
15 public static KeySizes NonceByteSizes { get; } = new KeySizes(7, 13, 1);
16
17
18 public static KeySizes TagByteSizes { get; } = new KeySizes(4, 16, 2);
19
20
21 public static bool IsSupported => true;
22
29
30 public AesCcm(byte[] key)
31 {
33 if (key == null)
34 {
35 throw new ArgumentNullException("key");
36 }
37 AesAEAD.CheckKeySize(key.Length);
39 }
40
41 public void Encrypt(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag, byte[]? associatedData = null)
42 {
43 AeadCommon.CheckArgumentsForNull(nonce, plaintext, ciphertext, tag);
44 Encrypt((ReadOnlySpan<byte>)nonce, (ReadOnlySpan<byte>)plaintext, (Span<byte>)ciphertext, (Span<byte>)tag, (ReadOnlySpan<byte>)associatedData);
45 }
46
47 public void Encrypt(ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> plaintext, Span<byte> ciphertext, Span<byte> tag, ReadOnlySpan<byte> associatedData = default(ReadOnlySpan<byte>))
48 {
49 CheckParameters(plaintext, ciphertext, nonce, tag);
50 EncryptCore(nonce, plaintext, ciphertext, tag, associatedData);
51 }
52
53 public void Decrypt(byte[] nonce, byte[] ciphertext, byte[] tag, byte[] plaintext, byte[]? associatedData = null)
54 {
55 AeadCommon.CheckArgumentsForNull(nonce, plaintext, ciphertext, tag);
56 Decrypt((ReadOnlySpan<byte>)nonce, (ReadOnlySpan<byte>)ciphertext, (ReadOnlySpan<byte>)tag, (Span<byte>)plaintext, (ReadOnlySpan<byte>)associatedData);
57 }
58
59 public void Decrypt(ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> ciphertext, ReadOnlySpan<byte> tag, Span<byte> plaintext, ReadOnlySpan<byte> associatedData = default(ReadOnlySpan<byte>))
60 {
61 CheckParameters(plaintext, ciphertext, nonce, tag);
62 DecryptCore(nonce, ciphertext, tag, plaintext, associatedData);
63 }
64
65 private static void CheckParameters(ReadOnlySpan<byte> plaintext, ReadOnlySpan<byte> ciphertext, ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> tag)
66 {
67 if (plaintext.Length != ciphertext.Length)
68 {
70 }
71 if (!nonce.Length.IsLegalSize(NonceByteSizes))
72 {
74 }
75 if (!tag.Length.IsLegalSize(TagByteSizes))
76 {
78 }
79 }
80
81 private static void ThrowIfNotSupported()
82 {
83 if (IsSupported)
84 {
85 }
86 }
87
88 [MemberNotNull("_keyHandle")]
90 {
91 _keyHandle = global::Interop.BCrypt.BCryptImportKey(BCryptAeadHandleCache.AesCcm, key);
92 }
93
94 private void EncryptCore(ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> plaintext, Span<byte> ciphertext, Span<byte> tag, ReadOnlySpan<byte> associatedData = default(ReadOnlySpan<byte>))
95 {
96 AeadCommon.Encrypt(_keyHandle, nonce, associatedData, plaintext, ciphertext, tag);
97 }
98
99 private void DecryptCore(ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> ciphertext, ReadOnlySpan<byte> tag, Span<byte> plaintext, ReadOnlySpan<byte> associatedData = default(ReadOnlySpan<byte>))
100 {
101 AeadCommon.Decrypt(_keyHandle, nonce, associatedData, ciphertext, tag, plaintext, clearPlaintextOnFailure: false);
102 }
103
104 public void Dispose()
105 {
107 }
108}
static string Cryptography_InvalidNonceLength
Definition SR.cs:98
static string Cryptography_PlaintextCiphertextLengthMismatch
Definition SR.cs:128
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
static void CheckKeySize(int keySizeInBytes)
Definition AesAEAD.cs:5
void Decrypt(ReadOnlySpan< byte > nonce, ReadOnlySpan< byte > ciphertext, ReadOnlySpan< byte > tag, Span< byte > plaintext, ReadOnlySpan< byte > associatedData=default(ReadOnlySpan< byte >))
Definition AesCcm.cs:59
void Encrypt(ReadOnlySpan< byte > nonce, ReadOnlySpan< byte > plaintext, Span< byte > ciphertext, Span< byte > tag, ReadOnlySpan< byte > associatedData=default(ReadOnlySpan< byte >))
Definition AesCcm.cs:47
void Decrypt(byte[] nonce, byte[] ciphertext, byte[] tag, byte[] plaintext, byte[]? associatedData=null)
Definition AesCcm.cs:53
static void CheckParameters(ReadOnlySpan< byte > plaintext, ReadOnlySpan< byte > ciphertext, ReadOnlySpan< byte > nonce, ReadOnlySpan< byte > tag)
Definition AesCcm.cs:65
void Encrypt(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag, byte[]? associatedData=null)
Definition AesCcm.cs:41
void ImportKey(ReadOnlySpan< byte > key)
Definition AesCcm.cs:89
void EncryptCore(ReadOnlySpan< byte > nonce, ReadOnlySpan< byte > plaintext, Span< byte > ciphertext, Span< byte > tag, ReadOnlySpan< byte > associatedData=default(ReadOnlySpan< byte >))
Definition AesCcm.cs:94
void DecryptCore(ReadOnlySpan< byte > nonce, ReadOnlySpan< byte > ciphertext, ReadOnlySpan< byte > tag, Span< byte > plaintext, ReadOnlySpan< byte > associatedData=default(ReadOnlySpan< byte >))
Definition AesCcm.cs:99
AesCcm(ReadOnlySpan< byte > key)
Definition AesCcm.cs:23
override void Dispose(bool disposing)