Skip to content

Commit 6340ad8

Browse files
authored
No panic is AES encyption (#1302)
* return defaults when error * add comments * add a bit comment
1 parent ca40222 commit 6340ad8

File tree

1 file changed

+14
-8
lines changed
  • tee-worker/litentry/primitives/src

1 file changed

+14
-8
lines changed

tee-worker/litentry/primitives/src/lib.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
#[cfg(all(not(feature = "std"), feature = "sgx"))]
1919
extern crate sgx_tstd as std;
2020

21-
mod ethereum_signature;
22-
mod identity;
23-
// mod trusted_call;
2421
mod assertion;
2522
mod enclave_quote;
23+
mod ethereum_signature;
24+
mod identity;
2625
mod validation_data;
2726

2827
pub use ethereum_signature::*;
@@ -48,24 +47,31 @@ pub mod sgx_reexport_prelude {
4847

4948
use rand::Rng;
5049

51-
// pub use trusted_call::*;
5250
pub use assertion::*;
5351
pub use enclave_quote::*;
5452
pub use validation_data::*;
5553

5654
pub const CHALLENGE_CODE_SIZE: usize = 16;
5755
pub type ChallengeCode = [u8; CHALLENGE_CODE_SIZE];
5856

57+
// Returns the default if any error happens
58+
// We don't propagate the error to upper level as this function is used in too many places,
59+
// it's too verbose to handle them all and pass back to the parentchain as events.
60+
// We rely on the parentchain event consumers to handle them correctly (and they kind of
61+
// have to, because they'll find all fields are 0)
5962
pub fn aes_encrypt_default(key: &UserShieldingKeyType, data: &[u8]) -> AesOutput {
6063
let mut in_out = data.to_vec();
6164

6265
let nonce = RingAeadNonceSequence::new();
6366
let aad = b"";
64-
let unbound_key = UnboundKey::new(&AES_256_GCM, key.as_slice()).unwrap();
65-
let mut sealing_key = SealingKey::new(unbound_key, nonce.clone());
66-
sealing_key.seal_in_place_append_tag(Aad::from(aad), &mut in_out).unwrap();
67+
if let Ok(unbound_key) = UnboundKey::new(&AES_256_GCM, key.as_slice()) {
68+
let mut sealing_key = SealingKey::new(unbound_key, nonce.clone());
69+
if sealing_key.seal_in_place_append_tag(Aad::from(aad), &mut in_out).is_ok() {
70+
return AesOutput { ciphertext: in_out.to_vec(), aad: aad.to_vec(), nonce: nonce.nonce }
71+
}
72+
}
6773

68-
AesOutput { ciphertext: in_out.to_vec(), aad: aad.to_vec(), nonce: nonce.nonce }
74+
AesOutput::default()
6975
}
7076

7177
#[derive(Clone)]

0 commit comments

Comments
 (0)