Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade to support mbedtls 3.x #1

Merged
merged 1 commit into from
Apr 2, 2024
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
1 change: 1 addition & 0 deletions src/key.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


#ifdef USE_MBED_TLS
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
#include <mbedtls/ecdsa.h>
#else
#include <openssl/cmac.h>
Expand Down
36 changes: 20 additions & 16 deletions src/mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdlib.h>

#ifdef USE_MBED_TLS
#define MBEDTLS_ALLOW_PRIVATE_ACCESS

#include "mbedtls/ccm.h"
#include "mbedtls/md.h"
Expand Down Expand Up @@ -857,6 +858,8 @@ bool ECDSA_Sign(COSE * pSigner, int index, const eckey_t * eckey, int cbitDigest
int cbR;
mbedtls_md_type_t mdType;
const mbedtls_md_info_t *pmdInfo;
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctrDrbg;
mbedtls_mpi r;
mbedtls_mpi s;
#ifdef USE_CBOR_CONTEXT
Expand All @@ -865,6 +868,9 @@ bool ECDSA_Sign(COSE * pSigner, int index, const eckey_t * eckey, int cbitDigest
cn_cbor * p = NULL;
bool result = false;

mbedtls_entropy_init(&entropy);
mbedtls_ctr_drbg_init(&ctrDrbg);
mbedtls_ctr_drbg_seed(&ctrDrbg, mbedtls_entropy_func, &entropy, NULL, 0);
mbedtls_mpi_init(&r);
mbedtls_mpi_init(&s);

Expand All @@ -891,7 +897,7 @@ bool ECDSA_Sign(COSE * pSigner, int index, const eckey_t * eckey, int cbitDigest
CHECK_CONDITION(pmdInfo != NULL, COSE_ERR_INVALID_PARAMETER);
CHECK_CONDITION(mbedtls_md(pmdInfo, rgbToSign, cbToSign, rgbDigest) == 0, COSE_ERR_INVALID_PARAMETER);

CHECK_CONDITION(mbedtls_ecdsa_sign_det((mbedtls_ecp_group*)&eckey->grp, &r, &s, &eckey->d, rgbDigest, mbedtls_md_get_size(pmdInfo), mdType) == 0, COSE_ERR_CRYPTO_FAIL);
CHECK_CONDITION(mbedtls_ecdsa_sign_det_ext((mbedtls_ecp_group*)&eckey->grp, &r, &s, &eckey->d, rgbDigest, mbedtls_md_get_size(pmdInfo), mdType, mbedtls_ctr_drbg_random, &ctrDrbg) == 0, COSE_ERR_CRYPTO_FAIL);

cbR = (eckey->grp.nbits + 7) / 8;

Expand All @@ -915,6 +921,8 @@ bool ECDSA_Sign(COSE * pSigner, int index, const eckey_t * eckey, int cbitDigest
COSE_FREE(pbSig, context);
mbedtls_mpi_free(&r);
mbedtls_mpi_free(&s);
mbedtls_entropy_free(&entropy);
mbedtls_ctr_drbg_free(&ctrDrbg);
return result;
#else
return false;
Expand Down Expand Up @@ -1069,22 +1077,18 @@ static int ctr_drbg_self_test_entropy( void *data, unsigned char *buf, size_t le
return( 0 );
}

void rand_bytes(byte* pb, size_t cb){

mbedtls_ctr_drbg_context ctx;
// unsigned char buf[16];

mbedtls_ctr_drbg_init( &ctx );

mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy, (void *) entropy_source_pr, nonce_pers_pr, 16, 32 );

//mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
void rand_bytes(byte* pb, size_t cb) {
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctrDrbg;

mbedtls_entropy_init(&entropy);
mbedtls_ctr_drbg_init(&ctrDrbg);
mbedtls_ctr_drbg_seed(&ctrDrbg, mbedtls_entropy_func, &entropy, nonce_pers_pr, sizeof(nonce_pers_pr));

mbedtls_ctr_drbg_random( &ctx, pb, cb );
//mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE );
//memcmp( buf, result_pr, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );

mbedtls_ctr_drbg_free( &ctx );
mbedtls_ctr_drbg_random(&ctrDrbg, pb, cb);

mbedtls_ctr_drbg_free(&ctrDrbg);
mbedtls_entropy_free(&entropy);
}
//END OF TODO RANDOM BYTES

Expand Down