Skip to content

Commit 727e1e5

Browse files
authored
Update the OPTEE shim to use CrngProvider (#504)
The OPTEE shim should use the new provider to generate random bytes--I missed this in my previous PR. As part of this, fix the shim's handling of large RNG requests to match OPTEE OS (failing above 4096 bytes). Also, add the fake RNG implementation to the LVBS kernel, to be replaced later.
1 parent 5cb4ee1 commit 727e1e5

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

dev_tests/src/ratchet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn ratchet_globals() -> Result<()> {
2929
("litebox/", 7),
3030
("litebox_platform_linux_kernel/", 5),
3131
("litebox_platform_linux_userland/", 6),
32-
("litebox_platform_lvbs/", 18),
32+
("litebox_platform_lvbs/", 19),
3333
("litebox_platform_multiplex/", 1),
3434
("litebox_platform_windows_userland/", 8),
3535
("litebox_runner_linux_userland/", 1),

litebox_platform_lvbs/src/host/lvbs_impl.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ unsafe impl litebox::platform::ThreadLocalStorageProvider for LvbsLinuxKernel {
8080
}
8181
}
8282

83+
impl litebox::platform::CrngProvider for LvbsLinuxKernel {
84+
fn fill_bytes_crng(&self, buf: &mut [u8]) {
85+
// FIXME: generate real random data.
86+
static RANDOM: spin::mutex::SpinMutex<litebox::utils::rng::FastRng> =
87+
spin::mutex::SpinMutex::new(litebox::utils::rng::FastRng::new_from_seed(
88+
core::num::NonZeroU64::new(0x4d595df4d0f33173).unwrap(),
89+
));
90+
let mut random = RANDOM.lock();
91+
for b in buf.chunks_mut(8) {
92+
b.copy_from_slice(&random.next_u64().to_ne_bytes()[..b.len()]);
93+
}
94+
}
95+
}
96+
8397
pub struct HostLvbsInterface;
8498

8599
impl HostLvbsInterface {}

litebox_shim_optee/src/lib.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,18 @@ fn handle_syscall_request(ctx: &mut litebox_common_linux::PtRegs) -> ContinueOpe
287287
syscalls::cryp::sys_cryp_obj_copy(dst_obj, src_obj)
288288
}
289289
SyscallRequest::CrypRandomNumberGenerate { buf, blen } => {
290-
let mut kernel_buf = vec![0u8; blen.min(MAX_KERNEL_BUF_SIZE)];
291-
syscalls::cryp::sys_cryp_random_number_generate(&mut kernel_buf).and_then(|()| {
292-
buf.copy_from_slice(0, &kernel_buf)
293-
.ok_or(TeeResult::ShortBuffer)
294-
})
290+
// This could take a long time for large sizes. But OP-TEE OS limits
291+
// the maximum size of random data generation to 4096 bytes, so
292+
// let's do the same rather than something more complicated.
293+
if blen > 4096 {
294+
Err(TeeResult::OutOfMemory)
295+
} else {
296+
let mut kernel_buf = vec![0u8; blen];
297+
syscalls::cryp::sys_cryp_random_number_generate(&mut kernel_buf).and_then(|()| {
298+
buf.copy_from_slice(0, &kernel_buf)
299+
.ok_or(TeeResult::AccessDenied)
300+
})
301+
}
295302
}
296303
_ => todo!(),
297304
};

litebox_shim_optee/src/syscalls/cryp.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use aes::{
66
cipher::{NewCipher, StreamCipher, generic_array::GenericArray},
77
};
88
use ctr::Ctr128BE;
9-
use litebox::{platform::RawMutPointer, utils::rng::FastRng};
9+
use litebox::platform::RawMutPointer;
1010
use litebox_common_optee::{
1111
TeeAlgorithm, TeeAlgorithmClass, TeeCrypStateHandle, TeeObjHandle, TeeObjectInfo,
1212
TeeObjectType, TeeOperationMode, TeeResult, UteeAttribute,
@@ -312,24 +312,12 @@ pub(crate) fn sys_cryp_obj_copy(dst: TeeObjHandle, src: TeeObjHandle) -> Result<
312312
}
313313

314314
pub(crate) fn sys_cryp_random_number_generate(buf: &mut [u8]) -> Result<(), TeeResult> {
315-
// FIXME: before we have secure randomness source (see #41), use a fast and insecure one.
316-
let mut rng = FastRng::new_from_seed(core::num::NonZeroU64::new(0x4d595df4d0f33173).unwrap());
317315
if buf.is_empty() {
318316
return Err(TeeResult::BadParameters);
319317
}
320-
321-
let blen8 = buf.len() >> 3;
322-
323-
for i in 0..blen8 {
324-
let val = rng.next_u64();
325-
buf[i * 8..(i + 1) * 8].copy_from_slice(&val.to_be_bytes());
326-
}
327-
328-
let remainder = buf.len() % 8;
329-
if remainder != 0 {
330-
let val = rng.next_u64();
331-
buf[blen8 * 8..blen8 * 8 + remainder].copy_from_slice(&val.to_be_bytes()[..remainder]);
332-
}
333-
318+
<crate::Platform as litebox::platform::CrngProvider>::fill_bytes_crng(
319+
litebox_platform_multiplex::platform(),
320+
buf,
321+
);
334322
Ok(())
335323
}

0 commit comments

Comments
 (0)