-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathrom_env.rs
More file actions
177 lines (141 loc) · 4.83 KB
/
Copy pathrom_env.rs
File metadata and controls
177 lines (141 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*++
Licensed under the Apache-2.0 license.
File Name:
rom_env.rs
Abstract:
File implements a context holding all the services utilized by ROM.
The primary need for this abstraction is to hide the hardware details
from the ROM flows. The natural side benefit of this abstraction is it
makes authoring mocks and unit tests easy.
--*/
use caliptra_drivers::{
Abr, AesGcm, DeobfuscationEngine, Dma, Ecc384, Hmac, KeyVault, Lms, Mailbox, PcrBank,
PersistentDataAccessor, Sha1, Sha256, Sha2_512_384, Sha2_512_384Acc, Sha3, SocIfc, Trng,
};
use caliptra_error::CaliptraResult;
use caliptra_registers::{
abr::AbrReg, aes::AesReg, aes_clp::AesClpReg, csrng::CsrngReg, doe::DoeReg, ecc::EccReg,
entropy_src::EntropySrcReg, hmac::HmacReg, kmac::Kmac as KmacReg, kv::KvReg, mbox::MboxCsr,
pv::PvReg, sha256::Sha256Reg, sha512::Sha512Reg, sha512_acc::Sha512AccCsr, soc_ifc::SocIfcReg,
soc_ifc_trng::SocIfcTrngReg,
};
/// Non-Crypto ROM Context
/// Contains only the non-cryptographic drivers and those needed before KAT execution
pub struct RomEnv {
/// Deobfuscation engine
pub doe: DeobfuscationEngine,
// SHA2-256 Engine
pub sha256: Sha256,
// SHA2-512/384 Engine
pub sha2_512_384: Sha2_512_384,
// SHA2-512/384 Accelerator
pub sha2_512_384_acc: Sha2_512_384Acc,
// SHA3/SHAKE
pub sha3: Sha3,
/// Hmac Engine
pub hmac: Hmac,
/// Ecc384 Engine
pub ecc384: Ecc384,
/// LMS Engine
pub lms: Lms,
/// Key Vault
pub key_vault: KeyVault,
/// SoC interface
pub soc_ifc: SocIfc,
/// Mailbox
pub mbox: Mailbox,
/// PCR Bank
pub pcr_bank: PcrBank,
/// Cryptographically Secure Random Number Generator
pub trng: Trng,
/// Mechanism to access the persistent data safely
pub persistent_data: PersistentDataAccessor,
/// ABR Engine (ML-DSA)
pub abr: Abr,
/// Dma engine
pub dma: Dma,
/// AES-GCM engine
pub aes_gcm: AesGcm,
}
impl RomEnv {
/// Create TRNG early for CFI initialization.
/// This must be called before `new_from_registers` so CFI can be initialized first.
pub unsafe fn create_trng() -> CaliptraResult<Trng> {
Trng::new(
CsrngReg::new(),
EntropySrcReg::new(),
SocIfcTrngReg::new(),
&SocIfcReg::new(),
PersistentDataAccessor::new(),
)
}
/// Create the ROM environment. CFI must be initialized before calling this.
/// Takes ownership of the pre-created TRNG.
pub unsafe fn new_from_registers(mut trng: Trng) -> CaliptraResult<Self> {
// Create SocIfc early so we can start the WDT before running KATs
let mut soc_ifc = SocIfc::new(SocIfcReg::new());
// Start the Watchdog Timer before running KATs
crate::wdt::start_wdt(&mut soc_ifc);
let aes_gcm = AesGcm::new(AesReg::new(), AesClpReg::new(), &mut trng)?;
Ok(Self {
doe: DeobfuscationEngine::new(DoeReg::new()),
sha256: Sha256::new(Sha256Reg::new()),
sha2_512_384: Sha2_512_384::new(Sha512Reg::new()),
sha2_512_384_acc: Sha2_512_384Acc::new(Sha512AccCsr::new()),
sha3: Sha3::new(KmacReg::new()),
hmac: Hmac::new(HmacReg::new()),
ecc384: Ecc384::new(EccReg::new()),
lms: Lms::default(),
key_vault: KeyVault::new(KvReg::new()),
soc_ifc,
mbox: Mailbox::new(MboxCsr::new()),
pcr_bank: PcrBank::new(PvReg::new()),
trng,
persistent_data: PersistentDataAccessor::new(),
abr: Abr::new(AbrReg::new()),
dma: Dma::default(),
aes_gcm,
})
}
}
/// Full ROM Context
/// Contains all drivers needed after KAT execution
pub struct RomEnvFips {
/// Non-crypto environment (embedded)
pub non_crypto: RomEnv,
// SHA1 Engine (initialized by KATs)
pub sha1: Sha1,
}
impl RomEnvFips {
/// Create RomEnvFips from non-crypto environment and initialized drivers
pub fn from_non_crypto(
non_crypto: RomEnv,
initialized: caliptra_kat::InitializedDrivers,
) -> Self {
Self {
non_crypto,
sha1: initialized.sha1,
}
}
/// Get a mutable reference to the non-crypto environment
pub fn non_crypto_mut(&mut self) -> &mut RomEnv {
&mut self.non_crypto
}
/// Get an immutable reference to the non-crypto environment
#[allow(dead_code)]
pub fn non_crypto(&self) -> &RomEnv {
&self.non_crypto
}
}
// Provide transparent access to non-crypto fields via Deref
impl core::ops::Deref for RomEnvFips {
type Target = RomEnv;
fn deref(&self) -> &Self::Target {
&self.non_crypto
}
}
impl core::ops::DerefMut for RomEnvFips {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.non_crypto
}
}