Skip to content

Commit e23497d

Browse files
[core] Fixed static variable usage in the crypto module (#3047).
Static variable `hcrypt_MsgInfo _hcMsg_SRT_MsgInfo` was declared twice. There was a mutual dependency between `_hcMsg_SRT_MsgInfo` and `hcryptMsg_SRT_ParseMsg(..)`.
1 parent 822bc1e commit e23497d

File tree

5 files changed

+24
-26
lines changed

5 files changed

+24
-26
lines changed

haicrypt/hcrypt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ typedef struct hcrypt_Session_str {
7777
size_t inbuf_siz;
7878

7979
int se; /* Stream Encapsulation (HCRYPT_SE_xxx) */
80-
hcrypt_MsgInfo * msg_info;
80+
const hcrypt_MsgInfo * msg_info;
8181

8282
struct {
8383
size_t data_max_len;

haicrypt/hcrypt_ctx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ typedef struct tag_hcrypt_Ctx {
8484
size_t sek_len;
8585
unsigned char sek[HAICRYPT_KEY_MAX_SZ];
8686

87-
hcrypt_MsgInfo * msg_info; /* Transport message handler */
87+
const hcrypt_MsgInfo * msg_info; /* Transport message handler */
8888
unsigned pkt_cnt; /* Key usage counter */
8989

9090
#define HCRYPT_CTX_MAX_KM_PFX_SZ 16

haicrypt/hcrypt_msg.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ typedef struct {
5757
void (*setPki)(unsigned char *msg, hcrypt_Pki);
5858
void (*resetCache)(unsigned char *pfx_cache, unsigned pkt_type, unsigned flags);
5959
void (*indexMsg)(unsigned char *msg, unsigned char *pfx_cache);
60-
int (*parseMsg)(unsigned char *msg);
6160
}hcrypt_MsgInfo;
6261

6362

@@ -72,6 +71,7 @@ typedef struct {
7271

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

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

7676
/*
7777
* HaiCrypt KMmsg (Keying Material):
@@ -128,9 +128,8 @@ typedef struct {
128128
#define HCRYPT_AUTH_AES_GCM 1
129129

130130
#define HCRYPT_SE_TSUDP 1
131-
hcrypt_MsgInfo * hcryptMsg_STA_MsgInfo(void);
132131
#define HCRYPT_SE_TSSRT 2
133-
hcrypt_MsgInfo * hcryptMsg_SRT_MsgInfo(void);
132+
const hcrypt_MsgInfo * hcryptMsg_SRT_MsgInfo(void);
134133

135134
#define hcryptMsg_KM_GetVersion(msg) (((msg)[HCRYPT_MSG_KM_OFS_VERSION]>>4)& 0xF)
136135
#define hcryptMsg_KM_GetPktType(msg) (((msg)[HCRYPT_MSG_KM_OFS_PT]) & 0xF)

haicrypt/hcrypt_rx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ int HaiCrypt_Rx_Process(HaiCrypt_Handle hhc,
7878
}
7979

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

haicrypt/hcrypt_xpt_srt.c

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ written by
6565
#define HCRYPT_MSG_SRT_OFS_MSGNO 4
6666
#define HCRYPT_MSG_SRT_SHF_KFLGS 27 //shift
6767

68-
static hcrypt_MsgInfo _hcMsg_SRT_MsgInfo;
6968

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

113-
static int hcryptMsg_SRT_ParseMsg(unsigned char *msg)
112+
static const hcrypt_MsgInfo _hcMsg_SRT_MsgInfo = {
113+
.hdr_len = HCRYPT_MSG_SRT_HDR_SZ,
114+
.pfx_len = HCRYPT_MSG_SRT_PFX_SZ,
115+
.getKeyFlags = hcryptMsg_SRT_GetKeyFlags,
116+
.getPki = hcryptMsg_SRT_GetPki,
117+
.setPki = hcryptMsg_SRT_SetPki,
118+
.resetCache = hcryptMsg_SRT_ResetCache,
119+
.indexMsg = hcryptMsg_SRT_IndexMsg
120+
};
121+
122+
const hcrypt_MsgInfo* hcryptMsg_SRT_MsgInfo(void)
123+
{
124+
return (&_hcMsg_SRT_MsgInfo);
125+
}
126+
127+
int hcryptMsg_SRT_ParseMsg(const hcrypt_MsgInfo* mi, unsigned char *msg)
114128
{
115129
int rc;
116130

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

127141
switch(rc) {
128142
case HCRYPT_MSG_PT_MS:
129-
if (hcryptMsg_HasNoSek(&_hcMsg_SRT_MsgInfo, msg)
130-
|| hcryptMsg_HasBothSek(&_hcMsg_SRT_MsgInfo, msg)) {
143+
if (hcryptMsg_HasNoSek(mi, msg)
144+
|| hcryptMsg_HasBothSek(mi, msg)) {
131145
HCRYPT_LOG(LOG_ERR, "invalid MS msg flgs: %02x\n",
132-
hcryptMsg_GetKeyIndex(&_hcMsg_SRT_MsgInfo, msg));
146+
hcryptMsg_GetKeyIndex(mi, msg));
133147
return(-1);
134148
}
135149
break;
@@ -153,19 +167,4 @@ static int hcryptMsg_SRT_ParseMsg(unsigned char *msg)
153167
return(rc); /* -1: error, 0: unknown: >0: PT */
154168
}
155169

156-
static hcrypt_MsgInfo _hcMsg_SRT_MsgInfo;
157-
158-
hcrypt_MsgInfo *hcryptMsg_SRT_MsgInfo(void)
159-
{
160-
_hcMsg_SRT_MsgInfo.hdr_len = HCRYPT_MSG_SRT_HDR_SZ;
161-
_hcMsg_SRT_MsgInfo.pfx_len = HCRYPT_MSG_SRT_PFX_SZ;
162-
_hcMsg_SRT_MsgInfo.getKeyFlags = hcryptMsg_SRT_GetKeyFlags;
163-
_hcMsg_SRT_MsgInfo.getPki = hcryptMsg_SRT_GetPki;
164-
_hcMsg_SRT_MsgInfo.setPki = hcryptMsg_SRT_SetPki;
165-
_hcMsg_SRT_MsgInfo.resetCache = hcryptMsg_SRT_ResetCache;
166-
_hcMsg_SRT_MsgInfo.indexMsg = hcryptMsg_SRT_IndexMsg;
167-
_hcMsg_SRT_MsgInfo.parseMsg = hcryptMsg_SRT_ParseMsg;
168-
169-
return(&_hcMsg_SRT_MsgInfo);
170-
}
171170

0 commit comments

Comments
 (0)