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::{Context, Operation, ParamType, Result, Session, Uuid};
19 use optee_teec::{ParamNone, ParamTmpRef, ParamValue};
20 use proto::{Command, KEY_SIZE, UUID};
21 
generate_key(session: &mut Session) -> Result<(Vec<u8>, Vec<u8>)>22 fn generate_key(session: &mut Session) -> Result<(Vec<u8>, Vec<u8>)> {
23     // Pass in the prime and base
24     let prime_base_vec = [0xB6, 0x73, 0x91, 0xB5, 0xD6, 0xBC, 0x95, 0x73,
25                           0x0D, 0x53, 0x64, 0x13, 0xB0, 0x51, 0xC6, 0xB4,
26                           0xEB, 0x9D, 0x74, 0x57, 0x8D, 0x65, 0x3A, 0x4B,
27                           0x7A, 0xB2, 0x93, 0x27, 0xA6, 0xC1, 0xBC, 0xAB,
28                           5];
29     let p0 = ParamTmpRef::new_input(&prime_base_vec);
30     // Save public and private key size
31     let p1 = ParamValue::new(0, 0, ParamType::ValueOutput);
32     // Vector for generated keys
33     let mut public_key = [0u8; KEY_SIZE];
34     let mut private_key = [0u8; KEY_SIZE];
35     let p2 = ParamTmpRef::new_output(&mut public_key);
36     let p3 = ParamTmpRef::new_output(&mut private_key);
37 
38     let mut operation = Operation::new(0, p0, p1, p2, p3);
39     session.invoke_command(Command::GenerateKey as u32, &mut operation)?;
40 
41     let public_size = operation.parameters().1.a() as usize;
42     let private_size = operation.parameters().1.b() as usize;
43     let mut public_res = vec![0u8; public_size];
44     let mut private_res = vec![0u8; private_size];
45     public_res.copy_from_slice(&public_key[..public_size]);
46     private_res.copy_from_slice(&private_key[..private_size]);
47 
48     Ok((public_res, private_res))
49 }
50 
derive_key(key0_pub: &Vec<u8>, session: &mut Session) -> Result<()>51 fn derive_key(key0_pub: &Vec<u8>, session: &mut Session) -> Result<()> {
52     let p0 = ParamTmpRef::new_input(key0_pub.as_slice());
53     let mut shared_key = [0u8; KEY_SIZE];
54     let p1 = ParamTmpRef::new_output(&mut shared_key);
55     let p2 = ParamValue::new(0, 0, ParamType::ValueOutput);
56     let mut operation = Operation::new(0, p0, p1, p2, ParamNone);
57 
58     session.invoke_command(Command::DeriveKey as u32, &mut operation)?;
59 
60     let key_size = operation.parameters().2.a() as usize;
61     let mut derive_res = vec![0u8; key_size];
62     derive_res.copy_from_slice(&shared_key[..key_size]);
63     println!("Derived share key as {:?}", derive_res);
64     Ok(())
65 }
66 
main() -> Result<()>67 fn main() -> Result<()> {
68     let mut ctx = Context::new()?;
69     let uuid = Uuid::parse_str(UUID).unwrap();
70     let mut session = ctx.open_session(uuid)?;
71 
72     let (mut key0_public, key0_private) = generate_key(&mut session).unwrap();
73     let (key1_public, key1_private) = generate_key(&mut session).unwrap();
74     println!(
75         "get key 0 pair as public: {:?}, private: {:?}",
76         key0_public, key0_private
77     );
78     println!(
79         "get key 1 pair as public: {:?}, private: {:?}",
80         key1_public, key1_private
81     );
82     derive_key(&mut key0_public, &mut session)?;
83 
84     println!("Success");
85     Ok(())
86 }
87