Skip to content

Commit 9fd41b4

Browse files
committed
Added sha204 library
Taken from the sha204_library folder in https://github.com/jimblom/sha204-Breakout Commit 8e1ac05ef987b1869605ad067db2bdbaec1c109f ATSHA204 is a crypto cirquit from Atmel that enables SHA256-based MAC and HMAC calculations as well as secure key storage and random number generation. This can be used to sign messages passed between sensor nodes and prevent both man-in-the-middle and replay attacking.
1 parent 0dd6687 commit 9fd41b4

File tree

4 files changed

+1280
-0
lines changed

4 files changed

+1280
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* ATSHA204 Library Simple Example
2+
by: Jim Lindblom
3+
SparkFun Electronics
4+
date: November 8, 2012
5+
6+
This code shows how to wake up and verify that an SHA204 is
7+
connected and operational. And how to obtain an SHA204's unique serial
8+
number, and send it a MAC challenge.
9+
10+
The ATSHA204's SDA pin can be connected to any of the Arduino's digital pins.
11+
When constructing your atsha204Class, pass the constructor the pin you want to use.
12+
In this example we'll attach SDA to pin 7.
13+
14+
The ATSHA204 can be powered between 3.3V and 5V.
15+
*/
16+
#include <sha204_library.h>
17+
18+
const int sha204Pin = 7;
19+
20+
atsha204Class sha204(sha204Pin);
21+
22+
void setup()
23+
{
24+
Serial.begin(9600);
25+
Serial.println("Sending a Wakup Command. Response should be:\r\n4 11 33 43:");
26+
Serial.println("Response is:");
27+
wakeupExample();
28+
Serial.println();
29+
Serial.println("Asking the SHA204's serial number. Response should be:");
30+
Serial.println("1 23 x x x x x x x EE");
31+
Serial.println("Response is:");
32+
serialNumberExample();
33+
Serial.println();
34+
Serial.println("Sending a MAC Challenge. Response should be:");
35+
Serial.println("23 6 67 0 4F 28 4D 6E 98 62 4 F4 60 A3 E8 75 8A 59 85 A6 79 96 C4 8A 88 46 43 4E B3 DB 58 A4 FB E5 73");
36+
Serial.println("Response is:");
37+
macChallengeExample();
38+
}
39+
40+
void loop()
41+
{
42+
}
43+
44+
byte wakeupExample()
45+
{
46+
uint8_t response[SHA204_RSP_SIZE_MIN];
47+
byte returnValue;
48+
49+
returnValue = sha204.sha204c_wakeup(&response[0]);
50+
for (int i=0; i<SHA204_RSP_SIZE_MIN; i++)
51+
{
52+
Serial.print(response[i], HEX);
53+
Serial.print(" ");
54+
}
55+
Serial.println();
56+
57+
return returnValue;
58+
}
59+
60+
byte serialNumberExample()
61+
{
62+
uint8_t serialNumber[9];
63+
byte returnValue;
64+
65+
returnValue = sha204.getSerialNumber(serialNumber);
66+
for (int i=0; i<9; i++)
67+
{
68+
Serial.print(serialNumber[i], HEX);
69+
Serial.print(" ");
70+
}
71+
Serial.println();
72+
73+
return returnValue;
74+
}
75+
76+
byte macChallengeExample()
77+
{
78+
uint8_t command[MAC_COUNT_LONG];
79+
uint8_t response[MAC_RSP_SIZE];
80+
81+
const uint8_t challenge[MAC_CHALLENGE_SIZE] = {
82+
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
83+
0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
84+
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
85+
0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
86+
};
87+
88+
uint8_t ret_code = sha204.sha204m_execute(SHA204_MAC, 0, 0, MAC_CHALLENGE_SIZE,
89+
(uint8_t *) challenge, 0, NULL, 0, NULL, sizeof(command), &command[0],
90+
sizeof(response), &response[0]);
91+
92+
for (int i=0; i<SHA204_RSP_SIZE_MAX; i++)
93+
{
94+
Serial.print(response[i], HEX);
95+
Serial.print(' ');
96+
}
97+
Serial.println();
98+
99+
return ret_code;
100+
}
101+
102+
103+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// ----------------------------------------------------------------------------
2+
// ATMEL Microcontroller Software Support - Colorado Springs, CO -
3+
// ----------------------------------------------------------------------------
4+
// DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
5+
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
6+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
7+
// DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
8+
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
10+
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
11+
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
12+
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
13+
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
// ----------------------------------------------------------------------------
15+
16+
/** \file
17+
* \brief SHA204 Library Return Code Definitions
18+
* \author Atmel Crypto Products
19+
* \date September 27, 2010
20+
*/
21+
22+
#ifndef SHA204_LIB_RETURN_CODES_H
23+
# define SHA204_LIB_RETURN_CODES_H
24+
25+
#include <stddef.h> // data type definitions
26+
27+
/** \todo Use same values for same meanings for SHA204 and AES132.
28+
* */
29+
30+
#define SHA204_SUCCESS ((uint8_t) 0x00) //!< Function succeeded.
31+
#define SHA204_PARSE_ERROR ((uint8_t) 0xD2) //!< response status byte indicates parsing error
32+
#define SHA204_CMD_FAIL ((uint8_t) 0xD3) //!< response status byte indicates command execution error
33+
#define SHA204_STATUS_CRC ((uint8_t) 0xD4) //!< response status byte indicates CRC error
34+
#define SHA204_STATUS_UNKNOWN ((uint8_t) 0xD5) //!< response status byte is unknown
35+
#define SHA204_FUNC_FAIL ((uint8_t) 0xE0) //!< Function could not execute due to incorrect condition / state.
36+
#define SHA204_GEN_FAIL ((uint8_t) 0xE1) //!< unspecified error
37+
#define SHA204_BAD_PARAM ((uint8_t) 0xE2) //!< bad argument (out of range, null pointer, etc.)
38+
#define SHA204_INVALID_ID ((uint8_t) 0xE3) //!< invalid device id, id not set
39+
#define SHA204_INVALID_SIZE ((uint8_t) 0xE4) //!< Count value is out of range or greater than buffer size.
40+
#define SHA204_BAD_CRC ((uint8_t) 0xE5) //!< incorrect CRC received
41+
#define SHA204_RX_FAIL ((uint8_t) 0xE6) //!< Timed out while waiting for response. Number of bytes received is > 0.
42+
#define SHA204_RX_NO_RESPONSE ((uint8_t) 0xE7) //!< Not an error while the Command layer is polling for a command response.
43+
#define SHA204_RESYNC_WITH_WAKEUP ((uint8_t) 0xE8) //!< re-synchronization succeeded, but only after generating a Wake-up
44+
45+
#define SHA204_COMM_FAIL ((uint8_t) 0xF0) //!< Communication with device failed. Same as in hardware dependent modules.
46+
#define SHA204_TIMEOUT ((uint8_t) 0xF1) //!< Timed out while waiting for response. Number of bytes received is 0.
47+
48+
#endif

0 commit comments

Comments
 (0)