• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     KEY_PURPOSE_KEY_ENCRYPT = 4,
54 } KeyPurpose;
55 
56 typedef enum {
57     CURVE_NONE,
58     CURVE_256,
59     CURVE_25519,
60 } CurveType;
61 
62 typedef struct {
63     uint8_t *nonce;
64     uint32_t nonceLen;
65     uint8_t *aad;
66     uint32_t aadLen;
67 } GcmParam;
68 
69 typedef struct {
70     uint8_t *key;
71     uint32_t keyLen;
72     bool isAlias;
73 } KeyBuff;
74 
75 typedef struct {
76     KeyBuff keyBuff;
77     bool isDeStorage;
78     int32_t osAccountId;
79 } KeyParams;
80 
81 typedef int32_t (*InitAlgFunc)(void);
82 
83 typedef int32_t (*Sha256Func)(const Uint8Buff *message, Uint8Buff *hash);
84 
85 typedef int32_t (*GenerateRandomFunc)(Uint8Buff *rand);
86 
87 typedef int32_t (*ComputeHmacFunc)(const KeyParams *keyParams, const Uint8Buff *message, Uint8Buff *outHmac);
88 
89 typedef int32_t (*ComputeHmacWithThreeStageFunc)(const KeyParams *keyParams, const Uint8Buff *message,
90     Uint8Buff *outHmac);
91 
92 typedef int32_t (*ComputeHkdfFunc)(const KeyParams *keyParams, const Uint8Buff *salt, const Uint8Buff *keyInfo,
93     Uint8Buff *outHkdf);
94 
95 typedef int32_t (*ComputePseudonymPskFunc)(const KeyParams *keyParams, const Uint8Buff *pskKeyAlias,
96     const Uint8Buff *extInfo, Uint8Buff *outPsk);
97 
98 typedef int32_t (*GetKeyExtInfoFunc)(const KeyParams *keyParams, Uint8Buff *outExtInfo);
99 
100 typedef int32_t (*ImportSymmetricKeyFunc)(const KeyParams *keyParams, const Uint8Buff *authToken, KeyPurpose purpose,
101     const ExtraInfo *exInfo);
102 
103 typedef int32_t (*CheckKeyExistFunc)(const Uint8Buff *keyAlias, bool isDeStorage, int32_t osAccountId);
104 typedef int32_t (*DeleteKeyFunc)(const Uint8Buff *keyAlias, bool isDeStorage, int32_t osAccountId);
105 
106 typedef int32_t (*AesGcmEncryptFunc)(const KeyParams *keyParams, const Uint8Buff *plain, const GcmParam *encryptInfo,
107     Uint8Buff *outCipher);
108 typedef int32_t (*AesGcmDecryptFunc)(const KeyParams *keyParams, const Uint8Buff *cipher, const GcmParam *decryptInfo,
109     Uint8Buff *outPlain);
110 
111 typedef int32_t (*GetTrustAuthIdListFunc)(const Uint8Buff *ownerAuthId, int32_t trustUserType,
112     Uint8Buff *outAuthIdList, uint32_t *outCount);
113 
114 typedef int32_t (*HashToPointFunc)(const Uint8Buff *hash, Algorithm algo, Uint8Buff *outEcPoint);
115 
116 typedef int32_t (*AgreeSharedSecretWithStorageFunc)(const KeyParams *priKeyParams, const KeyBuff *pubKeyBuff,
117     Algorithm algo, uint32_t sharedKeyLen, const Uint8Buff *sharedKeyAlias);
118 
119 typedef int32_t (*AgreeSharedSecretFunc)(const KeyParams *priKeyParams, const KeyBuff *pubKey, Algorithm algo,
120     Uint8Buff *sharedKey);
121 
122 typedef int32_t (*BigNumExpModFunc)(const Uint8Buff *base, const Uint8Buff *exp, const char *bigNumHex,
123     Uint8Buff *outNum);
124 
125 typedef int32_t (*GenerateKeyPairWithStorageFunc)(const KeyParams *keyParams, uint32_t keyLen, Algorithm algo,
126     KeyPurpose purpose, const ExtraInfo *exInfo);
127 
128 typedef int32_t (*GenerateKeyPairFunc)(Algorithm algo, Uint8Buff *outPriKey, Uint8Buff *outPubKey);
129 
130 typedef int32_t (*ExportPublicKeyFunc)(const KeyParams *keyParams, Uint8Buff *outPubKey);
131 
132 typedef int32_t (*SignFunc)(const KeyParams *keyParams, const Uint8Buff *message, Algorithm algo,
133     Uint8Buff *outSignature);
134 
135 typedef int32_t (*VerifyFunc)(const KeyParams *keyParams, const Uint8Buff *message, Algorithm algo,
136     const Uint8Buff *signature);
137 
138 typedef int32_t (*ImportPublicKeyFunc)(const KeyParams *keyParams, const Uint8Buff *pubKey, Algorithm algo,
139     const ExtraInfo *exInfo);
140 
141 typedef bool (*CheckEcPublicKeyFunc)(const Uint8Buff *pubKey, Algorithm algo);
142 
143 typedef bool (*CheckDlPublicKeyFunc)(const Uint8Buff *key, const char *primeHex);
144 
145 typedef int32_t (*BigNumCompareFunc)(const Uint8Buff *x, const Uint8Buff *y);
146 
147 typedef int32_t (*Base64EncodeFunc)(const uint8_t *byte, uint32_t byteLen,
148     char *base64Str, uint32_t strLen, uint32_t *outLen);
149 
150 typedef int32_t (*Base64DecodeFunc)(const char *base64Str, uint32_t strLen,
151     uint8_t *byte, uint32_t byteLen, uint32_t *outLen);
152 
153 typedef struct {
154     InitAlgFunc initAlg;
155     Sha256Func sha256;
156     GenerateRandomFunc generateRandom;
157     ComputeHmacFunc computeHmac;
158     ComputeHmacWithThreeStageFunc computeHmacWithThreeStage;
159     ComputeHkdfFunc computeHkdf;
160     ComputePseudonymPskFunc computePseudonymPsk;
161     GetKeyExtInfoFunc getKeyExtInfo;
162     ImportSymmetricKeyFunc importSymmetricKey;
163     CheckKeyExistFunc checkKeyExist;
164     DeleteKeyFunc deleteKey;
165     AesGcmEncryptFunc aesGcmEncrypt;
166     AesGcmDecryptFunc aesGcmDecrypt;
167     HashToPointFunc hashToPoint;
168     AgreeSharedSecretWithStorageFunc agreeSharedSecretWithStorage;
169     AgreeSharedSecretFunc agreeSharedSecret;
170     BigNumExpModFunc bigNumExpMod;
171     GenerateKeyPairWithStorageFunc generateKeyPairWithStorage;
172     GenerateKeyPairFunc generateKeyPair;
173     ExportPublicKeyFunc exportPublicKey;
174     SignFunc sign;
175     VerifyFunc verify;
176     ImportPublicKeyFunc importPublicKey;
177     CheckDlPublicKeyFunc checkDlPublicKey;
178     CheckEcPublicKeyFunc checkEcPublicKey;
179     BigNumCompareFunc bigNumCompare;
180     Base64EncodeFunc base64Encode;
181     Base64DecodeFunc base64Decode;
182 } AlgLoader;
183 
184 #endif