• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 DLP_CRYPT_H
17 #define DLP_CRYPT_H
18 
19 #include <stdint.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 enum DlpKeyDigest {
26     DLP_DIGEST_NONE = 0,
27     DLP_DIGEST_SHA256 = 12,
28     DLP_DIGEST_SHA384 = 13,
29     DLP_DIGEST_SHA512 = 14,
30 };
31 
32 struct DlpOpensslAesCtx {
33     uint32_t mode;
34     uint32_t padding;
35     void* append;
36 };
37 
38 enum DLP_DIGEST_LEN {
39     SHA256_LEN = 32,
40     SHA384_LEN = 48,
41     SHA512_LEN = 64,
42 };
43 
44 #define OPENSSL_CTX_PADDING_NONE (0)   /* set chipher padding none */
45 #define OPENSSL_CTX_PADDING_ENABLE (1) /* set chipher padding enable */
46 
47 #define DLP_BITS_PER_BYTE (8)
48 #define DLP_KEY_BYTES(keySize) (((keySize) + DLP_BITS_PER_BYTE - 1) / DLP_BITS_PER_BYTE)
49 
50 #define DLP_OPENSSL_ERROR_LEN 128
51 
52 #define DLP_OPENSSL_SUCCESS 1 /* openssl return 1: success */
53 
54 #define BIT_NUM_OF_UINT8 8
55 
56 #define DLP_RANDOM_MAX_SIZE 1024
57 
58 const static uint32_t HIAE_STATE_SIZE = 256;
59 
60 typedef struct {
61     uint8_t state[HIAE_STATE_SIZE];
62     uint64_t msgLen;
63     uint64_t adLen;
64 } HIAE_CipherCtx;
65 
66 enum DlpKeySize {
67     DLP_AES_KEY_SIZE_128 = 128,
68     DLP_AES_KEY_SIZE_192 = 192,
69     DLP_AES_KEY_SIZE_256 = 256,
70 };
71 
72 struct DlpBlob {
73     uint32_t size = 0;
74     uint8_t* data = nullptr;
75 };
76 
77 struct DlpCipherParam {
78     struct DlpBlob iv;
79 };
80 
81 struct DlpUsageSpec {
82     uint32_t mode;
83     struct DlpCipherParam* algParam;
84 };
85 
86 enum DlpCipherMode {
87     DLP_MODE_CTR = 1,
88     DLP_MODE_HIAE = 2,
89 };
90 
91 enum DlpKeyPadding {
92     DLP_PADDING_NONE = 0,
93     DLP_PADDING_OAEP = 1,
94     DLP_PADDING_PSS = 2,
95     DLP_PADDING_PKCS1_V1_5 = 3,
96     DLP_PADDING_PKCS5 = 4,
97     DLP_PADDING_PKCS7 = 5,
98 };
99 
100 #define SELF_FREE_PTR(PTR, FREE_FUNC) \
101     {                                 \
102         if ((PTR) != NULL) {          \
103             FREE_FUNC(PTR);           \
104             (PTR) = NULL;             \
105         }                             \
106     }
107 
108 #define DLP_FREE_PTR(p) SELF_FREE_PTR(p, free)
109 
110 int32_t DlpOpensslGenerateRandom(uint32_t keySize, struct DlpBlob* key);
111 
112 int32_t DlpOpensslGenerateRandomKey(uint32_t keySize, struct DlpBlob* key);
113 
114 int32_t DlpOpensslAesEncrypt(const struct DlpBlob* key, const struct DlpUsageSpec* usageSpec,
115     const struct DlpBlob* message, struct DlpBlob* cipherText);
116 
117 int32_t DlpOpensslAesDecrypt(const struct DlpBlob* key, const struct DlpUsageSpec* usageSpec,
118     const struct DlpBlob* message, struct DlpBlob* plainText);
119 
120 int32_t DlpOpensslAesEncryptInit(void** cryptoCtx, const struct DlpBlob* key, const struct DlpUsageSpec* usageSpec);
121 
122 int32_t DlpOpensslAesEncryptUpdate(void* cryptoCtx, const struct DlpBlob* message, struct DlpBlob* cipherText);
123 
124 int32_t DlpOpensslAesEncryptFinal(void** cryptoCtx, const struct DlpBlob* message, struct DlpBlob* cipherText);
125 
126 int32_t DlpOpensslAesDecryptInit(void** cryptoCtx, const struct DlpBlob* key, const struct DlpUsageSpec* usageSpec);
127 
128 int32_t DlpOpensslAesDecryptUpdate(void* cryptoCtx, const struct DlpBlob* message, struct DlpBlob* plainText);
129 
130 int32_t DlpOpensslAesDecryptFinal(void** cryptoCtx, const struct DlpBlob* message, struct DlpBlob* plainText);
131 
132 void DlpOpensslAesHalFreeCtx(void** cryptoCtx);
133 
134 int32_t DlpOpensslHash(uint32_t alg, const struct DlpBlob* msg, struct DlpBlob* hash);
135 
136 int32_t DlpOpensslHashInit(void** cryptoCtx, uint32_t alg);
137 
138 int32_t DlpOpensslHashUpdate(void* cryptoCtx, const struct DlpBlob* msg);
139 
140 int32_t DlpOpensslHashFinal(void** cryptoCtx, const struct DlpBlob* msg, struct DlpBlob* hash);
141 
142 int32_t DlpOpensslHashFreeCtx(void** cryptoCtx);
143 
144 int32_t DlpCtrModeIncreaeIvCounter(struct DlpBlob& iv, uint32_t count);
145 
146 int32_t DlpHmacEncodeForRaw(const DlpBlob& key, int32_t fd, uint64_t fileSize, DlpBlob& out);
147 
148 int32_t DlpHmacEncode(const DlpBlob& key, int32_t fd, DlpBlob& out);
149 
150 int32_t InitDlpHIAEMgr(void);
151 
152 void ClearDlpHIAEMgr(void);
153 
154 int32_t DlpHIAEEncrypt(const struct DlpBlob *key, const struct DlpUsageSpec *usageSpec, const uint32_t inLen,
155     const uint8_t *message, uint8_t *cipherText);
156 
157 int32_t DlpHIAEDecrypt(const struct DlpBlob *key, const struct DlpUsageSpec *usageSpec, const uint32_t inLen,
158     const uint8_t *message, uint8_t *plainText);
159 
160 #ifdef __cplusplus
161 }
162 #endif
163 
164 #endif
165