Skip to content

Comments

[KeyManager] Implement FFI for KEM and binding key generation#645

Merged
NilanjanDaw merged 9 commits intogoogle:mainfrom
NilanjanDaw:add-generatekem-ffi
Feb 14, 2026
Merged

[KeyManager] Implement FFI for KEM and binding key generation#645
NilanjanDaw merged 9 commits intogoogle:mainfrom
NilanjanDaw:add-generatekem-ffi

Conversation

@NilanjanDaw
Copy link
Collaborator

@NilanjanDaw NilanjanDaw commented Feb 6, 2026

Introduces FFI-accessible functions for generating and managing cryptographic keys within the Key Protection Service (KPS) and Workload Service (WS).
Key Changes:

  • FFI Compatibility: Configured prost_build in km_common to apply #[repr(C)] to the HpkeAlgorithm struct, enabling its use in C-style interfaces.
  • Key Generation Logic: Added create_key_record in km_common to centralize secure keypair generation (X25519), ensuring private keys are immediately moved to secure Vault storage and zeroized from temporary memory.
  • Key Management: Implemented KeyRegistry and a global registry instance in both KPS and WS to track generated keys by UUID.
  • FFI Exports:
    • Added key_manager_generate_kem_keypair to KPS for generating KEM keys with associated binding public keys.
    • Added key_manager_generate_binding_keypair to WS for generating standard binding keys.
  • Testing: Added comprehensive unit tests for FFI functions, including error handling for invalid algorithms and null pointers.

This PR is built on top of PR #643 [KeyManager] Add HPKE and DHKEM crypto primitives.
Please review commit #38731051 onwards

Comment on lines 78 to 80
if out_pubkey.is_null() || out_pubkey_len.is_null() || out_uuid.is_null() {
return -1;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Seems like all the parameter validation could be moved to the very top, I think that would make it easier to read.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, it's good to isolate the unsafe code as much as possible so it can be easy to see where the unsafe review should be focused. This check is all safe code so it would be good to remove it from the unsafe block.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack, thank you, this is a good advice. Updated the function to separate the safety checks and the business logic, and also tried to reduce the scope of the unsafe blocks.

Comment on lines 49 to 57
pub unsafe extern "C" fn key_manager_generate_kem_keypair(
algo: HpkeAlgorithm,
binding_pubkey: *const u8,
binding_pubkey_len: usize,
expiry_secs: u64,
out_uuid: *mut u8,
out_pubkey: *mut u8,
out_pubkey_len: *mut usize,
) -> i32 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now this function is mixing business logic & FFI safety invariants.

I recommend checking safety invariants (null ptr, buffer size, etc) in this function, converting it into safe types, and calling a safe function that operates on the sanitized data.

That will reduce the surface area of this FFI boundary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, make sense, updated the function to separate the safety checks and the business logic, and also tried to reduce the scope of the unsafe blocks.

Comment on lines 37 to 43
pub unsafe extern "C" fn key_manager_generate_binding_keypair(
algo: HpkeAlgorithm,
expiry_secs: u64,
out_uuid: *mut u8,
out_pubkey: *mut u8,
out_pubkey_len: *mut usize,
) -> i32 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment on mixing business & validation logic

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, make sense, updated the function to separate the safety checks and the business logic, and also tried to reduce the scope of the unsafe blocks.

Comment on lines 78 to 80
if out_pubkey.is_null() || out_pubkey_len.is_null() || out_uuid.is_null() {
return -1;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, it's good to isolate the unsafe code as much as possible so it can be easy to see where the unsafe review should be focused. This check is all safe code so it would be good to remove it from the unsafe block.

Copy link
Collaborator

@bluegate010 bluegate010 Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This same file shows up as new in #643. Similar to other core files. Makes it a bit hard to keep track of which feedback has been given.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My apologies. The PRs are stacked which is making the diff difficult to review. The parent PR has been merged. So this one should now be easier to review.

NilanjanDaw and others added 5 commits February 13, 2026 10:19
Introduces FFI-accessible functions for generating and managing
cryptographic keys within the Key Protection Service (KPS) and
Workload Service (WS).

Key Changes:
- FFI Compatibility: Configured prost_build in km_common to apply
  #[repr(C)] to the HpkeAlgorithm struct, enabling its use in C-style
  interfaces.
- Key Generation Logic: Added create_key_record in km_common to
  centralize secure keypair generation (X25519), ensuring private keys
  are immediately moved to secure Vault storage and zeroized from
  temporary memory.
- Key Management: Implemented KeyRegistry and a global registry instance
  in both KPS and WS to track generated keys by UUID.
- FFI Exports:
    - Added key_manager_generate_kem_keypair to KPS for generating
      KEM keys with associated binding public keys.
    - Added key_manager_generate_binding_keypair to WS for generating
      standard binding keys.
- Testing: Added comprehensive unit tests for FFI functions, including
  error handling for invalid algorithms and null pointers.
Extend both key_manager_generate_binding_keypair (WSD KCC) and
key_manager_generate_kem_keypair (KPS KCC) to also output the public
key via out_pubkey/out_pubkey_len parameters. This enables the Go
orchestration layer to pass the binding public key from WSD to KPS
when generating KEM keypairs.

Changes:
- Rust FFI: add out_pubkey and out_pubkey_len params to both functions
- Updated Rust tests for new signatures
- Updated FFI layers in key_protection_service and workload_service to use PublicKey::try_from and as_bytes().
- Refactored create_key_record to use PrivateKey::into_secret() for secure transition to protected memory.
- Added into_secret() to PrivateKey and X25519PrivateKey to facilitate secure ownership transfer of key material.
- Cleaned up unused zeroize imports and duplicate tests in km_common.
- Made X25519PublicKey internal field pub(crate) for internal module compatibility.
/// X25519-based public key implementation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct X25519PublicKey([u8; 32]);
pub struct X25519PublicKey(pub(crate) [u8; 32]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, the spec calls for sanitizing the public key in 7.1.2.

This is already being done by BoringSSL, correct?

@NilanjanDaw NilanjanDaw merged commit e08f358 into google:main Feb 14, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants