@@ -6,8 +6,13 @@ use std::convert::TryFrom;
66use std:: fmt;
77use thiserror:: Error ;
88use 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 ,
1116 } ,
1217 structures:: { HashScheme , SignatureScheme } ,
1318} ;
@@ -89,15 +94,68 @@ impl From<HashAlgorithm> for MessageDigest {
8994
9095#[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash , Serialize , Deserialize ) ]
9196pub 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 ,
94107}
95108
96109impl From < EncryptionAlgorithm > for AsymmetricAlgorithm {
97110 fn from ( enc_alg : EncryptionAlgorithm ) -> Self {
98111 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+ }
101159 }
102160 }
103161}
@@ -107,8 +165,25 @@ impl TryFrom<&str> for EncryptionAlgorithm {
107165
108166 fn try_from ( value : & str ) -> Result < Self , Self :: Error > {
109167 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 ) ,
112187 _ => Err ( AlgorithmError :: UnsupportedEncryptionAlgorithm (
113188 value. into ( ) ,
114189 ) ) ,
@@ -119,8 +194,16 @@ impl TryFrom<&str> for EncryptionAlgorithm {
119194impl fmt:: Display for EncryptionAlgorithm {
120195 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
121196 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" ,
124207 } ;
125208 write ! ( f, "{value}" )
126209 }
@@ -219,9 +302,13 @@ mod tests {
219302 #[ test]
220303 fn test_encrypt_try_from ( ) {
221304 let result = EncryptionAlgorithm :: try_from ( "rsa" ) ;
222- assert ! ( result. is_ok ( ) ) ;
305+ assert ! ( result. is_ok_and ( |r| r == EncryptionAlgorithm :: Rsa2048 ) ) ;
223306 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 ) ) ;
225312 }
226313 #[ test]
227314 fn test_unsupported_encrypt_try_from ( ) {
0 commit comments