Skip to content

Commit 3c903cc

Browse files
committed
cose: add tinycrypt HKDF support
HKDF support in tinycrypt is not integrated into the master branch so intel/tinycrypt#43 must be included.
1 parent 87e3fc1 commit 3c903cc

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

include/cose/crypto/selectors.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
*/
5656
#if defined(CRYPTO_SODIUM)
5757
#define CRYPTO_SODIUM_INCLUDE_HKDFSHA256
58+
#if __has_include (<tinycrypt/hkdf.h>)
59+
#define CRYPTO_TINYCRYPT_INCLUDE_HKDFSHA256
60+
#endif
5861
#endif
5962

6063
#endif /* COSE_CRYPTO_SELECTORS_H */

include/cose/crypto/tinycrypt.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ extern "C" {
3737
#define HAVE_CURVE_P256 /**< EC NIST p256 curve support */
3838

3939
#define HAVE_ALGO_AESCCM
40+
#if __has_include (<tinycrypt/hkdf.h>)
41+
#define HAVE_ALGO_HMAC256
42+
#endif
4043

4144
#define HAVE_ALGO_AESCCM_16_64_128 /**< AES CCM mode support with 16 bit length, 64 bit tag 128 bit key */
4245
#define HAVE_ALGO_AESCCM_16_128_128 /**< AES CCM mode support with 16 bit length, 128 bit tag 128 bit key */

src/crypt/tinycrypt.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include <tinycrypt/ecc_dh.h>
2121
#include <tinycrypt/ecc_dsa.h>
2222
#include <tinycrypt/sha256.h>
23+
#if __has_include (<tinycrypt/hkdf.h>)
24+
#include <tinycrypt/hkdf.h>
25+
#endif
2326

2427
extern cose_crypt_rng cose_crypt_get_random;
2528
extern void *cose_crypt_rng_arg;
@@ -188,3 +191,26 @@ int cose_crypto_verify_ecdsa(const cose_key_t *key, const uint8_t *sign, size_t
188191
int res = uECC_verify(pubkey, hash, sizeof(hash), (uint8_t*)sign, uECC_secp256r1());
189192
return res ? COSE_OK : COSE_ERR_CRYPTO;
190193
}
194+
195+
#ifdef CRYPTO_TINYCRYPT_INCLUDE_HKDFSHA256
196+
int cose_crypto_hkdf_derive_sha256(const uint8_t *salt, size_t salt_len,
197+
const uint8_t *ikm, size_t ikm_length,
198+
const uint8_t *info, size_t info_length,
199+
uint8_t *out, size_t out_length)
200+
{
201+
uint8_t prk[TC_SHA256_DIGEST_SIZE];
202+
203+
int ret = tc_hkdf_extract(ikm, ikm_length, salt, salt_len, prk);
204+
205+
if (ret != TC_CRYPTO_SUCCESS) {
206+
return COSE_ERR_CRYPTO;
207+
}
208+
209+
ret = tc_hkdf_expand(prk, info, info_length, out_length, out);
210+
211+
if (ret != TC_CRYPTO_SUCCESS) {
212+
return COSE_ERR_CRYPTO;
213+
}
214+
return COSE_OK;
215+
}
216+
#endif /* CRYPTO_TINYCRYPT_INCLUDE_HKDFSHA256 */

0 commit comments

Comments
 (0)