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, Session, Uuid};
19 use optee_teec::{Error, ErrorKind, ParamNone, ParamTmpRef, ParamValue};
20 use proto::{Command, UUID};
21 use std::{env, str};
22
gen_key(session: &mut Session, key_size: u32) -> optee_teec::Result<()>23 fn gen_key(session: &mut Session, key_size: u32) -> optee_teec::Result<()> {
24 let p0 = ParamValue::new(key_size, 0, ParamType::ValueInput);
25 let mut operation = Operation::new(0, p0, ParamNone, ParamNone, ParamNone);
26
27 session.invoke_command(Command::GenKey as u32, &mut operation)?;
28
29 Ok(())
30 }
31
enc_dec(session: &mut Session, plain_text: &[u8]) -> optee_teec::Result<()>32 fn enc_dec(session: &mut Session, plain_text: &[u8]) -> optee_teec::Result<()> {
33 let p0 = ParamValue::new(0, 0, ParamType::ValueOutput);
34 let mut operation = Operation::new(0, p0, ParamNone, ParamNone, ParamNone);
35
36 session.invoke_command(Command::GetSize as u32, &mut operation)?;
37
38 let mut cipher_text = vec![0u8; operation.parameters().0.a() as usize];
39 let p0 = ParamTmpRef::new_input(plain_text);
40 let p1 = ParamTmpRef::new_output(&mut cipher_text);
41 let mut operation2 = Operation::new(0, p0, p1, ParamNone, ParamNone);
42
43 session.invoke_command(Command::Encrypt as u32, &mut operation2)?;
44 println!(
45 "Success encrypt input text \"{}\" as {} bytes cipher text: {:?}",
46 str::from_utf8(plain_text).unwrap(),
47 cipher_text.len(),
48 cipher_text
49 );
50
51 let p0 = ParamTmpRef::new_input(&cipher_text);
52 let mut dec_res: Vec<u8> = vec![0u8; plain_text.len()];
53 let p1 = ParamTmpRef::new_output(&mut dec_res);
54 let mut operation2 = Operation::new(0, p0, p1, ParamNone, ParamNone);
55
56 session.invoke_command(Command::Decrypt as u32, &mut operation2)?;
57 println!(
58 "Success decrypt the above ciphertext as {} bytes plain text: {}",
59 dec_res.len(),
60 str::from_utf8(&dec_res).unwrap()
61 );
62 Ok(())
63 }
64
main() -> optee_teec::Result<()>65 fn main() -> optee_teec::Result<()> {
66 let args: Vec<String> = env::args().collect();
67 if args.len() != 3 {
68 println!(
69 "Receive {} arguments while 2 arguments are expected!",
70 args.len()
71 );
72 println!("Correct usage: passed 2 arguments as <key_size> and <string to encrypt>");
73 return Err(Error::new(ErrorKind::BadParameters));
74 }
75
76 let mut key_size = args[1].parse::<u32>().unwrap();
77 if key_size < 256 {
78 println!(
79 "Wrong key size {} is received. Use default minimal key size 256 instead.",
80 key_size
81 );
82 key_size = 256;
83 }
84
85 let mut ctx = Context::new()?;
86 let uuid = Uuid::parse_str(UUID).unwrap();
87 let mut session = ctx.open_session(uuid)?;
88
89 gen_key(&mut session, key_size)?;
90 enc_dec(&mut session, args[2].as_bytes())?;
91
92 Ok(())
93 }
94