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 req_key_type = TEE_TYPE_RSA_KEYPAIR;
133 if mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY {
134 req_key_type2 = TEE_TYPE_RSA_PUBLIC_KEY;
135 }
136 }
137 TEE_MAIN_ALGO_DSA => {
138 req_key_type = TEE_TYPE_DSA_KEYPAIR;
139 if mode == TEE_MODE_ENCRYPT || mode == TEE_MODE_VERIFY {
140 req_key_type2 = TEE_TYPE_DSA_PUBLIC_KEY;
141 }
142 }
143 TEE_MAIN_ALGO_DH => {
144 req_key_type = TEE_TYPE_DH_KEYPAIR;
145 }
146 TEE_MAIN_ALGO_ECDSA => {
147 req_key_type = TEE_TYPE_ECDSA_KEYPAIR;
148 if mode == TEE_MODE_VERIFY {
149 req_key_type2 = TEE_TYPE_ECDSA_PUBLIC_KEY;
150 }
151 }
152 TEE_MAIN_ALGO_ECDH => {
153 req_key_type = TEE_TYPE_ECDH_KEYPAIR;
154 }
155 TEE_MAIN_ALGO_ED25519 => {
156 req_key_type = TEE_TYPE_ED25519_KEYPAIR;
157 if mode == TEE_MODE_VERIFY {
158 req_key_type2 = TEE_TYPE_ED25519_PUBLIC_KEY;
159 }
160 }
161 TEE_MAIN_ALGO_SM2_PKE => {
162 if mode == TEE_MODE_ENCRYPT {
163 req_key_type = TEE_TYPE_SM2_PKE_PUBLIC_KEY;
164 } else {
165 req_key_type = TEE_TYPE_SM2_PKE_KEYPAIR;
166 }
167 }
168 TEE_MAIN_ALGO_SM2_DSA_SM3 => {
169 if mode == TEE_MODE_VERIFY {
170 req_key_type = TEE_TYPE_SM2_DSA_PUBLIC_KEY;
171 } else {
172 req_key_type = TEE_TYPE_SM2_DSA_KEYPAIR;
173 }
174 }
175 TEE_MAIN_ALGO_SM2_KEP => {
176 req_key_type = TEE_TYPE_SM2_KEP_KEYPAIR;
177 req_key_type2 = TEE_TYPE_SM2_KEP_PUBLIC_KEY;
178 }
179 TEE_MAIN_ALGO_HKDF => {
180 req_key_type = TEE_TYPE_HKDF_IKM;
181 }
182 TEE_MAIN_ALGO_CONCAT_KDF => {
183 req_key_type = TEE_TYPE_CONCAT_KDF_Z;
184 }
185 TEE_MAIN_ALGO_PBKDF2 => {
186 req_key_type = TEE_TYPE_PBKDF2_PASSWORD;
187 }
188 TEE_MAIN_ALGO_X25519 => {
189 req_key_type = TEE_TYPE_X25519_KEYPAIR;
190 }
191 TEE_MAIN_ALGO_X448 => {
192 req_key_type = TEE_TYPE_X448_KEYPAIR;
193 }
194 _ => return Err(TEE_ERROR_BAD_PARAMETERS),
195 }
196 Ok((req_key_type, req_key_type2))
197}
198
199impl TEE_OperationHandle {
200 pub fn new(
202 info: TEE_OperationInfo,
203 key1: TEE_ObjectHandle,
204 key2: TEE_ObjectHandle,
205 operation_state: u32,
206 block_size: usize,
207 state: u32,
208 ) -> Self {
209 TEE_OperationHandle {
210 info,
211 key1,
212 key2,
213 operation_state,
214 buffer: core::ptr::null_mut(),
215 buffer_two_blocks: false,
216 block_size,
217 buffer_offs: 0,
218 state,
219 }
220 }
221
222 pub fn free_buffer(&mut self) {
224 if !self.buffer.is_null() {
225 TEE_Free(self.buffer as *mut core::ffi::c_void);
226 self.buffer = ptr::null_mut();
227 self.buffer_offs = 0;
228 }
229 }
230}
231
232impl Drop for TEE_OperationHandle {
233 fn drop(&mut self) {
234 self.free_buffer();
235 }
236}
237
238#[derive(Debug, PartialEq)]
240pub enum BufferError {
241 BufferNotAllocated,
242 InsufficientSpace,
243 AllocationFailed,
244}
245
246impl std::fmt::Display for BufferError {
247 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
248 match self {
249 BufferError::BufferNotAllocated => write!(f, "Buffer not allocated"),
250 BufferError::InsufficientSpace => write!(f, "Insufficient space in buffer"),
251 BufferError::AllocationFailed => write!(f, "Memory allocation failed"),
252 }
253 }
254}
255
256impl std::error::Error for BufferError {}
257
258impl TEE_OperationHandle {
259 pub fn allocate_buffer_v2(&mut self, size: usize) -> std::result::Result<(), BufferError> {
261 let new_buffer = TEE_Malloc(size, TEE_MALLOC_FILL_ZERO);
263 if new_buffer.is_null() {
264 return Err(BufferError::AllocationFailed);
265 }
266
267 if !self.buffer.is_null() {
269 TEE_Free(self.buffer as *mut core::ffi::c_void);
270 }
271
272 self.buffer = new_buffer as *mut u8;
273 self.buffer_offs = 0;
274 Ok(())
275 }
276}
277
278#[derive(Debug, PartialEq)]
280enum OperationConfig {
281 Digest {
282 block_size: usize,
283 },
284 Cipher {
285 block_size: usize,
286 buffer_two_blocks: bool,
287 req_key_usage: u32,
288 with_private_key: bool,
289 },
290 AsymmetricSignature {
291 req_key_usage: u32,
292 with_private_key: bool,
293 },
294 AsymmetricEncryption {
295 req_key_usage: u32,
296 with_private_key: bool,
297 },
298 KeyDerivation {
299 req_key_usage: u32,
300 },
301 Mac {
302 req_key_usage: u32,
303 },
304}
305
306fn validate_algorithm_params(
308 algorithm: u32,
309 mode: u32,
310 max_key_size: u32,
311) -> Result<OperationConfig, TEE_Result> {
312 use crate::tee_api_defines::*;
313
314 match algorithm {
316 TEE_ALG_DSA_SHA1 => {
317 if max_key_size < 512 || max_key_size > 1024 || max_key_size % 64 != 0 {
318 return Err(TEE_ERROR_NOT_SUPPORTED);
319 }
320 }
321 TEE_ALG_DSA_SHA224 => {
322 if max_key_size != 2048 {
323 return Err(TEE_ERROR_NOT_SUPPORTED);
324 }
325 }
326 TEE_ALG_DSA_SHA256 => {
327 if max_key_size != 2048 && max_key_size != 3072 {
328 return Err(TEE_ERROR_NOT_SUPPORTED);
329 }
330 }
331 TEE_ALG_ECDSA_P192 | TEE_ALG_ECDH_P192 => {
332 if max_key_size != 192 {
333 return Err(TEE_ERROR_NOT_SUPPORTED);
334 }
335 }
336 TEE_ALG_ECDSA_P224 | TEE_ALG_ECDH_P224 => {
337 if max_key_size != 224 {
338 return Err(TEE_ERROR_NOT_SUPPORTED);
339 }
340 }
341 TEE_ALG_ECDSA_P256 | TEE_ALG_ECDH_P256 | TEE_ALG_SM2_PKE | TEE_ALG_SM2_DSA_SM3 => {
342 if max_key_size != 256 {
343 return Err(TEE_ERROR_NOT_SUPPORTED);
344 }
345 }
346 TEE_ALG_SM2_KEP => {
347 if max_key_size != 512 {
348 return Err(TEE_ERROR_NOT_SUPPORTED);
349 }
350 }
351 TEE_ALG_ECDSA_P384 | TEE_ALG_ECDH_P384 => {
352 if max_key_size != 384 {
353 return Err(TEE_ERROR_NOT_SUPPORTED);
354 }
355 }
356 TEE_ALG_ECDSA_P521 | TEE_ALG_ECDH_P521 => {
357 if max_key_size != 521 {
358 return Err(TEE_ERROR_NOT_SUPPORTED);
359 }
360 }
361 _ => {}
362 }
363
364 match algorithm {
366 TEE_ALG_MD5 | TEE_ALG_SHA1 | TEE_ALG_SHA224 | TEE_ALG_SHA256 | TEE_ALG_SHA384
368 | TEE_ALG_SHA512 | TEE_ALG_SM3 => {
369 if mode != TEE_MODE_DIGEST {
370 return Err(TEE_ERROR_NOT_SUPPORTED);
371 }
372 let digest_length = match algorithm & 0x000000FF {
373 0x01 => 16, 0x02 => 20, 0x03 => 28, 0x04 => 32, 0x05 => 48, 0x06 => 64, 0x10 => 32, _ => 0,
381 };
382 Ok(OperationConfig::Digest {
383 block_size: digest_length,
384 })
385 }
386
387 TEE_ALG_DES_CBC_MAC_NOPAD
389 | TEE_ALG_AES_CBC_MAC_NOPAD
390 | TEE_ALG_AES_CBC_MAC_PKCS5
391 | TEE_ALG_AES_CMAC
392 | TEE_ALG_DES_CBC_MAC_PKCS5
393 | TEE_ALG_DES3_CBC_MAC_NOPAD
394 | TEE_ALG_DES3_CBC_MAC_PKCS5
395 | TEE_ALG_HMAC_MD5
396 | TEE_ALG_HMAC_SHA1
397 | TEE_ALG_HMAC_SHA224
398 | TEE_ALG_HMAC_SHA256
399 | TEE_ALG_HMAC_SHA384
400 | TEE_ALG_HMAC_SHA512
401 | TEE_ALG_HMAC_SM3 => {
402 if mode != TEE_MODE_MAC {
403 return Err(TEE_ERROR_NOT_SUPPORTED);
404 }
405 Ok(OperationConfig::Mac {
406 req_key_usage: TEE_USAGE_MAC,
407 })
408 }
409
410 TEE_ALG_AES_ECB_NOPAD
412 | TEE_ALG_AES_CBC_NOPAD
413 | TEE_ALG_AES_CCM
414 | TEE_ALG_DES_ECB_NOPAD
415 | TEE_ALG_DES_CBC_NOPAD
416 | TEE_ALG_DES3_ECB_NOPAD
417 | TEE_ALG_DES3_CBC_NOPAD
418 | TEE_ALG_SM4_ECB_NOPAD
419 | TEE_ALG_SM4_CBC_NOPAD
420 | TEE_ALG_SM4_CTR => {
421 let main_alg = (algorithm & 0x00FF0000) >> 16;
422 let block_size = match main_alg {
423 0x1000 => 16, 0x4000 => 16, _ => 8,
426 };
427 Ok(OperationConfig::Cipher {
428 block_size,
429 buffer_two_blocks: false,
430 req_key_usage: 0, with_private_key: false, })
433 }
434
435 TEE_ALG_AES_CTS => {
437 let main_alg = (algorithm & 0x00FF0000) >> 16;
438 let block_size = match main_alg {
439 0x1000 => 16, 0x4000 => 16, _ => return Err(TEE_ERROR_NOT_SUPPORTED),
442 };
443 Ok(OperationConfig::Cipher {
444 block_size,
445 buffer_two_blocks: true,
446 req_key_usage: 0,
447 with_private_key: false,
448 })
449 }
450
451 TEE_ALG_AES_CTR | TEE_ALG_AES_GCM | TEE_ALG_SM4_GCM => {
453 let block_size = 16;
454 match mode {
455 TEE_MODE_ENCRYPT => Ok(OperationConfig::Cipher {
456 block_size,
457 buffer_two_blocks: false,
458 req_key_usage: TEE_USAGE_ENCRYPT,
459 with_private_key: false,
460 }),
461 TEE_MODE_DECRYPT => Ok(OperationConfig::Cipher {
462 block_size,
463 buffer_two_blocks: false,
464 req_key_usage: TEE_USAGE_DECRYPT,
465 with_private_key: true,
466 }),
467 _ => Err(TEE_ERROR_NOT_SUPPORTED),
468 }
469 }
470
471 TEE_ALG_ECDSA_P192 | TEE_ALG_ECDSA_P224 | TEE_ALG_ECDSA_P256 | TEE_ALG_ECDSA_P384
473 | TEE_ALG_ECDSA_P521 | TEE_ALG_SM2_DSA_SM3 => match mode {
474 TEE_MODE_SIGN => Ok(OperationConfig::AsymmetricSignature {
475 req_key_usage: TEE_USAGE_SIGN,
476 with_private_key: true,
477 }),
478 TEE_MODE_VERIFY => Ok(OperationConfig::AsymmetricSignature {
479 req_key_usage: TEE_USAGE_VERIFY,
480 with_private_key: false,
481 }),
482 _ => Err(TEE_ERROR_NOT_SUPPORTED),
483 },
484
485 TEE_ALG_RSAES_PKCS1_V1_5
487 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1
488 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224
489 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256
490 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384
491 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512
492 | TEE_ALG_SM2_PKE => match mode {
493 TEE_MODE_ENCRYPT => Ok(OperationConfig::AsymmetricEncryption {
494 req_key_usage: TEE_USAGE_ENCRYPT,
495 with_private_key: false,
496 }),
497 TEE_MODE_DECRYPT => Ok(OperationConfig::AsymmetricEncryption {
498 req_key_usage: TEE_USAGE_DECRYPT,
499 with_private_key: true,
500 }),
501 _ => Err(TEE_ERROR_NOT_SUPPORTED),
502 },
503
504 TEE_ALG_RSA_NOPAD => match mode {
506 TEE_MODE_ENCRYPT => Ok(OperationConfig::AsymmetricEncryption {
507 req_key_usage: TEE_USAGE_ENCRYPT | TEE_USAGE_VERIFY,
508 with_private_key: false,
509 }),
510 TEE_MODE_DECRYPT => Ok(OperationConfig::AsymmetricEncryption {
511 req_key_usage: TEE_USAGE_DECRYPT | TEE_USAGE_SIGN,
512 with_private_key: true,
513 }),
514 _ => Err(TEE_ERROR_NOT_SUPPORTED),
515 },
516
517 TEE_ALG_DH_DERIVE_SHARED_SECRET
519 | TEE_ALG_ECDH_P192
520 | TEE_ALG_ECDH_P224
521 | TEE_ALG_ECDH_P256
522 | TEE_ALG_ECDH_P384
523 | TEE_ALG_ECDH_P521
524 | TEE_ALG_SM2_KEP => {
525 if mode != TEE_MODE_DERIVE {
526 return Err(TEE_ERROR_NOT_SUPPORTED);
527 }
528 Ok(OperationConfig::KeyDerivation {
529 req_key_usage: TEE_USAGE_DERIVE,
530 })
531 }
532
533 _ => Err(TEE_ERROR_NOT_SUPPORTED),
534 }
535}
536
537pub fn tee_alg_get_class(algo: u32) -> u32 {
539 match algo {
540 TEE_ALG_SM2_PKE => TEE_OPERATION_ASYMMETRIC_CIPHER,
541 TEE_ALG_SM2_KEP => TEE_OPERATION_KEY_DERIVATION,
542 TEE_ALG_RSASSA_PKCS1_V1_5_MD5
543 | TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1
544 | TEE_ALG_RSASSA_PKCS1_V1_5_SHA1
545 | TEE_ALG_RSASSA_PKCS1_V1_5_SHA224
546 | TEE_ALG_RSASSA_PKCS1_V1_5_SHA256
547 | TEE_ALG_RSASSA_PKCS1_V1_5_SHA384
548 | TEE_ALG_RSASSA_PKCS1_V1_5_SHA512 => TEE_OPERATION_ASYMMETRIC_SIGNATURE,
549 TEE_ALG_DES3_CBC_MAC_NOPAD | TEE_ALG_DES3_CBC_MAC_PKCS5 => TEE_OPERATION_MAC,
550 TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1
551 | TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224
552 | TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256
553 | TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384
554 | TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512 => TEE_OPERATION_ASYMMETRIC_SIGNATURE,
555 TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1
556 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224
557 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256
558 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384
559 | TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512 => TEE_OPERATION_ASYMMETRIC_CIPHER,
560 _ => (algo >> 28) & 0xF,
561 }
562}
563
564#[unsafe(no_mangle)]
594pub extern "C" fn TEE_AllocateOperation(
595 operation: *mut *mut TEE_OperationHandle,
596 algorithm: u32,
597 mode: u32,
598 max_key_size: u32,
599) -> TEE_Result {
600 use crate::tee_api_defines::*;
601
602 if operation.is_null() {
604 TEE_Panic(0);
605 return TEE_ERROR_GENERIC;
606 }
607
608 let config = match validate_algorithm_params(algorithm, mode, max_key_size) {
610 Ok(c) => c,
611 Err(e) => return e,
612 };
613
614 let operation_class = tee_alg_get_class(algorithm);
616 let digest_length = match algorithm & 0x000000FF {
617 0x01 => 16, 0x02 => 20, 0x03 => 28, 0x04 => 32, 0x05 => 48, 0x06 => 64, 0x10 => 32, _ => 0,
625 };
626 let _main_alg = (algorithm & 0x00FF0000) >> 16;
627
628 let mut handle_state = 0u32;
629
630 if algorithm == TEE_ALG_SM2_KEP {
632 handle_state = TEE_HANDLE_FLAG_EXPECT_TWO_KEYS;
633 }
634
635 let (block_size, buffer_two_blocks, req_key_usage, _) = match config {
636 OperationConfig::Digest { block_size } => {
637 handle_state |= TEE_HANDLE_FLAG_KEY_SET;
638 (block_size, false, 0, false)
639 }
640 OperationConfig::Cipher {
641 block_size,
642 buffer_two_blocks,
643 req_key_usage,
644 with_private_key,
645 } => (
646 block_size,
647 buffer_two_blocks,
648 req_key_usage,
649 with_private_key,
650 ),
651 OperationConfig::AsymmetricSignature {
652 req_key_usage,
653 with_private_key,
654 } => (1, false, req_key_usage, with_private_key),
655 OperationConfig::AsymmetricEncryption {
656 req_key_usage,
657 with_private_key,
658 } => (1, false, req_key_usage, with_private_key),
659 OperationConfig::KeyDerivation { req_key_usage } => (1, false, req_key_usage, true),
660 OperationConfig::Mac { req_key_usage } => (1, false, req_key_usage, false),
661 };
662
663 let op_info = TEE_OperationInfo {
665 algorithm,
666 operationClass: operation_class,
667 mode,
668 digestLength: digest_length,
669 maxKeySize: max_key_size,
670 keySize: 0,
671 requiredKeyUsage: req_key_usage,
672 handleState: handle_state,
673 };
674
675 let operation_ptr = TEE_Malloc(
677 core::mem::size_of::<TEE_OperationHandle>(),
678 TEE_MALLOC_FILL_ZERO,
679 );
680 if operation_ptr.is_null() {
681 return TEE_ERROR_OUT_OF_MEMORY;
682 }
683
684 let op_handle = unsafe { &mut *(operation_ptr as *mut TEE_OperationHandle) };
686 *op_handle = TEE_OperationHandle::new(
687 op_info,
688 ptr::null_mut(),
689 ptr::null_mut(),
690 TEE_OPERATION_STATE_INITIAL,
691 block_size,
692 0,
693 );
694
695 if block_size > 1 {
697 let buffer_size = if buffer_two_blocks {
698 block_size * 2
699 } else {
700 block_size
701 };
702 if op_handle.allocate_buffer_v2(buffer_size).is_err() {
703 TEE_Free(operation_ptr);
704 return TEE_ERROR_OUT_OF_MEMORY;
705 }
706 }
707 op_handle.buffer_two_blocks = buffer_two_blocks;
708
709 if operation_class != TEE_OPERATION_DIGEST {
711 let mut mks = max_key_size;
712 let res = TEE_ALG_GET_KEY_TYPE(algorithm, mode);
713 let (key_type, key_type2) = match res {
714 Ok(res) => res,
715 Err(e) => return e,
716 };
717
718 if handle_state & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS != 0 {
719 mks /= 2;
720 }
721
722 let mut key1_ptr = core::ptr::null_mut();
723 let res = TEE_AllocateTransientObject(key_type, mks, &mut key1_ptr);
724 if res != TEE_SUCCESS {
725 TEE_Free(operation_ptr);
726 return res;
727 }
728 op_handle.key1 = key1_ptr;
729
730 if handle_state & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS != 0 {
731 let mut key2_ptr = core::ptr::null_mut();
732 let res = TEE_AllocateTransientObject(key_type2, mks, &mut key2_ptr);
733 if res != TEE_SUCCESS {
734 TEE_FreeTransientObject(op_handle.key1);
735 TEE_Free(operation_ptr);
736 return res;
737 }
738 op_handle.key2 = key2_ptr;
739 }
740 }
741
742 let mut state = 0u32;
744 let res = unsafe {
745 _utee_cryp_state_alloc(
746 algorithm as u64,
747 mode as u64,
748 op_handle.key1 as u64,
749 op_handle.key2 as u64,
750 &mut state,
751 )
752 };
753 if res != TEE_SUCCESS as usize {
754 TEE_FreeTransientObject(op_handle.key1);
755 TEE_FreeTransientObject(op_handle.key2);
756 TEE_Free(operation_ptr);
757 return res as TEE_Result;
758 }
759 op_handle.state = state;
760
761 if operation_class == TEE_OPERATION_DIGEST {
763 let res = unsafe { _utee_hash_init(state as u64, core::ptr::null(), 0) };
764 if res != TEE_SUCCESS as usize {
765 TEE_FreeTransientObject(op_handle.key1);
766 TEE_FreeTransientObject(op_handle.key2);
767 TEE_Free(operation_ptr);
768 return res as TEE_Result;
769 }
770 op_handle.operation_state |= TEE_HANDLE_FLAG_INITIALIZED;
771 }
772
773 op_handle.operation_state = TEE_OPERATION_STATE_INITIAL;
774
775 unsafe {
777 *operation = operation_ptr as *mut TEE_OperationHandle;
778 }
779
780 TEE_SUCCESS
781}
782
783#[unsafe(no_mangle)]
787pub extern "C" fn TEE_FreeOperation(operation: *mut TEE_OperationHandle) {
788 use crate::tee_api_defines::*;
789
790 if operation.is_null() {
792 return;
793 }
794
795 let op_handle = unsafe { &*operation };
797
798 if !op_handle.buffer.is_null() {
800 TEE_Free(op_handle.buffer as *mut core::ffi::c_void);
801 }
802 let res = unsafe { _utee_cryp_state_free(op_handle.state as u64) };
809 if res != TEE_SUCCESS as usize {
810 TEE_Panic(res as u32);
811 }
812}
813
814#[unsafe(no_mangle)]
823pub extern "C" fn TEE_GetOperationInfo(
824 operation: *mut TEE_OperationHandle,
825 operationInfo: *mut TEE_OperationInfo,
826) {
827 use crate::tee_api_defines::*;
828
829 if operation.is_null() {
831 TEE_Panic(0);
832 }
833
834 if operationInfo.is_null() {
836 TEE_Panic(0);
837 }
838
839 if cfg!(feature = "strict_annotation_checks") {
841 let res = TEE_CheckMemoryAccessRights(
842 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
843 operationInfo as *mut core::ffi::c_void,
844 std::mem::size_of::<TEE_OperationInfo>(),
845 );
846 if res != 0 {
847 eprintln!("[out] operationInfo: error {:#010x}", res);
848 TEE_Panic(0);
849 }
850 }
851
852 let op_handle = unsafe { &*operation };
854 unsafe {
855 *operationInfo = op_handle.info;
856 }
857
858 unsafe {
860 if (*operationInfo).handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS != 0 {
861 (*operationInfo).keySize = 0;
862 (*operationInfo).requiredKeyUsage = 0;
863 }
864 }
865}
866
867#[unsafe(no_mangle)]
879pub extern "C" fn TEE_GetOperationInfoMultiple(
880 operation: *mut TEE_OperationHandle,
881 op_info: *mut TEE_OperationInfoMultiple,
882 size: *mut usize,
883) -> TEE_Result {
884 use crate::tee_api_defines::*;
885
886 if operation.is_null() {
888 return TEE_ERROR_BAD_PARAMETERS;
889 }
890
891 if op_info.is_null() || size.is_null() {
893 TEE_Panic(0);
894 return TEE_ERROR_BAD_PARAMETERS;
895 }
896
897 if cfg!(feature = "strict_annotation_checks") {
899 let buffer_size = unsafe { *size };
900 let res = TEE_CheckMemoryAccessRights(
901 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
902 op_info as *mut core::ffi::c_void,
903 buffer_size,
904 );
905 if res != 0 {
906 eprintln!("[out] op_info: error {:#010x}", res);
907 TEE_Panic(0);
908 }
909
910 let res = TEE_CheckMemoryAccessRights(
911 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
912 size as *mut core::ffi::c_void,
913 std::mem::size_of::<usize>(),
914 );
915 if res != 0 {
916 eprintln!("[out] size: error {:#010x}", res);
917 TEE_Panic(0);
918 }
919 }
920
921 let op_info_size = std::mem::size_of::<TEE_OperationInfoMultiple>();
923 let buffer_size = unsafe { *size };
924 if buffer_size < op_info_size {
925 return TEE_ERROR_BAD_PARAMETERS;
926 }
927
928 let key_info_size = std::mem::size_of::<TEE_OperationInfoKey>();
930 let max_key_count = (buffer_size - op_info_size) / key_info_size;
931
932 TEE_MemFill(op_info as *mut core::ffi::c_void, 0, buffer_size);
934
935 let op_handle = unsafe { &mut *operation };
937 let two_keys = (op_handle.info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 0;
938
939 let result = if op_handle.info.mode == TEE_MODE_DIGEST {
940 unsafe {
942 (*op_info).numberOfKeys = 0;
943 }
944 TEE_SUCCESS
945 } else if !two_keys {
946 if max_key_count < 1 {
948 return TEE_ERROR_SHORT_BUFFER;
949 }
950
951 let mut kinfo = TEE_ObjectInfo {
952 objectType: 0,
953 objectSize: 0,
954 maxObjectSize: 0,
955 objectUsage: 0,
956 dataSize: 0,
957 dataPosition: 0,
958 handleFlags: 0,
959 };
960
961 let res = TEE_GetObjectInfo1(op_handle.key1, &mut kinfo);
962 if res != TEE_SUCCESS {
963 return check_operation_info_multiple_result(res);
964 }
965
966 unsafe {
967 (*op_info)
968 .keyInformation
969 .add(0)
970 .write(TEE_OperationInfoKey {
971 keySize: kinfo.objectSize,
972 requiredKeyUsage: op_handle.info.requiredKeyUsage,
973 });
974 (*op_info).numberOfKeys = 1;
975 }
976
977 TEE_SUCCESS
978 } else {
979 if max_key_count < 2 {
981 return TEE_ERROR_SHORT_BUFFER;
982 }
983
984 let mut kinfo = TEE_ObjectInfo {
985 objectType: 0,
986 objectSize: 0,
987 maxObjectSize: 0,
988 objectUsage: 0,
989 dataSize: 0,
990 dataPosition: 0,
991 handleFlags: 0,
992 };
993
994 let res = TEE_GetObjectInfo1(op_handle.key1, &mut kinfo);
996 if res != TEE_SUCCESS {
997 return check_operation_info_multiple_result(res);
998 }
999
1000 unsafe {
1001 (*op_info)
1002 .keyInformation
1003 .add(0)
1004 .write(TEE_OperationInfoKey {
1005 keySize: kinfo.objectSize,
1006 requiredKeyUsage: op_handle.info.requiredKeyUsage,
1007 });
1008 }
1009
1010 let res = TEE_GetObjectInfo1(op_handle.key2, &mut kinfo);
1012 if res != TEE_SUCCESS {
1013 return check_operation_info_multiple_result(res);
1014 }
1015
1016 unsafe {
1017 (*op_info)
1018 .keyInformation
1019 .add(1)
1020 .write(TEE_OperationInfoKey {
1021 keySize: kinfo.objectSize,
1022 requiredKeyUsage: op_handle.info.requiredKeyUsage,
1023 });
1024 (*op_info).numberOfKeys = 2;
1025 }
1026
1027 TEE_SUCCESS
1028 };
1029
1030 if result == TEE_SUCCESS {
1032 unsafe {
1033 (*op_info).algorithm = op_handle.info.algorithm;
1034 (*op_info).operationClass = op_handle.info.operationClass;
1035 (*op_info).mode = op_handle.info.mode;
1036 (*op_info).digestLength = op_handle.info.digestLength;
1037 (*op_info).maxKeySize = op_handle.info.maxKeySize;
1038 (*op_info).handleState = op_handle.info.handleState;
1039 (*op_info).operationState = op_handle.operation_state;
1040 }
1041 }
1042
1043 check_operation_info_multiple_result(result)
1044}
1045
1046fn check_operation_info_multiple_result(res: TEE_Result) -> TEE_Result {
1048 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
1049 TEE_Panic(res as u32);
1050 }
1051 res
1052}
1053
1054fn reset_operation_state(operation: &mut TEE_OperationHandle) {
1066 operation.operation_state = TEE_OPERATION_STATE_INITIAL;
1068 operation.buffer_offs = 0;
1070
1071 if operation.info.operationClass == TEE_OPERATION_DIGEST {
1072 let res = unsafe { _utee_hash_init(operation.state as u64, core::ptr::null(), 0) };
1073 if res != TEE_SUCCESS as usize {
1074 TEE_Panic(res as u32);
1075 }
1076 operation.info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1077 } else {
1078 operation.info.handleState &= !TEE_HANDLE_FLAG_INITIALIZED;
1079 }
1080}
1081
1082#[unsafe(no_mangle)]
1090pub extern "C" fn TEE_ResetOperation(operation: *mut TEE_OperationHandle) {
1091 if operation.is_null() {
1093 TEE_Panic(TEE_PANIC_ID_TEE_RESETOPERATION);
1094 }
1095 let op_handle = unsafe { &*operation };
1096 if (op_handle.info.handleState & TEE_HANDLE_FLAG_KEY_SET) != 0 {
1097 TEE_Panic(0);
1098 }
1099 unsafe {
1101 reset_operation_state(&mut *operation);
1102 }
1103}
1104
1105#[unsafe(no_mangle)]
1121pub extern "C" fn TEE_SetOperationKey(
1122 operation: *mut TEE_OperationHandle,
1123 key: TEE_ObjectHandle,
1124) -> TEE_Result {
1125 if operation.is_null() {
1127 TEE_Panic(TEE_PANIC_ID_TEE_SETOPERATIONKEY);
1128 return TEE_ERROR_BAD_PARAMETERS;
1129 }
1130
1131 let op_handle = unsafe { &mut *operation };
1133
1134 if key.is_null() {
1136 TEE_ResetTransientObject(op_handle.key1);
1138 op_handle.info.handleState &= !TEE_HANDLE_FLAG_KEY_SET;
1139
1140 if op_handle.operation_state != TEE_OPERATION_STATE_INITIAL {
1142 reset_operation_state(op_handle);
1143 }
1144 return TEE_SUCCESS;
1145 }
1146
1147 if op_handle.info.operationClass == TEE_OPERATION_DIGEST {
1149 TEE_Panic(TEE_PANIC_ID_TEE_SETOPERATIONKEY);
1150 return TEE_ERROR_BAD_PARAMETERS;
1151 }
1152
1153 if (op_handle.info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) != 0 {
1155 TEE_Panic(TEE_PANIC_ID_TEE_SETOPERATIONKEY);
1156 return TEE_ERROR_BAD_PARAMETERS;
1157 }
1158
1159 let mut key_info = TEE_ObjectInfo {
1161 objectType: 0,
1162 objectSize: 0,
1163 maxObjectSize: 0,
1164 objectUsage: 0,
1165 dataSize: 0,
1166 dataPosition: 0,
1167 handleFlags: 0,
1168 };
1169
1170 let res = TEE_GetObjectInfo1(key, &mut key_info);
1171 if res != TEE_SUCCESS {
1172 TEE_Panic(TEE_PANIC_ID_TEE_SETOPERATIONKEY);
1173 return TEE_ERROR_BAD_PARAMETERS;
1174 }
1175
1176 if (key_info.objectUsage & op_handle.info.requiredKeyUsage) != op_handle.info.requiredKeyUsage {
1178 TEE_Panic(TEE_PANIC_ID_TEE_SETOPERATIONKEY);
1179 return TEE_ERROR_SECURITY;
1180 }
1181
1182 if op_handle.info.maxKeySize < key_info.objectSize {
1184 TEE_Panic(TEE_PANIC_ID_TEE_SETOPERATIONKEY);
1185 return TEE_ERROR_BAD_PARAMETERS;
1186 }
1187
1188 TEE_ResetTransientObject(op_handle.key1);
1190 op_handle.info.handleState &= !TEE_HANDLE_FLAG_KEY_SET;
1191
1192 let res = TEE_CopyObjectAttributes1(op_handle.key1, key);
1194 if res != TEE_SUCCESS {
1195 TEE_Panic(TEE_PANIC_ID_TEE_SETOPERATIONKEY);
1196 return TEE_ERROR_BAD_PARAMETERS;
1197 }
1198
1199 op_handle.info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
1201
1202 op_handle.info.keySize = key_info.objectSize;
1204
1205 if op_handle.operation_state != TEE_OPERATION_STATE_INITIAL {
1207 reset_operation_state(op_handle);
1208 }
1209
1210 TEE_SUCCESS
1211}
1212
1213#[unsafe(no_mangle)]
1229pub extern "C" fn TEE_SetOperationKey2(
1230 operation: *mut TEE_OperationHandle,
1231 key1: TEE_ObjectHandle,
1232 key2: TEE_ObjectHandle,
1233) -> TEE_Result {
1234 if operation.is_null() {
1236 return TEE_ERROR_BAD_PARAMETERS;
1237 }
1238
1239 let op_handle = unsafe { &mut *operation };
1240
1241 if !operation.is_null() && !key1.is_null() && !key2.is_null() && key1 == key2 {
1243 return TEE_ERROR_SECURITY;
1244 }
1245
1246 if key1.is_null() && key2.is_null() {
1248 TEE_ResetTransientObject(op_handle.key1);
1250 TEE_ResetTransientObject(op_handle.key2);
1251 op_handle.info.handleState &= !TEE_HANDLE_FLAG_KEY_SET;
1252 if op_handle.operation_state != TEE_OPERATION_STATE_INITIAL {
1253 reset_operation_state(op_handle);
1254 }
1255 return TEE_SUCCESS;
1256 } else if key1.is_null() || key2.is_null() {
1257 return TEE_ERROR_BAD_PARAMETERS;
1259 }
1260
1261 if op_handle.info.operationClass == TEE_OPERATION_DIGEST {
1263 return TEE_ERROR_BAD_PARAMETERS;
1264 }
1265
1266 if (op_handle.info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0 {
1268 return TEE_ERROR_BAD_PARAMETERS;
1269 }
1270
1271 let mut key_info1 = TEE_ObjectInfo {
1273 objectType: 0,
1274 objectSize: 0,
1275 maxObjectSize: 0,
1276 objectUsage: 0,
1277 dataSize: 0,
1278 dataPosition: 0,
1279 handleFlags: 0,
1280 };
1281
1282 let mut res = TEE_GetObjectInfo1(key1, &mut key_info1);
1283 if res != TEE_SUCCESS {
1284 return handle_result_error(res);
1285 }
1286
1287 if (key_info1.objectUsage & op_handle.info.requiredKeyUsage) != op_handle.info.requiredKeyUsage
1289 {
1290 return TEE_ERROR_BAD_PARAMETERS;
1291 }
1292
1293 let mut key_info2 = TEE_ObjectInfo {
1295 objectType: 0,
1296 objectSize: 0,
1297 maxObjectSize: 0,
1298 objectUsage: 0,
1299 dataSize: 0,
1300 dataPosition: 0,
1301 handleFlags: 0,
1302 };
1303
1304 res = TEE_GetObjectInfo1(key2, &mut key_info2);
1305 if res != TEE_SUCCESS {
1306 return if res == TEE_ERROR_CORRUPT_OBJECT {
1307 TEE_ERROR_CORRUPT_OBJECT_2
1308 } else {
1309 handle_result_error(res)
1310 };
1311 }
1312
1313 if (key_info2.objectUsage & op_handle.info.requiredKeyUsage) != op_handle.info.requiredKeyUsage
1315 {
1316 return TEE_ERROR_BAD_PARAMETERS;
1317 }
1318
1319 if key_info1.objectSize != key_info2.objectSize {
1321 return TEE_ERROR_BAD_PARAMETERS;
1322 }
1323
1324 if op_handle.info.maxKeySize < key_info1.objectSize {
1326 return TEE_ERROR_BAD_PARAMETERS;
1327 }
1328
1329 TEE_ResetTransientObject(op_handle.key1);
1331 TEE_ResetTransientObject(op_handle.key2);
1332 op_handle.info.handleState &= !TEE_HANDLE_FLAG_KEY_SET;
1333
1334 res = TEE_CopyObjectAttributes1(op_handle.key1, key1);
1336 if res != TEE_SUCCESS {
1337 return handle_result_error(res);
1338 }
1339
1340 res = TEE_CopyObjectAttributes1(op_handle.key2, key2);
1341 if res != TEE_SUCCESS {
1342 return if res == TEE_ERROR_CORRUPT_OBJECT {
1343 TEE_ERROR_CORRUPT_OBJECT_2
1344 } else {
1345 handle_result_error(res)
1346 };
1347 }
1348
1349 op_handle.info.handleState |= TEE_HANDLE_FLAG_KEY_SET;
1351
1352 op_handle.info.keySize = key_info1.objectSize;
1354
1355 if op_handle.operation_state != TEE_OPERATION_STATE_INITIAL {
1357 reset_operation_state(op_handle);
1358 }
1359
1360 TEE_SUCCESS
1361}
1362
1363fn handle_result_error(res: TEE_Result) -> TEE_Result {
1365 match res {
1366 TEE_SUCCESS => TEE_SUCCESS,
1367 TEE_ERROR_CORRUPT_OBJECT => TEE_ERROR_CORRUPT_OBJECT,
1368 TEE_ERROR_CORRUPT_OBJECT_2 => TEE_ERROR_CORRUPT_OBJECT_2,
1369 TEE_ERROR_STORAGE_NOT_AVAILABLE => TEE_ERROR_STORAGE_NOT_AVAILABLE,
1370 TEE_ERROR_STORAGE_NOT_AVAILABLE_2 => TEE_ERROR_STORAGE_NOT_AVAILABLE_2,
1371 _ => {
1372 TEE_Panic(res as u32);
1373 res
1374 }
1375 }
1376}
1377
1378#[unsafe(no_mangle)]
1391pub extern "C" fn TEE_CopyOperation(
1392 dst_op: *mut TEE_OperationHandle,
1393 src_op: *mut TEE_OperationHandle,
1394) {
1395 if dst_op.is_null() || src_op.is_null() {
1397 TEE_Panic(0);
1398 }
1399
1400 let (dst_ref, src_ref) = unsafe { (&mut *dst_op, &*src_op) };
1402
1403 if dst_ref.info.algorithm != src_ref.info.algorithm {
1405 TEE_Panic(0);
1406 }
1407 if dst_ref.info.mode != src_ref.info.mode {
1408 TEE_Panic(0);
1409 }
1410
1411 if src_ref.info.operationClass != TEE_OPERATION_DIGEST {
1413 let (key1, key2) = if (src_ref.info.handleState & TEE_HANDLE_FLAG_KEY_SET) != 0 {
1414 (src_ref.key1, src_ref.key2)
1415 } else {
1416 (ptr::null_mut(), ptr::null_mut())
1417 };
1418
1419 if (src_ref.info.handleState & TEE_HANDLE_FLAG_EXPECT_TWO_KEYS) == 0 {
1420 TEE_SetOperationKey(dst_op, key1);
1421 } else {
1422 let res = TEE_SetOperationKey2(dst_op, key1, key2);
1424 if res != TEE_SUCCESS {
1425 TEE_Panic(res as u32);
1426 }
1427 }
1428 }
1429
1430 dst_ref.info.handleState = src_ref.info.handleState;
1432 dst_ref.info.keySize = src_ref.info.keySize;
1433 dst_ref.info.digestLength = src_ref.info.digestLength;
1434 dst_ref.operation_state = src_ref.operation_state;
1435
1436 if dst_ref.buffer_two_blocks != src_ref.buffer_two_blocks
1438 || dst_ref.block_size != src_ref.block_size
1439 {
1440 TEE_Panic(0);
1441 }
1442
1443 if !dst_ref.buffer.is_null() {
1445 if src_ref.buffer.is_null() {
1446 TEE_Panic(0);
1447 }
1448
1449 let sz = if src_ref.buffer_two_blocks {
1450 src_ref.block_size * 2
1451 } else {
1452 src_ref.block_size
1453 };
1454
1455 unsafe {
1457 std::ptr::copy_nonoverlapping(src_ref.buffer, dst_ref.buffer, sz);
1458 }
1459 dst_ref.buffer_offs = src_ref.buffer_offs;
1460 } else if !src_ref.buffer.is_null() {
1461 TEE_Panic(0);
1462 }
1463
1464 let res = unsafe { _utee_cryp_state_copy(dst_ref.state as u64, src_ref.state as u64) };
1466 if res != TEE_SUCCESS as usize {
1467 TEE_Panic(res as u32);
1468 }
1469}
1470
1471fn init_hash_operation(
1482 operation: *mut TEE_OperationHandle,
1483 iv: *const core::ffi::c_void,
1484 iv_len: u32,
1485) {
1486 if operation.is_null() {
1488 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
1489 return;
1490 }
1491
1492 let res = unsafe { _utee_hash_init((*operation).state as u64, iv, iv_len as usize) };
1493 if res != TEE_SUCCESS as usize {
1494 TEE_Panic(res as u32);
1495 }
1496
1497 unsafe {
1499 (*operation).buffer_offs = 0;
1500 (*operation).info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1502 }
1503}
1504
1505#[unsafe(no_mangle)]
1519pub extern "C" fn TEE_DigestUpdate(
1520 operation: *mut TEE_OperationHandle,
1521 chunk: *const core::ffi::c_void,
1522 chunk_size: usize,
1523) {
1524 if operation.is_null() {
1526 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
1527 return;
1528 }
1529
1530 let op_handle = unsafe { &mut *operation };
1532
1533 if op_handle.info.operationClass != TEE_OPERATION_DIGEST {
1534 TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
1535 return;
1536 }
1537
1538 op_handle.operation_state = TEE_OPERATION_STATE_ACTIVE;
1540
1541 let res = unsafe { _utee_hash_update(op_handle.state as u64, chunk, chunk_size) };
1543
1544 if res != TEE_SUCCESS as usize {
1545 TEE_Panic(res as u32);
1546 }
1547}
1548
1549#[unsafe(no_mangle)]
1563pub extern "C" fn TEE_DigestDoFinal(
1564 operation: *mut TEE_OperationHandle,
1565 chunk: *const core::ffi::c_void,
1566 chunk_len: usize,
1567 hash: *mut core::ffi::c_void,
1568 hash_len: *mut usize,
1569) -> TEE_Result {
1570 if operation.is_null() {
1572 return TEE_ERROR_BAD_PARAMETERS;
1573 }
1574
1575 if chunk.is_null() && chunk_len > 0 {
1577 return TEE_ERROR_BAD_PARAMETERS;
1578 }
1579
1580 let op_handle = unsafe { &mut *operation };
1582
1583 if op_handle.info.operationClass != TEE_OPERATION_DIGEST {
1585 return TEE_ERROR_BAD_PARAMETERS;
1586 }
1587
1588 if op_handle.operation_state == TEE_OPERATION_STATE_EXTRACTING && chunk_len > 0 {
1590 return TEE_ERROR_BAD_PARAMETERS;
1591 }
1592
1593 if hash_len.is_null() {
1595 TEE_Panic(0);
1596 return TEE_ERROR_BAD_PARAMETERS;
1597 }
1598
1599 if cfg!(feature = "strict_annotation_checks") {
1601 let res = TEE_CheckMemoryAccessRights(
1602 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
1603 hash_len as *mut core::ffi::c_void,
1604 std::mem::size_of::<usize>(),
1605 );
1606 if res != 0 {
1607 eprintln!("[inout] hash_len: error {:#010x}", res);
1608 TEE_Panic(0);
1609 return TEE_ERROR_BAD_PARAMETERS;
1610 }
1611 }
1612
1613 let res = if op_handle.operation_state == TEE_OPERATION_STATE_EXTRACTING
1614 && !op_handle.buffer.is_null()
1615 {
1616 let len = std::cmp::min(op_handle.block_size - op_handle.buffer_offs, unsafe {
1621 *hash_len
1622 });
1623 unsafe {
1624 std::ptr::copy_nonoverlapping(
1625 op_handle.buffer.add(op_handle.buffer_offs),
1626 hash as *mut u8,
1627 len,
1628 );
1629 *hash_len = len;
1630 }
1631 TEE_SUCCESS
1632 } else {
1633 let mut hl = unsafe { *hash_len as u64 };
1634 let res =
1635 unsafe { _utee_hash_final(op_handle.state as u64, chunk, chunk_len, hash, &mut hl) };
1636 unsafe {
1637 *hash_len = hl as usize;
1638 }
1639
1640 if res != TEE_SUCCESS as usize {
1641 return res as TEE_Result;
1642 }
1643
1644 TEE_SUCCESS
1645 };
1646
1647 init_hash_operation(operation, core::ptr::null(), 0);
1649
1650 unsafe {
1652 (*operation).operation_state = TEE_OPERATION_STATE_INITIAL;
1653 }
1654
1655 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
1657 TEE_Panic(res as u32);
1658 }
1659
1660 res
1661}
1662
1663#[unsafe(no_mangle)]
1673pub extern "C" fn TEE_DigestExtract(
1674 operation: *mut TEE_OperationHandle,
1675 hash: *mut core::ffi::c_void,
1676 hash_len: *mut usize,
1677) -> TEE_Result {
1678 if operation.is_null() {
1680 TEE_Panic(0);
1681 return TEE_ERROR_BAD_PARAMETERS;
1682 }
1683
1684 let op_handle = unsafe { &mut *operation };
1686 if (op_handle.info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0 {
1687 TEE_Panic(0);
1688 }
1689 unsafe {
1691 reset_operation_state(&mut *operation);
1692 }
1693
1694 if op_handle.info.operationClass != TEE_OPERATION_DIGEST {
1696 TEE_Panic(0);
1697 return TEE_ERROR_BAD_PARAMETERS;
1698 }
1699
1700 if hash_len.is_null() {
1702 TEE_Panic(0);
1703 return TEE_ERROR_BAD_PARAMETERS;
1704 }
1705
1706 if cfg!(feature = "strict_annotation_checks") {
1708 let res = TEE_CheckMemoryAccessRights(
1709 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
1710 hash_len as *mut core::ffi::c_void,
1711 std::mem::size_of::<usize>(),
1712 );
1713 if res != 0 {
1714 eprintln!("[inout] hash_len: error {:#010x}", res);
1715 TEE_Panic(0);
1716 return TEE_ERROR_BAD_PARAMETERS;
1717 }
1718 }
1719
1720 if op_handle.buffer.is_null() {
1722 unsafe {
1724 (*operation).info.handleState |= TEE_HANDLE_FLAG_EXTRACTING;
1725 (*operation).operation_state = TEE_OPERATION_STATE_EXTRACTING;
1726 }
1727
1728 let mut hl = unsafe { *hash_len as u64 };
1729 let res = unsafe {
1730 _utee_hash_final(op_handle.state as u64, core::ptr::null(), 0, hash, &mut hl)
1731 };
1732 if res != TEE_SUCCESS as usize {
1733 TEE_Panic(0);
1734 return TEE_ERROR_BAD_PARAMETERS;
1735 }
1736
1737 unsafe {
1738 *hash_len = hl as usize;
1739 }
1740
1741 return TEE_SUCCESS;
1742 }
1743
1744 if op_handle.operation_state != TEE_OPERATION_STATE_EXTRACTING {
1746 let mut hl = op_handle.block_size as u64;
1747 let res = unsafe {
1748 _utee_hash_final(
1749 op_handle.state as u64,
1750 core::ptr::null(),
1751 0,
1752 op_handle.buffer as *mut core::ffi::c_void,
1753 &mut hl,
1754 )
1755 };
1756 if res != TEE_SUCCESS as usize {
1757 TEE_Panic(0);
1758 return TEE_ERROR_BAD_PARAMETERS;
1759 }
1760
1761 if hl as usize != op_handle.block_size {
1762 TEE_Panic(0);
1763 return TEE_ERROR_BAD_PARAMETERS;
1764 }
1765
1766 debug_assert!(op_handle.buffer_offs == 0, "buffer_offs should be 0");
1767
1768 unsafe {
1769 (*operation).info.handleState |= TEE_HANDLE_FLAG_EXTRACTING;
1770 (*operation).operation_state = TEE_OPERATION_STATE_EXTRACTING;
1771 }
1772 }
1773
1774 let len = std::cmp::min(op_handle.block_size - op_handle.buffer_offs, unsafe {
1776 *hash_len
1777 });
1778
1779 unsafe {
1781 std::ptr::copy_nonoverlapping(
1782 op_handle.buffer.add(op_handle.buffer_offs),
1783 hash as *mut u8,
1784 len,
1785 );
1786 *hash_len = len;
1787 }
1788
1789 unsafe {
1791 (*operation).buffer_offs += len;
1792 }
1793
1794 TEE_SUCCESS
1795}
1796
1797#[unsafe(no_mangle)]
1808pub extern "C" fn TEE_CipherInit(
1809 operation: *mut TEE_OperationHandle,
1810 iv: *const core::ffi::c_void,
1811 iv_len: usize,
1812) {
1813 use crate::tee_api_defines::*;
1814
1815 if operation.is_null() {
1817 TEE_Panic(0);
1818 return;
1819 }
1820
1821 let op_handle = unsafe { &mut *operation };
1823
1824 if op_handle.info.operationClass != TEE_OPERATION_CIPHER {
1826 TEE_Panic(0);
1827 return;
1828 }
1829
1830 if (op_handle.info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0 || op_handle.key1.is_null() {
1832 TEE_Panic(0);
1833 return;
1834 }
1835
1836 if op_handle.operation_state != TEE_OPERATION_STATE_INITIAL {
1838 TEE_ResetOperation(operation);
1839 }
1840
1841 if !iv.is_null() && iv_len > 0 {
1843 match op_handle.info.algorithm {
1844 TEE_ALG_AES_ECB_NOPAD
1845 | TEE_ALG_DES_ECB_NOPAD
1846 | TEE_ALG_DES3_ECB_NOPAD
1847 | TEE_ALG_SM4_ECB_NOPAD => {
1848 TEE_Panic(0);
1849 return;
1850 }
1851 _ => {}
1852 }
1853 }
1854
1855 unsafe {
1857 (*operation).operation_state = TEE_OPERATION_STATE_ACTIVE;
1858 }
1859
1860 let res = unsafe { _utee_cipher_init(op_handle.state as u64, iv, iv_len) };
1862 if res != TEE_SUCCESS as usize {
1863 TEE_Panic(res as u32);
1864 return;
1865 }
1866
1867 unsafe {
1869 (*operation).buffer_offs = 0;
1870 (*operation).info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
1871 }
1872}
1873
1874const fn roundup2(v: usize, r: usize) -> usize {
1876 (v + r - 1) & !(r - 1)
1877}
1878
1879type UpdateFunc = unsafe extern "C" fn(
1881 state: u64,
1882 src: *const core::ffi::c_void,
1883 slen: usize,
1884 dst: *mut core::ffi::c_void,
1885 dlen: *mut u64,
1886) -> usize;
1887
1888fn tee_buffer_update(
1907 op: &mut TEE_OperationHandle,
1908 update_func: UpdateFunc,
1909 src_data: *const core::ffi::c_void,
1910 mut src_len: usize,
1911 dest_data: *mut core::ffi::c_void,
1912 dest_len: *mut u64,
1913) -> TEE_Result {
1914 use crate::tee_api_defines::*;
1915
1916 if src_data.is_null() {
1918 if src_len > 0 {
1919 TEE_Panic(0);
1920 }
1921 unsafe {
1922 *dest_len = 0;
1923 }
1924 return TEE_SUCCESS;
1925 }
1926
1927 let mut src = src_data as *const u8;
1928 let mut dst = dest_data as *mut u8;
1929 let mut dlen = unsafe { *dest_len } as usize;
1930 let mut acc_dlen = 0usize;
1931
1932 let (buffer_size, buffer_left) = if op.buffer_two_blocks {
1934 (op.block_size * 2, 1usize)
1935 } else {
1936 (op.block_size, 0usize)
1937 };
1938
1939 if op.buffer_offs > 0 {
1941 let l = if op.buffer_offs < op.block_size {
1942 std::cmp::min(src_len, op.block_size - op.buffer_offs)
1943 } else {
1944 std::cmp::min(src_len, buffer_size - op.buffer_offs)
1945 };
1946
1947 unsafe {
1948 std::ptr::copy_nonoverlapping(src, op.buffer.add(op.buffer_offs), l);
1949 }
1950 op.buffer_offs += l;
1951 src = unsafe { src.add(l) };
1952 src_len -= l;
1953
1954 if op.buffer_offs % op.block_size != 0 {
1956 unsafe {
1957 *dest_len = acc_dlen as u64;
1958 }
1959 return TEE_SUCCESS;
1960 }
1961 }
1962
1963 if op.buffer_offs > 0 && op.buffer_offs + src_len >= buffer_size + buffer_left {
1965 let mut l = roundup2(op.buffer_offs + src_len - buffer_size, op.block_size);
1966 l = std::cmp::min(op.buffer_offs, l);
1967
1968 if !op.buffer_two_blocks {
1970 l = op.block_size;
1971 }
1972
1973 let mut tmp_dlen = dlen as u64;
1974 let res = unsafe {
1975 update_func(
1976 op.state as u64,
1977 op.buffer as *const core::ffi::c_void,
1978 l,
1979 dst as *mut core::ffi::c_void,
1980 &mut tmp_dlen,
1981 )
1982 };
1983
1984 if res != TEE_SUCCESS as usize {
1985 TEE_Panic(res as u32);
1986 }
1987
1988 let tmp_dlen = tmp_dlen as usize;
1989 unsafe {
1990 dst = dst.add(tmp_dlen);
1991 }
1992 dlen -= tmp_dlen;
1993 acc_dlen += tmp_dlen;
1994 op.buffer_offs -= l;
1995
1996 if op.buffer_offs > 0 {
1998 unsafe {
2000 std::ptr::copy(op.buffer.add(l), op.buffer, buffer_size - l);
2001 std::ptr::copy_nonoverlapping(src, op.buffer.add(op.buffer_offs), src_len);
2002 }
2003 op.buffer_offs += src_len;
2004
2005 unsafe {
2006 *dest_len = acc_dlen as u64;
2007 }
2008 return TEE_SUCCESS;
2009 }
2010 }
2011
2012 if src_len >= buffer_size + buffer_left {
2014 let l = if op.buffer_two_blocks {
2016 roundup2(src_len - buffer_size, op.block_size)
2017 } else {
2018 roundup2(src_len - buffer_size + 1, op.block_size)
2019 };
2020
2021 let mut tmp_dlen = dlen as u64;
2022 let res = unsafe {
2023 update_func(
2024 op.state as u64,
2025 src as *const core::ffi::c_void,
2026 l,
2027 dst as *mut core::ffi::c_void,
2028 &mut tmp_dlen,
2029 )
2030 };
2031
2032 if res != TEE_SUCCESS as usize {
2033 TEE_Panic(res as u32);
2034 }
2035
2036 let tmp_dlen = tmp_dlen as usize;
2037 unsafe {
2038 src = src.add(l);
2039 }
2040 src_len -= l;
2041 acc_dlen += tmp_dlen;
2042 }
2043
2044 unsafe {
2046 std::ptr::copy_nonoverlapping(src, op.buffer.add(op.buffer_offs), src_len);
2047 }
2048 op.buffer_offs += src_len;
2049
2050 unsafe {
2051 *dest_len = acc_dlen as u64;
2052 }
2053 TEE_SUCCESS
2054}
2055
2056#[unsafe(no_mangle)]
2070pub extern "C" fn TEE_CipherUpdate(
2071 operation: *mut TEE_OperationHandle,
2072 src_data: *const core::ffi::c_void,
2073 src_len: usize,
2074 dest_data: *mut core::ffi::c_void,
2075 dest_len: *mut usize,
2076) -> TEE_Result {
2077 if operation.is_null() || (src_data.is_null() && src_len > 0) {
2079 return TEE_ERROR_BAD_PARAMETERS;
2080 }
2081
2082 if cfg!(feature = "strict_annotation_checks") {
2084 let res = TEE_CheckMemoryAccessRights(
2085 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
2086 dest_len as *mut core::ffi::c_void,
2087 std::mem::size_of::<usize>(),
2088 );
2089 if res != 0 {
2090 eprintln!("[inout] destLen: error {:#010x}", res);
2091 return TEE_ERROR_BAD_PARAMETERS;
2092 }
2093 }
2094
2095 let op_handle = unsafe { &mut *operation };
2097
2098 if op_handle.info.operationClass != TEE_OPERATION_CIPHER {
2100 return TEE_ERROR_BAD_PARAMETERS;
2101 }
2102
2103 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
2105 return TEE_ERROR_BAD_PARAMETERS;
2106 }
2107
2108 if op_handle.operation_state != TEE_OPERATION_STATE_ACTIVE {
2110 return TEE_ERROR_BAD_PARAMETERS;
2111 }
2112
2113 if src_data.is_null() && src_len == 0 {
2115 unsafe {
2116 *dest_len = 0;
2117 }
2118 return TEE_SUCCESS;
2119 }
2120
2121 let req_dlen = if op_handle.block_size > 1 {
2123 let base =
2124 ((op_handle.buffer_offs + src_len) / op_handle.block_size) * op_handle.block_size;
2125 if op_handle.buffer_two_blocks {
2126 if op_handle.buffer_offs + src_len > op_handle.block_size * 2 {
2127 let req = op_handle.buffer_offs + src_len - op_handle.block_size * 2;
2128 roundup2(req, op_handle.block_size)
2129 } else {
2130 0
2131 }
2132 } else {
2133 base
2134 }
2135 } else {
2136 src_len
2137 };
2138
2139 unsafe {
2144 if *dest_len < req_dlen {
2145 *dest_len = req_dlen;
2146 let res = TEE_ERROR_SHORT_BUFFER;
2147 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
2148 TEE_Panic(res as u32);
2149 }
2150 return res;
2151 }
2152 }
2153
2154 let mut dl = unsafe { *dest_len } as u64;
2155 let res = if op_handle.block_size > 1 {
2156 tee_buffer_update(
2157 op_handle,
2158 _utee_cipher_update,
2159 src_data,
2160 src_len,
2161 dest_data,
2162 &mut dl,
2163 )
2164 } else {
2165 if src_len > 0 {
2166 unsafe {
2167 _utee_cipher_update(
2168 op_handle.state as u64,
2169 src_data,
2170 src_len,
2171 dest_data,
2172 &mut dl,
2173 ) as TEE_Result
2174 }
2175 } else {
2176 dl = 0;
2177 TEE_SUCCESS
2178 }
2179 };
2180
2181 unsafe {
2182 *dest_len = dl as usize;
2183 }
2184
2185 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
2186 TEE_Panic(res as u32);
2187 }
2188
2189 res
2190}
2191
2192#[unsafe(no_mangle)]
2206pub extern "C" fn TEE_CipherDoFinal(
2207 operation: *mut TEE_OperationHandle,
2208 src_data: *const core::ffi::c_void,
2209 src_len: usize,
2210 dest_data: *mut core::ffi::c_void,
2211 dest_len: *mut usize,
2212) -> TEE_Result {
2213 if operation.is_null() || (src_data.is_null() && src_len > 0) {
2215 return TEE_ERROR_BAD_PARAMETERS;
2216 }
2217
2218 if !dest_len.is_null() {
2220 if cfg!(feature = "strict_annotation_checks") {
2221 let check_res = TEE_CheckMemoryAccessRights(
2222 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
2223 dest_len as *mut core::ffi::c_void,
2224 std::mem::size_of::<usize>(),
2225 );
2226 if check_res != 0 {
2227 eprintln!("[inout] destLen: error {:#010x}", check_res);
2228 return TEE_ERROR_BAD_PARAMETERS;
2229 }
2230 }
2231 }
2232
2233 let op_handle = unsafe { &mut *operation };
2235
2236 if op_handle.info.operationClass != TEE_OPERATION_CIPHER {
2238 return TEE_ERROR_BAD_PARAMETERS;
2239 }
2240
2241 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
2243 return TEE_ERROR_BAD_PARAMETERS;
2244 }
2245
2246 if op_handle.operation_state != TEE_OPERATION_STATE_ACTIVE {
2248 return TEE_ERROR_BAD_PARAMETERS;
2249 }
2250
2251 match op_handle.info.algorithm {
2255 TEE_ALG_AES_ECB_NOPAD
2256 | TEE_ALG_AES_CBC_NOPAD
2257 | TEE_ALG_DES_ECB_NOPAD
2258 | TEE_ALG_DES_CBC_NOPAD
2259 | TEE_ALG_DES3_ECB_NOPAD
2260 | TEE_ALG_DES3_CBC_NOPAD
2261 | TEE_ALG_SM4_ECB_NOPAD
2262 | TEE_ALG_SM4_CBC_NOPAD => {
2263 if (op_handle.buffer_offs + src_len) % op_handle.block_size != 0 {
2264 return TEE_ERROR_BAD_PARAMETERS;
2265 }
2266 }
2267 _ => {}
2268 }
2269
2270 let req_dlen = if op_handle.block_size > 1 {
2275 op_handle.buffer_offs + src_len
2276 } else {
2277 src_len
2278 };
2279
2280 let mut tmp_dlen = if !dest_len.is_null() {
2281 unsafe { *dest_len as u64 }
2282 } else {
2283 0u64
2284 };
2285
2286 if tmp_dlen < req_dlen as u64 {
2287 if !dest_len.is_null() {
2288 unsafe {
2289 *dest_len = req_dlen;
2290 }
2291 }
2292 let res = TEE_ERROR_SHORT_BUFFER;
2293 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
2294 TEE_Panic(res as u32);
2295 }
2296 return res;
2297 }
2298
2299 let mut res = TEE_SUCCESS;
2300 let mut dst = dest_data as *mut u8;
2301 let mut acc_dlen = 0usize;
2302
2303 if op_handle.block_size > 1 {
2304 if src_len > 0 {
2305 res = tee_buffer_update(
2306 unsafe { &mut *operation },
2307 _utee_cipher_update,
2308 src_data,
2309 src_len,
2310 dest_data,
2311 &mut tmp_dlen,
2312 );
2313
2314 if res != TEE_SUCCESS {
2315 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
2316 TEE_Panic(res as u32);
2317 }
2318 return res;
2319 }
2320
2321 let tmp_dlen_usize = tmp_dlen as usize;
2322 unsafe {
2323 dst = dst.add(tmp_dlen_usize);
2324 }
2325 acc_dlen += tmp_dlen_usize;
2326
2327 if !dest_len.is_null() {
2328 unsafe {
2329 tmp_dlen = *dest_len as u64 - acc_dlen as u64;
2330 }
2331 }
2332 }
2333
2334 res = unsafe {
2335 _utee_cipher_final(
2336 op_handle.state as u64,
2337 src_data,
2338 src_len,
2339 dest_data,
2340 dest_len as _,
2341 ) as TEE_Result
2342 };
2343 } else {
2344 res = unsafe {
2345 _utee_cipher_final(
2346 op_handle.state as u64,
2347 src_data,
2348 src_len,
2349 dest_data,
2350 dest_len as _,
2351 ) as TEE_Result
2352 };
2353 }
2354
2355 if res != TEE_SUCCESS {
2356 if res != TEE_ERROR_SHORT_BUFFER {
2357 TEE_Panic(res as u32);
2358 }
2359 return res;
2360 }
2361
2362 unsafe {
2364 (*operation).info.handleState &= !TEE_HANDLE_FLAG_INITIALIZED;
2365 (*operation).operation_state = TEE_OPERATION_STATE_INITIAL;
2366 }
2367
2368 res
2369}
2370
2371#[unsafe(no_mangle)]
2383pub extern "C" fn TEE_MACInit(
2384 operation: *mut TEE_OperationHandle,
2385 iv: *const core::ffi::c_void,
2386 iv_len: usize,
2387) {
2388 if operation.is_null() {
2390 TEE_Panic(TEE_PANIC_ID_TEE_MACINIT);
2391 return;
2392 }
2393
2394 let op_handle = unsafe { &*operation };
2396
2397 if op_handle.info.operationClass != TEE_OPERATION_MAC {
2399 TEE_Panic(TEE_PANIC_ID_TEE_MACINIT);
2400 return;
2401 }
2402
2403 if (op_handle.info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0 || op_handle.key1.is_null() {
2405 TEE_Panic(TEE_PANIC_ID_TEE_MACINIT);
2406 return;
2407 }
2408
2409 if op_handle.operation_state != TEE_OPERATION_STATE_INITIAL {
2411 TEE_ResetOperation(operation);
2412 }
2413
2414 unsafe {
2416 (*operation).operation_state = TEE_OPERATION_STATE_ACTIVE;
2417 }
2418
2419 init_hash_operation(operation, iv, iv_len as u32);
2421}
2422
2423#[unsafe(no_mangle)]
2438pub extern "C" fn TEE_MACUpdate(
2439 operation: *mut TEE_OperationHandle,
2440 chunk: *const core::ffi::c_void,
2441 chunk_size: usize,
2442) {
2443 if operation.is_null() {
2445 TEE_Panic(TEE_PANIC_ID_TEE_MACINIT);
2446 return;
2447 }
2448
2449 if chunk.is_null() && chunk_size > 0 {
2451 TEE_Panic(TEE_PANIC_ID_TEE_MACINIT);
2452 return;
2453 }
2454
2455 let op_handle = unsafe { &*operation };
2457
2458 if op_handle.info.operationClass != TEE_OPERATION_MAC {
2460 TEE_Panic(TEE_PANIC_ID_TEE_MACINIT);
2461 return;
2462 }
2463
2464 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
2466 TEE_Panic(TEE_PANIC_ID_TEE_MACINIT);
2467 return;
2468 }
2469
2470 if op_handle.operation_state != TEE_OPERATION_STATE_ACTIVE {
2472 TEE_Panic(TEE_PANIC_ID_TEE_MACINIT);
2473 return;
2474 }
2475
2476 let res = unsafe { _utee_hash_update(op_handle.state as u64, chunk, chunk_size) };
2478
2479 if res != TEE_SUCCESS as usize {
2480 TEE_Panic(res as u32);
2481 }
2482}
2483
2484#[unsafe(no_mangle)]
2498pub extern "C" fn TEE_MACComputeFinal(
2499 operation: *mut TEE_OperationHandle,
2500 message: *const core::ffi::c_void,
2501 message_len: usize,
2502 mac: *mut core::ffi::c_void,
2503 mac_len: *mut usize,
2504) -> TEE_Result {
2505 if operation.is_null()
2507 || (!message.is_null() && message_len == 0)
2508 || (message.is_null() && message_len > 0)
2509 {
2510 return TEE_ERROR_BAD_PARAMETERS;
2511 }
2512
2513 if cfg!(feature = "strict_annotation_checks") {
2515 let res = TEE_CheckMemoryAccessRights(
2516 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
2517 mac_len as *mut core::ffi::c_void,
2518 std::mem::size_of::<usize>(),
2519 );
2520 if res != 0 {
2521 eprintln!("[inout] mac_len: error {:#010x}", res);
2522 TEE_Panic(0);
2523 return TEE_ERROR_BAD_PARAMETERS;
2524 }
2525 }
2526
2527 let op_handle = unsafe { &mut *operation };
2529
2530 if op_handle.info.operationClass != TEE_OPERATION_MAC {
2532 return TEE_ERROR_BAD_PARAMETERS;
2533 }
2534
2535 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
2537 return TEE_ERROR_BAD_PARAMETERS;
2538 }
2539
2540 if op_handle.operation_state != TEE_OPERATION_STATE_ACTIVE {
2542 return TEE_ERROR_BAD_PARAMETERS;
2543 }
2544
2545 let mut ml = unsafe { *mac_len as u64 };
2547
2548 let res =
2550 unsafe { _utee_hash_final(op_handle.state as u64, message, message_len, mac, &mut ml) };
2551
2552 unsafe {
2554 *mac_len = ml as usize;
2555 }
2556
2557 if res != TEE_SUCCESS as usize {
2558 let result = res as TEE_Result;
2559 if result != TEE_ERROR_SHORT_BUFFER {
2560 TEE_Panic(result as u32);
2561 }
2562 return result;
2563 }
2564
2565 unsafe {
2567 (*operation).info.handleState &= !TEE_HANDLE_FLAG_INITIALIZED;
2568 (*operation).operation_state = TEE_OPERATION_STATE_INITIAL;
2569 }
2570
2571 TEE_SUCCESS as TEE_Result
2572}
2573
2574#[unsafe(no_mangle)]
2588pub extern "C" fn TEE_MACCompareFinal(
2589 operation: *mut TEE_OperationHandle,
2590 message: *const core::ffi::c_void,
2591 message_len: usize,
2592 mac: *const core::ffi::c_void,
2593 mac_len: usize,
2594) -> TEE_Result {
2595 if operation.is_null() {
2597 return TEE_ERROR_BAD_PARAMETERS;
2598 }
2599
2600 let op_handle = unsafe { &mut *operation };
2602
2603 if op_handle.info.operationClass != TEE_OPERATION_MAC {
2605 return TEE_ERROR_BAD_PARAMETERS;
2606 }
2607
2608 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
2610 return TEE_ERROR_BAD_PARAMETERS;
2611 }
2612
2613 if op_handle.operation_state != TEE_OPERATION_STATE_ACTIVE {
2615 return TEE_ERROR_BAD_PARAMETERS;
2616 }
2617
2618 let mut computed_mac = [0u8; TEE_MAX_HASH_SIZE as usize];
2620 let mut computed_mac_size = TEE_MAX_HASH_SIZE as usize;
2621
2622 let res = TEE_MACComputeFinal(
2624 operation,
2625 message,
2626 message_len,
2627 computed_mac.as_mut_ptr() as *mut core::ffi::c_void,
2628 &mut computed_mac_size,
2629 );
2630
2631 if res != TEE_SUCCESS {
2632 if res != TEE_ERROR_SHORT_BUFFER {
2633 TEE_Panic(res as u32);
2634 }
2635 return res;
2636 }
2637
2638 if computed_mac_size != mac_len {
2640 return TEE_ERROR_MAC_INVALID;
2641 }
2642
2643 let provided_mac = unsafe { std::slice::from_raw_parts(mac as *const u8, mac_len) };
2645
2646 if !consttime_memcmp(provided_mac, &computed_mac[..computed_mac_size]) {
2647 return TEE_ERROR_MAC_INVALID;
2648 }
2649
2650 unsafe {
2652 (*operation).operation_state = TEE_OPERATION_STATE_INITIAL;
2653 }
2654
2655 TEE_SUCCESS
2656}
2657
2658fn consttime_memcmp(a: &[u8], b: &[u8]) -> bool {
2669 if a.len() != b.len() {
2670 return false;
2671 }
2672
2673 let mut result = 0u8;
2674 for (x, y) in a.iter().zip(b.iter()) {
2675 result |= x ^ y;
2676 }
2677 result == 0
2678}
2679
2680#[unsafe(no_mangle)]
2695pub extern "C" fn TEE_AEInit(
2696 operation: *mut TEE_OperationHandle,
2697 nonce: *const core::ffi::c_void,
2698 nonce_len: usize,
2699 tag_len: u32,
2700 aad_len: usize,
2701 payload_len: usize,
2702) -> TEE_Result {
2703 if operation.is_null() || nonce.is_null() {
2705 return TEE_ERROR_BAD_PARAMETERS;
2706 }
2707
2708 let op_handle = unsafe { &mut *operation };
2710
2711 if op_handle.info.operationClass != TEE_OPERATION_AE {
2713 return TEE_ERROR_BAD_PARAMETERS;
2714 }
2715
2716 if op_handle.operation_state != TEE_OPERATION_STATE_INITIAL {
2718 return TEE_ERROR_BAD_PARAMETERS;
2719 }
2720
2721 if op_handle.info.algorithm == TEE_ALG_AES_GCM {
2723 if tag_len < 96 || tag_len > 128 || (tag_len % 8 != 0) {
2725 return TEE_ERROR_NOT_SUPPORTED;
2726 }
2727 }
2728
2729 let res = unsafe {
2731 _utee_authenc_init(
2732 op_handle.state as u64,
2733 nonce,
2734 nonce_len,
2735 tag_len as usize / 8, aad_len,
2737 payload_len,
2738 )
2739 };
2740
2741 if res != TEE_SUCCESS as usize {
2742 let result = res as TEE_Result;
2743 if result != TEE_ERROR_NOT_SUPPORTED {
2744 TEE_Panic(result as u32);
2745 }
2746 return result;
2747 }
2748
2749 unsafe {
2751 (*operation).info.digestLength = (tag_len / 8) as u32; (*operation).buffer_offs = 0;
2753 (*operation).info.handleState |= TEE_HANDLE_FLAG_INITIALIZED;
2754 }
2755
2756 TEE_SUCCESS
2757}
2758
2759#[unsafe(no_mangle)]
2766pub extern "C" fn TEE_AEUpdateAAD(
2767 operation: *mut TEE_OperationHandle,
2768 aad_data: *const core::ffi::c_void,
2769 aad_data_len: usize,
2770) {
2771 if operation.is_null() || (aad_data.is_null() && aad_data_len > 0) {
2773 TEE_Panic(0);
2774 return;
2775 }
2776
2777 let op_handle = unsafe { &*operation };
2779
2780 if op_handle.info.operationClass != TEE_OPERATION_AE {
2782 TEE_Panic(0);
2783 return;
2784 }
2785
2786 if op_handle.operation_state != TEE_OPERATION_STATE_INITIAL {
2788 TEE_Panic(0);
2789 return;
2790 }
2791
2792 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
2794 TEE_Panic(0);
2795 return;
2796 }
2797
2798 let res = unsafe { _utee_authenc_update_aad(op_handle.state as u64, aad_data, aad_data_len) };
2800
2801 if res != TEE_SUCCESS as usize {
2802 TEE_Panic(res as u32);
2803 }
2804}
2805
2806fn ae_update_helper(
2819 operation: *mut TEE_OperationHandle,
2820 src: *const core::ffi::c_void,
2821 slen: usize,
2822 dst: *mut core::ffi::c_void,
2823 dlen: *mut usize,
2824) -> TEE_Result {
2825 use crate::tee_api_defines::*;
2826
2827 if src.is_null() && slen == 0 {
2829 unsafe {
2830 *dlen = 0;
2831 }
2832 return TEE_SUCCESS;
2833 }
2834
2835 if operation.is_null() {
2837 return TEE_ERROR_BAD_PARAMETERS;
2838 }
2839
2840 let op_handle = unsafe { &*operation };
2842
2843 if dlen.is_null() {
2845 return TEE_ERROR_BAD_PARAMETERS;
2846 }
2847
2848 let req_dlen = if op_handle.block_size > 1 {
2850 let total_size = op_handle.buffer_offs + slen;
2852 roundup2(total_size, op_handle.block_size)
2854 } else {
2855 slen
2856 };
2857
2858 let provided_dlen = unsafe { *dlen };
2860 if provided_dlen < req_dlen {
2861 unsafe {
2862 *dlen = req_dlen;
2863 }
2864 return TEE_ERROR_SHORT_BUFFER;
2865 }
2866
2867 let mut dl = provided_dlen as u64;
2869 let res = if op_handle.block_size > 1 {
2870 tee_buffer_update(
2872 unsafe { &mut *operation },
2873 _utee_authenc_update_payload,
2874 src,
2875 slen,
2876 dst,
2877 &mut dl,
2878 )
2879 } else {
2880 if slen > 0 {
2882 unsafe {
2883 _utee_authenc_update_payload(op_handle.state as u64, src, slen, dst, &mut dl)
2884 as TEE_Result
2885 }
2886 } else {
2887 dl = 0;
2888 TEE_SUCCESS
2889 }
2890 };
2891
2892 if res == TEE_SUCCESS {
2894 unsafe {
2895 *dlen = dl as usize;
2896 }
2897 }
2898
2899 res
2900}
2901
2902#[unsafe(no_mangle)]
2916pub extern "C" fn TEE_AEUpdate(
2917 operation: *mut TEE_OperationHandle,
2918 src_data: *const core::ffi::c_void,
2919 src_len: usize,
2920 dest_data: *mut core::ffi::c_void,
2921 dest_len: *mut usize,
2922) -> TEE_Result {
2923 if operation.is_null() || (src_data.is_null() && src_len > 0) {
2925 let res = TEE_ERROR_BAD_PARAMETERS;
2926 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
2927 TEE_Panic(res as u32);
2928 }
2929 return res;
2930 }
2931
2932 if !dest_data.is_null() && !dest_len.is_null() {
2934 if cfg!(feature = "strict_annotation_checks") {
2935 let check_res = TEE_CheckMemoryAccessRights(
2936 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
2937 dest_len as *mut core::ffi::c_void,
2938 std::mem::size_of::<usize>(),
2939 );
2940 if check_res != 0 {
2941 eprintln!("[inout] destLen: error {:#010x}", check_res);
2942 TEE_Panic(0);
2943 return TEE_ERROR_BAD_PARAMETERS;
2944 }
2945 }
2946 }
2947
2948 let op_handle = unsafe { &*operation };
2950
2951 if op_handle.info.operationClass != TEE_OPERATION_AE {
2953 let res = TEE_ERROR_BAD_PARAMETERS;
2954 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
2955 TEE_Panic(res as u32);
2956 }
2957 return res;
2958 }
2959
2960 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
2962 let res = TEE_ERROR_BAD_PARAMETERS;
2963 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
2964 TEE_Panic(res as u32);
2965 }
2966 return res;
2967 }
2968
2969 let res = ae_update_helper(operation, src_data, src_len, dest_data, dest_len);
2971 if res != TEE_ERROR_SHORT_BUFFER && src_len > 0 {
2972 unsafe {
2973 (*operation).operation_state = TEE_OPERATION_STATE_ACTIVE;
2974 }
2975 }
2976
2977 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
2979 TEE_Panic(res as u32);
2980 }
2981
2982 res
2983}
2984
2985#[unsafe(no_mangle)]
3001pub extern "C" fn TEE_AEEncryptFinal(
3002 operation: *mut TEE_OperationHandle,
3003 src_data: *const core::ffi::c_void,
3004 src_len: usize,
3005 dest_data: *mut core::ffi::c_void,
3006 dest_len: *mut usize,
3007 tag: *mut core::ffi::c_void,
3008 tag_len: *mut usize,
3009) -> TEE_Result {
3010 let mut res = TEE_SUCCESS;
3011
3012 if operation.is_null() || (src_data.is_null() && src_len > 0) {
3014 res = TEE_ERROR_BAD_PARAMETERS;
3015 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3016 TEE_Panic(res as u32);
3017 }
3018 return res;
3019 }
3020
3021 if !dest_len.is_null() {
3023 if cfg!(feature = "strict_annotation_checks") {
3024 let check_res = TEE_CheckMemoryAccessRights(
3025 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
3026 dest_len as *mut core::ffi::c_void,
3027 std::mem::size_of::<usize>(),
3028 );
3029 if check_res != 0 {
3030 eprintln!("[inout] destLen: error {:#010x}", check_res);
3031 TEE_Panic(0);
3032 return TEE_ERROR_BAD_PARAMETERS;
3033 }
3034 }
3035 }
3036
3037 if !tag_len.is_null() {
3038 if cfg!(feature = "strict_annotation_checks") {
3039 let check_res = TEE_CheckMemoryAccessRights(
3040 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
3041 tag_len as *mut core::ffi::c_void,
3042 std::mem::size_of::<usize>(),
3043 );
3044 if check_res != 0 {
3045 eprintln!("[inout] tagLen: error {:#010x}", check_res);
3046 TEE_Panic(0);
3047 return TEE_ERROR_BAD_PARAMETERS;
3048 }
3049 }
3050 }
3051
3052 let op_handle = unsafe { &*operation };
3054
3055 if op_handle.info.operationClass != TEE_OPERATION_AE {
3057 let res = TEE_ERROR_BAD_PARAMETERS;
3058 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3059 TEE_Panic(res as u32);
3060 }
3061 return res;
3062 }
3063
3064 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
3066 let res = TEE_ERROR_BAD_PARAMETERS;
3067 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3068 TEE_Panic(res as u32);
3069 }
3070 return res;
3071 }
3072
3073 if dest_len.is_null() || tag_len.is_null() {
3075 res = TEE_ERROR_BAD_PARAMETERS;
3076 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3077 TEE_Panic(res as u32);
3078 }
3079 return res;
3080 }
3081
3082 let dest_len_val = unsafe { *dest_len };
3084 let tag_len_val = unsafe { *tag_len };
3085
3086 let req_dlen = op_handle.buffer_offs + src_len;
3088
3089 if dest_len_val < req_dlen {
3091 unsafe {
3092 *dest_len = req_dlen;
3093 }
3094 res = TEE_ERROR_SHORT_BUFFER;
3095 }
3096
3097 if tag_len_val < op_handle.info.digestLength as usize {
3099 unsafe {
3100 *tag_len = op_handle.info.digestLength as usize;
3101 }
3102 res = TEE_ERROR_SHORT_BUFFER;
3103 }
3104
3105 if res == TEE_ERROR_SHORT_BUFFER {
3106 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3107 TEE_Panic(res as u32);
3108 }
3109 return res;
3110 }
3111
3112 let mut acc_dlen = 0usize;
3117 let mut tl = tag_len_val as u64;
3118 let mut tmp_dlen = (dest_len_val - acc_dlen) as u64;
3119
3120 let dst = dest_data as *mut u8;
3121
3122 if op_handle.block_size > 1 {
3124 res = tee_buffer_update(
3126 unsafe { &mut *operation },
3127 _utee_authenc_update_payload,
3128 src_data,
3129 src_len,
3130 dest_data,
3131 &mut tmp_dlen,
3132 );
3133 if res != TEE_SUCCESS {
3134 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3135 TEE_Panic(res as u32);
3136 }
3137 return res;
3138 }
3139
3140 acc_dlen += tmp_dlen as usize;
3141 tmp_dlen = (dest_len_val - acc_dlen) as u64;
3142
3143 let buffer_ptr = op_handle.buffer as *const core::ffi::c_void;
3145 res = unsafe {
3146 _utee_authenc_enc_final(
3147 op_handle.state as u64,
3148 buffer_ptr,
3149 op_handle.buffer_offs,
3150 dst.add(acc_dlen) as *mut core::ffi::c_void,
3151 &mut tmp_dlen,
3152 tag,
3153 &mut tl,
3154 ) as TEE_Result
3155 };
3156 } else {
3157 res = unsafe {
3159 _utee_authenc_enc_final(
3160 op_handle.state as u64,
3161 src_data,
3162 src_len,
3163 dst as *mut core::ffi::c_void,
3164 &mut tmp_dlen,
3165 tag,
3166 &mut tl,
3167 ) as TEE_Result
3168 };
3169 }
3170
3171 unsafe {
3173 *tag_len = tl as usize;
3174 }
3175
3176 if res != TEE_SUCCESS {
3177 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3178 TEE_Panic(res as u32);
3179 }
3180 return res;
3181 }
3182
3183 acc_dlen += tmp_dlen as usize;
3184 unsafe {
3185 *dest_len = acc_dlen;
3186 }
3187
3188 unsafe {
3190 (*operation).info.handleState &= !TEE_HANDLE_FLAG_INITIALIZED;
3191 (*operation).operation_state = TEE_OPERATION_STATE_INITIAL;
3192 }
3193
3194 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3196 TEE_Panic(res as u32);
3197 }
3198
3199 res
3200}
3201
3202#[unsafe(no_mangle)]
3219pub extern "C" fn TEE_AEDecryptFinal(
3220 operation: *mut TEE_OperationHandle,
3221 src_data: *const core::ffi::c_void,
3222 src_len: usize,
3223 dest_data: *mut core::ffi::c_void,
3224 dest_len: *mut usize,
3225 tag: *const core::ffi::c_void,
3226 tag_len: usize,
3227) -> TEE_Result {
3228 let mut res = TEE_SUCCESS;
3229 if operation.is_null() || (src_data.is_null() && src_len > 0) {
3231 let res = TEE_ERROR_BAD_PARAMETERS;
3232 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER && res != TEE_ERROR_MAC_INVALID {
3233 TEE_Panic(res as u32);
3234 }
3235 return res;
3236 }
3237
3238 if !dest_len.is_null() {
3240 if cfg!(feature = "strict_annotation_checks") {
3241 let check_res = TEE_CheckMemoryAccessRights(
3242 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
3243 dest_len as *mut core::ffi::c_void,
3244 std::mem::size_of::<usize>(),
3245 );
3246 if check_res != 0 {
3247 eprintln!("[inout] destLen: error {:#010x}", check_res);
3248 TEE_Panic(0);
3249 return TEE_ERROR_BAD_PARAMETERS;
3250 }
3251 }
3252 } else {
3253 let res = TEE_ERROR_BAD_PARAMETERS;
3254 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER && res != TEE_ERROR_MAC_INVALID {
3255 TEE_Panic(res as u32);
3256 }
3257 return res;
3258 }
3259
3260 let op_handle = unsafe { &*operation };
3262
3263 if op_handle.info.operationClass != TEE_OPERATION_AE {
3265 let res = TEE_ERROR_BAD_PARAMETERS;
3266 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER && res != TEE_ERROR_MAC_INVALID {
3267 TEE_Panic(res as u32);
3268 }
3269 return res;
3270 }
3271
3272 if (op_handle.info.handleState & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
3274 let res = TEE_ERROR_BAD_PARAMETERS;
3275 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER && res != TEE_ERROR_MAC_INVALID {
3276 TEE_Panic(res as u32);
3277 }
3278 return res;
3279 }
3280
3281 let req_dlen = op_handle.buffer_offs + src_len;
3283
3284 let dest_len_val = unsafe { *dest_len };
3286 if dest_len_val < req_dlen {
3287 unsafe {
3288 *dest_len = req_dlen;
3289 }
3290 let res = TEE_ERROR_SHORT_BUFFER;
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 mut acc_dlen = 0usize;
3299 let mut tmp_dlen = (dest_len_val - acc_dlen) as u64;
3300
3301 let dst = dest_data as *mut u8;
3302
3303 if op_handle.block_size > 1 {
3305 res = tee_buffer_update(
3307 unsafe { &mut *operation },
3308 _utee_authenc_update_payload,
3309 src_data,
3310 src_len,
3311 dest_data,
3312 &mut tmp_dlen,
3313 );
3314 if res != TEE_SUCCESS {
3315 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER && res != TEE_ERROR_MAC_INVALID {
3316 TEE_Panic(res as u32);
3317 }
3318 return res;
3319 }
3320
3321 acc_dlen += tmp_dlen as usize;
3322 tmp_dlen = (dest_len_val - acc_dlen) as u64;
3323
3324 let buffer_ptr = op_handle.buffer as *const core::ffi::c_void;
3326 res = unsafe {
3327 _utee_authenc_dec_final(
3328 op_handle.state as u64,
3329 buffer_ptr,
3330 op_handle.buffer_offs,
3331 dst.add(acc_dlen) as *mut core::ffi::c_void,
3332 &mut tmp_dlen,
3333 tag,
3334 tag_len,
3335 ) as TEE_Result
3336 };
3337 } else {
3338 res = unsafe {
3340 _utee_authenc_dec_final(
3341 op_handle.state as u64,
3342 src_data,
3343 src_len,
3344 dst as *mut core::ffi::c_void,
3345 &mut tmp_dlen,
3346 tag,
3347 tag_len,
3348 ) as TEE_Result
3349 };
3350 }
3351
3352 if res != TEE_SUCCESS {
3353 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER && res != TEE_ERROR_MAC_INVALID {
3354 TEE_Panic(res as u32);
3355 }
3356 return res;
3357 }
3358
3359 acc_dlen += tmp_dlen as usize;
3360 unsafe {
3361 *dest_len = acc_dlen;
3362 }
3363
3364 unsafe {
3366 (*operation).info.handleState &= !TEE_HANDLE_FLAG_INITIALIZED;
3367 (*operation).operation_state = TEE_OPERATION_STATE_INITIAL;
3368 }
3369
3370 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER && res != TEE_ERROR_MAC_INVALID {
3372 TEE_Panic(res as u32);
3373 }
3374
3375 res
3376}
3377
3378#[unsafe(no_mangle)]
3396pub extern "C" fn TEE_AsymmetricEncrypt(
3397 operation: *mut TEE_OperationHandle,
3398 params: *const TEE_Attribute,
3399 param_count: u32,
3400 src_data: *const core::ffi::c_void,
3401 src_len: usize,
3402 dest_data: *mut core::ffi::c_void,
3403 dest_len: *mut usize,
3404) -> TEE_Result {
3405 let mut res = TEE_SUCCESS;
3406
3407 if operation.is_null() || (src_data.is_null() && src_len > 0) {
3409 TEE_Panic(0);
3410 return TEE_ERROR_BAD_PARAMETERS;
3411 }
3412
3413 if !params.is_null() && param_count > 0 {
3415 if cfg!(feature = "strict_annotation_checks") {
3416 let check_res = TEE_CheckMemoryAccessRights(
3417 TEE_MEMORY_ACCESS_READ,
3418 params as *mut core::ffi::c_void,
3419 (param_count as usize) * std::mem::size_of::<TEE_Attribute>(),
3420 );
3421 if check_res != 0 {
3422 eprintln!("[in] params: error {:#010x}", check_res);
3423 TEE_Panic(0);
3424 return TEE_ERROR_BAD_PARAMETERS;
3425 }
3426 }
3427 }
3428
3429 if !dest_len.is_null() {
3431 if cfg!(feature = "strict_annotation_checks") {
3432 let check_res = TEE_CheckMemoryAccessRights(
3433 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
3434 dest_len as *mut core::ffi::c_void,
3435 std::mem::size_of::<usize>(),
3436 );
3437 if check_res != 0 {
3438 eprintln!("[inout] destLen: error {:#010x}", check_res);
3439 TEE_Panic(0);
3440 return TEE_ERROR_BAD_PARAMETERS;
3441 }
3442 }
3443 } else {
3444 TEE_Panic(0);
3445 return TEE_ERROR_BAD_PARAMETERS;
3446 }
3447
3448 let op_handle = unsafe { &*operation };
3450
3451 if op_handle.key1.is_null() {
3453 TEE_Panic(0);
3454 return TEE_ERROR_BAD_PARAMETERS;
3455 }
3456
3457 if op_handle.info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER {
3459 TEE_Panic(0);
3460 return TEE_ERROR_BAD_PARAMETERS;
3461 }
3462
3463 if op_handle.info.mode != TEE_MODE_ENCRYPT {
3464 TEE_Panic(0);
3465 return TEE_ERROR_BAD_PARAMETERS;
3466 }
3467
3468 let mut dl = unsafe { *dest_len as u64 };
3470
3471 let mut ua = Vec::with_capacity(param_count as usize);
3473 ua.resize(
3474 param_count as usize,
3475 crate::utee_types::utee_attribute::default(),
3476 );
3477 unsafe {
3478 __utee_from_attr(ua.as_mut_ptr(), params, param_count);
3479 }
3480
3481 let syscall_res = unsafe {
3483 _utee_asymm_operate(
3484 op_handle.state as u64,
3485 ua.as_ptr(),
3486 param_count as u64,
3487 src_data,
3488 src_len,
3489 dest_data,
3490 &mut dl,
3491 )
3492 };
3493
3494 unsafe {
3496 *dest_len = dl as usize;
3497 }
3498
3499 res = syscall_res as TEE_Result;
3500
3501 if res != TEE_SUCCESS
3503 && res != TEE_ERROR_SHORT_BUFFER
3504 && res != TEE_ERROR_BAD_PARAMETERS
3505 && res != TEE_ERROR_CIPHERTEXT_INVALID
3506 && res != TEE_ERROR_NOT_SUPPORTED
3507 {
3508 TEE_Panic(res as u32);
3509 }
3510
3511 res
3512}
3513
3514#[unsafe(no_mangle)]
3532pub extern "C" fn TEE_AsymmetricDecrypt(
3533 operation: *mut TEE_OperationHandle,
3534 params: *const TEE_Attribute,
3535 param_count: u32,
3536 src_data: *const core::ffi::c_void,
3537 src_len: usize,
3538 dest_data: *mut core::ffi::c_void,
3539 dest_len: *mut usize,
3540) -> TEE_Result {
3541 let mut res = TEE_SUCCESS;
3542
3543 if operation.is_null() || (src_data.is_null() && src_len > 0) {
3545 TEE_Panic(0);
3546 return TEE_ERROR_BAD_PARAMETERS;
3547 }
3548
3549 if !params.is_null() && param_count > 0 {
3551 if cfg!(feature = "strict_annotation_checks") {
3552 let check_res = TEE_CheckMemoryAccessRights(
3553 TEE_MEMORY_ACCESS_READ,
3554 params as *mut core::ffi::c_void,
3555 (param_count as usize) * std::mem::size_of::<TEE_Attribute>(),
3556 );
3557 if check_res != 0 {
3558 eprintln!("[in] params: error {:#010x}", check_res);
3559 TEE_Panic(0);
3560 return TEE_ERROR_BAD_PARAMETERS;
3561 }
3562 }
3563 }
3564
3565 if !dest_len.is_null() {
3567 if cfg!(feature = "strict_annotation_checks") {
3568 let check_res = TEE_CheckMemoryAccessRights(
3569 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
3570 dest_len as *mut core::ffi::c_void,
3571 std::mem::size_of::<usize>(),
3572 );
3573 if check_res != 0 {
3574 eprintln!("[inout] destLen: error {:#010x}", check_res);
3575 TEE_Panic(0);
3576 return TEE_ERROR_BAD_PARAMETERS;
3577 }
3578 }
3579 } else {
3580 TEE_Panic(0);
3581 return TEE_ERROR_BAD_PARAMETERS;
3582 }
3583
3584 let op_handle = unsafe { &*operation };
3586
3587 if op_handle.key1.is_null() {
3589 TEE_Panic(0);
3590 return TEE_ERROR_BAD_PARAMETERS;
3591 }
3592
3593 if op_handle.info.operationClass != TEE_OPERATION_ASYMMETRIC_CIPHER {
3595 TEE_Panic(0);
3596 return TEE_ERROR_BAD_PARAMETERS;
3597 }
3598
3599 if op_handle.info.mode != TEE_MODE_DECRYPT {
3600 TEE_Panic(0);
3601 return TEE_ERROR_BAD_PARAMETERS;
3602 }
3603
3604 let mut dl = unsafe { *dest_len as u64 };
3606
3607 let mut ua = Vec::with_capacity(param_count as usize);
3609 ua.resize(
3610 param_count as usize,
3611 crate::utee_types::utee_attribute::default(),
3612 );
3613 unsafe {
3614 __utee_from_attr(ua.as_mut_ptr(), params, param_count);
3615 }
3616
3617 let syscall_res = unsafe {
3619 _utee_asymm_operate(
3620 op_handle.state as u64,
3621 ua.as_ptr(),
3622 param_count as u64,
3623 src_data,
3624 src_len,
3625 dest_data,
3626 &mut dl,
3627 )
3628 };
3629
3630 unsafe {
3632 *dest_len = dl as usize;
3633 }
3634
3635 res = syscall_res as TEE_Result;
3637 if res != TEE_SUCCESS
3638 && res != TEE_ERROR_SHORT_BUFFER
3639 && res != TEE_ERROR_BAD_PARAMETERS
3640 && res != TEE_ERROR_CIPHERTEXT_INVALID
3641 && res != TEE_ERROR_NOT_SUPPORTED
3642 {
3643 TEE_Panic(res as u32);
3644 }
3645
3646 res
3647}
3648
3649#[unsafe(no_mangle)]
3665pub extern "C" fn TEE_AsymmetricSignDigest(
3666 operation: *mut TEE_OperationHandle,
3667 params: *const TEE_Attribute,
3668 param_count: u32,
3669 digest: *const core::ffi::c_void,
3670 digest_len: usize,
3671 signature: *mut core::ffi::c_void,
3672 signature_len: *mut usize,
3673) -> TEE_Result {
3674 let mut res = TEE_SUCCESS;
3675
3676 if operation.is_null() || (digest.is_null() && digest_len > 0) {
3678 TEE_Panic(0);
3679 return TEE_ERROR_BAD_PARAMETERS;
3680 }
3681
3682 if !params.is_null() && param_count > 0 {
3684 if cfg!(feature = "strict_annotation_checks") {
3685 let check_res = TEE_CheckMemoryAccessRights(
3686 TEE_MEMORY_ACCESS_READ,
3687 params as *mut core::ffi::c_void,
3688 (param_count as usize) * std::mem::size_of::<TEE_Attribute>(),
3689 );
3690 if check_res != 0 {
3691 eprintln!("[in] params: error {:#010x}", check_res);
3692 TEE_Panic(0);
3693 return TEE_ERROR_BAD_PARAMETERS;
3694 }
3695 }
3696 }
3697
3698 if !signature_len.is_null() {
3700 if cfg!(feature = "strict_annotation_checks") {
3701 let check_res = TEE_CheckMemoryAccessRights(
3702 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
3703 signature_len as *mut core::ffi::c_void,
3704 std::mem::size_of::<usize>(),
3705 );
3706 if check_res != 0 {
3707 eprintln!("[inout] signatureLen: error {:#010x}", check_res);
3708 TEE_Panic(0);
3709 return TEE_ERROR_BAD_PARAMETERS;
3710 }
3711 }
3712 } else {
3713 TEE_Panic(0);
3714 return TEE_ERROR_BAD_PARAMETERS;
3715 }
3716
3717 let op_handle = unsafe { &*operation };
3719
3720 if op_handle.key1.is_null() {
3722 TEE_Panic(0);
3723 return TEE_ERROR_BAD_PARAMETERS;
3724 }
3725
3726 if op_handle.info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE {
3728 TEE_Panic(0);
3729 return TEE_ERROR_BAD_PARAMETERS;
3730 }
3731
3732 if op_handle.info.mode != TEE_MODE_SIGN {
3733 TEE_Panic(0);
3734 return TEE_ERROR_BAD_PARAMETERS;
3735 }
3736
3737 let mut sl = unsafe { *signature_len as u64 };
3739
3740 let mut ua = Vec::with_capacity(param_count as usize);
3742 ua.resize(
3743 param_count as usize,
3744 crate::utee_types::utee_attribute::default(),
3745 );
3746 unsafe {
3747 __utee_from_attr(ua.as_mut_ptr(), params, param_count);
3748 }
3749
3750 let syscall_res = unsafe {
3752 _utee_asymm_operate(
3753 op_handle.state as u64,
3754 ua.as_ptr(),
3755 param_count as u64,
3756 digest,
3757 digest_len,
3758 signature,
3759 &mut sl,
3760 )
3761 };
3762
3763 unsafe {
3765 *signature_len = sl as usize;
3766 }
3767
3768 res = syscall_res as TEE_Result;
3770 if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER {
3771 TEE_Panic(res as u32);
3772 }
3773
3774 res
3775}
3776
3777#[unsafe(no_mangle)]
3793pub extern "C" fn TEE_AsymmetricVerifyDigest(
3794 operation: *mut TEE_OperationHandle,
3795 params: *const TEE_Attribute,
3796 param_count: u32,
3797 digest: *const core::ffi::c_void,
3798 digest_len: usize,
3799 signature: *const core::ffi::c_void,
3800 signature_len: usize,
3801) -> TEE_Result {
3802 let mut res = TEE_SUCCESS;
3803
3804 if operation.is_null()
3806 || (digest.is_null() && digest_len != 0)
3807 || (signature.is_null() && signature_len != 0)
3808 {
3809 TEE_Panic(0);
3810 return TEE_ERROR_BAD_PARAMETERS;
3811 }
3812
3813 if !params.is_null() && param_count > 0 {
3815 if cfg!(feature = "strict_annotation_checks") {
3816 let check_res = TEE_CheckMemoryAccessRights(
3817 TEE_MEMORY_ACCESS_READ,
3818 params as *mut core::ffi::c_void,
3819 (param_count as usize) * std::mem::size_of::<TEE_Attribute>(),
3820 );
3821 if check_res != 0 {
3822 eprintln!("[in] params: error {:#010x}", check_res);
3823 TEE_Panic(0);
3824 return TEE_ERROR_BAD_PARAMETERS;
3825 }
3826 }
3827 }
3828
3829 if !digest.is_null() && digest_len > 0 {
3831 if cfg!(feature = "strict_annotation_checks") {
3832 let check_res = TEE_CheckMemoryAccessRights(
3833 TEE_MEMORY_ACCESS_READ,
3834 digest as *mut core::ffi::c_void,
3835 digest_len,
3836 );
3837 if check_res != 0 {
3838 eprintln!("[in] digest: error {:#010x}", check_res);
3839 TEE_Panic(0);
3840 return TEE_ERROR_BAD_PARAMETERS;
3841 }
3842 }
3843 }
3844
3845 if !signature.is_null() && signature_len > 0 {
3847 if cfg!(feature = "strict_annotation_checks") {
3848 let check_res = TEE_CheckMemoryAccessRights(
3849 TEE_MEMORY_ACCESS_READ,
3850 signature as *mut core::ffi::c_void,
3851 signature_len,
3852 );
3853 if check_res != 0 {
3854 eprintln!("[in] signature: error {:#010x}", check_res);
3855 TEE_Panic(0);
3856 return TEE_ERROR_BAD_PARAMETERS;
3857 }
3858 }
3859 }
3860
3861 let op_handle = unsafe { &*operation };
3863
3864 if op_handle.key1.is_null() {
3866 TEE_Panic(0);
3867 return TEE_ERROR_BAD_PARAMETERS;
3868 }
3869
3870 if op_handle.info.operationClass != TEE_OPERATION_ASYMMETRIC_SIGNATURE {
3872 TEE_Panic(0);
3873 return TEE_ERROR_BAD_PARAMETERS;
3874 }
3875
3876 if op_handle.info.mode != TEE_MODE_VERIFY {
3877 TEE_Panic(0);
3878 return TEE_ERROR_BAD_PARAMETERS;
3879 }
3880
3881 let mut ua = Vec::with_capacity(param_count as usize);
3883 ua.resize(
3884 param_count as usize,
3885 crate::utee_types::utee_attribute::default(),
3886 );
3887 unsafe {
3888 __utee_from_attr(ua.as_mut_ptr(), params, param_count);
3889 }
3890
3891 let syscall_res = unsafe {
3893 _utee_asymm_verify(
3894 op_handle.state as u64,
3895 ua.as_ptr(),
3896 param_count as u64,
3897 digest,
3898 digest_len,
3899 signature,
3900 signature_len,
3901 )
3902 };
3903
3904 res = syscall_res as TEE_Result;
3906 if res != TEE_SUCCESS && res != TEE_ERROR_SIGNATURE_INVALID {
3907 TEE_Panic(res as u32);
3908 }
3909
3910 res
3911}
3912
3913#[unsafe(no_mangle)]
3921pub extern "C" fn TEE_DeriveKey(
3922 operation: *mut TEE_OperationHandle,
3923 params: *const TEE_Attribute,
3924 param_count: u32,
3925 derived_key: TEE_ObjectHandle,
3926) {
3927 if operation.is_null() || derived_key.is_null() {
3929 TEE_Panic(0);
3930 return;
3931 }
3932
3933 if !params.is_null() && param_count > 0 {
3935 if cfg!(feature = "strict_annotation_checks") {
3936 let check_res = TEE_CheckMemoryAccessRights(
3937 TEE_MEMORY_ACCESS_READ,
3938 params as *mut core::ffi::c_void,
3939 (param_count as usize) * std::mem::size_of::<TEE_Attribute>(),
3940 );
3941 if check_res != 0 {
3942 eprintln!("[in] params: error {:#010x}", check_res);
3943 TEE_Panic(0);
3944 return;
3945 }
3946 }
3947 }
3948
3949 let op_handle = unsafe { &*operation };
3951
3952 if TEE_ALG_GET_CLASS(op_handle.info.algorithm) != TEE_OPERATION_KEY_DERIVATION {
3954 TEE_Panic(0);
3955 return;
3956 }
3957
3958 if op_handle.info.operationClass != TEE_OPERATION_KEY_DERIVATION {
3960 TEE_Panic(0);
3961 return;
3962 }
3963
3964 if op_handle.key1.is_null() {
3965 TEE_Panic(0);
3966 return;
3967 }
3968
3969 if op_handle.info.mode != TEE_MODE_DERIVE {
3970 TEE_Panic(0);
3971 return;
3972 }
3973
3974 if (op_handle.info.handleState & TEE_HANDLE_FLAG_KEY_SET) == 0 {
3975 TEE_Panic(0);
3976 return;
3977 }
3978
3979 let mut key_info = crate::utee_types::utee_object_info::default();
3981
3982 let res = unsafe { _utee_cryp_obj_get_info(derived_key as u64, &mut key_info) };
3983 if res != TEE_SUCCESS as usize {
3984 TEE_Panic(res as u32);
3985 return;
3986 }
3987
3988 if key_info.obj_type != TEE_TYPE_GENERIC_SECRET {
3990 TEE_Panic(0);
3991 return;
3992 }
3993
3994 if (key_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0 {
3995 TEE_Panic(0);
3996 return;
3997 }
3998
3999 let mut ua = Vec::with_capacity(param_count as usize);
4001 ua.resize(
4002 param_count as usize,
4003 crate::utee_types::utee_attribute::default(),
4004 );
4005 unsafe {
4006 __utee_from_attr(ua.as_mut_ptr(), params, param_count);
4007 }
4008
4009 let res = unsafe {
4011 _utee_cryp_derive_key(
4012 op_handle.state as u64,
4013 ua.as_ptr(),
4014 param_count as u64,
4015 derived_key as u64,
4016 )
4017 };
4018
4019 if res != TEE_SUCCESS as usize {
4020 TEE_Panic(res as u32);
4021 }
4022}
4023
4024#[unsafe(no_mangle)]
4030pub extern "C" fn TEE_GenerateRandom(
4031 random_buffer: *mut core::ffi::c_void,
4032 random_buffer_len: usize,
4033) {
4034 if random_buffer.is_null() && random_buffer_len > 0 {
4036 TEE_Panic(TEE_ERROR_BAD_PARAMETERS as u32);
4037 return;
4038 }
4039
4040 let res = unsafe { _utee_cryp_random_number_generate(random_buffer, random_buffer_len) };
4042
4043 if res != TEE_SUCCESS as usize {
4044 TEE_Panic(res as u32);
4045 }
4046}
4047
4048#[unsafe(no_mangle)]
4058pub extern "C" fn TEE_IsAlgorithmSupported(alg: u32, element: u32) -> TEE_Result {
4059 use crate::tee_api_defines::*;
4060
4061 if alg == TEE_ALG_AES_ECB_NOPAD {
4063 if element == TEE_CRYPTO_ELEMENT_NONE {
4064 return TEE_SUCCESS;
4065 }
4066 }
4067
4068 if alg == TEE_ALG_AES_CBC_NOPAD {
4069 if element == TEE_CRYPTO_ELEMENT_NONE {
4070 return TEE_SUCCESS;
4071 }
4072 }
4073
4074 if alg == TEE_ALG_AES_CTR {
4075 if element == TEE_CRYPTO_ELEMENT_NONE {
4076 return TEE_SUCCESS;
4077 }
4078 }
4079
4080 if alg == TEE_ALG_AES_CTS {
4081 if element == TEE_CRYPTO_ELEMENT_NONE {
4082 return TEE_SUCCESS;
4083 }
4084 }
4085
4086 if alg == TEE_ALG_AES_XTS {
4087 if element == TEE_CRYPTO_ELEMENT_NONE {
4088 return TEE_SUCCESS;
4089 }
4090 }
4091
4092 if alg == TEE_ALG_AES_CBC_MAC_NOPAD || alg == TEE_ALG_AES_CBC_MAC_PKCS5 {
4093 if element == TEE_CRYPTO_ELEMENT_NONE {
4094 return TEE_SUCCESS;
4095 }
4096 }
4097
4098 if alg == TEE_ALG_AES_CMAC {
4099 if element == TEE_CRYPTO_ELEMENT_NONE {
4100 return TEE_SUCCESS;
4101 }
4102 }
4103
4104 if alg == TEE_ALG_AES_CCM {
4105 if element == TEE_CRYPTO_ELEMENT_NONE {
4106 return TEE_SUCCESS;
4107 }
4108 }
4109
4110 if alg == TEE_ALG_AES_GCM {
4111 if element == TEE_CRYPTO_ELEMENT_NONE {
4112 return TEE_SUCCESS;
4113 }
4114 }
4115
4116 if alg == TEE_ALG_DES_ECB_NOPAD || alg == TEE_ALG_DES3_ECB_NOPAD {
4118 if element == TEE_CRYPTO_ELEMENT_NONE {
4119 return TEE_SUCCESS;
4120 }
4121 }
4122
4123 if alg == TEE_ALG_DES_CBC_NOPAD || alg == TEE_ALG_DES3_CBC_NOPAD {
4124 if element == TEE_CRYPTO_ELEMENT_NONE {
4125 return TEE_SUCCESS;
4126 }
4127 }
4128
4129 if alg == TEE_ALG_DES_CBC_MAC_NOPAD
4130 || alg == TEE_ALG_DES_CBC_MAC_PKCS5
4131 || alg == TEE_ALG_DES3_CBC_MAC_NOPAD
4132 || alg == TEE_ALG_DES3_CBC_MAC_PKCS5
4133 {
4134 if element == TEE_CRYPTO_ELEMENT_NONE {
4135 return TEE_SUCCESS;
4136 }
4137 }
4138
4139 if alg == TEE_ALG_MD5 {
4141 if element == TEE_CRYPTO_ELEMENT_NONE {
4142 return TEE_SUCCESS;
4143 }
4144 }
4145
4146 if alg == TEE_ALG_SHA1 {
4148 if element == TEE_CRYPTO_ELEMENT_NONE {
4149 return TEE_SUCCESS;
4150 }
4151 }
4152
4153 if alg == TEE_ALG_SHA224 {
4155 if element == TEE_CRYPTO_ELEMENT_NONE {
4156 return TEE_SUCCESS;
4157 }
4158 }
4159
4160 if alg == TEE_ALG_SHA256 {
4162 if element == TEE_CRYPTO_ELEMENT_NONE {
4163 return TEE_SUCCESS;
4164 }
4165 }
4166
4167 if alg == TEE_ALG_SHA384 {
4169 if element == TEE_CRYPTO_ELEMENT_NONE {
4170 return TEE_SUCCESS;
4171 }
4172 }
4173
4174 if alg == TEE_ALG_SHA512 {
4176 if element == TEE_CRYPTO_ELEMENT_NONE {
4177 return TEE_SUCCESS;
4178 }
4179 }
4180
4181 if alg == TEE_ALG_MD5SHA1 {
4183 if element == TEE_CRYPTO_ELEMENT_NONE {
4184 return TEE_SUCCESS;
4185 }
4186 }
4187
4188 if alg == TEE_ALG_HMAC_MD5 {
4190 if element == TEE_CRYPTO_ELEMENT_NONE {
4191 return TEE_SUCCESS;
4192 }
4193 }
4194
4195 if alg == TEE_ALG_HMAC_SHA1 {
4196 if element == TEE_CRYPTO_ELEMENT_NONE {
4197 return TEE_SUCCESS;
4198 }
4199 }
4200
4201 if alg == TEE_ALG_HMAC_SHA224 {
4202 if element == TEE_CRYPTO_ELEMENT_NONE {
4203 return TEE_SUCCESS;
4204 }
4205 }
4206
4207 if alg == TEE_ALG_HMAC_SHA256 {
4208 if element == TEE_CRYPTO_ELEMENT_NONE {
4209 return TEE_SUCCESS;
4210 }
4211 }
4212
4213 if alg == TEE_ALG_HMAC_SHA384 {
4214 if element == TEE_CRYPTO_ELEMENT_NONE {
4215 return TEE_SUCCESS;
4216 }
4217 }
4218
4219 if alg == TEE_ALG_HMAC_SHA512 {
4220 if element == TEE_CRYPTO_ELEMENT_NONE {
4221 return TEE_SUCCESS;
4222 }
4223 }
4224
4225 if alg == TEE_ALG_HMAC_SM3 {
4226 if element == TEE_CRYPTO_ELEMENT_NONE {
4227 return TEE_SUCCESS;
4228 }
4229 }
4230
4231 if alg == TEE_ALG_SM3 {
4233 if element == TEE_CRYPTO_ELEMENT_NONE {
4234 return TEE_SUCCESS;
4235 }
4236 }
4237
4238 if alg == TEE_ALG_SM4_ECB_NOPAD {
4240 if element == TEE_CRYPTO_ELEMENT_NONE {
4241 return TEE_SUCCESS;
4242 }
4243 }
4244
4245 if alg == TEE_ALG_SM4_CBC_NOPAD {
4246 if element == TEE_CRYPTO_ELEMENT_NONE {
4247 return TEE_SUCCESS;
4248 }
4249 }
4250
4251 if alg == TEE_ALG_SM4_CTR {
4252 if element == TEE_CRYPTO_ELEMENT_NONE {
4253 return TEE_SUCCESS;
4254 }
4255 }
4256
4257 if alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5 {
4259 if element == TEE_CRYPTO_ELEMENT_NONE {
4260 return TEE_SUCCESS;
4261 }
4262 }
4263
4264 if alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA1
4265 || alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA1
4266 || alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1
4267 {
4268 if element == TEE_CRYPTO_ELEMENT_NONE {
4269 return TEE_SUCCESS;
4270 }
4271 }
4272
4273 if alg == TEE_ALG_RSASSA_PKCS1_V1_5_MD5SHA1 {
4274 if element == TEE_CRYPTO_ELEMENT_NONE {
4275 return TEE_SUCCESS;
4276 }
4277 }
4278
4279 if alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA224
4280 || alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA224
4281 || alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224
4282 {
4283 if element == TEE_CRYPTO_ELEMENT_NONE {
4284 return TEE_SUCCESS;
4285 }
4286 }
4287
4288 if alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA256
4289 || alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA256
4290 || alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256
4291 {
4292 if element == TEE_CRYPTO_ELEMENT_NONE {
4293 return TEE_SUCCESS;
4294 }
4295 }
4296
4297 if alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA384
4298 || alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA384
4299 || alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384
4300 {
4301 if element == TEE_CRYPTO_ELEMENT_NONE {
4302 return TEE_SUCCESS;
4303 }
4304 }
4305
4306 if alg == TEE_ALG_RSASSA_PKCS1_V1_5_SHA512
4307 || alg == TEE_ALG_RSASSA_PKCS1_PSS_MGF1_SHA512
4308 || alg == TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512
4309 {
4310 if element == TEE_CRYPTO_ELEMENT_NONE {
4311 return TEE_SUCCESS;
4312 }
4313 }
4314
4315 if alg == TEE_ALG_RSA_NOPAD {
4316 if element == TEE_CRYPTO_ELEMENT_NONE {
4317 return TEE_SUCCESS;
4318 }
4319 }
4320
4321 if alg == TEE_ALG_DSA_SHA1 {
4323 if element == TEE_CRYPTO_ELEMENT_NONE {
4324 return TEE_SUCCESS;
4325 }
4326 }
4327
4328 if alg == TEE_ALG_DSA_SHA224 {
4329 if element == TEE_CRYPTO_ELEMENT_NONE {
4330 return TEE_SUCCESS;
4331 }
4332 }
4333
4334 if alg == TEE_ALG_DSA_SHA256 {
4335 if element == TEE_CRYPTO_ELEMENT_NONE {
4336 return TEE_SUCCESS;
4337 }
4338 }
4339
4340 if alg == TEE_ALG_DH_DERIVE_SHARED_SECRET {
4342 if element == TEE_CRYPTO_ELEMENT_NONE {
4343 return TEE_SUCCESS;
4344 }
4345 }
4346
4347 if (alg == TEE_ALG_ECDH_P192
4349 || alg == TEE_ALG_ECDSA_P192
4350 || alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
4351 && element == TEE_ECC_CURVE_NIST_P192
4352 {
4353 return TEE_SUCCESS;
4354 }
4355
4356 if (alg == TEE_ALG_ECDH_P224
4357 || alg == TEE_ALG_ECDSA_P224
4358 || alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
4359 && element == TEE_ECC_CURVE_NIST_P224
4360 {
4361 return TEE_SUCCESS;
4362 }
4363
4364 if (alg == TEE_ALG_ECDH_P256
4365 || alg == TEE_ALG_ECDSA_P256
4366 || alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
4367 && element == TEE_ECC_CURVE_NIST_P256
4368 {
4369 return TEE_SUCCESS;
4370 }
4371
4372 if (alg == TEE_ALG_ECDH_P384
4373 || alg == TEE_ALG_ECDSA_P384
4374 || alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
4375 && element == TEE_ECC_CURVE_NIST_P384
4376 {
4377 return TEE_SUCCESS;
4378 }
4379
4380 if (alg == TEE_ALG_ECDH_P521
4381 || alg == TEE_ALG_ECDSA_P521
4382 || alg == TEE_ALG_DH_DERIVE_SHARED_SECRET)
4383 && element == TEE_ECC_CURVE_NIST_P521
4384 {
4385 return TEE_SUCCESS;
4386 }
4387
4388 if alg == TEE_ALG_SM2_DSA_SM3 && element == TEE_ECC_CURVE_SM2 {
4390 return TEE_SUCCESS;
4391 }
4392
4393 if alg == TEE_ALG_SM2_KEP && element == TEE_ECC_CURVE_SM2 {
4395 return TEE_SUCCESS;
4396 }
4397
4398 if alg == TEE_ALG_SM2_PKE && element == TEE_ECC_CURVE_SM2 {
4400 return TEE_SUCCESS;
4401 }
4402
4403 TEE_ERROR_NOT_SUPPORTED
4404}