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 optee_teec::{Error, ErrorKind};
22 use proto::{Command, UUID};
23 use std::env;
24 
update(session: &mut Session, src: &[u8]) -> optee_teec::Result<()>25 fn update(session: &mut Session, src: &[u8]) -> optee_teec::Result<()> {
26     let p0 = ParamTmpRef::new_input(src);
27     let mut operation = Operation::new(0, p0, ParamNone, ParamNone, ParamNone);
28 
29     session.invoke_command(Command::Update as u32, &mut operation)?;
30     Ok(())
31 }
32 
do_final(session: &mut Session, src: &[u8], res: &mut [u8]) -> optee_teec::Result<usize>33 fn do_final(session: &mut Session, src: &[u8], res: &mut [u8]) -> optee_teec::Result<usize> {
34     let p0 = ParamTmpRef::new_input(src);
35     let p1 = ParamTmpRef::new_output(res);
36     let p2 = ParamValue::new(0, 0, ParamType::ValueOutput);
37     let mut operation = Operation::new(0, p0, p1, p2, ParamNone);
38 
39     session.invoke_command(Command::DoFinal as u32, &mut operation)?;
40 
41     Ok(operation.parameters().2.a() as usize)
42 }
43 
main() -> optee_teec::Result<()>44 fn main() -> optee_teec::Result<()> {
45     let args: Vec<String> = env::args().collect();
46     let args_len = args.len();
47     if args_len < 2 {
48         println!("Do not receive any message for digest.");
49         println!("Correct usage: passed more than 1 argument as <part_of_message>");
50         return Err(Error::new(ErrorKind::BadParameters));
51     }
52 
53     let mut ctx = Context::new()?;
54     let uuid = Uuid::parse_str(UUID).unwrap();
55 
56     let mut hash: [u8; 32] = [0u8; 32];
57     let mut session = ctx.open_session(uuid)?;
58 
59     for i in 1..args_len - 1 {
60         update(&mut session, args[i].as_bytes())?;
61     }
62 
63     let hash_length = do_final(&mut session, args[args_len - 1].as_bytes(), &mut hash).unwrap();
64     let mut res = hash.to_vec();
65     res.truncate(hash_length as usize);
66 
67     println!("Get message hash as: {:?}.", res);
68 
69     println!("Success");
70     Ok(())
71 }
72