Skip to content

Commit 1b54969

Browse files
committed
Fix integer overflows
1 parent 4a6a917 commit 1b54969

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
lines changed

modules/GPS_module/src/GPS_module_1.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "crypto_module_types.h"
77
#include "crypto_module_1.h"
88

9+
#include "key_management_module_1.h"
10+
911
GPS_position current_destination;
1012

1113

@@ -38,12 +40,12 @@ enum GPS_return_status get_current_position(GPS_position * position) {
3840
if (GPS_driver_obtain_current_position(position_as_bytes, hmac_as_bytes) == 0) {
3941
if (crypto_verify_hmac(position_as_bytes, 16, hmac_as_bytes) == valid_hmac) {
4042
GPS_position pos = {
41-
position_as_bytes[0] << 1 + position_as_bytes[1],
42-
position_as_bytes[2] << 1 + position_as_bytes[3],
43-
position_as_bytes[4] << 1 + position_as_bytes[5],
44-
position_as_bytes[6] << 1 + position_as_bytes[7],
45-
position_as_bytes[8] << 1 + position_as_bytes[9],
46-
position_as_bytes[10] << 1 + position_as_bytes[11]
43+
(uint8_t)(position_as_bytes[0] << 1) + position_as_bytes[1],
44+
(uint8_t)(position_as_bytes[2] << 1) + position_as_bytes[3],
45+
(uint8_t)(position_as_bytes[4] << 1) + position_as_bytes[5],
46+
(uint8_t)(position_as_bytes[6] << 1) + position_as_bytes[7],
47+
(uint8_t)(position_as_bytes[8] << 1) + position_as_bytes[9],
48+
(uint8_t)(position_as_bytes[10] << 1) + position_as_bytes[11]
4749
};
4850
*position = pos;
4951
return GPS_success;

modules/crypto_module/src/crypto_module_1.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <string.h>
2+
#include <stdlib.h>
23

34
#include "crypto_module_1.h"
45
#include "crypto_module_2.h"
@@ -37,6 +38,9 @@ enum crypto_return_status crypto_set_key(crypto_key key) {
3738

3839
enum crypto_return_status crypto_set_nonce(crypto_nonce nonce) {
3940
if(crypto_verify_nonce(&nonce) == valid_nonce_provided) {
41+
if (current_nonce == 0) {
42+
crypto_init();
43+
}
4044
for (int i = 0; i < NONCE_LENGTH; i++) {
4145
current_nonce->nonce[i] = nonce.nonce[i];
4246
}

modules/crypto_module/src/crypto_module_1.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
#include "crypto_module_types.h"
77

8-
extern uint8_t third_party_library_calc_hmac(uint8_t * const message, int len, char * const key, char * const nonce, uint8_t * hmac);
8+
// extern uint8_t third_party_library_calc_hmac(uint8_t * const message, int len, char * const key, char * const nonce, uint8_t * hmac);
9+
extern uint8_t third_party_library_calc_hmac(const uint8_t * message, int len, const char * key, const char * nonce, uint8_t * hmac);
910

1011
void crypto_init();
1112

modules/crypto_module/src/crypto_module_2.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33

44
#include "time_module_1.h"
55

6+
#include <limits.h>
7+
68

79
enum crypto_return_status crypto_verify_nonce(crypto_nonce * nonce) {
810
for (int i = 0; i < NONCE_LENGTH; i++ ) {
911
if (nonce->nonce[i] != 0) {
10-
if (nonce->time_of_creation > time_current_time() - 300) {
12+
int ct = current_time();
13+
if (ct > INT_MIN + 300 && nonce->time_of_creation > ct - 300) {
1114
return valid_nonce_provided;
1215
}
1316
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "time_module_1.h"
22

3-
int time_current_time() {
3+
int current_time() {
44
return driver_get_current_time();
55
}

0 commit comments

Comments
 (0)