22
22
*/
23
23
24
24
#include " SparkFun_ATECCX08a_Arduino_Library.h"
25
+ #include < am_util_debug.h>
26
+ #define uprintf am_util_debug_printf
25
27
26
28
/* * \brief
27
29
@@ -108,7 +110,7 @@ boolean ATECCX08A::wakeUp()
108
110
void ATECCX08A::idleMode ()
109
111
{
110
112
_i2cPort->beginTransmission (_i2caddr); // set up to write to address
111
- _i2cPort->write (WORD_ADDRESS_VALUE_IDLE); // enter idle command (aka word address - the first part of every communication to the IC)
113
+ _i2cPort->write_byte (WORD_ADDRESS_VALUE_IDLE); // enter idle command (aka word address - the first part of every communication to the IC)
112
114
_i2cPort->endTransmission (); // actually send it
113
115
}
114
116
@@ -452,15 +454,13 @@ long ATECCX08A::random(long min, long max)
452
454
It needs length argument:
453
455
length: length of data to receive (includes count + DATA + 2 crc bytes)
454
456
*/
455
-
456
457
boolean ATECCX08A::receiveResponseData (uint8_t length, boolean debug)
457
458
{
458
459
459
460
// pull in data 32 bytes at at time. (necessary to avoid overflow on atmega328)
460
461
// if length is less than or equal to 32, then just pull it in.
461
462
// if length is greater than 32, then we must first pull in 32, then pull in remainder.
462
463
// lets use length as our tracker and we will subtract from it as we pull in data.
463
-
464
464
countGlobal = 0 ; // reset for each new message (most important, like wensleydale at a cheese party)
465
465
cleanInputBuffer ();
466
466
byte requestAttempts = 0 ; // keep track of how many times we've attempted to request, to break out if necessary
@@ -481,12 +481,13 @@ boolean ATECCX08A::receiveResponseData(uint8_t length, boolean debug)
481
481
requestAmount = length; // now we're ready to pull in the last chunk.
482
482
}
483
483
484
- _i2cPort->requestFrom (_i2caddr, requestAmount); // request bytes from slave
484
+ uint32_t ret = _i2cPort->requestFrom (_i2caddr, requestAmount); // request bytes from slave
485
+
485
486
requestAttempts++;
486
487
487
- while (_i2cPort->available ()) // slave may send less than requested
488
+ while (_i2cPort->available2 ()) // slave may send less than requested
488
489
{
489
- uint8_t value = _i2cPort->read ();
490
+ uint8_t value = _i2cPort->read2 ();
490
491
491
492
/* Make sure not to read beyond buffer size */
492
493
if (countGlobal < sizeof (inputBuffer))
@@ -499,7 +500,7 @@ boolean ATECCX08A::receiveResponseData(uint8_t length, boolean debug)
499
500
if (requestAttempts == ATRCC508A_MAX_RETRIES)
500
501
break ; // this probably means that the device is not responding.
501
502
}
502
-
503
+ # if 0
503
504
if (debug)
504
505
{
505
506
_debugSerial->print("inputBuffer: ");
@@ -510,6 +511,7 @@ boolean ATECCX08A::receiveResponseData(uint8_t length, boolean debug)
510
511
}
511
512
_debugSerial->println();
512
513
}
514
+ #endif
513
515
514
516
return true ;
515
517
}
@@ -1006,6 +1008,65 @@ boolean ATECCX08A::verifySignature(uint8_t *message, uint8_t *signature, uint8_t
1006
1008
return err;
1007
1009
}
1008
1010
1011
+ boolean ATECCX08A::sha256 (uint8_t * plain, size_t len, uint8_t * hash)
1012
+ {
1013
+ int i;
1014
+ int j;
1015
+ size_t chunks = len / SHA_BLOCK_SIZE + !!(len % SHA_BLOCK_SIZE);
1016
+ boolean err = false ;
1017
+
1018
+ if (!sendCommand (COMMAND_OPCODE_SHA, SHA_START, 0 ))
1019
+ goto error;
1020
+
1021
+ /* Divide into blocks of 64 bytes per chunk */
1022
+ for (i = 0 ; i < chunks; ++i)
1023
+ {
1024
+ size_t data_size = SHA_BLOCK_SIZE;
1025
+ uint8_t chunk[SHA_BLOCK_SIZE];
1026
+
1027
+ delay (9 );
1028
+
1029
+ if (!receiveResponseData (RESPONSE_COUNT_SIZE + RESPONSE_SIGNAL_SIZE + CRC_SIZE))
1030
+ goto error;
1031
+
1032
+ idleMode ();
1033
+
1034
+ if (!checkCount () || !checkCrc ())
1035
+ goto error;
1036
+
1037
+ // If we hear a "0x00", that means it had a successful load
1038
+ if (inputBuffer[RESPONSE_SIGNAL_INDEX] != ATRCC508A_SUCCESSFUL_SHA)
1039
+ goto error;
1040
+
1041
+ if ((len % SHA_BLOCK_SIZE) && (i + 1 == chunks))
1042
+ data_size = len % SHA_BLOCK_SIZE;
1043
+
1044
+ /* Send next */
1045
+ if (!sendCommand (COMMAND_OPCODE_SHA, (i + 1 != chunks) ? SHA_UPDATE : SHA_END, data_size, plain + i * SHA_BLOCK_SIZE, data_size))
1046
+ goto error;
1047
+ }
1048
+
1049
+ /* Read digest */
1050
+ delay (9 );
1051
+
1052
+ if (!receiveResponseData (RESPONSE_COUNT_SIZE + RESPONSE_SHA_SIZE + CRC_SIZE))
1053
+ goto error;
1054
+
1055
+ idleMode ();
1056
+
1057
+ if (!checkCount () || !checkCrc ())
1058
+ goto error;
1059
+
1060
+ /* Copy digest */
1061
+ for (i = 0 ; i < SHA256_SIZE; ++i)
1062
+ {
1063
+ hash[i] = inputBuffer[RESPONSE_SHA_INDEX + i];
1064
+ }
1065
+
1066
+ err = true ;
1067
+ error:
1068
+ return err;
1069
+ }
1009
1070
/* * \brief
1010
1071
1011
1072
writeConfigSparkFun()
@@ -1094,7 +1155,7 @@ boolean ATECCX08A::sendCommand(uint8_t command_opcode, uint8_t param1, uint16_t
1094
1155
wakeUp ();
1095
1156
1096
1157
_i2cPort->beginTransmission (_i2caddr);
1097
- _i2cPort->write (total_transmission, total_transmission_length);
1158
+ _i2cPort->write2 (total_transmission, total_transmission_length);
1098
1159
_i2cPort->endTransmission ();
1099
1160
1100
1161
err = true ;
0 commit comments