Skip to content

Commit c0579fd

Browse files
committed
New type for AKPub hash
1 parent e185741 commit c0579fd

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

vm/devices/tpm/src/lib.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ const RSA_2K_MODULUS_BITS: u16 = 2048;
9090
const RSA_2K_MODULUS_SIZE: usize = (RSA_2K_MODULUS_BITS / 8) as usize;
9191
const RSA_2K_EXPONENT_SIZE: usize = 3;
9292

93+
const SHA_256_OUTPUT_SIZE_BYTES: usize = 32;
94+
9395
const TPM_RSA_SRK_HANDLE: ReservedHandle = ReservedHandle::new(TPM20_HT_PERSISTENT, 0x01);
9496
const TPM_AZURE_AIK_HANDLE: ReservedHandle = ReservedHandle::new(TPM20_HT_PERSISTENT, 0x03);
9597
const TPM_GUEST_SECRET_HANDLE: ReservedHandle = ReservedHandle::new(TPM20_HT_PERSISTENT, 0x04);
@@ -260,7 +262,7 @@ pub struct Tpm {
260262

261263
// For logging
262264
bios_guid: Guid,
263-
ak_pub_hash: String,
265+
ak_pub_hash: [u8; SHA_256_OUTPUT_SIZE_BYTES],
264266

265267
// Runtime glue
266268
rt: TpmRuntime,
@@ -441,7 +443,7 @@ impl Tpm {
441443
mmio_region,
442444
allow_ak_cert_renewal: false,
443445
bios_guid,
444-
ak_pub_hash: "".into(),
446+
ak_pub_hash: [0; SHA_256_OUTPUT_SIZE_BYTES],
445447

446448
rt: TpmRuntime {
447449
mem,
@@ -630,15 +632,14 @@ impl Tpm {
630632
let mut ak_pub_hasher = Sha256::new();
631633
ak_pub_hasher.update(ak_pub.exponent);
632634
ak_pub_hasher.update(ak_pub.modulus);
633-
let ak_pub_hash = ak_pub_hasher.finalize();
634-
self.ak_pub_hash = base64::engine::general_purpose::STANDARD.encode(ak_pub_hash);
635+
self.ak_pub_hash = ak_pub_hasher.finalize().into();
635636

636637
tracing::info!(
637638
CVM_ALLOWED,
638639
op_type = ?LogOpType::VtpmKeysProvision,
639640
key_type = ?KeyType::AkPub,
640641
bios_guid = %self.bios_guid,
641-
pub_key = self.ak_pub_hash,
642+
pub_key = self.ak_pub_str(),
642643
success = true,
643644
latency = std::time::SystemTime::now()
644645
.duration_since(start_time)
@@ -1048,7 +1049,7 @@ impl Tpm {
10481049
CVM_ALLOWED,
10491050
op_type = ?LogOpType::BeginAkCertProvision,
10501051
is_renew,
1051-
pub_key = self.ak_pub_hash,
1052+
pub_key = self.ak_pub_str(),
10521053
bios_guid = %self.bios_guid,
10531054
"Request AK cert renewal"
10541055
);
@@ -1111,7 +1112,7 @@ impl Tpm {
11111112
CVM_ALLOWED,
11121113
op_type = ?LogOpType::AkCertProvision,
11131114
bios_guid = %self.bios_guid,
1114-
pub_key = self.ak_pub_hash,
1115+
pub_key = self.ak_pub_str(),
11151116
is_renew,
11161117
got_cert = 0,
11171118
latency = latency.map_or(0, |d| d.as_millis()),
@@ -1132,7 +1133,7 @@ impl Tpm {
11321133
CVM_ALLOWED,
11331134
op_type = ?LogOpType::AkCertProvision,
11341135
bios_guid = %self.bios_guid,
1135-
pub_key = self.ak_pub_hash,
1136+
pub_key = self.ak_pub_str(),
11361137
is_renew,
11371138
got_cert = 0,
11381139
latency = latency.map_or(0, |d| d.as_millis()),
@@ -1168,7 +1169,7 @@ impl Tpm {
11681169
CVM_ALLOWED,
11691170
op_type = ?LogOpType::AkCertProvision,
11701171
bios_guid = %self.bios_guid,
1171-
pub_key = self.ak_pub_hash,
1172+
pub_key = self.ak_pub_str(),
11721173
is_renew,
11731174
got_cert = 1,
11741175
size = response.len(),
@@ -1261,6 +1262,10 @@ impl Tpm {
12611262
}
12621263
}
12631264
}
1265+
1266+
fn ak_pub_str(&self) -> String {
1267+
base64::engine::general_purpose::STANDARD.encode(self.ak_pub_hash)
1268+
}
12641269
}
12651270

12661271
impl ChangeDeviceState for Tpm {

0 commit comments

Comments
 (0)