Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/include/jwk_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ bool cjose_jwk_hkdf(const EVP_MD *md,
const uint8_t *ikm,
size_t ikm_len,
uint8_t *okm,
unsigned int okm_len,
size_t okm_len,
cjose_err *err);

#endif // SRC_JWK_INT_H
15 changes: 8 additions & 7 deletions src/jwe.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <openssl/hmac.h>

#include "include/concatkdf_int.h"
#include "include/header_int.h"
Expand Down Expand Up @@ -2141,7 +2140,7 @@ static bool _cjose_jwe_encrypt_dat_aes_gcm(cjose_jwe_t *jwe, const uint8_t *plai
}

////////////////////////////////////////////////////////////////////////////////
static bool _cjose_jwe_calc_auth_tag(const char *enc, cjose_jwe_t *jwe, uint8_t *md, unsigned int *md_len, cjose_err *err)
static bool _cjose_jwe_calc_auth_tag(const char *enc, cjose_jwe_t *jwe, uint8_t *md, size_t *md_len, cjose_err *err)
{
bool retval = false;
const EVP_MD *hash = NULL;
Expand All @@ -2165,6 +2164,7 @@ static bool _cjose_jwe_calc_auth_tag(const char *enc, cjose_jwe_t *jwe, uint8_t
return false;
}

const char *digest_name = EVP_MD_get0_name(hash);
uint8_t *msg = NULL;

// calculate the Authentication Tag value over AAD + IV + ciphertext + AAD length
Expand Down Expand Up @@ -2215,7 +2215,8 @@ static bool _cjose_jwe_calc_auth_tag(const char *enc, cjose_jwe_t *jwe, uint8_t
memcpy(p, &al, sizeof(uint64_t));

// HMAC the input
if (!HMAC(hash, jwe->cek, jwe->cek_len / 2, msg, msg_len, md, md_len))
if (NULL
== EVP_Q_mac(NULL, "HMAC", NULL, digest_name, NULL, jwe->cek, jwe->cek_len / 2, msg, msg_len, md, EVP_MAX_MD_SIZE, md_len))
{
CJOSE_ERROR(err, CJOSE_ERR_CRYPTO);
goto _cjose_jwe_calc_auth_tag_end;
Expand Down Expand Up @@ -2310,9 +2311,9 @@ static bool _cjose_jwe_encrypt_dat_aes_cbc(cjose_jwe_t *jwe, const uint8_t *plai
jwe->enc_ct.raw_len += bytes_encrypted;

// calculate Authentication Tag
unsigned int tag_len = 0;
size_t tag_len = 0;
uint8_t tag[EVP_MAX_MD_SIZE];
if (_cjose_jwe_calc_auth_tag(enc, jwe, (unsigned char *)&tag, &tag_len, err) == false)
if (_cjose_jwe_calc_auth_tag(enc, jwe, tag, &tag_len, err) == false)
{
goto _cjose_jwe_encrypt_dat_aes_cbc_fail;
}
Expand Down Expand Up @@ -2463,9 +2464,9 @@ static bool _cjose_jwe_decrypt_dat_aes_cbc(cjose_jwe_t *jwe, cjose_err *err)
}

// calculate Authentication Tag
unsigned int tag_len = 0;
size_t tag_len = 0;
uint8_t tag[EVP_MAX_MD_SIZE];
if (_cjose_jwe_calc_auth_tag(enc, jwe, (unsigned char *)&tag, &tag_len, err) == false)
if (_cjose_jwe_calc_auth_tag(enc, jwe, tag, &tag_len, err) == false)
{
return false;
}
Expand Down
16 changes: 11 additions & 5 deletions src/jwk.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <openssl/rand.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/param_build.h>

// internal data structures
Expand Down Expand Up @@ -2623,7 +2622,7 @@ bool cjose_jwk_hkdf(const EVP_MD *md,
const uint8_t *ikm,
size_t ikm_len,
uint8_t *okm,
unsigned int okm_len,
size_t okm_len,
cjose_err *err)
{
// current impl. is very limited: SHA256, 256 bit output, and no info
Expand All @@ -2633,18 +2632,25 @@ bool cjose_jwk_hkdf(const EVP_MD *md,
return false;
}

const char *digest_name = EVP_MD_get0_name(md);

// HKDF-Extract, HMAC-SHA256(salt, IKM) -> PRK
unsigned int prk_len;
size_t prk_len = 0;
unsigned char prk[EVP_MAX_MD_SIZE];
if (NULL == HMAC(md, salt, salt_len, ikm, ikm_len, prk, &prk_len))

if (NULL == EVP_Q_mac(NULL, "HMAC", NULL, digest_name, NULL, salt, salt_len, ikm, ikm_len, prk, sizeof(prk), &prk_len)
|| prk_len != EVP_MD_get_size(md))
{
CJOSE_ERROR(err, CJOSE_ERR_CRYPTO);
_cjose_cleanse(prk, sizeof(prk));
return false;
}

// HKDF-Expand, HMAC-SHA256(PRK,0x01) -> OKM
const unsigned char t[] = { 0x01 };
if (NULL == HMAC(md, prk, prk_len, t, sizeof(t), okm, NULL))
size_t output_len = 0;
if (NULL == EVP_Q_mac(NULL, "HMAC", NULL, digest_name, NULL, prk, prk_len, t, sizeof(t), okm, okm_len, &output_len)
|| output_len != okm_len)
{
CJOSE_ERROR(err, CJOSE_ERR_CRYPTO);
_cjose_cleanse(prk, sizeof(prk));
Expand Down
Loading