1060ff233Sopenharmony_ci/* 2060ff233Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3060ff233Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4060ff233Sopenharmony_ci * you may not use this file except in compliance with the License. 5060ff233Sopenharmony_ci * You may obtain a copy of the License at 6060ff233Sopenharmony_ci * 7060ff233Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8060ff233Sopenharmony_ci * 9060ff233Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10060ff233Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11060ff233Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12060ff233Sopenharmony_ci * See the License for the specific language governing permissions and 13060ff233Sopenharmony_ci * limitations under the License. 14060ff233Sopenharmony_ci */ 15060ff233Sopenharmony_ci 16060ff233Sopenharmony_ci#ifndef SOFTBUS_ADAPTER_CRYPTO_H 17060ff233Sopenharmony_ci#define SOFTBUS_ADAPTER_CRYPTO_H 18060ff233Sopenharmony_ci 19060ff233Sopenharmony_ci#include <stdint.h> 20060ff233Sopenharmony_ci 21060ff233Sopenharmony_ci#include "softbus_def.h" 22060ff233Sopenharmony_ci 23060ff233Sopenharmony_ci#ifndef AES_GCM_H 24060ff233Sopenharmony_ci#define AES_GCM_H 25060ff233Sopenharmony_ci 26060ff233Sopenharmony_ci#define HUKS_AES_GCM_KEY_LEN 256 27060ff233Sopenharmony_ci#define GCM_IV_LEN 12 28060ff233Sopenharmony_ci#define AAD_LEN 16 29060ff233Sopenharmony_ci 30060ff233Sopenharmony_ci#define TAG_LEN 16 31060ff233Sopenharmony_ci#define OVERHEAD_LEN (GCM_IV_LEN + TAG_LEN) 32060ff233Sopenharmony_ci 33060ff233Sopenharmony_ci#define GCM_KEY_BITS_LEN_128 128 34060ff233Sopenharmony_ci#define GCM_KEY_BITS_LEN_256 256 35060ff233Sopenharmony_ci#define KEY_BITS_UNIT 8 36060ff233Sopenharmony_ci 37060ff233Sopenharmony_ci#define BLE_BROADCAST_IV_LEN 16 38060ff233Sopenharmony_ci 39060ff233Sopenharmony_ci#ifdef __cplusplus 40060ff233Sopenharmony_ci#if __cplusplus 41060ff233Sopenharmony_ciextern "C" { 42060ff233Sopenharmony_ci#endif 43060ff233Sopenharmony_ci#endif 44060ff233Sopenharmony_citypedef struct { 45060ff233Sopenharmony_ci unsigned char *aad; 46060ff233Sopenharmony_ci uint32_t aadLen; 47060ff233Sopenharmony_ci const unsigned char *input; 48060ff233Sopenharmony_ci uint32_t inputLen; 49060ff233Sopenharmony_ci unsigned char **output; 50060ff233Sopenharmony_ci uint32_t *outputLen; 51060ff233Sopenharmony_ci} GcmInputParams; 52060ff233Sopenharmony_ci 53060ff233Sopenharmony_citypedef struct { 54060ff233Sopenharmony_ci uint32_t keyLen; 55060ff233Sopenharmony_ci unsigned char key[SESSION_KEY_LENGTH]; 56060ff233Sopenharmony_ci unsigned char iv[GCM_IV_LEN]; 57060ff233Sopenharmony_ci} AesGcmCipherKey; 58060ff233Sopenharmony_ci 59060ff233Sopenharmony_citypedef struct { 60060ff233Sopenharmony_ci uint32_t keyLen; 61060ff233Sopenharmony_ci unsigned char key[SESSION_KEY_LENGTH]; 62060ff233Sopenharmony_ci unsigned char iv[BLE_BROADCAST_IV_LEN]; 63060ff233Sopenharmony_ci} AesCtrCipherKey; 64060ff233Sopenharmony_ci 65060ff233Sopenharmony_ciint32_t SoftBusBase64Encode(unsigned char *dst, size_t dlen, 66060ff233Sopenharmony_ci size_t *olen, const unsigned char *src, size_t slen); 67060ff233Sopenharmony_ci 68060ff233Sopenharmony_ciint32_t SoftBusBase64Decode(unsigned char *dst, size_t dlen, 69060ff233Sopenharmony_ci size_t *olen, const unsigned char *src, size_t slen); 70060ff233Sopenharmony_ci 71060ff233Sopenharmony_ciint32_t SoftBusGenerateStrHash(const unsigned char *str, uint32_t len, unsigned char *hash); 72060ff233Sopenharmony_ci 73060ff233Sopenharmony_ciint32_t SoftBusGenerateSessionKey(char *key, uint32_t len); 74060ff233Sopenharmony_ci 75060ff233Sopenharmony_ciint32_t SoftBusGenerateRandomArray(unsigned char *randStr, uint32_t len); 76060ff233Sopenharmony_ci 77060ff233Sopenharmony_ciint32_t SoftBusEncryptData(AesGcmCipherKey *key, const unsigned char *input, uint32_t inLen, 78060ff233Sopenharmony_ci unsigned char *encryptData, uint32_t *encryptLen); 79060ff233Sopenharmony_ci 80060ff233Sopenharmony_ciint32_t SoftBusEncryptDataWithSeq(AesGcmCipherKey *cipherKey, const unsigned char *input, uint32_t inLen, 81060ff233Sopenharmony_ci unsigned char *encryptData, uint32_t *encryptLen, int32_t seqNum); 82060ff233Sopenharmony_ci 83060ff233Sopenharmony_ciint32_t SoftBusDecryptData(AesGcmCipherKey *key, const unsigned char *input, uint32_t inLen, 84060ff233Sopenharmony_ci unsigned char *decryptData, uint32_t *decryptLen); 85060ff233Sopenharmony_ci 86060ff233Sopenharmony_ciint32_t SoftBusDecryptDataWithSeq(AesGcmCipherKey *cipherKey, const unsigned char *input, uint32_t inLen, 87060ff233Sopenharmony_ci unsigned char *encryptData, uint32_t *encryptLen, int32_t seqNum); 88060ff233Sopenharmony_ci 89060ff233Sopenharmony_ciuint32_t SoftBusCryptoRand(void); 90060ff233Sopenharmony_ci 91060ff233Sopenharmony_ciint32_t SoftBusEncryptDataByCtr(AesCtrCipherKey *key, const unsigned char *input, uint32_t inLen, 92060ff233Sopenharmony_ci unsigned char *encryptData, uint32_t *encryptLen); 93060ff233Sopenharmony_ci 94060ff233Sopenharmony_ciint32_t SoftBusDecryptDataByCtr(AesCtrCipherKey *key, const unsigned char *input, uint32_t inLen, 95060ff233Sopenharmony_ci unsigned char *decryptData, uint32_t *decryptLen); 96060ff233Sopenharmony_ci 97060ff233Sopenharmony_ci#endif 98060ff233Sopenharmony_ci 99060ff233Sopenharmony_ci#ifdef __cplusplus 100060ff233Sopenharmony_ci#if __cplusplus 101060ff233Sopenharmony_ci} 102060ff233Sopenharmony_ci#endif /* __cplusplus */ 103060ff233Sopenharmony_ci#endif /* __cplusplus */ 104060ff233Sopenharmony_ci#endif /* SOFTBUS_ADAPTER_CRYPTO_H */ 105