• 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 
16 #include "cert_manager_crypto_operation.h"
17 
18 #include "securec.h"
19 
20 #include "cm_log.h"
21 #include "cm_type.h"
22 
23 #include "blob.h"
24 #include "md.h"
25 #include "rand.h"
26 
CmGetRandom(struct CmBlob * random)27 int32_t CmGetRandom(struct CmBlob *random)
28 {
29     HcfRand *randObj = NULL;
30     struct HcfBlob randomBlob = { NULL, 0 };
31 
32     int32_t ret = CMR_ERROR_KEY_OPERATION_FAILED;
33     do {
34         int32_t retHcf = (int32_t)HcfRandCreate(&randObj);
35         if (retHcf != HCF_SUCCESS) {
36             CM_LOG_E("creat random obj failed, ret = %d", retHcf);
37             break;
38         }
39 
40         retHcf = (int32_t)randObj->generateRandom(randObj, random->size, &randomBlob);
41         if (retHcf != HCF_SUCCESS) {
42             CM_LOG_E("generate random value failed, ret = %d", retHcf);
43             break;
44         }
45 
46         if (memcpy_s(random->data, random->size, randomBlob.data, randomBlob.len) != EOK) {
47             CM_LOG_E("copy random value failed");
48             break;
49         }
50         random->size = randomBlob.len;
51         ret = CM_SUCCESS;
52     } while (0);
53 
54     HcfBlobDataClearAndFree(&randomBlob);
55     HcfObjDestroy(randObj);
56 
57     return ret;
58 }
59 
CmGetHash(const struct CmBlob * inData,struct CmBlob * hash)60 int32_t CmGetHash(const struct CmBlob *inData, struct CmBlob *hash)
61 {
62     HcfMd *mdObj = NULL;
63     struct HcfBlob outBlob = { NULL, 0 };
64 
65     int32_t ret = CMR_ERROR_KEY_OPERATION_FAILED;
66     do {
67         int32_t retHcf = (int32_t)HcfMdCreate("SHA256", &mdObj);
68         if (retHcf != HCF_SUCCESS) {
69             CM_LOG_E("creat hash obj failed, ret = %d", retHcf);
70             break;
71         }
72 
73         HcfBlob inBlob = { .data = inData->data, .len = inData->size };
74         retHcf = mdObj->update(mdObj, &inBlob);
75         if (retHcf != HCF_SUCCESS) {
76             CM_LOG_E("hash update failed, ret = %d", retHcf);
77             break;
78         }
79         retHcf = mdObj->doFinal(mdObj, &outBlob);
80         if (retHcf != HCF_SUCCESS) {
81             CM_LOG_E("hash final failed, ret = %d", retHcf);
82             break;
83         }
84 
85         if (hash->size < outBlob.len) {
86             CM_LOG_E("hash input size[%u] too small", hash->size);
87             ret = CMR_ERROR_BUFFER_TOO_SMALL;
88             break;
89         }
90         if (memcpy_s(hash->data, hash->size, outBlob.data, outBlob.len) != EOK) {
91             CM_LOG_E("copy hash value failed");
92             break;
93         }
94         hash->size = outBlob.len;
95         ret = CM_SUCCESS;
96     } while (0);
97 
98     HcfBlobDataClearAndFree(&outBlob);
99     HcfObjDestroy(mdObj);
100     return ret;
101 }
102 
103