nDPId decryption example

Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
This commit is contained in:
Toni Uhlig
2024-12-19 15:07:54 +01:00
parent 2c81f116bf
commit 7d94632811
4 changed files with 18 additions and 7 deletions

View File

@@ -105,10 +105,11 @@ int udp_server(struct ncrypt * const nc)
int bytes_read = ncrypt_decrypt_recv(nc, sock_fd, &read_buf);
if (bytes_read <= 0)
{
logger(1, "Crypto error: %d", bytes_read);
break;
}
printf("read %d bytes\n", bytes_read);
printf("read %d bytes: %.*s", bytes_read, (int)read_buf.data_used, read_buf.plaintext.data);
}
return 0;
@@ -142,7 +143,7 @@ int main(int argc, char ** argv)
return 1;
}
struct ncrypt nc;
struct ncrypt nc = {};
{
int ret;
unsigned char priv_key[NCRYPT_X25519_KEYLEN];
@@ -173,6 +174,12 @@ int main(int argc, char ** argv)
logger_early(1, "Crypto initialization failed: %d", ret);
return 1;
}
ret = ncrypt_init_decrypt(&nc);
if (ret != 0)
{
logger_early(1, "Crypto decrypt initialization failed: %d", ret);
return 1;
}
}
return udp_server(&nc);

View File

@@ -1673,7 +1673,8 @@ static int ncrypt_selftest()
{
ret++;
}
if (ncrypt_init_decrypt(&nc_peer2, nc_peer1.iv) != 0)
memcpy(&nc_peer2.iv, &nc_peer1.iv, sizeof(nc_peer1.iv));
if (ncrypt_init_decrypt(&nc_peer2) != 0)
{
ret++;
}

View File

@@ -278,7 +278,7 @@ int ncrypt_init_encrypt(struct ncrypt * const nc)
return 0;
}
int ncrypt_init_decrypt(struct ncrypt * const nc, unsigned char iv[NCRYPT_AES_IVLEN])
int ncrypt_init_decrypt(struct ncrypt * const nc)
{
if (nc->aesctx == NULL)
{
@@ -299,8 +299,6 @@ int ncrypt_init_decrypt(struct ncrypt * const nc, unsigned char iv[NCRYPT_AES_IV
}
}
memcpy(nc->iv, iv, NCRYPT_AES_IVLEN);
if (EVP_DecryptInit_ex(nc->aesctx, NULL, NULL, nc->shared_secret, nc->iv) == 0)
{
return -4;

View File

@@ -56,6 +56,11 @@ struct ncrypt_buffer
size_t write_offset; // partial write; offset to next bytes of data
};
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
_Static_assert(sizeof(((struct ncrypt_buffer *)0)->encrypted) == sizeof(((struct ncrypt_buffer *)0)->encrypted.raw),
"Raw buffer and iv/tag/data sizes differ");
#endif
int ncrypt_keygen(unsigned char priv_key[NCRYPT_X25519_KEYLEN], unsigned char pub_key[NCRYPT_X25519_KEYLEN]);
int ncrypt_load_privkey(char const * const private_key_file, unsigned char priv_key[NCRYPT_X25519_KEYLEN]);
@@ -68,7 +73,7 @@ int ncrypt_init(struct ncrypt * const nc,
int ncrypt_init_encrypt(struct ncrypt * const nc);
int ncrypt_init_decrypt(struct ncrypt * const nc, unsigned char iv[NCRYPT_AES_IVLEN]);
int ncrypt_init_decrypt(struct ncrypt * const nc);
void ncrypt_free(struct ncrypt * const nc);