mirror of
https://github.com/espressif/esp-idf.git
synced 2025-10-02 18:10:57 +02:00
Merge branch 'bugfix/cleanup_ctr_drbg' into 'master'
wpa_supplicant: Replace use of mbedtls_ctr_drbg with esp_mbedtls_random() Closes IDFGH-14978 See merge request espressif/esp-idf!39221
This commit is contained in:
@@ -9,6 +9,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "esp_random.h"
|
#include "esp_random.h"
|
||||||
|
#include "mbedtls/esp_mbedtls_random.h"
|
||||||
|
|
||||||
#include <entropy_poll.h>
|
#include <entropy_poll.h>
|
||||||
|
|
||||||
@@ -23,3 +24,10 @@ int mbedtls_hardware_poll( void *data,
|
|||||||
*olen = len;
|
*olen = len;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int mbedtls_esp_random(void *ctx, unsigned char *buf, size_t len)
|
||||||
|
{
|
||||||
|
(void) ctx; // unused
|
||||||
|
esp_fill_random(buf, len);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
32
components/mbedtls/port/include/mbedtls/esp_mbedtls_random.h
Normal file
32
components/mbedtls/port/include/mbedtls/esp_mbedtls_random.h
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief MbedTLS-compatible RNG function
|
||||||
|
*
|
||||||
|
* @note Suitable for passing as f_rng to various MbedTLS APIs that require it.
|
||||||
|
* It uses esp_fill_random internally, and the caller must ensure that the
|
||||||
|
* entropy sources of the RNG peripheral are enabled correctly. See the RNG
|
||||||
|
* chapter in the TRM for more details.
|
||||||
|
*
|
||||||
|
* @param ctx User-supplied context
|
||||||
|
* @param buf Pointer to a buffer to fill with random numbers
|
||||||
|
* @param len Length of the buffer in bytes
|
||||||
|
*
|
||||||
|
* @return 0 on success
|
||||||
|
*/
|
||||||
|
int mbedtls_esp_random(void *ctx, unsigned char *buf, size_t len);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
@@ -21,7 +21,7 @@ static heap_trace_record_t trace_record[NUM_RECORDS]; // This buffer must be in
|
|||||||
#include "esp_ds.h"
|
#include "esp_ds.h"
|
||||||
#include "esp_ds/esp_ds_rsa.h"
|
#include "esp_ds/esp_ds_rsa.h"
|
||||||
|
|
||||||
int mbedtls_esp_random(void *ctx, unsigned char *output, size_t len)
|
static int mbedtls_esp_random(void *ctx, unsigned char *output, size_t len)
|
||||||
{
|
{
|
||||||
if (len == 0 || output == NULL) {
|
if (len == 0 || output == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@@ -7,6 +7,7 @@
|
|||||||
#ifdef ESP_PLATFORM
|
#ifdef ESP_PLATFORM
|
||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "mbedtls/bignum.h"
|
#include "mbedtls/bignum.h"
|
||||||
|
#include "mbedtls/esp_mbedtls_random.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "utils/includes.h"
|
#include "utils/includes.h"
|
||||||
@@ -16,11 +17,6 @@
|
|||||||
#include "sha256.h"
|
#include "sha256.h"
|
||||||
#include "mbedtls/pk.h"
|
#include "mbedtls/pk.h"
|
||||||
|
|
||||||
static int crypto_rng_wrapper(void *ctx, unsigned char *buf, size_t len)
|
|
||||||
{
|
|
||||||
return random_get_bytes(buf, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct crypto_bignum *crypto_bignum_init(void)
|
struct crypto_bignum *crypto_bignum_init(void)
|
||||||
{
|
{
|
||||||
mbedtls_mpi *bn = os_zalloc(sizeof(mbedtls_mpi));
|
mbedtls_mpi *bn = os_zalloc(sizeof(mbedtls_mpi));
|
||||||
@@ -220,7 +216,7 @@ int crypto_bignum_is_odd(const struct crypto_bignum *a)
|
|||||||
int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m)
|
int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m)
|
||||||
{
|
{
|
||||||
return ((mbedtls_mpi_random((mbedtls_mpi *) r, 0, (const mbedtls_mpi *) m,
|
return ((mbedtls_mpi_random((mbedtls_mpi *) r, 0, (const mbedtls_mpi *) m,
|
||||||
crypto_rng_wrapper, NULL) != 0) ? -1 : 0);
|
mbedtls_esp_random, NULL) != 0) ? -1 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int crypto_bignum_legendre(const struct crypto_bignum *a,
|
int crypto_bignum_legendre(const struct crypto_bignum *a,
|
||||||
|
@@ -7,6 +7,7 @@
|
|||||||
#ifdef ESP_PLATFORM
|
#ifdef ESP_PLATFORM
|
||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "mbedtls/bignum.h"
|
#include "mbedtls/bignum.h"
|
||||||
|
#include "mbedtls/esp_mbedtls_random.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "utils/includes.h"
|
#include "utils/includes.h"
|
||||||
@@ -16,8 +17,6 @@
|
|||||||
#include "random.h"
|
#include "random.h"
|
||||||
|
|
||||||
#include "mbedtls/ecp.h"
|
#include "mbedtls/ecp.h"
|
||||||
#include "mbedtls/entropy.h"
|
|
||||||
#include "mbedtls/ctr_drbg.h"
|
|
||||||
|
|
||||||
#include "mbedtls/pk.h"
|
#include "mbedtls/pk.h"
|
||||||
#include "mbedtls/ecdh.h"
|
#include "mbedtls/ecdh.h"
|
||||||
@@ -36,10 +35,6 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_ECC
|
#ifdef CONFIG_ECC
|
||||||
static int crypto_rng_wrapper(void *ctx, unsigned char *buf, size_t len)
|
|
||||||
{
|
|
||||||
return random_get_bytes(buf, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct crypto_ec *crypto_ec_init(int group)
|
struct crypto_ec *crypto_ec_init(int group)
|
||||||
{
|
{
|
||||||
@@ -294,24 +289,14 @@ int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
|
|||||||
struct crypto_ec_point *res)
|
struct crypto_ec_point *res)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
mbedtls_entropy_context entropy;
|
|
||||||
mbedtls_ctr_drbg_context ctr_drbg;
|
|
||||||
|
|
||||||
mbedtls_entropy_init(&entropy);
|
|
||||||
mbedtls_ctr_drbg_init(&ctr_drbg);
|
|
||||||
|
|
||||||
MBEDTLS_MPI_CHK(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
|
|
||||||
NULL, 0));
|
|
||||||
|
|
||||||
MBEDTLS_MPI_CHK(mbedtls_ecp_mul((mbedtls_ecp_group *)e,
|
MBEDTLS_MPI_CHK(mbedtls_ecp_mul((mbedtls_ecp_group *)e,
|
||||||
(mbedtls_ecp_point *) res,
|
(mbedtls_ecp_point *) res,
|
||||||
(const mbedtls_mpi *)b,
|
(const mbedtls_mpi *)b,
|
||||||
(const mbedtls_ecp_point *)p,
|
(const mbedtls_ecp_point *)p,
|
||||||
mbedtls_ctr_drbg_random,
|
mbedtls_esp_random,
|
||||||
&ctr_drbg));
|
NULL));
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
mbedtls_ctr_drbg_free(&ctr_drbg);
|
|
||||||
mbedtls_entropy_free(&entropy);
|
|
||||||
return ret ? -1 : 0;
|
return ret ? -1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -491,23 +476,10 @@ int crypto_ec_point_cmp(const struct crypto_ec *e,
|
|||||||
|
|
||||||
int crypto_ec_key_compare(struct crypto_ec_key *key1, struct crypto_ec_key *key2)
|
int crypto_ec_key_compare(struct crypto_ec_key *key1, struct crypto_ec_key *key2)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
if (mbedtls_pk_check_pair((mbedtls_pk_context *)key1, (mbedtls_pk_context *)key2, mbedtls_esp_random, NULL) < 0) {
|
||||||
mbedtls_entropy_context entropy;
|
return 0;
|
||||||
mbedtls_ctr_drbg_context ctr_drbg;
|
|
||||||
|
|
||||||
mbedtls_entropy_init(&entropy);
|
|
||||||
mbedtls_ctr_drbg_init(&ctr_drbg);
|
|
||||||
|
|
||||||
MBEDTLS_MPI_CHK(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0));
|
|
||||||
if (mbedtls_pk_check_pair((mbedtls_pk_context *)key1, (mbedtls_pk_context *)key2, mbedtls_ctr_drbg_random, &ctr_drbg) < 0) {
|
|
||||||
goto cleanup;
|
|
||||||
}
|
}
|
||||||
|
return 1;
|
||||||
ret = 1;
|
|
||||||
cleanup:
|
|
||||||
mbedtls_ctr_drbg_free(&ctr_drbg);
|
|
||||||
mbedtls_entropy_free(&entropy);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void crypto_debug_print_point(const char *title, struct crypto_ec *e,
|
void crypto_debug_print_point(const char *title, struct crypto_ec *e,
|
||||||
@@ -707,7 +679,7 @@ struct crypto_ec_key *crypto_ec_key_parse_priv(const u8 *privkey, size_t privkey
|
|||||||
wpa_printf(MSG_ERROR, "memory allocation failed");
|
wpa_printf(MSG_ERROR, "memory allocation failed");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ret = mbedtls_pk_parse_key(kctx, privkey, privkey_len, NULL, 0, crypto_rng_wrapper, NULL);
|
ret = mbedtls_pk_parse_key(kctx, privkey, privkey_len, NULL, 0, mbedtls_esp_random, NULL);
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
//crypto_print_error_string(ret);
|
//crypto_print_error_string(ret);
|
||||||
@@ -763,17 +735,8 @@ int crypto_ecdh(struct crypto_ec_key *key_own, struct crypto_ec_key *key_peer,
|
|||||||
mbedtls_ecdh_context *ctx = NULL;
|
mbedtls_ecdh_context *ctx = NULL;
|
||||||
mbedtls_pk_context *own = (mbedtls_pk_context *)key_own;
|
mbedtls_pk_context *own = (mbedtls_pk_context *)key_own;
|
||||||
mbedtls_pk_context *peer = (mbedtls_pk_context *)key_peer;
|
mbedtls_pk_context *peer = (mbedtls_pk_context *)key_peer;
|
||||||
mbedtls_entropy_context entropy;
|
|
||||||
mbedtls_ctr_drbg_context ctr_drbg;
|
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
|
|
||||||
mbedtls_entropy_init(&entropy);
|
|
||||||
mbedtls_ctr_drbg_init(&ctr_drbg);
|
|
||||||
|
|
||||||
if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
*secret_len = 0;
|
*secret_len = 0;
|
||||||
ctx = os_malloc(sizeof(*ctx));
|
ctx = os_malloc(sizeof(*ctx));
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
@@ -801,7 +764,7 @@ int crypto_ecdh(struct crypto_ec_key *key_own, struct crypto_ec_key *key_peer,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mbedtls_ecdh_calc_secret(ctx, secret_len, secret, DPP_MAX_SHARED_SECRET_LEN,
|
if (mbedtls_ecdh_calc_secret(ctx, secret_len, secret, DPP_MAX_SHARED_SECRET_LEN,
|
||||||
mbedtls_ctr_drbg_random, &ctr_drbg) < 0) {
|
mbedtls_esp_random, NULL) < 0) {
|
||||||
wpa_printf(MSG_ERROR, "failed to calculate secret");
|
wpa_printf(MSG_ERROR, "failed to calculate secret");
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@@ -814,8 +777,6 @@ int crypto_ecdh(struct crypto_ec_key *key_own, struct crypto_ec_key *key_peer,
|
|||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
mbedtls_ctr_drbg_free(&ctr_drbg);
|
|
||||||
mbedtls_entropy_free(&entropy);
|
|
||||||
if (ctx) {
|
if (ctx) {
|
||||||
mbedtls_ecdh_free(ctx);
|
mbedtls_ecdh_free(ctx);
|
||||||
os_free(ctx);
|
os_free(ctx);
|
||||||
@@ -840,7 +801,7 @@ int crypto_ecdsa_get_sign(unsigned char *hash,
|
|||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
ret = mbedtls_ecdsa_sign(&ctx->MBEDTLS_PRIVATE(grp), (mbedtls_mpi *)r, (mbedtls_mpi *)s,
|
ret = mbedtls_ecdsa_sign(&ctx->MBEDTLS_PRIVATE(grp), (mbedtls_mpi *)r, (mbedtls_mpi *)s,
|
||||||
&ctx->MBEDTLS_PRIVATE(d), hash, SHA256_MAC_LEN, crypto_rng_wrapper, NULL);
|
&ctx->MBEDTLS_PRIVATE(d), hash, SHA256_MAC_LEN, mbedtls_esp_random, NULL);
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
mbedtls_ecdsa_free(ctx);
|
mbedtls_ecdsa_free(ctx);
|
||||||
@@ -939,7 +900,7 @@ struct crypto_ec_key * crypto_ec_key_gen(u16 ike_group)
|
|||||||
}
|
}
|
||||||
|
|
||||||
mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1, mbedtls_pk_ec(*kctx), //get this from argument
|
mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1, mbedtls_pk_ec(*kctx), //get this from argument
|
||||||
crypto_rng_wrapper, NULL);
|
mbedtls_esp_random, NULL);
|
||||||
|
|
||||||
return (struct crypto_ec_key *)kctx;
|
return (struct crypto_ec_key *)kctx;
|
||||||
fail:
|
fail:
|
||||||
@@ -1124,8 +1085,6 @@ void crypto_ecdh_deinit(struct crypto_ecdh *ecdh)
|
|||||||
|
|
||||||
struct crypto_ecdh * crypto_ecdh_init(int group)
|
struct crypto_ecdh * crypto_ecdh_init(int group)
|
||||||
{
|
{
|
||||||
mbedtls_ctr_drbg_context ctr_drbg;
|
|
||||||
mbedtls_entropy_context entropy;
|
|
||||||
mbedtls_ecdh_context *ctx;
|
mbedtls_ecdh_context *ctx;
|
||||||
|
|
||||||
ctx = os_zalloc(sizeof(*ctx));
|
ctx = os_zalloc(sizeof(*ctx));
|
||||||
@@ -1143,24 +1102,12 @@ struct crypto_ecdh * crypto_ecdh_init(int group)
|
|||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize CTR_DRBG context */
|
|
||||||
mbedtls_ctr_drbg_init(&ctr_drbg);
|
|
||||||
mbedtls_entropy_init(&entropy);
|
|
||||||
|
|
||||||
/* Seed and setup CTR_DRBG entropy source for future reseeds */
|
|
||||||
if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) != 0) {
|
|
||||||
wpa_printf(MSG_ERROR, "Seeding entropy source failed");
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Generates ECDH keypair on elliptic curve */
|
/* Generates ECDH keypair on elliptic curve */
|
||||||
if (mbedtls_ecdh_gen_public(ACCESS_ECDH(&ctx, grp), ACCESS_ECDH(&ctx, d), ACCESS_ECDH(&ctx, Q), mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
|
if (mbedtls_ecdh_gen_public(ACCESS_ECDH(&ctx, grp), ACCESS_ECDH(&ctx, d), ACCESS_ECDH(&ctx, Q), mbedtls_esp_random, NULL) != 0) {
|
||||||
wpa_printf(MSG_ERROR, "ECDH keypair on curve failed");
|
wpa_printf(MSG_ERROR, "ECDH keypair on curve failed");
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
mbedtls_ctr_drbg_free(&ctr_drbg);
|
|
||||||
mbedtls_entropy_free(&entropy);
|
|
||||||
return (struct crypto_ecdh *)ctx;
|
return (struct crypto_ecdh *)ctx;
|
||||||
fail:
|
fail:
|
||||||
if (ctx) {
|
if (ctx) {
|
||||||
@@ -1168,8 +1115,6 @@ fail:
|
|||||||
os_free(ctx);
|
os_free(ctx);
|
||||||
ctx = NULL;
|
ctx = NULL;
|
||||||
}
|
}
|
||||||
mbedtls_ctr_drbg_free(&ctr_drbg);
|
|
||||||
mbedtls_entropy_free(&entropy);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1217,18 +1162,6 @@ struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
mbedtls_ctr_drbg_context ctr_drbg;
|
|
||||||
mbedtls_entropy_context entropy;
|
|
||||||
|
|
||||||
/* Initialize CTR_DRBG context */
|
|
||||||
mbedtls_ctr_drbg_init(&ctr_drbg);
|
|
||||||
mbedtls_entropy_init(&entropy);
|
|
||||||
|
|
||||||
/* Seed and setup CTR_DRBG entropy source for future reseeds */
|
|
||||||
if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) != 0) {
|
|
||||||
wpa_printf(MSG_ERROR, "Seeding entropy source failed");
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
len_prime = ACCESS_ECDH(ctx, grp).pbits / 8;
|
len_prime = ACCESS_ECDH(ctx, grp).pbits / 8;
|
||||||
bn_x = crypto_bignum_init_set(key, len);
|
bn_x = crypto_bignum_init_set(key, len);
|
||||||
|
|
||||||
@@ -1287,7 +1220,7 @@ struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
|
|||||||
|
|
||||||
/* Calculate secret
|
/* Calculate secret
|
||||||
z = F(DH(x,Y)) */
|
z = F(DH(x,Y)) */
|
||||||
secret_key = mbedtls_ecdh_calc_secret(ctx, &olen, secret, len_prime, mbedtls_ctr_drbg_random, &ctr_drbg);
|
secret_key = mbedtls_ecdh_calc_secret(ctx, &olen, secret, len_prime, mbedtls_esp_random, NULL);
|
||||||
if (secret_key != 0) {
|
if (secret_key != 0) {
|
||||||
wpa_printf(MSG_ERROR, "Calculation of secret failed");
|
wpa_printf(MSG_ERROR, "Calculation of secret failed");
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
@@ -1302,8 +1235,6 @@ cleanup:
|
|||||||
crypto_ec_key_deinit(pkey);
|
crypto_ec_key_deinit(pkey);
|
||||||
crypto_bignum_deinit(bn_x, 1);
|
crypto_bignum_deinit(bn_x, 1);
|
||||||
crypto_ec_point_deinit(ec_pt, 1);
|
crypto_ec_point_deinit(ec_pt, 1);
|
||||||
mbedtls_ctr_drbg_free(&ctr_drbg);
|
|
||||||
mbedtls_entropy_free(&entropy);
|
|
||||||
return sh_secret;
|
return sh_secret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#ifdef ESP_PLATFORM
|
#ifdef ESP_PLATFORM
|
||||||
#include "mbedtls/bignum.h"
|
#include "mbedtls/bignum.h"
|
||||||
|
#include "mbedtls/esp_mbedtls_random.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "utils/includes.h"
|
#include "utils/includes.h"
|
||||||
@@ -14,9 +15,6 @@
|
|||||||
#include "common/defs.h"
|
#include "common/defs.h"
|
||||||
|
|
||||||
#ifdef CONFIG_CRYPTO_MBEDTLS
|
#ifdef CONFIG_CRYPTO_MBEDTLS
|
||||||
#include "mbedtls/entropy.h"
|
|
||||||
#include "mbedtls/ctr_drbg.h"
|
|
||||||
|
|
||||||
#include <mbedtls/error.h>
|
#include <mbedtls/error.h>
|
||||||
#include <mbedtls/x509_crt.h>
|
#include <mbedtls/x509_crt.h>
|
||||||
#include <mbedtls/platform.h>
|
#include <mbedtls/platform.h>
|
||||||
@@ -38,11 +36,6 @@ static void crypto_dump_verify_info(u32 flags)
|
|||||||
static void crypto_dump_verify_info(u32 flags) { }
|
static void crypto_dump_verify_info(u32 flags) { }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int crypto_rng_wrapper(void *ctx, unsigned char *buf, size_t len)
|
|
||||||
{
|
|
||||||
return os_get_random(buf, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
int crypto_verify_cert(const u8 *cert_start, int certlen, const u8 *ca_cert_start, int ca_certlen)
|
int crypto_verify_cert(const u8 *cert_start, int certlen, const u8 *ca_cert_start, int ca_certlen)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
@@ -125,7 +118,7 @@ struct crypto_private_key * crypto_private_key_import(const u8 *key,
|
|||||||
mbedtls_pk_init(pkey);
|
mbedtls_pk_init(pkey);
|
||||||
|
|
||||||
ret = mbedtls_pk_parse_key(pkey, key, len, (const unsigned char *)passwd,
|
ret = mbedtls_pk_parse_key(pkey, key, len, (const unsigned char *)passwd,
|
||||||
passwd ? os_strlen(passwd) : 0, crypto_rng_wrapper, NULL);
|
passwd ? os_strlen(passwd) : 0, mbedtls_esp_random, NULL);
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
wpa_printf(MSG_ERROR, "failed to parse private key");
|
wpa_printf(MSG_ERROR, "failed to parse private key");
|
||||||
@@ -190,35 +183,13 @@ int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
|
|||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
|
mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
|
||||||
const char *pers = "rsa_encrypt";
|
|
||||||
mbedtls_entropy_context *entropy = os_zalloc(sizeof(*entropy));
|
|
||||||
mbedtls_ctr_drbg_context *ctr_drbg = os_zalloc(sizeof(*ctr_drbg));
|
|
||||||
|
|
||||||
if (!pkey || !entropy || !ctr_drbg) {
|
if (!pkey) {
|
||||||
if (entropy) {
|
|
||||||
os_free(entropy);
|
|
||||||
}
|
|
||||||
if (ctr_drbg) {
|
|
||||||
os_free(ctr_drbg);
|
|
||||||
}
|
|
||||||
wpa_printf(MSG_ERROR, "failed to allocate memory");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
mbedtls_entropy_init(entropy);
|
ret = mbedtls_rsa_pkcs1_encrypt(mbedtls_pk_rsa(*pkey), mbedtls_esp_random,
|
||||||
mbedtls_ctr_drbg_init(ctr_drbg);
|
NULL, inlen, in, out);
|
||||||
|
|
||||||
ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func,
|
|
||||||
entropy, (const unsigned char *) pers,
|
|
||||||
strlen(pers));
|
|
||||||
if (ret != 0) {
|
|
||||||
wpa_printf(MSG_ERROR, " failed ! mbedtls_ctr_drbg_seed returned %d",
|
|
||||||
ret);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = mbedtls_rsa_pkcs1_encrypt(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random,
|
|
||||||
ctr_drbg, inlen, in, out);
|
|
||||||
|
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
wpa_printf(MSG_ERROR, " failed ! mbedtls_rsa_pkcs1_encrypt returned -0x%04x", -ret);
|
wpa_printf(MSG_ERROR, " failed ! mbedtls_rsa_pkcs1_encrypt returned -0x%04x", -ret);
|
||||||
@@ -227,11 +198,6 @@ int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
|
|||||||
*outlen = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey));
|
*outlen = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey));
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
mbedtls_ctr_drbg_free(ctr_drbg);
|
|
||||||
mbedtls_entropy_free(entropy);
|
|
||||||
os_free(entropy);
|
|
||||||
os_free(ctr_drbg);
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,40 +208,18 @@ int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key,
|
|||||||
int ret;
|
int ret;
|
||||||
size_t i;
|
size_t i;
|
||||||
mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
|
mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
|
||||||
const char *pers = "rsa_decrypt";
|
|
||||||
mbedtls_entropy_context *entropy = os_malloc(sizeof(*entropy));
|
|
||||||
mbedtls_ctr_drbg_context *ctr_drbg = os_malloc(sizeof(*ctr_drbg));
|
|
||||||
|
|
||||||
if (!pkey || !entropy || !ctr_drbg) {
|
if (!pkey) {
|
||||||
if (entropy) {
|
|
||||||
os_free(entropy);
|
|
||||||
}
|
|
||||||
if (ctr_drbg) {
|
|
||||||
os_free(ctr_drbg);
|
|
||||||
}
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
mbedtls_ctr_drbg_init(ctr_drbg);
|
|
||||||
mbedtls_entropy_init(entropy);
|
|
||||||
ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func,
|
|
||||||
entropy, (const unsigned char *) pers,
|
|
||||||
strlen(pers));
|
|
||||||
|
|
||||||
if (ret < 0) {
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
i = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey));
|
i = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey));
|
||||||
ret = mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random,
|
ret = mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_pk_rsa(*pkey), mbedtls_esp_random,
|
||||||
ctr_drbg, &i, in, out, *outlen);
|
NULL, &i, in, out, *outlen);
|
||||||
|
|
||||||
|
if (ret == 0) {
|
||||||
*outlen = i;
|
*outlen = i;
|
||||||
|
}
|
||||||
cleanup:
|
|
||||||
mbedtls_ctr_drbg_free(ctr_drbg);
|
|
||||||
mbedtls_entropy_free(entropy);
|
|
||||||
os_free(entropy);
|
|
||||||
os_free(ctr_drbg);
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -285,27 +229,13 @@ int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
|
|||||||
u8 *out, size_t *outlen)
|
u8 *out, size_t *outlen)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
const char *pers = "rsa_encrypt";
|
|
||||||
mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
|
mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
|
||||||
mbedtls_entropy_context *entropy = os_malloc(sizeof(*entropy));
|
|
||||||
mbedtls_ctr_drbg_context *ctr_drbg = os_malloc(sizeof(*ctr_drbg));
|
|
||||||
|
|
||||||
if (!pkey || !entropy || !ctr_drbg) {
|
if (!pkey) {
|
||||||
if (entropy) {
|
|
||||||
os_free(entropy);
|
|
||||||
}
|
|
||||||
if (ctr_drbg) {
|
|
||||||
os_free(ctr_drbg);
|
|
||||||
}
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
mbedtls_ctr_drbg_init(ctr_drbg);
|
|
||||||
mbedtls_entropy_init(entropy);
|
|
||||||
ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func,
|
|
||||||
entropy, (const unsigned char *) pers,
|
|
||||||
strlen(pers));
|
|
||||||
|
|
||||||
if ((ret = mbedtls_rsa_pkcs1_sign(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random, ctr_drbg,
|
if ((ret = mbedtls_rsa_pkcs1_sign(mbedtls_pk_rsa(*pkey), mbedtls_esp_random, NULL,
|
||||||
(mbedtls_pk_rsa(*pkey))->MBEDTLS_PRIVATE(hash_id),
|
(mbedtls_pk_rsa(*pkey))->MBEDTLS_PRIVATE(hash_id),
|
||||||
inlen, in, out)) != 0) {
|
inlen, in, out)) != 0) {
|
||||||
wpa_printf(MSG_ERROR, " failed ! mbedtls_rsa_pkcs1_sign returned %d", ret);
|
wpa_printf(MSG_ERROR, " failed ! mbedtls_rsa_pkcs1_sign returned %d", ret);
|
||||||
@@ -314,10 +244,6 @@ int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
|
|||||||
*outlen = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey));
|
*outlen = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey));
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
mbedtls_ctr_drbg_free(ctr_drbg);
|
|
||||||
mbedtls_entropy_free(entropy);
|
|
||||||
os_free(entropy);
|
|
||||||
os_free(ctr_drbg);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -15,8 +15,6 @@
|
|||||||
#include "sha256.h"
|
#include "sha256.h"
|
||||||
|
|
||||||
#include "mbedtls/ecp.h"
|
#include "mbedtls/ecp.h"
|
||||||
#include "mbedtls/entropy.h"
|
|
||||||
#include "mbedtls/ctr_drbg.h"
|
|
||||||
#include "mbedtls/md.h"
|
#include "mbedtls/md.h"
|
||||||
#include "mbedtls/aes.h"
|
#include "mbedtls/aes.h"
|
||||||
#include "mbedtls/bignum.h"
|
#include "mbedtls/bignum.h"
|
||||||
|
@@ -13,6 +13,8 @@
|
|||||||
#include "crypto/sha256.h"
|
#include "crypto/sha256.h"
|
||||||
#include "crypto/sha384.h"
|
#include "crypto/sha384.h"
|
||||||
|
|
||||||
|
#include "mbedtls/esp_mbedtls_random.h"
|
||||||
|
|
||||||
/* TODO: Remove this once the appropriate solution is found
|
/* TODO: Remove this once the appropriate solution is found
|
||||||
*
|
*
|
||||||
* ssl_misc.h header uses private elements from
|
* ssl_misc.h header uses private elements from
|
||||||
@@ -24,8 +26,6 @@
|
|||||||
// located at mbedtls/library/ssl_misc.h
|
// located at mbedtls/library/ssl_misc.h
|
||||||
#include "ssl_misc.h"
|
#include "ssl_misc.h"
|
||||||
|
|
||||||
#include "mbedtls/ctr_drbg.h"
|
|
||||||
#include "mbedtls/entropy.h"
|
|
||||||
#include "mbedtls/debug.h"
|
#include "mbedtls/debug.h"
|
||||||
#include "mbedtls/oid.h"
|
#include "mbedtls/oid.h"
|
||||||
#ifdef ESPRESSIF_USE
|
#ifdef ESPRESSIF_USE
|
||||||
@@ -75,8 +75,6 @@ struct tls_data {
|
|||||||
|
|
||||||
typedef struct tls_context {
|
typedef struct tls_context {
|
||||||
mbedtls_ssl_context ssl; /*!< TLS/SSL context */
|
mbedtls_ssl_context ssl; /*!< TLS/SSL context */
|
||||||
mbedtls_entropy_context entropy; /*!< mbedTLS entropy context structure */
|
|
||||||
mbedtls_ctr_drbg_context ctr_drbg; /*!< mbedTLS ctr drbg context structure */
|
|
||||||
mbedtls_ssl_config conf; /*!< TLS/SSL config to be shared structures */
|
mbedtls_ssl_config conf; /*!< TLS/SSL config to be shared structures */
|
||||||
mbedtls_x509_crt cacert; /*!< Container for X.509 CA certificate */
|
mbedtls_x509_crt cacert; /*!< Container for X.509 CA certificate */
|
||||||
mbedtls_x509_crt *cacert_ptr; /*!< Pointer to the cacert being used. */
|
mbedtls_x509_crt *cacert_ptr; /*!< Pointer to the cacert being used. */
|
||||||
@@ -105,9 +103,7 @@ static void tls_mbedtls_cleanup(tls_context_t *tls)
|
|||||||
mbedtls_x509_crt_free(&tls->cacert);
|
mbedtls_x509_crt_free(&tls->cacert);
|
||||||
mbedtls_x509_crt_free(&tls->clientcert);
|
mbedtls_x509_crt_free(&tls->clientcert);
|
||||||
mbedtls_pk_free(&tls->clientkey);
|
mbedtls_pk_free(&tls->clientkey);
|
||||||
mbedtls_entropy_free(&tls->entropy);
|
|
||||||
mbedtls_ssl_config_free(&tls->conf);
|
mbedtls_ssl_config_free(&tls->conf);
|
||||||
mbedtls_ctr_drbg_free(&tls->ctr_drbg);
|
|
||||||
mbedtls_ssl_free(&tls->ssl);
|
mbedtls_ssl_free(&tls->ssl);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,7 +177,7 @@ static int set_pki_context(tls_context_t *tls, const struct tls_connection_param
|
|||||||
|
|
||||||
ret = mbedtls_pk_parse_key(&tls->clientkey, cfg->private_key_blob, cfg->private_key_blob_len,
|
ret = mbedtls_pk_parse_key(&tls->clientkey, cfg->private_key_blob, cfg->private_key_blob_len,
|
||||||
(const unsigned char *)cfg->private_key_passwd,
|
(const unsigned char *)cfg->private_key_passwd,
|
||||||
cfg->private_key_passwd ? os_strlen(cfg->private_key_passwd) : 0, mbedtls_ctr_drbg_random, &tls->ctr_drbg);
|
cfg->private_key_passwd ? os_strlen(cfg->private_key_passwd) : 0, mbedtls_esp_random, NULL);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
wpa_printf(MSG_ERROR, "mbedtls_pk_parse_keyfile returned -0x%x", -ret);
|
wpa_printf(MSG_ERROR, "mbedtls_pk_parse_keyfile returned -0x%x", -ret);
|
||||||
return ret;
|
return ret;
|
||||||
@@ -599,9 +595,7 @@ static int tls_create_mbedtls_handle(struct tls_connection *conn,
|
|||||||
assert(tls != NULL);
|
assert(tls != NULL);
|
||||||
|
|
||||||
mbedtls_ssl_init(&tls->ssl);
|
mbedtls_ssl_init(&tls->ssl);
|
||||||
mbedtls_ctr_drbg_init(&tls->ctr_drbg);
|
|
||||||
mbedtls_ssl_config_init(&tls->conf);
|
mbedtls_ssl_config_init(&tls->conf);
|
||||||
mbedtls_entropy_init(&tls->entropy);
|
|
||||||
|
|
||||||
ret = set_client_config(params, tls);
|
ret = set_client_config(params, tls);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
@@ -609,14 +603,7 @@ static int tls_create_mbedtls_handle(struct tls_connection *conn,
|
|||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = mbedtls_ctr_drbg_seed(&tls->ctr_drbg, mbedtls_entropy_func,
|
mbedtls_ssl_conf_rng(&tls->conf, mbedtls_esp_random, NULL);
|
||||||
&tls->entropy, NULL, 0);
|
|
||||||
if (ret != 0) {
|
|
||||||
wpa_printf(MSG_ERROR, "mbedtls_ctr_drbg_seed returned -0x%x", -ret);
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
mbedtls_ssl_conf_rng(&tls->conf, mbedtls_ctr_drbg_random, &tls->ctr_drbg);
|
|
||||||
|
|
||||||
#if defined(CONFIG_MBEDTLS_SSL_PROTO_TLS1_3) && !defined(CONFIG_TLSV13)
|
#if defined(CONFIG_MBEDTLS_SSL_PROTO_TLS1_3) && !defined(CONFIG_TLSV13)
|
||||||
/* Disable TLSv1.3 even when enabled in MbedTLS and not enabled in WiFi config.
|
/* Disable TLSv1.3 even when enabled in MbedTLS and not enabled in WiFi config.
|
||||||
|
Reference in New Issue
Block a user