|
50 | 50 | #include "tinycrypt/ecc_dh.h" |
51 | 51 | #include "tinycrypt/ctr_mode.h" |
52 | 52 | #include "tinycrypt/hmac.h" |
| 53 | +#include "tinycrypt/hkdf.h" |
53 | 54 | #include "mbedtls/oid.h" |
54 | 55 | #include "mbedtls/asn1.h" |
55 | 56 | #endif |
@@ -275,113 +276,6 @@ parse_ec256_enckey(uint8_t **p, uint8_t *end, uint8_t *pk) |
275 | 276 |
|
276 | 277 | return 0; |
277 | 278 | } |
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 | | -} |
385 | 279 | #endif |
386 | 280 |
|
387 | 281 | int |
@@ -445,12 +339,12 @@ boot_enc_decrypt(const uint8_t *buf, uint8_t *enckey) |
445 | 339 | struct tc_aes_key_sched_struct aes; |
446 | 340 | uint8_t tag[TC_SHA256_DIGEST_SIZE]; |
447 | 341 | uint8_t shared[NUM_ECC_BYTES]; |
| 342 | + uint8_t prk[TC_SHA256_DIGEST_SIZE]; |
448 | 343 | uint8_t derived_key[TC_AES_KEY_SIZE + TC_SHA256_DIGEST_SIZE]; |
449 | 344 | uint8_t *cp; |
450 | 345 | uint8_t *cpend; |
451 | 346 | uint8_t pk[NUM_ECC_BYTES]; |
452 | 347 | uint8_t counter[TC_AES_BLOCK_SIZE]; |
453 | | - uint16_t len; |
454 | 348 | #endif |
455 | 349 | int rc = -1; |
456 | 350 |
|
@@ -509,13 +403,18 @@ boot_enc_decrypt(const uint8_t *buf, uint8_t *enckey) |
509 | 403 | } |
510 | 404 |
|
511 | 405 | /* |
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 |
513 | 408 | */ |
514 | 409 |
|
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) { |
519 | 418 | return -1; |
520 | 419 | } |
521 | 420 |
|
|
0 commit comments