Refactor: Updated SHA256 implementation and secure channel improvements
This commit is contained in:
1 parent
f022e7105e
commit
07d4616aea
14 files changed
+220
-194
No files matched your search
@@ -75,6 +75,8 @@ static void etcp_link_send_init(struct ETCP_LINK* link) {
|
||||
dgram->data[offset++] = link->local_link_id;
|
||||
|
||||
memcpy(dgram->data + offset, link->etcp->instance->my_keys.public_key, SC_PUBKEY_SIZE);
|
||||
// Шифруем public_key inplace используя собственный ключ
|
||||
sc_sha_transcode(link->etcp->crypto_ctx.peer_public_key, SC_PUBKEY_SIZE, dgram->data + offset, SC_PUBKEY_SIZE);
|
||||
dgram->data_len = offset + SC_PUBKEY_SIZE;
|
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Sending INIT request to link, node_id=%llu, retry=%d", (unsigned long long)node_id, link->init_retry_count);
|
||||
@@ -589,6 +591,9 @@ static void etcp_connections_read_callback(int fd, void* arg) {
|
||||
// printf("[ETCP DEBUG] Last 64 bytes of packet (PUBKEY): ");
|
||||
// for (int i=0; i<SC_PUBKEY_SIZE; i++) DEBUG_INFO(DEBUG_CATEGORY_CRYPTO, "%02x ", data[recv_len-SC_PUBKEY_SIZE+i]);
|
||||
|
||||
// Расшифровываем public_key inplace используя собственный ключ
|
||||
sc_sha_transcode(e_sock->instance->my_keys.public_key, SC_PUBKEY_SIZE, data + recv_len - SC_PUBKEY_SIZE, SC_PUBKEY_SIZE);
|
||||
|
||||
if (sc_set_peer_public_key(&sc, &data[recv_len-SC_PUBKEY_SIZE], SC_PEER_PUBKEY_BIN)!=SC_OK) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "etcp_connections_read_callback: failed to set peer public key during init");
|
||||
errorcode=2;
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include "crc32.h"
|
||||
#include "../lib/sha256.h"
|
||||
|
||||
// To switch between implementations, define USE_OPENSSL before including/compiling.
|
||||
// If USE_OPENSSL is defined, use OpenSSL; otherwise, use TinyCrypt (original logic).
|
||||
@@ -803,3 +804,22 @@ sc_status_t sc_compute_public_key_from_private(const uint8_t *private_key, uint8
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
sc_status_t sc_sha_transcode(const uint8_t *key, size_t key_len, uint8_t *data, size_t data_len) {
|
||||
if (!key || !data || key_len == 0 || data_len == 0) {
|
||||
return SC_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
uint8_t sha_hash[SC_SHA256_BLOCK_SIZE];
|
||||
SC_SHA256_CTX ctx;
|
||||
|
||||
sc_sha256_init(&ctx);
|
||||
sc_sha256_update(&ctx, key, key_len);
|
||||
sc_sha256_final(&ctx, sha_hash);
|
||||
|
||||
for (size_t i = 0; i < data_len; i++) {
|
||||
data[i] ^= sha_hash[i % SC_SHA256_BLOCK_SIZE];
|
||||
}
|
||||
|
||||
return SC_OK;
|
||||
}
|
||||
@@ -65,4 +65,7 @@ sc_status_t sc_compute_public_key_from_private(const uint8_t *private_key, uint8
|
||||
sc_status_t sc_encrypt(sc_context_t *ctx, const uint8_t *plaintext, size_t plaintext_len, uint8_t *ciphertext, size_t *ciphertext_len);
|
||||
sc_status_t sc_decrypt(sc_context_t *ctx, const uint8_t *ciphertext, size_t ciphertext_len, uint8_t *plaintext, size_t *plaintext_len);
|
||||
|
||||
// SHA256-based transcode: XOR data with SHA256(key)
|
||||
sc_status_t sc_sha_transcode(const uint8_t *key, size_t key_len, uint8_t *data, size_t data_len);
|
||||
|
||||
#endif // SECURE_CHANNEL_H
|
||||
Reference in new issue
Block a user