1 /*
2 * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3 * Licensed under the Mulan PSL v2.
4 * You can use this software according to the terms and conditions of the Mulan PSL v2.
5 * You may obtain a copy of Mulan PSL v2 at:
6 * http://license.coscl.org.cn/MulanPSL2
7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9 * PURPOSE.
10 * See the Mulan PSL v2 for more details.
11 */
12 #include "tee_crypto_api.h"
13 #include <string.h>
14 #include <tee_log.h>
15 #include <crypto_driver_adaptor.h>
16 #include <crypto_inner_defines.h>
17 #include <crypto_hal_hmac.h>
18 #include <tee_property_inner.h>
19 #include <tee_object_api.h>
20 #include "tee_operation.h"
21
TEE_MACCompareFinal(TEE_OperationHandle operation,const void * message,size_t messageLen,const void * mac,const size_t macLen)22 TEE_Result TEE_MACCompareFinal(TEE_OperationHandle operation, const void *message, size_t messageLen,
23 const void *mac, const size_t macLen)
24 {
25 uint8_t hmac_result_buff_temp[MAX_HMAC_LEN] = { 0 };
26 size_t size = macLen;
27
28 TEE_Result ret = TEE_MACComputeFinal(operation, message, messageLen, hmac_result_buff_temp, &size);
29 if (crypto_lock_operation(operation) != TEE_SUCCESS)
30 return TEE_ERROR_GENERIC;
31
32 if (ret != TEE_SUCCESS) {
33 tloge("MAC compute final failed\n");
34 goto error;
35 }
36
37 bool check = (size != macLen || TEE_MemCompare((void *)hmac_result_buff_temp, mac, (uint32_t)size) != 0);
38 if (check) {
39 tloge("size 0x%x != macLen 0x%x or compare failed!\n", size, macLen);
40 ret = TEE_ERROR_MAC_INVALID;
41 goto error;
42 }
43
44 free_operation_ctx(operation);
45 crypto_unlock_operation(operation);
46 return ret;
47 error:
48 free_operation_ctx(operation);
49 crypto_unlock_operation(operation);
50 if (ret != TEE_ERROR_MAC_INVALID)
51 TEE_Panic(ret);
52 return ret;
53 }
54