Skip to content

Commit fe920b6

Browse files
author
itayzafrir
committed
Basic communication with the ATECC608A
1 parent 0bf232b commit fe920b6

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

main.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,112 @@
1+
/*
2+
* Copyright (c) 2018, Arm Limited and affiliates
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
117

18+
#include <stdio.h>
19+
20+
#if defined(ATCA_HAL_I2C)
21+
22+
#define ASSERT_STATUS(actual, expected) \
23+
do \
24+
{ \
25+
if ((actual) != (expected)) \
26+
{ \
27+
printf("assertion failed at %s:%d " \
28+
"(actual=%d expected=%d)\n", __FILE__, __LINE__, \
29+
(int)actual, (int)expected); \
30+
return; \
31+
} \
32+
} while(0)
33+
34+
#define SEPARATOR printf("**********************************************\n");
35+
36+
#include <inttypes.h>
37+
#include <string.h>
38+
#include "atca_status.h"
39+
#include "atca_devtypes.h"
40+
#include "atca_iface.h"
41+
#include "atca_command.h"
42+
#include "atca_basic.h"
43+
#include "atca_helpers.h"
44+
45+
static ATCAIfaceCfg atca_iface_config = {
46+
ATCA_I2C_IFACE,
47+
ATECC608A,
48+
0xC0,
49+
2,
50+
400000,
51+
1500,
52+
20
53+
};
54+
55+
static const uint8_t hash_input1[] = "abc";
56+
/* SHA-256 hash of ['a','b','c'] */
57+
static const uint8_t sha256_expected_hash1[] = {
58+
0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA, 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
59+
0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C, 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD
60+
};
61+
62+
static const uint8_t hash_input2[] = "";
63+
/* SHA-256 hash of an empty string */
64+
static const uint8_t sha256_expected_hash2[] = {
65+
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
66+
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
67+
};
68+
69+
static void hash_sha256(const uint8_t *input, size_t input_size,
70+
const uint8_t *expected_hash, size_t expected_hash_size)
71+
{
72+
uint8_t actual_hash[ATCA_SHA_DIGEST_SIZE] = {0};
73+
printf("SHA-256:\n\n");
74+
atcab_printbin_label("Input: ", (uint8_t *)input, input_size);
75+
atcab_printbin_label("Expected Hash: ", (uint8_t *)expected_hash, expected_hash_size);
76+
ASSERT_STATUS(atcab_init(&atca_iface_config), ATCA_SUCCESS);
77+
ASSERT_STATUS(atcab_hw_sha2_256(input, input_size, actual_hash), ATCA_SUCCESS);
78+
ASSERT_STATUS(atcab_release(), ATCA_SUCCESS);
79+
atcab_printbin_label("Actual Hash: ", actual_hash, ATCA_SHA_DIGEST_SIZE);
80+
ASSERT_STATUS(memcmp(actual_hash, expected_hash, sizeof(actual_hash)), 0);
81+
printf("Success!\n\n");
82+
}
83+
84+
static void read_serial_number(void)
85+
{
86+
uint8_t serial[ATCA_SERIAL_NUM_SIZE];
87+
ASSERT_STATUS(atcab_init(&atca_iface_config), ATCA_SUCCESS);
88+
ASSERT_STATUS(atcab_read_serial_number(serial), ATCA_SUCCESS);
89+
ASSERT_STATUS(atcab_release(), ATCA_SUCCESS);
90+
printf("Serial Number:\n");
91+
atcab_printbin_sp(serial, ATCA_SERIAL_NUM_SIZE);
92+
printf("\n");
93+
}
94+
95+
int main(void)
96+
{
97+
SEPARATOR
98+
read_serial_number();
99+
SEPARATOR
100+
hash_sha256(hash_input1, sizeof(hash_input1) - 1, sha256_expected_hash1, sizeof(sha256_expected_hash1));
101+
SEPARATOR
102+
hash_sha256(hash_input2, sizeof(hash_input2) - 1, sha256_expected_hash2, sizeof(sha256_expected_hash2));
103+
return 0;
104+
}
105+
#else
2106
int main(void)
3107
{
108+
printf("Not all of the required options are defined:\n"
109+
" - ATCA_HAL_I2C\n");
4110
return 0;
5111
}
112+
#endif

mbed_app.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
"cryptoauthlib.i2c_sda": "PA08",
1414
"cryptoauthlib.i2c_scl": "PA09"
1515
}
16-
}
16+
},
17+
"macros": ["ATCAPRINTF"]
1718
}

0 commit comments

Comments
 (0)