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, Error, ErrorKind, Operation, ParamNone, ParamTmpRef, ParamType, ParamValue, Session,
20     Uuid,
21 };
22 use proto::{Command, UUID};
23 
24 const TEST_SIZE: usize = 10;
25 const SIZE_K: usize = 20;
26 const RFC4226_TEST_VALUES: [u32; TEST_SIZE] = [
27     755224, 287082, 359152, 969429, 338314, 254676, 287922, 162583, 399871, 520489,
28 ];
29 
register_shared_key(session: &mut Session) -> optee_teec::Result<()>30 fn register_shared_key(session: &mut Session) -> optee_teec::Result<()> {
31     let k: [u8; SIZE_K] = [
32         0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
33         0x36, 0x37, 0x38, 0x39, 0x30,
34     ];
35 
36     let p0 = ParamTmpRef::new_input(&k);
37     let mut operation = Operation::new(0, p0, ParamNone, ParamNone, ParamNone);
38 
39     session.invoke_command(Command::RegisterSharedKey as u32, &mut operation)?;
40     Ok(())
41 }
42 
get_hotp(session: &mut Session) -> optee_teec::Result<()>43 fn get_hotp(session: &mut Session) -> optee_teec::Result<()> {
44     let p0 = ParamValue::new(0, 0, ParamType::ValueOutput);
45     let mut operation = Operation::new(0, p0, ParamNone, ParamNone, ParamNone);
46 
47     for i in 0..TEST_SIZE {
48         session.invoke_command(Command::GetHOTP as u32, &mut operation)?;
49         let (p0, _, _, _) = operation.parameters();
50         let hotp_value = p0.a();
51 
52         println!("Get HOTP: {}", hotp_value);
53 
54         if hotp_value != RFC4226_TEST_VALUES[i] {
55             println!(
56                 "Wrong value get! Expected value: {}",
57                 RFC4226_TEST_VALUES[i]
58             );
59             return Err(Error::new(ErrorKind::Generic));
60         }
61     }
62     Ok(())
63 }
64 
main() -> optee_teec::Result<()>65 fn main() -> optee_teec::Result<()> {
66     let mut ctx = Context::new()?;
67     let uuid = Uuid::parse_str(UUID).unwrap();
68     let mut session = ctx.open_session(uuid)?;
69 
70     register_shared_key(&mut session)?;
71     get_hotp(&mut session)?;
72 
73     println!("Success");
74     Ok(())
75 }
76