optee_utee/object/attribute.rs
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
18use core::marker;
19
20use optee_utee_sys as raw;
21
22/// A general attribute (buffer or value) that can be used to populate an object or to specify
23/// operation parameters.
24pub struct Attribute {
25 raw: raw::TEE_Attribute,
26}
27
28impl Attribute {
29 /// Return the raw struct `TEE_Attribute`.
30 pub fn raw(&self) -> raw::TEE_Attribute {
31 self.raw
32 }
33}
34
35/// Convert the buffer attribute [AttributeMemref](crate::AttributeMemref) to
36/// the general attribute.
37impl<'attrref> From<AttributeMemref<'attrref>> for Attribute {
38 fn from(attr: AttributeMemref) -> Self {
39 Self { raw: attr.raw() }
40 }
41}
42
43/// Convert the value attribute [AttributeValue](crate::AttributeValue) to
44/// the general attribute.
45impl From<AttributeValue> for Attribute {
46 fn from(attr: AttributeValue) -> Self {
47 Self { raw: attr.raw() }
48 }
49}
50
51/// A buffer attribute.
52#[derive(Clone, Copy)]
53pub struct AttributeMemref<'attrref> {
54 raw: raw::TEE_Attribute,
55 _marker: marker::PhantomData<&'attrref mut [u8]>,
56}
57
58impl<'attrref> AttributeMemref<'attrref> {
59 /// Return the raw struct TEE_Attribute.
60 pub fn raw(&self) -> raw::TEE_Attribute {
61 self.raw
62 }
63
64 fn new_ref() -> Self {
65 let raw = raw::TEE_Attribute {
66 attributeID: 0,
67 content: raw::content {
68 memref: raw::Memref {
69 buffer: core::ptr::null_mut(),
70 size: 0,
71 },
72 },
73 };
74 Self {
75 raw,
76 _marker: marker::PhantomData,
77 }
78 }
79
80 /// Populate a single attribute with a reference to a buffer.
81 ///
82 /// # Parameters
83 ///
84 /// 1) `id`: The [AttributeId](crate::AttributeId) is an identifier of the
85 /// attribute to populate.
86 /// 2) `buffer`: Input buffer that holds the content of the attribute.
87 ///
88 /// # Example
89 ///
90 /// ``` rust,no_run
91 /// # use optee_utee::{AttributeMemref, AttributeId};
92 /// let mut attr = AttributeMemref::from_ref(AttributeId::SecretValue, &mut [0u8;1]);
93 /// ```
94 pub fn from_ref(id: AttributeId, buffer: &'attrref [u8]) -> Self {
95 let mut res = AttributeMemref::new_ref();
96 unsafe {
97 raw::TEE_InitRefAttribute(
98 &mut res.raw,
99 id as u32,
100 buffer.as_ptr() as *mut _,
101 buffer.len(),
102 );
103 }
104 res
105 }
106}
107
108/// A value attribute.
109pub struct AttributeValue {
110 raw: raw::TEE_Attribute,
111}
112
113impl AttributeValue {
114 /// Return the raw struct TEE_Attribute.
115 pub fn raw(&self) -> raw::TEE_Attribute {
116 self.raw
117 }
118
119 fn new_value() -> Self {
120 let raw = raw::TEE_Attribute {
121 attributeID: 0,
122 content: raw::content {
123 value: raw::Value { a: 0, b: 0 },
124 },
125 };
126 Self { raw }
127 }
128
129 /// Populate a single attribute with two u32 values.
130 ///
131 /// # Parameters
132 ///
133 /// 1) `id`: The [AttributeId](crate::AttributeId) is an identifier of the
134 /// attribute to populate.
135 /// 2) `a`, `b`: u32 values to assign to the members of the value attribute.
136 ///
137 /// # Example
138 ///
139 /// ``` rust,no_run
140 /// # use optee_utee::{AttributeValue, AttributeId};
141 /// let mut attr = AttributeValue::from_value(AttributeId::SecretValue, 0, 0);
142 /// ```
143 pub fn from_value(id: AttributeId, a: u32, b: u32) -> Self {
144 let mut res = AttributeValue::new_value();
145 unsafe {
146 raw::TEE_InitValueAttribute(&mut res.raw, id as u32, a, b);
147 }
148 res
149 }
150}
151
152#[repr(u32)]
153pub enum AttributeId {
154 /// Used for all secret keys for symmetric ciphers, MACs, and HMACs
155 SecretValue = 0xC0000000,
156 /// RSA modulus: `n`
157 RsaModulus = 0xD0000130,
158 /// RSA public key exponent: `e`
159 RsaPublicExponent = 0xD0000230,
160 /// RSA private key exponent: `d`
161 RsaPrivateExponent = 0xC0000330,
162 /// RSA prime number: `p`
163 RsaPrime1 = 0xC0000430,
164 /// RSA prime number: `q`
165 RsaPrime2 = 0xC0000530,
166 /// RSA exponent: `dp`
167 RsaExponent1 = 0xC0000630,
168 /// RSA exponent: `dq`
169 RsaExponent2 = 0xC0000730,
170 /// RSA coefficient: `iq`
171 RsaCoefficient = 0xC0000830,
172 /// DSA prime number: `p`
173 DsaPrime = 0xD0001031,
174 /// DSA sub prime number: `q`
175 DsaSubprime = 0xD0001131,
176 /// DSA base: `g`
177 DsaBase = 0xD0001231,
178 /// DSA public value: `y`
179 DsaPublicValue = 0xD0000131,
180 /// DSA private value: `x`
181 DsaPrivateValue = 0xC0000231,
182 /// Diffie-Hellman prime number: `p`
183 DhPrime = 0xD0001032,
184 /// Diffie-Hellman subprime number: `q`
185 DhSubprime = 0xD0001132,
186 /// Diffie-Hellman base: `g`
187 DhBase = 0xD0001232,
188 /// Diffie-Hellman x bits: `l`
189 DhXBits = 0xF0001332,
190 /// Diffie-Hellman public value: `y`
191 DhPublicValue = 0xD0000132,
192 /// Diffie-Hellman public value: `x`
193 DhPrivateValue = 0xC0000232,
194 RsaOaepLabel = 0xD0000930,
195 RsaOaepMgf1Hash = 0xD0000931,
196 RsaPssSaltLength = 0xF0000A30,
197 /// ECC public value: `x`
198 EccPublicValueX = 0xD0000141,
199 /// ECC public value: `y`
200 EccPublicValueY = 0xD0000241,
201 /// ECC private value: `d`
202 EccPrivateValue = 0xC0000341,
203 /// Ed25519 public value
204 Ed25519PublicValue = 0xD0000743,
205 /// Ed25519 private value
206 Ed25519PrivateValue = 0xC0000843,
207 /// X25519 public value
208 X25519PublicValue = 0xD0000944,
209 /// X25519 private value
210 X25519PrivateValue = 0xC0000A44,
211 /// ECC Curve algorithm
212 EccCurve = 0xF0000441,
213 BitProtected = (1 << 28),
214 BitValue = (1 << 29),
215}