2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
+ using System . Security . Cryptography ;
6
+ using Microsoft . AspNetCore . Cryptography ;
5
7
using Microsoft . AspNetCore . Cryptography . Cng ;
6
8
using Microsoft . AspNetCore . DataProtection . AuthenticatedEncryption . ConfigurationModel ;
7
9
using Microsoft . AspNetCore . DataProtection . KeyManagement ;
@@ -26,7 +28,7 @@ public IAuthenticatedEncryptor CreateEncryptorInstance(IKey key)
26
28
return null ;
27
29
}
28
30
29
- return CreateAuthenticatedEncryptorInstance ( descriptor . MasterKey , descriptor . Settings ) ;
31
+ return CreateAuthenticatedEncryptorInstance ( descriptor . MasterKey , descriptor . Configuration ) ;
30
32
}
31
33
32
34
internal IAuthenticatedEncryptor CreateAuthenticatedEncryptorInstance (
@@ -38,7 +40,7 @@ internal IAuthenticatedEncryptor CreateAuthenticatedEncryptorInstance(
38
40
return null ;
39
41
}
40
42
41
- if ( authenticatedConfiguration . IsGcmAlgorithm ( ) )
43
+ if ( IsGcmAlgorithm ( authenticatedConfiguration . EncryptionAlgorithm ) )
42
44
{
43
45
// GCM requires CNG, and CNG is only supported on Windows.
44
46
if ( ! OSVersionUtil . IsWindows ( ) )
@@ -48,8 +50,8 @@ internal IAuthenticatedEncryptor CreateAuthenticatedEncryptorInstance(
48
50
49
51
var configuration = new CngGcmAuthenticatedEncryptorConfiguration ( )
50
52
{
51
- EncryptionAlgorithm = authenticatedConfiguration . GetBCryptAlgorithmNameFromEncryptionAlgorithm ( ) ,
52
- EncryptionAlgorithmKeySize = authenticatedConfiguration . GetAlgorithmKeySizeInBits ( )
53
+ EncryptionAlgorithm = GetBCryptAlgorithmNameFromEncryptionAlgorithm ( authenticatedConfiguration . EncryptionAlgorithm ) ,
54
+ EncryptionAlgorithmKeySize = GetAlgorithmKeySizeInBits ( authenticatedConfiguration . EncryptionAlgorithm )
53
55
} ;
54
56
55
57
return new CngGcmAuthenticatedEncryptorFactory ( _loggerFactory ) . CreateAuthenticatedEncryptorInstance ( secret , configuration ) ;
@@ -61,9 +63,9 @@ internal IAuthenticatedEncryptor CreateAuthenticatedEncryptorInstance(
61
63
// CNG preferred over managed implementations if running on Windows
62
64
var configuration = new CngCbcAuthenticatedEncryptorConfiguration ( )
63
65
{
64
- EncryptionAlgorithm = authenticatedConfiguration . GetBCryptAlgorithmNameFromEncryptionAlgorithm ( ) ,
65
- EncryptionAlgorithmKeySize = authenticatedConfiguration . GetAlgorithmKeySizeInBits ( ) ,
66
- HashAlgorithm = authenticatedConfiguration . GetBCryptAlgorithmNameFromValidationAlgorithm ( )
66
+ EncryptionAlgorithm = GetBCryptAlgorithmNameFromEncryptionAlgorithm ( authenticatedConfiguration . EncryptionAlgorithm ) ,
67
+ EncryptionAlgorithmKeySize = GetAlgorithmKeySizeInBits ( authenticatedConfiguration . EncryptionAlgorithm ) ,
68
+ HashAlgorithm = GetBCryptAlgorithmNameFromValidationAlgorithm ( authenticatedConfiguration . ValidationAlgorithm )
67
69
} ;
68
70
69
71
return new CngCbcAuthenticatedEncryptorFactory ( _loggerFactory ) . CreateAuthenticatedEncryptorInstance ( secret , configuration ) ;
@@ -73,14 +75,104 @@ internal IAuthenticatedEncryptor CreateAuthenticatedEncryptorInstance(
73
75
// Use managed implementations as a fallback
74
76
var configuration = new ManagedAuthenticatedEncryptorConfiguration ( )
75
77
{
76
- EncryptionAlgorithmType = authenticatedConfiguration . GetManagedTypeFromEncryptionAlgorithm ( ) ,
77
- EncryptionAlgorithmKeySize = authenticatedConfiguration . GetAlgorithmKeySizeInBits ( ) ,
78
- ValidationAlgorithmType = authenticatedConfiguration . GetManagedTypeFromValidationAlgorithm ( )
78
+ EncryptionAlgorithmType = GetManagedTypeFromEncryptionAlgorithm ( authenticatedConfiguration . EncryptionAlgorithm ) ,
79
+ EncryptionAlgorithmKeySize = GetAlgorithmKeySizeInBits ( authenticatedConfiguration . EncryptionAlgorithm ) ,
80
+ ValidationAlgorithmType = GetManagedTypeFromValidationAlgorithm ( authenticatedConfiguration . ValidationAlgorithm )
79
81
} ;
80
82
81
83
return new ManagedAuthenticatedEncryptorFactory ( _loggerFactory ) . CreateAuthenticatedEncryptorInstance ( secret , configuration ) ;
82
84
}
83
85
}
84
86
}
87
+
88
+ internal static bool IsGcmAlgorithm ( EncryptionAlgorithm algorithm )
89
+ {
90
+ return ( EncryptionAlgorithm . AES_128_GCM <= algorithm && algorithm <= EncryptionAlgorithm . AES_256_GCM ) ;
91
+ }
92
+
93
+ private static int GetAlgorithmKeySizeInBits ( EncryptionAlgorithm algorithm )
94
+ {
95
+ switch ( algorithm )
96
+ {
97
+ case EncryptionAlgorithm . AES_128_CBC :
98
+ case EncryptionAlgorithm . AES_128_GCM :
99
+ return 128 ;
100
+
101
+ case EncryptionAlgorithm . AES_192_CBC :
102
+ case EncryptionAlgorithm . AES_192_GCM :
103
+ return 192 ;
104
+
105
+ case EncryptionAlgorithm . AES_256_CBC :
106
+ case EncryptionAlgorithm . AES_256_GCM :
107
+ return 256 ;
108
+
109
+ default :
110
+ throw new ArgumentOutOfRangeException ( nameof ( EncryptionAlgorithm ) ) ;
111
+ }
112
+ }
113
+
114
+ private static string GetBCryptAlgorithmNameFromEncryptionAlgorithm ( EncryptionAlgorithm algorithm )
115
+ {
116
+ switch ( algorithm )
117
+ {
118
+ case EncryptionAlgorithm . AES_128_CBC :
119
+ case EncryptionAlgorithm . AES_192_CBC :
120
+ case EncryptionAlgorithm . AES_256_CBC :
121
+ case EncryptionAlgorithm . AES_128_GCM :
122
+ case EncryptionAlgorithm . AES_192_GCM :
123
+ case EncryptionAlgorithm . AES_256_GCM :
124
+ return Constants . BCRYPT_AES_ALGORITHM ;
125
+
126
+ default :
127
+ throw new ArgumentOutOfRangeException ( nameof ( EncryptionAlgorithm ) ) ;
128
+ }
129
+ }
130
+
131
+ private static string GetBCryptAlgorithmNameFromValidationAlgorithm ( ValidationAlgorithm algorithm )
132
+ {
133
+ switch ( algorithm )
134
+ {
135
+ case ValidationAlgorithm . HMACSHA256 :
136
+ return Constants . BCRYPT_SHA256_ALGORITHM ;
137
+
138
+ case ValidationAlgorithm . HMACSHA512 :
139
+ return Constants . BCRYPT_SHA512_ALGORITHM ;
140
+
141
+ default :
142
+ throw new ArgumentOutOfRangeException ( nameof ( ValidationAlgorithm ) ) ;
143
+ }
144
+ }
145
+
146
+ private static Type GetManagedTypeFromEncryptionAlgorithm ( EncryptionAlgorithm algorithm )
147
+ {
148
+ switch ( algorithm )
149
+ {
150
+ case EncryptionAlgorithm . AES_128_CBC :
151
+ case EncryptionAlgorithm . AES_192_CBC :
152
+ case EncryptionAlgorithm . AES_256_CBC :
153
+ case EncryptionAlgorithm . AES_128_GCM :
154
+ case EncryptionAlgorithm . AES_192_GCM :
155
+ case EncryptionAlgorithm . AES_256_GCM :
156
+ return typeof ( Aes ) ;
157
+
158
+ default :
159
+ throw new ArgumentOutOfRangeException ( nameof ( EncryptionAlgorithm ) ) ;
160
+ }
161
+ }
162
+
163
+ private static Type GetManagedTypeFromValidationAlgorithm ( ValidationAlgorithm algorithm )
164
+ {
165
+ switch ( algorithm )
166
+ {
167
+ case ValidationAlgorithm . HMACSHA256 :
168
+ return typeof ( HMACSHA256 ) ;
169
+
170
+ case ValidationAlgorithm . HMACSHA512 :
171
+ return typeof ( HMACSHA512 ) ;
172
+
173
+ default :
174
+ throw new ArgumentOutOfRangeException ( nameof ( ValidationAlgorithm ) ) ;
175
+ }
176
+ }
85
177
}
86
178
}
0 commit comments