1 /*
2  * Copyright (c) 2023 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 SOFTBUS_AES_ENCRYPT_H
17 #define SOFTBUS_AES_ENCRYPT_H
18 
19 #include <stdint.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #define ENCRYPT_MODE           1
26 #define DECRYPT_MODE           0
27 #define SHA256_MAC_LEN         32
28 #define AES_SESSION_KEY_LENGTH 16
29 #define AES_IV_LENGTH          16
30 #define AES_GCM_TAG_LEN        16
31 #define RANDOM_LENGTH          8
32 
33 typedef struct {
34     uint8_t *key;
35     uint32_t keyLen;
36     uint8_t *iv;
37     uint32_t ivLen;
38 } AesCipherKey;
39 
40 typedef struct {
41     const uint8_t *key;
42     uint32_t len;
43 } EncryptKey;
44 
45 typedef struct {
46     const uint8_t *data;
47     uint32_t len;
48 } AesInputData;
49 
50 typedef struct {
51     uint8_t *data;
52     uint32_t len;
53 } AesOutputData;
54 
55 int32_t SoftBusGenerateHmacHash(
56     const EncryptKey *randomKey, const uint8_t *rootKey, uint32_t rootKeyLen, uint8_t *hash, uint32_t hashLen);
57 
58 // Aes-cfb encrypt and decrypt by randomKey and rootKey
59 int32_t SoftBusAesCfbRootEncrypt(const AesInputData *inData, const EncryptKey *randomKey, EncryptKey *rootKey,
60     int32_t encMode, AesOutputData *outData);
61 
62 int32_t SoftBusAesCfbEncrypt(
63     const AesInputData *inData, AesCipherKey *cipherKey, int32_t encMode, AesOutputData *outData);
64 
65 int32_t SoftBusAesGcmEncrypt(
66     const AesInputData *inData, AesCipherKey *cipherKey, int32_t encMode, AesOutputData *outData);
67 
68 #ifdef __cplusplus
69 }
70 #endif
71 #endif /* SOFTBUS_AES_ENCRYPT_H */
72