1 /*
2  * Copyright (C) 2021 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 ALG_DEFS_H
17 #define ALG_DEFS_H
18 
19 #include "hc_types.h"
20 #include "string_util.h"
21 
22 #define SHA256_LEN 32
23 #define HMAC_LEN 32
24 #define SIGNATURE_LEN 64
25 #define AE_TAG_LEN 16
26 #define BIG_PRIME_LEN_384 384
27 #define BIG_PRIME_LEN_256 256
28 
29 typedef enum {
30     PAIR_TYPE_BIND = 0,
31     PAIR_TYPE_CLONE = 1,
32     PAIR_TYPE_END
33 } PairType; // range: 0 ~ 2^8-1
34 
35 typedef struct {
36     Uint8Buff authId;
37     int32_t userType;
38     int32_t pairType;
39 } ExtraInfo;
40 
41 typedef enum {
42     ED25519 = 0,
43     X25519 = 1,
44     P256 = 2,
45     AES = 3,
46 } Algorithm;
47 
48 typedef enum {
49     KEY_PURPOSE_MAC = 0,
50     KEY_PURPOSE_DERIVE = 1,
51     KEY_PURPOSE_SIGN_VERIFY = 2,
52     KEY_PURPOSE_KEY_AGREE = 3
53 } KeyPurpose;
54 
55 typedef enum {
56     CURVE_NONE,
57     CURVE_256,
58     CURVE_25519,
59 } CurveType;
60 
61 typedef struct {
62     uint8_t *nonce;
63     uint32_t nonceLen;
64     uint8_t *aad;
65     uint32_t aadLen;
66 } GcmParam;
67 
68 typedef struct {
69     uint8_t *key;
70     uint32_t keyLen;
71     bool isAlias;
72 } KeyBuff;
73 
74 typedef struct {
75     KeyBuff keyBuff;
76     bool isDeStorage;
77     int32_t osAccountId;
78 } KeyParams;
79 
80 typedef int32_t (*InitAlgFunc)(void);
81 
82 typedef int32_t (*Sha256Func)(const Uint8Buff *message, Uint8Buff *hash);
83 
84 typedef int32_t (*GenerateRandomFunc)(Uint8Buff *rand);
85 
86 typedef int32_t (*ComputeHmacFunc)(const KeyParams *keyParams, const Uint8Buff *message, Uint8Buff *outHmac);
87 
88 typedef int32_t (*ComputeHmacWithThreeStageFunc)(const KeyParams *keyParams, const Uint8Buff *message,
89     Uint8Buff *outHmac);
90 
91 typedef int32_t (*ComputeHkdfFunc)(const KeyParams *keyParams, const Uint8Buff *salt, const Uint8Buff *keyInfo,
92     Uint8Buff *outHkdf);
93 
94 typedef int32_t (*ComputePseudonymPskFunc)(const KeyParams *keyParams, const Uint8Buff *pskKeyAlias,
95     const Uint8Buff *extInfo, Uint8Buff *outPsk);
96 
97 typedef int32_t (*GetKeyExtInfoFunc)(const KeyParams *keyParams, Uint8Buff *outExtInfo);
98 
99 typedef int32_t (*ImportSymmetricKeyFunc)(const KeyParams *keyParams, const Uint8Buff *authToken, KeyPurpose purpose,
100     const ExtraInfo *exInfo);
101 
102 typedef int32_t (*CheckKeyExistFunc)(const Uint8Buff *keyAlias, bool isDeStorage, int32_t osAccountId);
103 typedef int32_t (*DeleteKeyFunc)(const Uint8Buff *keyAlias, bool isDeStorage, int32_t osAccountId);
104 
105 typedef int32_t (*AesGcmEncryptFunc)(const KeyParams *keyParams, const Uint8Buff *plain, const GcmParam *encryptInfo,
106     Uint8Buff *outCipher);
107 typedef int32_t (*AesGcmDecryptFunc)(const KeyParams *keyParams, const Uint8Buff *cipher, const GcmParam *decryptInfo,
108     Uint8Buff *outPlain);
109 
110 typedef int32_t (*GetTrustAuthIdListFunc)(const Uint8Buff *ownerAuthId, int32_t trustUserType,
111     Uint8Buff *outAuthIdList, uint32_t *outCount);
112 
113 typedef int32_t (*HashToPointFunc)(const Uint8Buff *hash, Algorithm algo, Uint8Buff *outEcPoint);
114 
115 typedef int32_t (*AgreeSharedSecretWithStorageFunc)(const KeyParams *priKeyParams, const KeyBuff *pubKeyBuff,
116     Algorithm algo, uint32_t sharedKeyLen, const Uint8Buff *sharedKeyAlias);
117 
118 typedef int32_t (*AgreeSharedSecretFunc)(const KeyParams *priKeyParams, const KeyBuff *pubKey, Algorithm algo,
119     Uint8Buff *sharedKey);
120 
121 typedef int32_t (*BigNumExpModFunc)(const Uint8Buff *base, const Uint8Buff *exp, const char *bigNumHex,
122     Uint8Buff *outNum);
123 
124 typedef int32_t (*GenerateKeyPairWithStorageFunc)(const KeyParams *keyParams, uint32_t keyLen, Algorithm algo,
125     KeyPurpose purpose, const ExtraInfo *exInfo);
126 
127 typedef int32_t (*GenerateKeyPairFunc)(Algorithm algo, Uint8Buff *outPriKey, Uint8Buff *outPubKey);
128 
129 typedef int32_t (*ExportPublicKeyFunc)(const KeyParams *keyParams, Uint8Buff *outPubKey);
130 
131 typedef int32_t (*SignFunc)(const KeyParams *keyParams, const Uint8Buff *message, Algorithm algo,
132     Uint8Buff *outSignature);
133 
134 typedef int32_t (*VerifyFunc)(const KeyParams *keyParams, const Uint8Buff *message, Algorithm algo,
135     const Uint8Buff *signature);
136 
137 typedef int32_t (*ImportPublicKeyFunc)(const KeyParams *keyParams, const Uint8Buff *pubKey, Algorithm algo,
138     const ExtraInfo *exInfo);
139 
140 typedef bool (*CheckEcPublicKeyFunc)(const Uint8Buff *pubKey, Algorithm algo);
141 
142 typedef bool (*CheckDlPublicKeyFunc)(const Uint8Buff *key, const char *primeHex);
143 
144 typedef int32_t (*BigNumCompareFunc)(const Uint8Buff *x, const Uint8Buff *y);
145 
146 typedef int32_t (*Base64EncodeFunc)(const uint8_t *byte, uint32_t byteLen,
147     char *base64Str, uint32_t strLen, uint32_t *outLen);
148 
149 typedef int32_t (*Base64DecodeFunc)(const char *base64Str, uint32_t strLen,
150     uint8_t *byte, uint32_t byteLen, uint32_t *outLen);
151 
152 typedef struct {
153     InitAlgFunc initAlg;
154     Sha256Func sha256;
155     GenerateRandomFunc generateRandom;
156     ComputeHmacFunc computeHmac;
157     ComputeHmacWithThreeStageFunc computeHmacWithThreeStage;
158     ComputeHkdfFunc computeHkdf;
159     ComputePseudonymPskFunc computePseudonymPsk;
160     GetKeyExtInfoFunc getKeyExtInfo;
161     ImportSymmetricKeyFunc importSymmetricKey;
162     CheckKeyExistFunc checkKeyExist;
163     DeleteKeyFunc deleteKey;
164     AesGcmEncryptFunc aesGcmEncrypt;
165     AesGcmDecryptFunc aesGcmDecrypt;
166     HashToPointFunc hashToPoint;
167     AgreeSharedSecretWithStorageFunc agreeSharedSecretWithStorage;
168     AgreeSharedSecretFunc agreeSharedSecret;
169     BigNumExpModFunc bigNumExpMod;
170     GenerateKeyPairWithStorageFunc generateKeyPairWithStorage;
171     GenerateKeyPairFunc generateKeyPair;
172     ExportPublicKeyFunc exportPublicKey;
173     SignFunc sign;
174     VerifyFunc verify;
175     ImportPublicKeyFunc importPublicKey;
176     CheckDlPublicKeyFunc checkDlPublicKey;
177     CheckEcPublicKeyFunc checkEcPublicKey;
178     BigNumCompareFunc bigNumCompare;
179     Base64EncodeFunc base64Encode;
180     Base64DecodeFunc base64Decode;
181 } AlgLoader;
182 
183 #endif