• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 #include <stdint.h>
17 #include <string.h>
18 #include "bsl_params.h"
19 #include "bsl_errno.h"
20 #include "crypt_errno.h"
21 #include "crypt_params_key.h"
22 #include "crypt_eal_provider.h"
23 #include "provider_test_utils.h"
24 
25 #define BSL_PARAM_MAX_NUMBER 1000
TestFindParam(BSL_Param * param,int32_t key)26 BSL_Param *TestFindParam(BSL_Param *param, int32_t key)
27 {
28     if (key == 0) {
29         return NULL;
30     }
31     if (param == NULL) {
32         return NULL;
33     }
34     int32_t index = 0;
35     while (param[index].key != 0 && index < BSL_PARAM_MAX_NUMBER) {
36         if (param[index].key == key) {
37             return &param[index];
38         }
39         index++;
40     }
41     return NULL;
42 }
43 
TestFindConstParam(const BSL_Param * param,int32_t key)44 const BSL_Param *TestFindConstParam(const BSL_Param *param, int32_t key)
45 {
46     if (key == 0) {
47         return NULL;
48     }
49     if (param == NULL) {
50         return NULL;
51     }
52     int32_t index = 0;
53     while (param[index].key != 0 && index < BSL_PARAM_MAX_NUMBER) {
54         if (param[index].key == key) {
55             return &param[index];
56         }
57         index++;
58     }
59     return NULL;
60 }
61 
TestParamInitValue(BSL_Param * param,int32_t key,uint32_t type,void * val,uint32_t valueLen)62 int32_t TestParamInitValue(BSL_Param *param, int32_t key, uint32_t type, void *val, uint32_t valueLen)
63 {
64     if (key == 0) {
65         return BSL_PARAMS_INVALID_KEY;
66     }
67     if (param == NULL) {
68         return BSL_INVALID_ARG;
69     }
70     if (type != BSL_PARAM_TYPE_FUNC_PTR && type != BSL_PARAM_TYPE_CTX_PTR) {
71         if (val == NULL && valueLen != 0) {
72             return BSL_INVALID_ARG;
73         }
74     }
75 
76     switch (type) {
77         case BSL_PARAM_TYPE_UINT8:
78         case BSL_PARAM_TYPE_UINT16:
79         case BSL_PARAM_TYPE_UINT32:
80         case BSL_PARAM_TYPE_OCTETS:
81         case BSL_PARAM_TYPE_BOOL:
82         case BSL_PARAM_TYPE_UINT32_PTR:
83         case BSL_PARAM_TYPE_FUNC_PTR:
84         case BSL_PARAM_TYPE_CTX_PTR:
85         case BSL_PARAM_TYPE_INT32:
86         case BSL_PARAM_TYPE_OCTETS_PTR:
87             param->value = val;
88             param->valueLen = valueLen;
89             param->valueType = type;
90             param->key = key;
91             param->useLen = 0;
92             return BSL_SUCCESS;
93         default:
94             return BSL_PARAMS_INVALID_TYPE;
95     }
96 }
97 
98 #define TLS_GROUP_PARAM_COUNT 11
BuildTlsGroupParam(const Provider_Group * groupInfo,BSL_Param * param)99 static int32_t BuildTlsGroupParam(const Provider_Group *groupInfo, BSL_Param *param)
100 {
101     int32_t ret = TestParamInitValue(&param[0], CRYPT_PARAM_CAP_TLS_GROUP_IANA_GROUP_NAME, BSL_PARAM_TYPE_OCTETS_PTR,
102         (void *)(uintptr_t)groupInfo->name, (uint32_t)strlen(groupInfo->name));
103     if (ret != BSL_SUCCESS) {
104         return ret;
105     }
106     ret = TestParamInitValue(&param[1], CRYPT_PARAM_CAP_TLS_GROUP_IANA_GROUP_ID, BSL_PARAM_TYPE_UINT16,
107         (void *)(uintptr_t)&(groupInfo->groupId), sizeof(groupInfo->groupId));
108     if (ret != BSL_SUCCESS) {
109         return ret;
110     }
111     ret = TestParamInitValue(&param[2], CRYPT_PARAM_CAP_TLS_GROUP_PARA_ID, BSL_PARAM_TYPE_INT32,
112         (void *)(uintptr_t)&(groupInfo->paraId), sizeof(groupInfo->paraId));
113     if (ret != BSL_SUCCESS) {
114         return ret;
115     }
116     ret = TestParamInitValue(&param[3], CRYPT_PARAM_CAP_TLS_GROUP_ALG_ID, BSL_PARAM_TYPE_INT32,
117         (void *)(uintptr_t)&(groupInfo->algId), sizeof(groupInfo->algId));
118     if (ret != BSL_SUCCESS) {
119         return ret;
120     }
121     ret = TestParamInitValue(&param[4], CRYPT_PARAM_CAP_TLS_GROUP_SEC_BITS, BSL_PARAM_TYPE_INT32,
122         (void *)(uintptr_t)&(groupInfo->secBits), sizeof(groupInfo->secBits));
123     if (ret != BSL_SUCCESS) {
124         return ret;
125     }
126     ret = TestParamInitValue(&param[5], CRYPT_PARAM_CAP_TLS_GROUP_VERSION_BITS, BSL_PARAM_TYPE_UINT32,
127         (void *)(uintptr_t)&(groupInfo->versionBits), sizeof(groupInfo->versionBits));
128     if (ret != BSL_SUCCESS) {
129         return ret;
130     }
131     ret = TestParamInitValue(&param[6], CRYPT_PARAM_CAP_TLS_GROUP_IS_KEM, BSL_PARAM_TYPE_BOOL,
132         (void *)(uintptr_t)&(groupInfo->isKem), sizeof(groupInfo->isKem));
133     if (ret != BSL_SUCCESS) {
134         return ret;
135     }
136     ret = TestParamInitValue(&param[7], CRYPT_PARAM_CAP_TLS_GROUP_PUBKEY_LEN, BSL_PARAM_TYPE_INT32,
137         (void *)(uintptr_t)&(groupInfo->pubkeyLen), sizeof(groupInfo->pubkeyLen));
138     if (ret != BSL_SUCCESS) {
139         return ret;
140     }
141     ret = TestParamInitValue(&param[8], CRYPT_PARAM_CAP_TLS_GROUP_SHAREDKEY_LEN, BSL_PARAM_TYPE_INT32,
142         (void *)(uintptr_t)&(groupInfo->sharedkeyLen), sizeof(groupInfo->sharedkeyLen));
143     if (ret != BSL_SUCCESS) {
144         return ret;
145     }
146     ret = TestParamInitValue(&param[9], CRYPT_PARAM_CAP_TLS_GROUP_CIPHERTEXT_LEN, BSL_PARAM_TYPE_INT32,
147         (void *)(uintptr_t)&(groupInfo->ciphertextLen), sizeof(groupInfo->ciphertextLen));
148     if (ret != BSL_SUCCESS) {
149         return ret;
150     }
151 
152     return BSL_SUCCESS;
153 }
154 
TestCryptGetGroupCaps(const Provider_Group * tlsGroup,uint32_t groupCount,CRYPT_EAL_ProcessFuncCb cb,void * args)155 int32_t TestCryptGetGroupCaps(const Provider_Group *tlsGroup, uint32_t groupCount,
156     CRYPT_EAL_ProcessFuncCb cb, void *args)
157 {
158     for (size_t i = 0; i < groupCount; i++) {
159         BSL_Param param[TLS_GROUP_PARAM_COUNT] = {0};
160         int32_t ret = BuildTlsGroupParam(&tlsGroup[i], param);
161         if (ret != BSL_SUCCESS) {
162             return ret;
163         }
164         ret = cb(param, args);
165         if (ret != CRYPT_SUCCESS) {
166             return ret;
167         }
168     }
169     return CRYPT_SUCCESS;
170 }