Skip to content

Commit 8f54f80

Browse files
authored
Merge pull request #665 from pabuhler/const-cast
start using const on internal arguments
2 parents 2d65fe7 + b96e52c commit 8f54f80

File tree

7 files changed

+38
-32
lines changed

7 files changed

+38
-32
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ endif()
120120

121121
if(BUILD_WITH_WARNINGS)
122122
if(CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID MATCHES "GNU")
123-
set(WARNINGS -Wall -pedantic -Wextra -Werror)
123+
set(WARNINGS -Wall -pedantic -Wextra -Wcast-qual -Werror)
124124
elseif(MSVC)
125125
set(WARNINGS /W4 /WX)
126126
endif()

crypto/cipher/aes_gcm_nss.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ static srtp_err_status_t srtp_aes_gcm_nss_context_init(void *cv,
212212
return (srtp_err_status_cipher_fail);
213213
}
214214

215-
SECItem key_item = { siBuffer, (unsigned char *)key, c->key_size };
215+
/* explicitly cast away const of key */
216+
SECItem key_item = { siBuffer, (unsigned char *)(uintptr_t)key,
217+
c->key_size };
216218
c->key = PK11_ImportSymKey(slot, CKM_AES_GCM, PK11_OriginUnwrap,
217219
CKA_ENCRYPT, &key_item, NULL);
218220
PK11_FreeSlot(slot);

crypto/cipher/aes_icm_nss.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ static srtp_err_status_t srtp_aes_icm_nss_context_init(void *cv,
254254
return srtp_err_status_bad_param;
255255
}
256256

257-
SECItem keyItem = { siBuffer, (unsigned char *)key, c->key_size };
257+
/* explicitly cast away const of key */
258+
SECItem keyItem = { siBuffer, (unsigned char *)(uintptr_t)key,
259+
c->key_size };
258260
c->key = PK11_ImportSymKey(slot, CKM_AES_CTR, PK11_OriginUnwrap,
259261
CKA_ENCRYPT, &keyItem, NULL);
260262
PK11_FreeSlot(slot);

crypto/hash/hmac_nss.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ static srtp_err_status_t srtp_hmac_init(void *statev,
187187
return srtp_err_status_bad_param;
188188
}
189189

190-
SECItem key_item = { siBuffer, (unsigned char *)key, key_len };
190+
/* explicitly cast away const of key */
191+
SECItem key_item = { siBuffer, (unsigned char *)(uintptr_t)key, key_len };
191192
sym_key = PK11_ImportSymKey(slot, CKM_SHA_1_HMAC, PK11_OriginUnwrap,
192193
CKA_SIGN, &key_item, NULL);
193194
PK11_FreeSlot(slot);

crypto/include/datatypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void v128_left_shift(v128_t *x, int shift_index);
163163
* verifying authentication tags.
164164
*/
165165

166-
int srtp_octet_string_is_eq(uint8_t *a, uint8_t *b, int len);
166+
int srtp_octet_string_is_eq(const uint8_t *a, const uint8_t *b, int len);
167167

168168
/*
169169
* A portable way to zero out memory as recommended by

crypto/math/datatypes.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,14 +397,14 @@ void bitvector_left_shift(bitvector_t *x, int shift)
397397

398398
#endif /* defined(__SSSE3__) */
399399

400-
int srtp_octet_string_is_eq(uint8_t *a, uint8_t *b, int len)
400+
int srtp_octet_string_is_eq(const uint8_t *a, const uint8_t *b, int len)
401401
{
402402
/*
403403
* We use this somewhat obscure implementation to try to ensure the running
404404
* time only depends on len, even accounting for compiler optimizations.
405405
* The accumulator ends up zero iff the strings are equal.
406406
*/
407-
uint8_t *end = b + len;
407+
const uint8_t *end = b + len;
408408
uint32_t accumulator = 0;
409409

410410
#if defined(__SSE2__)

srtp/srtp.c

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,32 @@ srtp_debug_module_t mod_srtp = {
7979
#define uint32s_in_rtcp_header 2
8080
#define octets_in_rtp_extn_hdr 4
8181

82-
static srtp_err_status_t srtp_validate_rtp_header(void *rtp_hdr,
83-
int *pkt_octet_len)
82+
static srtp_err_status_t srtp_validate_rtp_header(const void *rtp_hdr,
83+
int pkt_octet_len)
8484
{
85-
srtp_hdr_t *hdr = (srtp_hdr_t *)rtp_hdr;
85+
const srtp_hdr_t *hdr = (const srtp_hdr_t *)rtp_hdr;
8686
int rtp_header_len;
8787

88-
if (*pkt_octet_len < octets_in_rtp_header)
88+
if (pkt_octet_len < octets_in_rtp_header)
8989
return srtp_err_status_bad_param;
9090

9191
/* Check RTP header length */
9292
rtp_header_len = octets_in_rtp_header + 4 * hdr->cc;
9393
if (hdr->x == 1)
9494
rtp_header_len += octets_in_rtp_extn_hdr;
9595

96-
if (*pkt_octet_len < rtp_header_len)
96+
if (pkt_octet_len < rtp_header_len)
9797
return srtp_err_status_bad_param;
9898

9999
/* Verifing profile length. */
100100
if (hdr->x == 1) {
101-
srtp_hdr_xtnd_t *xtn_hdr =
102-
(srtp_hdr_xtnd_t *)((uint32_t *)hdr + uint32s_in_rtp_header +
103-
hdr->cc);
101+
const srtp_hdr_xtnd_t *xtn_hdr =
102+
(const srtp_hdr_xtnd_t *)((const uint32_t *)hdr +
103+
uint32s_in_rtp_header + hdr->cc);
104104
int profile_len = ntohs(xtn_hdr->length);
105105
rtp_header_len += profile_len * 4;
106106
/* profile length counts the number of 32-bit words */
107-
if (*pkt_octet_len < rtp_header_len)
107+
if (pkt_octet_len < rtp_header_len)
108108
return srtp_err_status_bad_param;
109109
}
110110
return srtp_err_status_ok;
@@ -1577,7 +1577,7 @@ static srtp_err_status_t srtp_process_header_encryption(
15771577
static void srtp_calc_aead_iv(srtp_session_keys_t *session_keys,
15781578
v128_t *iv,
15791579
srtp_xtd_seq_num_t *seq,
1580-
srtp_hdr_t *hdr)
1580+
const srtp_hdr_t *hdr)
15811581
{
15821582
v128_t in;
15831583
v128_t salt;
@@ -1616,11 +1616,11 @@ static void srtp_calc_aead_iv(srtp_session_keys_t *session_keys,
16161616
}
16171617

16181618
srtp_session_keys_t *srtp_get_session_keys(srtp_stream_ctx_t *stream,
1619-
uint8_t *hdr,
1620-
const unsigned int *pkt_octet_len,
1619+
const uint8_t *hdr,
1620+
unsigned int pkt_octet_len,
16211621
unsigned int *mki_size)
16221622
{
1623-
unsigned int base_mki_start_location = *pkt_octet_len;
1623+
unsigned int base_mki_start_location = pkt_octet_len;
16241624
unsigned int mki_start_location = 0;
16251625
unsigned int tag_len = 0;
16261626
unsigned int i = 0;
@@ -1731,7 +1731,7 @@ static srtp_err_status_t srtp_estimate_index(srtp_rdbx_t *rdbx,
17311731
return srtp_err_status_ok;
17321732
}
17331733

1734-
static srtp_err_status_t srtp_get_est_pkt_index(srtp_hdr_t *hdr,
1734+
static srtp_err_status_t srtp_get_est_pkt_index(const srtp_hdr_t *hdr,
17351735
srtp_stream_ctx_t *stream,
17361736
srtp_xtd_seq_num_t *est,
17371737
int *delta)
@@ -2169,7 +2169,7 @@ srtp_err_status_t srtp_protect_mki(srtp_ctx_t *ctx,
21692169
/* we assume the hdr is 32-bit aligned to start */
21702170

21712171
/* Verify RTP header */
2172-
status = srtp_validate_rtp_header(rtp_hdr, pkt_octet_len);
2172+
status = srtp_validate_rtp_header(rtp_hdr, *pkt_octet_len);
21732173
if (status)
21742174
return status;
21752175

@@ -2499,7 +2499,7 @@ srtp_err_status_t srtp_unprotect_mki(srtp_ctx_t *ctx,
24992499
/* we assume the hdr is 32-bit aligned to start */
25002500

25012501
/* Verify RTP header */
2502-
status = srtp_validate_rtp_header(srtp_hdr, pkt_octet_len);
2502+
status = srtp_validate_rtp_header(srtp_hdr, *pkt_octet_len);
25032503
if (status)
25042504
return status;
25052505

@@ -2568,9 +2568,9 @@ srtp_err_status_t srtp_unprotect_mki(srtp_ctx_t *ctx,
25682568

25692569
/* Determine if MKI is being used and what session keys should be used */
25702570
if (use_mki) {
2571-
session_keys = srtp_get_session_keys(
2572-
stream, (uint8_t *)hdr, (const unsigned int *)pkt_octet_len,
2573-
&mki_size);
2571+
session_keys =
2572+
srtp_get_session_keys(stream, (const uint8_t *)hdr,
2573+
(unsigned int)*pkt_octet_len, &mki_size);
25742574

25752575
if (session_keys == NULL)
25762576
return srtp_err_status_bad_mki;
@@ -3574,7 +3574,7 @@ static srtp_err_status_t srtp_calc_aead_iv_srtcp(
35743574
srtp_session_keys_t *session_keys,
35753575
v128_t *iv,
35763576
uint32_t seq_num,
3577-
srtcp_hdr_t *hdr)
3577+
const srtcp_hdr_t *hdr)
35783578
{
35793579
v128_t in;
35803580
v128_t salt;
@@ -3873,8 +3873,9 @@ static srtp_err_status_t srtp_unprotect_rtcp_aead(
38733873
* If payload encryption is enabled, then the AAD consist of
38743874
* the RTCP header and the seq# at the end of the packet
38753875
*/
3876-
status = srtp_cipher_set_aad(session_keys->rtcp_cipher, (uint8_t *)hdr,
3877-
octets_in_rtcp_header);
3876+
status =
3877+
srtp_cipher_set_aad(session_keys->rtcp_cipher, (const uint8_t *)hdr,
3878+
octets_in_rtcp_header);
38783879
if (status) {
38793880
return (srtp_err_status_cipher_fail);
38803881
}
@@ -4282,9 +4283,9 @@ srtp_err_status_t srtp_unprotect_rtcp_mki(srtp_t ctx,
42824283
* Determine if MKI is being used and what session keys should be used
42834284
*/
42844285
if (use_mki) {
4285-
session_keys = srtp_get_session_keys(
4286-
stream, (uint8_t *)hdr, (const unsigned int *)pkt_octet_len,
4287-
&mki_size);
4286+
session_keys =
4287+
srtp_get_session_keys(stream, (const uint8_t *)hdr,
4288+
(unsigned int)*pkt_octet_len, &mki_size);
42884289

42894290
if (session_keys == NULL)
42904291
return srtp_err_status_bad_mki;

0 commit comments

Comments
 (0)