Skip to content

Commit 67318b0

Browse files
authored
Add EncryptOptions, DecryptOptions to model factory (Azure#16178)
* Add EncryptOptions, DecryptOptions to model factory * Update public APIs
1 parent 28d3a2a commit 67318b0

File tree

5 files changed

+118
-18
lines changed

5 files changed

+118
-18
lines changed

sdk/keyvault/Azure.Security.KeyVault.Keys/api/Azure.Security.KeyVault.Keys.netstandard2.0.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ public CreateRsaKeyOptions(string name, bool hardwareProtected = false) { }
2727
}
2828
public static partial class CryptographyModelFactory
2929
{
30+
public static Azure.Security.KeyVault.Keys.Cryptography.DecryptOptions DecryptOptions(Azure.Security.KeyVault.Keys.Cryptography.EncryptionAlgorithm algorithm, byte[] ciphertext, byte[] iv = null, byte[] authenticationTag = null) { throw null; }
3031
public static Azure.Security.KeyVault.Keys.Cryptography.DecryptResult DecryptResult(string keyId = null, byte[] plaintext = null, Azure.Security.KeyVault.Keys.Cryptography.EncryptionAlgorithm algorithm = default(Azure.Security.KeyVault.Keys.Cryptography.EncryptionAlgorithm)) { throw null; }
32+
public static Azure.Security.KeyVault.Keys.Cryptography.EncryptOptions EncryptOptions(Azure.Security.KeyVault.Keys.Cryptography.EncryptionAlgorithm algorithm, byte[] plaintext, byte[] iv = null) { throw null; }
3133
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
3234
public static Azure.Security.KeyVault.Keys.Cryptography.EncryptResult EncryptResult(string keyId, byte[] ciphertext, Azure.Security.KeyVault.Keys.Cryptography.EncryptionAlgorithm algorithm) { throw null; }
3335
public static Azure.Security.KeyVault.Keys.Cryptography.EncryptResult EncryptResult(string keyId = null, byte[] ciphertext = null, Azure.Security.KeyVault.Keys.Cryptography.EncryptionAlgorithm algorithm = default(Azure.Security.KeyVault.Keys.Cryptography.EncryptionAlgorithm), byte[] iv = null, byte[] authenticatedTag = null, byte[] additionalAuthenticatedData = null) { throw null; }
@@ -393,7 +395,7 @@ internal EncryptOptions() { }
393395
public partial class EncryptResult
394396
{
395397
internal EncryptResult() { }
396-
public byte[] AdditionalAuthenticatedData { get { throw null; } set { } }
398+
public byte[] AdditionalAuthenticatedData { get { throw null; } }
397399
public Azure.Security.KeyVault.Keys.Cryptography.EncryptionAlgorithm Algorithm { get { throw null; } }
398400
public byte[] AuthenticationTag { get { throw null; } }
399401
public byte[] Ciphertext { get { throw null; } }

sdk/keyvault/Azure.Security.KeyVault.Keys/src/Cryptography/CryptographyModelFactory.cs

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System;
45
using System.ComponentModel;
56
using Azure.Security.KeyVault.Keys.Cryptography;
67

@@ -12,12 +13,27 @@ namespace Azure.Security.KeyVault.Keys
1213
public static class CryptographyModelFactory
1314
{
1415
/// <summary>
15-
/// Initializes a new instance of the <see cref="Cryptography.DecryptResult"/> for mocking purposes.
16+
/// Initializes a new instance of the <see cref="Cryptography.DecryptOptions"/> class for mocking purposes.
17+
/// </summary>
18+
/// <param name="algorithm">Sets the <see cref="DecryptOptions.Algorithm"/> property.</param>
19+
/// <param name="ciphertext">Sets the <see cref="DecryptOptions.Ciphertext"/> property.</param>
20+
/// <param name="iv">Sets the <see cref="DecryptOptions.Iv"/> property.</param>
21+
/// <param name="authenticationTag">Sets the <see cref="DecryptOptions.AuthenticationTag"/> property.</param>
22+
/// <returns>A new instance of the <see cref="Cryptography.DecryptOptions"/> class for mocking purposes.</returns>
23+
/// <exception cref="ArgumentNullException"><paramref name="ciphertext"/> is null.</exception>
24+
public static DecryptOptions DecryptOptions(EncryptionAlgorithm algorithm, byte[] ciphertext, byte[] iv = default, byte[] authenticationTag = default) => new DecryptOptions(algorithm, ciphertext)
25+
{
26+
Iv = iv,
27+
AuthenticationTag = authenticationTag,
28+
};
29+
30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="Cryptography.DecryptResult"/> class for mocking purposes.
1632
/// </summary>
1733
/// <param name="keyId">Sets the <see cref="DecryptResult.KeyId"/> property.</param>
1834
/// <param name="plaintext">Sets the <see cref="DecryptResult.Plaintext"/> property.</param>
1935
/// <param name="algorithm">Sets the <see cref="DecryptResult.Algorithm"/> property.</param>
20-
/// <returns>A new instance of the <see cref="Cryptography.DecryptResult"/> for mocking purposes.</returns>
36+
/// <returns>A new instance of the <see cref="Cryptography.DecryptResult"/> class for mocking purposes.</returns>
2137
public static DecryptResult DecryptResult(string keyId = default, byte[] plaintext = default, EncryptionAlgorithm algorithm = default) => new DecryptResult
2238
{
2339
KeyId = keyId,
@@ -26,25 +42,35 @@ public static class CryptographyModelFactory
2642
};
2743

2844
/// <summary>
29-
/// Initializes a new instance of the <see cref="Cryptography.EncryptResult"/> for mocking purposes.
45+
/// Initializes a new instance of the <see cref="Cryptography.EncryptOptions"/> class for mocking purposes.
46+
/// </summary>
47+
/// <param name="algorithm">Sets the <see cref="EncryptOptions.Algorithm"/> property.</param>
48+
/// <param name="plaintext">Sets the <see cref="EncryptOptions.Plaintext"/> property.</param>
49+
/// <param name="iv">Sets the <see cref="DecryptOptions.Iv"/> property.</param>
50+
/// <returns>A new instance of the <see cref="Cryptography.EncryptOptions"/> class for mocking purposes.</returns>
51+
/// <exception cref="ArgumentNullException"><paramref name="plaintext"/> is null.</exception>
52+
public static EncryptOptions EncryptOptions(EncryptionAlgorithm algorithm, byte[] plaintext, byte[] iv = default) => new EncryptOptions(algorithm, plaintext, iv, null);
53+
54+
/// <summary>
55+
/// Initializes a new instance of the <see cref="Cryptography.EncryptResult"/> class for mocking purposes.
3056
/// </summary>
3157
/// <param name="keyId">Sets the <see cref="EncryptResult.KeyId"/> property.</param>
3258
/// <param name="ciphertext">Sets the <see cref="EncryptResult.Ciphertext"/> property.</param>
3359
/// <param name="algorithm">Sets the <see cref="EncryptResult.Algorithm"/> property.</param>
34-
/// <returns>A new instance of the <see cref="Cryptography.EncryptResult"/> for mocking purposes.</returns>
60+
/// <returns>A new instance of the <see cref="Cryptography.EncryptResult"/> class for mocking purposes.</returns>
3561
[EditorBrowsable(EditorBrowsableState.Never)]
3662
public static EncryptResult EncryptResult(string keyId, byte[] ciphertext, EncryptionAlgorithm algorithm) => EncryptResult(keyId, ciphertext, algorithm, null);
3763

3864
/// <summary>
39-
/// Initializes a new instance of the <see cref="Cryptography.EncryptResult"/> for mocking purposes.
65+
/// Initializes a new instance of the <see cref="Cryptography.EncryptResult"/> class for mocking purposes.
4066
/// </summary>
4167
/// <param name="keyId">Sets the <see cref="EncryptResult.KeyId"/> property.</param>
4268
/// <param name="ciphertext">Sets the <see cref="EncryptResult.Ciphertext"/> property.</param>
4369
/// <param name="algorithm">Sets the <see cref="EncryptResult.Algorithm"/> property.</param>
4470
/// <param name="iv">Sets the initialization vector for encryption.</param>
4571
/// <param name="authenticatedTag">Sets the authenticated tag resulting from encryption with a symmetric key using AES.</param>
4672
/// <param name="additionalAuthenticatedData">Sets additional data that is authenticated during decryption but not encrypted.</param>
47-
/// <returns>A new instance of the <see cref="Cryptography.EncryptResult"/> for mocking purposes.</returns>
73+
/// <returns>A new instance of the <see cref="Cryptography.EncryptResult"/> class for mocking purposes.</returns>
4874
public static EncryptResult EncryptResult(string keyId = default, byte[] ciphertext = default, EncryptionAlgorithm algorithm = default, byte[] iv = default, byte[] authenticatedTag = default, byte[] additionalAuthenticatedData = default) => new EncryptResult
4975
{
5076
KeyId = keyId,
@@ -56,12 +82,12 @@ public static class CryptographyModelFactory
5682
};
5783

5884
/// <summary>
59-
/// Initializes a new instance of the <see cref="Cryptography.SignResult"/> for mocking purposes.
85+
/// Initializes a new instance of the <see cref="Cryptography.SignResult"/> class for mocking purposes.
6086
/// </summary>
6187
/// <param name="keyId">Sets the <see cref="SignResult.KeyId"/> property.</param>
6288
/// <param name="signature">Sets the <see cref="SignResult.Signature"/> property.</param>
6389
/// <param name="algorithm">Sets the <see cref="SignResult.Algorithm"/> property.</param>
64-
/// <returns>A new instance of the <see cref="Cryptography.SignResult"/> for mocking purposes.</returns>
90+
/// <returns>A new instance of the <see cref="Cryptography.SignResult"/> class for mocking purposes.</returns>
6591
public static SignResult SignResult(string keyId = default, byte[] signature = default, SignatureAlgorithm algorithm = default) => new SignResult
6692
{
6793
KeyId = keyId,
@@ -70,12 +96,12 @@ public static class CryptographyModelFactory
7096
};
7197

7298
/// <summary>
73-
/// Initializes a new instance of the <see cref="Cryptography.UnwrapResult"/> for mocking purposes.
99+
/// Initializes a new instance of the <see cref="Cryptography.UnwrapResult"/> class for mocking purposes.
74100
/// </summary>
75101
/// <param name="keyId">Sets the <see cref="UnwrapResult.KeyId"/> property.</param>
76102
/// <param name="key">Sets the <see cref="UnwrapResult.Key"/> property.</param>
77103
/// <param name="algorithm">Sets the <see cref="UnwrapResult.Algorithm"/> property.</param>
78-
/// <returns>A new instance of the <see cref="Cryptography.UnwrapResult"/> for mocking purposes.</returns>
104+
/// <returns>A new instance of the <see cref="Cryptography.UnwrapResult"/> class for mocking purposes.</returns>
79105
public static UnwrapResult UnwrapResult(string keyId = default, byte[] key = default, KeyWrapAlgorithm algorithm = default) => new UnwrapResult
80106
{
81107
KeyId = keyId,
@@ -84,12 +110,12 @@ public static class CryptographyModelFactory
84110
};
85111

86112
/// <summary>
87-
/// Initializes a new instance of the <see cref="Cryptography.VerifyResult"/> for mocking purposes.
113+
/// Initializes a new instance of the <see cref="Cryptography.VerifyResult"/> class for mocking purposes.
88114
/// </summary>
89115
/// <param name="keyId">Sets the <see cref="VerifyResult.KeyId"/> property.</param>
90116
/// <param name="isValid">Sets the <see cref="VerifyResult.IsValid"/> property.</param>
91117
/// <param name="algorithm">Sets the <see cref="VerifyResult.Algorithm"/> property.</param>
92-
/// <returns>A new instance of the <see cref="Cryptography.VerifyResult"/> for mocking purposes.</returns>
118+
/// <returns>A new instance of the <see cref="Cryptography.VerifyResult"/> class for mocking purposes.</returns>
93119
public static VerifyResult VerifyResult(string keyId = default, bool isValid = default, SignatureAlgorithm algorithm = default) => new VerifyResult
94120
{
95121
KeyId = keyId,
@@ -98,12 +124,12 @@ public static class CryptographyModelFactory
98124
};
99125

100126
/// <summary>
101-
/// Initializes a new instance of the <see cref="Cryptography.WrapResult"/> for mocking purposes.
127+
/// Initializes a new instance of the <see cref="Cryptography.WrapResult"/> class for mocking purposes.
102128
/// </summary>
103129
/// <param name="keyId">Sets the <see cref="WrapResult.KeyId"/> property.</param>
104130
/// <param name="key">Sets the <see cref="WrapResult.EncryptedKey"/> property.</param>
105131
/// <param name="algorithm">Sets the <see cref="WrapResult.Algorithm"/> property.</param>
106-
/// <returns>A new instance of the <see cref="Cryptography.WrapResult"/> for mocking purposes.</returns>
132+
/// <returns>A new instance of the <see cref="Cryptography.WrapResult"/> class for mocking purposes.</returns>
107133
public static WrapResult WrapResult(string keyId = default, byte[] key = default, KeyWrapAlgorithm algorithm = default) => new WrapResult
108134
{
109135
KeyId = keyId,

sdk/keyvault/Azure.Security.KeyVault.Keys/src/Cryptography/DecryptOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ internal DecryptOptions(EncryptionAlgorithm algorithm, byte[] ciphertext, byte[]
179179
/// <summary>
180180
/// Gets the initialization vector for decryption.
181181
/// </summary>
182-
public byte[] Iv { get; }
182+
public byte[] Iv { get; internal set; }
183183

184184
/// <summary>
185185
/// Gets the authenticated tag resulting from encryption with a symmetric key using AES.
186186
/// </summary>
187-
public byte[] AuthenticationTag { get; }
187+
public byte[] AuthenticationTag { get; internal set; }
188188

189189
/// <summary>
190190
/// Gets or sets additional data that is authenticated during decryption but not encrypted.

sdk/keyvault/Azure.Security.KeyVault.Keys/src/Cryptography/EncryptResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal EncryptResult()
4343
/// <summary>
4444
/// Gets additional data that is authenticated during decryption but not encrypted.
4545
/// </summary>
46-
public byte[] AdditionalAuthenticatedData { get; set; }
46+
public byte[] AdditionalAuthenticatedData { get; internal set; }
4747

4848
/// <summary>
4949
/// Gets the <see cref="EncryptionAlgorithm"/> used for encryption. This must be stored alongside the <see cref="Ciphertext"/> as the same algorithm must be used to decrypt it.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using Azure.Security.KeyVault.Keys.Cryptography;
6+
using NUnit.Framework;
7+
8+
namespace Azure.Security.KeyVault.Keys.Tests
9+
{
10+
public class CryptographyModelFactoryTests
11+
{
12+
[Test]
13+
public void DecryptOptionsRequiresCiphertext() =>
14+
Assert.AreEqual("ciphertext", Assert.Throws<ArgumentNullException>(() => CryptographyModelFactory.DecryptOptions(EncryptionAlgorithm.A128Cbc, null)).ParamName);
15+
16+
[Test]
17+
public void DecryptOptionsOnlyRequired()
18+
{
19+
byte[] buffer = new byte[] { 0, 1, 2, 3 };
20+
DecryptOptions options = CryptographyModelFactory.DecryptOptions(EncryptionAlgorithm.A128Cbc, buffer, null, null);
21+
22+
Assert.AreEqual(EncryptionAlgorithm.A128Cbc, options.Algorithm);
23+
CollectionAssert.AreEqual(buffer, options.Ciphertext);
24+
Assert.IsNull(options.Iv);
25+
Assert.IsNull(options.AuthenticationTag);
26+
Assert.IsNull(options.AdditionalAuthenticatedData);
27+
}
28+
29+
[Test]
30+
public void DecryptOptionsAll()
31+
{
32+
byte[] buffer = new byte[] { 0, 1, 2, 3 };
33+
DecryptOptions options = CryptographyModelFactory.DecryptOptions(EncryptionAlgorithm.A128Cbc, buffer, buffer, buffer);
34+
options.AdditionalAuthenticatedData = buffer;
35+
36+
Assert.AreEqual(EncryptionAlgorithm.A128Cbc, options.Algorithm);
37+
CollectionAssert.AreEqual(buffer, options.Ciphertext);
38+
CollectionAssert.AreEqual(buffer, options.Iv);
39+
CollectionAssert.AreEqual(buffer, options.AuthenticationTag);
40+
CollectionAssert.AreEqual(buffer, options.AdditionalAuthenticatedData);
41+
}
42+
43+
[Test]
44+
public void EncryptOptionsRequiresPlaintext() =>
45+
Assert.AreEqual("plaintext", Assert.Throws<ArgumentNullException>(() => CryptographyModelFactory.EncryptOptions(EncryptionAlgorithm.A128Cbc, null)).ParamName);
46+
47+
[Test]
48+
public void EncryptOptionsOnlyRequired()
49+
{
50+
byte[] buffer = new byte[] { 0, 1, 2, 3 };
51+
EncryptOptions options = CryptographyModelFactory.EncryptOptions(EncryptionAlgorithm.A128Cbc, buffer);
52+
53+
Assert.AreEqual(EncryptionAlgorithm.A128Cbc, options.Algorithm);
54+
CollectionAssert.AreEqual(buffer, options.Plaintext);
55+
Assert.IsNull(options.Iv);
56+
Assert.IsNull(options.AdditionalAuthenticatedData);
57+
}
58+
59+
[Test]
60+
public void EncryptOptionsAll()
61+
{
62+
byte[] buffer = new byte[] { 0, 1, 2, 3 };
63+
EncryptOptions options = CryptographyModelFactory.EncryptOptions(EncryptionAlgorithm.A128Cbc, buffer, buffer);
64+
options.AdditionalAuthenticatedData = buffer;
65+
66+
Assert.AreEqual(EncryptionAlgorithm.A128Cbc, options.Algorithm);
67+
CollectionAssert.AreEqual(buffer, options.Plaintext);
68+
CollectionAssert.AreEqual(buffer, options.Iv);
69+
CollectionAssert.AreEqual(buffer, options.AdditionalAuthenticatedData);
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)