@@ -13,7 +13,7 @@ use parsec_client::auth::AuthenticationData;
13
13
use parsec_client:: core:: basic_client:: BasicClient ;
14
14
use parsec_client:: core:: interface:: operations:: list_providers:: ProviderInfo ;
15
15
use parsec_client:: core:: interface:: operations:: psa_algorithm:: {
16
- Algorithm , AsymmetricSignature , Hash ,
16
+ Algorithm , AsymmetricSignature , AsymmetricEncryption , Hash ,
17
17
} ;
18
18
use parsec_client:: core:: interface:: operations:: psa_key_attributes:: {
19
19
Attributes , Lifetime , Policy , Type , UsageFlags ,
@@ -157,6 +157,60 @@ impl TestClient {
157
157
)
158
158
}
159
159
160
+ pub fn generate_rsa_encryption_keys_rsapkcs1v15crypt ( & mut self , key_name : String ) -> Result < ( ) > {
161
+ self . generate_key (
162
+ key_name,
163
+ Attributes {
164
+ lifetime : Lifetime :: Persistent ,
165
+ key_type : Type :: RsaKeyPair ,
166
+ bits : 1024 ,
167
+ policy : Policy {
168
+ usage_flags : UsageFlags {
169
+ sign_hash : false ,
170
+ verify_hash : false ,
171
+ sign_message : false ,
172
+ verify_message : false ,
173
+ export : true ,
174
+ encrypt : true ,
175
+ decrypt : true ,
176
+ cache : false ,
177
+ copy : false ,
178
+ derive : false ,
179
+ } ,
180
+ permitted_algorithms : AsymmetricEncryption :: RsaPkcs1v15Crypt . into ( ) ,
181
+ } ,
182
+ }
183
+ )
184
+ }
185
+
186
+ pub fn generate_rsa_encryption_keys_rsaoaep_sha256 ( & mut self , key_name : String ) -> Result < ( ) > {
187
+ self . generate_key (
188
+ key_name,
189
+ Attributes {
190
+ lifetime : Lifetime :: Persistent ,
191
+ key_type : Type :: RsaKeyPair ,
192
+ bits : 1024 ,
193
+ policy : Policy {
194
+ usage_flags : UsageFlags {
195
+ sign_hash : false ,
196
+ verify_hash : false ,
197
+ sign_message : false ,
198
+ verify_message : false ,
199
+ export : true ,
200
+ encrypt : true ,
201
+ decrypt : true ,
202
+ cache : false ,
203
+ copy : false ,
204
+ derive : false ,
205
+ } ,
206
+ permitted_algorithms : AsymmetricEncryption :: RsaOaep {
207
+ hash_alg : Hash :: Sha256 ,
208
+ } . into ( ) ,
209
+ } ,
210
+ }
211
+ )
212
+ }
213
+
160
214
/// Imports and creates a key with specific attributes.
161
215
pub fn import_key (
162
216
& mut self ,
@@ -178,7 +232,36 @@ impl TestClient {
178
232
Ok ( ( ) )
179
233
}
180
234
181
- /// Import a 1024 bits RSA public key.
235
+ /// Import a 1024 bit RSA key pair
236
+ /// The key pair can only be used for encryption and decryption with RSA PKCS 1v15
237
+ pub fn import_rsa_key_pair ( & mut self , key_name : String , data : Vec < u8 > ) -> Result < ( ) > {
238
+ self . import_key (
239
+ key_name,
240
+ Attributes {
241
+ lifetime : Lifetime :: Persistent ,
242
+ key_type : Type :: RsaKeyPair ,
243
+ bits : 1024 ,
244
+ policy : Policy {
245
+ usage_flags : UsageFlags {
246
+ sign_hash : false ,
247
+ verify_hash : false ,
248
+ sign_message : false ,
249
+ verify_message : true ,
250
+ export : false ,
251
+ encrypt : true ,
252
+ decrypt : true ,
253
+ cache : false ,
254
+ copy : false ,
255
+ derive : false ,
256
+ } ,
257
+ permitted_algorithms : AsymmetricEncryption :: RsaPkcs1v15Crypt . into ( ) ,
258
+ } ,
259
+ } ,
260
+ data,
261
+ )
262
+ }
263
+
264
+ /// Import a 1024 bit RSA public key.
182
265
/// The key can only be used for verifying with the RSA PKCS 1v15 signing algorithm with SHA-256.
183
266
pub fn import_rsa_public_key ( & mut self , key_name : String , data : Vec < u8 > ) -> Result < ( ) > {
184
267
self . import_key (
@@ -287,6 +370,64 @@ impl TestClient {
287
370
)
288
371
}
289
372
373
+ pub fn asymmetric_encrypt_message_with_rsapkcs1v15 (
374
+ & mut self ,
375
+ key_name : String ,
376
+ plaintext : Vec < u8 > ,
377
+ ) -> Result < Vec < u8 > > {
378
+ self . asymmetric_encrypt_message (
379
+ key_name,
380
+ AsymmetricEncryption :: RsaPkcs1v15Crypt ,
381
+ & plaintext,
382
+ None ,
383
+ )
384
+ }
385
+
386
+ pub fn asymmetric_decrypt_message_with_rsapkcs1v15 (
387
+ & mut self ,
388
+ key_name : String ,
389
+ ciphertext : Vec < u8 > ,
390
+ ) -> Result < Vec < u8 > > {
391
+ self . asymmetric_decrypt_message (
392
+ key_name,
393
+ AsymmetricEncryption :: RsaPkcs1v15Crypt ,
394
+ & ciphertext,
395
+ None ,
396
+ )
397
+ }
398
+
399
+ pub fn asymmetric_encrypt_message (
400
+ & mut self ,
401
+ key_name : String ,
402
+ encryption_alg : AsymmetricEncryption ,
403
+ plaintext : & [ u8 ] ,
404
+ salt : Option < & [ u8 ] > ) -> Result < Vec < u8 > > {
405
+ self . basic_client
406
+ . psa_asymmetric_encrypt (
407
+ key_name,
408
+ encryption_alg,
409
+ & plaintext,
410
+ salt,
411
+ )
412
+ . map_err ( convert_error)
413
+ }
414
+
415
+ pub fn asymmetric_decrypt_message (
416
+ & mut self ,
417
+ key_name : String ,
418
+ encryption_alg : AsymmetricEncryption ,
419
+ ciphertext : & [ u8 ] ,
420
+ salt : Option < & [ u8 ] > ) -> Result < Vec < u8 > > {
421
+ self . basic_client
422
+ . psa_asymmetric_decrypt (
423
+ key_name,
424
+ encryption_alg,
425
+ & ciphertext,
426
+ salt,
427
+ )
428
+ . map_err ( convert_error)
429
+ }
430
+
290
431
/// Lists the provider available for the Parsec service.
291
432
pub fn list_providers ( & mut self ) -> Result < Vec < ProviderInfo > > {
292
433
self . basic_client . list_providers ( ) . map_err ( convert_error)
0 commit comments