@@ -14,7 +14,7 @@ use parsec_client::auth::AuthenticationData;
14
14
use parsec_client:: core:: basic_client:: BasicClient ;
15
15
use parsec_client:: core:: interface:: operations:: list_providers:: ProviderInfo ;
16
16
use parsec_client:: core:: interface:: operations:: psa_algorithm:: {
17
- Algorithm , AsymmetricSignature , Hash ,
17
+ Algorithm , AsymmetricEncryption , AsymmetricSignature , Hash ,
18
18
} ;
19
19
use parsec_client:: core:: interface:: operations:: psa_key_attributes:: {
20
20
Attributes , Lifetime , Policy , Type , UsageFlags ,
@@ -79,6 +79,12 @@ impl TestClient {
79
79
ProviderID :: Core
80
80
}
81
81
82
+ pub fn is_operation_supported ( & mut self , op : Opcode ) -> bool {
83
+ self . list_opcodes ( self . provider ( ) . unwrap ( ) )
84
+ . unwrap ( )
85
+ . contains ( & op)
86
+ }
87
+
82
88
/// Manually set the provider to execute the requests.
83
89
pub fn set_provider ( & mut self , provider : ProviderID ) {
84
90
self . basic_client . set_implicit_provider ( provider) ;
@@ -158,6 +164,64 @@ impl TestClient {
158
164
)
159
165
}
160
166
167
+ pub fn generate_rsa_encryption_keys_rsapkcs1v15crypt (
168
+ & mut self ,
169
+ key_name : String ,
170
+ ) -> Result < ( ) > {
171
+ self . generate_key (
172
+ key_name,
173
+ Attributes {
174
+ lifetime : Lifetime :: Persistent ,
175
+ key_type : Type :: RsaKeyPair ,
176
+ bits : 1024 ,
177
+ policy : Policy {
178
+ usage_flags : UsageFlags {
179
+ sign_hash : false ,
180
+ verify_hash : false ,
181
+ sign_message : false ,
182
+ verify_message : false ,
183
+ export : true ,
184
+ encrypt : true ,
185
+ decrypt : true ,
186
+ cache : false ,
187
+ copy : false ,
188
+ derive : false ,
189
+ } ,
190
+ permitted_algorithms : AsymmetricEncryption :: RsaPkcs1v15Crypt . into ( ) ,
191
+ } ,
192
+ } ,
193
+ )
194
+ }
195
+
196
+ pub fn generate_rsa_encryption_keys_rsaoaep_sha256 ( & mut self , key_name : String ) -> Result < ( ) > {
197
+ self . generate_key (
198
+ key_name,
199
+ Attributes {
200
+ lifetime : Lifetime :: Persistent ,
201
+ key_type : Type :: RsaKeyPair ,
202
+ bits : 1024 ,
203
+ policy : Policy {
204
+ usage_flags : UsageFlags {
205
+ sign_hash : false ,
206
+ verify_hash : false ,
207
+ sign_message : false ,
208
+ verify_message : false ,
209
+ export : true ,
210
+ encrypt : true ,
211
+ decrypt : true ,
212
+ cache : false ,
213
+ copy : false ,
214
+ derive : false ,
215
+ } ,
216
+ permitted_algorithms : AsymmetricEncryption :: RsaOaep {
217
+ hash_alg : Hash :: Sha256 ,
218
+ }
219
+ . into ( ) ,
220
+ } ,
221
+ } ,
222
+ )
223
+ }
224
+
161
225
/// Imports and creates a key with specific attributes.
162
226
pub fn import_key (
163
227
& mut self ,
@@ -179,7 +243,36 @@ impl TestClient {
179
243
Ok ( ( ) )
180
244
}
181
245
182
- /// Import a 1024 bits RSA public key.
246
+ /// Import a 1024 bit RSA key pair
247
+ /// The key pair can only be used for encryption and decryption with RSA PKCS 1v15
248
+ pub fn import_rsa_key_pair ( & mut self , key_name : String , data : Vec < u8 > ) -> Result < ( ) > {
249
+ self . import_key (
250
+ key_name,
251
+ Attributes {
252
+ lifetime : Lifetime :: Persistent ,
253
+ key_type : Type :: RsaKeyPair ,
254
+ bits : 1024 ,
255
+ policy : Policy {
256
+ usage_flags : UsageFlags {
257
+ sign_hash : false ,
258
+ verify_hash : false ,
259
+ sign_message : false ,
260
+ verify_message : true ,
261
+ export : false ,
262
+ encrypt : true ,
263
+ decrypt : true ,
264
+ cache : false ,
265
+ copy : false ,
266
+ derive : false ,
267
+ } ,
268
+ permitted_algorithms : AsymmetricEncryption :: RsaPkcs1v15Crypt . into ( ) ,
269
+ } ,
270
+ } ,
271
+ data,
272
+ )
273
+ }
274
+
275
+ /// Import a 1024 bit RSA public key.
183
276
/// The key can only be used for verifying with the RSA PKCS 1v15 signing algorithm with SHA-256.
184
277
pub fn import_rsa_public_key ( & mut self , key_name : String , data : Vec < u8 > ) -> Result < ( ) > {
185
278
self . import_key (
@@ -288,6 +381,56 @@ impl TestClient {
288
381
)
289
382
}
290
383
384
+ pub fn asymmetric_encrypt_message_with_rsapkcs1v15 (
385
+ & mut self ,
386
+ key_name : String ,
387
+ plaintext : Vec < u8 > ,
388
+ ) -> Result < Vec < u8 > > {
389
+ self . asymmetric_encrypt_message (
390
+ key_name,
391
+ AsymmetricEncryption :: RsaPkcs1v15Crypt ,
392
+ & plaintext,
393
+ None ,
394
+ )
395
+ }
396
+
397
+ pub fn asymmetric_decrypt_message_with_rsapkcs1v15 (
398
+ & mut self ,
399
+ key_name : String ,
400
+ ciphertext : Vec < u8 > ,
401
+ ) -> Result < Vec < u8 > > {
402
+ self . asymmetric_decrypt_message (
403
+ key_name,
404
+ AsymmetricEncryption :: RsaPkcs1v15Crypt ,
405
+ & ciphertext,
406
+ None ,
407
+ )
408
+ }
409
+
410
+ pub fn asymmetric_encrypt_message (
411
+ & mut self ,
412
+ key_name : String ,
413
+ encryption_alg : AsymmetricEncryption ,
414
+ plaintext : & [ u8 ] ,
415
+ salt : Option < & [ u8 ] > ,
416
+ ) -> Result < Vec < u8 > > {
417
+ self . basic_client
418
+ . psa_asymmetric_encrypt ( key_name, encryption_alg, & plaintext, salt)
419
+ . map_err ( convert_error)
420
+ }
421
+
422
+ pub fn asymmetric_decrypt_message (
423
+ & mut self ,
424
+ key_name : String ,
425
+ encryption_alg : AsymmetricEncryption ,
426
+ ciphertext : & [ u8 ] ,
427
+ salt : Option < & [ u8 ] > ,
428
+ ) -> Result < Vec < u8 > > {
429
+ self . basic_client
430
+ . psa_asymmetric_decrypt ( key_name, encryption_alg, & ciphertext, salt)
431
+ . map_err ( convert_error)
432
+ }
433
+
291
434
/// Lists the provider available for the Parsec service.
292
435
pub fn list_providers ( & mut self ) -> Result < Vec < ProviderInfo > > {
293
436
self . basic_client . list_providers ( ) . map_err ( convert_error)
0 commit comments