Skip to content

Commit 7fea15e

Browse files
committed
some more simplifications
1 parent cb4fdbd commit 7fea15e

File tree

7 files changed

+43
-213
lines changed

7 files changed

+43
-213
lines changed

keymanager/key_protection_service/key_custody_core/kps_key_custody_core_cgo.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,10 @@ func EnumerateKEMKeys(limit, offset int) ([]KEMKeyInfo, bool, error) {
125125
// DestroyKEMKey destroys the KEM key identified by kemUUID via Rust FFI.
126126
func DestroyKEMKey(kemUUID uuid.UUID) error {
127127
uuidBytes := kemUUID[:]
128-
if rc := C.key_manager_destroy_kem_key(
128+
rc := C.key_manager_destroy_kem_key(
129129
(*C.uint8_t)(unsafe.Pointer(&uuidBytes[0])),
130-
); keymanager.Status(rc) != keymanager.Status_STATUS_SUCCESS {
131-
return keymanager.Status(rc).ToStatus()
132-
}
133-
return nil
130+
)
131+
return keymanager.Status(rc).ToStatus()
134132
}
135133

136134
// GetKEMKey retrieves KEM and binding public keys, HpkeAlgorithm and remaining lifespan via Rust FFI.

keymanager/key_protection_service/key_custody_core/src/lib.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use km_common::crypto::PublicKey;
22
use km_common::key_types::{KeyRecord, KeyRegistry, KeySpec};
3-
use km_common::proto::{Status, HpkeAlgorithm};
3+
use km_common::proto::{HpkeAlgorithm, Status};
44
use km_common::{MAX_ALGORITHM_LEN, MAX_PUBLIC_KEY_LEN};
55

66
use prost::Message;
@@ -23,21 +23,16 @@ fn generate_kem_keypair_internal(
2323
binding_pubkey: PublicKey,
2424
expiry_secs: u64,
2525
) -> Result<(uuid::Uuid, PublicKey), Status> {
26-
let result =
27-
KeyRecord::create_bound_kem_key(algo, binding_pubkey, Duration::from_secs(expiry_secs));
28-
29-
match result {
30-
Ok(record) => {
31-
let id = record.meta.id;
32-
let pubkey = match &record.meta.spec {
33-
KeySpec::KemWithBindingPub { kem_public_key, .. } => kem_public_key.clone(),
34-
_ => return Err(Status::InternalError),
35-
};
36-
KEY_REGISTRY.add_key(record);
37-
Ok((id, pubkey))
38-
}
39-
Err(e) => Err(Status::from(e)),
40-
}
26+
let record =
27+
KeyRecord::create_bound_kem_key(algo, binding_pubkey, Duration::from_secs(expiry_secs))?;
28+
29+
let id = record.meta.id;
30+
let pubkey = match &record.meta.spec {
31+
KeySpec::KemWithBindingPub { kem_public_key, .. } => kem_public_key.clone(),
32+
_ => return Err(Status::InternalError),
33+
};
34+
KEY_REGISTRY.add_key(record);
35+
Ok((id, pubkey))
4136
}
4237

4338
/// Generates a new KEM keypair associated with a binding public key.

keymanager/km_common/proto/algorithms.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ message HpkeAlgorithm {
3232
// Specifies parameters for a particular algorithm type.
3333
message AlgorithmParams {
3434
oneof params {
35-
// KEM algorithm identifier (e.g., DHKEM_X25519_HKDF_SHA256).
35+
// KEM algorithm identifier (e.g., KEM_ALGORITHM_DHKEM_X25519_HKDF_SHA256).
3636
KemAlgorithm kem_id = 1;
3737
}
3838
}

keymanager/km_common/proto/status.proto

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ package keymanager;
55
option go_package = "github.com/google/go-tpm-tools/keymanager/km_common/proto;keymanager";
66

77
enum Status {
8-
STATUS_UNSPECIFIED = 0;
9-
STATUS_SUCCESS = 1;
10-
STATUS_INTERNAL_ERROR = 2;
11-
STATUS_INVALID_ARGUMENT = 3;
12-
STATUS_NOT_FOUND = 4;
13-
STATUS_ALREADY_EXISTS = 5;
14-
STATUS_PERMISSION_DENIED = 6;
15-
STATUS_UNAUTHENTICATED = 7;
8+
STATUS_UNSPECIFIED = 0;
9+
STATUS_SUCCESS = 1;
10+
STATUS_INTERNAL_ERROR = 2;
11+
STATUS_INVALID_ARGUMENT = 3;
12+
STATUS_NOT_FOUND = 4;
13+
STATUS_ALREADY_EXISTS = 5;
14+
STATUS_PERMISSION_DENIED = 6;
15+
STATUS_UNAUTHENTICATED = 7;
1616
STATUS_UNSUPPORTED_ALGORITHM = 8;
17-
STATUS_INVALID_KEY = 9;
18-
STATUS_CRYPTO_ERROR = 10;
19-
STATUS_DECRYPTION_FAILURE = 11;
20-
STATUS_ENCRYPTION_FAILURE = 12;
17+
STATUS_INVALID_KEY = 9;
18+
STATUS_CRYPTO_ERROR = 10;
19+
STATUS_DECRYPTION_FAILURE = 11;
20+
STATUS_ENCRYPTION_FAILURE = 12;
2121
STATUS_DECAPSULATION_FAILURE = 13;
2222
}

keymanager/km_common/src/keymanager.rs

Lines changed: 1 addition & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -9,160 +9,6 @@ pub struct HpkeAlgorithm {
99
#[prost(enumeration = "AeadAlgorithm", tag = "3")]
1010
pub aead: i32,
1111
}
12-
/// Key Encapsulation Mechanism (KEM)
13-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
14-
#[repr(i32)]
15-
pub enum KemAlgorithm {
16-
Unspecified = 0,
17-
DhkemX25519HkdfSha256 = 1,
18-
}
19-
impl KemAlgorithm {
20-
/// String value of the enum field names used in the ProtoBuf definition.
21-
///
22-
/// The values are not transformed in any way and thus are considered stable
23-
/// (if the ProtoBuf definition does not change) and safe for programmatic use.
24-
pub fn as_str_name(&self) -> &'static str {
25-
match self {
26-
Self::Unspecified => "KEM_ALGORITHM_UNSPECIFIED",
27-
Self::DhkemX25519HkdfSha256 => "KEM_ALGORITHM_DHKEM_X25519_HKDF_SHA256",
28-
}
29-
}
30-
/// Creates an enum from field names used in the ProtoBuf definition.
31-
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
32-
match value {
33-
"KEM_ALGORITHM_UNSPECIFIED" => Some(Self::Unspecified),
34-
"KEM_ALGORITHM_DHKEM_X25519_HKDF_SHA256" => Some(Self::DhkemX25519HkdfSha256),
35-
_ => None,
36-
}
37-
}
38-
}
39-
/// Key Derivation Function (KDF)
40-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
41-
#[repr(i32)]
42-
pub enum KdfAlgorithm {
43-
Unspecified = 0,
44-
HkdfSha256 = 1,
45-
}
46-
impl KdfAlgorithm {
47-
/// String value of the enum field names used in the ProtoBuf definition.
48-
///
49-
/// The values are not transformed in any way and thus are considered stable
50-
/// (if the ProtoBuf definition does not change) and safe for programmatic use.
51-
pub fn as_str_name(&self) -> &'static str {
52-
match self {
53-
Self::Unspecified => "KDF_ALGORITHM_UNSPECIFIED",
54-
Self::HkdfSha256 => "KDF_ALGORITHM_HKDF_SHA256",
55-
}
56-
}
57-
/// Creates an enum from field names used in the ProtoBuf definition.
58-
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
59-
match value {
60-
"KDF_ALGORITHM_UNSPECIFIED" => Some(Self::Unspecified),
61-
"KDF_ALGORITHM_HKDF_SHA256" => Some(Self::HkdfSha256),
62-
_ => None,
63-
}
64-
}
65-
}
66-
/// Authenticated Encryption with Associated Data (AEAD)
67-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
68-
#[repr(i32)]
69-
pub enum AeadAlgorithm {
70-
Unspecified = 0,
71-
Aes256Gcm = 1,
72-
}
73-
impl AeadAlgorithm {
74-
/// String value of the enum field names used in the ProtoBuf definition.
75-
///
76-
/// The values are not transformed in any way and thus are considered stable
77-
/// (if the ProtoBuf definition does not change) and safe for programmatic use.
78-
pub fn as_str_name(&self) -> &'static str {
79-
match self {
80-
Self::Unspecified => "AEAD_ALGORITHM_UNSPECIFIED",
81-
Self::Aes256Gcm => "AEAD_ALGORITHM_AES_256_GCM",
82-
}
83-
}
84-
/// Creates an enum from field names used in the ProtoBuf definition.
85-
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
86-
match value {
87-
"AEAD_ALGORITHM_UNSPECIFIED" => Some(Self::Unspecified),
88-
"AEAD_ALGORITHM_AES_256_GCM" => Some(Self::Aes256Gcm),
89-
_ => None,
90-
}
91-
}
92-
}
93-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
94-
#[repr(i32)]
95-
pub enum Status {
96-
Unspecified = 0,
97-
Success = 1,
98-
InternalError = 2,
99-
InvalidArgument = 3,
100-
NotFound = 4,
101-
AlreadyExists = 5,
102-
PermissionDenied = 6,
103-
Unauthenticated = 7,
104-
UnsupportedAlgorithm = 8,
105-
InvalidKey = 9,
106-
CryptoError = 10,
107-
DecryptionFailure = 11,
108-
EncryptionFailure = 12,
109-
DecapsulationFailure = 13,
110-
}
111-
impl Status {
112-
/// String value of the enum field names used in the ProtoBuf definition.
113-
///
114-
/// The values are not transformed in any way and thus are considered stable
115-
/// (if the ProtoBuf definition does not change) and safe for programmatic use.
116-
pub fn as_str_name(&self) -> &'static str {
117-
match self {
118-
Self::Unspecified => "STATUS_UNSPECIFIED",
119-
Self::Success => "STATUS_SUCCESS",
120-
Self::InternalError => "STATUS_INTERNAL_ERROR",
121-
Self::InvalidArgument => "STATUS_INVALID_ARGUMENT",
122-
Self::NotFound => "STATUS_NOT_FOUND",
123-
Self::AlreadyExists => "STATUS_ALREADY_EXISTS",
124-
Self::PermissionDenied => "STATUS_PERMISSION_DENIED",
125-
Self::Unauthenticated => "STATUS_UNAUTHENTICATED",
126-
Self::UnsupportedAlgorithm => "STATUS_UNSUPPORTED_ALGORITHM",
127-
Self::InvalidKey => "STATUS_INVALID_KEY",
128-
Self::CryptoError => "STATUS_CRYPTO_ERROR",
129-
Self::DecryptionFailure => "STATUS_DECRYPTION_FAILURE",
130-
Self::EncryptionFailure => "STATUS_ENCRYPTION_FAILURE",
131-
Self::DecapsulationFailure => "STATUS_DECAPSULATION_FAILURE",
132-
}
133-
}
134-
/// Creates an enum from field names used in the ProtoBuf definition.
135-
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
136-
match value {
137-
"STATUS_UNSPECIFIED" => Some(Self::Unspecified),
138-
"STATUS_SUCCESS" => Some(Self::Success),
139-
"STATUS_INTERNAL_ERROR" => Some(Self::InternalError),
140-
"STATUS_INVALID_ARGUMENT" => Some(Self::InvalidArgument),
141-
"STATUS_NOT_FOUND" => Some(Self::NotFound),
142-
"STATUS_ALREADY_EXISTS" => Some(Self::AlreadyExists),
143-
"STATUS_PERMISSION_DENIED" => Some(Self::PermissionDenied),
144-
"STATUS_UNAUTHENTICATED" => Some(Self::Unauthenticated),
145-
"STATUS_UNSUPPORTED_ALGORITHM" => Some(Self::UnsupportedAlgorithm),
146-
"STATUS_INVALID_KEY" => Some(Self::InvalidKey),
147-
"STATUS_CRYPTO_ERROR" => Some(Self::CryptoError),
148-
"STATUS_DECRYPTION_FAILURE" => Some(Self::DecryptionFailure),
149-
"STATUS_ENCRYPTION_FAILURE" => Some(Self::EncryptionFailure),
150-
"STATUS_DECAPSULATION_FAILURE" => Some(Self::DecapsulationFailure),
151-
_ => None,
152-
}
153-
}
154-
}
155-
// This file is @generated by prost-build.
156-
/// Composite HPKE Algorithm suite
157-
#[derive(Clone, Copy, PartialEq, ::prost::Message)]
158-
pub struct HpkeAlgorithm {
159-
#[prost(enumeration = "KemAlgorithm", tag = "1")]
160-
pub kem: i32,
161-
#[prost(enumeration = "KdfAlgorithm", tag = "2")]
162-
pub kdf: i32,
163-
#[prost(enumeration = "AeadAlgorithm", tag = "3")]
164-
pub aead: i32,
165-
}
16612
/// Specifies parameters for a particular algorithm type.
16713
#[derive(Clone, Copy, PartialEq, ::prost::Message)]
16814
pub struct AlgorithmParams {
@@ -173,7 +19,7 @@ pub struct AlgorithmParams {
17319
pub mod algorithm_params {
17420
#[derive(Clone, Copy, PartialEq, ::prost::Oneof)]
17521
pub enum Params {
176-
/// KEM algorithm identifier (e.g., DHKEM_X25519_HKDF_SHA256).
22+
/// KEM algorithm identifier (e.g., KEM_ALGORITHM_DHKEM_X25519_HKDF_SHA256).
17723
#[prost(enumeration = "super::KemAlgorithm", tag = "1")]
17824
KemId(i32),
17925
}

keymanager/workload_service/key_custody_core/src/lib.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use km_common::crypto::PublicKey;
22
use km_common::crypto::secret_box::SecretBox;
33
use km_common::key_types::{KeyRecord, KeyRegistry, KeySpec};
4-
use km_common::proto::{Status, HpkeAlgorithm};
4+
use km_common::proto::{HpkeAlgorithm, Status};
55
use km_common::{MAX_ALGORITHM_LEN, MAX_PUBLIC_KEY_LEN};
66
use prost::Message;
77
use std::slice;
@@ -22,22 +22,17 @@ fn generate_binding_keypair_internal(
2222
algo: HpkeAlgorithm,
2323
expiry_secs: u64,
2424
) -> Result<(uuid::Uuid, PublicKey), Status> {
25-
let result = KeyRecord::create_binding_key(algo, Duration::from_secs(expiry_secs));
26-
27-
match result {
28-
Ok(record) => {
29-
let id = record.meta.id;
30-
let pubkey = match &record.meta.spec {
31-
KeySpec::Binding {
32-
binding_public_key, ..
33-
} => binding_public_key.clone(),
34-
_ => return Err(Status::InternalError),
35-
};
36-
KEY_REGISTRY.add_key(record);
37-
Ok((id, pubkey))
38-
}
39-
Err(e) => Err(Status::from(e)),
40-
}
25+
let record = KeyRecord::create_binding_key(algo, Duration::from_secs(expiry_secs))?;
26+
27+
let id = record.meta.id;
28+
let pubkey = match &record.meta.spec {
29+
KeySpec::Binding {
30+
binding_public_key, ..
31+
} => binding_public_key.clone(),
32+
_ => return Err(Status::InternalError),
33+
};
34+
KEY_REGISTRY.add_key(record);
35+
Ok((id, pubkey))
4136
}
4237

4338
/// Generates a new binding HPKE keypair.

keymanager/workload_service/key_custody_core/ws_key_custody_core_cgo.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ func DestroyBindingKey(bindingUUID uuid.UUID) error {
6969
rc := C.key_manager_destroy_binding_key(
7070
(*C.uint8_t)(unsafe.Pointer(&uuidBytes[0])),
7171
)
72-
if keymanager.Status(rc) != keymanager.Status_STATUS_SUCCESS {
73-
return keymanager.Status(rc).ToStatus()
74-
}
75-
return nil
72+
return keymanager.Status(rc).ToStatus()
7673
}
7774

7875
// Open decrypts a sealed ciphertext using the binding key identified by
@@ -130,14 +127,13 @@ func GetBindingKey(id uuid.UUID) ([]byte, *keymanager.HpkeAlgorithm, error) {
130127
var algoBuf [C.MAX_ALGORITHM_LEN]byte
131128
algoLenC := C.size_t(len(algoBuf))
132129

133-
rc := C.key_manager_get_binding_key(
130+
if rc := C.key_manager_get_binding_key(
134131
(*C.uint8_t)(unsafe.Pointer(&uuidBytes[0])),
135132
(*C.uint8_t)(unsafe.Pointer(&pubkeyBuf[0])),
136133
pubkeyLen,
137134
(*C.uint8_t)(unsafe.Pointer(&algoBuf[0])),
138135
&algoLenC,
139-
)
140-
if keymanager.Status(rc) != keymanager.Status_STATUS_SUCCESS {
136+
); keymanager.Status(rc) != keymanager.Status_STATUS_SUCCESS {
141137
return nil, nil, keymanager.Status(rc).ToStatus()
142138
}
143139

0 commit comments

Comments
 (0)