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