Another change of main to show an example of key exchange and data encryption.
This commit is contained in:
parent
acc016f10b
commit
8df5a6344e
95
src/main.c
95
src/main.c
@ -1,7 +1,6 @@
|
||||
#include <sodium/utils.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "audio.h"
|
||||
#include "crypt.h"
|
||||
@ -21,61 +20,89 @@ int main(int argc, char **argv) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
crypt_key_t own, ownr;
|
||||
crypt_key_t own, remote;
|
||||
|
||||
if (crypt_key_gen(&own) < 0) {
|
||||
fprintf(stderr, "Failed to generate the keys.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
FILE *fp = fopen("own_pub_key", "w+");
|
||||
FILE *fs = fopen("own_sec_key", "w+");
|
||||
|
||||
if (fp == NULL) {
|
||||
fprintf(stderr, "fp err: %s\n", strerror(errno));
|
||||
return 0;
|
||||
if (crypt_key_gen(&remote) < 0) {
|
||||
fprintf(stderr, "Failed to generate the keys.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fs == NULL) {
|
||||
fprintf(stderr, "fs err: %s\n", strerror(errno));
|
||||
return 0;
|
||||
unsigned char *oh = crypt_hello(&own);
|
||||
unsigned char *rh = crypt_hello(&remote);
|
||||
|
||||
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);
|
||||
crypt_key_export_secret(&own, shex);
|
||||
const unsigned char *nonce_own = crypt_hello_get_nonce(oh, rh, false);
|
||||
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);
|
||||
fflush(fs);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
fseek(fs, 0, SEEK_SET);
|
||||
if (crypt_session_init(&os, &own, &remote, nonce_own, true) < 0) {
|
||||
rt = -1;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
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);
|
||||
crypt_key_export_secret(&ownr, shexr);
|
||||
unsigned long long ocl = 0;
|
||||
unsigned char *oc = crypt_session_encrypt(&os, (unsigned char *)msg, msglen, &ocl);
|
||||
|
||||
fclose(fp);
|
||||
fclose(fs);
|
||||
if (oc == NULL) {
|
||||
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);
|
||||
printf("o = %i\n", o);
|
||||
if (rmsg == NULL) {
|
||||
rt = -1;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (strcmp(msg, rmsg) != 0)
|
||||
fprintf(stderr, "Yikes!");
|
||||
|
||||
cleanup:
|
||||
free(oh);
|
||||
free(rh);
|
||||
|
||||
crypt_key_destroy(&own);
|
||||
crypt_key_destroy(&ownr);
|
||||
crypt_key_destroy(&remote);
|
||||
|
||||
sodium_memzero(phex, CRYPT_PKEY_HEXLEN);
|
||||
sodium_memzero(phexr, CRYPT_PKEY_HEXLEN);
|
||||
sodium_memzero(shex, CRYPT_SKEY_HEXLEN);
|
||||
sodium_memzero(shexr, CRYPT_SKEY_HEXLEN);
|
||||
crypt_session_destroy(&os);
|
||||
crypt_session_destroy(&rs);
|
||||
|
||||
// audio_t aud;
|
||||
|
||||
@ -93,5 +120,5 @@ int main(int argc, char **argv) {
|
||||
|
||||
// audio_destroy(&aud);
|
||||
|
||||
return 0;
|
||||
return rt;
|
||||
}
|
Loading…
Reference in New Issue
Block a user