Skip to content

Commit 963a756

Browse files
committed
sha204 library extended with CRC calculator
Locking operations are recommended to include CRC and since sha204 library internally carries the proper algorithm for this, it has now been refactored to support both static and incremental calculations needed for both command marchalling and locking.
1 parent 1ac6f0a commit 963a756

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

libraries/sha204/sha204_library.cpp

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,33 @@ uint8_t atsha204Class::getSerialNumber(uint8_t * response)
5050
return returnCode;
5151
}
5252

53+
/* Calculates CRC16 value of provided data (and optionally including provided existing CRC16 data)
54+
returns the calculated CRC16 value */
55+
uint16_t atsha204Class::calculateAndUpdateCrc(uint8_t length, uint8_t *data, uint16_t current_crc)
56+
{
57+
uint8_t counter;
58+
uint16_t crc_register = current_crc;
59+
uint16_t polynom = 0x8005;
60+
uint8_t shift_register;
61+
uint8_t data_bit, crc_bit;
62+
63+
for (counter = 0; counter < length; counter++)
64+
{
65+
for (shift_register = 0x01; shift_register > 0x00; shift_register <<= 1)
66+
{
67+
data_bit = (data[counter] & shift_register) ? 1 : 0;
68+
crc_bit = crc_register >> 15;
69+
70+
// Shift CRC to the left by 1.
71+
crc_register <<= 1;
72+
73+
if ((data_bit ^ crc_bit) != 0)
74+
crc_register ^= polynom;
75+
}
76+
}
77+
return crc_register;
78+
}
79+
5380
/* SWI bit bang functions */
5481

5582
void atsha204Class::swi_set_signal_pin(uint8_t is_high)
@@ -772,26 +799,9 @@ uint8_t atsha204Class::sha204m_check_parameters(uint8_t op_code, uint8_t param1,
772799

773800
void atsha204Class::sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc)
774801
{
775-
uint8_t counter;
776802
uint16_t crc_register = 0;
777-
uint16_t polynom = 0x8005;
778-
uint8_t shift_register;
779-
uint8_t data_bit, crc_bit;
780-
781-
for (counter = 0; counter < length; counter++)
782-
{
783-
for (shift_register = 0x01; shift_register > 0x00; shift_register <<= 1)
784-
{
785-
data_bit = (data[counter] & shift_register) ? 1 : 0;
786-
crc_bit = crc_register >> 15;
787-
788-
// Shift CRC to the left by 1.
789-
crc_register <<= 1;
790803

791-
if ((data_bit ^ crc_bit) != 0)
792-
crc_register ^= polynom;
793-
}
794-
}
804+
crc_register = calculateAndUpdateCrc(length, data, crc_register);
795805
crc[0] = (uint8_t) (crc_register & 0x00FF);
796806
crc[1] = (uint8_t) (crc_register >> 8);
797807
}

libraries/sha204/sha204_library.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ class atsha204Class
314314
uint8_t tx_size, uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer);
315315

316316
uint8_t getSerialNumber(uint8_t *response);
317+
uint16_t calculateAndUpdateCrc(uint8_t length, uint8_t *data, uint16_t current_crc);
317318

318319
};
319320

0 commit comments

Comments
 (0)