Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
AeadCommon.cs
Go to the documentation of this file.
4
6
7internal static class AeadCommon
8{
9 public static void CheckArgumentsForNull(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag)
10 {
11 if (nonce == null)
12 {
13 throw new ArgumentNullException("nonce");
14 }
15 if (plaintext == null)
16 {
17 throw new ArgumentNullException("plaintext");
18 }
19 if (ciphertext == null)
20 {
21 throw new ArgumentNullException("ciphertext");
22 }
23 if (tag == null)
24 {
25 throw new ArgumentNullException("tag");
26 }
27 }
28
29 public unsafe static void Encrypt(SafeKeyHandle keyHandle, ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> associatedData, ReadOnlySpan<byte> plaintext, Span<byte> ciphertext, Span<byte> tag)
30 {
31 fixed (byte* pbInput = &GetNonNullPinnableReference(plaintext))
32 {
33 fixed (byte* pbNonce = &GetNonNullPinnableReference(nonce))
34 {
35 fixed (byte* pbOutput = &GetNonNullPinnableReference(ciphertext))
36 {
37 fixed (byte* pbTag = &GetNonNullPinnableReference(tag))
38 {
39 fixed (byte* pbAuthData = &GetNonNullPinnableReference(associatedData))
40 {
41 global::Interop.BCrypt.BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO bCRYPT_AUTHENTICATED_CIPHER_MODE_INFO = global::Interop.BCrypt.BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO.Create();
42 bCRYPT_AUTHENTICATED_CIPHER_MODE_INFO.pbNonce = pbNonce;
43 bCRYPT_AUTHENTICATED_CIPHER_MODE_INFO.cbNonce = nonce.Length;
44 bCRYPT_AUTHENTICATED_CIPHER_MODE_INFO.pbTag = pbTag;
45 bCRYPT_AUTHENTICATED_CIPHER_MODE_INFO.cbTag = tag.Length;
46 bCRYPT_AUTHENTICATED_CIPHER_MODE_INFO.pbAuthData = pbAuthData;
47 bCRYPT_AUTHENTICATED_CIPHER_MODE_INFO.cbAuthData = associatedData.Length;
48 int cbResult;
49 global::Interop.BCrypt.NTSTATUS nTSTATUS = global::Interop.BCrypt.BCryptEncrypt(keyHandle, pbInput, plaintext.Length, new IntPtr(&bCRYPT_AUTHENTICATED_CIPHER_MODE_INFO), null, 0, pbOutput, ciphertext.Length, out cbResult, 0);
50 if (nTSTATUS != 0)
51 {
52 throw global::Interop.BCrypt.CreateCryptographicException(nTSTATUS);
53 }
54 }
55 }
56 }
57 }
58 }
59 }
60
61 public unsafe static void Decrypt(SafeKeyHandle keyHandle, ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> associatedData, ReadOnlySpan<byte> ciphertext, ReadOnlySpan<byte> tag, Span<byte> plaintext, bool clearPlaintextOnFailure)
62 {
63 fixed (byte* pbOutput = &GetNonNullPinnableReference(plaintext))
64 {
65 fixed (byte* pbNonce = &GetNonNullPinnableReference(nonce))
66 {
67 fixed (byte* pbInput = &GetNonNullPinnableReference(ciphertext))
68 {
69 fixed (byte* pbTag = &GetNonNullPinnableReference(tag))
70 {
71 fixed (byte* pbAuthData = &GetNonNullPinnableReference(associatedData))
72 {
73 global::Interop.BCrypt.BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO bCRYPT_AUTHENTICATED_CIPHER_MODE_INFO = global::Interop.BCrypt.BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO.Create();
74 bCRYPT_AUTHENTICATED_CIPHER_MODE_INFO.pbNonce = pbNonce;
75 bCRYPT_AUTHENTICATED_CIPHER_MODE_INFO.cbNonce = nonce.Length;
76 bCRYPT_AUTHENTICATED_CIPHER_MODE_INFO.pbTag = pbTag;
77 bCRYPT_AUTHENTICATED_CIPHER_MODE_INFO.cbTag = tag.Length;
78 bCRYPT_AUTHENTICATED_CIPHER_MODE_INFO.pbAuthData = pbAuthData;
79 bCRYPT_AUTHENTICATED_CIPHER_MODE_INFO.cbAuthData = associatedData.Length;
80 int cbResult;
81 global::Interop.BCrypt.NTSTATUS nTSTATUS = global::Interop.BCrypt.BCryptDecrypt(keyHandle, pbInput, ciphertext.Length, new IntPtr(&bCRYPT_AUTHENTICATED_CIPHER_MODE_INFO), null, 0, pbOutput, plaintext.Length, out cbResult, 0);
82 switch (nTSTATUS)
83 {
84 case global::Interop.BCrypt.NTSTATUS.STATUS_SUCCESS:
85 break;
86 case global::Interop.BCrypt.NTSTATUS.STATUS_AUTH_TAG_MISMATCH:
87 if (clearPlaintextOnFailure)
88 {
90 }
92 default:
93 throw global::Interop.BCrypt.CreateCryptographicException(nTSTATUS);
94 }
95 }
96 }
97 }
98 }
99 }
100 }
101
102 [MethodImpl(MethodImplOptions.AggressiveInlining)]
103 private unsafe static ref readonly byte GetNonNullPinnableReference(ReadOnlySpan<byte> buffer)
104 {
105 if (buffer.Length == 0)
106 {
107 return ref Unsafe.AsRef<byte>((void*)1);
108 }
109 return ref MemoryMarshal.GetReference(buffer);
110 }
111
112 [MethodImpl(MethodImplOptions.AggressiveInlining)]
113 private unsafe static ref byte GetNonNullPinnableReference(Span<byte> buffer)
114 {
115 if (buffer.Length == 0)
116 {
117 return ref Unsafe.AsRef<byte>((void*)1);
118 }
119 return ref MemoryMarshal.GetReference(buffer);
120 }
121}
static string Cryptography_AuthTagMismatch
Definition SR.cs:44
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 ref byte GetNonNullPinnableReference(Span< byte > buffer)
static unsafe ref readonly byte GetNonNullPinnableReference(ReadOnlySpan< byte > buffer)
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
int Length
Definition Span.cs:70