• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define HUKS_DISABLE_LOG_AT_FILE_TO_REDUCE_ROM_SIZE
16 
17 #ifdef HKS_CONFIG_FILE
18 #include HKS_CONFIG_FILE
19 #else
20 #include "hks_config.h"
21 #endif
22 
23 #include "hks_client_service_adapter_common.h"
24 
25 #include <stddef.h>
26 
27 #include "hks_client_service_adapter.h"
28 #include "hks_log.h"
29 #include "hks_mem.h"
30 #include "hks_param.h"
31 #include "hks_template.h"
32 #include "securec.h"
33 
CopyToInnerKey(const struct HksBlob * key,struct HksBlob * outKey)34 int32_t CopyToInnerKey(const struct HksBlob *key, struct HksBlob *outKey)
35 {
36     if ((key->size == 0) || (key->size > MAX_KEY_SIZE)) {
37         HKS_LOG_E("invalid input key size: %" LOG_PUBLIC "u", key->size);
38         return HKS_ERROR_INVALID_ARGUMENT;
39     }
40 
41     uint8_t *outData = (uint8_t *)HksMalloc(key->size);
42     HKS_IF_NULL_LOGE_RETURN(outData, HKS_ERROR_MALLOC_FAIL, "malloc failed")
43 
44     (void)memcpy_s(outData, key->size, key->data, key->size);
45     outKey->data = outData;
46     outKey->size = key->size;
47 
48     return HKS_SUCCESS;
49 }
50 
51 #if defined(HKS_SUPPORT_ED25519_C) || defined(HKS_SUPPORT_X25519_C)
TranslateToInnerCurve25519Format(const uint32_t alg,const struct HksBlob * key,struct HksBlob * publicKey)52 static int32_t TranslateToInnerCurve25519Format(const uint32_t alg, const struct HksBlob *key,
53     struct HksBlob *publicKey)
54 {
55     if (key->size != HKS_KEY_BYTES(HKS_CURVE25519_KEY_SIZE_256)) {
56         HKS_LOG_E("Invalid curve25519 public key size! key size = 0x%" LOG_PUBLIC "X", key->size);
57         return HKS_ERROR_INVALID_KEY_INFO;
58     }
59 
60     uint32_t totalSize = sizeof(struct HksPubKeyInfo) + key->size;
61     uint8_t *buffer = (uint8_t *)HksMalloc(totalSize);
62     HKS_IF_NULL_LOGE_RETURN(buffer, HKS_ERROR_MALLOC_FAIL, "malloc failed! %" LOG_PUBLIC "u", totalSize)
63     (void)memset_s(buffer, totalSize, 0, totalSize);
64 
65     struct HksPubKeyInfo *curve25519Key = (struct HksPubKeyInfo *)buffer;
66     curve25519Key->keyAlg = (enum HksKeyAlg)alg;
67     curve25519Key->keySize = HKS_CURVE25519_KEY_SIZE_256;
68     curve25519Key->nOrXSize = key->size; /* curve25519 public key */
69 
70     uint32_t offset = sizeof(struct HksPubKeyInfo);
71     (void)memcpy_s(buffer + offset, totalSize - offset, key->data, key->size);
72     publicKey->data = buffer;
73     publicKey->size = totalSize;
74     return HKS_SUCCESS;
75 }
76 #endif
77 
HksSymmetricKeySizeCheck(struct HksParam * algParam,const struct HksBlob * key,struct HksBlob * outKey)78 static int32_t HksSymmetricKeySizeCheck(
79     struct HksParam *algParam, const struct HksBlob *key, struct HksBlob *outKey)
80 {
81     switch (algParam->uint32Param) {
82         case HKS_ALG_AES:
83             if ((key->size != HKS_KEY_BYTES(HKS_AES_KEY_SIZE_128)) &&
84                 (key->size != HKS_KEY_BYTES(HKS_AES_KEY_SIZE_192)) &&
85                 (key->size != HKS_KEY_BYTES(HKS_AES_KEY_SIZE_256))) {
86                 HKS_LOG_E("invalid input key size: %" LOG_PUBLIC "u", key->size);
87                 return HKS_ERROR_INVALID_KEY_INFO;
88             }
89             break;
90         case HKS_ALG_DES:
91             if (key->size != HKS_KEY_BYTES(HKS_DES_KEY_SIZE_64)) {
92                 HKS_LOG_E("invalid input key size: %" LOG_PUBLIC "u", key->size);
93                 return HKS_ERROR_INVALID_KEY_INFO;
94             }
95             break;
96         case HKS_ALG_3DES:
97             if ((key->size != HKS_KEY_BYTES(HKS_3DES_KEY_SIZE_128)) &&
98                 (key->size != HKS_KEY_BYTES(HKS_3DES_KEY_SIZE_192))) {
99                 HKS_LOG_E("invalid input key size: %" LOG_PUBLIC "u", key->size);
100                 return HKS_ERROR_INVALID_KEY_INFO;
101             }
102             break;
103         default:
104             HKS_LOG_E("invalid input key algParam: %" LOG_PUBLIC "u", algParam->uint32Param);
105             return HKS_ERROR_INVALID_ALGORITHM;
106     }
107 
108     return CopyToInnerKey(key, outKey);
109 }
110 
111 
GetHksPubKeyInnerFormat(const struct HksParamSet * paramSet,const struct HksBlob * key,struct HksBlob * outKey)112 int32_t GetHksPubKeyInnerFormat(const struct HksParamSet *paramSet,
113     const struct HksBlob *key, struct HksBlob *outKey)
114 {
115     if ((CheckBlob(key) != HKS_SUCCESS) || (outKey == NULL)) {
116         HKS_LOG_E("invalid key or outKey");
117         return HKS_ERROR_INVALID_ARGUMENT;
118     }
119 
120     struct HksParam *algParam = NULL;
121     int32_t ret = HksGetParam(paramSet, HKS_TAG_ALGORITHM, &algParam);
122     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_ALG_FAIL, "get alg param failed")
123 
124     switch (algParam->uint32Param) {
125 #if defined(HKS_SUPPORT_HMAC_C) || defined(HKS_SUPPORT_SM3_C) || defined(HKS_SUPPORT_SM4_C) || \
126     defined(HKS_SUPPORT_AES_C) || defined(HKS_SUPPORT_DES_C) || defined(HKS_SUPPORT_3DES_C)
127         case HKS_ALG_AES:
128         case HKS_ALG_DES:
129         case HKS_ALG_3DES:
130             return HksSymmetricKeySizeCheck(algParam, key, outKey);
131         case HKS_ALG_HMAC:
132         case HKS_ALG_SM3:
133         case HKS_ALG_SM4:
134             return CopyToInnerKey(key, outKey);
135 #endif
136 #if defined(HKS_SUPPORT_X25519_C) || defined(HKS_SUPPORT_ED25519_C)
137         case HKS_ALG_ED25519:
138         case HKS_ALG_X25519:
139             return TranslateToInnerCurve25519Format(algParam->uint32Param, key, outKey);
140 #endif
141 #if defined(HKS_SUPPORT_RSA_C) || defined(HKS_SUPPORT_ECC_C) || defined(HKS_SUPPORT_DSA_C) || \
142     defined(HKS_SUPPORT_DH_C) || defined(HKS_SUPPORT_SM2_C)
143         case HKS_ALG_RSA:
144         case HKS_ALG_ECC:
145         case HKS_ALG_ECDH:
146         case HKS_ALG_DSA:
147         case HKS_ALG_DH:
148         case HKS_ALG_SM2:
149             return TranslateFromX509PublicKey(algParam->uint32Param, key, outKey);
150 #endif
151         default:
152             return HKS_ERROR_INVALID_ALGORITHM;
153     }
154 }
155 
156