Skip to content

Commit 7c014f2

Browse files
author
Andrzej Kurek
committed
Reshape the example to use the secure element driver structure
1 parent ea2396d commit 7c014f2

File tree

3 files changed

+273
-67
lines changed

3 files changed

+273
-67
lines changed

atecc608a_utils.c

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* \file atecc608a_utils.c
3+
* \brief ATECC508A and ATECC509A utility functions.
4+
*/
5+
6+
/*
7+
* Copyright (C) 2019, ARM Limited, All Rights Reserved
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
11+
* not use this file except in compliance with the License.
12+
* You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing, software
17+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
* See the License for the specific language governing permissions and
20+
* limitations under the License.
21+
*/
22+
#include "atecc608a_se.h"
23+
#include "atecc608a_utils.h"
24+
25+
#include "atca_basic.h"
26+
27+
ATCAIfaceCfg atca_iface_config = {
28+
.iface_type = ATCA_I2C_IFACE,
29+
.devtype = ATECC608A,
30+
.atcai2c.slave_address = 0xC0,
31+
.atcai2c.bus = 2,
32+
.atcai2c.baud = 400000,
33+
.wake_delay = 1500,
34+
.rx_retries = 20,
35+
};
36+
37+
psa_status_t atecc608a_to_psa_error(ATCA_STATUS ret)
38+
{
39+
switch (ret)
40+
{
41+
case ATCA_SUCCESS:
42+
case ATCA_RX_NO_RESPONSE:
43+
case ATCA_WAKE_SUCCESS:
44+
return PSA_SUCCESS;
45+
case ATCA_BAD_PARAM:
46+
case ATCA_INVALID_ID:
47+
return PSA_ERROR_INVALID_ARGUMENT;
48+
case ATCA_ASSERT_FAILURE:
49+
return PSA_ERROR_TAMPERING_DETECTED;
50+
case ATCA_SMALL_BUFFER:
51+
return PSA_ERROR_BUFFER_TOO_SMALL;
52+
case ATCA_RX_CRC_ERROR:
53+
case ATCA_RX_FAIL:
54+
case ATCA_STATUS_CRC:
55+
case ATCA_RESYNC_WITH_WAKEUP:
56+
case ATCA_PARITY_ERROR:
57+
case ATCA_TX_TIMEOUT:
58+
case ATCA_RX_TIMEOUT:
59+
case ATCA_TOO_MANY_COMM_RETRIES:
60+
case ATCA_COMM_FAIL:
61+
case ATCA_TIMEOUT:
62+
case ATCA_TX_FAIL:
63+
case ATCA_NO_DEVICES:
64+
return PSA_ERROR_COMMUNICATION_FAILURE;
65+
case ATCA_UNIMPLEMENTED:
66+
return PSA_ERROR_NOT_SUPPORTED;
67+
case ATCA_ALLOC_FAILURE:
68+
return PSA_ERROR_INSUFFICIENT_MEMORY;
69+
case ATCA_BAD_OPCODE:
70+
case ATCA_CONFIG_ZONE_LOCKED:
71+
case ATCA_DATA_ZONE_LOCKED:
72+
case ATCA_NOT_LOCKED:
73+
case ATCA_WAKE_FAILED:
74+
case ATCA_STATUS_UNKNOWN:
75+
case ATCA_STATUS_ECC:
76+
case ATCA_STATUS_SELFTEST_ERROR:
77+
case ATCA_CHECKMAC_VERIFY_FAILED:
78+
case ATCA_PARSE_ERROR:
79+
case ATCA_FUNC_FAIL:
80+
case ATCA_GEN_FAIL:
81+
case ATCA_EXECUTION_ERROR:
82+
case ATCA_HEALTH_TEST_ERROR:
83+
case ATCA_INVALID_SIZE:
84+
default:
85+
return PSA_ERROR_HARDWARE_FAILURE;
86+
}
87+
}
88+
89+
psa_status_t atecc608a_get_serial_number(uint8_t* buffer,
90+
size_t buffer_size,
91+
size_t *buffer_length)
92+
{
93+
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
94+
95+
if (buffer_size < ATCA_SERIAL_NUM_SIZE)
96+
{
97+
return PSA_ERROR_BUFFER_TOO_SMALL;
98+
}
99+
100+
ATCAB_INIT();
101+
102+
ASSERT_SUCCESS(atcab_read_serial_number(buffer));
103+
*buffer_length = ATCA_SERIAL_NUM_SIZE;
104+
105+
exit:
106+
ATCAB_DEINIT();
107+
return status;
108+
}
109+
110+
psa_status_t atecc608a_check_config_locked()
111+
{
112+
bool config_locked;
113+
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
114+
115+
ATCAB_INIT();
116+
117+
ASSERT_SUCCESS(atcab_is_locked(LOCK_ZONE_CONFIG, &config_locked));
118+
119+
exit:
120+
ATCAB_DEINIT();
121+
if (status == PSA_SUCCESS)
122+
{
123+
status = config_locked? PSA_SUCCESS : PSA_ERROR_HARDWARE_FAILURE;
124+
}
125+
return status;
126+
}

atecc608a_utils.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* \file atecc608a_utils.h
3+
* \brief ATECC508A and ATECC509A utility functions.
4+
*/
5+
6+
/*
7+
* Copyright (C) 2019, ARM Limited, All Rights Reserved
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
11+
* not use this file except in compliance with the License.
12+
* You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing, software
17+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
* See the License for the specific language governing permissions and
20+
* limitations under the License.
21+
*/
22+
23+
#ifndef ATECC608A_UTILS_H
24+
#define ATECC608A_UTILS_H
25+
26+
#include "atca_iface.h"
27+
#include "psa/crypto.h"
28+
29+
#define ATCAB_INIT() \
30+
do \
31+
{ \
32+
if (atcab_init(&atca_iface_config) != ATCA_SUCCESS) \
33+
{ \
34+
status = PSA_ERROR_HARDWARE_FAILURE; \
35+
goto exit; \
36+
} \
37+
} while(0)
38+
39+
/** `atcab_release()` might return `ATCA_BAD_PARAM` if there is no global device
40+
* initialized via `atcab_init()`. HAL might return an error if an i2c device
41+
* cannot be released, but in current implementations it always returns
42+
* `ATCA_SUCCESS` - therefore we are ignoring the return code. */
43+
#define ATCAB_DEINIT() \
44+
do \
45+
{ \
46+
atcab_release(); \
47+
} while(0)
48+
49+
/** This macro checks if the result of an `expression` is equal to an
50+
* `expected` value and sets a `status` variable of type `psa_status_t` to
51+
* `PSA_SUCCESS`. If they are not equal, the `status` is set to
52+
* `psa_error instead`, the error details are printed, and the code jumps
53+
* to the `exit` label. */
54+
#define ASSERT_STATUS(expression, expected, psa_error) \
55+
do \
56+
{ \
57+
ATCA_STATUS ASSERT_result = (expression); \
58+
ATCA_STATUS ASSERT_expected = (expected); \
59+
if ((ASSERT_result) != (ASSERT_expected)) \
60+
{ \
61+
printf("assertion failed at %s:%d " \
62+
"(actual=%d expected=%d)\n", __FILE__, __LINE__, \
63+
ASSERT_result, ASSERT_expected); \
64+
status = (psa_error); \
65+
goto exit; \
66+
} \
67+
status = PSA_SUCCESS; \
68+
} while(0)
69+
70+
/** Check if an ATCA operation is succesfull, translate the error otherwise. */
71+
#define ASSERT_SUCCESS(expression) ASSERT_STATUS(expression, ATCA_SUCCESS, \
72+
atecc608a_to_psa_error(ASSERT_result))
73+
74+
/** Does the same as the macro above, but without the error translation and for
75+
* the PSA return code - PSA_SUCCESS.*/
76+
#define ASSERT_SUCCESS_PSA(expression) ASSERT_STATUS(expression, PSA_SUCCESS, \
77+
ASSERT_result)
78+
79+
psa_status_t atecc608a_get_serial_number(uint8_t* buffer, size_t buffer_size,
80+
size_t *buffer_length);
81+
psa_status_t atecc608a_check_config_locked();
82+
psa_status_t atecc608a_to_psa_error(ATCA_STATUS ret);
83+
84+
extern ATCAIfaceCfg atca_iface_config;
85+
86+
#endif /* ATECC608A_SE_H */

0 commit comments

Comments
 (0)