!7343 修改协议字段

Merge pull request !7343 from erestu/master
This commit is contained in:
openharmony_ci 2024-08-30 02:29:55 +00:00 committed by Gitee
commit c6ed2e2a93
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
15 changed files with 208 additions and 77 deletions

View File

@ -40,6 +40,12 @@ typedef struct _WifiStationInfo {
uint32_t freq;
} WifiStationInfo;
/* DFX */
typedef struct _RamInfo {
uint32_t availableRam;
uint32_t totalRam;
} RamInfo;
typedef enum {
ROOT_QUEUE = -1, /* ROOT queueu */
HI_QUEUE = 1, /* QDISC queue 1 WLAN_HI_QUEUE = 0 */

View File

@ -47,7 +47,7 @@ pthread_mutex_t g_dFileSessionChainMutex = PTHREAD_MUTEX_INITIALIZER;
List g_dFileSessionChain = {&(g_dFileSessionChain), &(g_dFileSessionChain)};
static uint16_t g_dFileSessionId = 0;
/* currently enabled capabilities */
static uint32_t g_capabilities = NSTACKX_CAPS_WLAN_CATAGORY | NSTACKX_CAPS_CHACHA;
static uint32_t g_capabilities = NSTACKX_CAPS_WLAN_CATAGORY;
/* wlan catagory from APP */
static uint32_t g_wlanCatagory = NSTACKX_WLAN_CAT_TCP;
@ -900,7 +900,7 @@ static inline void PostSessionCreate(DFileSession *session)
{
session->capability = g_capabilities;
session->wlanCatagory = g_wlanCatagory;
session->cipherCapability = NSTACKX_CIPHER_AES_GCM | NSTACKX_CIPHER_CHACHA;
DFILE_LOGI(TAG, "current capabilities tcp:%d", CapsTcp(session));
}

View File

@ -41,6 +41,14 @@ BindInfo g_recver8CoreList[DFILE_BIND_TYPE_INDEX_MAX][DFILE_MAX_THREAD_NUM] = {
{{0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x00}, {0, 0x20}, {0, 0x00}}, /* lowspeed */
};
enum {
NO_CHECK = 0,
CHECKED_NOT_SUPPORT,
CHECKED_SUPPORT
};
static uint8_t g_aesInChecked = NO_CHECK;
void SetTidToBindInfo(const DFileSession *session, uint32_t pos)
{
pid_t tid = gettid();
@ -199,24 +207,54 @@ void SetTcpKeepAlive(SocketDesc fd)
#endif
}
static bool CheckIsSupportHardwareAesNi(void)
{
if (g_aesInChecked == NO_CHECK) {
g_aesInChecked = IsSupportHardwareAesNi() ? CHECKED_SUPPORT : CHECKED_NOT_SUPPORT;
DFILE_LOGI(TAG, "g_aesInChecked is set as %hhu", g_aesInChecked);
}
return g_aesInChecked == CHECKED_SUPPORT;
}
void DFileGetCipherCaps(DFileSession *session, SettingFrame *settingFramePara)
{
if (CapsChaCha(session) && DFileGetDeviceBits() == DEVICE_32_BITS &&
QueryCipherSupportByName(CHACHA20_POLY1305_NAME)) {
settingFramePara->cipherCapability |= NSTACKX_CIPHER_CHACHA;
if (CapsChaCha(session) && QueryCipherSupportByName(CHACHA20_POLY1305_NAME)) {
session->cipherCapability |= NSTACKX_CIPHER_CHACHA;
DFILE_LOGI(TAG, "local cipher support %s.", CHACHA20_POLY1305_NAME);
} else {
session->capability &= ~NSTACKX_CAPS_CHACHA;
DFILE_LOGI(TAG, "local cipher no support %s.", CHACHA20_POLY1305_NAME);
session->cipherCapability &= ~NSTACKX_CIPHER_CHACHA;
DFILE_LOGI(TAG, "local cipher no support %s, CapsChaCha is %hhu.", CHACHA20_POLY1305_NAME, CapsChaCha(session));
}
bool ret = CheckIsSupportHardwareAesNi();
if (ret) {
session->cipherCapability |= NSTACKX_CIPHER_AES_NI;
}
settingFramePara->cipherCapability = session->cipherCapability;
DFILE_LOGI(TAG, "local cipher AES_NI state is %s", ret ? "true" : "false");
}
void DFileChooseCipherType(SettingFrame *hostSettingFrame, DFileSession *session)
{
if ((hostSettingFrame->cipherCapability & NSTACKX_CIPHER_CHACHA) && (DFileGetDeviceBits() == DEVICE_32_BITS) &&
(session->fileManager->keyLen == CHACHA20_KEY_LENGTH)) {
session->capability |= NSTACKX_CAPS_CHACHA;
} else {
session->capability &= ~NSTACKX_CAPS_CHACHA;
if (session->fileManager->keyLen != CHACHA20_KEY_LENGTH) {
session->cipherCapability &= ~NSTACKX_CIPHER_CHACHA;
DFILE_LOGI(TAG, "opposite replies no use chacha cipher");
return;
}
uint8_t isRemoteSupportChacha = ((hostSettingFrame->cipherCapability & NSTACKX_CIPHER_CHACHA) != 0);
uint8_t isRemoteSupportAesNi = ((hostSettingFrame->cipherCapability & NSTACKX_CIPHER_AES_NI) != 0);
uint8_t isUseMtp = NSTACKX_FALSE;
#ifdef DFILE_ADAPT_MTP
isUseMtp = session->useMtpFlag;
#endif
uint8_t isLocalUseChacha = QueryCipherSupportByName(CHACHA20_POLY1305_NAME) && !isUseMtp;
bool isLocalSupportAesNi = CheckIsSupportHardwareAesNi();
if (isRemoteSupportChacha && isLocalUseChacha && !(isRemoteSupportAesNi && isLocalSupportAesNi)) {
session->cipherCapability |= NSTACKX_CIPHER_CHACHA;
} else {
session->cipherCapability &= ~NSTACKX_CIPHER_CHACHA;
}
DFILE_LOGI(TAG, "opposite replies %s use chacha cipher", CapsChaCha(session) ? "" : "no");
}

View File

@ -302,7 +302,6 @@ void EncodeSettingFrame(uint8_t *buffer, size_t length, size_t *frameLength, con
settingFrame->dataFrameSize = htonl(settingFramePara->dataFrameSize);
settingFrame->capsCheck = htonl(settingFramePara->capsCheck);
settingFrame->cipherCapability = htonl(settingFramePara->cipherCapability);
settingFrame->deviceBits = htons(settingFramePara->deviceBits);
}
/* Caller should make sure that "length" can cover the minimum header length */
@ -530,39 +529,59 @@ static uint8_t IsSettingFrameLengthValid(const SettingFrame *hostSettingFrame, u
* From dfile with historical version NSTACKX_DFILE_VERSION_0, whose setting frame is composed of header,
* mtu and connType.
*/
if (payloadLength == sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType)) {
size_t hostFrameLength = 0;
if (payloadLength == (hostFrameLength += sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType))) {
return NSTACKX_TRUE;
}
if (payloadLength == sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType) +
sizeof(hostSettingFrame->dFileVersion)) {
if (payloadLength == (hostFrameLength += sizeof(hostSettingFrame->dFileVersion))) {
return NSTACKX_TRUE;
}
if (payloadLength == sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType) +
sizeof(hostSettingFrame->dFileVersion) + sizeof(hostSettingFrame->abmCapability)) {
if (payloadLength == (hostFrameLength += sizeof(hostSettingFrame->abmCapability))) {
return NSTACKX_TRUE;
}
if (payloadLength == sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType) +
sizeof(hostSettingFrame->dFileVersion) + sizeof(hostSettingFrame->abmCapability) +
sizeof(hostSettingFrame->capability)) {
if (payloadLength == (hostFrameLength += sizeof(hostSettingFrame->capability))) {
return NSTACKX_TRUE;
}
if (payloadLength == sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType) +
sizeof(hostSettingFrame->dFileVersion) + sizeof(hostSettingFrame->abmCapability) +
sizeof(hostSettingFrame->capability) + sizeof(hostSettingFrame->dataFrameSize)) {
if (payloadLength == (hostFrameLength += sizeof(hostSettingFrame->dataFrameSize))) {
return NSTACKX_TRUE;
}
if (payloadLength == sizeof(hostSettingFrame->mtu) + sizeof(hostSettingFrame->connType) +
sizeof(hostSettingFrame->dFileVersion) + sizeof(hostSettingFrame->abmCapability) +
sizeof(hostSettingFrame->capability) + sizeof(hostSettingFrame->dataFrameSize) +
sizeof(hostSettingFrame->capsCheck)) {
if (payloadLength == (hostFrameLength += sizeof(hostSettingFrame->capsCheck))) {
return NSTACKX_TRUE;
}
if (payloadLength == (hostFrameLength += sizeof(hostSettingFrame->capsCheck))) {
return NSTACKX_TRUE;
}
if (payloadLength == (hostFrameLength += sizeof(hostSettingFrame->productVersion))) {
return NSTACKX_TRUE;
}
if (payloadLength == (hostFrameLength += sizeof(hostSettingFrame->isSupport160M))) {
return NSTACKX_TRUE;
}
if (payloadLength ==
(hostFrameLength += sizeof(hostSettingFrame->isSupportMtp) + sizeof(hostSettingFrame->mtpPort))) {
return NSTACKX_TRUE;
}
if (payloadLength == (hostFrameLength += sizeof(hostSettingFrame->headerEnc))) {
return NSTACKX_TRUE;
}
if (payloadLength == (hostFrameLength += sizeof(hostSettingFrame->mtpCapability))) {
return NSTACKX_TRUE;
}
if (payloadLength == (hostFrameLength += sizeof(hostSettingFrame->cipherCapability))) {
return NSTACKX_TRUE;
}
/*
* From dfile with the same version with local dfile.
*/
@ -649,14 +668,13 @@ int32_t DecodeSettingFrame(SettingFrame *netSettingFrame, SettingFrame *hostSett
sizeof(hostSettingFrame->capability) + sizeof(hostSettingFrame->dataFrameSize) +
sizeof(hostSettingFrame->capsCheck))) {
hostSettingFrame->cipherCapability = ntohl(netSettingFrame->cipherCapability);
hostSettingFrame->deviceBits = ntohs(netSettingFrame->deviceBits);
}
}
DFILE_LOGI(TAG, "local version is %u, remote version is %u capability 0x%x dataFrameSize %u capsCheck 0x%x "
"cipherCaps 0x%x deviceBits %u",
"cipherCaps 0x%x",
NSTACKX_DFILE_VERSION, hostSettingFrame->dFileVersion, hostSettingFrame->capability,
hostSettingFrame->dataFrameSize, hostSettingFrame->capsCheck,
hostSettingFrame->cipherCapability, hostSettingFrame->deviceBits);
hostSettingFrame->cipherCapability);
return NSTACKX_EOK;
}

View File

@ -457,7 +457,6 @@ void DFileSessionSendSetting(PeerInfo *peerInfo)
settingFramePara.capsCheck = NSTACKX_INTERNAL_CAPS_RECV_FEEDBACK;
if (peerInfo->session->fileManager->keyLen) {
DFileGetCipherCaps(peerInfo->session, &settingFramePara);
settingFramePara.deviceBits = DFileGetDeviceBits();
}
EncodeSettingFrame(buf, NSTACKX_DEFAULT_FRAME_SIZE, &frameLen, &settingFramePara);

View File

@ -132,6 +132,7 @@ typedef struct {
uint16_t fileId;
uint32_t blockSequence;
uint32_t linkSequence;
uint8_t socketIndex;
uint8_t blockPayload[0];
} FileDataFrameZS;
@ -156,6 +157,7 @@ typedef struct {
uint16_t fileId[0];
} FileTransferDoneAckFrame;
#define VERSION_STR_LEN 64
typedef struct {
DFileFrameHeader header;
uint16_t mtu;
@ -165,8 +167,13 @@ typedef struct {
uint32_t capability;
uint32_t dataFrameSize;
uint32_t capsCheck;
char productVersion[VERSION_STR_LEN]; /* DFX */
uint8_t isSupport160M;
uint8_t isSupportMtp;
uint8_t mtpPort;
uint8_t headerEnc;
uint32_t mtpCapability;
uint32_t cipherCapability;
uint16_t deviceBits;
} SettingFrame;
typedef struct {
@ -178,6 +185,7 @@ typedef struct {
typedef struct {
DFileFrameHeader header;
WifiStationInfo wifiStationInfo;
RamInfo ramInfo;
} CongestionControlFrame;
#pragma pack(pop)

View File

@ -36,6 +36,7 @@ enum {
#define NSTACKX_CIPHER_AES_GCM NBITS(CIPHER_AES_GCM)
#define NSTACKX_CIPHER_CHACHA NBITS(CIPHER_CHACHA)
#define NSTACKX_CIPHER_AES_NI NBITS(CIPHER_AES_NI)
#ifdef __cplusplus
}

View File

@ -151,6 +151,7 @@ struct DFileSession {
int32_t allTaskCount;
pthread_mutex_t backPressLock;
uint32_t stopSendCnt[NSTACKX_MAX_CLIENT_SEND_THREAD_NUM];
uint32_t cipherCapability;
};
PeerInfo *CreatePeerInfo(DFileSession *session, const struct sockaddr_in *peerAddr,
@ -238,7 +239,8 @@ static inline bool CapsRecvFeedback(const struct DFileSession *session)
static inline bool CapsChaCha(const struct DFileSession *session)
{
return (session->fileManager->keyLen == CHACHA20_KEY_LENGTH) && (session->capability & NSTACKX_CAPS_CHACHA);
return (session->fileManager->keyLen == CHACHA20_KEY_LENGTH) &&
(session->cipherCapability & NSTACKX_CIPHER_CHACHA);
}
void NSTACKX_DFileAssembleFunc(void *softObj, const DFileEvent *info);

View File

@ -87,7 +87,6 @@ enum {
CAPS_ALG_NORATE, // NoRate algorithm
CAPS_RESUMABLE_TRANS,
CAPS_ZEROCOPY,
CAPS_CHACHA_CIPHRE,
/* add more capability here */
CAPS_MAX,
};
@ -104,7 +103,6 @@ enum {
#define NSTACKX_CAPS_LINK_SEQUENCE NBITS(CAPS_LINK_SEQUENCE)
#define NSTACKX_CAPS_WLAN_CATAGORY NBITS(CAPS_WLAN_CATAGORY)
#define NSTACKX_CAPS_MULTIPATH NBITS(CAPS_MULTIPATH)
#define NSTACKX_CAPS_CHACHA NBITS(CAPS_CHACHA_CIPHRE)
#define NSTACKX_CAPS_MASK (NBITS(CAPS_MAX) - 1)

View File

@ -186,6 +186,7 @@ if (defined(ohos_lite)) {
cflags += [
"-DNSTACKX_WITH_HMOS_LINUX",
"-DENABLE_USER_LOG",
"-DNSTACKX_WITH_LINUX_STANDARD",
]
if (defined(board_toolchain_type)) {
if (board_toolchain_type != "iccarm") {

View File

@ -270,4 +270,11 @@ uint8_t QueryCipherSupportByName(char *name)
LOGI(TAG, "devices no support %s", name);
return NSTACKX_FALSE;
}
/* check CPU supports AES-NI hardware optimize */
uint8_t IsSupportHardwareAesNi(void)
{
LOGI(TAG, "no support AES-NI");
return NSTACKX_FALSE;
}
#endif // MBEDTLS_INCLUDED

View File

@ -18,6 +18,22 @@
#include "nstackx_log.h"
#include "securec.h"
#if defined(SSL_AND_CRYPTO_INCLUDED) && defined(NSTACKX_WITH_LINUX_STANDARD)
#include <sys/auxv.h>
#endif
#ifdef BUILD_FOR_WINDOWS
#if defined(__GNC__)
#include <cpuid.h>
#elif define(_MSC_VER)
#include <intrin.h>
#endif
#ifndef bit_AES
#define bit_AES (1<<25)
#endif
#endif // BUILD_FOR_WINDOWS
#define TAG "nStackXDFile"
#ifdef SSL_AND_CRYPTO_INCLUDED
@ -48,30 +64,33 @@ void ClearCryptCtx(EVP_CIPHER_CTX *ctx)
EVP_CIPHER_CTX_free(ctx);
}
}
static int32_t InitEncryptCtx(CryptPara *cryptPara)
static const EVP_CIPHER *GetCipher(CryptPara *cryptPara)
{
int32_t length;
const EVP_CIPHER *cipher = NULL;
if (cryptPara->cipherType == CIPHER_CHACHA) {
cipher = EVP_get_cipherbyname(CHACHA20_POLY1305_NAME);
return EVP_get_cipherbyname(CHACHA20_POLY1305_NAME);
} else if (cryptPara->cipherType == CIPHER_AES_GCM) {
switch (cryptPara->keylen) {
case AES_128_KEY_LENGTH:
cipher = EVP_aes_128_gcm();
return EVP_aes_128_gcm();
break;
case AES_192_KEY_LENGTH:
cipher = EVP_aes_192_gcm();
return EVP_aes_192_gcm();
break;
case AES_256_KEY_LENGTH:
cipher = EVP_aes_256_gcm();
return EVP_aes_256_gcm();
break;
default:
return NSTACKX_EFAILED;
return NULL;
}
}
return NULL;
}
static int32_t InitEncryptCtx(CryptPara *cryptPara)
{
int32_t length;
const EVP_CIPHER *cipher = GetCipher(cryptPara);
if (cryptPara->aadLen == 0 || cryptPara->ctx == NULL) {
if (cipher == NULL ||cryptPara->aadLen == 0 || cryptPara->ctx == NULL) {
return NSTACKX_EFAILED;
}
@ -149,25 +168,9 @@ uint32_t AesGcmEncrypt(const uint8_t *inBuf, uint32_t inLen, CryptPara *cryptPar
static int32_t InitDecryptCtx(CryptPara *cryptPara)
{
int32_t length;
const EVP_CIPHER *cipher = NULL;
if (cryptPara->cipherType == CIPHER_CHACHA) {
cipher = EVP_get_cipherbyname(CHACHA20_POLY1305_NAME);
} else if (cryptPara->cipherType == CIPHER_AES_GCM) {
switch (cryptPara->keylen) {
case AES_128_KEY_LENGTH:
cipher = EVP_aes_128_gcm();
break;
case AES_192_KEY_LENGTH:
cipher = EVP_aes_192_gcm();
break;
case AES_256_KEY_LENGTH:
cipher = EVP_aes_256_gcm();
break;
default:
return NSTACKX_EFAILED;
}
}
if (cryptPara->ivLen != GCM_IV_LENGTH || cryptPara->aadLen == 0 || cryptPara->ctx == NULL) {
const EVP_CIPHER *cipher = GetCipher(cryptPara);
if (cipher == NULL || cryptPara->ivLen != GCM_IV_LENGTH || cryptPara->aadLen == 0 || cryptPara->ctx == NULL) {
return NSTACKX_EFAILED;
}
@ -236,6 +239,52 @@ uint8_t QueryCipherSupportByName(char *name)
LOGI(TAG, "devices no support %s", name);
return NSTACKX_FALSE;
}
#ifdef NSTACKX_WITH_LINUX_STANDARD
#define AES_HWCAP (1UL << 3)
#define AES_HWCAP2 (1UL << 0)
static uint8_t CheckAesCapability(void)
{
uint8_t ret = NSTACKX_FALSE;
LOGI(TAG, "CheckAesCapability enter");
unsigned long hwcaps = getauxval(AT_HWCAP);
unsigned long hwcaps2 = getauxval(AT_HWCAP2);
if ((hwcaps & AES_HWCAP) || (hwcaps2 & AES_HWCAP2)) {
ret = NSTACKX_TRUE;
}
return ret;
}
#endif
/* check CPU supports AES-NI hardware optimize */
uint8_t IsSupportHardwareAesNi(void)
{
#if defined(_WIN32) || defined(_WIN64)
#if defined(__GNUC__) || defined(_MSC_VER)
#if defined(__i386__) || defined(_x86_64__)
uint32_t eax = 0;
uint32_t ebx = 0;
uint32_t ecx = 0;
uint32_t edx = 0;
__get_cpuid(1, &eax, &ebx, &ecx, &edx);
return (ecx & bit_AES) > 0;
#else
return NSTACKX_FALSE;
#endif // defined(__i386__) || defined(_x86_64__)
#else
return NSTACKX_FALSE;
#endif defined(__GNUC__) || defined(_MSC_VER)
#else // linux
#ifdef NSTACKX_WITH_LINUX_STANDARD
return CheckAesCapability();
#else
return NSTACKX_FALSE;
#endif
#endif // defined(_WIN32) || defined(_WIN64)
}
#else
int32_t GetRandBytes(uint8_t *buf, uint32_t len)
@ -291,5 +340,11 @@ uint8_t QueryCipherSupportByName(char *name)
LOGI(TAG, "devices no support %s", name);
return NSTACKX_FALSE;
}
/* check CPU supports AES-NI hardware optimize */
uint8_t IsSupportHardwareAesNi(void)
{
LOGI(TAG, "no support AES-NI");
return NSTACKX_FALSE;
}
#endif // SSL_AND_CRYPTO_INCLUDED

View File

@ -39,4 +39,10 @@
#define BYTE_BITS_NUM 8
#define TYPE_BITS_NUM(_type) (sizeof(_type) * BYTE_BITS_NUM)
typedef enum {
CIPHER_AES_GCM = 0,
CIPHER_CHACHA,
CIPHER_AES_NI, // hardware optimize
} DFileCipherType;
#endif // NSTACKX_COMMON_HEADER_H

View File

@ -28,11 +28,6 @@
extern "C" {
#endif
typedef enum {
CIPHER_AES_GCM = 0,
CIPHER_CHACHA,
} DFileCipherType;
#define AES_128_KEY_LENGTH 16
#define AES_192_KEY_LENGTH 24
#define AES_256_KEY_LENGTH 32
@ -53,7 +48,7 @@ typedef struct {
uint32_t ivLen;
uint8_t aad[GCM_MAX_AAD_LENGTH];
uint32_t aadLen;
int cipherType;
uint8_t cipherType;
MBEDTLS_CTX *ctx;
} CryptPara;
@ -66,6 +61,7 @@ NSTACKX_EXPORT uint8_t IsCryptoIncluded(void);
NSTACKX_EXPORT uint8_t QueryCipherSupportByName(char *name);
NSTACKX_EXPORT MBEDTLS_CTX ClearCryptCtx(MBEDTLS_CTX *ctx);
NSTACKX_EXPORT MBEDTLS_CTX *CreateCryptCtx(void);
NSTACKX_EXPORT uint8_t IsSupportHardwareAesNi(void);
#endif

View File

@ -29,11 +29,6 @@
extern "C" {
#endif
typedef enum {
CIPHER_AES_GCM = 0,
CIPHER_CHACHA,
} DFileCipherType;
#define AES_128_KEY_LENGTH 16
#define AES_192_KEY_LENGTH 24
#define AES_256_KEY_LENGTH 32
@ -60,7 +55,7 @@ typedef struct {
uint8_t aad[GCM_MAX_AAD_LENGTH];
uint32_t aadLen;
EVP_CIPHER_CTX *ctx;
int cipherType;
uint8_t cipherType;
} CryptPara;
typedef struct {
@ -79,6 +74,7 @@ NSTACKX_EXPORT uint32_t AesGcmDecrypt(uint8_t *inBuff, uint32_t inLen, CryptPara
NSTACKX_EXPORT int32_t GetRandBytes(uint8_t *buf, uint32_t len);
NSTACKX_EXPORT uint8_t IsCryptoIncluded(void);
NSTACKX_EXPORT uint8_t QueryCipherSupportByName(char *name);
NSTACKX_EXPORT uint8_t IsSupportHardwareAesNi(void);
#ifdef __cplusplus
}