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 NSTACKX_MBEDTLS_H 17#define NSTACKX_MBEDTLS_H 18 19#ifdef MBEDTLS_INCLUDED 20#include "nstackx_common_header.h" 21 22#include "mbedtls/gcm.h" 23#include "mbedtls/ctr_drbg.h" 24#include "mbedtls/entropy.h" 25#include "mbedtls/chachapoly.h" 26#include "mbedtls/version.h" 27#ifdef __cplusplus 28extern "C" { 29#endif 30 31#define AES_128_KEY_LENGTH 16 32#define AES_192_KEY_LENGTH 24 33#define AES_256_KEY_LENGTH 32 34#define GCM_IV_LENGTH 12 35#define GCM_MAX_AAD_LENGTH 64 36#define GCM_TAG_LENGTH 16 37#define GCM_ADDED_LEN (GCM_IV_LENGTH + GCM_TAG_LENGTH) 38#define KEY_BITS_UNIT 8 39#define CHACHA20_KEY_LENGTH 32 40#define CHACHA20_POLY1305_NAME "MBEDTLS_POLY1305_C" 41 42typedef void MBEDTLS_CTX; 43 44typedef struct { 45 uint8_t key[AES_256_KEY_LENGTH]; 46 uint32_t keylen; 47 uint8_t iv[GCM_IV_LENGTH]; 48 uint32_t ivLen; 49 uint8_t aad[GCM_MAX_AAD_LENGTH]; 50 uint32_t aadLen; 51 uint8_t cipherType; 52 MBEDTLS_CTX *ctx; 53} CryptPara; 54 55NSTACKX_EXPORT uint32_t AesGcmEncrypt(const uint8_t *inBuff, uint32_t inLen, CryptPara *cryptPara, 56 uint8_t *outBuff, uint32_t outLen); 57NSTACKX_EXPORT uint32_t AesGcmDecrypt(uint8_t *inBuff, uint32_t inLen, CryptPara *cryptPara, 58 uint8_t *outBuff, uint32_t outLen); 59NSTACKX_EXPORT int32_t GetRandBytes(uint8_t *buf, uint32_t len); 60NSTACKX_EXPORT uint8_t IsCryptoIncluded(void); 61NSTACKX_EXPORT uint8_t QueryCipherSupportByName(char *name); 62NSTACKX_EXPORT MBEDTLS_CTX ClearCryptCtx(MBEDTLS_CTX *ctx); 63NSTACKX_EXPORT MBEDTLS_CTX *CreateCryptCtx(void); 64NSTACKX_EXPORT uint8_t IsSupportHardwareAesNi(void); 65 66#endif 67 68#ifdef __cplusplus 69} 70#endif 71 72#endif 73