1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 use optee_teec::{Context, ErrorKind, Operation, ParamNone, ParamTmpRef, Session, Uuid};
19 use proto::{Command, UUID};
20 use std::ffi::CString;
21
22 const TEST_OBJECT_SIZE: usize = 7000;
23
read_secure_object( session: &mut Session, obj_id: &[u8], obj_data: &mut [u8], ) -> optee_teec::Result<()>24 fn read_secure_object(
25 session: &mut Session,
26 obj_id: &[u8],
27 obj_data: &mut [u8],
28 ) -> optee_teec::Result<()> {
29 let p0 = ParamTmpRef::new_input(obj_id);
30 let p1 = ParamTmpRef::new_output(obj_data);
31 let mut operation = Operation::new(0, p0, p1, ParamNone, ParamNone);
32
33 session.invoke_command(Command::Read as u32, &mut operation)?;
34
35 println!("- Read back the object");
36 Ok(())
37 }
38
write_secure_object( session: &mut Session, obj_id: &[u8], obj_data: &[u8], ) -> optee_teec::Result<()>39 fn write_secure_object(
40 session: &mut Session,
41 obj_id: &[u8],
42 obj_data: &[u8],
43 ) -> optee_teec::Result<()> {
44 let p0 = ParamTmpRef::new_input(obj_id);
45 let p1 = ParamTmpRef::new_input(obj_data);
46 let mut operation = Operation::new(0, p0, p1, ParamNone, ParamNone);
47
48 session.invoke_command(Command::Write as u32, &mut operation)?;
49
50 println!("- Create and load object in the TA secure storage");
51 Ok(())
52 }
53
delete_secure_object(session: &mut Session, obj_id: &[u8]) -> optee_teec::Result<()>54 fn delete_secure_object(session: &mut Session, obj_id: &[u8]) -> optee_teec::Result<()> {
55 let p0 = ParamTmpRef::new_input(obj_id);
56 let mut operation = Operation::new(0, p0, ParamNone, ParamNone, ParamNone);
57
58 session.invoke_command(Command::Delete as u32, &mut operation)?;
59
60 println!("- Delete the object");
61 Ok(())
62 }
63
main() -> optee_teec::Result<()>64 fn main() -> optee_teec::Result<()> {
65 let mut ctx = Context::new()?;
66 let uuid = Uuid::parse_str(UUID).unwrap();
67 let mut session = ctx.open_session(uuid)?;
68
69 let obj1_id = CString::new("object#1").unwrap().into_bytes_with_nul();
70 let obj1_data = [0xA1u8; TEST_OBJECT_SIZE];
71 let mut read_data = [0x00u8; TEST_OBJECT_SIZE];
72
73 println!("\nTest on object \"object#1\"");
74 write_secure_object(&mut session, obj1_id.as_slice(), &obj1_data)?;
75 read_secure_object(&mut session, obj1_id.as_slice(), &mut read_data)?;
76
77 if obj1_data.iter().zip(read_data.iter()).all(|(a, b)| a == b) {
78 println!("- Content read-out correctly");
79 } else {
80 println!("- Unexpected content found in secure storage");
81 }
82 delete_secure_object(&mut session, &obj1_id)?;
83
84 let obj2_id = CString::new("object#2").unwrap().into_bytes_with_nul();
85
86 println!("\nTest on object \"object#2\"");
87 match read_secure_object(&mut session, obj2_id.as_slice(), &mut read_data) {
88 Err(e) => {
89 if e.kind() != ErrorKind::ItemNotFound {
90 println!("{}", e);
91 return Err(e);
92 } else {
93 println!("- Object not found in TA secure storage, create it");
94 let obj2_data = [0xB1u8; TEST_OBJECT_SIZE];
95 write_secure_object(&mut session, &obj2_id, &obj2_data)?;
96 }
97 }
98
99 Ok(()) => {
100 println!("- Object found in TA secure storage, delete it");
101 delete_secure_object(&mut session, &obj2_id)?;
102 }
103 }
104
105 println!("\nWe're done, close and release TEE resources");
106 Ok(())
107 }
108