From c3ed7c7a779fe6fec16c2ac52a23f36b329c2dae Mon Sep 17 00:00:00 2001 From: Virtual Maker <56232456+virtual-maker@users.noreply.github.com> Date: Tue, 31 Dec 2024 23:38:51 +0100 Subject: [PATCH] Refactor functions to use const pointers Refactored multiple functions across various files to use `const` pointers for parameters, enhancing code safety and readability. Made minor formatting changes for consistency and improved readability, particularly in `sha204_library.cpp`. --- .../AirQualitySensor/AirQualitySensor.ino | 2 +- .../SecurityPersonalizer/sha204_library.cpp | 345 +++++++++--------- hal/crypto/generic/drivers/AES/AES.cpp | 4 +- 3 files changed, 176 insertions(+), 175 deletions(-) diff --git a/examples/AirQualitySensor/AirQualitySensor.ino b/examples/AirQualitySensor/AirQualitySensor.ino index 120eebc82..6caac2fca 100644 --- a/examples/AirQualitySensor/AirQualitySensor.ino +++ b/examples/AirQualitySensor/AirQualitySensor.ino @@ -214,7 +214,7 @@ Remarks: By using the slope and a point of the line. The x(logarithmic value of logarithmic coordinate, power of 10 is used to convert the result to non-logarithmic value. ************************************************************************************/ -int MQGetPercentage(float rs_ro_ratio, float *pcurve) +int MQGetPercentage(float rs_ro_ratio, const float *pcurve) { return (pow(10,( ((log(rs_ro_ratio)-pcurve[1])/pcurve[2]) + pcurve[0]))); } diff --git a/examples/SecurityPersonalizer/sha204_library.cpp b/examples/SecurityPersonalizer/sha204_library.cpp index 0d44cc1b6..0fefecec7 100644 --- a/examples/SecurityPersonalizer/sha204_library.cpp +++ b/examples/SecurityPersonalizer/sha204_library.cpp @@ -59,7 +59,8 @@ uint8_t atsha204Class::getSerialNumber(uint8_t * response) /* Calculates CRC16 value of provided data (and optionally including provided existing CRC16 data) returns the calculated CRC16 value */ -uint16_t atsha204Class::calculateAndUpdateCrc(uint8_t length, uint8_t *data, uint16_t current_crc) +uint16_t atsha204Class::calculateAndUpdateCrc(uint8_t length, const uint8_t *data, + uint16_t current_crc) { uint8_t counter; uint16_t crc_register = current_crc; @@ -96,7 +97,7 @@ void atsha204Class::swi_set_signal_pin(uint8_t is_high) } -uint8_t atsha204Class::swi_send_bytes(uint8_t count, uint8_t *buffer) +uint8_t atsha204Class::swi_send_bytes(uint8_t count, const uint8_t *buffer) { uint8_t i, bit_mask; @@ -322,7 +323,7 @@ uint8_t atsha204Class::sha204c_wakeup(uint8_t *response) ret_code = SHA204_COMM_FAIL; } else { if ((response[SHA204_RSP_SIZE_MIN - SHA204_CRC_SIZE] != 0x33) - || (response[SHA204_RSP_SIZE_MIN + 1 - SHA204_CRC_SIZE] != 0x43)) { + || (response[SHA204_RSP_SIZE_MIN + 1 - SHA204_CRC_SIZE] != 0x43)) { ret_code = SHA204_BAD_CRC; } } @@ -355,7 +356,7 @@ uint8_t atsha204Class::sha204c_resync(uint8_t size, uint8_t *response) } uint8_t atsha204Class::sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, - uint8_t *rx_buffer, uint8_t execution_delay, uint8_t execution_timeout) + uint8_t *rx_buffer, uint8_t execution_delay, uint8_t execution_timeout) { uint8_t ret_code = SHA204_FUNC_FAIL; uint8_t ret_code_resync; @@ -512,7 +513,7 @@ uint8_t atsha204Class::sha204m_random(uint8_t * tx_buffer, uint8_t * rx_buffer, tx_buffer[RANDOM_MODE_IDX] = mode & RANDOM_SEED_UPDATE; tx_buffer[RANDOM_PARAM2_IDX] = - tx_buffer[RANDOM_PARAM2_IDX + 1] = 0; + tx_buffer[RANDOM_PARAM2_IDX + 1] = 0; return sha204c_send_and_receive(&tx_buffer[0], RANDOM_RSP_SIZE, &rx_buffer[0], RANDOM_DELAY, RANDOM_EXEC_MAX - RANDOM_DELAY); @@ -529,8 +530,8 @@ uint8_t atsha204Class::sha204m_dev_rev(uint8_t *tx_buffer, uint8_t *rx_buffer) // Parameters are 0. tx_buffer[DEVREV_PARAM1_IDX] = - tx_buffer[DEVREV_PARAM2_IDX] = - tx_buffer[DEVREV_PARAM2_IDX + 1] = 0; + tx_buffer[DEVREV_PARAM2_IDX] = + tx_buffer[DEVREV_PARAM2_IDX + 1] = 0; return sha204c_send_and_receive(&tx_buffer[0], DEVREV_RSP_SIZE, &rx_buffer[0], DEVREV_DELAY, DEVREV_EXEC_MAX - DEVREV_DELAY); @@ -542,7 +543,7 @@ uint8_t atsha204Class::sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint uint8_t rx_size; if (!tx_buffer || !rx_buffer || ((zone & ~READ_ZONE_MASK) != 0) - || ((zone & READ_ZONE_MODE_32_BYTES) && (zone == SHA204_ZONE_OTP))) { + || ((zone & READ_ZONE_MODE_32_BYTES) && (zone == SHA204_ZONE_OTP))) { return SHA204_BAD_PARAM; } @@ -591,90 +592,90 @@ uint8_t atsha204Class::sha204m_execute(uint8_t op_code, uint8_t param1, uint16_t // Supply delays and response size. switch (op_code) { - case SHA204_CHECKMAC: - poll_delay = CHECKMAC_DELAY; - poll_timeout = CHECKMAC_EXEC_MAX - CHECKMAC_DELAY; - response_size = CHECKMAC_RSP_SIZE; - break; - - case SHA204_DERIVE_KEY: - poll_delay = DERIVE_KEY_DELAY; - poll_timeout = DERIVE_KEY_EXEC_MAX - DERIVE_KEY_DELAY; - response_size = DERIVE_KEY_RSP_SIZE; - break; - - case SHA204_DEVREV: - poll_delay = DEVREV_DELAY; - poll_timeout = DEVREV_EXEC_MAX - DEVREV_DELAY; - response_size = DEVREV_RSP_SIZE; - break; - - case SHA204_GENDIG: - poll_delay = GENDIG_DELAY; - poll_timeout = GENDIG_EXEC_MAX - GENDIG_DELAY; - response_size = GENDIG_RSP_SIZE; - break; - - case SHA204_HMAC: - poll_delay = HMAC_DELAY; - poll_timeout = HMAC_EXEC_MAX - HMAC_DELAY; - response_size = HMAC_RSP_SIZE; - break; - - case SHA204_LOCK: - poll_delay = LOCK_DELAY; - poll_timeout = LOCK_EXEC_MAX - LOCK_DELAY; - response_size = LOCK_RSP_SIZE; - break; - - case SHA204_MAC: - poll_delay = MAC_DELAY; - poll_timeout = MAC_EXEC_MAX - MAC_DELAY; - response_size = MAC_RSP_SIZE; - break; - - case SHA204_NONCE: - poll_delay = NONCE_DELAY; - poll_timeout = NONCE_EXEC_MAX - NONCE_DELAY; - response_size = param1 == NONCE_MODE_PASSTHROUGH - ? NONCE_RSP_SIZE_SHORT : NONCE_RSP_SIZE_LONG; - break; - - case SHA204_PAUSE: - poll_delay = PAUSE_DELAY; - poll_timeout = PAUSE_EXEC_MAX - PAUSE_DELAY; - response_size = PAUSE_RSP_SIZE; - break; - - case SHA204_RANDOM: - poll_delay = RANDOM_DELAY; - poll_timeout = RANDOM_EXEC_MAX - RANDOM_DELAY; - response_size = RANDOM_RSP_SIZE; - break; - - case SHA204_READ: - poll_delay = READ_DELAY; - poll_timeout = READ_EXEC_MAX - READ_DELAY; - response_size = (param1 & SHA204_ZONE_COUNT_FLAG) - ? READ_32_RSP_SIZE : READ_4_RSP_SIZE; - break; - - case SHA204_UPDATE_EXTRA: - poll_delay = UPDATE_DELAY; - poll_timeout = UPDATE_EXEC_MAX - UPDATE_DELAY; - response_size = UPDATE_RSP_SIZE; - break; - - case SHA204_WRITE: - poll_delay = WRITE_DELAY; - poll_timeout = WRITE_EXEC_MAX - WRITE_DELAY; - response_size = WRITE_RSP_SIZE; - break; - - default: - poll_delay = 0; - poll_timeout = SHA204_COMMAND_EXEC_MAX; - response_size = rx_size; + case SHA204_CHECKMAC: + poll_delay = CHECKMAC_DELAY; + poll_timeout = CHECKMAC_EXEC_MAX - CHECKMAC_DELAY; + response_size = CHECKMAC_RSP_SIZE; + break; + + case SHA204_DERIVE_KEY: + poll_delay = DERIVE_KEY_DELAY; + poll_timeout = DERIVE_KEY_EXEC_MAX - DERIVE_KEY_DELAY; + response_size = DERIVE_KEY_RSP_SIZE; + break; + + case SHA204_DEVREV: + poll_delay = DEVREV_DELAY; + poll_timeout = DEVREV_EXEC_MAX - DEVREV_DELAY; + response_size = DEVREV_RSP_SIZE; + break; + + case SHA204_GENDIG: + poll_delay = GENDIG_DELAY; + poll_timeout = GENDIG_EXEC_MAX - GENDIG_DELAY; + response_size = GENDIG_RSP_SIZE; + break; + + case SHA204_HMAC: + poll_delay = HMAC_DELAY; + poll_timeout = HMAC_EXEC_MAX - HMAC_DELAY; + response_size = HMAC_RSP_SIZE; + break; + + case SHA204_LOCK: + poll_delay = LOCK_DELAY; + poll_timeout = LOCK_EXEC_MAX - LOCK_DELAY; + response_size = LOCK_RSP_SIZE; + break; + + case SHA204_MAC: + poll_delay = MAC_DELAY; + poll_timeout = MAC_EXEC_MAX - MAC_DELAY; + response_size = MAC_RSP_SIZE; + break; + + case SHA204_NONCE: + poll_delay = NONCE_DELAY; + poll_timeout = NONCE_EXEC_MAX - NONCE_DELAY; + response_size = param1 == NONCE_MODE_PASSTHROUGH + ? NONCE_RSP_SIZE_SHORT : NONCE_RSP_SIZE_LONG; + break; + + case SHA204_PAUSE: + poll_delay = PAUSE_DELAY; + poll_timeout = PAUSE_EXEC_MAX - PAUSE_DELAY; + response_size = PAUSE_RSP_SIZE; + break; + + case SHA204_RANDOM: + poll_delay = RANDOM_DELAY; + poll_timeout = RANDOM_EXEC_MAX - RANDOM_DELAY; + response_size = RANDOM_RSP_SIZE; + break; + + case SHA204_READ: + poll_delay = READ_DELAY; + poll_timeout = READ_EXEC_MAX - READ_DELAY; + response_size = (param1 & SHA204_ZONE_COUNT_FLAG) + ? READ_32_RSP_SIZE : READ_4_RSP_SIZE; + break; + + case SHA204_UPDATE_EXTRA: + poll_delay = UPDATE_DELAY; + poll_timeout = UPDATE_EXEC_MAX - UPDATE_DELAY; + response_size = UPDATE_RSP_SIZE; + break; + + case SHA204_WRITE: + poll_delay = WRITE_DELAY; + poll_timeout = WRITE_EXEC_MAX - WRITE_DELAY; + response_size = WRITE_RSP_SIZE; + break; + + default: + poll_delay = 0; + poll_timeout = SHA204_COMMAND_EXEC_MAX; + response_size = rx_size; } // Assemble command. @@ -707,9 +708,9 @@ uint8_t atsha204Class::sha204m_execute(uint8_t op_code, uint8_t param1, uint16_t } uint8_t atsha204Class::sha204m_check_parameters(uint8_t op_code, uint8_t param1, uint16_t param2, - uint8_t datalen1, uint8_t *data1, uint8_t datalen2, uint8_t *data2, uint8_t datalen3, - uint8_t *data3, - uint8_t tx_size, uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer) + uint8_t datalen1, const uint8_t *data1, uint8_t datalen2, const uint8_t *data2, uint8_t datalen3, + const uint8_t *data3, uint8_t tx_size, const uint8_t *tx_buffer, uint8_t rx_size, + const uint8_t *rx_buffer) { #ifdef SHA204_CHECK_PARAMETERS @@ -724,97 +725,97 @@ uint8_t atsha204Class::sha204m_check_parameters(uint8_t op_code, uint8_t param1, // Check parameters depending on op-code. switch (op_code) { - case SHA204_CHECKMAC: - if ( - // no null pointers allowed - !data1 || !data2 - // No reserved bits should be set. - || (param1 | CHECKMAC_MODE_MASK) != CHECKMAC_MODE_MASK - // key_id > 15 not allowed - || param2 > SHA204_KEY_ID_MAX - ) { - return SHA204_BAD_PARAM; - } - break; + case SHA204_CHECKMAC: + if ( + // no null pointers allowed + !data1 || !data2 + // No reserved bits should be set. + || (param1 | CHECKMAC_MODE_MASK) != CHECKMAC_MODE_MASK + // key_id > 15 not allowed + || param2 > SHA204_KEY_ID_MAX + ) { + return SHA204_BAD_PARAM; + } + break; - case SHA204_DERIVE_KEY: - if (param2 > SHA204_KEY_ID_MAX) { - return SHA204_BAD_PARAM; - } - break; + case SHA204_DERIVE_KEY: + if (param2 > SHA204_KEY_ID_MAX) { + return SHA204_BAD_PARAM; + } + break; - case SHA204_DEVREV: - break; + case SHA204_DEVREV: + break; - case SHA204_GENDIG: - if ((param1 != GENDIG_ZONE_OTP) && (param1 != GENDIG_ZONE_DATA)) { - return SHA204_BAD_PARAM; - } - break; + case SHA204_GENDIG: + if ((param1 != GENDIG_ZONE_OTP) && (param1 != GENDIG_ZONE_DATA)) { + return SHA204_BAD_PARAM; + } + break; - case SHA204_HMAC: - if ((param1 & ~HMAC_MODE_MASK) != 0) { - return SHA204_BAD_PARAM; - } - break; + case SHA204_HMAC: + if ((param1 & ~HMAC_MODE_MASK) != 0) { + return SHA204_BAD_PARAM; + } + break; - case SHA204_LOCK: - if (((param1 & ~LOCK_ZONE_MASK) != 0) - || ((param1 & LOCK_ZONE_NO_CRC) && (param2 != 0))) { - return SHA204_BAD_PARAM; - } - break; + case SHA204_LOCK: + if (((param1 & ~LOCK_ZONE_MASK) != 0) + || ((param1 & LOCK_ZONE_NO_CRC) && (param2 != 0))) { + return SHA204_BAD_PARAM; + } + break; - case SHA204_MAC: - if (((param1 & ~MAC_MODE_MASK) != 0) - || (((param1 & MAC_MODE_BLOCK2_TEMPKEY) == 0) && !data1)) { - return SHA204_BAD_PARAM; - } - break; + case SHA204_MAC: + if (((param1 & ~MAC_MODE_MASK) != 0) + || (((param1 & MAC_MODE_BLOCK2_TEMPKEY) == 0) && !data1)) { + return SHA204_BAD_PARAM; + } + break; - case SHA204_NONCE: - if ( !data1 - || (param1 > NONCE_MODE_PASSTHROUGH) - || (param1 == NONCE_MODE_INVALID) - ) { - return SHA204_BAD_PARAM; - } - break; + case SHA204_NONCE: + if ( !data1 + || (param1 > NONCE_MODE_PASSTHROUGH) + || (param1 == NONCE_MODE_INVALID) + ) { + return SHA204_BAD_PARAM; + } + break; - case SHA204_PAUSE: - break; + case SHA204_PAUSE: + break; - case SHA204_RANDOM: - if (param1 > RANDOM_NO_SEED_UPDATE) { - return SHA204_BAD_PARAM; - } - break; + case SHA204_RANDOM: + if (param1 > RANDOM_NO_SEED_UPDATE) { + return SHA204_BAD_PARAM; + } + break; - case SHA204_READ: - if (((param1 & ~READ_ZONE_MASK) != 0) - || ((param1 & READ_ZONE_MODE_32_BYTES) && (param1 == SHA204_ZONE_OTP))) { - return SHA204_BAD_PARAM; - } - break; + case SHA204_READ: + if (((param1 & ~READ_ZONE_MASK) != 0) + || ((param1 & READ_ZONE_MODE_32_BYTES) && (param1 == SHA204_ZONE_OTP))) { + return SHA204_BAD_PARAM; + } + break; - case SHA204_TEMPSENSE: - break; + case SHA204_TEMPSENSE: + break; - case SHA204_UPDATE_EXTRA: - if (param1 > UPDATE_CONFIG_BYTE_86) { - return SHA204_BAD_PARAM; - } - break; + case SHA204_UPDATE_EXTRA: + if (param1 > UPDATE_CONFIG_BYTE_86) { + return SHA204_BAD_PARAM; + } + break; - case SHA204_WRITE: - if (!data1 || ((param1 & ~WRITE_ZONE_MASK) != 0)) { - return SHA204_BAD_PARAM; - } - break; + case SHA204_WRITE: + if (!data1 || ((param1 & ~WRITE_ZONE_MASK) != 0)) { + return SHA204_BAD_PARAM; + } + break; - default: - // unknown op-code - return SHA204_BAD_PARAM; + default: + // unknown op-code + return SHA204_BAD_PARAM; } return SHA204_SUCCESS; diff --git a/hal/crypto/generic/drivers/AES/AES.cpp b/hal/crypto/generic/drivers/AES/AES.cpp index 5f35bb159..d541e31d9 100644 --- a/hal/crypto/generic/drivers/AES/AES.cpp +++ b/hal/crypto/generic/drivers/AES/AES.cpp @@ -201,7 +201,7 @@ static void inv_shift_sub_rows (byte st[N_BLOCK]) /* SUB COLUMNS PHASE */ -static void mix_sub_columns (byte dt[N_BLOCK], byte st[N_BLOCK]) +static void mix_sub_columns (byte dt[N_BLOCK], const byte st[N_BLOCK]) { byte j = 5 ; byte k = 10 ; @@ -223,7 +223,7 @@ static void mix_sub_columns (byte dt[N_BLOCK], byte st[N_BLOCK]) } } -static void inv_mix_sub_columns (byte dt[N_BLOCK], byte st[N_BLOCK]) +static void inv_mix_sub_columns (byte dt[N_BLOCK], const byte st[N_BLOCK]) { for (byte i = 0 ; i < N_BLOCK ; i += N_COL) { byte a1 = st [i] ;