1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifdef HKS_CONFIG_FILE
17 #include HKS_CONFIG_FILE
18 #else
19 #include "hks_config.h"
20 #endif
21
22 #ifdef HKS_SUPPORT_SM3_C
23
24 #include "hks_log.h"
25 #include "hks_openssl_common.h"
26 #include "hks_openssl_engine.h"
27 #include "hks_openssl_sm3.h"
28 #include "hks_template.h"
29
30 #ifdef HKS_SUPPORT_SM3_GENERATE_KEY
Sm3GenKeyCheckParam(const struct HksKeySpec * spec)31 static int32_t Sm3GenKeyCheckParam(const struct HksKeySpec *spec)
32 {
33 if ((spec->keyLen == 0) || (spec->keyLen % BIT_NUM_OF_UINT8 != 0)) {
34 HKS_LOG_E("keyLen is wrong, len = %" LOG_PUBLIC "u", spec->keyLen);
35 return HKS_ERROR_INVALID_ARGUMENT;
36 }
37 return HKS_SUCCESS;
38 }
39
HksOpensslSm3GenerateKey(const struct HksKeySpec * spec,struct HksBlob * key)40 int32_t HksOpensslSm3GenerateKey(const struct HksKeySpec *spec, struct HksBlob *key)
41 {
42 HKS_IF_NOT_SUCC_LOGE_RETURN(Sm3GenKeyCheckParam(spec),
43 HKS_ERROR_INVALID_ARGUMENT, "sm3 generate key invalid params!")
44 return HksOpensslGenerateRandomKey(spec->keyLen, key);
45 }
46 #endif /* HKS_SUPPORT_SM3_GENERATE_KEY */
47
48 #endif /* HKS_SUPPORT_SM3_C */
49