Browse Source

Исправлены ошибки компиляции в secure_channel.c

v2_dev
Evgeny 2 months ago
parent
commit
3491fac812
  1. 31
      src/secure_channel.c

31
src/secure_channel.c

@ -7,14 +7,15 @@
#include <tinycrypt/ccm_mode.h> #include <tinycrypt/ccm_mode.h>
#include <tinycrypt/constants.h> #include <tinycrypt/constants.h>
#include <tinycrypt/ecc_platform_specific.h> #include <tinycrypt/ecc_platform_specific.h>
#include <tinycrypt/sha256.h>
#include <string.h> #include <string.h>
#include <stddef.h> #include <stddef.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <sys/time.h> #include <sys/time.h>
#include "crc32.h" #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include "sha256.h" #include "crc32.h"
static const struct uECC_Curve_t *curve = NULL; static const struct uECC_Curve_t *curve = NULL;
static uint8_t sc_urandom_seed[8] = {0}; 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); 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; return SC_ERR_INVALID_ARG;
} }
@ -81,7 +82,7 @@ sc_status_t sc_generate_keypair(struct SC_MYKEYS *ctx)
/* Set custom RNG function */ /* Set custom RNG function */
uECC_set_rng(sc_rng); 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_ERR_CRYPTO;
} }
return SC_OK; 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) { 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; 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 */ /* 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)) { shared_secret, curve)) {
return SC_ERR_CRYPTO; 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) 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]; uint8_t hash[32];
struct timeval tv; struct timeval tv;
uint8_t data[8 + 8 + 4]; 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[18] = (tv.tv_sec >> 16) & 0xFF;
data[19] = (tv.tv_sec >> 24) & 0xFF; data[19] = (tv.tv_sec >> 24) & 0xFF;
sha256_init(&sha_ctx); tc_sha256_init(&sha_ctx);
sha256_update(&sha_ctx, data, 20); tc_sha256_update(&sha_ctx, data, 20);
sha256_final(&sha_ctx, hash); tc_sha256_final(hash, &sha_ctx);
memcpy(nonce_out, hash, SC_NONCE_SIZE); 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]; uint8_t nonce[SC_NONCE_SIZE];
struct tc_aes_key_sched_struct sched; struct tc_aes_key_sched_struct sched;
struct tc_ccm_mode_struct ccm_state; struct tc_ccm_mode_struct ccm_state;
TCCcmMode_t c = &ccm_state;
size_t total_plaintext_len = plaintext_len + SC_CRC32_SIZE; size_t total_plaintext_len = plaintext_len + SC_CRC32_SIZE;
uint8_t plaintext_with_crc[total_plaintext_len]; uint8_t plaintext_with_crc[total_plaintext_len];
uint8_t combined_output[total_plaintext_len + SC_TAG_SIZE]; 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); sc_build_nonce(ctx->tx_counter, nonce);
/* Configure CCM mode */ /* 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; 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), if (tc_ccm_generation_encryption(combined_output, sizeof(combined_output),
NULL, 0, /* no associated data */ NULL, 0, /* no associated data */
plaintext_with_crc, total_plaintext_len, plaintext_with_crc, total_plaintext_len,
c) != TC_CRYPTO_SUCCESS) { &ccm_state) != TC_CRYPTO_SUCCESS) {
return SC_ERR_CRYPTO; return SC_ERR_CRYPTO;
} }

Loading…
Cancel
Save