Skip to content

Commit

Permalink
treewide: make type casts explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Sep 28, 2022
1 parent 9320251 commit 7d9126b
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 50 deletions.
14 changes: 7 additions & 7 deletions ccm.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ block0(size_t M, /* number of auth bytes */
unsigned char *result) {
unsigned int i;

result[0] = CCM_FLAGS(la, M, L);
result[0] = (unsigned char) CCM_FLAGS(la, M, L);

/* copy the nonce */
memcpy(result + 1, nonce, DTLS_CCM_BLOCKSIZE - L - 1);
Expand Down Expand Up @@ -88,11 +88,11 @@ add_auth_data(rijndael_ctx *ctx, const unsigned char *msg, uint64_t la,
#ifndef WITH_CONTIKI
if (la < 0xFF00) { /* 2^16 - 2^8 */
j = 2;
dtls_int_to_uint16(B, la);
dtls_int_to_uint16(B, (uint16_t) la);
} else if (la <= UINT32_MAX) {
j = 6;
dtls_int_to_uint16(B, 0xFFFE);
dtls_int_to_uint32(B+2, la);
dtls_int_to_uint32(B+2, (uint32_t) la);
} else {
j = 10;
dtls_int_to_uint16(B, 0xFFFF);
Expand Down Expand Up @@ -184,7 +184,7 @@ dtls_ccm_encrypt_message(rijndael_ctx *ctx, size_t M, size_t L,
add_auth_data(ctx, aad, la, B, X);

/* initialize block template */
A[0] = L-1;
A[0] = (unsigned char) (L-1);

/* copy the nonce */
memcpy(A + 1, nonce, DTLS_CCM_BLOCKSIZE - L - 1);
Expand Down Expand Up @@ -225,7 +225,7 @@ dtls_ccm_encrypt_message(rijndael_ctx *ctx, size_t M, size_t L,
for (i = 0; i < M; ++i)
*msg++ = X[i] ^ S[i];

return len + M;
return (long int) (len + M);
}

long int
Expand Down Expand Up @@ -254,7 +254,7 @@ dtls_ccm_decrypt_message(rijndael_ctx *ctx, size_t M, size_t L,
add_auth_data(ctx, aad, la, B, X);

/* initialize block template */
A[0] = L-1;
A[0] = (unsigned char) (L-1);

/* copy the nonce */
memcpy(A + 1, nonce, DTLS_CCM_BLOCKSIZE - L - 1);
Expand Down Expand Up @@ -296,8 +296,8 @@ dtls_ccm_decrypt_message(rijndael_ctx *ctx, size_t M, size_t L,

/* return length if MAC is valid, otherwise continue with error handling */
if (equals(X, msg, M))
return len - M;

return (long int) (len - M);
error:
return -1;
}
24 changes: 12 additions & 12 deletions crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ dtls_mac(dtls_hmac_context_t *hmac_ctx,
const unsigned char *packet, size_t length,
unsigned char *buf) {
uint16 L;
dtls_int_to_uint16(L, length);
dtls_int_to_uint16(L, (uint16_t) length);

assert(hmac_ctx);
dtls_hmac_update(hmac_ctx, record +3, sizeof(uint16) + sizeof(uint48));
Expand Down Expand Up @@ -340,7 +340,7 @@ dtls_psk_pre_master_secret(unsigned char *key, size_t keylen,
return -1;
}

dtls_int_to_uint16(p, keylen);
dtls_int_to_uint16(p, (uint16_t) keylen);
p += sizeof(uint16);

memset(p, 0, keylen);
Expand All @@ -351,7 +351,7 @@ dtls_psk_pre_master_secret(unsigned char *key, size_t keylen,

memcpy(p, key, keylen);

return 2 * (sizeof(uint16) + keylen);
return (int) (2 * (sizeof(uint16) + keylen));
}
#endif /* DTLS_PSK */

Expand All @@ -360,7 +360,7 @@ static void dtls_ec_key_to_uint32(const unsigned char *key, size_t key_size,
uint32_t *result) {
int i;

for (i = (key_size / sizeof(uint32_t)) - 1; i >= 0 ; i--) {
for (i = (int) ((key_size / sizeof(uint32_t)) - 1); i >= 0 ; i--) {
*result = dtls_uint32_to_int(&key[i * sizeof(uint32_t)]);
result++;
}
Expand All @@ -370,7 +370,7 @@ static void dtls_ec_key_from_uint32(const uint32_t *key, size_t key_size,
unsigned char *result) {
int i;

for (i = (key_size / sizeof(uint32_t)) - 1; i >= 0 ; i--) {
for (i = (int) ((key_size / sizeof(uint32_t)) - 1); i >= 0 ; i--) {
dtls_int_to_uint32(result, key[i]);
result += 4;
}
Expand Down Expand Up @@ -428,8 +428,8 @@ int dtls_ec_key_asn1_from_uint32(const uint32_t *key, size_t key_size,
key_size++;
}
/* Update the length of positive ASN.1 integer */
dtls_int_to_uint8(lptr, key_size);
return key_size + 2;
dtls_int_to_uint8(lptr, (uint8_t) key_size);
return (int) (key_size + 2);
}

int dtls_ecdh_pre_master_secret(unsigned char *priv_key,
Expand All @@ -455,7 +455,7 @@ int dtls_ecdh_pre_master_secret(unsigned char *priv_key,
ecc_ecdh(pub_x, pub_y, priv, result_x, result_y);

dtls_ec_key_from_uint32(result_x, key_size, result);
return key_size;
return (int) key_size;
}

void
Expand Down Expand Up @@ -568,7 +568,7 @@ dtls_encrypt_params(const dtls_ccm_params_t *params,
ctx->data.tag_length = params->tag_length;
ctx->data.l = params->l;

ret = rijndael_set_key_enc_only(&ctx->data.ctx, key, 8 * keylen);
ret = rijndael_set_key_enc_only(&ctx->data.ctx, key, (int) (8 * keylen));
if (ret < 0) {
/* cleanup everything in case the key has the wrong size */
dtls_warn("cannot set rijndael key\n");
Expand All @@ -577,7 +577,7 @@ dtls_encrypt_params(const dtls_ccm_params_t *params,

if (src != buf)
memmove(buf, src, length);
ret = dtls_ccm_encrypt(&ctx->data, src, length, buf, params->nonce, aad, la);
ret = (int) dtls_ccm_encrypt(&ctx->data, src, length, buf, params->nonce, aad, la);

error:
dtls_cipher_context_release();
Expand Down Expand Up @@ -610,7 +610,7 @@ dtls_decrypt_params(const dtls_ccm_params_t *params,
ctx->data.tag_length = params->tag_length;
ctx->data.l = params->l;

ret = rijndael_set_key_enc_only(&ctx->data.ctx, key, 8 * keylen);
ret = rijndael_set_key_enc_only(&ctx->data.ctx, key, (int) (8 * keylen));
if (ret < 0) {
/* cleanup everything in case the key has the wrong size */
dtls_warn("cannot set rijndael key\n");
Expand All @@ -619,7 +619,7 @@ dtls_decrypt_params(const dtls_ccm_params_t *params,

if (src != buf)
memmove(buf, src, length);
ret = dtls_ccm_decrypt(&ctx->data, src, length, buf, params->nonce, aad, la);
ret = (int) dtls_ccm_decrypt(&ctx->data, src, length, buf, params->nonce, aad, la);

error:
dtls_cipher_context_release();
Expand Down
34 changes: 17 additions & 17 deletions dtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ static int verify_ext_eliptic_curves(uint8 *data, size_t data_length) {
return dtls_alert_fatal_create(DTLS_ALERT_HANDSHAKE_FAILURE);
}

for (i = data_length - sizeof(uint16); i > 0; i -= sizeof(uint16)) {
for (i = (int) data_length - sizeof(uint16); i > 0; i -= sizeof(uint16)) {
/* check if this curve is supported */
curve_name = dtls_uint16_to_int(data);
data += sizeof(uint16);
Expand All @@ -893,7 +893,7 @@ static int verify_ext_cert_type(uint8 *data, size_t data_length) {
return dtls_alert_fatal_create(DTLS_ALERT_HANDSHAKE_FAILURE);
}

for (i = data_length - sizeof(uint8); i > 0; i -= sizeof(uint8)) {
for (i = (int) data_length - sizeof(uint8); i > 0; i -= sizeof(uint8)) {
/* check if this cert type is supported */
cert_type = dtls_uint8_to_int(data);
data += sizeof(uint8);
Expand All @@ -917,7 +917,7 @@ static int verify_ext_ec_point_formats(uint8 *data, size_t data_length) {
return dtls_alert_fatal_create(DTLS_ALERT_HANDSHAKE_FAILURE);
}

for (i = data_length - sizeof(uint8); i > 0; i -= sizeof(uint8)) {
for (i = (int) data_length - sizeof(uint8); i > 0; i -= sizeof(uint8)) {
/* check if this ec_point_format is supported */
cert_type = dtls_uint8_to_int(data);
data += sizeof(uint8);
Expand All @@ -941,7 +941,7 @@ static int verify_ext_sig_hash_algo(uint8 *data, size_t data_length) {
return dtls_alert_fatal_create(DTLS_ALERT_HANDSHAKE_FAILURE);
}

for (i = data_length - sizeof(uint16); i > 0; i -= sizeof(uint16)) {
for (i = (int) data_length - sizeof(uint16); i > 0; i -= sizeof(uint16)) {
/* check if this _sig_hash_algo is supported */
hash_type = dtls_uint8_to_int(data);
data += sizeof(uint8);
Expand Down Expand Up @@ -1425,7 +1425,7 @@ dtls_prepare_record(dtls_peer_t *peer, dtls_security_parameters_t *security,

memcpy(p, data_array[i], data_len_array[i]);
p += data_len_array[i];
res += data_len_array[i];
res += (int) data_len_array[i];
}
} else { /* TLS_PSK_WITH_AES_128_CCM_8 or TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */
/**
Expand Down Expand Up @@ -1500,7 +1500,7 @@ dtls_prepare_record(dtls_peer_t *peer, dtls_security_parameters_t *security,

memcpy(p, data_array[i], data_len_array[i]);
p += data_len_array[i];
res += data_len_array[i];
res += (int) data_len_array[i];
}

memset(nonce, 0, DTLS_CCM_BLOCKSIZE);
Expand Down Expand Up @@ -1624,10 +1624,10 @@ dtls_0_send_hello_verify_request(dtls_context_t *ctx,
dtls_int_to_uint16(buf + 1, DTLS10_VERSION);

/* fix length of fragment in sendbuf */
dtls_int_to_uint16(buf + 11, DTLS_HS_LENGTH + data_length);
dtls_int_to_uint16(buf + 11, (uint16_t) (DTLS_HS_LENGTH + data_length));

p = dtls_set_handshake_header(DTLS_HT_HELLO_VERIFY_REQUEST, &(ephemeral_peer->mseq), data_length, 0,
data_length, p);
p = dtls_set_handshake_header(DTLS_HT_HELLO_VERIFY_REQUEST, &(ephemeral_peer->mseq), (int) data_length, 0,
(int) data_length, p);

memcpy(p, data, data_length);

Expand All @@ -1654,8 +1654,8 @@ dtls_send_handshake_msg_hash(dtls_context_t *ctx,
int i = 0;
dtls_security_parameters_t *security = dtls_security_params(peer);

dtls_set_handshake_header(header_type, &(peer->handshake_params->hs_state.mseq_s), data_length, 0,
data_length, buf);
dtls_set_handshake_header(header_type, &(peer->handshake_params->hs_state.mseq_s), (int) data_length, 0,
(int) data_length, buf);

if (add_hash) {
update_hs_hash(peer, buf, sizeof(buf));
Expand Down Expand Up @@ -2060,7 +2060,7 @@ dtls_asn1_integer_to_ec_key(uint8 *data, size_t data_len, uint8 *key,
/* drop leading 0s if needed */
memcpy(key, data + length - key_len, key_len);
}
return length + 2;
return (int) length + 2;
}

static int
Expand Down Expand Up @@ -2129,7 +2129,7 @@ dtls_check_ecdsa_signature_elem(uint8 *data, size_t data_length,
data += ret;
data_length -= ret;

return data - data_orig;
return (int) (data - data_orig);
}

static int
Expand Down Expand Up @@ -2450,7 +2450,7 @@ dtls_send_server_key_exchange_psk(dtls_context_t *ctx, dtls_peer_t *peer,
return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);
}

dtls_int_to_uint16(p, len);
dtls_int_to_uint16(p, (uint16_t) len);
p += sizeof(uint16);

memcpy(p, psk_hint, len);
Expand Down Expand Up @@ -2762,7 +2762,7 @@ dtls_send_finished(dtls_context_t *ctx, dtls_peer_t *peer,

copy_hs_hash(peer, &hs_hash);

length = dtls_hash_finalize(hash, &hs_hash);
length = (int) dtls_hash_finalize(hash, &hs_hash);

dtls_prf(peer->handshake_params->tmp.master_secret,
DTLS_MASTER_SECRET_LENGTH,
Expand Down Expand Up @@ -2823,7 +2823,7 @@ dtls_send_client_hello(dtls_context_t *ctx, dtls_peer_t *peer,
p += sizeof(uint8);

/* cookie */
dtls_int_to_uint8(p, cookie_length);
dtls_int_to_uint8(p, (uint8_t) cookie_length);
p += sizeof(uint8);
if (cookie_length != 0) {
memcpy(p, cookie, cookie_length);
Expand Down Expand Up @@ -3411,7 +3411,7 @@ decrypt_verify(dtls_peer_t *peer, uint8 *packet, size_t length,
int clen;

*cleartext = (uint8 *)packet + sizeof(dtls_record_header_t);
clen = length - sizeof(dtls_record_header_t);
clen = (int) (length - sizeof(dtls_record_header_t));

if (!security) {
dtls_alert("No security context for epoch: %i\n", dtls_get_epoch(header));
Expand Down
2 changes: 1 addition & 1 deletion dtls_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ void dump(unsigned char *buf, size_t len) {
void dtls_dsrv_log_addr(log_t level, const char *name, const session_t *addr)
{
char addrbuf[73];
int len;
size_t len;

len = dsrv_print_addr(addr, addrbuf, sizeof(addrbuf));
if (!len)
Expand Down
4 changes: 2 additions & 2 deletions dtls_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ void dtls_ticks(dtls_tick_t *t) {
#elif defined(_MSC_VER)
SYSTEMTIME current_time;
GetSystemTime(&current_time);
*t = (current_time.wSecond - dtls_clock_offset) * DTLS_TICKS_PER_SECOND
+ (current_time.wMilliseconds * DTLS_TICKS_PER_SECOND / 1000);
*t = (dtls_tick_t) ((current_time.wSecond - dtls_clock_offset) * DTLS_TICKS_PER_SECOND
+ (current_time.wMilliseconds * DTLS_TICKS_PER_SECOND / 1000));
#else
#error "clock not implemented"
#endif
Expand Down
2 changes: 1 addition & 1 deletion ecc/ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static uint32_t add( const uint32_t *x, const uint32_t *y, uint32_t *result, uin
//printf("%02x + %02x + %01x = ", x[v], y[v], d);
d += (uint64_t) x[v] + (uint64_t) y[v];
//printf("%02x\n", d);
result[v] = d;
result[v] = (uint32_t) d;
d = d>>32; //save carry
}

Expand Down
2 changes: 1 addition & 1 deletion hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ dtls_hmac_finalize(dtls_hmac_context_t *ctx, unsigned char *result) {

len = dtls_hash_finalize(result, &ctx->data);

return len;
return (int) len;
}

#ifdef HMAC_TEST
Expand Down
2 changes: 1 addition & 1 deletion platform-specific/dtls_prng_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dtls_prng(unsigned char *buf, size_t len) {
}
*buf++ = number & 0xFF;
}
return klen;
return (int) klen;
}

void
Expand Down
16 changes: 8 additions & 8 deletions sha2/sha2.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,14 @@ static inline sha2_word64 get64be(const sha2_byte* data)
static inline void put64be(sha2_byte* data, sha2_word64 val)
{
#if BYTE_ORDER == LITTLE_ENDIAN
data[7] = val; val >>= 8;
data[6] = val; val >>= 8;
data[5] = val; val >>= 8;
data[4] = val; val >>= 8;
data[3] = val; val >>= 8;
data[2] = val; val >>= 8;
data[1] = val; val >>= 8;
data[0] = val;
data[7] = (sha2_byte) val; val >>= 8;
data[6] = (sha2_byte) val; val >>= 8;
data[5] = (sha2_byte) val; val >>= 8;
data[4] = (sha2_byte) val; val >>= 8;
data[3] = (sha2_byte) val; val >>= 8;
data[2] = (sha2_byte) val; val >>= 8;
data[1] = (sha2_byte) val; val >>= 8;
data[0] = (sha2_byte) val;
#else /* BYTE_ORDER != LITTLE_ENDIAN */
MEMCPY_BCOPY(data, &val, sizeof(val));
#endif /* BYTE_ORDER != LITTLE_ENDIAN */
Expand Down

0 comments on commit 7d9126b

Please sign in to comment.