Skip to content

Commit cb2cbea

Browse files
committed
common: Move AuthTag from common to the library
Move the AuthTag structure from the common.rs file to the Keylime library in crypto::auth_tag Signed-off-by: Anderson Toshiyuki Sasaki <[email protected]>
1 parent 5bcce7e commit cb2cbea

File tree

4 files changed

+56
-32
lines changed

4 files changed

+56
-32
lines changed

keylime-agent/src/common.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ use tss_esapi::{
3333
structures::PcrSlot, traits::UnMarshall, utils::TpmsContext,
3434
};
3535

36-
/*
37-
* Constants and static variables
38-
*/
39-
pub const AUTH_TAG_LEN: usize = 48;
40-
4136
#[derive(Serialize, Deserialize, Debug)]
4237
pub(crate) struct APIVersion {
4338
major: u32,
@@ -83,32 +78,6 @@ where
8378
}
8479
}
8580

86-
#[derive(Debug, Clone, Serialize, Deserialize)]
87-
pub struct AuthTag {
88-
bytes: Vec<u8>,
89-
}
90-
91-
impl AsRef<[u8]> for AuthTag {
92-
fn as_ref(&self) -> &[u8] {
93-
self.bytes.as_slice()
94-
}
95-
}
96-
97-
impl TryFrom<&[u8]> for AuthTag {
98-
type Error = String;
99-
100-
fn try_from(v: &[u8]) -> std::result::Result<Self, Self::Error> {
101-
match v.len() {
102-
AUTH_TAG_LEN => {
103-
Ok(AuthTag { bytes: v.to_vec() })
104-
}
105-
other => Err(format!(
106-
"auth tag length {other} does not correspond to valid SHA-384 HMAC",
107-
)),
108-
}
109-
}
110-
}
111-
11281
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
11382
pub struct EncryptedData {
11483
bytes: Vec<u8>,

keylime-agent/src/keys_handler.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Copyright 2021 Keylime Authors
33

44
use crate::{
5-
common::{AuthTag, EncryptedData, JsonWrapper},
5+
common::{EncryptedData, JsonWrapper},
66
config::KeylimeConfig,
77
payloads::{Payload, PayloadMessage},
88
Error, QuoteData, Result,
@@ -12,6 +12,7 @@ use base64::{engine::general_purpose, Engine as _};
1212
use keylime::crypto::{
1313
self,
1414
symmkey::{KeySet, SymmKey},
15+
auth_tag::AuthTag,
1516
};
1617
use log::*;
1718
use serde::{Deserialize, Serialize};

keylime/src/crypto.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// Copyright 2021 Keylime Authors
33

4+
pub mod auth_tag;
45
pub mod symmkey;
56
pub mod x509;
67

@@ -34,6 +35,7 @@ use thiserror::Error;
3435
pub const AES_128_KEY_LEN: usize = 16;
3536
pub const AES_256_KEY_LEN: usize = 32;
3637
pub const AES_BLOCK_SIZE: usize = 16;
38+
pub const AUTH_TAG_LEN: usize = 48;
3739

3840
#[derive(Error, Debug)]
3941
pub enum CryptoError {

keylime/src/crypto/auth_tag.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright 2025 Keylime Authors
3+
4+
use crate::crypto::AUTH_TAG_LEN;
5+
use serde::{Deserialize, Serialize};
6+
use thiserror::Error;
7+
8+
#[derive(Debug, Error)]
9+
pub enum AuthTagError {
10+
// Invalid authentication tag size
11+
#[error("auth tag length {0} does not correspond to valid SHA-384 HMAC")]
12+
InvalidAuthTagSize(usize),
13+
}
14+
15+
#[derive(Debug, Clone, Serialize, Deserialize)]
16+
pub struct AuthTag {
17+
bytes: Vec<u8>,
18+
}
19+
20+
impl AsRef<[u8]> for AuthTag {
21+
fn as_ref(&self) -> &[u8] {
22+
self.bytes.as_slice()
23+
}
24+
}
25+
26+
impl TryFrom<&[u8]> for AuthTag {
27+
type Error = AuthTagError;
28+
29+
fn try_from(v: &[u8]) -> std::result::Result<Self, Self::Error> {
30+
match v.len() {
31+
AUTH_TAG_LEN => Ok(AuthTag { bytes: v.to_vec() }),
32+
_ => Err(AuthTagError::InvalidAuthTagSize(v.len())),
33+
}
34+
}
35+
}
36+
37+
#[cfg(test)]
38+
mod tests {
39+
use super::*;
40+
41+
#[test]
42+
fn test_convert() {
43+
let a: [u8; AUTH_TAG_LEN] = [0xAA; AUTH_TAG_LEN];
44+
let invalid: [u8; 32] = [0xBB; 32];
45+
46+
let r = AuthTag::try_from(a.as_ref());
47+
assert!(r.is_ok());
48+
49+
let r = AuthTag::try_from(invalid.as_ref());
50+
assert!(r.is_err());
51+
}
52+
}

0 commit comments

Comments
 (0)