Skip to content

Commit 4299456

Browse files
author
Pete Lewis
committed
SHA256 bug fix - end command only accepts 63 bytes
-the END command of an SHA calculation cannot accept more than 63 bytes. (Datasheet Page 83) . Previously, this function, sha256(), was sending the last chunk (all 64 bytes) with the end command, and so it was failing when you sent any message data that was exactly divisable by the block size (i.e. 64, 128,256, and so on).
1 parent 6695d1a commit 4299456

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ lockDataSlot0 KEYWORD2
3030
generatePublicKey KEYWORD2
3131
createSignature KEYWORD2
3232
verifySignature KEYWORD2
33+
sha256 KEYWORD2
3334

3435

3536
#######################################

src/SparkFun_ATECCX08a_Arduino_Library.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,8 +975,11 @@ boolean ATECCX08A::verifySignature(uint8_t *message, uint8_t *signature, uint8_t
975975
boolean ATECCX08A::sha256(uint8_t * plain, size_t len, uint8_t * hash)
976976
{
977977
int i;
978-
int j;
979978
size_t chunks = len / SHA_BLOCK_SIZE + !!(len % SHA_BLOCK_SIZE);
979+
if((len % SHA_BLOCK_SIZE) == 0) chunks += 1; // END command can only accept up to 63 bytes, so we must add a "blank chunk" for the end command
980+
981+
// Serial.print("chunks:");
982+
// Serial.println(chunks);
980983

981984
if (!sendCommand(COMMAND_OPCODE_SHA, SHA_START, 0))
982985
return false;
@@ -1001,9 +1004,16 @@ boolean ATECCX08A::sha256(uint8_t * plain, size_t len, uint8_t * hash)
10011004
if (inputBuffer[RESPONSE_SIGNAL_INDEX] != ATRCC508A_SUCCESSFUL_SHA)
10021005
return false;
10031006

1004-
if ((len % SHA_BLOCK_SIZE) && (i + 1 == chunks))
1007+
if (i + 1 == chunks) // if we're on the last chunk, there will be a remainder or 0 (and 0 is okay for an end command)
10051008
data_size = len % SHA_BLOCK_SIZE;
10061009

1010+
// Serial.print("chunk:");
1011+
// Serial.println(i);
1012+
// Serial.print("data_size:");
1013+
// Serial.println(data_size);
1014+
// Serial.print("update vs end:");
1015+
// Serial.println((i + 1 != chunks) ? SHA_UPDATE : SHA_END);
1016+
10071017
/* Send next */
10081018
if (!sendCommand(COMMAND_OPCODE_SHA, (i + 1 != chunks) ? SHA_UPDATE : SHA_END, data_size, plain + i * SHA_BLOCK_SIZE, data_size))
10091019
return false;

0 commit comments

Comments
 (0)