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::{
19 Context, Operation, ParamNone, ParamTmpRef, ParamType, ParamValue, Session, Uuid,
20 };
21 use proto::{Command, Mode, AAD_LEN, BUFFER_SIZE, KEY_SIZE, TAG_LEN, UUID};
22
prepare( session: &mut Session, mode: Mode, nonce: &[u8], key: &[u8], aad: &[u8], ) -> optee_teec::Result<()>23 fn prepare(
24 session: &mut Session,
25 mode: Mode,
26 nonce: &[u8],
27 key: &[u8],
28 aad: &[u8],
29 ) -> optee_teec::Result<()> {
30 let p0 = ParamValue::new(mode as u32, 0, ParamType::ValueInput);
31 let p1 = ParamTmpRef::new_input(nonce);
32 let p2 = ParamTmpRef::new_input(key);
33 let p3 = ParamTmpRef::new_input(aad);
34 let mut operation = Operation::new(0, p0, p1, p2, p3);
35
36 session.invoke_command(Command::Prepare as u32, &mut operation)?;
37 Ok(())
38 }
39
update(session: &mut Session, src: &[u8], res: &mut [u8]) -> optee_teec::Result<()>40 fn update(session: &mut Session, src: &[u8], res: &mut [u8]) -> optee_teec::Result<()> {
41 let p0 = ParamTmpRef::new_input(src);
42 let p1 = ParamTmpRef::new_output(res);
43 let mut operation = Operation::new(0, p0, p1, ParamNone, ParamNone);
44
45 session.invoke_command(Command::Update as u32, &mut operation)?;
46
47 Ok(())
48 }
49
encrypt_final( session: &mut Session, src: &[u8], res: &mut [u8], tag: &mut [u8], ) -> optee_teec::Result<()>50 fn encrypt_final(
51 session: &mut Session,
52 src: &[u8],
53 res: &mut [u8],
54 tag: &mut [u8],
55 ) -> optee_teec::Result<()> {
56 let p0 = ParamTmpRef::new_input(src);
57 let p1 = ParamTmpRef::new_output(res);
58 let p2 = ParamTmpRef::new_output(tag);
59 let mut operation = Operation::new(0, p0, p1, p2, ParamNone);
60
61 session.invoke_command(Command::EncFinal as u32, &mut operation)?;
62 Ok(())
63 }
64
decrypt_final( session: &mut Session, src: &[u8], res: &mut [u8], tag: &[u8], ) -> optee_teec::Result<()>65 fn decrypt_final(
66 session: &mut Session,
67 src: &[u8],
68 res: &mut [u8],
69 tag: &[u8],
70 ) -> optee_teec::Result<()> {
71 let p0 = ParamTmpRef::new_input(src);
72 let p1 = ParamTmpRef::new_output(res);
73 let p2 = ParamTmpRef::new_input(tag);
74 let mut operation = Operation::new(0, p0, p1, p2, ParamNone);
75
76 session.invoke_command(Command::DecFinal as u32, &mut operation)?;
77 Ok(())
78 }
79
main() -> optee_teec::Result<()>80 fn main() -> optee_teec::Result<()> {
81 let mut ctx = Context::new()?;
82 let uuid = Uuid::parse_str(UUID).unwrap();
83 let mut session = ctx.open_session(uuid)?;
84
85 let key = [0xa5u8; KEY_SIZE];
86 let nonce = [0x00u8; 2];
87 let aad = [0xffu8; AAD_LEN];
88 let clear1 = [0x5au8; BUFFER_SIZE + 3];
89 let clear2 = [0xa5u8; BUFFER_SIZE - 3];
90 let mut ciph1 = [0x00u8; BUFFER_SIZE];
91 let mut ciph2 = [0x00u8; BUFFER_SIZE];
92 let mut tmp1 = [0x00u8; BUFFER_SIZE];
93 let mut tmp2 = [0x00u8; BUFFER_SIZE];
94 let mut tag = [0x00u8; TAG_LEN];
95
96 prepare(&mut session, Mode::Encrypt, &nonce, &key, &aad)?;
97 update(&mut session, &clear1, &mut ciph1)?;
98 encrypt_final(&mut session, &clear2, &mut ciph2, &mut tag)?;
99
100 prepare(&mut session, Mode::Decrypt, &nonce, &key, &aad)?;
101 update(&mut session, &ciph1, &mut tmp1)?;
102 decrypt_final(&mut session, &ciph2, &mut tmp2, &tag)?;
103
104 let mut clear_total = clear1.to_vec();
105 clear_total.extend_from_slice(&clear2);
106 let mut tmp_total = tmp1.to_vec();
107 tmp_total.extend_from_slice(&tmp2);
108 if clear_total
109 .iter()
110 .zip(tmp_total.iter())
111 .all(|(a, b)| a == b)
112 {
113 println!("Clear text and decoded text match");
114 } else {
115 println!("Clear text and decoded text differ => ERROR");
116 }
117
118 println!("Success");
119 Ok(())
120 }
121