1 /*
2  * Copyright (C) 2022-2024 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 #ifndef HCF_SIGNATURE_H
17 #define HCF_SIGNATURE_H
18 
19 #include <stdint.h>
20 #include <stdbool.h>
21 #include "algorithm_parameter.h"
22 #include "result.h"
23 #include "key_pair.h"
24 
25 typedef enum {
26     PSS_MD_NAME_STR = 100,
27     PSS_MGF_NAME_STR = 101,
28     PSS_MGF1_MD_STR = 102,
29     PSS_SALT_LEN_INT = 103,  // warning: PSS_SALT_LEN_NUM in JS
30     PSS_TRAILER_FIELD_INT = 104,  // warning: PSS_TRAILER_FIELD_NUM in JS
31     SM2_USER_ID_UINT8ARR = 105
32 } SignSpecItem;
33 
34 typedef struct HcfSign HcfSign;
35 
36 struct HcfSign {
37     HcfObjectBase base;
38 
39     HcfResult (*init)(HcfSign *self, HcfParamsSpec *params, HcfPriKey *privateKey);
40 
41     HcfResult (*update)(HcfSign *self, HcfBlob *data);
42 
43     HcfResult (*sign)(HcfSign *self, HcfBlob *data, HcfBlob *returnSignatureData);
44 
45     const char *(*getAlgoName)(HcfSign *self);
46 
47     HcfResult (*setSignSpecInt)(HcfSign *self, SignSpecItem item, int32_t saltLen);
48 
49     HcfResult (*getSignSpecString)(HcfSign *self, SignSpecItem item, char **returnString);
50 
51     HcfResult (*getSignSpecInt)(HcfSign *self, SignSpecItem item, int32_t *returnInt);
52 
53     HcfResult (*setSignSpecUint8Array)(HcfSign *self, SignSpecItem item, HcfBlob blob);
54 };
55 
56 typedef struct HcfVerify HcfVerify;
57 
58 struct HcfVerify {
59     HcfObjectBase base;
60 
61     HcfResult (*init)(HcfVerify *self, HcfParamsSpec *params, HcfPubKey *publicKey);
62 
63     HcfResult (*update)(HcfVerify *self, HcfBlob *data);
64 
65     bool (*verify)(HcfVerify *self, HcfBlob *data, HcfBlob *signatureData);
66 
67     HcfResult (*recover)(HcfVerify *self, HcfBlob *signatureData, HcfBlob *rawSignatureData);
68 
69     const char *(*getAlgoName)(HcfVerify *self);
70 
71     HcfResult (*setVerifySpecInt)(HcfVerify *self, SignSpecItem item, int32_t saltLen);
72 
73     HcfResult (*getVerifySpecString)(HcfVerify *self, SignSpecItem item, char **returnString);
74 
75     HcfResult (*getVerifySpecInt)(HcfVerify *self, SignSpecItem item, int32_t *returnInt);
76 
77     HcfResult (*setVerifySpecUint8Array)(HcfVerify *self, SignSpecItem item, HcfBlob blob);
78 };
79 
80 #ifdef __cplusplus
81 extern "C" {
82 #endif
83 
84 HcfResult HcfSignCreate(const char *algoName, HcfSign **returnObj);
85 
86 HcfResult HcfVerifyCreate(const char *algoName, HcfVerify **returnObj);
87 
88 #ifdef __cplusplus
89 }
90 #endif
91 
92 #endif
93