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 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 #define AES_128_KEY_LENGTH 16 31 #define AES_192_KEY_LENGTH 24 32 #define AES_256_KEY_LENGTH 32 33 #define GCM_IV_LENGTH 12 34 #define GCM_MAX_AAD_LENGTH 64 35 #define GCM_TAG_LENGTH 16 36 #define GCM_ADDED_LEN (GCM_IV_LENGTH + GCM_TAG_LENGTH) 37 #define KEY_BITS_UNIT 8 38 39 typedef struct { 40 uint8_t key[AES_256_KEY_LENGTH]; 41 uint32_t keylen; 42 uint8_t iv[GCM_IV_LENGTH]; 43 uint32_t ivLen; 44 uint8_t aad[GCM_MAX_AAD_LENGTH]; 45 uint32_t aadLen; 46 } CryptPara; 47 48 NSTACKX_EXPORT uint32_t AesGcmEncrypt(const uint8_t *inBuff, uint32_t inLen, CryptPara *cryptPara, 49 uint8_t *outBuff, uint32_t outLen); 50 NSTACKX_EXPORT uint32_t AesGcmDecrypt(uint8_t *inBuff, uint32_t inLen, CryptPara *cryptPara, 51 uint8_t *outBuff, uint32_t outLen); 52 NSTACKX_EXPORT int32_t GetRandBytes(uint8_t *buf, uint32_t len); 53 NSTACKX_EXPORT uint8_t IsCryptoIncluded(void); 54 #endif 55 56 #ifdef __cplusplus 57 } 58 #endif 59 60 #endif 61