Skip to content

Commit 72247c8

Browse files
committed
sha256: just hash salts at compile time
1 parent 7cc52d3 commit 72247c8

File tree

4 files changed

+10
-41
lines changed

4 files changed

+10
-41
lines changed

common/src/enclave/sgx.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,12 @@ use secrecy::zeroize::Zeroizing;
1111
use sgx_isa::{AttributesFlags, Keyname, Keypolicy};
1212

1313
use crate::enclave::{Error, MachineId, Measurement, Sealed, MIN_SGX_CPUSVN};
14-
use crate::hex;
1514
use crate::rng::Crng;
15+
use crate::sha256;
1616

17-
#[cfg(test)]
18-
const HKDF_SALT_STR: &[u8] = b"LEXE-HASH-REALM::SgxSealing";
19-
20-
/// We salt the HKDF for domain separation purposes. The raw bytes here are
21-
/// equal to the hash value: `SHA-256(b"LEXE-HASH-REALM::SgxSealing")`.
22-
const HKDF_SALT: [u8; 32] = hex::decode_const(
23-
b"66331e89a9282101072c8879263a948ca8e48ef22c6f18eccf11d91864b3911a",
24-
);
17+
/// We salt the HKDF for domain separation purposes.
18+
const HKDF_SALT: [u8; 32] =
19+
sha256::digest_const(b"LEXE-HASH-REALM::SgxSealing").into_inner();
2520

2621
/// AES-256-GCM tag length
2722
pub const TAG_LEN: usize = 16;
@@ -269,11 +264,9 @@ pub fn machine_id() -> MachineId {
269264
#[cfg(test)]
270265
mod test {
271266
use super::*;
272-
use crate::sha256;
273267

274268
#[test]
275269
fn test_constants() {
276270
assert_eq!(AES_256_GCM.tag_len(), TAG_LEN);
277-
assert_eq!(sha256::digest(HKDF_SALT_STR).as_ref(), HKDF_SALT);
278271
}
279272
}

common/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub mod task;
3636
/// two values.
3737
#[macro_export]
3838
macro_rules! const_assert_usize_eq {
39-
($x:expr, $y:expr $(,)*) => {
39+
($x:expr, $y:expr $(,)?) => {
4040
const _: [(); $x] = [(); $y];
4141
};
4242
}

common/src/root_seed.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use secrecy::{ExposeSecret, Secret, SecretVec};
1010
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
1111

1212
use crate::rng::Crng;
13-
use crate::{ed25519, hex};
13+
use crate::{ed25519, hex, sha256};
1414

1515
/// The user's root seed from which we derive all child secrets.
1616
pub struct RootSeed(Secret<[u8; Self::LENGTH]>);
@@ -22,15 +22,9 @@ impl RootSeed {
2222
/// single secret.
2323
const HKDF_MAX_OUT_LEN: usize = 8160 /* 255*32 */;
2424

25-
/// The HKDF domain separation value as a human-readable byte string.
26-
#[cfg(test)]
27-
const HKDF_SALT_STR: &'static [u8] = b"LEXE-HASH-REALM::RootSeed";
28-
29-
/// We salt the HKDF for domain separation purposes. The raw bytes here are
30-
/// equal to the hash value: `SHA-256(b"LEXE-HASH-REALM::RootSeed")`.
31-
const HKDF_SALT: [u8; 32] = hex::decode_const(
32-
b"363b116be1690fcd481f2d4014812aaecff2411b861198eec42c6e31d80a28a4",
33-
);
25+
/// We salt the HKDF for domain separation purposes.
26+
const HKDF_SALT: [u8; 32] =
27+
sha256::digest_const(b"LEXE-HASH-REALM::RootSeed").into_inner();
3428

3529
pub fn new(bytes: Secret<[u8; Self::LENGTH]>) -> Self {
3630
Self(bytes)
@@ -350,24 +344,6 @@ mod test {
350344
assert_eq!(foo2.y, "asdf");
351345
}
352346

353-
#[test]
354-
fn test_root_seed_hkdf_salt() {
355-
let actual = RootSeed::HKDF_SALT.as_slice();
356-
let expected = sha256::digest(RootSeed::HKDF_SALT_STR);
357-
358-
// // print out salt
359-
// let hex = hex::encode(expected.as_ref());
360-
// let (chunks, _) = hex.as_bytes().as_chunks::<2>();
361-
// for &[hi, lo] in chunks {
362-
// let hi = hi as char;
363-
// let lo = lo as char;
364-
// println!("0x{hi}{lo},");
365-
// }
366-
367-
// compare hex encode for easier debugging
368-
assert_eq!(hex::encode(actual), hex::encode(expected.as_ref()));
369-
}
370-
371347
#[test]
372348
fn test_root_seed_derive() {
373349
let seed = RootSeed::new(Secret::new([0x42; 32]));

common/src/sha256.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub const fn digest_const(input: &[u8]) -> Hash {
3838
digest_many_const(&[input])
3939
}
4040

41-
/// SHA-256 digest a multiple concatenated inputs at compile time.
41+
/// SHA-256 digest multiple concatenated inputs at compile time.
4242
pub const fn digest_many_const(mut inputs: &[&[u8]]) -> Hash {
4343
let mut acc = sha2_const::Sha256::new();
4444
while let Some((input, rest)) = inputs.split_first() {

0 commit comments

Comments
 (0)