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_std]
19 #![no_main]
20 #![feature(c_size_t)]
21 
22 use optee_utee::BigInt;
23 use optee_utee::{
24     ta_close_session, ta_create, ta_destroy, ta_invoke_command, ta_open_session, trace_println,
25 };
26 use optee_utee::{Error, ErrorKind, Parameters, Result};
27 use proto::Command;
28 
29 #[ta_create]
create() -> Result<()>30 fn create() -> Result<()> {
31     trace_println!("[+] TA create");
32     Ok(())
33 }
34 
35 #[ta_open_session]
open_session(_params: &mut Parameters) -> Result<()>36 fn open_session(_params: &mut Parameters) -> Result<()> {
37     trace_println!("[+] TA open session");
38     Ok(())
39 }
40 
41 #[ta_close_session]
close_session()42 fn close_session() {
43     trace_println!("[+] TA close session");
44 }
45 
46 #[ta_destroy]
destroy()47 fn destroy() {
48     trace_println!("[+] TA destroy");
49 }
50 
compare(n0: &BigInt, n1: &BigInt) -> Result<()>51 fn compare(n0: &BigInt, n1: &BigInt) -> Result<()> {
52     match n0.compare_big_int(n1) {
53         0 => trace_println!("{} == {}.", n0, n1),
54         res if res > 0 => trace_println!("{} > {}.", n0, n1),
55         _ => trace_println!("{} < {}.", n0, n1),
56     }
57     Ok(())
58 }
59 
convert(n0: &BigInt, n1: &BigInt) -> Result<()>60 fn convert(n0: &BigInt, n1: &BigInt) -> Result<()> {
61     trace_println!(
62         "{} in u8 array is {:x?}.",
63         n0,
64         n0.convert_to_octet_string().unwrap()
65     );
66     trace_println!("{} in i32 is {}.", n1, n1.convert_to_s32().unwrap());
67     Ok(())
68 }
69 
add(n0: &BigInt, n1: &BigInt) -> Result<()>70 fn add(n0: &BigInt, n1: &BigInt) -> Result<()> {
71     let res = BigInt::add(n0, n1);
72     trace_println!("{} + {} = {}.", n0, n1, res);
73     Ok(())
74 }
75 
sub(n0: &BigInt, n1: &BigInt) -> Result<()>76 fn sub(n0: &BigInt, n1: &BigInt) -> Result<()> {
77     let res = BigInt::sub(n0, n1);
78     trace_println!("{} - {} = {}.", n0, n1, res);
79     Ok(())
80 }
81 
multiply(n0: &BigInt, n1: &BigInt) -> Result<()>82 fn multiply(n0: &BigInt, n1: &BigInt) -> Result<()> {
83     let res = BigInt::multiply(n0, n1);
84     trace_println!("{} * {} = {}.", n0, n1, res);
85     Ok(())
86 }
87 
divide(n0: &BigInt, n1: &BigInt) -> Result<()>88 fn divide(n0: &BigInt, n1: &BigInt) -> Result<()> {
89     let (quot, rem) = BigInt::divide(n0, n1);
90     trace_println!("{} / {} = {}, ramians {}.", n0, n1, quot, rem);
91     Ok(())
92 }
93 
module(n0: &BigInt, n1: &BigInt) -> Result<()>94 fn module(n0: &BigInt, n1: &BigInt) -> Result<()> {
95     let res = BigInt::module(n0, n1);
96     trace_println!("{} % {} = {}.", n0, n1, res);
97     Ok(())
98 }
99 
100 #[ta_invoke_command]
invoke_command(cmd_id: u32, params: &mut Parameters) -> Result<()>101 fn invoke_command(cmd_id: u32, params: &mut Parameters) -> Result<()> {
102     trace_println!("[+] TA invoke command");
103     let mut n0_buffer = unsafe { params.0.as_memref().unwrap() };
104     let n1_value = unsafe { params.1.as_value().unwrap() };
105 
106     let mut n0 = BigInt::new(64);
107     let mut n1 = BigInt::new(2);
108 
109     n0.convert_from_octet_string(n0_buffer.buffer(), 0)?;
110     n1.convert_from_s32(n1_value.a() as i32);
111 
112     match Command::from(cmd_id) {
113         Command::Compare => compare(&n0, &n1),
114         Command::Convert => convert(&n0, &n1),
115         Command::Add => add(&n0, &n1),
116         Command::Sub => sub(&n0, &n1),
117         Command::Multiply => multiply(&n0, &n1),
118         Command::Divide => divide(&n0, &n1),
119         Command::Module => module(&n0, &n1),
120         _ => Err(Error::new(ErrorKind::BadParameters)),
121     }
122 }
123 
124 // TA configurations
125 const TA_FLAGS: u32 = 0;
126 const TA_DATA_SIZE: u32 = 32 * 1024;
127 const TA_STACK_SIZE: u32 = 2 * 1024;
128 const TA_VERSION: &[u8] = b"0.1\0";
129 const TA_DESCRIPTION: &[u8] = b"Example of TA using arithmeitcal APIs.\0";
130 const EXT_PROP_VALUE_1: &[u8] = b"Big int TA\0";
131 const EXT_PROP_VALUE_2: u32 = 0x0010;
132 const TRACE_LEVEL: i32 = 4;
133 const TRACE_EXT_PREFIX: &[u8] = b"TA\0";
134 const TA_FRAMEWORK_STACK_SIZE: u32 = 2048;
135 
136 include!(concat!(env!("OUT_DIR"), "/user_ta_header.rs"));
137