-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
EncryptParameters.cs
215 lines (191 loc) · 14.4 KB
/
EncryptParameters.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Security.Cryptography;
using System.Text.Json;
using Azure.Core;
namespace Azure.Security.KeyVault.Keys.Cryptography
{
/// <summary>
/// Parameters for encrypting plaintext.
/// </summary>
public class EncryptParameters : IJsonSerializable
{
/// <summary>
/// Creates an instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.Rsa15"/> encryption algorithm.
/// </summary>
/// <param name="plaintext">The plaintext to encrypt.</param>
/// <returns>An instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.Rsa15"/> encryption algorithm.</returns>
/// <exception cref="ArgumentNullException"><paramref name="plaintext"/> is null.</exception>
public static EncryptParameters Rsa15Parameters(byte[] plaintext) =>
new EncryptParameters(EncryptionAlgorithm.Rsa15, plaintext, null, null);
/// <summary>
/// Creates an instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.RsaOaep"/> encryption algorithm.
/// </summary>
/// <param name="plaintext">The plaintext to encrypt.</param>
/// <returns>An instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.RsaOaep"/> encryption algorithm.</returns>
/// <exception cref="ArgumentNullException"><paramref name="plaintext"/> is null.</exception>
public static EncryptParameters RsaOaepParameters(byte[] plaintext) =>
new EncryptParameters(EncryptionAlgorithm.RsaOaep, plaintext, null, null);
/// <summary>
/// Creates an instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.RsaOaep256"/> encryption algorithm.
/// </summary>
/// <param name="plaintext">The plaintext to encrypt.</param>
/// <returns>An instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.RsaOaep256"/> encryption algorithm.</returns>
/// <exception cref="ArgumentNullException"><paramref name="plaintext"/> is null.</exception>
public static EncryptParameters RsaOaep256Parameters(byte[] plaintext) =>
new EncryptParameters(EncryptionAlgorithm.RsaOaep256, plaintext, null, null);
/// <summary>
/// Creates an instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A128Gcm"/> encryption algorithm.
/// The nonce will be generated automatically and returned in the <see cref="EncryptResult"/> after encryption.
/// </summary>
/// <param name="plaintext">The plaintext to encrypt.</param>
/// <param name="additionalAuthenticatedData">Optional data that is authenticated but not encrypted.</param>
/// <returns>An instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A128Gcm"/> encryption algorithm.</returns>
/// <exception cref="ArgumentNullException"><paramref name="plaintext"/> is null.</exception>
public static EncryptParameters A128GcmParameters(byte[] plaintext, byte[] additionalAuthenticatedData = null) =>
new EncryptParameters(EncryptionAlgorithm.A128Gcm, plaintext, null, additionalAuthenticatedData);
/// <summary>
/// Creates an instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A192Gcm"/> encryption algorithm.
/// The nonce will be generated automatically and returned in the <see cref="EncryptResult"/> after encryption.
/// </summary>
/// <param name="plaintext">The plaintext to encrypt.</param>
/// <param name="additionalAuthenticatedData">Optional data that is authenticated but not encrypted.</param>
/// <returns>An instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A192Gcm"/> encryption algorithm.</returns>
/// <exception cref="ArgumentNullException"><paramref name="plaintext"/> is null.</exception>
public static EncryptParameters A192GcmParameters(byte[] plaintext, byte[] additionalAuthenticatedData = null) =>
new EncryptParameters(EncryptionAlgorithm.A192Gcm, plaintext, null, additionalAuthenticatedData);
/// <summary>
/// Creates an instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A256Gcm"/> encryption algorithm.
/// The nonce will be generated automatically and returned in the <see cref="EncryptResult"/> after encryption.
/// </summary>
/// <param name="plaintext">The plaintext to encrypt.</param>
/// <param name="additionalAuthenticatedData">Optional data that is authenticated but not encrypted.</param>
/// <returns>An instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A256Gcm"/> encryption algorithm.</returns>
/// <exception cref="ArgumentNullException"><paramref name="plaintext"/> is null.</exception>
public static EncryptParameters A256GcmParameters(byte[] plaintext, byte[] additionalAuthenticatedData = null) =>
new EncryptParameters(EncryptionAlgorithm.A256Gcm, plaintext, null, additionalAuthenticatedData);
/// <summary>
/// Creates an instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A128Cbc"/> encryption algorithm.
/// </summary>
/// <param name="plaintext">The plaintext to encrypt.</param>
/// <param name="iv">Optional initialization vector. If null, a cryptographically random initialization vector will be generated using <see cref="RandomNumberGenerator"/>.</param>
/// <returns>An instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A128Cbc"/> encryption algorithm.</returns>
/// <remarks>
/// Microsoft recommends you not use CBC without first ensuring the integrity of the ciphertext using an HMAC, for example. See https://docs.microsoft.com/dotnet/standard/security/vulnerabilities-cbc-mode for more information.
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="plaintext"/> is null.</exception>
public static EncryptParameters A128CbcParameters(byte[] plaintext, byte[] iv = null) =>
new EncryptParameters(EncryptionAlgorithm.A128Cbc, plaintext, iv, null);
/// <summary>
/// Creates an instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A192Cbc"/> encryption algorithm.
/// </summary>
/// <param name="plaintext">The plaintext to encrypt.</param>
/// <param name="iv">Optional initialization vector. If null, a cryptographically random initialization vector will be generated using <see cref="RandomNumberGenerator"/>.</param>
/// <returns>An instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A192Cbc"/> encryption algorithm.</returns>
/// <remarks>
/// Microsoft recommends you not use CBC without first ensuring the integrity of the ciphertext using an HMAC, for example. See https://docs.microsoft.com/dotnet/standard/security/vulnerabilities-cbc-mode for more information.
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="plaintext"/> is null.</exception>
public static EncryptParameters A192CbcParameters(byte[] plaintext, byte[] iv = null) =>
new EncryptParameters(EncryptionAlgorithm.A192Cbc, plaintext, iv, null);
/// <summary>
/// Creates an instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A256Cbc"/> encryption algorithm.
/// </summary>
/// <param name="plaintext">The plaintext to encrypt.</param>
/// <param name="iv">Optional initialization vector. If null, a cryptographically random initialization vector will be generated using <see cref="RandomNumberGenerator"/>.</param>
/// <returns>An instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A256Cbc"/> encryption algorithm.</returns>
/// <remarks>
/// Microsoft recommends you not use CBC without first ensuring the integrity of the ciphertext using an HMAC, for example. See https://docs.microsoft.com/dotnet/standard/security/vulnerabilities-cbc-mode for more information.
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="plaintext"/> is null.</exception>
public static EncryptParameters A256CbcParameters(byte[] plaintext, byte[] iv = null) =>
new EncryptParameters(EncryptionAlgorithm.A256Cbc, plaintext, iv, null);
/// <summary>
/// Creates an instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A128CbcPad"/> encryption algorithm with PKCS#7 padding.
/// </summary>
/// <param name="plaintext">The plaintext to encrypt.</param>
/// <param name="iv">Optional initialization vector. If null, a cryptographically random initialization vector will be generated using <see cref="RandomNumberGenerator"/>.</param>
/// <returns>An instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A128CbcPad"/> encryption algorithm.</returns>
/// <remarks>
/// Microsoft recommends you not use CBC without first ensuring the integrity of the ciphertext using an HMAC, for example. See https://docs.microsoft.com/dotnet/standard/security/vulnerabilities-cbc-mode for more information.
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="plaintext"/> is null.</exception>
public static EncryptParameters A128CbcPadParameters(byte[] plaintext, byte[] iv = null) =>
new EncryptParameters(EncryptionAlgorithm.A128CbcPad, plaintext, iv, null);
/// <summary>
/// Creates an instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A192CbcPad"/> encryption algorithm with PKCS#7 padding.
/// </summary>
/// <param name="plaintext">The plaintext to encrypt.</param>
/// <param name="iv">Optional initialization vector. If null, a cryptographically random initialization vector will be generated using <see cref="RandomNumberGenerator"/>.</param>
/// <returns>An instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A192CbcPad"/> encryption algorithm.</returns>
/// <remarks>
/// Microsoft recommends you not use CBC without first ensuring the integrity of the ciphertext using an HMAC, for example. See https://docs.microsoft.com/dotnet/standard/security/vulnerabilities-cbc-mode for more information.
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="plaintext"/> is null.</exception>
public static EncryptParameters A192CbcPadParameters(byte[] plaintext, byte[] iv = null) =>
new EncryptParameters(EncryptionAlgorithm.A192CbcPad, plaintext, iv, null);
/// <summary>
/// Creates an instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A256CbcPad"/> encryption algorithm with PKCS#7 padding.
/// </summary>
/// <param name="plaintext">The plaintext to encrypt.</param>
/// <param name="iv">Optional initialization vector. If null, a cryptographically random initialization vector will be generated using <see cref="RandomNumberGenerator"/>.</param>
/// <returns>An instance of the <see cref="EncryptParameters"/> class for the <see cref="EncryptionAlgorithm.A256CbcPad"/> encryption algorithm.</returns>
/// <remarks>
/// Microsoft recommends you not use CBC without first ensuring the integrity of the ciphertext using an HMAC, for example. See https://docs.microsoft.com/dotnet/standard/security/vulnerabilities-cbc-mode for more information.
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="plaintext"/> is null.</exception>
public static EncryptParameters A256CbcPadParameters(byte[] plaintext, byte[] iv = null) =>
new EncryptParameters(EncryptionAlgorithm.A256CbcPad, plaintext, iv, null);
internal EncryptParameters(EncryptionAlgorithm algorithm, byte[] plaintext) :
this(algorithm, plaintext, null, null)
{
}
internal EncryptParameters(EncryptionAlgorithm algorithm, byte[] plaintext, byte[] iv, byte[] additionalAuthenticatedData)
{
Argument.AssertNotNull(plaintext, nameof(plaintext));
Algorithm = algorithm;
Plaintext = plaintext;
Iv = iv;
AdditionalAuthenticatedData = additionalAuthenticatedData;
}
/// <summary>
/// Gets the <see cref="EncryptionAlgorithm"/>.
/// </summary>
public EncryptionAlgorithm Algorithm { get; }
/// <summary>
/// Gets the plaintext to encrypt.
/// </summary>
public byte[] Plaintext { get; }
/// <summary>
/// Gets the initialization vector for encryption.
/// </summary>
public byte[] Iv { get; private set; }
/// <summary>
/// Gets additional data that is authenticated during decryption but not encrypted.
/// </summary>
public byte[] AdditionalAuthenticatedData { get; }
internal void Initialize()
{
// Initialize AES-CBC IV if not specified. This will be used locally or sent remotely.
// The nonce for AES-GCM is only generated for local operations and will be rejected by the service.
if (Iv == null && Algorithm.GetAesCbcEncryptionAlgorithm() != null)
{
Iv = Crypto.GenerateIv(AesCbc.BlockByteSize);
}
}
void IJsonSerializable.WriteProperties(Utf8JsonWriter json)
{
json.WriteString("alg", Algorithm.ToString());
json.WriteString("value", Base64Url.Encode(Plaintext));
if (Iv != null)
{
json.WriteString("iv", Base64Url.Encode(Iv));
}
if (AdditionalAuthenticatedData != null)
{
json.WriteString("aad", Base64Url.Encode(AdditionalAuthenticatedData));
}
}
}
}