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 #![no_main]
19 
20 use optee_utee::{
21     ta_close_session, ta_create, ta_destroy, ta_invoke_command, ta_open_session, trace_println,
22 };
23 use optee_utee::{AlgorithmId, Digest};
24 use optee_utee::{Error, ErrorKind, Parameters, Result};
25 use proto::Command;
26 
27 pub struct DigestOp {
28     pub op: Digest,
29 }
30 
31 impl Default for DigestOp {
default() -> Self32     fn default() -> Self {
33         Self {
34             op: Digest::allocate(AlgorithmId::Sha256).unwrap(),
35         }
36     }
37 }
38 
39 #[ta_create]
create() -> Result<()>40 fn create() -> Result<()> {
41     trace_println!("[+] TA create");
42     Ok(())
43 }
44 
45 #[ta_open_session]
open_session(_params: &mut Parameters, _sess_ctx: &mut DigestOp) -> Result<()>46 fn open_session(_params: &mut Parameters, _sess_ctx: &mut DigestOp) -> Result<()> {
47     trace_println!("[+] TA open session");
48     Ok(())
49 }
50 
51 #[ta_close_session]
close_session(_sess_ctx: &mut DigestOp)52 fn close_session(_sess_ctx: &mut DigestOp) {
53     trace_println!("[+] TA close session");
54 }
55 
56 #[ta_destroy]
destroy()57 fn destroy() {
58     trace_println!("[+] TA destroy");
59 }
60 
61 #[ta_invoke_command]
invoke_command(sess_ctx: &mut DigestOp, cmd_id: u32, params: &mut Parameters) -> Result<()>62 fn invoke_command(sess_ctx: &mut DigestOp, cmd_id: u32, params: &mut Parameters) -> Result<()> {
63     trace_println!("[+] TA invoke command");
64     match Command::from(cmd_id) {
65         Command::Update => {
66             return update(sess_ctx, params);
67         }
68         Command::DoFinal => {
69             return do_final(sess_ctx, params);
70         }
71         _ => {
72             return Err(Error::new(ErrorKind::BadParameters));
73         }
74     }
75 }
76 
update(digest: &mut DigestOp, params: &mut Parameters) -> Result<()>77 pub fn update(digest: &mut DigestOp, params: &mut Parameters) -> Result<()> {
78     let mut p = unsafe { params.0.as_memref().unwrap() };
79     let buffer = p.buffer();
80     digest.op.update(buffer);
81     Ok(())
82 }
83 
do_final(digest: &mut DigestOp, params: &mut Parameters) -> Result<()>84 pub fn do_final(digest: &mut DigestOp, params: &mut Parameters) -> Result<()> {
85     let mut p0 = unsafe { params.0.as_memref().unwrap() };
86     let mut p1 = unsafe { params.1.as_memref().unwrap() };
87     let mut p2 = unsafe { params.2.as_value().unwrap() };
88     let input = p0.buffer();
89     let output = p1.buffer();
90     match digest.op.do_final(input, output) {
91         Err(e) => Err(e),
92         Ok(hash_length) => {
93             p2.set_a(hash_length as u32);
94             Ok(())
95         }
96     }
97 }
98 
99 // TA configurations
100 const TA_FLAGS: u32 = 0;
101 const TA_DATA_SIZE: u32 = 32 * 1024;
102 const TA_STACK_SIZE: u32 = 2 * 1024;
103 const TA_VERSION: &[u8] = b"0.1\0";
104 const TA_DESCRIPTION: &[u8] = b"This is a message digest example.\0";
105 const EXT_PROP_VALUE_1: &[u8] = b"Digest TA\0";
106 const EXT_PROP_VALUE_2: u32 = 0x0010;
107 const TRACE_LEVEL: i32 = 4;
108 const TRACE_EXT_PREFIX: &[u8] = b"TA\0";
109 const TA_FRAMEWORK_STACK_SIZE: u32 = 2048;
110 
111 include!(concat!(env!("OUT_DIR"), "/user_ta_header.rs"));
112