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 #ifdef __cplusplus 38 #if __cplusplus 39 extern "C" { 40 #endif 41 #endif 42 typedef struct { 43 unsigned char *aad; 44 uint32_t aadLen; 45 const unsigned char *input; 46 uint32_t inputLen; 47 unsigned char **output; 48 uint32_t *outputLen; 49 } GcmInputParams; 50 51 typedef struct { 52 uint32_t keyLen; 53 unsigned char key[SESSION_KEY_LENGTH]; 54 unsigned char iv[GCM_IV_LEN]; 55 } AesGcmCipherKey; 56 57 int32_t SoftBusBase64Encode(unsigned char *dst, size_t dlen, 58 size_t *olen, const unsigned char *src, size_t slen); 59 60 int32_t SoftBusBase64Decode(unsigned char *dst, size_t dlen, 61 size_t *olen, const unsigned char *src, size_t slen); 62 63 int32_t SoftBusGenerateStrHash(const unsigned char *str, uint32_t len, unsigned char *hash); 64 65 int32_t SoftBusGenerateSessionKey(char* key, uint32_t len); 66 67 int32_t SoftBusGenerateRandomArray(unsigned char *randStr, uint32_t len); 68 69 int32_t SoftBusEncryptData(AesGcmCipherKey *key, const unsigned char *input, uint32_t inLen, 70 unsigned char *encryptData, uint32_t *encryptLen); 71 72 int32_t SoftBusEncryptDataWithSeq(AesGcmCipherKey *cipherKey, const unsigned char *input, uint32_t inLen, 73 unsigned char *encryptData, uint32_t *encryptLen, int32_t seqNum); 74 75 int32_t SoftBusDecryptData(AesGcmCipherKey *key, const unsigned char *input, uint32_t inLen, 76 unsigned char *decryptData, uint32_t *decryptLen); 77 78 int32_t SoftBusDecryptDataWithSeq(AesGcmCipherKey *cipherKey, const unsigned char *input, uint32_t inLen, 79 unsigned char *encryptData, uint32_t *encryptLen, int32_t seqNum); 80 81 uint32_t SoftBusCryptoRand(void); 82 83 #endif 84 85 #ifdef __cplusplus 86 #if __cplusplus 87 } 88 #endif /* __cplusplus */ 89 #endif /* __cplusplus */ 90 #endif /* SOFTBUS_ADAPTER_CRYPTO_H */ 91