-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
AesGcm.cs
181 lines (159 loc) · 7.82 KB
/
AesGcm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Runtime.Versioning;
using Internal.Cryptography;
namespace System.Security.Cryptography
{
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
public sealed partial class AesGcm : IDisposable
{
private const int NonceSize = 12;
public static KeySizes NonceByteSizes { get; } = new KeySizes(NonceSize, NonceSize, 1);
[Obsolete(Obsoletions.AesGcmTagConstructorMessage, DiagnosticId = Obsoletions.AesGcmTagConstructorDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public AesGcm(ReadOnlySpan<byte> key)
{
ThrowIfNotSupported();
AesAEAD.CheckKeySize(key.Length);
ImportKey(key);
}
[Obsolete(Obsoletions.AesGcmTagConstructorMessage, DiagnosticId = Obsoletions.AesGcmTagConstructorDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public AesGcm(byte[] key)
: this(new ReadOnlySpan<byte>(key ?? throw new ArgumentNullException(nameof(key))))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="AesGcm" /> class with a provided key and required tag size.
/// </summary>
/// <param name="key">The secret key to use for this instance.</param>
/// <param name="tagSizeInBytes">The size of the tag, in bytes, that encryption and decryption must use.</param>
/// <exception cref="CryptographicException">
/// The <paramref name="key" /> parameter length is other than 16, 24, or 32 bytes (128, 192, or 256 bits).
/// </exception>
/// <exception cref="ArgumentException">
/// The <paramref name="tagSizeInBytes" /> parameter is an unsupported tag size indicated by
/// <see cref="TagByteSizes" />.
/// </exception>
/// <exception cref="PlatformNotSupportedException">
/// The current platform does not support AES-GCM.
/// </exception>
/// <remarks>
/// The <paramref name="tagSizeInBytes" /> parameter is used to indicate that the tag parameter in <c>Encrypt</c>
/// or <c>Decrypt</c> must be exactly this size. Indicating the required tag size prevents issues where callers
/// of <c>Decrypt</c> may supply a tag as input and that input is truncated to an unexpected size.
/// </remarks>
public AesGcm(ReadOnlySpan<byte> key, int tagSizeInBytes)
{
ThrowIfNotSupported();
AesAEAD.CheckKeySize(key.Length);
if (!tagSizeInBytes.IsLegalSize(TagByteSizes))
{
throw new ArgumentException(SR.Cryptography_InvalidTagLength, nameof(tagSizeInBytes));
}
TagSizeInBytes = tagSizeInBytes;
ImportKey(key);
}
/// <summary>
/// Initializes a new instance of the <see cref="AesGcm" /> class with a provided key and required tag size.
/// </summary>
/// <param name="key">The secret key to use for this instance.</param>
/// <param name="tagSizeInBytes">The size of the tag, in bytes, that encryption and decryption must use.</param>
/// <exception cref="ArgumentNullException">The <paramref name="key" /> parameter is null.</exception>
/// <exception cref="CryptographicException">
/// The <paramref name="key" /> parameter length is other than 16, 24, or 32 bytes (128, 192, or 256 bits).
/// </exception>
/// <exception cref="ArgumentException">
/// The <paramref name="tagSizeInBytes" /> parameter is an unsupported tag size indicated by
/// <see cref="TagByteSizes" />.
/// </exception>
/// <exception cref="PlatformNotSupportedException">
/// The current platform does not support AES-GCM.
/// </exception>
/// <remarks>
/// The <paramref name="tagSizeInBytes" /> parameter is used to indicate that the tag parameter in <c>Encrypt</c>
/// or <c>Decrypt</c> must be exactly this size. Indicating the required tag size prevents issues where callers
/// of <c>Decrypt</c> may supply a tag as input and that input is truncated to an unexpected size.
/// </remarks>
public AesGcm(byte[] key, int tagSizeInBytes)
: this(new ReadOnlySpan<byte>(key ?? throw new ArgumentNullException(nameof(key))), tagSizeInBytes)
{
}
/// <summary>
/// Gets the size of the tag, in bytes.
/// </summary>
/// <value>
/// The size of the tag that must be used for encryption or decryption, or <see langword="null" /> if the
/// tag size is unspecified.
/// </value>
public int? TagSizeInBytes { get; }
public void Encrypt(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag, byte[]? associatedData = null)
{
ArgumentNullException.ThrowIfNull(nonce);
ArgumentNullException.ThrowIfNull(plaintext);
ArgumentNullException.ThrowIfNull(ciphertext);
ArgumentNullException.ThrowIfNull(tag);
Encrypt((ReadOnlySpan<byte>)nonce, plaintext, ciphertext, tag, associatedData);
}
public void Encrypt(
ReadOnlySpan<byte> nonce,
ReadOnlySpan<byte> plaintext,
Span<byte> ciphertext,
Span<byte> tag,
ReadOnlySpan<byte> associatedData = default)
{
CheckParameters(plaintext, ciphertext, nonce, tag);
EncryptCore(nonce, plaintext, ciphertext, tag, associatedData);
}
public void Decrypt(byte[] nonce, byte[] ciphertext, byte[] tag, byte[] plaintext, byte[]? associatedData = null)
{
ArgumentNullException.ThrowIfNull(nonce);
ArgumentNullException.ThrowIfNull(ciphertext);
ArgumentNullException.ThrowIfNull(tag);
ArgumentNullException.ThrowIfNull(plaintext);
Decrypt((ReadOnlySpan<byte>)nonce, ciphertext, tag, plaintext, associatedData);
}
public void Decrypt(
ReadOnlySpan<byte> nonce,
ReadOnlySpan<byte> ciphertext,
ReadOnlySpan<byte> tag,
Span<byte> plaintext,
ReadOnlySpan<byte> associatedData = default)
{
CheckParameters(plaintext, ciphertext, nonce, tag);
DecryptCore(nonce, ciphertext, tag, plaintext, associatedData);
}
private void CheckParameters(
ReadOnlySpan<byte> plaintext,
ReadOnlySpan<byte> ciphertext,
ReadOnlySpan<byte> nonce,
ReadOnlySpan<byte> tag)
{
if (plaintext.Length != ciphertext.Length)
throw new ArgumentException(SR.Cryptography_PlaintextCiphertextLengthMismatch);
if (!nonce.Length.IsLegalSize(NonceByteSizes))
throw new ArgumentException(SR.Cryptography_InvalidNonceLength, nameof(nonce));
if (TagSizeInBytes is int tagSizeInBytes)
{
// constructor promise
Debug.Assert(tagSizeInBytes.IsLegalSize(TagByteSizes));
if (tag.Length != tagSizeInBytes)
{
throw new ArgumentException(SR.Format(SR.Cryptography_IncorrectTagLength, tagSizeInBytes), nameof(tag));
}
}
else if (!tag.Length.IsLegalSize(TagByteSizes))
{
throw new ArgumentException(SR.Cryptography_InvalidTagLength, nameof(tag));
}
}
private static void ThrowIfNotSupported()
{
if (!IsSupported)
{
throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(AesGcm)));
}
}
}
}