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#[derive(Clone, Copy, strum::Display)]
153#[repr(u32)]
154pub enum AttributeId {
155 /// Used for all secret keys for symmetric ciphers, MACs, and HMACs
156 SecretValue = 0xC0000000,
157 /// RSA modulus: `n`
158 RsaModulus = 0xD0000130,
159 /// RSA public key exponent: `e`
160 RsaPublicExponent = 0xD0000230,
161 /// RSA private key exponent: `d`
162 RsaPrivateExponent = 0xC0000330,
163 /// RSA prime number: `p`
164 RsaPrime1 = 0xC0000430,
165 /// RSA prime number: `q`
166 RsaPrime2 = 0xC0000530,
167 /// RSA exponent: `dp`
168 RsaExponent1 = 0xC0000630,
169 /// RSA exponent: `dq`
170 RsaExponent2 = 0xC0000730,
171 /// RSA coefficient: `iq`
172 RsaCoefficient = 0xC0000830,
173 /// DSA prime number: `p`
174 DsaPrime = 0xD0001031,
175 /// DSA sub prime number: `q`
176 DsaSubprime = 0xD0001131,
177 /// DSA base: `g`
178 DsaBase = 0xD0001231,
179 /// DSA public value: `y`
180 DsaPublicValue = 0xD0000131,
181 /// DSA private value: `x`
182 DsaPrivateValue = 0xC0000231,
183 /// Diffie-Hellman prime number: `p`
184 DhPrime = 0xD0001032,
185 /// Diffie-Hellman subprime number: `q`
186 DhSubprime = 0xD0001132,
187 /// Diffie-Hellman base: `g`
188 DhBase = 0xD0001232,
189 /// Diffie-Hellman x bits: `l`
190 DhXBits = 0xF0001332,
191 /// Diffie-Hellman public value: `y`
192 DhPublicValue = 0xD0000132,
193 /// Diffie-Hellman public value: `x`
194 DhPrivateValue = 0xC0000232,
195 /// PBKDF2 password, bytes of the password
196 Pbkdf2Password = 0xC00001C2,
197 /// PBKDF2 salt, bytes of the salt
198 Pbkdf2Salt = 0xD00002C2,
199 /// PBKDF2 iteration count
200 Pbkdf2IterationCount = 0xF00003C2,
201 /// PBKDF2 derived key material length in bytes
202 Pbkdf2DkmLength = 0xF00004C2,
203 RsaOaepLabel = 0xD0000930,
204 RsaOaepMgf1Hash = 0xD0000931,
205 RsaPssSaltLength = 0xF0000A30,
206 /// ECC public value: `x`
207 EccPublicValueX = 0xD0000141,
208 /// ECC public value: `y`
209 EccPublicValueY = 0xD0000241,
210 /// ECC private value: `d`
211 EccPrivateValue = 0xC0000341,
212 /// Ed25519 public value
213 Ed25519PublicValue = 0xD0000743,
214 /// Ed25519 private value
215 Ed25519PrivateValue = 0xC0000843,
216 /// X25519 public value
217 X25519PublicValue = 0xD0000944,
218 /// X25519 private value
219 X25519PrivateValue = 0xC0000A44,
220 /// ECC Curve algorithm
221 EccCurve = 0xF0000441,
222 BitProtected = (1 << 28),
223 BitValue = (1 << 29),
224}