Skip to content

Commit e282423

Browse files
committed
crypto: Add specific type for every possible error
Also move crypto-related definitions from common.rs to crypto.rs. Adjust the code to handle the new error types. Add few auxiliary functions and tests for them. Signed-off-by: Anderson Toshiyuki Sasaki <[email protected]>
1 parent abf36c7 commit e282423

File tree

8 files changed

+822
-205
lines changed

8 files changed

+822
-205
lines changed

keylime-agent/src/common.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// Copyright 2021 Keylime Authors
33

4-
use crate::error::{Error, Result};
5-
use crate::permissions;
4+
use crate::{
5+
crypto::{AES_128_KEY_LEN, AES_256_KEY_LEN},
6+
error::{Error, Result},
7+
permissions,
8+
};
9+
610
use keylime::algorithms::{
711
EncryptionAlgorithm, HashAlgorithm, SignAlgorithm,
812
};
@@ -42,9 +46,6 @@ pub static RSA_PUBLICKEY_EXPORTABLE: &str = "rsa placeholder";
4246
pub static KEY: &str = "secret";
4347
pub const AGENT_UUID_LEN: usize = 36;
4448
pub const AUTH_TAG_LEN: usize = 48;
45-
pub const AES_128_KEY_LEN: usize = 16;
46-
pub const AES_256_KEY_LEN: usize = 32;
47-
pub const AES_BLOCK_SIZE: usize = 16;
4849

4950
#[derive(Serialize, Deserialize, Debug)]
5051
pub(crate) struct APIVersion {
@@ -253,14 +254,9 @@ impl AgentData {
253254
///
254255
/// This is used as the agent UUID when the configuration option 'uuid' is set as 'hash_ek'
255256
pub(crate) fn hash_ek_pubkey(ek_pub: Public) -> Result<String> {
256-
// Converting Public TPM key to PEM
257-
let key = SubjectPublicKeyInfo::try_from(ek_pub)?;
258-
let key_der = picky_asn1_der::to_vec(&key)?;
259-
let openssl_key = PKey::public_key_from_der(&key_der)?;
260-
let pem = openssl_key.public_key_to_pem()?;
261-
262257
// Calculate the SHA-256 hash of the public key in PEM format
263-
let mut hash = hash(MessageDigest::sha256(), &pem)?;
258+
let pem = crate::crypto::tss_pubkey_to_pem(ek_pub)?;
259+
let hash = crate::crypto::hash(&pem, MessageDigest::sha256())?;
264260
Ok(hex::encode(hash))
265261
}
266262

0 commit comments

Comments
 (0)