@@ -6,8 +6,13 @@ use std::convert::TryFrom;
6
6
use std:: fmt;
7
7
use thiserror:: Error ;
8
8
use tss_esapi:: {
9
- interface_types:: algorithm:: {
10
- AsymmetricAlgorithm , HashingAlgorithm , SignatureSchemeAlgorithm ,
9
+ abstraction:: AsymmetricAlgorithmSelection ,
10
+ interface_types:: {
11
+ algorithm:: {
12
+ AsymmetricAlgorithm , HashingAlgorithm , SignatureSchemeAlgorithm ,
13
+ } ,
14
+ ecc:: EccCurve ,
15
+ key_bits:: RsaKeyBits ,
11
16
} ,
12
17
structures:: { HashScheme , SignatureScheme } ,
13
18
} ;
@@ -89,15 +94,68 @@ impl From<HashAlgorithm> for MessageDigest {
89
94
90
95
#[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash , Serialize , Deserialize ) ]
91
96
pub enum EncryptionAlgorithm {
92
- Rsa ,
93
- Ecc ,
97
+ Rsa1024 ,
98
+ Rsa2048 ,
99
+ Rsa3072 ,
100
+ Rsa4096 ,
101
+ Ecc192 ,
102
+ Ecc224 ,
103
+ Ecc256 ,
104
+ Ecc384 ,
105
+ Ecc521 ,
106
+ EccSm2 ,
94
107
}
95
108
96
109
impl From < EncryptionAlgorithm > for AsymmetricAlgorithm {
97
110
fn from ( enc_alg : EncryptionAlgorithm ) -> Self {
98
111
match enc_alg {
99
- EncryptionAlgorithm :: Rsa => AsymmetricAlgorithm :: Rsa ,
100
- EncryptionAlgorithm :: Ecc => AsymmetricAlgorithm :: Ecc ,
112
+ EncryptionAlgorithm :: Rsa1024 => AsymmetricAlgorithm :: Rsa ,
113
+ EncryptionAlgorithm :: Rsa2048 => AsymmetricAlgorithm :: Rsa ,
114
+ EncryptionAlgorithm :: Rsa3072 => AsymmetricAlgorithm :: Rsa ,
115
+ EncryptionAlgorithm :: Rsa4096 => AsymmetricAlgorithm :: Rsa ,
116
+ EncryptionAlgorithm :: Ecc192 => AsymmetricAlgorithm :: Ecc ,
117
+ EncryptionAlgorithm :: Ecc224 => AsymmetricAlgorithm :: Ecc ,
118
+ EncryptionAlgorithm :: Ecc256 => AsymmetricAlgorithm :: Ecc ,
119
+ EncryptionAlgorithm :: Ecc384 => AsymmetricAlgorithm :: Ecc ,
120
+ EncryptionAlgorithm :: Ecc521 => AsymmetricAlgorithm :: Ecc ,
121
+ EncryptionAlgorithm :: EccSm2 => AsymmetricAlgorithm :: Ecc ,
122
+ }
123
+ }
124
+ }
125
+
126
+ impl From < EncryptionAlgorithm > for AsymmetricAlgorithmSelection {
127
+ fn from ( enc_alg : EncryptionAlgorithm ) -> Self {
128
+ match enc_alg {
129
+ EncryptionAlgorithm :: Rsa1024 => {
130
+ AsymmetricAlgorithmSelection :: Rsa ( RsaKeyBits :: Rsa1024 )
131
+ }
132
+ EncryptionAlgorithm :: Rsa2048 => {
133
+ AsymmetricAlgorithmSelection :: Rsa ( RsaKeyBits :: Rsa2048 )
134
+ }
135
+ EncryptionAlgorithm :: Rsa3072 => {
136
+ AsymmetricAlgorithmSelection :: Rsa ( RsaKeyBits :: Rsa3072 )
137
+ }
138
+ EncryptionAlgorithm :: Rsa4096 => {
139
+ AsymmetricAlgorithmSelection :: Rsa ( RsaKeyBits :: Rsa4096 )
140
+ }
141
+ EncryptionAlgorithm :: Ecc192 => {
142
+ AsymmetricAlgorithmSelection :: Ecc ( EccCurve :: NistP192 )
143
+ }
144
+ EncryptionAlgorithm :: Ecc224 => {
145
+ AsymmetricAlgorithmSelection :: Ecc ( EccCurve :: NistP224 )
146
+ }
147
+ EncryptionAlgorithm :: Ecc256 => {
148
+ AsymmetricAlgorithmSelection :: Ecc ( EccCurve :: NistP256 )
149
+ }
150
+ EncryptionAlgorithm :: Ecc384 => {
151
+ AsymmetricAlgorithmSelection :: Ecc ( EccCurve :: NistP384 )
152
+ }
153
+ EncryptionAlgorithm :: Ecc521 => {
154
+ AsymmetricAlgorithmSelection :: Ecc ( EccCurve :: NistP521 )
155
+ }
156
+ EncryptionAlgorithm :: EccSm2 => {
157
+ AsymmetricAlgorithmSelection :: Ecc ( EccCurve :: Sm2P256 )
158
+ }
101
159
}
102
160
}
103
161
}
@@ -107,8 +165,25 @@ impl TryFrom<&str> for EncryptionAlgorithm {
107
165
108
166
fn try_from ( value : & str ) -> Result < Self , Self :: Error > {
109
167
match value {
110
- "rsa" => Ok ( EncryptionAlgorithm :: Rsa ) ,
111
- "ecc" => Ok ( EncryptionAlgorithm :: Ecc ) ,
168
+ /* Use default key size and curve if not explicitly specified */
169
+ "rsa" => Ok ( EncryptionAlgorithm :: Rsa2048 ) ,
170
+ "ecc" => Ok ( EncryptionAlgorithm :: Ecc256 ) ,
171
+ "rsa1024" => Ok ( EncryptionAlgorithm :: Rsa1024 ) ,
172
+ "rsa2048" => Ok ( EncryptionAlgorithm :: Rsa2048 ) ,
173
+ "rsa3072" => Ok ( EncryptionAlgorithm :: Rsa3072 ) ,
174
+ "rsa4096" => Ok ( EncryptionAlgorithm :: Rsa4096 ) ,
175
+ "ecc192" => Ok ( EncryptionAlgorithm :: Ecc192 ) ,
176
+ "ecc_nist_p192" => Ok ( EncryptionAlgorithm :: Ecc192 ) ,
177
+ "ecc224" => Ok ( EncryptionAlgorithm :: Ecc224 ) ,
178
+ "ecc_nist_p224" => Ok ( EncryptionAlgorithm :: Ecc224 ) ,
179
+ "ecc256" => Ok ( EncryptionAlgorithm :: Ecc256 ) ,
180
+ "ecc_nist_p256" => Ok ( EncryptionAlgorithm :: Ecc256 ) ,
181
+ "ecc384" => Ok ( EncryptionAlgorithm :: Ecc384 ) ,
182
+ "ecc_nist_p384" => Ok ( EncryptionAlgorithm :: Ecc384 ) ,
183
+ "ecc521" => Ok ( EncryptionAlgorithm :: Ecc521 ) ,
184
+ "ecc_nist_p521" => Ok ( EncryptionAlgorithm :: Ecc521 ) ,
185
+ "ecc_sm2" => Ok ( EncryptionAlgorithm :: EccSm2 ) ,
186
+ "ecc_sm2_p256" => Ok ( EncryptionAlgorithm :: EccSm2 ) ,
112
187
_ => Err ( AlgorithmError :: UnsupportedEncryptionAlgorithm (
113
188
value. into ( ) ,
114
189
) ) ,
@@ -119,8 +194,16 @@ impl TryFrom<&str> for EncryptionAlgorithm {
119
194
impl fmt:: Display for EncryptionAlgorithm {
120
195
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
121
196
let value = match self {
122
- EncryptionAlgorithm :: Rsa => "rsa" ,
123
- EncryptionAlgorithm :: Ecc => "ecc" ,
197
+ EncryptionAlgorithm :: Rsa1024 => "rsa1024" ,
198
+ EncryptionAlgorithm :: Rsa2048 => "rsa2048" ,
199
+ EncryptionAlgorithm :: Rsa3072 => "rsa3072" ,
200
+ EncryptionAlgorithm :: Rsa4096 => "rsa4096" ,
201
+ EncryptionAlgorithm :: Ecc192 => "ecc192" ,
202
+ EncryptionAlgorithm :: Ecc224 => "ecc224" ,
203
+ EncryptionAlgorithm :: Ecc256 => "ecc256" ,
204
+ EncryptionAlgorithm :: Ecc384 => "ecc384" ,
205
+ EncryptionAlgorithm :: Ecc521 => "ecc521" ,
206
+ EncryptionAlgorithm :: EccSm2 => "ecc_sm2" ,
124
207
} ;
125
208
write ! ( f, "{value}" )
126
209
}
@@ -219,9 +302,13 @@ mod tests {
219
302
#[ test]
220
303
fn test_encrypt_try_from ( ) {
221
304
let result = EncryptionAlgorithm :: try_from ( "rsa" ) ;
222
- assert ! ( result. is_ok ( ) ) ;
305
+ assert ! ( result. is_ok_and ( |r| r == EncryptionAlgorithm :: Rsa2048 ) ) ;
223
306
let result = EncryptionAlgorithm :: try_from ( "ecc" ) ;
224
- assert ! ( result. is_ok( ) ) ;
307
+ assert ! ( result. is_ok_and( |r| r == EncryptionAlgorithm :: Ecc256 ) ) ;
308
+ let result = EncryptionAlgorithm :: try_from ( "rsa4096" ) ;
309
+ assert ! ( result. is_ok_and( |r| r == EncryptionAlgorithm :: Rsa4096 ) ) ;
310
+ let result = EncryptionAlgorithm :: try_from ( "ecc256" ) ;
311
+ assert ! ( result. is_ok_and( |r| r == EncryptionAlgorithm :: Ecc256 ) ) ;
225
312
}
226
313
#[ test]
227
314
fn test_unsupported_encrypt_try_from ( ) {
0 commit comments