@@ -197,6 +197,18 @@ where
197
197
Ok ( ( ) )
198
198
}
199
199
200
+ /// Prepares the context for encapsulateion using the public key.
201
+ #[ corresponds( EVP_PKEY_encapsulate_init ) ]
202
+ #[ inline]
203
+ pub fn encapsulate_init ( & mut self ) -> Result < ( ) , ErrorStack > {
204
+ unsafe {
205
+ cvt ( ffi:: EVP_PKEY_encapsulate_init ( self . as_ptr ( ) ,
206
+ ptr:: null ( ) ) ) ?;
207
+ }
208
+
209
+ Ok ( ( ) )
210
+ }
211
+
200
212
/// Prepares the context for signature verification using the public key.
201
213
#[ corresponds( EVP_PKEY_verify_init ) ]
202
214
#[ inline]
@@ -250,6 +262,43 @@ where
250
262
Ok ( len)
251
263
}
252
264
265
+ /// Performs a public key encapsulation operation.
266
+ #[ corresponds( EVP_PKEY_encapsulate ) ]
267
+ pub fn encapsulate ( & mut self , wrappedkey : Option < & mut [ u8 ] > , genkey : Option < & mut [ u8 ] > )
268
+ -> Result < ( usize , usize ) , ErrorStack >
269
+ {
270
+ let mut wrappedkey_len = wrappedkey. as_ref ( ) . map_or ( 0 , |b| b. len ( ) ) ;
271
+ let mut genkey_len = genkey. as_ref ( ) . map_or ( 0 , |b| b. len ( ) ) ;
272
+ unsafe {
273
+ cvt ( ffi:: EVP_PKEY_encapsulate (
274
+ self . as_ptr ( ) ,
275
+ wrappedkey. map_or ( ptr:: null_mut ( ) , |b| b. as_mut_ptr ( ) ) ,
276
+ & mut wrappedkey_len,
277
+ genkey. map_or ( ptr:: null_mut ( ) , |b| b. as_mut_ptr ( ) ) ,
278
+ & mut genkey_len,
279
+ ) ) ?;
280
+ }
281
+
282
+ Ok ( ( wrappedkey_len, genkey_len) )
283
+ }
284
+
285
+ /// Like [`Self::encapsulate`] but appends ciphertext and key to a [`Vec`].
286
+ pub fn encapsulate_to_vec ( & mut self , wrappedkey : & mut Vec < u8 > , genkey : & mut Vec < u8 > )
287
+ -> Result < ( usize , usize ) , ErrorStack >
288
+ {
289
+ let wrappedkey_base = wrappedkey. len ( ) ;
290
+ let genkey_base = genkey. len ( ) ;
291
+ let ( wrappedkey_len, genkey_len) = self . encapsulate ( None , None ) ?;
292
+ wrappedkey. resize ( wrappedkey_base + wrappedkey_len, 0 ) ;
293
+ genkey. resize ( genkey_base + genkey_len, 0 ) ;
294
+ let ( wrappedkey_len, genkey_len) =
295
+ self . encapsulate ( Some ( & mut wrappedkey[ wrappedkey_base..] ) ,
296
+ Some ( & mut genkey[ genkey_base..] ) ) ?;
297
+ wrappedkey. truncate ( wrappedkey_base + wrappedkey_len) ;
298
+ genkey. truncate ( genkey_base + genkey_len) ;
299
+ Ok ( ( wrappedkey_len, genkey_len) )
300
+ }
301
+
253
302
/// Verifies the signature of data using the public key.
254
303
///
255
304
/// Returns `Ok(true)` if the signature is valid, `Ok(false)` if the signature is invalid, and `Err` if an error
@@ -329,6 +378,17 @@ where
329
378
Ok ( ( ) )
330
379
}
331
380
381
+ /// Prepares the context for decapsulation using the private key.
382
+ #[ corresponds( EVP_PKEY_decapsulate_init ) ]
383
+ #[ inline]
384
+ pub fn decapsulate_init ( & mut self ) -> Result < ( ) , ErrorStack > {
385
+ unsafe {
386
+ cvt ( ffi:: EVP_PKEY_decapsulate_init ( self . as_ptr ( ) , ptr:: null ( ) ) ) ?;
387
+ }
388
+
389
+ Ok ( ( ) )
390
+ }
391
+
332
392
/// Prepares the context for signing using the private key.
333
393
#[ corresponds( EVP_PKEY_sign_init ) ]
334
394
#[ inline]
@@ -384,6 +444,37 @@ where
384
444
Ok ( len)
385
445
}
386
446
447
+ /// Performs a decapsulation operation using the private key.
448
+ #[ corresponds( EVP_PKEY_decapsulate ) ]
449
+ pub fn decapsulate ( & mut self , from : & [ u8 ] , to : Option < & mut [ u8 ] > )
450
+ -> Result < usize , ErrorStack >
451
+ {
452
+ let mut written = to. as_ref ( ) . map_or ( 0 , |b| b. len ( ) ) ;
453
+ unsafe {
454
+ cvt ( ffi:: EVP_PKEY_decapsulate (
455
+ self . as_ptr ( ) ,
456
+ to. map_or ( ptr:: null_mut ( ) , |b| b. as_mut_ptr ( ) ) ,
457
+ & mut written,
458
+ from. as_ptr ( ) ,
459
+ from. len ( ) ,
460
+ ) ) ?;
461
+ }
462
+
463
+ Ok ( written)
464
+ }
465
+
466
+ /// Like [`Self::decapsulate`] but appends plaintext to a [`Vec`].
467
+ pub fn decapsulate_to_vec ( & mut self , from : & [ u8 ] , out : & mut Vec < u8 > )
468
+ -> Result < usize , ErrorStack >
469
+ {
470
+ let base = out. len ( ) ;
471
+ let len = self . decapsulate ( from, None ) ?;
472
+ out. resize ( base + len, 0 ) ;
473
+ let len = self . decapsulate ( from, Some ( & mut out[ base..] ) ) ?;
474
+ out. truncate ( base + len) ;
475
+ Ok ( len)
476
+ }
477
+
387
478
/// Signs the contents of `data`.
388
479
///
389
480
/// If `sig` is set to `None`, an upper bound on the number of bytes required for the output buffer will be
0 commit comments