Skip to content

Commit

Permalink
Change loops to use local size_t variables
Browse files Browse the repository at this point in the history
  • Loading branch information
paulej committed Jan 12, 2024
1 parent fcc2517 commit cf39a69
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 21 deletions.
13 changes: 6 additions & 7 deletions crypto/cipher/aes_icm.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ static srtp_err_status_t srtp_aes_icm_encrypt(void *cv,
{
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
unsigned int bytes_to_encr = (unsigned int)*enc_len;
unsigned int i;
uint32_t *b;

/* check that there's enough segment left*/
Expand All @@ -313,7 +312,7 @@ static srtp_err_status_t srtp_aes_icm_encrypt(void *cv,
debug_print(srtp_mod_aes_icm, "block index: %d", htons(c->counter.v16[7]));
if (bytes_to_encr <= (unsigned int)c->bytes_in_buffer) {
/* deal with odd case of small bytes_to_encr */
for (i = (sizeof(v128_t) - c->bytes_in_buffer);
for (size_t i = (sizeof(v128_t) - c->bytes_in_buffer);
i < (sizeof(v128_t) - c->bytes_in_buffer + bytes_to_encr); i++) {
*buf++ ^= c->keystream_buffer.v8[i];
}
Expand All @@ -325,8 +324,8 @@ static srtp_err_status_t srtp_aes_icm_encrypt(void *cv,

} else {
/* encrypt bytes until the remaining data is 16-byte aligned */
for (i = (sizeof(v128_t) - c->bytes_in_buffer); i < sizeof(v128_t);
i++) {
for (size_t i = (sizeof(v128_t) - c->bytes_in_buffer);
i < sizeof(v128_t); i++) {
*buf++ ^= c->keystream_buffer.v8[i];
}

Expand All @@ -335,7 +334,7 @@ static srtp_err_status_t srtp_aes_icm_encrypt(void *cv,
}

/* now loop over entire 16-byte blocks of keystream */
for (i = 0; i < (bytes_to_encr / sizeof(v128_t)); i++) {
for (size_t i = 0; i < (bytes_to_encr / sizeof(v128_t)); i++) {
/* fill buffer with new keystream */
srtp_aes_icm_advance(c);

Expand Down Expand Up @@ -385,12 +384,12 @@ static srtp_err_status_t srtp_aes_icm_encrypt(void *cv,
/* fill buffer with new keystream */
srtp_aes_icm_advance(c);

for (i = 0; i < (bytes_to_encr & 0xf); i++) {
for (size_t i = 0; i < (bytes_to_encr & 0xf); i++) {
*buf++ ^= c->keystream_buffer.v8[i];
}

/* reset the keystream buffer size to right value */
c->bytes_in_buffer = sizeof(v128_t) - i;
c->bytes_in_buffer = sizeof(v128_t) - (bytes_to_encr & 0xf);
} else {
/* no tail, so just reset the keystream buffer size to zero */
c->bytes_in_buffer = 0;
Expand Down
5 changes: 2 additions & 3 deletions crypto/hash/hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ static srtp_err_status_t srtp_hmac_init(void *statev,
size_t key_len)
{
srtp_hmac_ctx_t *state = (srtp_hmac_ctx_t *)statev;
size_t i;
uint8_t ipad[64];

/*
Expand All @@ -132,12 +131,12 @@ static srtp_err_status_t srtp_hmac_init(void *statev,
* set values of ipad and opad by exoring the key into the
* appropriate constant values
*/
for (i = 0; i < key_len; i++) {
for (size_t i = 0; i < key_len; i++) {
ipad[i] = key[i] ^ 0x36;
state->opad[i] = key[i] ^ 0x5c;
}
/* set the rest of ipad, opad to constant values */
for (; i < 64; i++) {
for (size_t i = key_len; i < 64; i++) {
ipad[i] = 0x36;
((uint8_t *)state->opad)[i] = 0x5c;
}
Expand Down
2 changes: 1 addition & 1 deletion include/srtp_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ typedef struct srtp_session_keys_t {
typedef struct srtp_stream_ctx_t_ {
uint32_t ssrc;
srtp_session_keys_t *session_keys;
unsigned int num_master_keys;
size_t num_master_keys;
srtp_rdbx_t rtp_rdbx;
srtp_sec_serv_t rtp_services;
srtp_rdb_t rtcp_rdb;
Expand Down
16 changes: 6 additions & 10 deletions srtp/srtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ static srtp_err_status_t srtp_stream_dealloc(
const srtp_stream_ctx_t *stream_template)
{
srtp_err_status_t status;
unsigned int i = 0;
srtp_session_keys_t *session_keys = NULL;
srtp_session_keys_t *template_session_keys = NULL;

Expand All @@ -187,7 +186,7 @@ static srtp_err_status_t srtp_stream_dealloc(
* anything else
*/
if (stream->session_keys) {
for (i = 0; i < stream->num_master_keys; i++) {
for (size_t i = 0; i < stream->num_master_keys; i++) {
session_keys = &stream->session_keys[i];

if (stream_template &&
Expand Down Expand Up @@ -377,7 +376,7 @@ static srtp_err_status_t srtp_stream_alloc(srtp_stream_ctx_t **str_ptr,
{
srtp_stream_ctx_t *str;
srtp_err_status_t stat;
unsigned int i = 0;
size_t i = 0;
srtp_session_keys_t *session_keys = NULL;

stat = srtp_valid_policy(p);
Expand Down Expand Up @@ -544,7 +543,6 @@ static srtp_err_status_t srtp_stream_clone(
{
srtp_err_status_t status;
srtp_stream_ctx_t *str;
unsigned int i = 0;
srtp_session_keys_t *session_keys = NULL;
const srtp_session_keys_t *template_session_keys = NULL;

Expand All @@ -566,7 +564,7 @@ static srtp_err_status_t srtp_stream_clone(
return srtp_err_status_alloc_fail;
}

for (i = 0; i < stream_template->num_master_keys; i++) {
for (size_t i = 0; i < stream_template->num_master_keys; i++) {
session_keys = &str->session_keys[i];
template_session_keys = &stream_template->session_keys[i];

Expand Down Expand Up @@ -920,7 +918,6 @@ srtp_err_status_t srtp_stream_init_all_master_keys(
srtp_master_key_t **keys,
const unsigned int max_master_keys)
{
unsigned int i = 0;
srtp_err_status_t status = srtp_err_status_ok;
srtp_master_key_t single_master_key;

Expand All @@ -933,8 +930,8 @@ srtp_err_status_t srtp_stream_init_all_master_keys(
} else {
srtp->num_master_keys = max_master_keys;

for (i = 0; i < srtp->num_master_keys && i < SRTP_MAX_NUM_MASTER_KEYS;
i++) {
for (size_t i = 0;
i < srtp->num_master_keys && i < SRTP_MAX_NUM_MASTER_KEYS; i++) {
status = srtp_stream_init_keys(srtp, keys[i], i);

if (status) {
Expand Down Expand Up @@ -1637,7 +1634,6 @@ srtp_session_keys_t *srtp_get_session_keys(srtp_stream_ctx_t *stream,
size_t base_mki_start_location = pkt_octet_len;
size_t mki_start_location = 0;
size_t tag_len = 0;
unsigned int i = 0;

// Determine the authentication tag size
if (stream->session_keys[0].rtp_cipher->algorithm == SRTP_AES_GCM_128 ||
Expand All @@ -1654,7 +1650,7 @@ srtp_session_keys_t *srtp_get_session_keys(srtp_stream_ctx_t *stream,

base_mki_start_location -= tag_len;

for (i = 0; i < stream->num_master_keys; i++) {
for (size_t i = 0; i < stream->num_master_keys; i++) {
if (stream->session_keys[i].mki_size != 0 &&
stream->session_keys[i].mki_size <= base_mki_start_location) {
*mki_size = stream->session_keys[i].mki_size;
Expand Down

0 comments on commit cf39a69

Please sign in to comment.