1
0

Another change of main to show an example of key exchange and data encryption.

This commit is contained in:
Alexander Andreev 2024-03-24 04:41:08 +04:00
parent acc016f10b
commit 8df5a6344e
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34

View File

@ -1,7 +1,6 @@
#include <sodium/utils.h> #include <sodium/utils.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include "audio.h" #include "audio.h"
#include "crypt.h" #include "crypt.h"
@ -21,61 +20,89 @@ int main(int argc, char **argv) {
return -1; return -1;
} }
crypt_key_t own, ownr; crypt_key_t own, remote;
if (crypt_key_gen(&own) < 0) { if (crypt_key_gen(&own) < 0) {
fprintf(stderr, "Failed to generate the keys."); fprintf(stderr, "Failed to generate the keys.");
return -1; return -1;
} }
FILE *fp = fopen("own_pub_key", "w+"); if (crypt_key_gen(&remote) < 0) {
FILE *fs = fopen("own_sec_key", "w+"); fprintf(stderr, "Failed to generate the keys.");
return -1;
if (fp == NULL) {
fprintf(stderr, "fp err: %s\n", strerror(errno));
return 0;
} }
if (fs == NULL) { unsigned char *oh = crypt_hello(&own);
fprintf(stderr, "fs err: %s\n", strerror(errno)); unsigned char *rh = crypt_hello(&remote);
return 0;
int rt = 0;
if (crypt_hello_verify(oh, &own) < 0) {
fprintf(stderr, "oh v fail");
rt = -1;
goto cleanup;
} }
char phex[CRYPT_PKEY_HEXLEN], shex[CRYPT_SKEY_HEXLEN]; if (crypt_hello_verify(rh, &remote) < 0) {
fprintf(stderr, "rh v fail");
rt = -1;
goto cleanup;
}
crypt_key_export_public(&own, phex); const unsigned char *nonce_own = crypt_hello_get_nonce(oh, rh, false);
crypt_key_export_secret(&own, shex); const unsigned char *nonce_rem = crypt_hello_get_nonce(rh, oh, true);
crypt_store_key(&own, fp, fs); for (unsigned int i = 0; i < CRYPT_NONCE_LEN; ++i)
if (nonce_own[i] != nonce_rem[i]) {
fprintf(stderr, "A nonce mismatch occured.");
rt = -1;
goto cleanup;
}
crypt_session_t os, rs;
fflush(fp); if (crypt_session_init(&os, &own, &remote, nonce_own, true) < 0) {
fflush(fs); rt = -1;
fseek(fp, 0, SEEK_SET); goto cleanup;
fseek(fs, 0, SEEK_SET); }
crypt_load_key(&ownr, fp, fs); if (crypt_session_init(&rs, &remote, &own, nonce_rem, false) < 0) {
rt = -1;
goto cleanup;
}
char phexr[CRYPT_PKEY_HEXLEN], shexr[CRYPT_SKEY_HEXLEN]; char msg[] = "Hello!";
unsigned long long msglen = 6;
crypt_key_export_public(&ownr, phexr); unsigned long long ocl = 0;
crypt_key_export_secret(&ownr, shexr); unsigned char *oc = crypt_session_encrypt(&os, (unsigned char *)msg, msglen, &ocl);
fclose(fp); if (oc == NULL) {
fclose(fs); fprintf(stderr, "oc is null");
rt = -1;
goto cleanup;
}
unsigned char *h = crypt_hello(&own); unsigned long long rml = 0;
char *rmsg = (char *)crypt_session_decrypt(&rs, oc, ocl, &rml);
int o = crypt_hello_verify(h, &ownr); if (rmsg == NULL) {
printf("o = %i\n", o); rt = -1;
goto cleanup;
}
if (strcmp(msg, rmsg) != 0)
fprintf(stderr, "Yikes!");
cleanup:
free(oh);
free(rh);
crypt_key_destroy(&own); crypt_key_destroy(&own);
crypt_key_destroy(&ownr); crypt_key_destroy(&remote);
sodium_memzero(phex, CRYPT_PKEY_HEXLEN); crypt_session_destroy(&os);
sodium_memzero(phexr, CRYPT_PKEY_HEXLEN); crypt_session_destroy(&rs);
sodium_memzero(shex, CRYPT_SKEY_HEXLEN);
sodium_memzero(shexr, CRYPT_SKEY_HEXLEN);
// audio_t aud; // audio_t aud;
@ -93,5 +120,5 @@ int main(int argc, char **argv) {
// audio_destroy(&aud); // audio_destroy(&aud);
return 0; return rt;
} }