From cd61c5b678d463bc0911c77da676e40e4e06af5d Mon Sep 17 00:00:00 2001 From: Evgeny Date: Tue, 20 Jan 2026 00:36:31 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=BF=D0=B8=D0=BB=D1=8F=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=B2=20secure=5Fchannel.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/secure_channel.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/secure_channel.c b/src/secure_channel.c index 3719b2f8d..c97a4e18b 100644 --- a/src/secure_channel.c +++ b/src/secure_channel.c @@ -7,14 +7,15 @@ #include #include #include +#include #include #include #include #include #include -#include "crc32.h" +#include #include -#include "sha256.h" +#include "crc32.h" static const struct uECC_Curve_t *curve = NULL; static uint8_t sc_urandom_seed[8] = {0}; @@ -68,9 +69,9 @@ static int sc_validate_key(const uint8_t *public_key) return uECC_valid_public_key(public_key, curve); } -sc_status_t sc_generate_keypair(struct SC_MYKEYS *ctx) +sc_status_t sc_generate_keypair(struct SC_MYKEYS *pk) { - if (!ctx) { + if (!pk) { return SC_ERR_INVALID_ARG; } @@ -81,7 +82,7 @@ sc_status_t sc_generate_keypair(struct SC_MYKEYS *ctx) /* Set custom RNG function */ uECC_set_rng(sc_rng); - if (!uECC_make_key(ctx->pk->public_key, ctx->pk->private_key, curve)) { + if (!uECC_make_key(pk->public_key, pk->private_key, curve)) { return SC_ERR_CRYPTO; } return SC_OK; @@ -100,7 +101,7 @@ static int hex_to_binary(const char *hex_str, uint8_t *binary, size_t binary_len } sc_status_t sc_init_local_keys(struct SC_MYKEYS *mykeys, const char *public_key, const char *private_key) { - if (!ctx || !public_key || !private_key) { + if (!mykeys || !public_key || !private_key) { return SC_ERR_INVALID_ARG; } @@ -157,7 +158,10 @@ sc_status_t sc_set_peer_public_key(sc_context_t *ctx, const char *peer_public_ke } /* Compute shared secret using ECDH */ - if (!uECC_shared_secret(peer_public_key, ctx->private_key, + if (!ctx->pk) { + return SC_ERR_NOT_INITIALIZED; + } + if (!uECC_shared_secret(peer_public_key, ctx->pk->private_key, shared_secret, curve)) { return SC_ERR_CRYPTO; } @@ -176,7 +180,7 @@ sc_status_t sc_set_peer_public_key(sc_context_t *ctx, const char *peer_public_ke static void sc_build_nonce(uint64_t counter, uint8_t *nonce_out) { - SHA256_CTX sha_ctx; + struct tc_sha256_state_struct sha_ctx; uint8_t hash[32]; struct timeval tv; uint8_t data[8 + 8 + 4]; @@ -201,9 +205,9 @@ static void sc_build_nonce(uint64_t counter, uint8_t *nonce_out) data[18] = (tv.tv_sec >> 16) & 0xFF; data[19] = (tv.tv_sec >> 24) & 0xFF; - sha256_init(&sha_ctx); - sha256_update(&sha_ctx, data, 20); - sha256_final(&sha_ctx, hash); + tc_sha256_init(&sha_ctx); + tc_sha256_update(&sha_ctx, data, 20); + tc_sha256_final(hash, &sha_ctx); memcpy(nonce_out, hash, SC_NONCE_SIZE); } @@ -217,7 +221,6 @@ sc_status_t sc_encrypt(sc_context_t *ctx, uint8_t nonce[SC_NONCE_SIZE]; struct tc_aes_key_sched_struct sched; struct tc_ccm_mode_struct ccm_state; - TCCcmMode_t c = &ccm_state; size_t total_plaintext_len = plaintext_len + SC_CRC32_SIZE; uint8_t plaintext_with_crc[total_plaintext_len]; uint8_t combined_output[total_plaintext_len + SC_TAG_SIZE]; @@ -251,7 +254,7 @@ sc_status_t sc_encrypt(sc_context_t *ctx, sc_build_nonce(ctx->tx_counter, nonce); /* Configure CCM mode */ - if (tc_ccm_config(c, &sched, nonce, SC_NONCE_SIZE, SC_TAG_SIZE) != TC_CRYPTO_SUCCESS) { + if (tc_ccm_config(&ccm_state, &sched, nonce, SC_NONCE_SIZE, SC_TAG_SIZE) != TC_CRYPTO_SUCCESS) { return SC_ERR_CRYPTO; } @@ -259,7 +262,7 @@ sc_status_t sc_encrypt(sc_context_t *ctx, if (tc_ccm_generation_encryption(combined_output, sizeof(combined_output), NULL, 0, /* no associated data */ plaintext_with_crc, total_plaintext_len, - c) != TC_CRYPTO_SUCCESS) { + &ccm_state) != TC_CRYPTO_SUCCESS) { return SC_ERR_CRYPTO; }