Skip to content

Commit 990652a

Browse files
committed
common: Move Symmkey to library as crypto::symmkey
Move the code for the shared type SymmKey out of the common.rs file into the keylime library. Signed-off-by: Anderson Toshiyuki Sasaki <[email protected]>
1 parent 5bad73d commit 990652a

File tree

5 files changed

+129
-56
lines changed

5 files changed

+129
-56
lines changed

keylime-agent/src/common.rs

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use keylime::algorithms::{
1010
EncryptionAlgorithm, HashAlgorithm, SignAlgorithm,
1111
};
1212
use keylime::{
13-
crypto::{hash, tss_pubkey_to_pem, AES_128_KEY_LEN, AES_256_KEY_LEN},
13+
crypto::{hash, tss_pubkey_to_pem},
1414
tpm,
1515
};
1616
use log::*;
@@ -83,55 +83,6 @@ where
8383
}
8484
}
8585

86-
// a vector holding keys
87-
pub type KeySet = Vec<SymmKey>;
88-
89-
// a key of len AES_128_KEY_LEN or AES_256_KEY_LEN
90-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
91-
pub struct SymmKey {
92-
bytes: Vec<u8>,
93-
}
94-
95-
impl SymmKey {
96-
pub(crate) fn xor(&self, other: &Self) -> Result<Self> {
97-
let my_bytes = self.as_ref();
98-
let other_bytes = other.as_ref();
99-
if my_bytes.len() != other_bytes.len() {
100-
return Err(Error::Other(
101-
"cannot xor differing length slices".to_string(),
102-
));
103-
}
104-
let mut outbuf = vec![0u8; my_bytes.len()];
105-
for (out, (x, y)) in
106-
outbuf.iter_mut().zip(my_bytes.iter().zip(other_bytes))
107-
{
108-
*out = x ^ y;
109-
}
110-
Ok(Self { bytes: outbuf })
111-
}
112-
}
113-
114-
impl AsRef<[u8]> for SymmKey {
115-
fn as_ref(&self) -> &[u8] {
116-
self.bytes.as_slice()
117-
}
118-
}
119-
120-
impl TryFrom<&[u8]> for SymmKey {
121-
type Error = String;
122-
123-
fn try_from(v: &[u8]) -> std::result::Result<Self, Self::Error> {
124-
match v.len() {
125-
AES_128_KEY_LEN | AES_256_KEY_LEN => {
126-
Ok(SymmKey { bytes: v.to_vec() })
127-
}
128-
other => Err(format!(
129-
"key length {other} does not correspond to valid GCM cipher",
130-
)),
131-
}
132-
}
133-
}
134-
13586
#[derive(Debug, Clone, Serialize, Deserialize)]
13687
pub struct AuthTag {
13788
bytes: Vec<u8>,

keylime-agent/src/keys_handler.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// Copyright 2021 Keylime Authors
33

4-
use crate::crypto;
54
use crate::{
6-
common::{
7-
AuthTag, EncryptedData, JsonWrapper, KeySet, SymmKey,
8-
},
5+
common::{AuthTag, EncryptedData, JsonWrapper},
96
config::KeylimeConfig,
107
payloads::{Payload, PayloadMessage},
118
Error, QuoteData, Result,
129
};
1310
use actix_web::{http, web, HttpRequest, HttpResponse, Responder};
1411
use base64::{engine::general_purpose, Engine as _};
12+
use keylime::crypto::{
13+
self,
14+
symmkey::{KeySet, SymmKey},
15+
};
1516
use log::*;
1617
use serde::{Deserialize, Serialize};
1718
use serde_json::json;

keylime-agent/src/payloads.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
// Copyright 2021 Keylime Authors
33

44
use crate::{
5-
common::{EncryptedData, SymmKey},
6-
config, crypto,
5+
common::EncryptedData,
6+
config,
77
revocation::{Revocation, RevocationMessage},
88
Error, Result,
99
};
1010

1111
#[cfg(feature = "with-zmq")]
1212
use crate::revocation::ZmqMessage;
1313

14+
use keylime::crypto::{
15+
self,
16+
symmkey::{KeySet, SymmKey},
17+
};
1418
use log::*;
1519
use serde::{Deserialize, Serialize};
1620
use serde_json::json;

keylime/src/crypto.rs

Lines changed: 1 addition & 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 symmkey;
45
pub mod x509;
56

67
use base64::{engine::general_purpose, Engine as _};

keylime/src/crypto/symmkey.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright 2025 Keylime Authors
3+
4+
use crate::crypto::{AES_128_KEY_LEN, AES_256_KEY_LEN};
5+
use serde::{Deserialize, Serialize};
6+
use thiserror::Error;
7+
8+
#[derive(Debug, Error)]
9+
pub enum SymmKeyError {
10+
// Invalid key size for AES
11+
#[error("invalid AES key size: {0}")]
12+
InvalidKeySize(usize),
13+
14+
// Incompatible sizes for XOR
15+
#[error("cannot XOR slices of different sizes")]
16+
XorIncompatibleSizes,
17+
}
18+
19+
// a vector holding keys
20+
pub type KeySet = Vec<SymmKey>;
21+
22+
// a key of len AES_128_KEY_LEN or AES_256_KEY_LEN
23+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
24+
pub struct SymmKey {
25+
bytes: Vec<u8>,
26+
}
27+
28+
impl SymmKey {
29+
pub fn xor(&self, other: &Self) -> Result<Self, SymmKeyError> {
30+
let my_bytes = self.as_ref();
31+
let other_bytes = other.as_ref();
32+
if my_bytes.len() != other_bytes.len() {
33+
return Err(SymmKeyError::XorIncompatibleSizes);
34+
}
35+
let mut outbuf = vec![0u8; my_bytes.len()];
36+
for (out, (x, y)) in
37+
outbuf.iter_mut().zip(my_bytes.iter().zip(other_bytes))
38+
{
39+
*out = x ^ y;
40+
}
41+
Ok(Self { bytes: outbuf })
42+
}
43+
}
44+
45+
impl AsRef<[u8]> for SymmKey {
46+
fn as_ref(&self) -> &[u8] {
47+
self.bytes.as_slice()
48+
}
49+
}
50+
51+
impl TryFrom<&[u8]> for SymmKey {
52+
type Error = SymmKeyError;
53+
54+
fn try_from(v: &[u8]) -> std::result::Result<Self, SymmKeyError> {
55+
match v.len() {
56+
AES_128_KEY_LEN | AES_256_KEY_LEN => {
57+
Ok(SymmKey { bytes: v.to_vec() })
58+
}
59+
other => Err(SymmKeyError::InvalidKeySize(other)),
60+
}
61+
}
62+
}
63+
64+
#[cfg(test)]
65+
mod tests {
66+
use super::*;
67+
68+
#[test]
69+
fn test_convert() {
70+
let a_128: [u8; AES_128_KEY_LEN] = [0; AES_128_KEY_LEN];
71+
let a_256: [u8; AES_256_KEY_LEN] = [0; AES_256_KEY_LEN];
72+
let a_unknown: [u8; 127] = [0; 127];
73+
74+
let r_128 = SymmKey::try_from(a_128.as_ref());
75+
assert!(r_128.is_ok());
76+
77+
let r_256 = SymmKey::try_from(a_256.as_ref());
78+
assert!(r_256.is_ok());
79+
80+
let r_unknown = SymmKey::try_from(a_unknown.as_ref());
81+
assert!(r_unknown.is_err());
82+
}
83+
84+
#[test]
85+
fn test_xor() {
86+
// Input for 128 bits keys
87+
let a: [u8; AES_128_KEY_LEN] = [0xA0; AES_128_KEY_LEN];
88+
let b: [u8; AES_128_KEY_LEN] = [0x0A; AES_128_KEY_LEN];
89+
let axb: [u8; AES_128_KEY_LEN] = [0xAA; AES_128_KEY_LEN];
90+
let r_128 =
91+
SymmKey::try_from(axb.as_ref()).expect("failed to convert");
92+
93+
// Input for 256 bits keys
94+
let c: [u8; AES_256_KEY_LEN] = [0xA0; AES_256_KEY_LEN];
95+
let d: [u8; AES_256_KEY_LEN] = [0x0A; AES_256_KEY_LEN];
96+
let cxd: [u8; AES_256_KEY_LEN] = [0xAA; AES_256_KEY_LEN];
97+
let r_256 =
98+
SymmKey::try_from(cxd.as_ref()).expect("failed to convert");
99+
100+
// Test for each set of inputs
101+
for (i, j, expected) in [
102+
(a.as_ref(), b.as_ref(), &r_128),
103+
(c.as_ref(), d.as_ref(), &r_256),
104+
] {
105+
let k_i =
106+
SymmKey::try_from(i).expect("failed to get key from slice");
107+
let k_j =
108+
SymmKey::try_from(j).expect("failed to get key from slice");
109+
let result = k_i.xor(&k_j);
110+
assert!(result.is_ok());
111+
112+
let out = result.expect("xor failed");
113+
assert_eq!(&out, expected);
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)