Skip to content

Commit

Permalink
[core] Fixed static variable usage in the crypto module (#3047).
Browse files Browse the repository at this point in the history
Static variable `hcrypt_MsgInfo _hcMsg_SRT_MsgInfo` was declared twice.
There was a mutual dependency between `_hcMsg_SRT_MsgInfo` and `hcryptMsg_SRT_ParseMsg(..)`.
  • Loading branch information
maxsharabayko authored Oct 10, 2024
1 parent 822bc1e commit e23497d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion haicrypt/hcrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ typedef struct hcrypt_Session_str {
size_t inbuf_siz;

int se; /* Stream Encapsulation (HCRYPT_SE_xxx) */
hcrypt_MsgInfo * msg_info;
const hcrypt_MsgInfo * msg_info;

struct {
size_t data_max_len;
Expand Down
2 changes: 1 addition & 1 deletion haicrypt/hcrypt_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ typedef struct tag_hcrypt_Ctx {
size_t sek_len;
unsigned char sek[HAICRYPT_KEY_MAX_SZ];

hcrypt_MsgInfo * msg_info; /* Transport message handler */
const hcrypt_MsgInfo * msg_info; /* Transport message handler */
unsigned pkt_cnt; /* Key usage counter */

#define HCRYPT_CTX_MAX_KM_PFX_SZ 16
Expand Down
5 changes: 2 additions & 3 deletions haicrypt/hcrypt_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ typedef struct {
void (*setPki)(unsigned char *msg, hcrypt_Pki);
void (*resetCache)(unsigned char *pfx_cache, unsigned pkt_type, unsigned flags);
void (*indexMsg)(unsigned char *msg, unsigned char *pfx_cache);
int (*parseMsg)(unsigned char *msg);
}hcrypt_MsgInfo;


Expand All @@ -72,6 +71,7 @@ typedef struct {

#define hcryptMsg_PaddedLen(len, fact) ((((len)+(fact)-1)/(fact))*(fact))

int hcryptMsg_SRT_ParseMsg(const hcrypt_MsgInfo* mi, unsigned char* msg);

/*
* HaiCrypt KMmsg (Keying Material):
Expand Down Expand Up @@ -128,9 +128,8 @@ typedef struct {
#define HCRYPT_AUTH_AES_GCM 1

#define HCRYPT_SE_TSUDP 1
hcrypt_MsgInfo * hcryptMsg_STA_MsgInfo(void);
#define HCRYPT_SE_TSSRT 2
hcrypt_MsgInfo * hcryptMsg_SRT_MsgInfo(void);
const hcrypt_MsgInfo * hcryptMsg_SRT_MsgInfo(void);

#define hcryptMsg_KM_GetVersion(msg) (((msg)[HCRYPT_MSG_KM_OFS_VERSION]>>4)& 0xF)
#define hcryptMsg_KM_GetPktType(msg) (((msg)[HCRYPT_MSG_KM_OFS_PT]) & 0xF)
Expand Down
2 changes: 1 addition & 1 deletion haicrypt/hcrypt_rx.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int HaiCrypt_Rx_Process(HaiCrypt_Handle hhc,
}

/* Validate HaiCrypt message */
if (0 > (msg_type = crypto->msg_info->parseMsg(in_msg))) {
if (0 > (msg_type = hcryptMsg_SRT_ParseMsg(crypto->msg_info, in_msg))) {
return(-1);
}

Expand Down
39 changes: 19 additions & 20 deletions haicrypt/hcrypt_xpt_srt.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ written by
#define HCRYPT_MSG_SRT_OFS_MSGNO 4
#define HCRYPT_MSG_SRT_SHF_KFLGS 27 //shift

static hcrypt_MsgInfo _hcMsg_SRT_MsgInfo;

static unsigned hcryptMsg_SRT_GetKeyFlags(unsigned char *msg)
{
Expand Down Expand Up @@ -110,7 +109,22 @@ static void hcryptMsg_SRT_IndexMsg(unsigned char *msg, unsigned char *pfx_cache)
return; //nothing to do, header and index maintained by SRT
}

static int hcryptMsg_SRT_ParseMsg(unsigned char *msg)
static const hcrypt_MsgInfo _hcMsg_SRT_MsgInfo = {
.hdr_len = HCRYPT_MSG_SRT_HDR_SZ,
.pfx_len = HCRYPT_MSG_SRT_PFX_SZ,
.getKeyFlags = hcryptMsg_SRT_GetKeyFlags,
.getPki = hcryptMsg_SRT_GetPki,
.setPki = hcryptMsg_SRT_SetPki,
.resetCache = hcryptMsg_SRT_ResetCache,
.indexMsg = hcryptMsg_SRT_IndexMsg
};

const hcrypt_MsgInfo* hcryptMsg_SRT_MsgInfo(void)
{
return (&_hcMsg_SRT_MsgInfo);
}

int hcryptMsg_SRT_ParseMsg(const hcrypt_MsgInfo* mi, unsigned char *msg)
{
int rc;

Expand All @@ -126,10 +140,10 @@ static int hcryptMsg_SRT_ParseMsg(unsigned char *msg)

switch(rc) {
case HCRYPT_MSG_PT_MS:
if (hcryptMsg_HasNoSek(&_hcMsg_SRT_MsgInfo, msg)
|| hcryptMsg_HasBothSek(&_hcMsg_SRT_MsgInfo, msg)) {
if (hcryptMsg_HasNoSek(mi, msg)
|| hcryptMsg_HasBothSek(mi, msg)) {
HCRYPT_LOG(LOG_ERR, "invalid MS msg flgs: %02x\n",
hcryptMsg_GetKeyIndex(&_hcMsg_SRT_MsgInfo, msg));
hcryptMsg_GetKeyIndex(mi, msg));
return(-1);
}
break;
Expand All @@ -153,19 +167,4 @@ static int hcryptMsg_SRT_ParseMsg(unsigned char *msg)
return(rc); /* -1: error, 0: unknown: >0: PT */
}

static hcrypt_MsgInfo _hcMsg_SRT_MsgInfo;

hcrypt_MsgInfo *hcryptMsg_SRT_MsgInfo(void)
{
_hcMsg_SRT_MsgInfo.hdr_len = HCRYPT_MSG_SRT_HDR_SZ;
_hcMsg_SRT_MsgInfo.pfx_len = HCRYPT_MSG_SRT_PFX_SZ;
_hcMsg_SRT_MsgInfo.getKeyFlags = hcryptMsg_SRT_GetKeyFlags;
_hcMsg_SRT_MsgInfo.getPki = hcryptMsg_SRT_GetPki;
_hcMsg_SRT_MsgInfo.setPki = hcryptMsg_SRT_SetPki;
_hcMsg_SRT_MsgInfo.resetCache = hcryptMsg_SRT_ResetCache;
_hcMsg_SRT_MsgInfo.indexMsg = hcryptMsg_SRT_IndexMsg;
_hcMsg_SRT_MsgInfo.parseMsg = hcryptMsg_SRT_ParseMsg;

return(&_hcMsg_SRT_MsgInfo);
}

0 comments on commit e23497d

Please sign in to comment.