Skip to content

Commit 16083a1

Browse files
committed
Merge branch 'sc-internal'
2 parents 20fbda4 + a9ebf85 commit 16083a1

File tree

5 files changed

+68
-29
lines changed

5 files changed

+68
-29
lines changed

src/keystore.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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 (securechip_kdf(SECURECHIP_SLOT_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)) {
@@ -187,9 +187,8 @@ static keystore_error_t _stretch_password(
187187
UTIL_CLEANUP_32(kdf_in);
188188
memcpy(kdf_in, password_salted_hashed, 32);
189189

190-
// First KDF on SECURECHIP_SLOT_ROLLKEY increments the monotonic
191-
// counter. Call only once!
192-
int securechip_result = securechip_kdf(SECURECHIP_SLOT_ROLLKEY, kdf_in, 32, kdf_out);
190+
// First KDF on rollkey increments the monotonic counter. Call only once!
191+
int securechip_result = securechip_kdf_rollkey(kdf_in, 32, kdf_out);
193192
if (securechip_result) {
194193
if (securechip_result_out != NULL) {
195194
*securechip_result_out = securechip_result;
@@ -199,7 +198,7 @@ static keystore_error_t _stretch_password(
199198
// Second KDF does not use the counter and we call it multiple times.
200199
for (int i = 0; i < KDF_NUM_ITERATIONS; i++) {
201200
memcpy(kdf_in, kdf_out, 32);
202-
securechip_result = securechip_kdf(SECURECHIP_SLOT_KDF, kdf_in, 32, kdf_out);
201+
securechip_result = securechip_kdf(kdf_in, 32, kdf_out);
203202
if (securechip_result) {
204203
if (securechip_result_out != NULL) {
205204
*securechip_result_out = securechip_result;

src/securechip/securechip.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@
2626
#include <host/atca_host.h>
2727
#pragma GCC diagnostic pop
2828

29+
typedef enum {
30+
SECURECHIP_SLOT_IO_PROTECTION_KEY = 0,
31+
SECURECHIP_SLOT_AUTHKEY = 1,
32+
SECURECHIP_SLOT_ENCRYPTION_KEY = 2,
33+
SECURECHIP_SLOT_ROLLKEY = 3,
34+
SECURECHIP_SLOT_KDF = 4,
35+
SECURECHIP_SLOT_ATTESTATION = 5,
36+
SECURECHIP_SLOT_ECC_UNSAFE_SIGN = 6,
37+
SECURECHIP_SLOT_DATA0 = 9,
38+
// The other slots are currently not in use.
39+
} securechip_slot_t;
40+
2941
// Chip Configuration, generated with "make generate-atecc608-config"
3042
// The first 16 bytes, as well as the LockValue/LockConfig can't be changed and are ignored when
3143
// writing the configuration to the device. Locking is performed via the Lock command during setup,
@@ -515,7 +527,7 @@ bool securechip_update_keys(void)
515527
return _update_kdf_key() == ATCA_SUCCESS;
516528
}
517529

518-
int securechip_kdf(securechip_slot_t slot, const uint8_t* msg, size_t len, uint8_t* kdf_out)
530+
static int _securechip_kdf(securechip_slot_t slot, const uint8_t* msg, size_t len, uint8_t* kdf_out)
519531
{
520532
if (len > 127 || (slot != SECURECHIP_SLOT_ROLLKEY && slot != SECURECHIP_SLOT_KDF)) {
521533
return SC_ERR_INVALID_ARGS;
@@ -572,6 +584,16 @@ int securechip_kdf(securechip_slot_t slot, const uint8_t* msg, size_t len, uint8
572584
return atcah_io_decrypt(&io_dec_params);
573585
}
574586

587+
int securechip_kdf(const uint8_t* msg, size_t len, uint8_t* kdf_out)
588+
{
589+
return _securechip_kdf(SECURECHIP_SLOT_KDF, msg, len, kdf_out);
590+
}
591+
592+
int securechip_kdf_rollkey(const uint8_t* msg, size_t len, uint8_t* kdf_out)
593+
{
594+
return _securechip_kdf(SECURECHIP_SLOT_ROLLKEY, msg, len, kdf_out);
595+
}
596+
575597
bool securechip_gen_attestation_key(uint8_t* pubkey_out)
576598
{
577599
ATCA_STATUS result = _authorize_key();

src/securechip/securechip.h

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,6 @@ typedef struct {
4949
void (*const random_32_bytes)(uint8_t* buf);
5050
} securechip_interface_functions_t;
5151

52-
typedef enum {
53-
SECURECHIP_SLOT_IO_PROTECTION_KEY = 0,
54-
SECURECHIP_SLOT_AUTHKEY = 1,
55-
SECURECHIP_SLOT_ENCRYPTION_KEY = 2,
56-
SECURECHIP_SLOT_ROLLKEY = 3,
57-
SECURECHIP_SLOT_KDF = 4,
58-
SECURECHIP_SLOT_ATTESTATION = 5,
59-
SECURECHIP_SLOT_ECC_UNSAFE_SIGN = 6,
60-
SECURECHIP_SLOT_DATA0 = 9,
61-
// The other slots are currently not in use.
62-
} securechip_slot_t;
63-
6452
/**
6553
* Initializes the cryptoauthlib communication, by providing a custom i2c chip
6654
* communication interface/bridge to cryptoauthlib. On first call, the chip
@@ -80,22 +68,26 @@ USE_RESULT int securechip_setup(const securechip_interface_functions_t* ifs);
8068
USE_RESULT bool securechip_update_keys(void);
8169

8270
/**
83-
* Perform KDF using the key in predefined slot with the input msg.
84-
* Calling this function for SECURECHIP_SLOT_ROLLKEY also increments the
71+
* Perform HMAC using the key in KDF slot with the input msg.
72+
* @param[in] msg Use this msg as input
73+
* @param[in] len Must be <= 127.
74+
* @param[out] kdf_out Must have size 32. Result of the kdf will be stored here.
75+
* Cannot be the same as `msg`.
76+
* @return values of `securechip_error_t` if negative, values of `ATCA_STATUS` if positive, 0 on
77+
*/
78+
USE_RESULT int securechip_kdf(const uint8_t* msg, size_t len, uint8_t* kdf_out);
79+
80+
/**
81+
* Perform KDF using the key in rollkey slot with the input msg.
82+
* Calling this function increments the
8583
* monotonic counter Counter0.
86-
* @param[in] slot should be one of SECURECHIP_SLOT_ROLLKEY and
87-
* SECURECHIP_SLOT_KDF.
8884
* @param[in] msg Use this msg as input
8985
* @param[in] len Must be <= 127.
9086
* @param[out] kdf_out Must have size 32. Result of the kdf will be stored here.
9187
* Cannot be the same as `msg`.
9288
* @return values of `securechip_error_t` if negative, values of `ATCA_STATUS` if positive, 0 on
9389
*/
94-
USE_RESULT int securechip_kdf(
95-
securechip_slot_t slot,
96-
const uint8_t* msg,
97-
size_t len,
98-
uint8_t* kdf_out);
90+
USE_RESULT int securechip_kdf_rollkey(const uint8_t* msg, size_t len, uint8_t* kdf_out);
9991

10092
/**
10193
* Generates a new attestation device key and outputs the public key.

test/simulator/framework/mock_securechip.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
#include <string.h>
2323
#include <wally_crypto.h>
2424

25+
typedef enum {
26+
SECURECHIP_SLOT_ROLLKEY = 3,
27+
SECURECHIP_SLOT_KDF = 4,
28+
} securechip_slot_t;
29+
2530
static uint32_t _u2f_counter;
2631

2732
bool securechip_update_keys(void)
@@ -39,7 +44,7 @@ static const uint8_t _kdfkey[32] =
3944
"\xd2\xe1\xe6\xb1\x8b\x6c\x6b\x08\x43\x3e\xdb\xc1\xd1\x68\xc1\xa0\x04\x37\x74\xa4\x22\x18\x77"
4045
"\xe7\x9e\xd5\x66\x84\xbe\x5a\xc0\x1b";
4146

42-
int securechip_kdf(securechip_slot_t slot, const uint8_t* msg, size_t len, uint8_t* kdf_out)
47+
static int _securechip_kdf(securechip_slot_t slot, const uint8_t* msg, size_t len, uint8_t* kdf_out)
4348
{
4449
const uint8_t* key;
4550
switch (slot) {
@@ -55,6 +60,14 @@ int securechip_kdf(securechip_slot_t slot, const uint8_t* msg, size_t len, uint8
5560
wally_hmac_sha256(key, 32, msg, len, kdf_out, 32);
5661
return 0;
5762
}
63+
int securechip_kdf(const uint8_t* msg, size_t len, uint8_t* kdf_out)
64+
{
65+
return _securechip_kdf(SECURECHIP_SLOT_KDF, msg, len, kdf_out);
66+
}
67+
int securechip_kdf_rollkey(const uint8_t* msg, size_t len, uint8_t* kdf_out)
68+
{
69+
return _securechip_kdf(SECURECHIP_SLOT_ROLLKEY, msg, len, kdf_out);
70+
}
5871

5972
bool securechip_u2f_counter_set(uint32_t counter)
6073
{

test/unit-test/framework/mock_securechip.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
#include <string.h>
2424
#include <wally_crypto.h>
2525

26+
typedef enum {
27+
SECURECHIP_SLOT_ROLLKEY = 3,
28+
SECURECHIP_SLOT_KDF = 4,
29+
} securechip_slot_t;
30+
2631
static uint32_t _u2f_counter;
2732

2833
bool securechip_update_keys(void)
@@ -40,7 +45,7 @@ static const uint8_t _kdfkey[32] =
4045
"\xd2\xe1\xe6\xb1\x8b\x6c\x6b\x08\x43\x3e\xdb\xc1\xd1\x68\xc1\xa0\x04\x37\x74\xa4\x22\x18\x77"
4146
"\xe7\x9e\xd5\x66\x84\xbe\x5a\xc0\x1b";
4247

43-
int securechip_kdf(securechip_slot_t slot, const uint8_t* msg, size_t len, uint8_t* kdf_out)
48+
static int _securechip_kdf(securechip_slot_t slot, const uint8_t* msg, size_t len, uint8_t* kdf_out)
4449
{
4550
const uint8_t* key;
4651
switch (slot) {
@@ -56,6 +61,14 @@ int securechip_kdf(securechip_slot_t slot, const uint8_t* msg, size_t len, uint8
5661
wally_hmac_sha256(key, 32, msg, len, kdf_out, 32);
5762
return 0;
5863
}
64+
int securechip_kdf(const uint8_t* msg, size_t len, uint8_t* kdf_out)
65+
{
66+
return _securechip_kdf(SECURECHIP_SLOT_KDF, msg, len, kdf_out);
67+
}
68+
int securechip_kdf_rollkey(const uint8_t* msg, size_t len, uint8_t* kdf_out)
69+
{
70+
return _securechip_kdf(SECURECHIP_SLOT_ROLLKEY, msg, len, kdf_out);
71+
}
5972

6073
bool securechip_u2f_counter_set(uint32_t counter)
6174
{

0 commit comments

Comments
 (0)