1 // Copyright 2021, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use diced_open_dice_cbor as dice;
16 use diced_sample_inputs;
17 use diced_utils;
18 use std::convert::{TryInto, Into};
19
20 mod utils;
21 use utils::with_connection;
22
23 static TEST_MESSAGE: &[u8] = &[
24 // "My test message!"
25 0x4d, 0x79, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x21,
26 0x0a,
27 ];
28
29 // This test calls derive with an empty argument vector and with a set of three input values.
30 // It then performs the same three derivation steps on the result of the former and compares
31 // the result to the result of the latter.
32 #[test]
equivalence_test()33 fn equivalence_test() {
34 with_connection(|device| {
35 let input_values = diced_sample_inputs::get_input_values_vector();
36 let former = device.derive(&[]).expect("Trying to call derive.");
37 let latter = device
38 .derive(&input_values)
39 .expect("Trying to call derive with input values.");
40 let artifacts = diced_utils::ResidentArtifacts::new(
41 former.cdiAttest[..].try_into().unwrap(),
42 former.cdiSeal[..].try_into().unwrap(),
43 &former.bcc.data,
44 )
45 .unwrap();
46
47 let input_values: Vec<diced_utils::InputValues> = input_values
48 .iter()
49 .map(|v| v.into())
50 .collect();
51
52 let artifacts = artifacts
53 .execute_steps(input_values.iter().map(|v| v as &dyn dice::InputValues))
54 .unwrap();
55 let (cdi_attest, cdi_seal, bcc) = artifacts.into_tuple();
56 let from_former = diced_utils::make_bcc_handover(
57 cdi_attest[..].try_into().unwrap(),
58 cdi_seal[..].try_into().unwrap(),
59 &bcc,
60 )
61 .unwrap();
62 // TODO b/204938506 when we have a parser/verifier, check equivalence rather
63 // than bit by bit equality.
64 assert_eq!(latter, from_former);
65 Ok(())
66 })
67 }
68
69 #[test]
sign_and_verify()70 fn sign_and_verify() {
71 with_connection(|device| {
72 let _signature = device
73 .sign(&[], TEST_MESSAGE)
74 .expect("Trying to call sign.");
75
76 let _bcc = device
77 .getAttestationChain(&[])
78 .expect("Trying to call getAttestationChain.");
79 // TODO b/204938506 check the signature with the bcc when the verifier is available.
80 Ok(())
81 })
82 }
83