Skip to content

Commit 036d7f6

Browse files
committed
resolve review comments
1 parent 1104d79 commit 036d7f6

File tree

6 files changed

+120
-101
lines changed

6 files changed

+120
-101
lines changed

keymanager/Cargo.toml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,22 @@ members = [
44
"workload_service/key_custody_core",
55
"key_protection_service/key_custody_core",
66
"km_common",
7-
]
7+
]
8+
9+
[workspace.package]
10+
version = "0.1.0"
11+
edition = "2024"
12+
13+
[workspace.dependencies]
14+
km_common = { path = "km_common" }
15+
uuid = { version = "1.20.0", features = ["v4"] }
16+
zeroize = "1.8.2"
17+
prost = "0.13"
18+
libc = "0.2.180"
19+
memmap2 = "0.9.9"
20+
thiserror = "2.0"
21+
clear_on_drop = "0.2"
22+
bssl-crypto = { path = "third_party/bssl-crypto" }
23+
hex = "0.4"
24+
prost-build = "0.13"
25+
protoc-bin-vendored = "3.2.0"
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
[package]
22
name = "kps_key_custody_core"
3-
version = "0.1.0"
4-
edition = "2024"
3+
version.workspace = true
4+
edition.workspace = true
55

66
[lib]
77
crate-type = ["staticlib", "rlib"]
88

99
[dependencies]
10-
km_common = { path = "../../km_common" }
11-
uuid = { version = "1.20.0", features = ["v4"] }
12-
zeroize = "1.8"
13-
prost = "0.13"
10+
km_common.workspace = true
11+
uuid.workspace = true
12+
zeroize.workspace = true
13+
prost.workspace = true
1414

1515
[dev-dependencies]
16-
km_common = { path = "../../km_common", features = ["test-utils"] }
16+
km_common = { workspace = true, features = ["test-utils"] }

keymanager/key_protection_service/key_custody_core/src/lib.rs

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,41 @@ pub unsafe extern "C" fn key_manager_destroy_kem_key(uuid_bytes: *const u8) -> i
152152
.unwrap_or(-1)
153153
}
154154

155+
/// Internal function to decapsulate and reseal a shared secret.
156+
fn decap_and_seal_internal(
157+
uuid: Uuid,
158+
encapsulated_key: &[u8],
159+
aad: &[u8],
160+
) -> Result<(Vec<u8>, Vec<u8>), i32> {
161+
// Get key record from registry
162+
let Some(key_record) = KEY_REGISTRY.get_key(&uuid) else {
163+
Err(-1)? // Key not found
164+
};
165+
166+
let KeySpec::KemWithBindingPub {
167+
algo: hpke_algo,
168+
binding_public_key,
169+
..
170+
} = &key_record.meta.spec
171+
else {
172+
Err(-1)? // Invalid key type
173+
};
174+
175+
let priv_key = key_record.get_private_key();
176+
177+
// Decapsulate
178+
let shared_secret = match km_common::crypto::decaps(&priv_key, encapsulated_key) {
179+
Ok(s) => s,
180+
Err(_) => return Err(-3),
181+
};
182+
183+
// Seal
184+
match km_common::crypto::hpke_seal(binding_public_key, &shared_secret, aad, hpke_algo) {
185+
Ok((enc, ct)) => Ok((enc.to_vec(), ct.to_vec())),
186+
Err(_) => Err(-4),
187+
}
188+
}
189+
155190
/// Decapsulates a shared secret using a stored KEM key and immediately reseals it using the associated binding public key.
156191
///
157192
/// ## Arguments
@@ -161,10 +196,10 @@ pub unsafe extern "C" fn key_manager_destroy_kem_key(uuid_bytes: *const u8) -> i
161196
/// * `aad` - A pointer to the Additional Authenticated Data (AAD) for the sealing operation.
162197
/// * `aad_len` - The length of the AAD.
163198
/// * `out_encapsulated_key` - A pointer to a buffer where the new encapsulated key will be written.
164-
/// * `out_encapsulated_key_len` - A pointer to a `usize` containing the size of `out_encapsulated_key`.
199+
/// * `out_encapsulated_key_len` - The size of `out_encapsulated_key`.
165200
/// On success, updated with the actual size.
166201
/// * `out_ciphertext` - A pointer to a buffer where the sealed ciphertext will be written.
167-
/// * `out_ciphertext_len` - A pointer to a `usize` containing the size of `out_ciphertext`.
202+
/// * `out_ciphertext_len` -The size of `out_ciphertext`.
168203
/// On success, updated with the actual size.
169204
///
170205
/// ## Safety
@@ -208,7 +243,7 @@ pub unsafe extern "C" fn key_manager_decap_and_seal(
208243
}
209244

210245
// Convert to Safe Types
211-
let uuid = unsafe { std::slice::from_raw_parts(uuid_bytes, 16) };
246+
let uuid_slice = unsafe { slice::from_raw_parts(uuid_bytes, 16) };
212247
let enc_key_slice =
213248
unsafe { slice::from_raw_parts(encapsulated_key, encapsulated_key_len) };
214249
let aad_slice = if !aad.is_null() && aad_len > 0 {
@@ -218,65 +253,26 @@ pub unsafe extern "C" fn key_manager_decap_and_seal(
218253
};
219254
let out_encapsulated_key_slice =
220255
unsafe { slice::from_raw_parts_mut(out_encapsulated_key, out_encapsulated_key_len) };
221-
let out_ciphertext =
256+
let out_ciphertext_slice =
222257
unsafe { slice::from_raw_parts_mut(out_ciphertext, out_ciphertext_len) };
223258

224259
let uuid = match Uuid::from_slice(uuid_slice) {
225260
Ok(u) => u,
226261
Err(_) => return -1,
227262
};
228263

229-
// Get key record from registry
230-
let key_record = match KEY_REGISTRY.get_key(&uuid_val) {
231-
Some(record) => record,
232-
None => return -1,
233-
};
234-
235-
let (hpke_algo, binding_public_key) = match &key_record.meta.spec {
236-
KeySpec::KemWithBindingPub {
237-
algo,
238-
binding_public_key,
239-
..
240-
} => (algo, binding_public_key),
241-
_ => return -1, // Wrong key type
242-
};
243-
244-
let _kem_algo = match km_common::algorithms::KemAlgorithm::try_from(hpke_algo.kem) {
245-
Ok(k) => k,
246-
Err(_) => return -1, // Invalid KEM algorithm
247-
};
248-
249-
let priv_key = key_record.get_private_key();
250-
251-
// Decapsulate
252-
let shared_secret = match km_common::crypto::decaps(&priv_key, enc_key_slice) {
253-
Ok(s) => s,
254-
Err(_) => return -3,
255-
};
256-
257-
// Seal
258-
let (enc_key, sealed_ciphertext) = match km_common::crypto::hpke_seal(
259-
binding_public_key,
260-
&shared_secret,
261-
aad_slice,
262-
hpke_algo,
263-
) {
264-
Ok(res) => res,
265-
Err(_) => {
266-
return -4;
264+
// Call Safe Internal Function
265+
match decap_and_seal_internal(uuid, enc_key_slice, aad_slice) {
266+
Ok((enc, ct)) => {
267+
if out_encapsulated_key_len != enc.len() || out_ciphertext_len != ct.len() {
268+
return -2;
269+
}
270+
out_encapsulated_key_slice.copy_from_slice(&enc);
271+
out_ciphertext_slice.copy_from_slice(&ct);
272+
0 // Success
267273
}
268-
};
269-
270-
// Copy outputs
271-
let enc_len_req = enc_key.len();
272-
let ct_len_req = sealed_ciphertext.len();
273-
274-
if out_encapsulated_key_len != enc_len_req || out_ciphertext_len != ct_len_req {
275-
return -2;
274+
Err(e) => e,
276275
}
277-
out_encapsulated_key.copy_from_slice(enc_key.as_slice());
278-
out_ciphertext.copy_from_slice(sealed_ciphertext.as_slice());
279-
0
280276
}))
281277
.unwrap_or(-1)
282278
}
@@ -462,10 +458,11 @@ mod tests {
462458
kdf: KdfAlgorithm::HkdfSha256 as i32,
463459
aead: AeadAlgorithm::Aes256Gcm as i32,
464460
};
465-
461+
let algo_bytes = algo.encode_to_vec();
466462
unsafe {
467463
let res = key_manager_generate_kem_keypair(
468-
algo,
464+
algo_bytes.as_ptr(),
465+
algo_bytes.len(),
469466
binding_pubkey.as_ptr(),
470467
binding_pubkey.len(),
471468
3600,
@@ -513,10 +510,11 @@ mod tests {
513510
kdf: KdfAlgorithm::HkdfSha256 as i32,
514511
aead: AeadAlgorithm::Aes256Gcm as i32,
515512
};
516-
513+
let algo_bytes = algo.encode_to_vec();
517514
unsafe {
518515
key_manager_generate_kem_keypair(
519-
algo,
516+
algo_bytes.as_ptr(),
517+
algo_bytes.len(),
520518
binding_pk.as_bytes().as_ptr(),
521519
binding_pk.as_bytes().len(),
522520
3600,
@@ -632,9 +630,11 @@ mod tests {
632630
kdf: KdfAlgorithm::HkdfSha256 as i32,
633631
aead: AeadAlgorithm::Aes256Gcm as i32,
634632
};
633+
let algo_bytes = algo.encode_to_vec();
635634
let res = unsafe {
636635
key_manager_generate_kem_keypair(
637-
algo,
636+
algo_bytes.as_ptr(),
637+
algo_bytes.len(),
638638
binding_pk.as_bytes().as_ptr(),
639639
binding_pk.as_bytes().len(),
640640
3600,
@@ -683,9 +683,12 @@ mod tests {
683683
kdf: KdfAlgorithm::HkdfSha256 as i32,
684684
aead: AeadAlgorithm::Aes256Gcm as i32,
685685
};
686+
687+
let algo_bytes = algo.encode_to_vec();
686688
let res = unsafe {
687689
key_manager_generate_kem_keypair(
688-
algo,
690+
algo_bytes.as_ptr(),
691+
algo_bytes.len(),
689692
binding_pk.as_bytes().as_ptr(),
690693
binding_pk.as_bytes().len(),
691694
3600,

keymanager/km_common/Cargo.toml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
[package]
22
name = "km_common"
3-
version = "0.1.0"
4-
edition = "2024"
3+
version.workspace = true
4+
edition.workspace = true
55

66
[dependencies]
7-
libc = "0.2.180"
8-
memmap2 = "0.9.9"
9-
prost = "0.13"
10-
uuid = { version = "1.20.0", features = ["v4", "serde"] }
11-
zeroize = { version = "1.8.2", features = ["derive"] }
12-
thiserror = "2.0"
13-
bssl-crypto = { path = "../third_party/bssl-crypto" }
14-
clear_on_drop = "0.2"
7+
libc.workspace = true
8+
memmap2.workspace = true
9+
prost.workspace = true
10+
uuid = { workspace = true, features = ["serde"] }
11+
zeroize = { workspace = true, features = ["derive"] }
12+
thiserror.workspace = true
13+
bssl-crypto.workspace = true
14+
clear_on_drop.workspace = true
1515

1616
[features]
1717
test-utils = []
1818

1919
[dev-dependencies]
20-
hex = "0.4"
20+
hex.workspace = true
2121

2222
[build-dependencies]
23-
prost-build = "0.13"
24-
protoc-bin-vendored = "3.2.0"
23+
prost-build.workspace = true
24+
protoc-bin-vendored.workspace = true
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[package]
22
name = "ws_key_custody_core"
3-
version = "0.1.0"
4-
edition = "2024"
3+
version.workspace = true
4+
edition.workspace = true
55

66
[lib]
77
crate-type = ["staticlib", "rlib"]
88

99
[dependencies]
10-
km_common = { path = "../../km_common" }
11-
uuid = { version = "1.20.0", features = ["v4"] }
12-
zeroize = "1.8"
13-
prost = "0.13"
10+
km_common.workspace = true
11+
uuid.workspace = true
12+
zeroize.workspace = true
13+
prost.workspace = true

keymanager/workload_service/key_custody_core/src/lib.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use km_common::algorithms::HpkeAlgorithm;
2-
use km_common::crypto::secret_box::SecretBox;
32
use km_common::crypto::PublicKey;
3+
use km_common::crypto::secret_box::SecretBox;
44
use km_common::key_types::{KeyRecord, KeyRegistry, KeySpec};
55
use prost::Message;
66
use std::slice;
@@ -62,7 +62,6 @@ fn generate_binding_keypair_internal(
6262
/// * `0` on success.
6363
/// * `-1` if an error occurred during key generation.
6464
/// * `-2` if the `out_pubkey` buffer size does not match the key size.
65-
6665
#[unsafe(no_mangle)]
6766
pub unsafe extern "C" fn key_manager_generate_binding_keypair(
6867
algo_ptr: *const u8,
@@ -134,15 +133,9 @@ pub unsafe extern "C" fn key_manager_destroy_binding_key(uuid_bytes: *const u8)
134133
}
135134

136135
/// Internal function to decrypt a ciphertext using a stored binding key.
137-
fn open_internal(
138-
uuid: Uuid,
139-
enc: &[u8],
140-
ciphertext: &[u8],
141-
aad: &[u8],
142-
) -> Result<SecretBox, i32> {
143-
let record = match KEY_REGISTRY.get_key(&uuid) {
144-
Some(r) => r,
145-
None => return Err(-1),
136+
fn open_internal(uuid: Uuid, enc: &[u8], ciphertext: &[u8], aad: &[u8]) -> Result<SecretBox, i32> {
137+
let Some(record) = KEY_REGISTRY.get_key(&uuid) else {
138+
Err(-1)? // Key not found
146139
};
147140

148141
let algo = match &record.meta.spec {
@@ -376,10 +369,11 @@ mod tests {
376369
kdf: KdfAlgorithm::HkdfSha256 as i32,
377370
aead: AeadAlgorithm::Aes256Gcm as i32,
378371
};
379-
372+
let algo_bytes = algo.encode_to_vec();
380373
unsafe {
381374
let res = key_manager_generate_binding_keypair(
382-
algo,
375+
algo_bytes.as_ptr(),
376+
algo_bytes.len(),
383377
3600,
384378
uuid_bytes.as_mut_ptr(),
385379
pubkey_bytes.as_mut_ptr(),
@@ -421,9 +415,11 @@ mod tests {
421415
};
422416

423417
// 1. Generate keypair
418+
let algo_bytes = algo.encode_to_vec();
424419
unsafe {
425420
key_manager_generate_binding_keypair(
426-
algo,
421+
algo_bytes.as_ptr(),
422+
algo_bytes.len(),
427423
3600,
428424
uuid_bytes.as_mut_ptr(),
429425
pubkey_bytes.as_mut_ptr(),
@@ -490,9 +486,11 @@ mod tests {
490486
kdf: KdfAlgorithm::HkdfSha256 as i32,
491487
aead: AeadAlgorithm::Aes256Gcm as i32,
492488
};
489+
let algo_bytes = algo.encode_to_vec();
493490
let res = unsafe {
494491
key_manager_generate_binding_keypair(
495-
algo,
492+
algo_bytes.as_ptr(),
493+
algo_bytes.len(),
496494
3600,
497495
uuid_bytes.as_mut_ptr(),
498496
pubkey_bytes.as_mut_ptr(),

0 commit comments

Comments
 (0)