• 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 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 #define HKDF_BYTES_LEN 32
30 
31 #define TAG_LEN 16
32 #define SHORT_TAG_LEN 8
33 #define OVERHEAD_LEN (GCM_IV_LEN + TAG_LEN)
34 
35 #define GCM_KEY_BITS_LEN_128 128
36 #define GCM_KEY_BITS_LEN_256 256
37 #define KEY_BITS_UNIT 8
38 
39 #define BLE_BROADCAST_IV_LEN 16
40 
41 #ifdef __cplusplus
42 #if __cplusplus
43 extern "C" {
44 #endif
45 #endif
46 typedef struct {
47     unsigned char *aad;
48     uint32_t aadLen;
49     const unsigned char *input;
50     uint32_t inputLen;
51     unsigned char **output;
52     uint32_t *outputLen;
53 } GcmInputParams;
54 
55 typedef struct {
56     uint32_t keyLen;
57     unsigned char key[SESSION_KEY_LENGTH];
58     unsigned char iv[GCM_IV_LEN];
59 } AesGcmCipherKey;
60 
61 typedef struct {
62     uint32_t keyLen;
63     unsigned char key[SHORT_SESSION_KEY_LENGTH];
64     unsigned char iv[GCM_IV_LEN];
65 } AesGcm128CipherKey;
66 
67 typedef struct {
68     uint32_t keyLen;
69     unsigned char key[SESSION_KEY_LENGTH];
70     unsigned char iv[BLE_BROADCAST_IV_LEN];
71 } AesCtrCipherKey;
72 
73 int32_t SoftBusBase64Encode(unsigned char *dst, size_t dlen,
74     size_t *olen, const unsigned char *src, size_t slen);
75 
76 int32_t SoftBusBase64Decode(unsigned char *dst, size_t dlen,
77     size_t *olen, const unsigned char *src, size_t slen);
78 
79 int32_t SoftBusGenerateStrHash(const unsigned char *str, uint32_t len, unsigned char *hash);
80 
81 int32_t SoftBusGenerateSessionKey(char *key, uint32_t len);
82 
83 int32_t SoftBusGenerateRandomArray(unsigned char *randStr, uint32_t len);
84 
85 int32_t SoftBusEncryptData(AesGcmCipherKey *key, const unsigned char *input, uint32_t inLen,
86     unsigned char *encryptData, uint32_t *encryptLen);
87 
88 int32_t SoftBusEncryptDataWithSeq(AesGcmCipherKey *cipherKey, const unsigned char *input, uint32_t inLen,
89     unsigned char *encryptData, uint32_t *encryptLen, int32_t seqNum);
90 
91 int32_t SoftBusDecryptData(AesGcmCipherKey *key, const unsigned char *input, uint32_t inLen,
92     unsigned char *decryptData, uint32_t *decryptLen);
93 
94 int32_t SoftBusDecryptDataWithSeq(AesGcmCipherKey *cipherKey, const unsigned char *input, uint32_t inLen,
95     unsigned char *encryptData, uint32_t *encryptLen, int32_t seqNum);
96 
97 uint32_t SoftBusCryptoRand(void);
98 
99 int32_t SoftBusEncryptDataByCtr(AesCtrCipherKey *key, const unsigned char *input, uint32_t inLen,
100     unsigned char *encryptData, uint32_t *encryptLen);
101 
102 int32_t SoftBusDecryptDataByCtr(AesCtrCipherKey *key, const unsigned char *input, uint32_t inLen,
103     unsigned char *decryptData, uint32_t *decryptLen);
104 
105 int32_t SoftBusEncryptDataByGcm128(AesGcm128CipherKey *cipherKey, const unsigned char *input, uint32_t inLen,
106     unsigned char *encryptData, uint32_t *encryptLen);
107 
108 int32_t SoftBusDecryptDataByGcm128(AesGcm128CipherKey *cipherKey, const unsigned char *input, uint32_t inLen,
109     unsigned char *decryptData, uint32_t *decryptLen);
110 
111 int32_t SoftBusCalcHKDF(const uint8_t *inData, uint32_t inLen, uint8_t *outData, uint32_t outLen);
112 
113 #endif
114 
115 #ifdef __cplusplus
116 #if __cplusplus
117 }
118 #endif /* __cplusplus */
119 #endif /* __cplusplus */
120 #endif /* SOFTBUS_ADAPTER_CRYPTO_H */
121