Skip to content

Commit f26ccf0

Browse files
committed
Merge branch 'secchip-interface'
2 parents 8cf1e44 + c4ff670 commit f26ccf0

22 files changed

+418
-93
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ set(PLATFORM-BITBOX02-SOURCES ${PLATFORM-BITBOX02-SOURCES} PARENT_SCOPE)
143143

144144
set(SECURECHIP-SOURCES
145145
${CMAKE_SOURCE_DIR}/src/atecc/atecc.c
146+
${CMAKE_SOURCE_DIR}/src/securechip/securechip.c
146147
)
147148
set(SECURECHIP-SOURCES ${SECURECHIP-SOURCES} PARENT_SCOPE)
148149

src/atecc/atecc.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "atecc.h"
1616
#include "hardfault.h"
17+
#include "securechip/securechip.h"
1718
#include <i2c_ecc.h>
1819
#include <util.h>
1920

@@ -90,7 +91,7 @@ typedef union {
9091

9192
#pragma GCC diagnostic pop
9293

93-
static const atecc_interface_functions_t* _interface_functions = NULL;
94+
static const securechip_interface_functions_t* _interface_functions = NULL;
9495

9596
/** \brief initialize an I2C interface using given config.
9697
* \param[in] hal - opaque ptr to HAL data
@@ -378,7 +379,7 @@ static int _verify_config(void)
378379
return ATCA_SUCCESS;
379380
}
380381

381-
int atecc_setup(const atecc_interface_functions_t* ifs)
382+
int atecc_setup(const securechip_interface_functions_t* ifs)
382383
{
383384
if (ifs == NULL) {
384385
return ATECC_ERR_IFS;
@@ -781,7 +782,7 @@ bool atecc_u2f_counter_inc(uint32_t* counter)
781782
}
782783
#endif
783784

784-
bool atecc_model(atecc_model_t* model_out)
785+
bool atecc_model(securechip_model_t* model_out)
785786
{
786787
uint8_t revision[4] = {0};
787788
if (atcab_info(revision) != ATCA_SUCCESS) {

src/atecc/atecc.h

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define _ATECC_H_
1717

1818
#include "compiler_util.h"
19+
#include "securechip/securechip.h"
1920
#include <platform/platform_config.h>
2021
#include <stdbool.h>
2122
#include <stddef.h>
@@ -32,23 +33,6 @@ typedef enum {
3233
ATECC_ERR_INVALID_ARGS = -8,
3334
} atecc_error_t;
3435

35-
typedef struct {
36-
/**
37-
* @param[out] key_out must be of size 32
38-
*/
39-
void (*const get_auth_key)(uint8_t* key_out);
40-
/**
41-
* @param[out] key_out must be of size 32
42-
*/
43-
void (*const get_io_protection_key)(uint8_t* key_out);
44-
/**
45-
* @param[out] key_out must be of size 32
46-
*/
47-
void (*const get_encryption_key)(uint8_t* key_out);
48-
49-
void (*const random_32_bytes)(uint8_t* buf);
50-
} atecc_interface_functions_t;
51-
5236
/**
5337
* Initializes the cryptoauthlib communication, by providing a custom i2c chip
5438
* communication interface/bridge to cryptoauthlib. On first call, the chip
@@ -57,7 +41,7 @@ typedef struct {
5741
* @return values of `atecc_error_t` if negative, values of `ATCA_STATUS` if positive, 0 on
5842
* success.
5943
*/
60-
USE_RESULT int atecc_setup(const atecc_interface_functions_t* ifs);
44+
USE_RESULT int atecc_setup(const securechip_interface_functions_t* ifs);
6145

6246
/**
6347
* Updates the two KDF keys (rollkey and kdf key). The previous keys are lost
@@ -150,16 +134,11 @@ USE_RESULT bool atecc_u2f_counter_set(uint32_t counter);
150134
USE_RESULT bool atecc_u2f_counter_inc(uint32_t* counter);
151135
#endif
152136

153-
typedef enum {
154-
ATECC_ATECC608A,
155-
ATECC_ATECC608B,
156-
} atecc_model_t;
157-
158137
/**
159138
* Output the atecc model.
160139
* @param[out] model_out atecc model
161140
* @return True if success
162141
*/
163-
USE_RESULT bool atecc_model(atecc_model_t* model_out);
142+
USE_RESULT bool atecc_model(securechip_model_t* model_out);
164143

165144
#endif

src/common_main.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
#include "common_main.h"
16-
#include "atecc/atecc.h"
1716
#include "driver_init.h"
1817
#include "flags.h"
1918
#include "hardfault.h"
@@ -23,6 +22,7 @@
2322
#include "memory/smarteeprom.h"
2423
#include "random.h"
2524
#include "screen.h"
25+
#include "securechip/securechip.h"
2626
#include "util.h"
2727
#include <wally_core.h>
2828

@@ -45,7 +45,7 @@ static const memory_interface_functions_t _memory_interface_functions = {
4545
.random_32_bytes = random_32_bytes_mcu,
4646
};
4747

48-
static const atecc_interface_functions_t _securechip_interface_functions = {
48+
static const securechip_interface_functions_t _securechip_interface_functions = {
4949
.get_auth_key = memory_get_authorization_key,
5050
.get_io_protection_key = memory_get_io_protection_key,
5151
.get_encryption_key = memory_get_encryption_key,
@@ -83,9 +83,12 @@ void common_main(void)
8383
/* Enable/configure SmartEEPROM. */
8484
smarteeprom_bb02_config();
8585

86+
if (!securechip_init()) {
87+
AbortAutoenter("Failed to detect securechip");
88+
}
8689
// securechip_setup must come after memory_setup, so the io/auth keys to be
8790
// used are already initialized.
88-
int securechip_result = atecc_setup(&_securechip_interface_functions);
91+
int securechip_result = securechip_setup(&_securechip_interface_functions);
8992
if (securechip_result) {
9093
char errmsg[100] = {0};
9194
snprintf(

src/factorysetup.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "atecc/atecc.h"
1615
#include "common_main.h"
1716
#include "driver_init.h"
1817
#include "flags.h"
1918
#include "hardfault.h"
2019
#include "memory/memory.h"
2120
#include "platform_init.h"
2221
#include "screen.h"
22+
#include "securechip/securechip.h"
2323
#include "usb/usb.h"
2424
#include "usb/usb_packet.h"
2525
#include "usb/usb_processing.h"
@@ -210,7 +210,7 @@ static void _api_msg(const uint8_t* input, size_t in_len, uint8_t* output, size_
210210
case OP_GENKEY: {
211211
screen_print_debug("generating pubkey...", 0);
212212
uint8_t pubkey[64];
213-
if (!atecc_gen_attestation_key(pubkey)) {
213+
if (!securechip_gen_attestation_key(pubkey)) {
214214
screen_print_debug("generating pubkey\nfailed", 0);
215215
result = ERR_FAILED;
216216
break;
@@ -277,13 +277,13 @@ static void _api_msg(const uint8_t* input, size_t in_len, uint8_t* output, size_
277277
screen_print_debug("DONE", 0);
278278
break;
279279
case OP_SC_ROLLKEYS:
280-
if (!atecc_update_keys()) {
280+
if (!securechip_update_keys()) {
281281
screen_print_debug("rollkeys: failed", 0);
282282
result = ERR_FAILED;
283283
break;
284284
}
285285
screen_print_debug("rollkeys: success", 100);
286-
if (!atecc_u2f_counter_set(0)) {
286+
if (!securechip_u2f_counter_set(0)) {
287287
screen_print_debug("reset u2f counter", 0);
288288
result = ERR_FAILED;
289289
break;

src/keystore.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include <string.h>
1616

17-
#include "atecc/atecc.h"
1817
#include "cipher/cipher.h"
1918
#include "hardfault.h"
2019
#include "keystore.h"
@@ -23,6 +22,7 @@
2322
#include "random.h"
2423
#include "reset.h"
2524
#include "salt.h"
25+
#include "securechip/securechip.h"
2626
#include "util.h"
2727

2828
#include <rust/rust.h>
@@ -72,7 +72,7 @@ USE_RESULT static keystore_error_t _stretch_retained_seed_encryption_key(
7272
if (!salt_hash_data(encryption_key, 32, purpose_in, salted_hashed)) {
7373
return KEYSTORE_ERR_SALT;
7474
}
75-
if (atecc_kdf(salted_hashed, 32, out)) {
75+
if (securechip_kdf(salted_hashed, 32, out)) {
7676
return KEYSTORE_ERR_SECURECHIP;
7777
}
7878
if (!salt_hash_data(encryption_key, 32, purpose_out, salted_hashed)) {
@@ -188,7 +188,7 @@ static keystore_error_t _stretch_password(
188188
memcpy(kdf_in, password_salted_hashed, 32);
189189

190190
// First KDF on rollkey increments the monotonic counter. Call only once!
191-
int securechip_result = atecc_kdf_rollkey(kdf_in, 32, kdf_out);
191+
int securechip_result = securechip_kdf_rollkey(kdf_in, 32, kdf_out);
192192
if (securechip_result) {
193193
if (securechip_result_out != NULL) {
194194
*securechip_result_out = securechip_result;
@@ -198,7 +198,7 @@ static keystore_error_t _stretch_password(
198198
// Second KDF does not use the counter and we call it multiple times.
199199
for (int i = 0; i < KDF_NUM_ITERATIONS; i++) {
200200
memcpy(kdf_in, kdf_out, 32);
201-
securechip_result = atecc_kdf(kdf_in, 32, kdf_out);
201+
securechip_result = securechip_kdf(kdf_in, 32, kdf_out);
202202
if (securechip_result) {
203203
if (securechip_result_out != NULL) {
204204
*securechip_result_out = securechip_result;
@@ -302,7 +302,7 @@ keystore_error_t keystore_encrypt_and_store_seed(
302302
// Update the two kdf keys before setting a new password. This already
303303
// happens on a device reset, but we do it here again anyway so the keys are
304304
// initialized also on first use, reducing trust in the factory setup.
305-
if (!atecc_update_keys()) {
305+
if (!securechip_update_keys()) {
306306
return KEYSTORE_ERR_SECURECHIP;
307307
}
308308
uint8_t secret[32] = {0};

src/memory/memory_shared.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,8 @@ uint8_t memory_get_screen_type(void)
5151
return MEMORY_SCREEN_TYPE_SH1107;
5252
}
5353
}
54+
55+
uint8_t memory_get_securechip_type(void)
56+
{
57+
return MEMORY_SECURECHIP_TYPE_ATECC;
58+
}

src/memory/memory_shared.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,8 @@ void memory_read_shared_bootdata(chunk_shared_t* chunk_out);
102102
*/
103103
USE_RESULT uint8_t memory_get_screen_type(void);
104104

105+
#define MEMORY_SECURECHIP_TYPE_ATECC 0xFF
106+
#define MEMORY_SECURECHIP_TYPE_OPTIGA 0x01
107+
USE_RESULT uint8_t memory_get_securechip_type(void);
108+
105109
#endif

src/random.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
#include <stdio.h>
1616
#include <string.h>
1717
#ifndef TESTING
18-
#include "atecc/atecc.h"
1918
#include "driver_init.h"
2019
#include "flags.h"
20+
#include "securechip/securechip.h"
2121
#include <hal_rand_sync.h>
2222
#endif
2323
#include "hardfault.h"
@@ -72,8 +72,8 @@ static void random_32_bytes_sec(uint8_t* buf)
7272
random[i] = rand();
7373
}
7474
#else
75-
if (!atecc_random(random)) {
76-
Abort("Abort: atecc_random");
75+
if (!securechip_random(random)) {
76+
Abort("Abort: securechip_random");
7777
}
7878
#endif
7979
for (size_t i = 0; i < sizeof(random); i++) {

src/reset.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "memory/smarteeprom.h"
2121

2222
#ifndef TESTING
23-
#include "atecc/atecc.h"
23+
#include "securechip/securechip.h"
2424
#include <driver_init.h>
2525
#include <hal_delay.h>
2626
#include <ui/components/status.h>
@@ -50,7 +50,7 @@ void reset_reset(bool status)
5050
#if !defined(TESTING)
5151
bool sc_result_update_keys = false;
5252
for (int retries = 0; retries < 5; retries++) {
53-
sc_result_update_keys = atecc_update_keys();
53+
sc_result_update_keys = securechip_update_keys();
5454
if (sc_result_update_keys) {
5555
break;
5656
}
@@ -61,7 +61,7 @@ void reset_reset(bool status)
6161
#if APP_U2F == 1
6262
bool sc_result_u2f_counter_set = false;
6363
for (int retries = 0; retries < 5; retries++) {
64-
sc_result_u2f_counter_set = atecc_u2f_counter_set(0);
64+
sc_result_u2f_counter_set = securechip_u2f_counter_set(0);
6565
if (sc_result_u2f_counter_set) {
6666
break;
6767
}

src/rust/bitbox02-sys/build.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ const ALLOWLIST_FNS: &[&str] = &[
125125
"sd_write_bin",
126126
"sdcard_create",
127127
"secp256k1_ecdsa_anti_exfil_host_commit",
128-
"atecc_attestation_sign",
129-
"atecc_model",
130-
"atecc_monotonic_increments_remaining",
131-
"atecc_u2f_counter_set",
128+
"securechip_attestation_sign",
129+
"securechip_model",
130+
"securechip_monotonic_increments_remaining",
131+
"securechip_u2f_counter_set",
132132
"smarteeprom_bb02_config",
133133
"status_create",
134134
"trinary_choice_create",
@@ -150,7 +150,7 @@ const RUSTIFIED_ENUMS: &[&str] = &[
150150
"memory_result_t",
151151
"multisig_script_type_t",
152152
"output_type_t",
153-
"atecc_model_t",
153+
"securechip_model_t",
154154
"simple_type_t",
155155
"trinary_choice_t",
156156
];

src/rust/bitbox02-sys/wrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include <atecc/atecc.h>
1615
#include <bip32.h>
1716
#include <keystore.h>
1817
#include <memory/bitbox02_smarteeprom.h>
@@ -24,6 +23,7 @@
2423
#include <sd.h>
2524
#include <secp256k1_ecdsa_adaptor.h>
2625
#include <secp256k1_ecdsa_s2c.h>
26+
#include <securechip/securechip.h>
2727
#include <system.h>
2828
#include <time.h>
2929
#include <ui/components/confirm.h>

src/rust/bitbox02/src/securechip.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
pub use bitbox02_sys::atecc_model_t as Model;
15+
pub use bitbox02_sys::securechip_model_t as Model;
1616

1717
pub fn attestation_sign(challenge: &[u8; 32], signature: &mut [u8; 64]) -> Result<(), ()> {
1818
match unsafe {
19-
bitbox02_sys::atecc_attestation_sign(challenge.as_ptr(), signature.as_mut_ptr())
19+
bitbox02_sys::securechip_attestation_sign(challenge.as_ptr(), signature.as_mut_ptr())
2020
} {
2121
true => Ok(()),
2222
false => Err(()),
@@ -25,7 +25,7 @@ pub fn attestation_sign(challenge: &[u8; 32], signature: &mut [u8; 64]) -> Resul
2525

2626
pub fn monotonic_increments_remaining() -> Result<u32, ()> {
2727
let mut result: u32 = 0;
28-
match unsafe { bitbox02_sys::atecc_monotonic_increments_remaining(&mut result as _) } {
28+
match unsafe { bitbox02_sys::securechip_monotonic_increments_remaining(&mut result as _) } {
2929
true => Ok(result),
3030
false => Err(()),
3131
}
@@ -34,7 +34,7 @@ pub fn monotonic_increments_remaining() -> Result<u32, ()> {
3434
#[cfg(feature = "app-u2f")]
3535
#[cfg(not(feature = "testing"))]
3636
pub fn u2f_counter_set(counter: u32) -> Result<(), ()> {
37-
match unsafe { bitbox02_sys::atecc_u2f_counter_set(counter) } {
37+
match unsafe { bitbox02_sys::securechip_u2f_counter_set(counter) } {
3838
true => Ok(()),
3939
false => Err(()),
4040
}
@@ -48,7 +48,7 @@ pub fn u2f_counter_set(_counter: u32) -> Result<(), ()> {
4848

4949
pub fn model() -> Result<Model, ()> {
5050
let mut ver = core::mem::MaybeUninit::uninit();
51-
match unsafe { bitbox02_sys::atecc_model(ver.as_mut_ptr()) } {
51+
match unsafe { bitbox02_sys::securechip_model(ver.as_mut_ptr()) } {
5252
true => Ok(unsafe { ver.assume_init() }),
5353
false => Err(()),
5454
}

0 commit comments

Comments
 (0)