1#![allow(unused_assignments)]
8
9use crate::api::tee_api_mm::{TEE_CheckMemoryAccessRights, TEE_Free, TEE_Malloc, TEE_MemFill};
10use crate::api::tee_api_objects::{
11 __utee_from_attr, TEE_AllocateTransientObject, TEE_CopyObjectAttributes1,
12 TEE_FreeTransientObject, TEE_GetObjectInfo1, TEE_ResetTransientObject,
13};
14use crate::api::tee_api_panic::TEE_Panic;
15use crate::syscalls::syscall_table::{
16 _utee_asymm_operate, _utee_asymm_verify, _utee_authenc_dec_final, _utee_authenc_enc_final,
17 _utee_authenc_init, _utee_authenc_update_aad, _utee_authenc_update_payload, _utee_cipher_final,
18 _utee_cipher_init, _utee_cipher_update, _utee_cryp_derive_key, _utee_cryp_obj_get_info,
19 _utee_cryp_random_number_generate, _utee_cryp_state_alloc, _utee_cryp_state_copy,
20 _utee_cryp_state_free, _utee_hash_final, _utee_hash_init, _utee_hash_update,
21};
22
23use crate::tee_api_defines::*;
24use crate::tee_api_types::{
25 TEE_Attribute, TEE_ObjectHandle, TEE_ObjectInfo, TEE_OperationHandle, TEE_OperationInfo,
26 TEE_OperationInfoKey, TEE_OperationInfoMultiple, TEE_Result,
27};
28
29use std::ptr;
30
31pub fn TEE_ALG_GET_CLASS(alg: u32) -> u32 {
42 (alg >> 24) & 0xFF
43}
44
45pub(crate) fn TEE_ALG_GET_MAIN_ALG(algo: u32) -> u32 {
46 match algo {
47 TEE_ALG_SM2_PKE => TEE_MAIN_ALGO_SM2_PKE,
48 TEE_ALG_SM2_KEP => TEE_MAIN_ALGO_SM2_KEP,
49 TEE_ALG_X25519 => TEE_MAIN_ALGO_X25519,
50 TEE_ALG_ED25519 => TEE_MAIN_ALGO_ED25519,
51 TEE_ALG_ECDSA_SHA1 | TEE_ALG_ECDSA_SHA224 | TEE_ALG_ECDSA_SHA256 | TEE_ALG_ECDSA_SHA384
52 | TEE_ALG_ECDSA_SHA512 => TEE_MAIN_ALGO_ECDSA,
53 TEE_ALG_HKDF => TEE_MAIN_ALGO_HKDF,
54 TEE_ALG_SHAKE128 => TEE_MAIN_ALGO_SHAKE128,
55 TEE_ALG_SHAKE256 => TEE_MAIN_ALGO_SHAKE256,
56 TEE_ALG_X448 => TEE_MAIN_ALGO_X448,
57 _ => algo & 0xff,
58 }
59}
60
61pub fn TEE_ALG_GET_KEY_TYPE(alg: u32, mode: u32) -> Result<(u32, u32), u32> {
83 let req_key_type;
84 let mut req_key_type2: u32 = 0;
85 match TEE_ALG_GET_MAIN_ALG(alg) {
86 TEE_MAIN_ALGO_MD5 => {
87 req_key_type = TEE_TYPE_HMAC_MD5;
88 }
89 TEE_MAIN_ALGO_SHA1 => {
90 req_key_type = TEE_TYPE_HMAC_SHA1;
91 }
92 TEE_MAIN_ALGO_SHA224 => {
93 req_key_type = TEE_TYPE_HMAC_SHA224;
94 }
95 TEE_MAIN_ALGO_SHA256 => {
96 req_key_type = TEE_TYPE_HMAC_SHA256;
97 }
98 TEE_MAIN_ALGO_SHA384 => {
99 req_key_type = TEE_TYPE_HMAC_SHA384;
100 }
101 TEE_MAIN_ALGO_SHA512 => {
102 req_key_type = TEE_TYPE_HMAC_SHA512;
103 }
104 TEE_MAIN_ALGO_SHA3_224 => {
105 req_key_type = TEE_TYPE_HMAC_SHA3_224;
106 }
107 TEE_MAIN_ALGO_SHA3_256 => {
108 req_key_type = TEE_TYPE_HMAC_SHA3_256;
109 }
110 TEE_MAIN_ALGO_SHA3_384 => {
111 req_key_type = TEE_TYPE_HMAC_SHA3_384;
112 }
113 TEE_MAIN_ALGO_SHA3_512 => {
114 req_key_type = TEE_TYPE_HMAC_SHA3_512;
115 }
116 TEE_MAIN_ALGO_SM3 => {
117 req_key_type = TEE_TYPE_HMAC_SM3;
118 }
119 TEE_MAIN_ALGO_AES => {
120 req_key_type = TEE_TYPE_AES;
121 }
122 TEE_MAIN_ALGO_DES => {
123 req_key_type = TEE_TYPE_DES;
124 }
125 TEE_MAIN_ALGO_DES3 => {
126 req_key_type = TEE_TYPE_DES3;
127 }
128 TEE_MAIN_ALGO_SM4 => {
129 req_key_type = TEE_TYPE_SM4;
130 }
131 TEE_MAIN_ALGO_RSA => {
132 if mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY {
133 req_key_type = TEE_TYPE_RSA_PUBLIC_KEY;
134 } else {
135 req_key_type = TEE_TYPE_RSA_KEYPAIR;
136 }
137 }
138 TEE_MAIN_ALGO_DSA => {
139 req_key_type = TEE_TYPE_DSA_KEYPAIR;
140 if mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY {
141 req_key_type2 = TEE_TYPE_DSA_PUBLIC_KEY;
142 }
143 }
144 TEE_MAIN_ALGO_DH => {
145 req_key_type = TEE_TYPE_DH_KEYPAIR;
146 }
147 TEE_MAIN_ALGO_ECDSA => {
148 req_key_type = TEE_TYPE_ECDSA_KEYPAIR;
149 if mode == TEE_MODE_VERIFY {
150 req_key_type2 = TEE_TYPE_ECDSA_PUBLIC_KEY;
151 }
152 }
153 TEE_MAIN_ALGO_ECDH => {
154 req_key_type = TEE_TYPE_ECDH_KEYPAIR;
155 }
156 TEE_MAIN_ALGO_ED25519 => {
157 req_key_type = TEE_TYPE_ED25519_KEYPAIR;
158 if mode == TEE_MODE_VERIFY {
159 req_key_type2 = TEE_TYPE_ED25519_PUBLIC_KEY;
160 }
161 }
162 TEE_MAIN_ALGO_SM2_PKE => {
163 if mode == TEE_MODE_ENCRYPT {
164 req_key_type = TEE_TYPE_SM2_PKE_PUBLIC_KEY;
165 } else {
166 req_key_type = TEE_TYPE_SM2_PKE_KEYPAIR;
167 }
168 }
169 TEE_MAIN_ALGO_SM2_DSA_SM3 => {
170 if mode == TEE_MODE_VERIFY {
171 req_key_type = TEE_TYPE_SM2_DSA_PUBLIC_KEY;
172 } else {
173 req_key_type = TEE_TYPE_SM2_DSA_KEYPAIR;
174 }
175 }
176 TEE_MAIN_ALGO_SM2_KEP => {
177 req_key_type = TEE_TYPE_SM2_KEP_KEYPAIR;
178 req_key_type2 = TEE_TYPE_SM2_KEP_PUBLIC_KEY;
179 }
180 TEE_MAIN_ALGO_HKDF => {
181 req_key_type = TEE_TYPE_HKDF_IKM;
182 }
183 TEE_MAIN_ALGO_CONCAT_KDF => {
184 req_key_type = TEE_TYPE_CONCAT_KDF_Z;
185 }
186 TEE_MAIN_ALGO_PBKDF2 => {
187 req_key_type = TEE_TYPE_PBKDF2_PASSWORD;
188 }
189 TEE_MAIN_ALGO_X25519 => {
190 req_key_type = TEE_TYPE_X25519_KEYPAIR;
191 }
192 TEE_MAIN_ALGO_X448 => {
193 req_key_type = TEE_TYPE_X448_KEYPAIR;
194 }
195 _ => return Err(TEE_ERROR_BAD_PARAMETERS),
196 }
197 Ok((req_key_type, req_key_type2))
198}
199
200impl TEE_OperationHandle {
201 pub fn new(
203 info: TEE_OperationInfo,
204 key1: TEE_ObjectHandle,
205 key2: TEE_ObjectHandle,
206 operation_state: u32,
207 block_size: usize,
208 state: u32,
209 ) -> Self {
210 TEE_OperationHandle {
211 info,
212 key1,
213 key2,
214 operation_state,
215 buffer: core::ptr::null_mut(),
216 buffer_two_blocks: false,
217 block_size,
218 buffer_offs: 0,
219 state,
220 }
221 }
222
223 pub fn free_buffer(&mut self) {
225 if !self.buffer.is_null() {
226 TEE_Free(self.buffer as *mut core::ffi::c_void);
227 self.buffer = ptr::null_mut();
228 self.buffer_offs = 0;
229 }
230 }
231}
232
233impl Drop for TEE_OperationHandle {
234 fn drop(&mut self) {
235 self.free_buffer();
236 }
237}
238
239#[derive(Debug, PartialEq)]
241pub enum BufferError {
242 BufferNotAllocated,
243 InsufficientSpace,
244 AllocationFailed,
245}
246
247impl std::fmt::Display for BufferError {
248 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
249 match self {
250 BufferError::BufferNotAllocated => write!(f, "Buffer not allocated"),
251 BufferError::InsufficientSpace => write!(f, "Insufficient space in buffer"),
252 BufferError::AllocationFailed => write!(f, "Memory allocation failed"),
253 }
254 }
255}
256
257impl std::error::Error for BufferError {}
258
259impl TEE_OperationHandle {
260 pub fn allocate_buffer_v2(&mut self, size: usize) -> std::result::Result<(), BufferError> {
262 let new_buffer = TEE_Malloc(size, TEE_MALLOC_FILL_ZERO);
264 if new_buffer.is_null() {
265 return Err(BufferError::AllocationFailed);
266 }
267
268 if !self.buffer.is_null() {
270 TEE_Free(self.buffer as *mut core::ffi::c_void);
271 }
272
273 self.buffer = new_buffer as *mut u8;
274 self.buffer_offs = 0;
275 Ok(())
276 }
277}
278
279#[derive(Debug, PartialEq)]
281enum OperationConfig {
282 Digest {
283 block_size: usize,
284 },
285 Cipher {
286 block_size: usize,
287 buffer_two_blocks: bool,
288 req_key_usage: u32,
289 with_private_key: bool,
290 },
291 AsymmetricSignature {
292 req_key_usage: u32,
293 with_private_key: bool,
294 },
295 AsymmetricEncryption {
296 req_key_usage: u32,
297 with_private_key: bool,
298 },
299 KeyDerivation {
300 req_key_usage: u32,
301 },
302 Mac {
303 req_key_usage: u32,
304 },
305}
306
307fn validate_algorithm_params(
309 algorithm: u32,
310 mode: u32,
311 max_key_size: u32,
312) -> Result<OperationConfig, TEE_Result> {
313 use crate::tee_api_defines::*;
314
315 match algorithm {
317 TEE_ALG_DSA_SHA1 => {
318 if max_key_size < 512 || max_key_size > 1024 || max_key_size % 64 != 0 {
319 return Err(TEE_ERROR_NOT_SUPPORTED);
320 }
321 }
322 TEE_ALG_DSA_SHA224 => {
323 if max_key_size != 2048 {
324 return Err(TEE_ERROR_NOT_SUPPORTED);
325 }
326 }
327 TEE_ALG_DSA_SHA256 => {
328 if max_key_size != 2048 && max_key_size != 3072 {
329 return Err(TEE_ERROR_NOT_SUPPORTED);
330 }
331 }
332 TEE_ALG_ECDSA_P192 | TEE_ALG_ECDH_P192 => {
333 if max_key_size != 192 {
334 return Err(TEE_ERROR_NOT_SUPPORTED);
335 }
336 }
337 TEE_ALG_ECDSA_P224 | TEE_ALG_ECDH_P224 => {
338 if max_key_size != 224 {
339 return Err(TEE_ERROR_NOT_SUPPORTED);
340 }
341 }
342 TEE_ALG_ECDSA_P256 | TEE_ALG_ECDH_P256 | TEE_ALG_SM2_PKE | TEE_ALG_SM2_DSA_SM3 => {
343 if max_key_size != 256 {
344 return Err(TEE_ERROR_NOT_SUPPORTED);
345 }
346 }
347 TEE_ALG_SM2_KEP => {
348 if max_key_size != 512 {
349 return Err(TEE_ERROR_NOT_SUPPORTED);
350 }
351 }
352 TEE_ALG_ECDSA_P384 | TEE_ALG_ECDH_P384 => {
353 if max_key_size != 384 {
354 return Err(TEE_ERROR_NOT_SUPPORTED);
355 }
356 }
357 TEE_ALG_ECDSA_P521 | TEE_ALG_ECDH_P521 => {
358 if max_key_size != 521 {
359 return Err(TEE_ERROR_NOT_SUPPORTED);
360 }
361 }
362 _ => {}
363 }
364
365 match algorithm {
367 TEE_ALG_MD5 | TEE_ALG_SHA1 | TEE_ALG_SHA224 | TEE_ALG_SHA256 | TEE_ALG_SHA384
369 | TEE_ALG_SHA512 | TEE_ALG_SM3 => {
370 if mode != TEE_MODE_DIGEST {
371 return Err(TEE_ERROR_NOT_SUPPORTED);
372 }
373 let digest_length = match algorithm & 0x000000FF {
374 0x01 => 16, 0x02 => 20, 0x03 => 28, 0x04 => 32, 0x05 => 48, 0x06 => 64, 0x07 => 32, _ => 0,
382 };
383 Ok(OperationConfig::Digest {
384 block_size: digest_length,
385 })
386 }
387
388 TEE_ALG_DES_CBC_MAC_NOPAD
390 | TEE_ALG_AES_CBC_MAC_NOPAD
391 | TEE_ALG_AES_CBC_MAC_PKCS5
392 | TEE_ALG_AES_CMAC
393 | TEE_ALG_DES_CBC_MAC_PKCS5
394 | TEE_ALG_DES3_CBC_MAC_NOPAD
395 | TEE_ALG_DES3_CBC_MAC_PKCS5
396 | TEE_ALG_HMAC_MD5
397 | TEE_ALG_HMAC_SHA1
398 | TEE_ALG_HMAC_SHA224
399 | TEE_ALG_HMAC_SHA256
400 | TEE_ALG_HMAC_SHA384
401 | TEE_ALG_HMAC_SHA512
402 | TEE_ALG_HMAC_SM3 => {
403 if mode != TEE_MODE_MAC {
404 return Err(TEE_ERROR_NOT_SUPPORTED);
405 }
406 Ok(OperationConfig::Mac {
407 req_key_usage: TEE_USAGE_MAC,
408 })
409 }
410
411 TEE_ALG_AES_ECB_NOPAD
413 | TEE_ALG_AES_CBC_NOPAD
414 | TEE_ALG_AES_CCM
415 | TEE_ALG_DES_ECB_NOPAD
416 | TEE_ALG_DES_CBC_NOPAD
417 | TEE_ALG_DES3_ECB_NOPAD
418 | TEE_ALG_DES3_CBC_NOPAD
419 | TEE_ALG_SM4_ECB_NOPAD
420 | TEE_ALG_SM4_CBC_NOPAD
421 | TEE_ALG_SM4_CTR => {
422 let main_alg = (algorithm & 0x00FF0000) >> 16;
423 let block_size = match main_alg {
424 0x1000 => 16, 0x4000 => 16, _ => 8,
427 };
428 Ok(OperationConfig::Cipher {
429 block_size,
430 buffer_two_blocks: false,
431 req_key_usage: 0, with_private_key: false, })
434 }
435
436 TEE_ALG_AES_CTS => {
438 let main_alg = (algorithm & 0x00FF0000) >> 16;
439 let block_size = match main_alg {
440 0x1000 => 16, 0x4000 => 16, _ => return Err(TEE_ERROR_NOT_SUPPORTED),
443 };
444 Ok(OperationConfig::Cipher {
445 block_size,
446 buffer_two_blocks: true,
447 req_key_usage: 0,
448 with_private_key: false,
449 })
450 }
451
452 TEE_ALG_AES_CTR | TEE_ALG_AES_GCM | TEE_ALG_SM4_GCM => {
454 let block_size = 16;
455 match mode {
456 TEE_MODE_ENCRYPT => Ok(OperationConfig::Cipher {
457 block_size,
458 buffer_two_blocks: false,
459 req_key_usage: TEE_USAGE_ENCRYPT,
460 with_private_key: false,
461 }),
462 TEE_MODE_DECRYPT => Ok(OperationConfig::Cipher {
463 block_size,
464 buffer_two_blocks: false,
465 req_key_usage: TEE_USAGE_DECRYPT,
466 with_private_key: true,
467 }),
468 _ => Err(TEE_ERROR_NOT_SUPPORTED),
469 }
470 }
471
472 TEE_ALG_ECDSA_P192
474 | TEE_ALG_ECDSA_P224
475 | TEE_ALG_ECDSA_P256
476 | TEE_ALG_ECDSA_P384
477 | TEE_ALG_ECDSA_P521
478 | TEE_ALG_SM2_DSA_SM3
479 | TEE_ALG_RSASSA_PKCS1_V1_5_MD5
480 | TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1
481 | TEE_ALG_RSASSA_PKCS1_V1_5_SHA1
482 | TEE_ALG_RSASSA_PKCS1_V1_5_SHA224
483 | TEE_ALG_RSASSA_PKCS1_V1_5_SHA256
484 | TEE_ALG_RSASSA_PKCS1_V1_5_SHA384
485 | TEE_ALG_RSASSA_PKCS1_V1_5_SHA512
486 | TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1
487 | TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224
488 | TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256
489 | TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384
490 | TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 => match mode {
491 TEE_MODE_SIGN => Ok(OperationConfig::AsymmetricSignature {
492 req_key_usage: TEE_USAGE_SIGN,
493 with_private_key: true,
494 }),
495 TEE_MODE_VERIFY => Ok(OperationConfig::AsymmetricSignature {
496 req_key_usage: TEE_USAGE_VERIFY,
497 with_private_key: false,
498 }),
499 _ => Err(TEE_ERROR_NOT_SUPPORTED),
500 },
501
502 TEE_ALG_RSAES_PKCS1_V1_5
504 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1
505 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224
506 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256
507 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384
508 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512
509 | TEE_ALG_SM2_PKE => match mode {
510 TEE_MODE_ENCRYPT => Ok(OperationConfig::AsymmetricEncryption {
511 req_key_usage: TEE_USAGE_ENCRYPT,
512 with_private_key: false,
513 }),
514 TEE_MODE_DECRYPT => Ok(OperationConfig::AsymmetricEncryption {
515 req_key_usage: TEE_USAGE_DECRYPT,
516 with_private_key: true,
517 }),
518 _ => Err(TEE_ERROR_NOT_SUPPORTED),
519 },
520
521 TEE_ALG_RSA_NOPAD => match mode {
523 TEE_MODE_ENCRYPT => Ok(OperationConfig::AsymmetricEncryption {
524 req_key_usage: TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY,
525 with_private_key: false,
526 }),
527 TEE_MODE_DECRYPT => Ok(OperationConfig::AsymmetricEncryption {
528 req_key_usage: TEE_USAGE_DECRYPT | TEE_USAGE_SIGN,
529 with_private_key: true,
530 }),
531 _ => Err(TEE_ERROR_NOT_SUPPORTED),
532 },
533
534 TEE_ALG_DH_DERIVE_SHARED_SECRET
536 | TEE_ALG_ECDH_P192
537 | TEE_ALG_ECDH_P224
538 | TEE_ALG_ECDH_P256
539 | TEE_ALG_ECDH_P384
540 | TEE_ALG_ECDH_P521
541 | TEE_ALG_SM2_KEP => {
542 if mode != TEE_MODE_DERIVE {
543 return Err(TEE_ERROR_NOT_SUPPORTED);
544 }
545 Ok(OperationConfig::KeyDerivation {
546 req_key_usage: TEE_USAGE_DERIVE,
547 })
548 }
549
550 _ => Err(TEE_ERROR_NOT_SUPPORTED),
551 }
552}
553
554pub fn tee_alg_get_class(algo: u32) -> u32 {
556 match algo {
557 TEE_ALG_SM2_PKE => TEE_OPERATION_ASYMMETRIC_CIPHER,
558 TEE_ALG_SM2_KEP => TEE_OPERATION_KEY_DERIVATION,
559 TEE_ALG_RSASSA_PKCS1_V1_5_MD5
560 | TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1
561 | TEE_ALG_RSASSA_PKCS1_V1_5_SHA1
562 | TEE_ALG_RSASSA_PKCS1_V1_5_SHA224
563 | TEE_ALG_RSASSA_PKCS1_V1_5_SHA256
564 | TEE_ALG_RSASSA_PKCS1_V1_5_SHA384
565 | TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 => TEE_OPERATION_ASYMMETRIC_SIGNATURE,
566 TEE_ALG_DES3_CBC_MAC_NOPAD | TEE_ALG_DES3_CBC_MAC_PKCS5 => TEE_OPERATION_MAC,
567 TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1
568 | TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224
569 | TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256
570 | TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384
571 | TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 => TEE_OPERATION_ASYMMETRIC_SIGNATURE,
572 TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1
573 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224
574 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256
575 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384
576 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512 => TEE_OPERATION_ASYMMETRIC_CIPHER,
577 _ => (algo >> 28) & 0xF,
578 }
579}
580
581#[unsafe(no_mangle)]
611pub extern "C" fn TEE_AllocateOperation(
612 operation: *mut *mut TEE_OperationHandle,
613 algorithm: u32,
614 mode: u32,
615 max_key_size: u32,
616) -> TEE_Result {
617 use crate::tee_api_defines::*;
618
619 if operation.is_null() {
621 TEE_Panic(0);
622 return TEE_ERROR_GENERIC;
623 }
624
625 let config = match validate_algorithm_params(algorithm, mode, max_key_size) {
627 Ok(c) => c,
628 Err(e) => return e,
629 };
630
631 let operation_class = tee_alg_get_class(algorithm);
633 let digest_length = match algorithm & 0x000000FF {
634 0x01 => 16, 0x02 => 20, 0x03 => 28, 0x04 => 32, 0x05 => 48, 0x06 => 64, 0x07 => 32, _ => 0,
642 };
643 let _main_alg = (algorithm & 0x00FF0000) >> 16;
644
645 let mut handle_state = 0u32;
646
647 if algorithm == TEE_ALG_SM2_KEP {
649 handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
650 }
651
652 let (block_size, buffer_two_blocks, req_key_usage, _) = match config {
653 OperationConfig::Digest { block_size } => {
654 handle_state |= TEE_HANDLE_FLAG_KEY_SET;
655 (block_size, false, 0, false)
656 }
657 OperationConfig::Cipher {
658 block_size,
659 buffer_two_blocks,
660 req_key_usage,
661 with_private_key,
662 } => (
663 block_size,
664 buffer_two_blocks,
665 req_key_usage,
666 with_private_key,
667 ),
668 OperationConfig::AsymmetricSignature {
669 req_key_usage,
670 with_private_key,
671 } => (1, false, req_key_usage, with_private_key),
672 OperationConfig::AsymmetricEncryption {
673 req_key_usage,
674 with_private_key,
675 } => (1, false, req_key_usage, with_private_key),
676 OperationConfig::KeyDerivation { req_key_usage } => (1, false, req_key_usage, true),
677 OperationConfig::Mac { req_key_usage } => (1, false, req_key_usage, false),
678 };
679
680 let op_info = TEE_OperationInfo {
682 algorithm,
683 operationClass: operation_class,
684 mode,
685 digestLength: digest_length,
686 maxKeySize: max_key_size,
687 keySize: 0,
688 requiredKeyUsage: req_key_usage,
689 handleState: handle_state,
690 };
691
692 let operation_ptr = TEE_Malloc(
694 core::mem::size_of::<TEE_OperationHandle>(),
695 TEE_MALLOC_FILL_ZERO,
696 );
697 if operation_ptr.is_null() {
698 return TEE_ERROR_OUT_OF_MEMORY;
699 }
700
701 let op_handle = unsafe { &mut *(operation_ptr as *mut TEE_OperationHandle) };
703 *op_handle = TEE_OperationHandle::new(
704 op_info,
705 ptr::null_mut(),
706 ptr::null_mut(),
707 TEE_OPERATION_STATE_INITIAL,
708 block_size,
709 0,
710 );
711
712 if block_size > 1 {
714 let buffer_size = if buffer_two_blocks {
715 block_size * 2
716 } else {
717 block_size
718 };
719 if op_handle.allocate_buffer_v2(buffer_size).is_err() {
720 TEE_Free(operation_ptr);
721 return TEE_ERROR_OUT_OF_MEMORY;
722 }
723 }
724 op_handle.buffer_two_blocks = buffer_two_blocks;
725
726 if operation_class != TEE_OPERATION_DIGEST {
728 let mut mks = max_key_size;
729 let res = TEE_ALG_GET_KEY_TYPE(algorithm, mode);
730 let (key_type, key_type2) = match res {
731 Ok(res) => res,
732 Err(e) => return e,
733 };
734
735 if handle_state & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS != 0 {
736 mks /= 2;
737 }
738
739 let mut key1_ptr = core::ptr::null_mut();
740 let res = TEE_AllocateTransientObject(key_type, mks, &mut key1_ptr);
741 if res != TEE_SUCCESS {
742 TEE_Free(operation_ptr);
743 return res;
744 }
745 op_handle.key1 = key1_ptr;
746
747 if handle_state & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS != 0 {
748 let mut key2_ptr = core::ptr::null_mut();
749 let res = TEE_AllocateTransientObject(key_type2, mks, &mut key2_ptr);
750 if res != TEE_SUCCESS {
751 TEE_FreeTransientObject(op_handle.key1);
752 TEE_Free(operation_ptr);
753 return res;
754 }
755 op_handle.key2 = key2_ptr;
756 }
757 }
758
759 let mut state = 0u32;
761 let res = unsafe {
762 _utee_cryp_state_alloc(
763 algorithm as u64,
764 mode as u64,
765 op_handle.key1 as u64,
766 op_handle.key2 as u64,
767 &mut state,
768 )
769 };
770 if res != TEE_SUCCESS as usize {
771 TEE_FreeTransientObject(op_handle.key1);
772 TEE_FreeTransientObject(op_handle.key2);
773 TEE_Free(operation_ptr);
774 return res as TEE_Result;
775 }
776 op_handle.state = state;
777
778 if operation_class == TEE_OPERATION_DIGEST {
780 let res = unsafe { _utee_hash_init(state as u64, core::ptr::null(), 0) };
781 if res != TEE_SUCCESS as usize {
782 TEE_FreeTransientObject(op_handle.key1);
783 TEE_FreeTransientObject(op_handle.key2);
784 TEE_Free(operation_ptr);
785 return res as TEE_Result;
786 }
787 op_handle.operation_state |= TEE_HANDLE_FLAG_INITIALIZED;
788 }
789
790 op_handle.operation_state = TEE_OPERATION_STATE_INITIAL;
791
792 unsafe {
794 *operation = operation_ptr as *mut TEE_OperationHandle;
795 }
796
797 TEE_SUCCESS
798}
799
800#[unsafe(no_mangle)]
804pub extern "C" fn TEE_FreeOperation(operation: *mut TEE_OperationHandle) {
805 use crate::tee_api_defines::*;
806
807 if operation.is_null() {
809 return;
810 }
811
812 let op_handle = unsafe { &*operation };
814
815 if !op_handle.buffer.is_null() {
817 TEE_Free(op_handle.buffer as *mut core::ffi::c_void);
818 }
819 let res = unsafe { _utee_cryp_state_free(op_handle.state as u64) };
826 if res != TEE_SUCCESS as usize {
827 TEE_Panic(res as u32);
828 }
829}
830
831#[unsafe(no_mangle)]
840pub extern "C" fn TEE_GetOperationInfo(
841 operation: *mut TEE_OperationHandle,
842 operationInfo: *mut TEE_OperationInfo,
843) {
844 use crate::tee_api_defines::*;
845
846 if operation.is_null() {
848 TEE_Panic(0);
849 }
850
851 if operationInfo.is_null() {
853 TEE_Panic(0);
854 }
855
856 if cfg!(feature = "strict_annotation_checks") {
858 let res = TEE_CheckMemoryAccessRights(
859 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
860 operationInfo as *mut core::ffi::c_void,
861 std::mem::size_of::<TEE_OperationInfo>(),
862 );
863 if res != 0 {
864 eprintln!("[out] operationInfo: error {:#010x}", res);
865 TEE_Panic(0);
866 }
867 }
868
869 let op_handle = unsafe { &*operation };
871 unsafe {
872 *operationInfo = op_handle.info;
873 }
874
875 unsafe {
877 if (*operationInfo).handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS != 0 {
878 (*operationInfo).keySize = 0;
879 (*operationInfo).requiredKeyUsage = 0;
880 }
881 }
882}
883
884#[unsafe(no_mangle)]
896pub extern "C" fn TEE_GetOperationInfoMultiple(
897 operation: *mut TEE_OperationHandle,
898 op_info: *mut TEE_OperationInfoMultiple,
899 size: *mut usize,
900) -> TEE_Result {
901 use crate::tee_api_defines::*;
902
903 if operation.is_null() {
905 return TEE_ERROR_BAD_PARAMETERS;
906 }
907
908 if op_info.is_null() || size.is_null() {
910 TEE_Panic(0);
911 return TEE_ERROR_BAD_PARAMETERS;
912 }
913
914 if cfg!(feature = "strict_annotation_checks") {
916 let buffer_size = unsafe { *size };
917 let res = TEE_CheckMemoryAccessRights(
918 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
919 op_info as *mut core::ffi::c_void,
920 buffer_size,
921 );
922 if res != 0 {
923 eprintln!("[out] op_info: error {:#010x}", res);
924 TEE_Panic(0);
925 }
926
927 let res = TEE_CheckMemoryAccessRights(
928 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
929 size as *mut core::ffi::c_void,
930 std::mem::size_of::<usize>(),
931 );
932 if res != 0 {
933 eprintln!("[out] size: error {:#010x}", res);
934 TEE_Panic(0);
935 }
936 }
937
938 let op_info_size = std::mem::size_of::<TEE_OperationInfoMultiple>();
940 let buffer_size = unsafe { *size };
941 if buffer_size < op_info_size {
942 return TEE_ERROR_BAD_PARAMETERS;
943 }
944
945 let key_info_size = std::mem::size_of::<TEE_OperationInfoKey>();
947 let max_key_count = (buffer_size - op_info_size) / key_info_size;
948
949 TEE_MemFill(op_info as *mut core::ffi::c_void, 0, buffer_size);
951
952 let op_handle = unsafe { &mut *operation };
954 let two_keys = (op_handle.info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 0;
955
956 let result = if op_handle.info.mode == TEE_MODE_DIGEST {
957 unsafe {
959 (*op_info).numberOfKeys = 0;
960 }
961 TEE_SUCCESS
962 } else if !two_keys {
963 if max_key_count < 1 {
965 return TEE_ERROR_SHORT_BUFFER;
966 }
967
968 let mut kinfo = TEE_ObjectInfo {
969 objectType: 0,
970 objectSize: 0,
971 maxObjectSize: 0,
972 objectUsage: 0,
973 dataSize: 0,
974 dataPosition: 0,
975 handleFlags: 0,
976 };
977
978 let res = TEE_GetObjectInfo1(op_handle.key1, &mut kinfo);
979 if res != TEE_SUCCESS {
980 return check_operation_info_multiple_result(res);
981 }
982
983 unsafe {
984 (*op_info)
985 .keyInformation
986 .add(0)
987 .write(TEE_OperationInfoKey {
988 keySize: kinfo.objectSize,
989 requiredKeyUsage: op_handle.info.requiredKeyUsage,
990 });
991 (*op_info).numberOfKeys = 1;
992 }
993
994 TEE_SUCCESS
995 } else {
996 if max_key_count < 2 {
998 return TEE_ERROR_SHORT_BUFFER;
999 }
1000
1001 let mut kinfo = TEE_ObjectInfo {
1002 objectType: 0,
1003 objectSize: 0,
1004 maxObjectSize: 0,
1005 objectUsage: 0,
1006 dataSize: 0,
1007 dataPosition: 0,
1008 handleFlags: 0,
1009 };
1010
1011 let res = TEE_GetObjectInfo1(op_handle.key1, &mut kinfo);
1013 if res != TEE_SUCCESS {
1014 return check_operation_info_multiple_result(res);
1015 }
1016
1017 unsafe {
1018 (*op_info)
1019 .keyInformation
1020 .add(0)
1021 .write(TEE_OperationInfoKey {
1022 keySize: kinfo.objectSize,
1023 requiredKeyUsage: op_handle.info.requiredKeyUsage,
1024 });
1025 }
1026
1027 let res = TEE_GetObjectInfo1(op_handle.key2, &mut kinfo);
1029 if res != TEE_SUCCESS {
1030 return check_operation_info_multiple_result(res);
1031 }
1032
1033 unsafe {
1034 (*op_info)
1035 .keyInformation
1036 .add(1)
1037 .write(TEE_OperationInfoKey {
1038 keySize: kinfo.objectSize,
1039 requiredKeyUsage: op_handle.info.requiredKeyUsage,
1040 });
1041 (*op_info).numberOfKeys = 2;
1042 }
1043
1044 TEE_SUCCESS
1045 };
1046
1047 if result == TEE_SUCCESS {
1049 unsafe {
1050 (*op_info).algorithm = op_handle.info.algorithm;
1051 (*op_info).operationClass = op_handle.info.operationClass;
1052 (*op_info).mode = op_handle.info.mode;
1053 (*op_info).digestLength = op_handle.info.digestLength;
1054 (*op_info).maxKeySize = op_handle.info.maxKeySize;
1055 (*op_info).handleState = op_handle.info.handleState;
1056 (*op_info).operationState = op_handle.operation_state;
1057 }
1058 }
1059
1060 check_operation_info_multiple_result(result)
1061}
1062
1063fn check_operation_info_multiple_result(res: TEE_Result) -> TEE_Result {
1065 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
1066 TEE_Panic(res as u32);
1067 }
1068 res
1069}
1070
1071fn reset_operation_state(operation: &mut TEE_OperationHandle) {
1083 operation.operation_state = TEE_OPERATION_STATE_INITIAL;
1085 operation.buffer_offs = 0;
1087
1088 if operation.info.operationClass == TEE_OPERATION_DIGEST {
1089 let res = unsafe { _utee_hash_init(operation.state as u64, core::ptr::null(), 0) };
1090 if res != TEE_SUCCESS as usize {
1091 TEE_Panic(res as u32);
1092 }
1093 operation.info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1094 } else {
1095 operation.info.handleState &= !TEE_HANDLE_FLAG_INITIALIZED;
1096 }
1097}
1098
1099#[unsafe(no_mangle)]
1107pub extern "C" fn TEE_ResetOperation(operation: *mut TEE_OperationHandle) {
1108 if operation.is_null() {
1110 TEE_Panic(TEE_PANIC_ID_TEE_RESETOPERATION);
1111 }
1112 let op_handle = unsafe { &*operation };
1113 if (op_handle.info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0 {
1114 TEE_Panic(0);
1115 }
1116 unsafe {
1118 reset_operation_state(&mut *operation);
1119 }
1120}
1121
1122#[unsafe(no_mangle)]
1138pub extern "C" fn TEE_SetOperationKey(
1139 operation: *mut TEE_OperationHandle,
1140 key: TEE_ObjectHandle,
1141) -> TEE_Result {
1142 if operation.is_null() {
1144 TEE_Panic(TEE_PANIC_ID_TEE_SETOPERATIONKEY);
1145 return TEE_ERROR_BAD_PARAMETERS;
1146 }
1147
1148 let op_handle = unsafe { &mut *operation };
1150
1151 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) != 0 {
1152 TEE_Panic(TEE_PANIC_ID_TEE_SETOPERATIONKEY);
1153 }
1154
1155 set_operation_key(op_handle, key)
1156}
1157
1158fn set_operation_key(op_handle: &mut TEE_OperationHandle, key: TEE_ObjectHandle) -> TEE_Result {
1160 if key.is_null() {
1162 TEE_ResetTransientObject(op_handle.key1);
1164 op_handle.info.handleState &= !TEE_HANDLE_FLAG_KEY_SET;
1165
1166 if op_handle.operation_state != TEE_OPERATION_STATE_INITIAL {
1168 reset_operation_state(op_handle);
1169 }
1170 return TEE_SUCCESS;
1171 }
1172
1173 if op_handle.info.operationClass == TEE_OPERATION_DIGEST {
1175 TEE_Panic(TEE_PANIC_ID_TEE_SETOPERATIONKEY);
1176 return TEE_ERROR_BAD_PARAMETERS;
1177 }
1178
1179 if (op_handle.info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 0 {
1181 TEE_Panic(TEE_PANIC_ID_TEE_SETOPERATIONKEY);
1182 return TEE_ERROR_BAD_PARAMETERS;
1183 }
1184
1185 let mut key_info = TEE_ObjectInfo {
1187 objectType: 0,
1188 objectSize: 0,
1189 maxObjectSize: 0,
1190 objectUsage: 0,
1191 dataSize: 0,
1192 dataPosition: 0,
1193 handleFlags: 0,
1194 };
1195
1196 let res = TEE_GetObjectInfo1(key, &mut key_info);
1197 if res != TEE_SUCCESS {
1198 TEE_Panic(TEE_PANIC_ID_TEE_SETOPERATIONKEY);
1199 return TEE_ERROR_BAD_PARAMETERS;
1200 }
1201
1202 if (key_info.objectUsage & op_handle.info.requiredKeyUsage) != op_handle.info.requiredKeyUsage {
1204 TEE_Panic(TEE_PANIC_ID_TEE_SETOPERATIONKEY);
1205 return TEE_ERROR_SECURITY;
1206 }
1207
1208 if op_handle.info.maxKeySize < key_info.objectSize {
1210 TEE_Panic(TEE_PANIC_ID_TEE_SETOPERATIONKEY);
1211 return TEE_ERROR_BAD_PARAMETERS;
1212 }
1213
1214 TEE_ResetTransientObject(op_handle.key1);
1216 op_handle.info.handleState &= !TEE_HANDLE_FLAG_KEY_SET;
1217
1218 let res = TEE_CopyObjectAttributes1(op_handle.key1, key);
1220 if res != TEE_SUCCESS {
1221 TEE_Panic(TEE_PANIC_ID_TEE_SETOPERATIONKEY);
1222 return TEE_ERROR_BAD_PARAMETERS;
1223 }
1224
1225 op_handle.info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
1227
1228 op_handle.info.keySize = key_info.objectSize;
1230
1231 if op_handle.operation_state != TEE_OPERATION_STATE_INITIAL {
1233 reset_operation_state(op_handle);
1234 }
1235
1236 TEE_SUCCESS
1237}
1238
1239#[unsafe(no_mangle)]
1255pub extern "C" fn TEE_SetOperationKey2(
1256 operation: *mut TEE_OperationHandle,
1257 key1: TEE_ObjectHandle,
1258 key2: TEE_ObjectHandle,
1259) -> TEE_Result {
1260 if operation.is_null() {
1262 return TEE_ERROR_BAD_PARAMETERS;
1263 }
1264
1265 let op_handle = unsafe { &mut *operation };
1266
1267 if !operation.is_null() && !key1.is_null() && !key2.is_null() && key1 == key2 {
1269 return TEE_ERROR_SECURITY;
1270 }
1271
1272 if key1.is_null() && key2.is_null() {
1274 TEE_ResetTransientObject(op_handle.key1);
1276 TEE_ResetTransientObject(op_handle.key2);
1277 op_handle.info.handleState &= !TEE_HANDLE_FLAG_KEY_SET;
1278 if op_handle.operation_state != TEE_OPERATION_STATE_INITIAL {
1279 reset_operation_state(op_handle);
1280 }
1281 return TEE_SUCCESS;
1282 } else if key1.is_null() || key2.is_null() {
1283 return TEE_ERROR_BAD_PARAMETERS;
1285 }
1286
1287 if op_handle.info.operationClass == TEE_OPERATION_DIGEST {
1289 return TEE_ERROR_BAD_PARAMETERS;
1290 }
1291
1292 if (op_handle.info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0 {
1294 return TEE_ERROR_BAD_PARAMETERS;
1295 }
1296
1297 let mut key_info1 = TEE_ObjectInfo {
1299 objectType: 0,
1300 objectSize: 0,
1301 maxObjectSize: 0,
1302 objectUsage: 0,
1303 dataSize: 0,
1304 dataPosition: 0,
1305 handleFlags: 0,
1306 };
1307
1308 let mut res = TEE_GetObjectInfo1(key1, &mut key_info1);
1309 if res != TEE_SUCCESS {
1310 return handle_result_error(res);
1311 }
1312
1313 if (key_info1.objectUsage & op_handle.info.requiredKeyUsage) != op_handle.info.requiredKeyUsage
1315 {
1316 return TEE_ERROR_BAD_PARAMETERS;
1317 }
1318
1319 let mut key_info2 = TEE_ObjectInfo {
1321 objectType: 0,
1322 objectSize: 0,
1323 maxObjectSize: 0,
1324 objectUsage: 0,
1325 dataSize: 0,
1326 dataPosition: 0,
1327 handleFlags: 0,
1328 };
1329
1330 res = TEE_GetObjectInfo1(key2, &mut key_info2);
1331 if res != TEE_SUCCESS {
1332 return if res == TEE_ERROR_CORRUPT_OBJECT {
1333 TEE_ERROR_CORRUPT_OBJECT_2
1334 } else {
1335 handle_result_error(res)
1336 };
1337 }
1338
1339 if (key_info2.objectUsage & op_handle.info.requiredKeyUsage) != op_handle.info.requiredKeyUsage
1341 {
1342 return TEE_ERROR_BAD_PARAMETERS;
1343 }
1344
1345 if key_info1.objectSize != key_info2.objectSize {
1347 return TEE_ERROR_BAD_PARAMETERS;
1348 }
1349
1350 if op_handle.info.maxKeySize < key_info1.objectSize {
1352 return TEE_ERROR_BAD_PARAMETERS;
1353 }
1354
1355 TEE_ResetTransientObject(op_handle.key1);
1357 TEE_ResetTransientObject(op_handle.key2);
1358 op_handle.info.handleState &= !TEE_HANDLE_FLAG_KEY_SET;
1359
1360 res = TEE_CopyObjectAttributes1(op_handle.key1, key1);
1362 if res != TEE_SUCCESS {
1363 return handle_result_error(res);
1364 }
1365
1366 res = TEE_CopyObjectAttributes1(op_handle.key2, key2);
1367 if res != TEE_SUCCESS {
1368 return if res == TEE_ERROR_CORRUPT_OBJECT {
1369 TEE_ERROR_CORRUPT_OBJECT_2
1370 } else {
1371 handle_result_error(res)
1372 };
1373 }
1374
1375 op_handle.info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
1377
1378 op_handle.info.keySize = key_info1.objectSize;
1380
1381 if op_handle.operation_state != TEE_OPERATION_STATE_INITIAL {
1383 reset_operation_state(op_handle);
1384 }
1385
1386 TEE_SUCCESS
1387}
1388
1389fn handle_result_error(res: TEE_Result) -> TEE_Result {
1391 match res {
1392 TEE_SUCCESS => TEE_SUCCESS,
1393 TEE_ERROR_CORRUPT_OBJECT => TEE_ERROR_CORRUPT_OBJECT,
1394 TEE_ERROR_CORRUPT_OBJECT_2 => TEE_ERROR_CORRUPT_OBJECT_2,
1395 TEE_ERROR_STORAGE_NOT_AVAILABLE => TEE_ERROR_STORAGE_NOT_AVAILABLE,
1396 TEE_ERROR_STORAGE_NOT_AVAILABLE_2 => TEE_ERROR_STORAGE_NOT_AVAILABLE_2,
1397 _ => {
1398 TEE_Panic(res as u32);
1399 res
1400 }
1401 }
1402}
1403
1404#[unsafe(no_mangle)]
1417pub extern "C" fn TEE_CopyOperation(
1418 dst_op: *mut TEE_OperationHandle,
1419 src_op: *mut TEE_OperationHandle,
1420) {
1421 if dst_op.is_null() || src_op.is_null() {
1423 TEE_Panic(0);
1424 }
1425
1426 let (dst_ref, src_ref) = unsafe { (&mut *dst_op, &*src_op) };
1428
1429 if dst_ref.info.algorithm != src_ref.info.algorithm {
1431 TEE_Panic(0);
1432 }
1433 if dst_ref.info.mode != src_ref.info.mode {
1434 TEE_Panic(0);
1435 }
1436
1437 if src_ref.info.operationClass != TEE_OPERATION_DIGEST {
1439 let (key1, key2) = if (src_ref.info.handleState & TEE_HANDLE_FLAG_KEY_SET) != 0 {
1440 (src_ref.key1, src_ref.key2)
1441 } else {
1442 (ptr::null_mut(), ptr::null_mut())
1443 };
1444
1445 if (src_ref.info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0 {
1446 let res = set_operation_key(dst_ref, key1);
1447 if res != TEE_SUCCESS {
1448 TEE_Panic(res as u32);
1449 }
1450 } else {
1451 let res = TEE_SetOperationKey2(dst_op, key1, key2);
1453 if res != TEE_SUCCESS {
1454 TEE_Panic(res as u32);
1455 }
1456 }
1457 }
1458
1459 dst_ref.info.handleState = src_ref.info.handleState;
1461 dst_ref.info.keySize = src_ref.info.keySize;
1462 dst_ref.info.digestLength = src_ref.info.digestLength;
1463 dst_ref.operation_state = src_ref.operation_state;
1464
1465 if dst_ref.buffer_two_blocks != src_ref.buffer_two_blocks
1467 || dst_ref.block_size != src_ref.block_size
1468 {
1469 TEE_Panic(0);
1470 }
1471
1472 if !dst_ref.buffer.is_null() {
1474 if src_ref.buffer.is_null() {
1475 TEE_Panic(0);
1476 }
1477
1478 let sz = if src_ref.buffer_two_blocks {
1479 src_ref.block_size * 2
1480 } else {
1481 src_ref.block_size
1482 };
1483
1484 unsafe {
1486 std::ptr::copy_nonoverlapping(src_ref.buffer, dst_ref.buffer, sz);
1487 }
1488 dst_ref.buffer_offs = src_ref.buffer_offs;
1489 } else if !src_ref.buffer.is_null() {
1490 TEE_Panic(0);
1491 }
1492
1493 let res = unsafe { _utee_cryp_state_copy(dst_ref.state as u64, src_ref.state as u64) };
1495 if res != TEE_SUCCESS as usize {
1496 TEE_Panic(res as u32);
1497 }
1498}
1499
1500fn init_hash_operation(
1511 operation: *mut TEE_OperationHandle,
1512 iv: *const core::ffi::c_void,
1513 iv_len: u32,
1514) {
1515 if operation.is_null() {
1517 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
1518 return;
1519 }
1520
1521 let res = unsafe { _utee_hash_init((*operation).state as u64, iv, iv_len as usize) };
1522 if res != TEE_SUCCESS as usize {
1523 TEE_Panic(res as u32);
1524 }
1525
1526 unsafe {
1528 (*operation).buffer_offs = 0;
1529 (*operation).info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1531 }
1532}
1533
1534#[unsafe(no_mangle)]
1548pub extern "C" fn TEE_DigestUpdate(
1549 operation: *mut TEE_OperationHandle,
1550 chunk: *const core::ffi::c_void,
1551 chunk_size: usize,
1552) {
1553 if operation.is_null() {
1555 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
1556 return;
1557 }
1558
1559 let op_handle = unsafe { &mut *operation };
1561
1562 if op_handle.info.operationClass != TEE_OPERATION_DIGEST {
1563 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
1564 return;
1565 }
1566
1567 op_handle.operation_state = TEE_OPERATION_STATE_ACTIVE;
1569
1570 let res = unsafe { _utee_hash_update(op_handle.state as u64, chunk, chunk_size) };
1572
1573 if res != TEE_SUCCESS as usize {
1574 TEE_Panic(res as u32);
1575 }
1576}
1577
1578#[unsafe(no_mangle)]
1592pub extern "C" fn TEE_DigestDoFinal(
1593 operation: *mut TEE_OperationHandle,
1594 chunk: *const core::ffi::c_void,
1595 chunk_len: usize,
1596 hash: *mut core::ffi::c_void,
1597 hash_len: *mut usize,
1598) -> TEE_Result {
1599 if operation.is_null() {
1601 return TEE_ERROR_BAD_PARAMETERS;
1602 }
1603
1604 if chunk.is_null() && chunk_len > 0 {
1606 return TEE_ERROR_BAD_PARAMETERS;
1607 }
1608
1609 let op_handle = unsafe { &mut *operation };
1611
1612 if op_handle.info.operationClass != TEE_OPERATION_DIGEST {
1614 return TEE_ERROR_BAD_PARAMETERS;
1615 }
1616
1617 if op_handle.operation_state == TEE_OPERATION_STATE_EXTRACTING && chunk_len > 0 {
1619 return TEE_ERROR_BAD_PARAMETERS;
1620 }
1621
1622 if hash_len.is_null() {
1624 TEE_Panic(0);
1625 return TEE_ERROR_BAD_PARAMETERS;
1626 }
1627
1628 if cfg!(feature = "strict_annotation_checks") {
1630 let res = TEE_CheckMemoryAccessRights(
1631 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
1632 hash_len as *mut core::ffi::c_void,
1633 std::mem::size_of::<usize>(),
1634 );
1635 if res != 0 {
1636 eprintln!("[inout] hash_len: error {:#010x}", res);
1637 TEE_Panic(0);
1638 return TEE_ERROR_BAD_PARAMETERS;
1639 }
1640 }
1641
1642 let res = if op_handle.operation_state == TEE_OPERATION_STATE_EXTRACTING
1643 && !op_handle.buffer.is_null()
1644 {
1645 let len = std::cmp::min(op_handle.block_size - op_handle.buffer_offs, unsafe {
1650 *hash_len
1651 });
1652 unsafe {
1653 std::ptr::copy_nonoverlapping(
1654 op_handle.buffer.add(op_handle.buffer_offs),
1655 hash as *mut u8,
1656 len,
1657 );
1658 *hash_len = len;
1659 }
1660 TEE_SUCCESS
1661 } else {
1662 let mut hl = unsafe { *hash_len as u64 };
1663 let res =
1664 unsafe { _utee_hash_final(op_handle.state as u64, chunk, chunk_len, hash, &mut hl) };
1665 unsafe {
1666 *hash_len = hl as usize;
1667 }
1668
1669 if res != TEE_SUCCESS as usize {
1670 return res as TEE_Result;
1671 }
1672
1673 TEE_SUCCESS
1674 };
1675
1676 init_hash_operation(operation, core::ptr::null(), 0);
1678
1679 unsafe {
1681 (*operation).operation_state = TEE_OPERATION_STATE_INITIAL;
1682 }
1683
1684 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
1686 TEE_Panic(res as u32);
1687 }
1688
1689 res
1690}
1691
1692#[unsafe(no_mangle)]
1702pub extern "C" fn TEE_DigestExtract(
1703 operation: *mut TEE_OperationHandle,
1704 hash: *mut core::ffi::c_void,
1705 hash_len: *mut usize,
1706) -> TEE_Result {
1707 if operation.is_null() {
1708 TEE_Panic(0);
1709 }
1710
1711 let op_handle = unsafe { &mut *operation };
1712 if op_handle.info.operationClass != TEE_OPERATION_DIGEST {
1713 TEE_Panic(0);
1714 }
1715
1716 if hash_len.is_null() {
1718 TEE_Panic(0);
1719 return TEE_ERROR_BAD_PARAMETERS;
1720 }
1721
1722 if cfg!(feature = "strict_annotation_checks") {
1724 let res = TEE_CheckMemoryAccessRights(
1725 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
1726 hash_len as *mut core::ffi::c_void,
1727 std::mem::size_of::<usize>(),
1728 );
1729 if res != 0 {
1730 eprintln!("[inout] hash_len: error {:#010x}", res);
1731 TEE_Panic(0);
1732 return TEE_ERROR_BAD_PARAMETERS;
1733 }
1734 }
1735
1736 if op_handle.buffer.is_null() {
1738 unsafe {
1740 (*operation).info.handleState |= TEE_HANDLE_FLAG_EXTRACTING;
1741 (*operation).operation_state = TEE_OPERATION_STATE_EXTRACTING;
1742 }
1743
1744 let mut hl = unsafe { *hash_len as u64 };
1745 let res = unsafe {
1746 _utee_hash_final(op_handle.state as u64, core::ptr::null(), 0, hash, &mut hl)
1747 };
1748 if res != TEE_SUCCESS as usize {
1749 TEE_Panic(0);
1750 return TEE_ERROR_BAD_PARAMETERS;
1751 }
1752
1753 unsafe {
1754 *hash_len = hl as usize;
1755 }
1756
1757 return TEE_SUCCESS;
1758 }
1759
1760 if op_handle.operation_state != TEE_OPERATION_STATE_EXTRACTING {
1762 let mut hl = op_handle.block_size as u64;
1763 let res = unsafe {
1764 _utee_hash_final(
1765 op_handle.state as u64,
1766 core::ptr::null(),
1767 0,
1768 op_handle.buffer as *mut core::ffi::c_void,
1769 &mut hl,
1770 )
1771 };
1772 if res != TEE_SUCCESS as usize {
1773 TEE_Panic(0);
1774 return TEE_ERROR_BAD_PARAMETERS;
1775 }
1776
1777 if hl as usize != op_handle.block_size {
1778 TEE_Panic(0);
1779 return TEE_ERROR_BAD_PARAMETERS;
1780 }
1781
1782 debug_assert!(op_handle.buffer_offs == 0, "buffer_offs should be 0");
1783
1784 unsafe {
1785 (*operation).info.handleState |= TEE_HANDLE_FLAG_EXTRACTING;
1786 (*operation).operation_state = TEE_OPERATION_STATE_EXTRACTING;
1787 }
1788 }
1789
1790 let len = std::cmp::min(op_handle.block_size - op_handle.buffer_offs, unsafe {
1792 *hash_len
1793 });
1794
1795 unsafe {
1797 std::ptr::copy_nonoverlapping(
1798 op_handle.buffer.add(op_handle.buffer_offs),
1799 hash as *mut u8,
1800 len,
1801 );
1802 *hash_len = len;
1803 }
1804
1805 unsafe {
1807 (*operation).buffer_offs += len;
1808 }
1809
1810 TEE_SUCCESS
1811}
1812
1813#[unsafe(no_mangle)]
1824pub extern "C" fn TEE_CipherInit(
1825 operation: *mut TEE_OperationHandle,
1826 iv: *const core::ffi::c_void,
1827 iv_len: usize,
1828) {
1829 use crate::tee_api_defines::*;
1830
1831 if operation.is_null() {
1833 TEE_Panic(0);
1834 return;
1835 }
1836
1837 let op_handle = unsafe { &mut *operation };
1839
1840 if op_handle.info.operationClass != TEE_OPERATION_CIPHER {
1842 TEE_Panic(0);
1843 return;
1844 }
1845
1846 if (op_handle.info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0 || op_handle.key1.is_null() {
1848 TEE_Panic(0);
1849 return;
1850 }
1851
1852 if op_handle.operation_state != TEE_OPERATION_STATE_INITIAL {
1854 TEE_ResetOperation(operation);
1855 }
1856
1857 if !iv.is_null() && iv_len > 0 {
1859 match op_handle.info.algorithm {
1860 TEE_ALG_AES_ECB_NOPAD
1861 | TEE_ALG_DES_ECB_NOPAD
1862 | TEE_ALG_DES3_ECB_NOPAD
1863 | TEE_ALG_SM4_ECB_NOPAD => {
1864 TEE_Panic(0);
1865 return;
1866 }
1867 _ => {}
1868 }
1869 }
1870
1871 unsafe {
1873 (*operation).operation_state = TEE_OPERATION_STATE_ACTIVE;
1874 }
1875
1876 let res = unsafe { _utee_cipher_init(op_handle.state as u64, iv, iv_len) };
1878 if res != TEE_SUCCESS as usize {
1879 TEE_Panic(res as u32);
1880 return;
1881 }
1882
1883 unsafe {
1885 (*operation).buffer_offs = 0;
1886 (*operation).info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1887 }
1888}
1889
1890const fn roundup2(v: usize, r: usize) -> usize {
1892 (v + r - 1) & !(r - 1)
1893}
1894
1895type UpdateFunc = unsafe extern "C" fn(
1897 state: u64,
1898 src: *const core::ffi::c_void,
1899 slen: usize,
1900 dst: *mut core::ffi::c_void,
1901 dlen: *mut u64,
1902) -> usize;
1903
1904fn tee_buffer_update(
1923 op: &mut TEE_OperationHandle,
1924 update_func: UpdateFunc,
1925 src_data: *const core::ffi::c_void,
1926 mut src_len: usize,
1927 dest_data: *mut core::ffi::c_void,
1928 dest_len: *mut u64,
1929) -> TEE_Result {
1930 use crate::tee_api_defines::*;
1931
1932 if src_data.is_null() {
1934 if src_len > 0 {
1935 TEE_Panic(0);
1936 }
1937 unsafe {
1938 *dest_len = 0;
1939 }
1940 return TEE_SUCCESS;
1941 }
1942
1943 let mut src = src_data as *const u8;
1944 let mut dst = dest_data as *mut u8;
1945 let mut dlen = unsafe { *dest_len } as usize;
1946 let mut acc_dlen = 0usize;
1947
1948 let (buffer_size, buffer_left) = if op.buffer_two_blocks {
1950 (op.block_size * 2, 1usize)
1951 } else {
1952 (op.block_size, 0usize)
1953 };
1954
1955 if op.buffer_offs > 0 {
1957 let l = if op.buffer_offs < op.block_size {
1958 std::cmp::min(src_len, op.block_size - op.buffer_offs)
1959 } else {
1960 std::cmp::min(src_len, buffer_size - op.buffer_offs)
1961 };
1962
1963 unsafe {
1964 std::ptr::copy_nonoverlapping(src, op.buffer.add(op.buffer_offs), l);
1965 }
1966 op.buffer_offs += l;
1967 src = unsafe { src.add(l) };
1968 src_len -= l;
1969
1970 if op.buffer_offs % op.block_size != 0 {
1972 unsafe {
1973 *dest_len = acc_dlen as u64;
1974 }
1975 return TEE_SUCCESS;
1976 }
1977 }
1978
1979 if op.buffer_offs > 0 && op.buffer_offs + src_len >= buffer_size + buffer_left {
1981 let mut l = roundup2(op.buffer_offs + src_len - buffer_size, op.block_size);
1982 l = std::cmp::min(op.buffer_offs, l);
1983
1984 if !op.buffer_two_blocks {
1986 l = op.block_size;
1987 }
1988
1989 let mut tmp_dlen = dlen as u64;
1990 let res = unsafe {
1991 update_func(
1992 op.state as u64,
1993 op.buffer as *const core::ffi::c_void,
1994 l,
1995 dst as *mut core::ffi::c_void,
1996 &mut tmp_dlen,
1997 )
1998 };
1999
2000 if res != TEE_SUCCESS as usize {
2001 TEE_Panic(res as u32);
2002 }
2003
2004 let tmp_dlen = tmp_dlen as usize;
2005 unsafe {
2006 dst = dst.add(tmp_dlen);
2007 }
2008 dlen -= tmp_dlen;
2009 acc_dlen += tmp_dlen;
2010 op.buffer_offs -= l;
2011
2012 if op.buffer_offs > 0 {
2014 unsafe {
2016 std::ptr::copy(op.buffer.add(l), op.buffer, buffer_size - l);
2017 std::ptr::copy_nonoverlapping(src, op.buffer.add(op.buffer_offs), src_len);
2018 }
2019 op.buffer_offs += src_len;
2020
2021 unsafe {
2022 *dest_len = acc_dlen as u64;
2023 }
2024 return TEE_SUCCESS;
2025 }
2026 }
2027
2028 if src_len >= buffer_size + buffer_left {
2030 let l = if op.buffer_two_blocks {
2032 roundup2(src_len - buffer_size, op.block_size)
2033 } else {
2034 roundup2(src_len - buffer_size + 1, op.block_size)
2035 };
2036
2037 let mut tmp_dlen = dlen as u64;
2038 let res = unsafe {
2039 update_func(
2040 op.state as u64,
2041 src as *const core::ffi::c_void,
2042 l,
2043 dst as *mut core::ffi::c_void,
2044 &mut tmp_dlen,
2045 )
2046 };
2047
2048 if res != TEE_SUCCESS as usize {
2049 TEE_Panic(res as u32);
2050 }
2051
2052 let tmp_dlen = tmp_dlen as usize;
2053 unsafe {
2054 src = src.add(l);
2055 }
2056 src_len -= l;
2057 acc_dlen += tmp_dlen;
2058 }
2059
2060 unsafe {
2062 std::ptr::copy_nonoverlapping(src, op.buffer.add(op.buffer_offs), src_len);
2063 }
2064 op.buffer_offs += src_len;
2065
2066 unsafe {
2067 *dest_len = acc_dlen as u64;
2068 }
2069 TEE_SUCCESS
2070}
2071
2072#[unsafe(no_mangle)]
2086pub extern "C" fn TEE_CipherUpdate(
2087 operation: *mut TEE_OperationHandle,
2088 src_data: *const core::ffi::c_void,
2089 src_len: usize,
2090 dest_data: *mut core::ffi::c_void,
2091 dest_len: *mut usize,
2092) -> TEE_Result {
2093 if operation.is_null() || (src_data.is_null() && src_len > 0) {
2095 return TEE_ERROR_BAD_PARAMETERS;
2096 }
2097
2098 if cfg!(feature = "strict_annotation_checks") {
2100 let res = TEE_CheckMemoryAccessRights(
2101 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
2102 dest_len as *mut core::ffi::c_void,
2103 std::mem::size_of::<usize>(),
2104 );
2105 if res != 0 {
2106 eprintln!("[inout] destLen: error {:#010x}", res);
2107 return TEE_ERROR_BAD_PARAMETERS;
2108 }
2109 }
2110
2111 let op_handle = unsafe { &mut *operation };
2113
2114 if op_handle.info.operationClass != TEE_OPERATION_CIPHER {
2116 return TEE_ERROR_BAD_PARAMETERS;
2117 }
2118
2119 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
2121 return TEE_ERROR_BAD_PARAMETERS;
2122 }
2123
2124 if op_handle.operation_state != TEE_OPERATION_STATE_ACTIVE {
2126 return TEE_ERROR_BAD_PARAMETERS;
2127 }
2128
2129 if src_data.is_null() && src_len == 0 {
2131 unsafe {
2132 *dest_len = 0;
2133 }
2134 return TEE_SUCCESS;
2135 }
2136
2137 let req_dlen = if op_handle.block_size > 1 {
2139 let base =
2140 ((op_handle.buffer_offs + src_len) / op_handle.block_size) * op_handle.block_size;
2141 if op_handle.buffer_two_blocks {
2142 if op_handle.buffer_offs + src_len > op_handle.block_size * 2 {
2143 let req = op_handle.buffer_offs + src_len - op_handle.block_size * 2;
2144 roundup2(req, op_handle.block_size)
2145 } else {
2146 0
2147 }
2148 } else {
2149 base
2150 }
2151 } else {
2152 src_len
2153 };
2154
2155 unsafe {
2160 if *dest_len < req_dlen {
2161 *dest_len = req_dlen;
2162 let res = TEE_ERROR_SHORT_BUFFER;
2163 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
2164 TEE_Panic(res as u32);
2165 }
2166 return res;
2167 }
2168 }
2169
2170 let mut dl = unsafe { *dest_len } as u64;
2171 let res = if op_handle.block_size > 1 {
2172 tee_buffer_update(
2173 op_handle,
2174 _utee_cipher_update,
2175 src_data,
2176 src_len,
2177 dest_data,
2178 &mut dl,
2179 )
2180 } else {
2181 if src_len > 0 {
2182 unsafe {
2183 _utee_cipher_update(
2184 op_handle.state as u64,
2185 src_data,
2186 src_len,
2187 dest_data,
2188 &mut dl,
2189 ) as TEE_Result
2190 }
2191 } else {
2192 dl = 0;
2193 TEE_SUCCESS
2194 }
2195 };
2196
2197 unsafe {
2198 *dest_len = dl as usize;
2199 }
2200
2201 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
2202 TEE_Panic(res as u32);
2203 }
2204
2205 res
2206}
2207
2208#[unsafe(no_mangle)]
2222pub extern "C" fn TEE_CipherDoFinal(
2223 operation: *mut TEE_OperationHandle,
2224 src_data: *const core::ffi::c_void,
2225 src_len: usize,
2226 dest_data: *mut core::ffi::c_void,
2227 dest_len: *mut usize,
2228) -> TEE_Result {
2229 if operation.is_null() || (src_data.is_null() && src_len > 0) {
2231 return TEE_ERROR_BAD_PARAMETERS;
2232 }
2233
2234 if !dest_len.is_null() {
2236 if cfg!(feature = "strict_annotation_checks") {
2237 let check_res = TEE_CheckMemoryAccessRights(
2238 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
2239 dest_len as *mut core::ffi::c_void,
2240 std::mem::size_of::<usize>(),
2241 );
2242 if check_res != 0 {
2243 eprintln!("[inout] destLen: error {:#010x}", check_res);
2244 return TEE_ERROR_BAD_PARAMETERS;
2245 }
2246 }
2247 }
2248
2249 let op_handle = unsafe { &mut *operation };
2251
2252 if op_handle.info.operationClass != TEE_OPERATION_CIPHER {
2254 return TEE_ERROR_BAD_PARAMETERS;
2255 }
2256
2257 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
2259 return TEE_ERROR_BAD_PARAMETERS;
2260 }
2261
2262 if op_handle.operation_state != TEE_OPERATION_STATE_ACTIVE {
2264 return TEE_ERROR_BAD_PARAMETERS;
2265 }
2266
2267 match op_handle.info.algorithm {
2271 TEE_ALG_AES_ECB_NOPAD
2272 | TEE_ALG_AES_CBC_NOPAD
2273 | TEE_ALG_DES_ECB_NOPAD
2274 | TEE_ALG_DES_CBC_NOPAD
2275 | TEE_ALG_DES3_ECB_NOPAD
2276 | TEE_ALG_DES3_CBC_NOPAD
2277 | TEE_ALG_SM4_ECB_NOPAD
2278 | TEE_ALG_SM4_CBC_NOPAD => {
2279 if (op_handle.buffer_offs + src_len) % op_handle.block_size != 0 {
2280 return TEE_ERROR_BAD_PARAMETERS;
2281 }
2282 }
2283 _ => {}
2284 }
2285
2286 let req_dlen = if op_handle.block_size > 1 {
2291 op_handle.buffer_offs + src_len
2292 } else {
2293 src_len
2294 };
2295
2296 let mut tmp_dlen = if !dest_len.is_null() {
2297 unsafe { *dest_len as u64 }
2298 } else {
2299 0u64
2300 };
2301
2302 if tmp_dlen < req_dlen as u64 {
2303 if !dest_len.is_null() {
2304 unsafe {
2305 *dest_len = req_dlen;
2306 }
2307 }
2308 let res = TEE_ERROR_SHORT_BUFFER;
2309 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
2310 TEE_Panic(res as u32);
2311 }
2312 return res;
2313 }
2314
2315 let mut res = TEE_SUCCESS;
2316 let mut dst = dest_data as *mut u8;
2317 let mut acc_dlen = 0usize;
2318
2319 if op_handle.block_size > 1 {
2320 if src_len > 0 {
2321 res = tee_buffer_update(
2322 unsafe { &mut *operation },
2323 _utee_cipher_update,
2324 src_data,
2325 src_len,
2326 dest_data,
2327 &mut tmp_dlen,
2328 );
2329
2330 if res != TEE_SUCCESS {
2331 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
2332 TEE_Panic(res as u32);
2333 }
2334 return res;
2335 }
2336
2337 let tmp_dlen_usize = tmp_dlen as usize;
2338 unsafe {
2339 dst = dst.add(tmp_dlen_usize);
2340 }
2341 acc_dlen += tmp_dlen_usize;
2342
2343 if !dest_len.is_null() {
2344 unsafe {
2345 tmp_dlen = *dest_len as u64 - acc_dlen as u64;
2346 }
2347 }
2348 }
2349
2350 res = unsafe {
2351 _utee_cipher_final(
2352 op_handle.state as u64,
2353 src_data,
2354 src_len,
2355 dest_data,
2356 dest_len as _,
2357 ) as TEE_Result
2358 };
2359 } else {
2360 res = unsafe {
2361 _utee_cipher_final(
2362 op_handle.state as u64,
2363 src_data,
2364 src_len,
2365 dest_data,
2366 dest_len as _,
2367 ) as TEE_Result
2368 };
2369 }
2370
2371 if res != TEE_SUCCESS {
2372 if res != TEE_ERROR_SHORT_BUFFER {
2373 TEE_Panic(res as u32);
2374 }
2375 return res;
2376 }
2377
2378 unsafe {
2380 (*operation).info.handleState &= !TEE_HANDLE_FLAG_INITIALIZED;
2381 (*operation).operation_state = TEE_OPERATION_STATE_INITIAL;
2382 }
2383
2384 res
2385}
2386
2387#[unsafe(no_mangle)]
2399pub extern "C" fn TEE_MACInit(
2400 operation: *mut TEE_OperationHandle,
2401 iv: *const core::ffi::c_void,
2402 iv_len: usize,
2403) {
2404 if operation.is_null() {
2406 TEE_Panic(TEE_PANIC_ID_TEE_MACINIT);
2407 return;
2408 }
2409
2410 let op_handle = unsafe { &*operation };
2412
2413 if op_handle.info.operationClass != TEE_OPERATION_MAC {
2415 TEE_Panic(TEE_PANIC_ID_TEE_MACINIT);
2416 return;
2417 }
2418
2419 if (op_handle.info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0 || op_handle.key1.is_null() {
2421 TEE_Panic(TEE_PANIC_ID_TEE_MACINIT);
2422 return;
2423 }
2424
2425 if op_handle.operation_state != TEE_OPERATION_STATE_INITIAL {
2427 TEE_ResetOperation(operation);
2428 }
2429
2430 unsafe {
2432 (*operation).operation_state = TEE_OPERATION_STATE_ACTIVE;
2433 }
2434
2435 init_hash_operation(operation, iv, iv_len as u32);
2437}
2438
2439#[unsafe(no_mangle)]
2454pub extern "C" fn TEE_MACUpdate(
2455 operation: *mut TEE_OperationHandle,
2456 chunk: *const core::ffi::c_void,
2457 chunk_size: usize,
2458) {
2459 if operation.is_null() {
2461 TEE_Panic(TEE_PANIC_ID_TEE_MACINIT);
2462 return;
2463 }
2464
2465 if chunk.is_null() && chunk_size > 0 {
2467 TEE_Panic(TEE_PANIC_ID_TEE_MACINIT);
2468 return;
2469 }
2470
2471 let op_handle = unsafe { &*operation };
2473
2474 if op_handle.info.operationClass != TEE_OPERATION_MAC {
2476 TEE_Panic(TEE_PANIC_ID_TEE_MACINIT);
2477 return;
2478 }
2479
2480 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
2482 TEE_Panic(TEE_PANIC_ID_TEE_MACINIT);
2483 return;
2484 }
2485
2486 if op_handle.operation_state != TEE_OPERATION_STATE_ACTIVE {
2488 TEE_Panic(TEE_PANIC_ID_TEE_MACINIT);
2489 return;
2490 }
2491
2492 let res = unsafe { _utee_hash_update(op_handle.state as u64, chunk, chunk_size) };
2494
2495 if res != TEE_SUCCESS as usize {
2496 TEE_Panic(res as u32);
2497 }
2498}
2499
2500#[unsafe(no_mangle)]
2514pub extern "C" fn TEE_MACComputeFinal(
2515 operation: *mut TEE_OperationHandle,
2516 message: *const core::ffi::c_void,
2517 message_len: usize,
2518 mac: *mut core::ffi::c_void,
2519 mac_len: *mut usize,
2520) -> TEE_Result {
2521 if operation.is_null()
2523 || (!message.is_null() && message_len == 0)
2524 || (message.is_null() && message_len > 0)
2525 {
2526 return TEE_ERROR_BAD_PARAMETERS;
2527 }
2528
2529 if cfg!(feature = "strict_annotation_checks") {
2531 let res = TEE_CheckMemoryAccessRights(
2532 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
2533 mac_len as *mut core::ffi::c_void,
2534 std::mem::size_of::<usize>(),
2535 );
2536 if res != 0 {
2537 eprintln!("[inout] mac_len: error {:#010x}", res);
2538 TEE_Panic(0);
2539 return TEE_ERROR_BAD_PARAMETERS;
2540 }
2541 }
2542
2543 let op_handle = unsafe { &mut *operation };
2545
2546 if op_handle.info.operationClass != TEE_OPERATION_MAC {
2548 return TEE_ERROR_BAD_PARAMETERS;
2549 }
2550
2551 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
2553 return TEE_ERROR_BAD_PARAMETERS;
2554 }
2555
2556 if op_handle.operation_state != TEE_OPERATION_STATE_ACTIVE {
2558 return TEE_ERROR_BAD_PARAMETERS;
2559 }
2560
2561 let mut ml = unsafe { *mac_len as u64 };
2563
2564 let res =
2566 unsafe { _utee_hash_final(op_handle.state as u64, message, message_len, mac, &mut ml) };
2567
2568 unsafe {
2570 *mac_len = ml as usize;
2571 }
2572
2573 if res != TEE_SUCCESS as usize {
2574 let result = res as TEE_Result;
2575 if result != TEE_ERROR_SHORT_BUFFER {
2576 TEE_Panic(result as u32);
2577 }
2578 return result;
2579 }
2580
2581 unsafe {
2583 (*operation).info.handleState &= !TEE_HANDLE_FLAG_INITIALIZED;
2584 (*operation).operation_state = TEE_OPERATION_STATE_INITIAL;
2585 }
2586
2587 TEE_SUCCESS as TEE_Result
2588}
2589
2590#[unsafe(no_mangle)]
2604pub extern "C" fn TEE_MACCompareFinal(
2605 operation: *mut TEE_OperationHandle,
2606 message: *const core::ffi::c_void,
2607 message_len: usize,
2608 mac: *const core::ffi::c_void,
2609 mac_len: usize,
2610) -> TEE_Result {
2611 if operation.is_null() {
2613 return TEE_ERROR_BAD_PARAMETERS;
2614 }
2615
2616 let op_handle = unsafe { &mut *operation };
2618
2619 if op_handle.info.operationClass != TEE_OPERATION_MAC {
2621 return TEE_ERROR_BAD_PARAMETERS;
2622 }
2623
2624 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
2626 return TEE_ERROR_BAD_PARAMETERS;
2627 }
2628
2629 if op_handle.operation_state != TEE_OPERATION_STATE_ACTIVE {
2631 return TEE_ERROR_BAD_PARAMETERS;
2632 }
2633
2634 let mut computed_mac = [0u8; TEE_MAX_HASH_SIZE as usize];
2636 let mut computed_mac_size = TEE_MAX_HASH_SIZE as usize;
2637
2638 let res = TEE_MACComputeFinal(
2640 operation,
2641 message,
2642 message_len,
2643 computed_mac.as_mut_ptr() as *mut core::ffi::c_void,
2644 &mut computed_mac_size,
2645 );
2646
2647 if res != TEE_SUCCESS {
2648 if res != TEE_ERROR_SHORT_BUFFER {
2649 TEE_Panic(res as u32);
2650 }
2651 return res;
2652 }
2653
2654 if computed_mac_size != mac_len {
2656 return TEE_ERROR_MAC_INVALID;
2657 }
2658
2659 let provided_mac = unsafe { std::slice::from_raw_parts(mac as *const u8, mac_len) };
2661
2662 if !consttime_memcmp(provided_mac, &computed_mac[..computed_mac_size]) {
2663 return TEE_ERROR_MAC_INVALID;
2664 }
2665
2666 unsafe {
2668 (*operation).operation_state = TEE_OPERATION_STATE_INITIAL;
2669 }
2670
2671 TEE_SUCCESS
2672}
2673
2674fn consttime_memcmp(a: &[u8], b: &[u8]) -> bool {
2685 if a.len() != b.len() {
2686 return false;
2687 }
2688
2689 let mut result = 0u8;
2690 for (x, y) in a.iter().zip(b.iter()) {
2691 result |= x ^ y;
2692 }
2693 result == 0
2694}
2695
2696#[unsafe(no_mangle)]
2711pub extern "C" fn TEE_AEInit(
2712 operation: *mut TEE_OperationHandle,
2713 nonce: *const core::ffi::c_void,
2714 nonce_len: usize,
2715 tag_len: u32,
2716 aad_len: usize,
2717 payload_len: usize,
2718) -> TEE_Result {
2719 if operation.is_null() || nonce.is_null() {
2721 return TEE_ERROR_BAD_PARAMETERS;
2722 }
2723
2724 let op_handle = unsafe { &mut *operation };
2726
2727 if op_handle.info.operationClass != TEE_OPERATION_AE {
2729 return TEE_ERROR_BAD_PARAMETERS;
2730 }
2731
2732 if op_handle.operation_state != TEE_OPERATION_STATE_INITIAL {
2734 return TEE_ERROR_BAD_PARAMETERS;
2735 }
2736
2737 if op_handle.info.algorithm == TEE_ALG_AES_GCM {
2739 if tag_len < 96 || tag_len > 128 || (tag_len % 8 != 0) {
2741 return TEE_ERROR_NOT_SUPPORTED;
2742 }
2743 }
2744
2745 let res = unsafe {
2747 _utee_authenc_init(
2748 op_handle.state as u64,
2749 nonce,
2750 nonce_len,
2751 tag_len as usize / 8, aad_len,
2753 payload_len,
2754 )
2755 };
2756
2757 if res != TEE_SUCCESS as usize {
2758 let result = res as TEE_Result;
2759 if result != TEE_ERROR_NOT_SUPPORTED {
2760 TEE_Panic(result as u32);
2761 }
2762 return result;
2763 }
2764
2765 unsafe {
2767 (*operation).info.digestLength = (tag_len / 8) as u32; (*operation).buffer_offs = 0;
2769 (*operation).info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
2770 }
2771
2772 TEE_SUCCESS
2773}
2774
2775#[unsafe(no_mangle)]
2782pub extern "C" fn TEE_AEUpdateAAD(
2783 operation: *mut TEE_OperationHandle,
2784 aad_data: *const core::ffi::c_void,
2785 aad_data_len: usize,
2786) {
2787 if operation.is_null() || (aad_data.is_null() && aad_data_len > 0) {
2789 TEE_Panic(0);
2790 return;
2791 }
2792
2793 let op_handle = unsafe { &*operation };
2795
2796 if op_handle.info.operationClass != TEE_OPERATION_AE {
2798 TEE_Panic(0);
2799 return;
2800 }
2801
2802 if op_handle.operation_state != TEE_OPERATION_STATE_INITIAL {
2804 TEE_Panic(0);
2805 return;
2806 }
2807
2808 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
2810 TEE_Panic(0);
2811 return;
2812 }
2813
2814 let res = unsafe { _utee_authenc_update_aad(op_handle.state as u64, aad_data, aad_data_len) };
2816
2817 if res != TEE_SUCCESS as usize {
2818 TEE_Panic(res as u32);
2819 }
2820}
2821
2822fn ae_update_helper(
2835 operation: *mut TEE_OperationHandle,
2836 src: *const core::ffi::c_void,
2837 slen: usize,
2838 dst: *mut core::ffi::c_void,
2839 dlen: *mut usize,
2840) -> TEE_Result {
2841 use crate::tee_api_defines::*;
2842
2843 if src.is_null() && slen == 0 {
2845 unsafe {
2846 *dlen = 0;
2847 }
2848 return TEE_SUCCESS;
2849 }
2850
2851 if operation.is_null() {
2853 return TEE_ERROR_BAD_PARAMETERS;
2854 }
2855
2856 let op_handle = unsafe { &*operation };
2858
2859 if dlen.is_null() {
2861 return TEE_ERROR_BAD_PARAMETERS;
2862 }
2863
2864 let req_dlen = if op_handle.block_size > 1 {
2866 let total_size = op_handle.buffer_offs + slen;
2868 roundup2(total_size, op_handle.block_size)
2870 } else {
2871 slen
2872 };
2873
2874 let provided_dlen = unsafe { *dlen };
2876 if provided_dlen < req_dlen {
2877 unsafe {
2878 *dlen = req_dlen;
2879 }
2880 return TEE_ERROR_SHORT_BUFFER;
2881 }
2882
2883 let mut dl = provided_dlen as u64;
2885 let res = if op_handle.block_size > 1 {
2886 tee_buffer_update(
2888 unsafe { &mut *operation },
2889 _utee_authenc_update_payload,
2890 src,
2891 slen,
2892 dst,
2893 &mut dl,
2894 )
2895 } else {
2896 if slen > 0 {
2898 unsafe {
2899 _utee_authenc_update_payload(op_handle.state as u64, src, slen, dst, &mut dl)
2900 as TEE_Result
2901 }
2902 } else {
2903 dl = 0;
2904 TEE_SUCCESS
2905 }
2906 };
2907
2908 if res == TEE_SUCCESS {
2910 unsafe {
2911 *dlen = dl as usize;
2912 }
2913 }
2914
2915 res
2916}
2917
2918#[unsafe(no_mangle)]
2932pub extern "C" fn TEE_AEUpdate(
2933 operation: *mut TEE_OperationHandle,
2934 src_data: *const core::ffi::c_void,
2935 src_len: usize,
2936 dest_data: *mut core::ffi::c_void,
2937 dest_len: *mut usize,
2938) -> TEE_Result {
2939 if operation.is_null() || (src_data.is_null() && src_len > 0) {
2941 let res = TEE_ERROR_BAD_PARAMETERS;
2942 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
2943 TEE_Panic(res as u32);
2944 }
2945 return res;
2946 }
2947
2948 if !dest_data.is_null() && !dest_len.is_null() {
2950 if cfg!(feature = "strict_annotation_checks") {
2951 let check_res = TEE_CheckMemoryAccessRights(
2952 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
2953 dest_len as *mut core::ffi::c_void,
2954 std::mem::size_of::<usize>(),
2955 );
2956 if check_res != 0 {
2957 eprintln!("[inout] destLen: error {:#010x}", check_res);
2958 TEE_Panic(0);
2959 return TEE_ERROR_BAD_PARAMETERS;
2960 }
2961 }
2962 }
2963
2964 let op_handle = unsafe { &*operation };
2966
2967 if op_handle.info.operationClass != TEE_OPERATION_AE {
2969 let res = TEE_ERROR_BAD_PARAMETERS;
2970 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
2971 TEE_Panic(res as u32);
2972 }
2973 return res;
2974 }
2975
2976 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
2978 let res = TEE_ERROR_BAD_PARAMETERS;
2979 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
2980 TEE_Panic(res as u32);
2981 }
2982 return res;
2983 }
2984
2985 let res = ae_update_helper(operation, src_data, src_len, dest_data, dest_len);
2987 if res != TEE_ERROR_SHORT_BUFFER && src_len > 0 {
2988 unsafe {
2989 (*operation).operation_state = TEE_OPERATION_STATE_ACTIVE;
2990 }
2991 }
2992
2993 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
2995 TEE_Panic(res as u32);
2996 }
2997
2998 res
2999}
3000
3001#[unsafe(no_mangle)]
3017pub extern "C" fn TEE_AEEncryptFinal(
3018 operation: *mut TEE_OperationHandle,
3019 src_data: *const core::ffi::c_void,
3020 src_len: usize,
3021 dest_data: *mut core::ffi::c_void,
3022 dest_len: *mut usize,
3023 tag: *mut core::ffi::c_void,
3024 tag_len: *mut usize,
3025) -> TEE_Result {
3026 let mut res = TEE_SUCCESS;
3027
3028 if operation.is_null() || (src_data.is_null() && src_len > 0) {
3030 res = TEE_ERROR_BAD_PARAMETERS;
3031 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3032 TEE_Panic(res as u32);
3033 }
3034 return res;
3035 }
3036
3037 if !dest_len.is_null() {
3039 if cfg!(feature = "strict_annotation_checks") {
3040 let check_res = TEE_CheckMemoryAccessRights(
3041 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
3042 dest_len as *mut core::ffi::c_void,
3043 std::mem::size_of::<usize>(),
3044 );
3045 if check_res != 0 {
3046 eprintln!("[inout] destLen: error {:#010x}", check_res);
3047 TEE_Panic(0);
3048 return TEE_ERROR_BAD_PARAMETERS;
3049 }
3050 }
3051 }
3052
3053 if !tag_len.is_null() {
3054 if cfg!(feature = "strict_annotation_checks") {
3055 let check_res = TEE_CheckMemoryAccessRights(
3056 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
3057 tag_len as *mut core::ffi::c_void,
3058 std::mem::size_of::<usize>(),
3059 );
3060 if check_res != 0 {
3061 eprintln!("[inout] tagLen: error {:#010x}", check_res);
3062 TEE_Panic(0);
3063 return TEE_ERROR_BAD_PARAMETERS;
3064 }
3065 }
3066 }
3067
3068 let op_handle = unsafe { &*operation };
3070
3071 if op_handle.info.operationClass != TEE_OPERATION_AE {
3073 let res = TEE_ERROR_BAD_PARAMETERS;
3074 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3075 TEE_Panic(res as u32);
3076 }
3077 return res;
3078 }
3079
3080 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
3082 let res = TEE_ERROR_BAD_PARAMETERS;
3083 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3084 TEE_Panic(res as u32);
3085 }
3086 return res;
3087 }
3088
3089 if dest_len.is_null() || tag_len.is_null() {
3091 res = TEE_ERROR_BAD_PARAMETERS;
3092 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3093 TEE_Panic(res as u32);
3094 }
3095 return res;
3096 }
3097
3098 let dest_len_val = unsafe { *dest_len };
3100 let tag_len_val = unsafe { *tag_len };
3101
3102 let req_dlen = op_handle.buffer_offs + src_len;
3104
3105 if dest_len_val < req_dlen {
3107 unsafe {
3108 *dest_len = req_dlen;
3109 }
3110 res = TEE_ERROR_SHORT_BUFFER;
3111 }
3112
3113 if tag_len_val < op_handle.info.digestLength as usize {
3115 unsafe {
3116 *tag_len = op_handle.info.digestLength as usize;
3117 }
3118 res = TEE_ERROR_SHORT_BUFFER;
3119 }
3120
3121 if res == TEE_ERROR_SHORT_BUFFER {
3122 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3123 TEE_Panic(res as u32);
3124 }
3125 return res;
3126 }
3127
3128 let mut acc_dlen = 0usize;
3133 let mut tl = tag_len_val as u64;
3134 let mut tmp_dlen = (dest_len_val - acc_dlen) as u64;
3135
3136 let dst = dest_data as *mut u8;
3137
3138 if op_handle.block_size > 1 {
3140 res = tee_buffer_update(
3142 unsafe { &mut *operation },
3143 _utee_authenc_update_payload,
3144 src_data,
3145 src_len,
3146 dest_data,
3147 &mut tmp_dlen,
3148 );
3149 if res != TEE_SUCCESS {
3150 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3151 TEE_Panic(res as u32);
3152 }
3153 return res;
3154 }
3155
3156 acc_dlen += tmp_dlen as usize;
3157 tmp_dlen = (dest_len_val - acc_dlen) as u64;
3158
3159 let buffer_ptr = op_handle.buffer as *const core::ffi::c_void;
3161 res = unsafe {
3162 _utee_authenc_enc_final(
3163 op_handle.state as u64,
3164 buffer_ptr,
3165 op_handle.buffer_offs,
3166 dst.add(acc_dlen) as *mut core::ffi::c_void,
3167 &mut tmp_dlen,
3168 tag,
3169 &mut tl,
3170 ) as TEE_Result
3171 };
3172 } else {
3173 res = unsafe {
3175 _utee_authenc_enc_final(
3176 op_handle.state as u64,
3177 src_data,
3178 src_len,
3179 dst as *mut core::ffi::c_void,
3180 &mut tmp_dlen,
3181 tag,
3182 &mut tl,
3183 ) as TEE_Result
3184 };
3185 }
3186
3187 unsafe {
3189 *tag_len = tl as usize;
3190 }
3191
3192 if res != TEE_SUCCESS {
3193 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3194 TEE_Panic(res as u32);
3195 }
3196 return res;
3197 }
3198
3199 acc_dlen += tmp_dlen as usize;
3200 unsafe {
3201 *dest_len = acc_dlen;
3202 }
3203
3204 unsafe {
3206 (*operation).info.handleState &= !TEE_HANDLE_FLAG_INITIALIZED;
3207 (*operation).operation_state = TEE_OPERATION_STATE_INITIAL;
3208 }
3209
3210 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3212 TEE_Panic(res as u32);
3213 }
3214
3215 res
3216}
3217
3218#[unsafe(no_mangle)]
3235pub extern "C" fn TEE_AEDecryptFinal(
3236 operation: *mut TEE_OperationHandle,
3237 src_data: *const core::ffi::c_void,
3238 src_len: usize,
3239 dest_data: *mut core::ffi::c_void,
3240 dest_len: *mut usize,
3241 tag: *const core::ffi::c_void,
3242 tag_len: usize,
3243) -> TEE_Result {
3244 let mut res = TEE_SUCCESS;
3245 if operation.is_null() || (src_data.is_null() && src_len > 0) {
3247 let res = TEE_ERROR_BAD_PARAMETERS;
3248 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER && res != TEE_ERROR_MAC_INVALID {
3249 TEE_Panic(res as u32);
3250 }
3251 return res;
3252 }
3253
3254 if !dest_len.is_null() {
3256 if cfg!(feature = "strict_annotation_checks") {
3257 let check_res = TEE_CheckMemoryAccessRights(
3258 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
3259 dest_len as *mut core::ffi::c_void,
3260 std::mem::size_of::<usize>(),
3261 );
3262 if check_res != 0 {
3263 eprintln!("[inout] destLen: error {:#010x}", check_res);
3264 TEE_Panic(0);
3265 return TEE_ERROR_BAD_PARAMETERS;
3266 }
3267 }
3268 } else {
3269 let res = TEE_ERROR_BAD_PARAMETERS;
3270 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER && res != TEE_ERROR_MAC_INVALID {
3271 TEE_Panic(res as u32);
3272 }
3273 return res;
3274 }
3275
3276 let op_handle = unsafe { &*operation };
3278
3279 if op_handle.info.operationClass != TEE_OPERATION_AE {
3281 let res = TEE_ERROR_BAD_PARAMETERS;
3282 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER && res != TEE_ERROR_MAC_INVALID {
3283 TEE_Panic(res as u32);
3284 }
3285 return res;
3286 }
3287
3288 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
3290 let res = TEE_ERROR_BAD_PARAMETERS;
3291 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER && res != TEE_ERROR_MAC_INVALID {
3292 TEE_Panic(res as u32);
3293 }
3294 return res;
3295 }
3296
3297 let req_dlen = op_handle.buffer_offs + src_len;
3299
3300 let dest_len_val = unsafe { *dest_len };
3302 if dest_len_val < req_dlen {
3303 unsafe {
3304 *dest_len = req_dlen;
3305 }
3306 let res = TEE_ERROR_SHORT_BUFFER;
3307 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER && res != TEE_ERROR_MAC_INVALID {
3308 TEE_Panic(res as u32);
3309 }
3310 return res;
3311 }
3312
3313 let mut acc_dlen = 0usize;
3315 let mut tmp_dlen = (dest_len_val - acc_dlen) as u64;
3316
3317 let dst = dest_data as *mut u8;
3318
3319 if op_handle.block_size > 1 {
3321 res = tee_buffer_update(
3323 unsafe { &mut *operation },
3324 _utee_authenc_update_payload,
3325 src_data,
3326 src_len,
3327 dest_data,
3328 &mut tmp_dlen,
3329 );
3330 if res != TEE_SUCCESS {
3331 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER && res != TEE_ERROR_MAC_INVALID {
3332 TEE_Panic(res as u32);
3333 }
3334 return res;
3335 }
3336
3337 acc_dlen += tmp_dlen as usize;
3338 tmp_dlen = (dest_len_val - acc_dlen) as u64;
3339
3340 let buffer_ptr = op_handle.buffer as *const core::ffi::c_void;
3342 res = unsafe {
3343 _utee_authenc_dec_final(
3344 op_handle.state as u64,
3345 buffer_ptr,
3346 op_handle.buffer_offs,
3347 dst.add(acc_dlen) as *mut core::ffi::c_void,
3348 &mut tmp_dlen,
3349 tag,
3350 tag_len,
3351 ) as TEE_Result
3352 };
3353 } else {
3354 res = unsafe {
3356 _utee_authenc_dec_final(
3357 op_handle.state as u64,
3358 src_data,
3359 src_len,
3360 dst as *mut core::ffi::c_void,
3361 &mut tmp_dlen,
3362 tag,
3363 tag_len,
3364 ) as TEE_Result
3365 };
3366 }
3367
3368 if res != TEE_SUCCESS {
3369 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER && res != TEE_ERROR_MAC_INVALID {
3370 TEE_Panic(res as u32);
3371 }
3372 return res;
3373 }
3374
3375 acc_dlen += tmp_dlen as usize;
3376 unsafe {
3377 *dest_len = acc_dlen;
3378 }
3379
3380 unsafe {
3382 (*operation).info.handleState &= !TEE_HANDLE_FLAG_INITIALIZED;
3383 (*operation).operation_state = TEE_OPERATION_STATE_INITIAL;
3384 }
3385
3386 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER && res != TEE_ERROR_MAC_INVALID {
3388 TEE_Panic(res as u32);
3389 }
3390
3391 res
3392}
3393
3394#[unsafe(no_mangle)]
3412pub extern "C" fn TEE_AsymmetricEncrypt(
3413 operation: *mut TEE_OperationHandle,
3414 params: *const TEE_Attribute,
3415 param_count: u32,
3416 src_data: *const core::ffi::c_void,
3417 src_len: usize,
3418 dest_data: *mut core::ffi::c_void,
3419 dest_len: *mut usize,
3420) -> TEE_Result {
3421 let mut res = TEE_SUCCESS;
3422
3423 if operation.is_null() || (src_data.is_null() && src_len > 0) {
3425 TEE_Panic(0);
3426 return TEE_ERROR_BAD_PARAMETERS;
3427 }
3428
3429 if !params.is_null() && param_count > 0 {
3431 if cfg!(feature = "strict_annotation_checks") {
3432 let check_res = TEE_CheckMemoryAccessRights(
3433 TEE_MEMORY_ACCESS_READ,
3434 params as *mut core::ffi::c_void,
3435 (param_count as usize) * std::mem::size_of::<TEE_Attribute>(),
3436 );
3437 if check_res != 0 {
3438 eprintln!("[in] params: error {:#010x}", check_res);
3439 TEE_Panic(0);
3440 return TEE_ERROR_BAD_PARAMETERS;
3441 }
3442 }
3443 }
3444
3445 if !dest_len.is_null() {
3447 if cfg!(feature = "strict_annotation_checks") {
3448 let check_res = TEE_CheckMemoryAccessRights(
3449 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
3450 dest_len as *mut core::ffi::c_void,
3451 std::mem::size_of::<usize>(),
3452 );
3453 if check_res != 0 {
3454 eprintln!("[inout] destLen: error {:#010x}", check_res);
3455 TEE_Panic(0);
3456 return TEE_ERROR_BAD_PARAMETERS;
3457 }
3458 }
3459 } else {
3460 TEE_Panic(0);
3461 return TEE_ERROR_BAD_PARAMETERS;
3462 }
3463
3464 let op_handle = unsafe { &*operation };
3466
3467 if op_handle.key1.is_null() {
3469 TEE_Panic(0);
3470 return TEE_ERROR_BAD_PARAMETERS;
3471 }
3472
3473 if op_handle.info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER {
3475 TEE_Panic(0);
3476 return TEE_ERROR_BAD_PARAMETERS;
3477 }
3478
3479 if op_handle.info.mode != TEE_MODE_ENCRYPT {
3480 TEE_Panic(0);
3481 return TEE_ERROR_BAD_PARAMETERS;
3482 }
3483
3484 let mut dl = unsafe { *dest_len as u64 };
3486
3487 let mut ua = Vec::with_capacity(param_count as usize);
3489 ua.resize(
3490 param_count as usize,
3491 crate::utee_types::utee_attribute::default(),
3492 );
3493 unsafe {
3494 __utee_from_attr(ua.as_mut_ptr(), params, param_count);
3495 }
3496
3497 let syscall_res = unsafe {
3499 _utee_asymm_operate(
3500 op_handle.state as u64,
3501 ua.as_ptr(),
3502 param_count as u64,
3503 src_data,
3504 src_len,
3505 dest_data,
3506 &mut dl,
3507 )
3508 };
3509
3510 unsafe {
3512 *dest_len = dl as usize;
3513 }
3514
3515 res = syscall_res as TEE_Result;
3516
3517 if res != TEE_SUCCESS
3519 && res != TEE_ERROR_SHORT_BUFFER
3520 && res != TEE_ERROR_BAD_PARAMETERS
3521 && res != TEE_ERROR_CIPHERTEXT_INVALID
3522 && res != TEE_ERROR_NOT_SUPPORTED
3523 {
3524 TEE_Panic(res as u32);
3525 }
3526
3527 res
3528}
3529
3530#[unsafe(no_mangle)]
3548pub extern "C" fn TEE_AsymmetricDecrypt(
3549 operation: *mut TEE_OperationHandle,
3550 params: *const TEE_Attribute,
3551 param_count: u32,
3552 src_data: *const core::ffi::c_void,
3553 src_len: usize,
3554 dest_data: *mut core::ffi::c_void,
3555 dest_len: *mut usize,
3556) -> TEE_Result {
3557 let mut res = TEE_SUCCESS;
3558
3559 if operation.is_null() || (src_data.is_null() && src_len > 0) {
3561 TEE_Panic(0);
3562 return TEE_ERROR_BAD_PARAMETERS;
3563 }
3564
3565 if !params.is_null() && param_count > 0 {
3567 if cfg!(feature = "strict_annotation_checks") {
3568 let check_res = TEE_CheckMemoryAccessRights(
3569 TEE_MEMORY_ACCESS_READ,
3570 params as *mut core::ffi::c_void,
3571 (param_count as usize) * std::mem::size_of::<TEE_Attribute>(),
3572 );
3573 if check_res != 0 {
3574 eprintln!("[in] params: error {:#010x}", check_res);
3575 TEE_Panic(0);
3576 return TEE_ERROR_BAD_PARAMETERS;
3577 }
3578 }
3579 }
3580
3581 if !dest_len.is_null() {
3583 if cfg!(feature = "strict_annotation_checks") {
3584 let check_res = TEE_CheckMemoryAccessRights(
3585 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
3586 dest_len as *mut core::ffi::c_void,
3587 std::mem::size_of::<usize>(),
3588 );
3589 if check_res != 0 {
3590 eprintln!("[inout] destLen: error {:#010x}", check_res);
3591 TEE_Panic(0);
3592 return TEE_ERROR_BAD_PARAMETERS;
3593 }
3594 }
3595 } else {
3596 TEE_Panic(0);
3597 return TEE_ERROR_BAD_PARAMETERS;
3598 }
3599
3600 let op_handle = unsafe { &*operation };
3602
3603 if op_handle.key1.is_null() {
3605 TEE_Panic(0);
3606 return TEE_ERROR_BAD_PARAMETERS;
3607 }
3608
3609 if op_handle.info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER {
3611 TEE_Panic(0);
3612 return TEE_ERROR_BAD_PARAMETERS;
3613 }
3614
3615 if op_handle.info.mode != TEE_MODE_DECRYPT {
3616 TEE_Panic(0);
3617 return TEE_ERROR_BAD_PARAMETERS;
3618 }
3619
3620 let mut dl = unsafe { *dest_len as u64 };
3622
3623 let mut ua = Vec::with_capacity(param_count as usize);
3625 ua.resize(
3626 param_count as usize,
3627 crate::utee_types::utee_attribute::default(),
3628 );
3629 unsafe {
3630 __utee_from_attr(ua.as_mut_ptr(), params, param_count);
3631 }
3632
3633 let syscall_res = unsafe {
3635 _utee_asymm_operate(
3636 op_handle.state as u64,
3637 ua.as_ptr(),
3638 param_count as u64,
3639 src_data,
3640 src_len,
3641 dest_data,
3642 &mut dl,
3643 )
3644 };
3645
3646 unsafe {
3648 *dest_len = dl as usize;
3649 }
3650
3651 res = syscall_res as TEE_Result;
3653 if res != TEE_SUCCESS
3654 && res != TEE_ERROR_SHORT_BUFFER
3655 && res != TEE_ERROR_BAD_PARAMETERS
3656 && res != TEE_ERROR_CIPHERTEXT_INVALID
3657 && res != TEE_ERROR_NOT_SUPPORTED
3658 {
3659 TEE_Panic(res as u32);
3660 }
3661
3662 res
3663}
3664
3665#[unsafe(no_mangle)]
3681pub extern "C" fn TEE_AsymmetricSignDigest(
3682 operation: *mut TEE_OperationHandle,
3683 params: *const TEE_Attribute,
3684 param_count: u32,
3685 digest: *const core::ffi::c_void,
3686 digest_len: usize,
3687 signature: *mut core::ffi::c_void,
3688 signature_len: *mut usize,
3689) -> TEE_Result {
3690 let mut res = TEE_SUCCESS;
3691
3692 if operation.is_null() || (digest.is_null() && digest_len > 0) {
3694 TEE_Panic(0);
3695 return TEE_ERROR_BAD_PARAMETERS;
3696 }
3697
3698 if !params.is_null() && param_count > 0 {
3700 if cfg!(feature = "strict_annotation_checks") {
3701 let check_res = TEE_CheckMemoryAccessRights(
3702 TEE_MEMORY_ACCESS_READ,
3703 params as *mut core::ffi::c_void,
3704 (param_count as usize) * std::mem::size_of::<TEE_Attribute>(),
3705 );
3706 if check_res != 0 {
3707 eprintln!("[in] params: error {:#010x}", check_res);
3708 TEE_Panic(0);
3709 return TEE_ERROR_BAD_PARAMETERS;
3710 }
3711 }
3712 }
3713
3714 if !signature_len.is_null() {
3716 if cfg!(feature = "strict_annotation_checks") {
3717 let check_res = TEE_CheckMemoryAccessRights(
3718 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
3719 signature_len as *mut core::ffi::c_void,
3720 std::mem::size_of::<usize>(),
3721 );
3722 if check_res != 0 {
3723 eprintln!("[inout] signatureLen: error {:#010x}", check_res);
3724 TEE_Panic(0);
3725 return TEE_ERROR_BAD_PARAMETERS;
3726 }
3727 }
3728 } else {
3729 TEE_Panic(0);
3730 return TEE_ERROR_BAD_PARAMETERS;
3731 }
3732
3733 let op_handle = unsafe { &*operation };
3735
3736 if op_handle.key1.is_null() {
3738 TEE_Panic(0);
3739 return TEE_ERROR_BAD_PARAMETERS;
3740 }
3741
3742 if op_handle.info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE {
3744 TEE_Panic(0);
3745 return TEE_ERROR_BAD_PARAMETERS;
3746 }
3747
3748 if op_handle.info.mode != TEE_MODE_SIGN {
3749 TEE_Panic(0);
3750 return TEE_ERROR_BAD_PARAMETERS;
3751 }
3752
3753 let mut sl = unsafe { *signature_len as u64 };
3755
3756 let mut ua = Vec::with_capacity(param_count as usize);
3758 ua.resize(
3759 param_count as usize,
3760 crate::utee_types::utee_attribute::default(),
3761 );
3762 unsafe {
3763 __utee_from_attr(ua.as_mut_ptr(), params, param_count);
3764 }
3765
3766 let syscall_res = unsafe {
3768 _utee_asymm_operate(
3769 op_handle.state as u64,
3770 ua.as_ptr(),
3771 param_count as u64,
3772 digest,
3773 digest_len,
3774 signature,
3775 &mut sl,
3776 )
3777 };
3778
3779 unsafe {
3781 *signature_len = sl as usize;
3782 }
3783
3784 res = syscall_res as TEE_Result;
3786 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3787 TEE_Panic(res as u32);
3788 }
3789
3790 res
3791}
3792
3793#[unsafe(no_mangle)]
3809pub extern "C" fn TEE_AsymmetricVerifyDigest(
3810 operation: *mut TEE_OperationHandle,
3811 params: *const TEE_Attribute,
3812 param_count: u32,
3813 digest: *const core::ffi::c_void,
3814 digest_len: usize,
3815 signature: *const core::ffi::c_void,
3816 signature_len: usize,
3817) -> TEE_Result {
3818 let mut res = TEE_SUCCESS;
3819
3820 if operation.is_null()
3822 || (digest.is_null() && digest_len != 0)
3823 || (signature.is_null() && signature_len != 0)
3824 {
3825 TEE_Panic(0);
3826 return TEE_ERROR_BAD_PARAMETERS;
3827 }
3828
3829 if !params.is_null() && param_count > 0 {
3831 if cfg!(feature = "strict_annotation_checks") {
3832 let check_res = TEE_CheckMemoryAccessRights(
3833 TEE_MEMORY_ACCESS_READ,
3834 params as *mut core::ffi::c_void,
3835 (param_count as usize) * std::mem::size_of::<TEE_Attribute>(),
3836 );
3837 if check_res != 0 {
3838 eprintln!("[in] params: error {:#010x}", check_res);
3839 TEE_Panic(0);
3840 return TEE_ERROR_BAD_PARAMETERS;
3841 }
3842 }
3843 }
3844
3845 if !digest.is_null() && digest_len > 0 {
3847 if cfg!(feature = "strict_annotation_checks") {
3848 let check_res = TEE_CheckMemoryAccessRights(
3849 TEE_MEMORY_ACCESS_READ,
3850 digest as *mut core::ffi::c_void,
3851 digest_len,
3852 );
3853 if check_res != 0 {
3854 eprintln!("[in] digest: error {:#010x}", check_res);
3855 TEE_Panic(0);
3856 return TEE_ERROR_BAD_PARAMETERS;
3857 }
3858 }
3859 }
3860
3861 if !signature.is_null() && signature_len > 0 {
3863 if cfg!(feature = "strict_annotation_checks") {
3864 let check_res = TEE_CheckMemoryAccessRights(
3865 TEE_MEMORY_ACCESS_READ,
3866 signature as *mut core::ffi::c_void,
3867 signature_len,
3868 );
3869 if check_res != 0 {
3870 eprintln!("[in] signature: error {:#010x}", check_res);
3871 TEE_Panic(0);
3872 return TEE_ERROR_BAD_PARAMETERS;
3873 }
3874 }
3875 }
3876
3877 let op_handle = unsafe { &*operation };
3879
3880 if op_handle.key1.is_null() {
3882 TEE_Panic(0);
3883 return TEE_ERROR_BAD_PARAMETERS;
3884 }
3885
3886 if op_handle.info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE {
3888 TEE_Panic(0);
3889 return TEE_ERROR_BAD_PARAMETERS;
3890 }
3891
3892 if op_handle.info.mode != TEE_MODE_VERIFY {
3893 TEE_Panic(0);
3894 return TEE_ERROR_BAD_PARAMETERS;
3895 }
3896
3897 let mut ua = Vec::with_capacity(param_count as usize);
3899 ua.resize(
3900 param_count as usize,
3901 crate::utee_types::utee_attribute::default(),
3902 );
3903 unsafe {
3904 __utee_from_attr(ua.as_mut_ptr(), params, param_count);
3905 }
3906
3907 let syscall_res = unsafe {
3909 _utee_asymm_verify(
3910 op_handle.state as u64,
3911 ua.as_ptr(),
3912 param_count as u64,
3913 digest,
3914 digest_len,
3915 signature,
3916 signature_len,
3917 )
3918 };
3919
3920 res = syscall_res as TEE_Result;
3922 if res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID {
3923 TEE_Panic(res as u32);
3924 }
3925
3926 res
3927}
3928
3929#[unsafe(no_mangle)]
3937pub extern "C" fn TEE_DeriveKey(
3938 operation: *mut TEE_OperationHandle,
3939 params: *const TEE_Attribute,
3940 param_count: u32,
3941 derived_key: TEE_ObjectHandle,
3942) {
3943 if operation.is_null() || derived_key.is_null() {
3945 TEE_Panic(0);
3946 return;
3947 }
3948
3949 if !params.is_null() && param_count > 0 {
3951 if cfg!(feature = "strict_annotation_checks") {
3952 let check_res = TEE_CheckMemoryAccessRights(
3953 TEE_MEMORY_ACCESS_READ,
3954 params as *mut core::ffi::c_void,
3955 (param_count as usize) * std::mem::size_of::<TEE_Attribute>(),
3956 );
3957 if check_res != 0 {
3958 eprintln!("[in] params: error {:#010x}", check_res);
3959 TEE_Panic(0);
3960 return;
3961 }
3962 }
3963 }
3964
3965 let op_handle = unsafe { &*operation };
3967
3968 if TEE_ALG_GET_CLASS(op_handle.info.algorithm) != TEE_OPERATION_KEY_DERIVATION {
3970 TEE_Panic(0);
3971 return;
3972 }
3973
3974 if op_handle.info.operationClass != TEE_OPERATION_KEY_DERIVATION {
3976 TEE_Panic(0);
3977 return;
3978 }
3979
3980 if op_handle.key1.is_null() {
3981 TEE_Panic(0);
3982 return;
3983 }
3984
3985 if op_handle.info.mode != TEE_MODE_DERIVE {
3986 TEE_Panic(0);
3987 return;
3988 }
3989
3990 if (op_handle.info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0 {
3991 TEE_Panic(0);
3992 return;
3993 }
3994
3995 let mut key_info = crate::utee_types::utee_object_info::default();
3997
3998 let res = unsafe { _utee_cryp_obj_get_info(derived_key as u64, &mut key_info) };
3999 if res != TEE_SUCCESS as usize {
4000 TEE_Panic(res as u32);
4001 return;
4002 }
4003
4004 if key_info.obj_type != TEE_TYPE_GENERIC_SECRET {
4006 TEE_Panic(0);
4007 return;
4008 }
4009
4010 if (key_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0 {
4011 TEE_Panic(0);
4012 return;
4013 }
4014
4015 let mut ua = Vec::with_capacity(param_count as usize);
4017 ua.resize(
4018 param_count as usize,
4019 crate::utee_types::utee_attribute::default(),
4020 );
4021 unsafe {
4022 __utee_from_attr(ua.as_mut_ptr(), params, param_count);
4023 }
4024
4025 let res = unsafe {
4027 _utee_cryp_derive_key(
4028 op_handle.state as u64,
4029 ua.as_ptr(),
4030 param_count as u64,
4031 derived_key as u64,
4032 )
4033 };
4034
4035 if res != TEE_SUCCESS as usize {
4036 TEE_Panic(res as u32);
4037 }
4038}
4039
4040#[unsafe(no_mangle)]
4046pub extern "C" fn TEE_GenerateRandom(
4047 random_buffer: *mut core::ffi::c_void,
4048 random_buffer_len: usize,
4049) {
4050 if random_buffer.is_null() && random_buffer_len > 0 {
4052 TEE_Panic(TEE_ERROR_BAD_PARAMETERS as u32);
4053 return;
4054 }
4055
4056 let res = unsafe { _utee_cryp_random_number_generate(random_buffer, random_buffer_len) };
4058
4059 if res != TEE_SUCCESS as usize {
4060 TEE_Panic(res as u32);
4061 }
4062}
4063
4064#[unsafe(no_mangle)]
4074pub extern "C" fn TEE_IsAlgorithmSupported(alg: u32, element: u32) -> TEE_Result {
4075 use crate::tee_api_defines::*;
4076
4077 if alg == TEE_ALG_AES_ECB_NOPAD {
4079 if element == TEE_CRYPTO_ELEMENT_NONE {
4080 return TEE_SUCCESS;
4081 }
4082 }
4083
4084 if alg == TEE_ALG_AES_CBC_NOPAD {
4085 if element == TEE_CRYPTO_ELEMENT_NONE {
4086 return TEE_SUCCESS;
4087 }
4088 }
4089
4090 if alg == TEE_ALG_AES_CTR {
4091 if element == TEE_CRYPTO_ELEMENT_NONE {
4092 return TEE_SUCCESS;
4093 }
4094 }
4095
4096 if alg == TEE_ALG_AES_CTS {
4097 if element == TEE_CRYPTO_ELEMENT_NONE {
4098 return TEE_SUCCESS;
4099 }
4100 }
4101
4102 if alg == TEE_ALG_AES_XTS {
4103 if element == TEE_CRYPTO_ELEMENT_NONE {
4104 return TEE_SUCCESS;
4105 }
4106 }
4107
4108 if alg == TEE_ALG_AES_CBC_MAC_NOPAD || alg == TEE_ALG_AES_CBC_MAC_PKCS5 {
4109 if element == TEE_CRYPTO_ELEMENT_NONE {
4110 return TEE_SUCCESS;
4111 }
4112 }
4113
4114 if alg == TEE_ALG_AES_CMAC {
4115 if element == TEE_CRYPTO_ELEMENT_NONE {
4116 return TEE_SUCCESS;
4117 }
4118 }
4119
4120 if alg == TEE_ALG_AES_CCM {
4121 if element == TEE_CRYPTO_ELEMENT_NONE {
4122 return TEE_SUCCESS;
4123 }
4124 }
4125
4126 if alg == TEE_ALG_AES_GCM {
4127 if element == TEE_CRYPTO_ELEMENT_NONE {
4128 return TEE_SUCCESS;
4129 }
4130 }
4131
4132 if alg == TEE_ALG_DES_ECB_NOPAD || alg == TEE_ALG_DES3_ECB_NOPAD {
4134 if element == TEE_CRYPTO_ELEMENT_NONE {
4135 return TEE_SUCCESS;
4136 }
4137 }
4138
4139 if alg == TEE_ALG_DES_CBC_NOPAD || alg == TEE_ALG_DES3_CBC_NOPAD {
4140 if element == TEE_CRYPTO_ELEMENT_NONE {
4141 return TEE_SUCCESS;
4142 }
4143 }
4144
4145 if alg == TEE_ALG_DES_CBC_MAC_NOPAD
4146 || alg == TEE_ALG_DES_CBC_MAC_PKCS5
4147 || alg == TEE_ALG_DES3_CBC_MAC_NOPAD
4148 || alg == TEE_ALG_DES3_CBC_MAC_PKCS5
4149 {
4150 if element == TEE_CRYPTO_ELEMENT_NONE {
4151 return TEE_SUCCESS;
4152 }
4153 }
4154
4155 if alg == TEE_ALG_MD5 {
4157 if element == TEE_CRYPTO_ELEMENT_NONE {
4158 return TEE_SUCCESS;
4159 }
4160 }
4161
4162 if alg == TEE_ALG_SHA1 {
4164 if element == TEE_CRYPTO_ELEMENT_NONE {
4165 return TEE_SUCCESS;
4166 }
4167 }
4168
4169 if alg == TEE_ALG_SHA224 {
4171 if element == TEE_CRYPTO_ELEMENT_NONE {
4172 return TEE_SUCCESS;
4173 }
4174 }
4175
4176 if alg == TEE_ALG_SHA256 {
4178 if element == TEE_CRYPTO_ELEMENT_NONE {
4179 return TEE_SUCCESS;
4180 }
4181 }
4182
4183 if alg == TEE_ALG_SHA384 {
4185 if element == TEE_CRYPTO_ELEMENT_NONE {
4186 return TEE_SUCCESS;
4187 }
4188 }
4189
4190 if alg == TEE_ALG_SHA512 {
4192 if element == TEE_CRYPTO_ELEMENT_NONE {
4193 return TEE_SUCCESS;
4194 }
4195 }
4196
4197 if alg == TEE_ALG_MD5SHA1 {
4199 if element == TEE_CRYPTO_ELEMENT_NONE {
4200 return TEE_SUCCESS;
4201 }
4202 }
4203
4204 if alg == TEE_ALG_HMAC_MD5 {
4206 if element == TEE_CRYPTO_ELEMENT_NONE {
4207 return TEE_SUCCESS;
4208 }
4209 }
4210
4211 if alg == TEE_ALG_HMAC_SHA1 {
4212 if element == TEE_CRYPTO_ELEMENT_NONE {
4213 return TEE_SUCCESS;
4214 }
4215 }
4216
4217 if alg == TEE_ALG_HMAC_SHA224 {
4218 if element == TEE_CRYPTO_ELEMENT_NONE {
4219 return TEE_SUCCESS;
4220 }
4221 }
4222
4223 if alg == TEE_ALG_HMAC_SHA256 {
4224 if element == TEE_CRYPTO_ELEMENT_NONE {
4225 return TEE_SUCCESS;
4226 }
4227 }
4228
4229 if alg == TEE_ALG_HMAC_SHA384 {
4230 if element == TEE_CRYPTO_ELEMENT_NONE {
4231 return TEE_SUCCESS;
4232 }
4233 }
4234
4235 if alg == TEE_ALG_HMAC_SHA512 {
4236 if element == TEE_CRYPTO_ELEMENT_NONE {
4237 return TEE_SUCCESS;
4238 }
4239 }
4240
4241 if alg == TEE_ALG_HMAC_SM3 {
4242 if element == TEE_CRYPTO_ELEMENT_NONE {
4243 return TEE_SUCCESS;
4244 }
4245 }
4246
4247 if alg == TEE_ALG_SM3 {
4249 if element == TEE_CRYPTO_ELEMENT_NONE {
4250 return TEE_SUCCESS;
4251 }
4252 }
4253
4254 if alg == TEE_ALG_SM4_ECB_NOPAD {
4256 if element == TEE_CRYPTO_ELEMENT_NONE {
4257 return TEE_SUCCESS;
4258 }
4259 }
4260
4261 if alg == TEE_ALG_SM4_CBC_NOPAD {
4262 if element == TEE_CRYPTO_ELEMENT_NONE {
4263 return TEE_SUCCESS;
4264 }
4265 }
4266
4267 if alg == TEE_ALG_SM4_CTR {
4268 if element == TEE_CRYPTO_ELEMENT_NONE {
4269 return TEE_SUCCESS;
4270 }
4271 }
4272
4273 if alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5 {
4275 if element == TEE_CRYPTO_ELEMENT_NONE {
4276 return TEE_SUCCESS;
4277 }
4278 }
4279
4280 if alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1
4281 || alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1
4282 || alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1
4283 {
4284 if element == TEE_CRYPTO_ELEMENT_NONE {
4285 return TEE_SUCCESS;
4286 }
4287 }
4288
4289 if alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1 {
4290 if element == TEE_CRYPTO_ELEMENT_NONE {
4291 return TEE_SUCCESS;
4292 }
4293 }
4294
4295 if alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224
4296 || alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224
4297 || alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224
4298 {
4299 if element == TEE_CRYPTO_ELEMENT_NONE {
4300 return TEE_SUCCESS;
4301 }
4302 }
4303
4304 if alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256
4305 || alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256
4306 || alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256
4307 {
4308 if element == TEE_CRYPTO_ELEMENT_NONE {
4309 return TEE_SUCCESS;
4310 }
4311 }
4312
4313 if alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384
4314 || alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384
4315 || alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384
4316 {
4317 if element == TEE_CRYPTO_ELEMENT_NONE {
4318 return TEE_SUCCESS;
4319 }
4320 }
4321
4322 if alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512
4323 || alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512
4324 || alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512
4325 {
4326 if element == TEE_CRYPTO_ELEMENT_NONE {
4327 return TEE_SUCCESS;
4328 }
4329 }
4330
4331 if alg == TEE_ALG_RSA_NOPAD {
4332 if element == TEE_CRYPTO_ELEMENT_NONE {
4333 return TEE_SUCCESS;
4334 }
4335 }
4336
4337 if alg == TEE_ALG_DSA_SHA1 {
4339 if element == TEE_CRYPTO_ELEMENT_NONE {
4340 return TEE_SUCCESS;
4341 }
4342 }
4343
4344 if alg == TEE_ALG_DSA_SHA224 {
4345 if element == TEE_CRYPTO_ELEMENT_NONE {
4346 return TEE_SUCCESS;
4347 }
4348 }
4349
4350 if alg == TEE_ALG_DSA_SHA256 {
4351 if element == TEE_CRYPTO_ELEMENT_NONE {
4352 return TEE_SUCCESS;
4353 }
4354 }
4355
4356 if alg == TEE_ALG_DH_DERIVE_SHARED_SECRET {
4358 if element == TEE_CRYPTO_ELEMENT_NONE {
4359 return TEE_SUCCESS;
4360 }
4361 }
4362
4363 if (alg == TEE_ALG_ECDH_P192
4365 || alg == TEE_ALG_ECDSA_P192
4366 || alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
4367 && element == TEE_ECC_CURVE_NIST_P192
4368 {
4369 return TEE_SUCCESS;
4370 }
4371
4372 if (alg == TEE_ALG_ECDH_P224
4373 || alg == TEE_ALG_ECDSA_P224
4374 || alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
4375 && element == TEE_ECC_CURVE_NIST_P224
4376 {
4377 return TEE_SUCCESS;
4378 }
4379
4380 if (alg == TEE_ALG_ECDH_P256
4381 || alg == TEE_ALG_ECDSA_P256
4382 || alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
4383 && element == TEE_ECC_CURVE_NIST_P256
4384 {
4385 return TEE_SUCCESS;
4386 }
4387
4388 if (alg == TEE_ALG_ECDH_P384
4389 || alg == TEE_ALG_ECDSA_P384
4390 || alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
4391 && element == TEE_ECC_CURVE_NIST_P384
4392 {
4393 return TEE_SUCCESS;
4394 }
4395
4396 if (alg == TEE_ALG_ECDH_P521
4397 || alg == TEE_ALG_ECDSA_P521
4398 || alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
4399 && element == TEE_ECC_CURVE_NIST_P521
4400 {
4401 return TEE_SUCCESS;
4402 }
4403
4404 if alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2 {
4406 return TEE_SUCCESS;
4407 }
4408
4409 if alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2 {
4411 return TEE_SUCCESS;
4412 }
4413
4414 if alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2 {
4416 return TEE_SUCCESS;
4417 }
4418
4419 TEE_ERROR_NOT_SUPPORTED
4420}