• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-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 _CUT_AUTHENTICATE_
23 #undef HKS_SUPPORT_ED25519_C
24 #endif
25 
26 #ifdef HKS_SUPPORT_ED25519_C
27 
28 #include "hks_crypto_ed25519.h"
29 
30 #include <ec_local.h>
31 
32 #include "hks_crypto_hal.h"
33 #include "hks_log.h"
34 #include "hks_mem.h"
35 #include "hks_template.h"
36 
37 #define CRYPTO_SUCCESS 1
38 #define ED25519_PRIVATE_KEY_LEN 32
39 #define ED25519_PUBLIC_KEY_LEN 32
40 
41 #ifdef HKS_SUPPORT_ED25519_GENERATE_KEY
SaveEd25519KeyMaterial(const struct HksBlob * pubKey,const struct HksBlob * priKey,struct HksBlob * keyOut)42 static int32_t SaveEd25519KeyMaterial(const struct HksBlob *pubKey, const struct HksBlob *priKey,
43     struct HksBlob *keyOut)
44 {
45     uint32_t totalSize = sizeof(struct KeyMaterial25519) + pubKey->size + priKey->size;
46     uint8_t *buffer = (uint8_t *)HksMalloc(totalSize);
47     HKS_IF_NULL_RETURN(buffer, HKS_ERROR_MALLOC_FAIL)
48 
49     struct KeyMaterial25519 *keyMaterial = (struct KeyMaterial25519 *)buffer;
50     keyMaterial->keyAlg = HKS_ALG_ED25519;
51     keyMaterial->keySize = HKS_CURVE25519_KEY_SIZE_256;
52     keyMaterial->pubKeySize = pubKey->size;
53     keyMaterial->priKeySize = priKey->size;
54     keyMaterial->reserved = 0;
55 
56     uint32_t offset = sizeof(struct KeyMaterial25519);
57     if (memcpy_s(buffer + offset, totalSize - offset, pubKey->data, pubKey->size) != EOK) {
58         HKS_LOG_E("copy ed25519 public key failed");
59         (void)memset_s(buffer, totalSize, 0, totalSize);
60         HKS_FREE_PTR(buffer);
61         return HKS_ERROR_INSUFFICIENT_MEMORY;
62     }
63 
64     offset += pubKey->size;
65     if (memcpy_s(buffer + offset, totalSize - offset, priKey->data, priKey->size) != EOK) {
66         HKS_LOG_E("copy ed25519 private key failed");
67         (void)memset_s(buffer, totalSize, 0, totalSize);
68         HKS_FREE_PTR(buffer);
69         return HKS_ERROR_INSUFFICIENT_MEMORY;
70     }
71 
72     keyOut->data = buffer;
73     keyOut->size = totalSize;
74     return HKS_SUCCESS;
75 }
76 
IsBlobZero(const struct HksBlob * key)77 static bool IsBlobZero(const struct HksBlob *key)
78 {
79     for (uint32_t i = 0; i < key->size; ++i) {
80         if (key->data[i] != 0) {
81             return false;
82         }
83     }
84     return true;
85 }
86 
HksEd25519GenerateKey(const struct HksKeySpec * spec,struct HksBlob * keyOut)87 int32_t HksEd25519GenerateKey(const struct HksKeySpec *spec, struct HksBlob *keyOut)
88 {
89     (void)spec;
90     uint8_t pubKey[ED25519_PUBLIC_KEY_LEN] = {0};
91     uint8_t priKey[ED25519_PRIVATE_KEY_LEN] = {0};
92     struct HksBlob pubKeyBlob = { ED25519_PUBLIC_KEY_LEN, pubKey };
93     struct HksBlob priKeyBlob = { ED25519_PRIVATE_KEY_LEN, priKey };
94 
95     struct HksBlob tmp = { ED25519_PUBLIC_KEY_LEN, priKeyBlob.data };
96     int32_t ret = HksCryptoHalFillPrivRandom(&tmp);
97     HKS_IF_NOT_SUCC_RETURN(ret, ret)
98 
99     ED25519_public_from_private(pubKeyBlob.data, priKeyBlob.data);
100     if (IsBlobZero(&pubKeyBlob) || IsBlobZero(&priKeyBlob)) {
101         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
102     }
103 
104     ret = SaveEd25519KeyMaterial(&pubKeyBlob, &priKeyBlob, keyOut);
105     (void)memset_s(priKeyBlob.data, ED25519_PRIVATE_KEY_LEN, 0, ED25519_PRIVATE_KEY_LEN);
106     return ret;
107 }
108 #endif /* HKS_SUPPORT_ED25519_GENERATE_KEY */
109 
CheckEd25519Material(const struct HksBlob * key)110 static int32_t CheckEd25519Material(const struct HksBlob *key)
111 {
112     uint32_t totalSize = sizeof(struct KeyMaterial25519);
113     if (key->size < totalSize) {
114         HKS_LOG_E("Ed25519 key material too small");
115         return HKS_ERROR_INVALID_KEY_INFO;
116     }
117 
118     struct KeyMaterial25519 *km = (struct KeyMaterial25519 *)key->data;
119     if (((key->size - totalSize) < km->pubKeySize) ||
120         ((key->size - totalSize) < km->priKeySize) ||
121         (km->pubKeySize > (UINT32_MAX - km->priKeySize)) ||
122         ((key->size - totalSize) < (km->pubKeySize + km->priKeySize))) {
123         HKS_LOG_E("Ed25519 key material wrong pub and pri key size %" LOG_PUBLIC "u, %" LOG_PUBLIC "u, "
124             "%" LOG_PUBLIC "u", key->size, km->pubKeySize, km->priKeySize);
125         return HKS_ERROR_INVALID_KEY_INFO;
126     }
127 
128     return HKS_SUCCESS;
129 }
130 
131 #ifdef HKS_SUPPORT_ED2519_GET_PUBLIC_KEY
GetEd25519PubKeyCheck(const struct HksBlob * key,const struct HksBlob * keyOut)132 static int32_t GetEd25519PubKeyCheck(const struct HksBlob *key, const struct HksBlob *keyOut)
133 {
134     int32_t ret = CheckEd25519Material(key);
135     HKS_IF_NOT_SUCC_RETURN(ret, ret)
136 
137     /* check keyOut */
138     struct KeyMaterial25519 *km = (struct KeyMaterial25519 *)key->data;
139     if ((km->pubKeySize > (UINT32_MAX - sizeof(struct KeyMaterial25519))) ||
140         (keyOut->size < (sizeof(struct KeyMaterial25519) + km->pubKeySize))) {
141         HKS_LOG_E("Ecc public keyOut size too small! keyOut size = 0x%" LOG_PUBLIC "X", keyOut->size);
142         return HKS_ERROR_BUFFER_TOO_SMALL;
143     }
144 
145     return HKS_SUCCESS;
146 }
147 
HksGetEd25519PubKey(const struct HksBlob * input,struct HksBlob * output)148 int32_t HksGetEd25519PubKey(const struct HksBlob *input, struct HksBlob *output)
149 {
150     int32_t ret = GetEd25519PubKeyCheck(input, output);
151     HKS_IF_NOT_SUCC_RETURN(ret, ret)
152 
153     struct KeyMaterial25519 *key = (struct KeyMaterial25519 *)input->data;
154     uint32_t outLen = sizeof(struct KeyMaterial25519) + key->pubKeySize;
155     if (memcpy_s(output->data, output->size, key, outLen) != EOK) {
156         HKS_LOG_E("memcpy_s ed25519 pub key failed");
157         return HKS_ERROR_INSUFFICIENT_MEMORY;
158     }
159 
160     ((struct KeyMaterial25519 *)output->data)->priKeySize = 0;
161     output->size = outLen;
162     return HKS_SUCCESS;
163 }
164 #endif
165 
166 #ifdef HKS_SUPPORT_ED25519_SIGN_VERIFY
HksEd25519Sign(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,struct HksBlob * signature)167 int32_t HksEd25519Sign(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
168     const struct HksBlob *message, struct HksBlob *signature)
169 {
170     (void)usageSpec;
171     int32_t ret = CheckEd25519Material(key);
172     HKS_IF_NOT_SUCC_RETURN(ret, ret)
173     if (signature->size < HKS_SIGNATURE_MIN_SIZE ||
174         key->size <= (sizeof(struct KeyMaterial25519) + ED25519_PUBLIC_KEY_LEN)) {
175         HKS_LOG_E("invalid param : signature size = %" LOG_PUBLIC "u, key size = %" LOG_PUBLIC "u",
176             signature->size, key->size);
177         return HKS_ERROR_INVALID_ARGUMENT;
178     }
179 
180     ret = ED25519_sign(signature->data, message->data, message->size,
181         key->data + sizeof(struct KeyMaterial25519),
182         key->data + sizeof(struct KeyMaterial25519) + ED25519_PUBLIC_KEY_LEN);
183     if (ret != CRYPTO_SUCCESS) {
184         HKS_LOG_E("ED25519_sign failed");
185         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
186     }
187     signature->size = HKS_SIGNATURE_MIN_SIZE;
188 
189     return HKS_SUCCESS;
190 }
191 
HksEd25519Verify(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,const struct HksBlob * signature)192 int32_t HksEd25519Verify(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
193     const struct HksBlob *message, const struct HksBlob *signature)
194 {
195     (void)usageSpec;
196     int32_t ret = CheckEd25519Material(key);
197     HKS_IF_NOT_SUCC_RETURN(ret, ret)
198     if (signature->size < HKS_SIGNATURE_MIN_SIZE) {
199         return HKS_ERROR_INVALID_ARGUMENT;
200     }
201 
202     ret = ED25519_verify(message->data, message->size, signature->data,
203         key->data + sizeof(struct KeyMaterial25519));
204     if (ret != CRYPTO_SUCCESS) {
205         HKS_LOG_E("ED25519_verify failed");
206         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
207     }
208 
209     return HKS_SUCCESS;
210 }
211 #endif /* HKS_SUPPORT_ED25519_SIGN_VERIFY */
212 #endif /* HKS_SUPPORT_ED25519_C */
213