Skip to content

Commit 7d9126b

Browse files
committed
treewide: make type casts explicit
1 parent 9320251 commit 7d9126b

9 files changed

Lines changed: 50 additions & 50 deletions

File tree

ccm.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ block0(size_t M, /* number of auth bytes */
4747
unsigned char *result) {
4848
unsigned int i;
4949

50-
result[0] = CCM_FLAGS(la, M, L);
50+
result[0] = (unsigned char) CCM_FLAGS(la, M, L);
5151

5252
/* copy the nonce */
5353
memcpy(result + 1, nonce, DTLS_CCM_BLOCKSIZE - L - 1);
@@ -88,11 +88,11 @@ add_auth_data(rijndael_ctx *ctx, const unsigned char *msg, uint64_t la,
8888
#ifndef WITH_CONTIKI
8989
if (la < 0xFF00) { /* 2^16 - 2^8 */
9090
j = 2;
91-
dtls_int_to_uint16(B, la);
91+
dtls_int_to_uint16(B, (uint16_t) la);
9292
} else if (la <= UINT32_MAX) {
9393
j = 6;
9494
dtls_int_to_uint16(B, 0xFFFE);
95-
dtls_int_to_uint32(B+2, la);
95+
dtls_int_to_uint32(B+2, (uint32_t) la);
9696
} else {
9797
j = 10;
9898
dtls_int_to_uint16(B, 0xFFFF);
@@ -184,7 +184,7 @@ dtls_ccm_encrypt_message(rijndael_ctx *ctx, size_t M, size_t L,
184184
add_auth_data(ctx, aad, la, B, X);
185185

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

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

228-
return len + M;
228+
return (long int) (len + M);
229229
}
230230

231231
long int
@@ -254,7 +254,7 @@ dtls_ccm_decrypt_message(rijndael_ctx *ctx, size_t M, size_t L,
254254
add_auth_data(ctx, aad, la, B, X);
255255

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

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

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

300+
return (long int) (len - M);
301301
error:
302302
return -1;
303303
}

crypto.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ dtls_mac(dtls_hmac_context_t *hmac_ctx,
281281
const unsigned char *packet, size_t length,
282282
unsigned char *buf) {
283283
uint16 L;
284-
dtls_int_to_uint16(L, length);
284+
dtls_int_to_uint16(L, (uint16_t) length);
285285

286286
assert(hmac_ctx);
287287
dtls_hmac_update(hmac_ctx, record +3, sizeof(uint16) + sizeof(uint48));
@@ -340,7 +340,7 @@ dtls_psk_pre_master_secret(unsigned char *key, size_t keylen,
340340
return -1;
341341
}
342342

343-
dtls_int_to_uint16(p, keylen);
343+
dtls_int_to_uint16(p, (uint16_t) keylen);
344344
p += sizeof(uint16);
345345

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

352352
memcpy(p, key, keylen);
353353

354-
return 2 * (sizeof(uint16) + keylen);
354+
return (int) (2 * (sizeof(uint16) + keylen));
355355
}
356356
#endif /* DTLS_PSK */
357357

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

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

373-
for (i = (key_size / sizeof(uint32_t)) - 1; i >= 0 ; i--) {
373+
for (i = (int) ((key_size / sizeof(uint32_t)) - 1); i >= 0 ; i--) {
374374
dtls_int_to_uint32(result, key[i]);
375375
result += 4;
376376
}
@@ -428,8 +428,8 @@ int dtls_ec_key_asn1_from_uint32(const uint32_t *key, size_t key_size,
428428
key_size++;
429429
}
430430
/* Update the length of positive ASN.1 integer */
431-
dtls_int_to_uint8(lptr, key_size);
432-
return key_size + 2;
431+
dtls_int_to_uint8(lptr, (uint8_t) key_size);
432+
return (int) (key_size + 2);
433433
}
434434

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

457457
dtls_ec_key_from_uint32(result_x, key_size, result);
458-
return key_size;
458+
return (int) key_size;
459459
}
460460

461461
void
@@ -568,7 +568,7 @@ dtls_encrypt_params(const dtls_ccm_params_t *params,
568568
ctx->data.tag_length = params->tag_length;
569569
ctx->data.l = params->l;
570570

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

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

582582
error:
583583
dtls_cipher_context_release();
@@ -610,7 +610,7 @@ dtls_decrypt_params(const dtls_ccm_params_t *params,
610610
ctx->data.tag_length = params->tag_length;
611611
ctx->data.l = params->l;
612612

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

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

624624
error:
625625
dtls_cipher_context_release();

dtls.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ static int verify_ext_eliptic_curves(uint8 *data, size_t data_length) {
869869
return dtls_alert_fatal_create(DTLS_ALERT_HANDSHAKE_FAILURE);
870870
}
871871

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

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

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

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

14261426
memcpy(p, data_array[i], data_len_array[i]);
14271427
p += data_len_array[i];
1428-
res += data_len_array[i];
1428+
res += (int) data_len_array[i];
14291429
}
14301430
} else { /* TLS_PSK_WITH_AES_128_CCM_8 or TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */
14311431
/**
@@ -1500,7 +1500,7 @@ dtls_prepare_record(dtls_peer_t *peer, dtls_security_parameters_t *security,
15001500

15011501
memcpy(p, data_array[i], data_len_array[i]);
15021502
p += data_len_array[i];
1503-
res += data_len_array[i];
1503+
res += (int) data_len_array[i];
15041504
}
15051505

15061506
memset(nonce, 0, DTLS_CCM_BLOCKSIZE);
@@ -1624,10 +1624,10 @@ dtls_0_send_hello_verify_request(dtls_context_t *ctx,
16241624
dtls_int_to_uint16(buf + 1, DTLS10_VERSION);
16251625

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

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

16321632
memcpy(p, data, data_length);
16331633

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

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

16601660
if (add_hash) {
16611661
update_hs_hash(peer, buf, sizeof(buf));
@@ -2060,7 +2060,7 @@ dtls_asn1_integer_to_ec_key(uint8 *data, size_t data_len, uint8 *key,
20602060
/* drop leading 0s if needed */
20612061
memcpy(key, data + length - key_len, key_len);
20622062
}
2063-
return length + 2;
2063+
return (int) length + 2;
20642064
}
20652065

20662066
static int
@@ -2129,7 +2129,7 @@ dtls_check_ecdsa_signature_elem(uint8 *data, size_t data_length,
21292129
data += ret;
21302130
data_length -= ret;
21312131

2132-
return data - data_orig;
2132+
return (int) (data - data_orig);
21332133
}
21342134

21352135
static int
@@ -2450,7 +2450,7 @@ dtls_send_server_key_exchange_psk(dtls_context_t *ctx, dtls_peer_t *peer,
24502450
return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR);
24512451
}
24522452

2453-
dtls_int_to_uint16(p, len);
2453+
dtls_int_to_uint16(p, (uint16_t) len);
24542454
p += sizeof(uint16);
24552455

24562456
memcpy(p, psk_hint, len);
@@ -2762,7 +2762,7 @@ dtls_send_finished(dtls_context_t *ctx, dtls_peer_t *peer,
27622762

27632763
copy_hs_hash(peer, &hs_hash);
27642764

2765-
length = dtls_hash_finalize(hash, &hs_hash);
2765+
length = (int) dtls_hash_finalize(hash, &hs_hash);
27662766

27672767
dtls_prf(peer->handshake_params->tmp.master_secret,
27682768
DTLS_MASTER_SECRET_LENGTH,
@@ -2823,7 +2823,7 @@ dtls_send_client_hello(dtls_context_t *ctx, dtls_peer_t *peer,
28232823
p += sizeof(uint8);
28242824

28252825
/* cookie */
2826-
dtls_int_to_uint8(p, cookie_length);
2826+
dtls_int_to_uint8(p, (uint8_t) cookie_length);
28272827
p += sizeof(uint8);
28282828
if (cookie_length != 0) {
28292829
memcpy(p, cookie, cookie_length);
@@ -3411,7 +3411,7 @@ decrypt_verify(dtls_peer_t *peer, uint8 *packet, size_t length,
34113411
int clen;
34123412

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

34163416
if (!security) {
34173417
dtls_alert("No security context for epoch: %i\n", dtls_get_epoch(header));

dtls_debug.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ void dump(unsigned char *buf, size_t len) {
315315
void dtls_dsrv_log_addr(log_t level, const char *name, const session_t *addr)
316316
{
317317
char addrbuf[73];
318-
int len;
318+
size_t len;
319319

320320
len = dsrv_print_addr(addr, addrbuf, sizeof(addrbuf));
321321
if (!len)

dtls_time.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ void dtls_ticks(dtls_tick_t *t) {
9696
#elif defined(_MSC_VER)
9797
SYSTEMTIME current_time;
9898
GetSystemTime(&current_time);
99-
*t = (current_time.wSecond - dtls_clock_offset) * DTLS_TICKS_PER_SECOND
100-
+ (current_time.wMilliseconds * DTLS_TICKS_PER_SECOND / 1000);
99+
*t = (dtls_tick_t) ((current_time.wSecond - dtls_clock_offset) * DTLS_TICKS_PER_SECOND
100+
+ (current_time.wMilliseconds * DTLS_TICKS_PER_SECOND / 1000));
101101
#else
102102
#error "clock not implemented"
103103
#endif

ecc/ecc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static uint32_t add( const uint32_t *x, const uint32_t *y, uint32_t *result, uin
4848
//printf("%02x + %02x + %01x = ", x[v], y[v], d);
4949
d += (uint64_t) x[v] + (uint64_t) y[v];
5050
//printf("%02x\n", d);
51-
result[v] = d;
51+
result[v] = (uint32_t) d;
5252
d = d>>32; //save carry
5353
}
5454

hmac.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ dtls_hmac_finalize(dtls_hmac_context_t *ctx, unsigned char *result) {
7777

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

80-
return len;
80+
return (int) len;
8181
}
8282

8383
#ifdef HMAC_TEST

platform-specific/dtls_prng_win.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ dtls_prng(unsigned char *buf, size_t len) {
4141
}
4242
*buf++ = number & 0xFF;
4343
}
44-
return klen;
44+
return (int) klen;
4545
}
4646

4747
void

sha2/sha2.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,14 @@ static inline sha2_word64 get64be(const sha2_byte* data)
227227
static inline void put64be(sha2_byte* data, sha2_word64 val)
228228
{
229229
#if BYTE_ORDER == LITTLE_ENDIAN
230-
data[7] = val; val >>= 8;
231-
data[6] = val; val >>= 8;
232-
data[5] = val; val >>= 8;
233-
data[4] = val; val >>= 8;
234-
data[3] = val; val >>= 8;
235-
data[2] = val; val >>= 8;
236-
data[1] = val; val >>= 8;
237-
data[0] = val;
230+
data[7] = (sha2_byte) val; val >>= 8;
231+
data[6] = (sha2_byte) val; val >>= 8;
232+
data[5] = (sha2_byte) val; val >>= 8;
233+
data[4] = (sha2_byte) val; val >>= 8;
234+
data[3] = (sha2_byte) val; val >>= 8;
235+
data[2] = (sha2_byte) val; val >>= 8;
236+
data[1] = (sha2_byte) val; val >>= 8;
237+
data[0] = (sha2_byte) val;
238238
#else /* BYTE_ORDER != LITTLE_ENDIAN */
239239
MEMCPY_BCOPY(data, &val, sizeof(val));
240240
#endif /* BYTE_ORDER != LITTLE_ENDIAN */

0 commit comments

Comments
 (0)