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 SOFTBUS_ADAPTER_CRYPTO_H
17#define SOFTBUS_ADAPTER_CRYPTO_H
18
19#include <stdint.h>
20
21#include "softbus_def.h"
22
23#ifndef AES_GCM_H
24#define AES_GCM_H
25
26#define HUKS_AES_GCM_KEY_LEN 256
27#define GCM_IV_LEN 12
28#define AAD_LEN 16
29
30#define TAG_LEN 16
31#define OVERHEAD_LEN (GCM_IV_LEN + TAG_LEN)
32
33#define GCM_KEY_BITS_LEN_128 128
34#define GCM_KEY_BITS_LEN_256 256
35#define KEY_BITS_UNIT 8
36
37#define BLE_BROADCAST_IV_LEN 16
38
39#ifdef __cplusplus
40#if __cplusplus
41extern "C" {
42#endif
43#endif
44typedef struct {
45    unsigned char *aad;
46    uint32_t aadLen;
47    const unsigned char *input;
48    uint32_t inputLen;
49    unsigned char **output;
50    uint32_t *outputLen;
51} GcmInputParams;
52
53typedef struct {
54    uint32_t keyLen;
55    unsigned char key[SESSION_KEY_LENGTH];
56    unsigned char iv[GCM_IV_LEN];
57} AesGcmCipherKey;
58
59typedef struct {
60    uint32_t keyLen;
61    unsigned char key[SESSION_KEY_LENGTH];
62    unsigned char iv[BLE_BROADCAST_IV_LEN];
63} AesCtrCipherKey;
64
65int32_t SoftBusBase64Encode(unsigned char *dst, size_t dlen,
66    size_t *olen, const unsigned char *src, size_t slen);
67
68int32_t SoftBusBase64Decode(unsigned char *dst, size_t dlen,
69    size_t *olen, const unsigned char *src, size_t slen);
70
71int32_t SoftBusGenerateStrHash(const unsigned char *str, uint32_t len, unsigned char *hash);
72
73int32_t SoftBusGenerateSessionKey(char *key, uint32_t len);
74
75int32_t SoftBusGenerateRandomArray(unsigned char *randStr, uint32_t len);
76
77int32_t SoftBusEncryptData(AesGcmCipherKey *key, const unsigned char *input, uint32_t inLen,
78    unsigned char *encryptData, uint32_t *encryptLen);
79
80int32_t SoftBusEncryptDataWithSeq(AesGcmCipherKey *cipherKey, const unsigned char *input, uint32_t inLen,
81    unsigned char *encryptData, uint32_t *encryptLen, int32_t seqNum);
82
83int32_t SoftBusDecryptData(AesGcmCipherKey *key, const unsigned char *input, uint32_t inLen,
84    unsigned char *decryptData, uint32_t *decryptLen);
85
86int32_t SoftBusDecryptDataWithSeq(AesGcmCipherKey *cipherKey, const unsigned char *input, uint32_t inLen,
87    unsigned char *encryptData, uint32_t *encryptLen, int32_t seqNum);
88
89uint32_t SoftBusCryptoRand(void);
90
91int32_t SoftBusEncryptDataByCtr(AesCtrCipherKey *key, const unsigned char *input, uint32_t inLen,
92    unsigned char *encryptData, uint32_t *encryptLen);
93
94int32_t SoftBusDecryptDataByCtr(AesCtrCipherKey *key, const unsigned char *input, uint32_t inLen,
95    unsigned char *decryptData, uint32_t *decryptLen);
96
97#endif
98
99#ifdef __cplusplus
100#if __cplusplus
101}
102#endif /* __cplusplus */
103#endif /* __cplusplus */
104#endif /* SOFTBUS_ADAPTER_CRYPTO_H */
105