1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //! Module providing access to platform specific functions used by the library.
18 use kmr_common::crypto;
19
20 use crate::ffi_bindings;
21
22 // Placeholder for function to compare VM identities. Identities will probably be based on DICE,
23 // a simple comparison could be done if the DICE chains are unencrypted and the order of fields is
24 // always the same.
25 #[allow(dead_code)]
compare_vm_identities(vm1_identity: &[u8], vm2_identity: &[u8]) -> bool26 pub(crate) fn compare_vm_identities(vm1_identity: &[u8], vm2_identity: &[u8]) -> bool {
27 (vm1_identity.len() == vm2_identity.len()) && openssl::memcmp::eq(vm1_identity, vm2_identity)
28 }
29
30 #[derive(Default)]
31 pub(crate) struct PlatformRng;
32
33 impl crypto::Rng for PlatformRng {
add_entropy(&mut self, data: &[u8])34 fn add_entropy(&mut self, data: &[u8]) {
35 trusty_rng_add_entropy(data);
36 }
fill_bytes(&mut self, dest: &mut [u8])37 fn fill_bytes(&mut self, dest: &mut [u8]) {
38 openssl::rand::rand_bytes(dest)
39 .expect("shouldn't happen, function never fails on BoringSSL");
40 }
41 }
42
43 /// Add entropy to Trusty's RNG.
trusty_rng_add_entropy(data: &[u8])44 pub fn trusty_rng_add_entropy(data: &[u8]) {
45 // Safety: `data` is a valid slice
46 let rc = unsafe { ffi_bindings::sys::trusty_rng_add_entropy(data.as_ptr(), data.len()) };
47 if rc != 0 {
48 panic!("trusty_rng_add_entropy() failed, {}", rc)
49 }
50 }
51