Skip to content

Commit 4b34293

Browse files
committed
Add Tinycrypt based HKDF
A PR for the existing HKDF in MCUBoot was added here: intel/tinycrypt#43 Once this is merged, this PR should remove the local implementation and switch to using the one provided by Tinycrypt. Signed-off-by: Fabio Utzig <[email protected]>
1 parent c757ece commit 4b34293

File tree

3 files changed

+14
-113
lines changed

3 files changed

+14
-113
lines changed

boot/bootutil/src/encrypted.c

Lines changed: 12 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "tinycrypt/ecc_dh.h"
5151
#include "tinycrypt/ctr_mode.h"
5252
#include "tinycrypt/hmac.h"
53+
#include "tinycrypt/hkdf.h"
5354
#include "mbedtls/oid.h"
5455
#include "mbedtls/asn1.h"
5556
#endif
@@ -275,113 +276,6 @@ parse_ec256_enckey(uint8_t **p, uint8_t *end, uint8_t *pk)
275276

276277
return 0;
277278
}
278-
279-
/*
280-
* HKDF as described by RFC5869.
281-
*
282-
* @param ikm The input data to be derived.
283-
* @param ikm_len Length of the input data.
284-
* @param info An information tag.
285-
* @param info_len Length of the information tag.
286-
* @param okm Output of the KDF computation.
287-
* @param okm_len On input the requested length; on output the generated length
288-
*/
289-
static int
290-
hkdf(uint8_t *ikm, uint16_t ikm_len, uint8_t *info, uint16_t info_len,
291-
uint8_t *okm, uint16_t *okm_len)
292-
{
293-
struct tc_hmac_state_struct hmac;
294-
uint8_t salt[TC_SHA256_DIGEST_SIZE];
295-
uint8_t prk[TC_SHA256_DIGEST_SIZE];
296-
uint8_t T[TC_SHA256_DIGEST_SIZE];
297-
uint16_t off;
298-
uint16_t len;
299-
uint8_t counter;
300-
bool first;
301-
int rc;
302-
303-
/*
304-
* Extract
305-
*/
306-
307-
if (ikm == NULL || okm == NULL || ikm_len == 0) {
308-
return -1;
309-
}
310-
311-
memset(salt, 0, TC_SHA256_DIGEST_SIZE);
312-
rc = tc_hmac_set_key(&hmac, salt, TC_SHA256_DIGEST_SIZE);
313-
if (rc != TC_CRYPTO_SUCCESS) {
314-
return -1;
315-
}
316-
317-
rc = tc_hmac_init(&hmac);
318-
if (rc != TC_CRYPTO_SUCCESS) {
319-
return -1;
320-
}
321-
322-
rc = tc_hmac_update(&hmac, ikm, ikm_len);
323-
if (rc != TC_CRYPTO_SUCCESS) {
324-
return -1;
325-
}
326-
327-
rc = tc_hmac_final(prk, TC_SHA256_DIGEST_SIZE, &hmac);
328-
if (rc != TC_CRYPTO_SUCCESS) {
329-
return -1;
330-
}
331-
332-
/*
333-
* Expand
334-
*/
335-
336-
len = *okm_len;
337-
counter = 1;
338-
first = true;
339-
for (off = 0; len > 0; off += TC_SHA256_DIGEST_SIZE, ++counter) {
340-
rc = tc_hmac_set_key(&hmac, prk, TC_SHA256_DIGEST_SIZE);
341-
if (rc != TC_CRYPTO_SUCCESS) {
342-
return -1;
343-
}
344-
345-
rc = tc_hmac_init(&hmac);
346-
if (rc != TC_CRYPTO_SUCCESS) {
347-
return -1;
348-
}
349-
350-
if (first) {
351-
first = false;
352-
} else {
353-
rc = tc_hmac_update(&hmac, T, TC_SHA256_DIGEST_SIZE);
354-
if (rc != TC_CRYPTO_SUCCESS) {
355-
return -1;
356-
}
357-
}
358-
359-
rc = tc_hmac_update(&hmac, info, info_len);
360-
if (rc != TC_CRYPTO_SUCCESS) {
361-
return -1;
362-
}
363-
364-
rc = tc_hmac_update(&hmac, &counter, 1);
365-
if (rc != TC_CRYPTO_SUCCESS) {
366-
return -1;
367-
}
368-
369-
rc = tc_hmac_final(T, TC_SHA256_DIGEST_SIZE, &hmac);
370-
if (rc != TC_CRYPTO_SUCCESS) {
371-
return -1;
372-
}
373-
374-
if (len > TC_SHA256_DIGEST_SIZE) {
375-
memcpy(&okm[off], T, TC_SHA256_DIGEST_SIZE);
376-
len -= TC_SHA256_DIGEST_SIZE;
377-
} else {
378-
memcpy(&okm[off], T, len);
379-
len = 0;
380-
}
381-
}
382-
383-
return 0;
384-
}
385279
#endif
386280

387281
int
@@ -445,12 +339,12 @@ boot_enc_decrypt(const uint8_t *buf, uint8_t *enckey)
445339
struct tc_aes_key_sched_struct aes;
446340
uint8_t tag[TC_SHA256_DIGEST_SIZE];
447341
uint8_t shared[NUM_ECC_BYTES];
342+
uint8_t prk[TC_SHA256_DIGEST_SIZE];
448343
uint8_t derived_key[TC_AES_KEY_SIZE + TC_SHA256_DIGEST_SIZE];
449344
uint8_t *cp;
450345
uint8_t *cpend;
451346
uint8_t pk[NUM_ECC_BYTES];
452347
uint8_t counter[TC_AES_BLOCK_SIZE];
453-
uint16_t len;
454348
#endif
455349
int rc = -1;
456350

@@ -509,13 +403,18 @@ boot_enc_decrypt(const uint8_t *buf, uint8_t *enckey)
509403
}
510404

511405
/*
512-
* Expand shared secret to create keys for AES-128-CTR + HMAC-SHA256
406+
* Use HKDF to expand shared secret to create keys for
407+
* AES-128-CTR + HMAC-SHA256
513408
*/
514409

515-
len = TC_AES_KEY_SIZE + TC_SHA256_DIGEST_SIZE;
516-
rc = hkdf(shared, NUM_ECC_BYTES, (uint8_t *)"MCUBoot_ECIES_v1", 16,
517-
derived_key, &len);
518-
if (rc != 0 || len != (TC_AES_KEY_SIZE + TC_SHA256_DIGEST_SIZE)) {
410+
rc = tc_hkdf_extract(shared, NUM_ECC_BYTES, NULL, 0, prk);
411+
if (rc != TC_CRYPTO_SUCCESS) {
412+
return -1;
413+
}
414+
415+
rc = tc_hkdf_expand(prk, (uint8_t *)"MCUBoot_ECIES_v1", 16,
416+
TC_AES_KEY_SIZE + TC_SHA256_DIGEST_SIZE, derived_key);
417+
if (rc != TC_CRYPTO_SUCCESS) {
519418
return -1;
520419
}
521420

boot/zephyr/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ if(CONFIG_BOOT_ENCRYPT_EC256)
183183
${TINYCRYPT_DIR}/source/aes_encrypt.c
184184
${TINYCRYPT_DIR}/source/aes_decrypt.c
185185
${TINYCRYPT_DIR}/source/ctr_mode.c
186+
${TINYCRYPT_DIR}/source/hkdf.c
186187
${TINYCRYPT_DIR}/source/hmac.c
187188
${TINYCRYPT_DIR}/source/ecc_dh.c
188189
)

sim/mcuboot-sys/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ fn main() {
210210
conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
211211
conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
212212
conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
213+
conf.file("../../ext/tinycrypt/lib/source/hkdf.c");
213214
conf.file("../../ext/tinycrypt/lib/source/hmac.c");
214215
conf.file("../../ext/tinycrypt/lib/source/ecc_dh.c");
215216
}

0 commit comments

Comments
 (0)