Replace '< 0' with '== -1'. For the sake of optimisation! xD
This commit is contained in:
parent
e4b388f554
commit
16e0149802
30
src/crypt.c
30
src/crypt.c
@ -3,11 +3,11 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
int crypt_key_gen(crypt_key_t *const k) {
|
int crypt_key_gen(crypt_key_t *const k) {
|
||||||
if (crypto_kx_keypair(k->kx_pub, k->kx_sec) < 0) {
|
if (crypto_kx_keypair(k->kx_pub, k->kx_sec) == -1) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (crypto_sign_keypair(k->sign_pub, k->sign_sec) < 0) {
|
if (crypto_sign_keypair(k->sign_pub, k->sign_sec) == -1) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -17,13 +17,13 @@ int crypt_key_gen(crypt_key_t *const k) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int crypt_key_from_hex(crypt_key_t *const k, const char phex[CRYPT_PKEY_HEXLEN], const char shex[CRYPT_SKEY_HEXLEN]) {
|
int crypt_key_from_hex(crypt_key_t *const k, const char phex[CRYPT_PKEY_HEXLEN], const char shex[CRYPT_SKEY_HEXLEN]) {
|
||||||
if (sodium_hex2bin(k->kx_pub, CRYPT_KX_PKEY_LEN, phex, CRYPT_KX_PKEY_LEN * 2, NULL, NULL, NULL) < 0)
|
if (sodium_hex2bin(k->kx_pub, CRYPT_KX_PKEY_LEN, phex, CRYPT_KX_PKEY_LEN * 2, NULL, NULL, NULL) == -1)
|
||||||
return -1;
|
return -1;
|
||||||
if (sodium_hex2bin(k->kx_sec, CRYPT_KX_SKEY_LEN, shex, CRYPT_KX_SKEY_LEN * 2, NULL, NULL, NULL) < 0)
|
if (sodium_hex2bin(k->kx_sec, CRYPT_KX_SKEY_LEN, shex, CRYPT_KX_SKEY_LEN * 2, NULL, NULL, NULL) == -1)
|
||||||
return -1;
|
return -1;
|
||||||
if (sodium_hex2bin(k->sign_pub, CRYPT_SIGN_PKEY_LEN, phex+(CRYPT_KX_PKEY_LEN * 2), CRYPT_SIGN_PKEY_LEN * 2, NULL, NULL, NULL) < 0)
|
if (sodium_hex2bin(k->sign_pub, CRYPT_SIGN_PKEY_LEN, phex+(CRYPT_KX_PKEY_LEN * 2), CRYPT_SIGN_PKEY_LEN * 2, NULL, NULL, NULL) == -1)
|
||||||
return -1;
|
return -1;
|
||||||
if (sodium_hex2bin(k->sign_sec, CRYPT_SIGN_SKEY_LEN, shex+(CRYPT_KX_SKEY_LEN * 2), CRYPT_SIGN_SKEY_LEN * 2, NULL, NULL, NULL) < 0)
|
if (sodium_hex2bin(k->sign_sec, CRYPT_SIGN_SKEY_LEN, shex+(CRYPT_KX_SKEY_LEN * 2), CRYPT_SIGN_SKEY_LEN * 2, NULL, NULL, NULL) == -1)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
k->hasSecKey = true;
|
k->hasSecKey = true;
|
||||||
@ -32,9 +32,9 @@ int crypt_key_from_hex(crypt_key_t *const k, const char phex[CRYPT_PKEY_HEXLEN],
|
|||||||
}
|
}
|
||||||
|
|
||||||
int crypt_key_from_hex_public(crypt_key_t *const k, const char phex[CRYPT_PKEY_HEXLEN]) {
|
int crypt_key_from_hex_public(crypt_key_t *const k, const char phex[CRYPT_PKEY_HEXLEN]) {
|
||||||
if (sodium_hex2bin(k->kx_pub, CRYPT_KX_PKEY_LEN, phex, CRYPT_KX_PKEY_LEN * 2, NULL, NULL, NULL) < 0)
|
if (sodium_hex2bin(k->kx_pub, CRYPT_KX_PKEY_LEN, phex, CRYPT_KX_PKEY_LEN * 2, NULL, NULL, NULL) == -1)
|
||||||
return -1;
|
return -1;
|
||||||
if (sodium_hex2bin(k->sign_pub, CRYPT_SIGN_PKEY_LEN, phex+(CRYPT_KX_PKEY_LEN * 2), CRYPT_SIGN_PKEY_LEN * 2, NULL, NULL, NULL) < 0)
|
if (sodium_hex2bin(k->sign_pub, CRYPT_SIGN_PKEY_LEN, phex+(CRYPT_KX_PKEY_LEN * 2), CRYPT_SIGN_PKEY_LEN * 2, NULL, NULL, NULL) == -1)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
k->hasSecKey = false;
|
k->hasSecKey = false;
|
||||||
@ -90,9 +90,9 @@ int crypt_store_key(const crypt_key_t *const k, FILE *const pub, FILE *const sec
|
|||||||
|
|
||||||
char phex[CRYPT_PKEY_HEXLEN], shex[CRYPT_SKEY_HEXLEN];
|
char phex[CRYPT_PKEY_HEXLEN], shex[CRYPT_SKEY_HEXLEN];
|
||||||
|
|
||||||
if (crypt_key_export_public(k, phex) < 0)
|
if (crypt_key_export_public(k, phex) == -1)
|
||||||
return -1;
|
return -1;
|
||||||
if (crypt_key_export_secret(k, shex) < 0)
|
if (crypt_key_export_secret(k, shex) == -1)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (fwrite(phex, sizeof(phex[0]), CRYPT_PKEY_HEXLEN, pub) != CRYPT_PKEY_HEXLEN)
|
if (fwrite(phex, sizeof(phex[0]), CRYPT_PKEY_HEXLEN, pub) != CRYPT_PKEY_HEXLEN)
|
||||||
@ -106,7 +106,9 @@ int crypt_store_key(const crypt_key_t *const k, FILE *const pub, FILE *const sec
|
|||||||
unsigned char *crypt_hello(const crypt_key_t *const own) {
|
unsigned char *crypt_hello(const crypt_key_t *const own) {
|
||||||
unsigned char *hello = (unsigned char *)malloc(CRYPT_HELLO_LEN * sizeof(unsigned char));
|
unsigned char *hello = (unsigned char *)malloc(CRYPT_HELLO_LEN * sizeof(unsigned char));
|
||||||
|
|
||||||
crypt_key_export_public(own, (char *const)hello);
|
if (crypt_key_export_public(own, (char *const)hello) == -1) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (crypto_sign_detached(hello+CRYPT_PKEY_HEXLEN, NULL, (const unsigned char *)hello, CRYPT_PKEY_HEXLEN, own->sign_sec) == -1) {
|
if (crypto_sign_detached(hello+CRYPT_PKEY_HEXLEN, NULL, (const unsigned char *)hello, CRYPT_PKEY_HEXLEN, own->sign_sec) == -1) {
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -118,7 +120,7 @@ unsigned char *crypt_hello(const crypt_key_t *const own) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int crypt_hello_verify(const unsigned char *const hello, crypt_key_t *const remote) {
|
int crypt_hello_verify(const unsigned char *const hello, crypt_key_t *const remote) {
|
||||||
if (crypt_key_from_hex_public(remote, (const char *const)hello) < 0)
|
if (crypt_key_from_hex_public(remote, (const char *const)hello) == -1)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (crypto_sign_verify_detached(hello+CRYPT_PKEY_HEXLEN, (const unsigned char *const)hello, CRYPT_PKEY_HEXLEN, remote->sign_pub))
|
if (crypto_sign_verify_detached(hello+CRYPT_PKEY_HEXLEN, (const unsigned char *const)hello, CRYPT_PKEY_HEXLEN, remote->sign_pub))
|
||||||
@ -166,7 +168,9 @@ int crypt_session_init(crypt_session_t *const s, const crypt_key_t *const own, c
|
|||||||
unsigned char *crypt_session_encrypt(crypt_session_t *const s, const unsigned char *const m, unsigned long long mlen, unsigned long long *clen) {
|
unsigned char *crypt_session_encrypt(crypt_session_t *const s, const unsigned char *const m, unsigned long long mlen, unsigned long long *clen) {
|
||||||
unsigned char *c = (unsigned char *)malloc((mlen + crypto_aead_aegis256_ABYTES) * sizeof(unsigned char));
|
unsigned char *c = (unsigned char *)malloc((mlen + crypto_aead_aegis256_ABYTES) * sizeof(unsigned char));
|
||||||
|
|
||||||
crypto_aead_aegis256_encrypt(c, clen, m, mlen, NULL, 0, NULL, s->nonce, s->tx);
|
if (crypto_aead_aegis256_encrypt(c, clen, m, mlen, NULL, 0, NULL, s->nonce, s->tx) == -1) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
14
src/main.c
14
src/main.c
@ -14,19 +14,19 @@ int main(int argc, char **argv) {
|
|||||||
(void)argv;
|
(void)argv;
|
||||||
(void)argc;
|
(void)argc;
|
||||||
|
|
||||||
if (sodium_init() < 0) {
|
if (sodium_init() == -1) {
|
||||||
fprintf(stderr, "A Sodium cryptography library is failed to initialise!\n");
|
fprintf(stderr, "A Sodium cryptography library is failed to initialise!\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
crypt_key_t own, remote;
|
crypt_key_t own, remote;
|
||||||
|
|
||||||
if (crypt_key_gen(&own) < 0) {
|
if (crypt_key_gen(&own) == -1) {
|
||||||
fprintf(stderr, "Failed to generate the keys.");
|
fprintf(stderr, "Failed to generate the keys.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (crypt_key_gen(&remote) < 0) {
|
if (crypt_key_gen(&remote) == -1) {
|
||||||
fprintf(stderr, "Failed to generate the keys.");
|
fprintf(stderr, "Failed to generate the keys.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -42,13 +42,13 @@ int main(int argc, char **argv) {
|
|||||||
crypt_session_t os, rs;
|
crypt_session_t os, rs;
|
||||||
unsigned char *nonce_own = NULL, *nonce_rem = NULL;
|
unsigned char *nonce_own = NULL, *nonce_rem = NULL;
|
||||||
|
|
||||||
if (crypt_hello_verify(oh, &own) < 0) {
|
if (crypt_hello_verify(oh, &own) == -1) {
|
||||||
fprintf(stderr, "oh v fail");
|
fprintf(stderr, "oh v fail");
|
||||||
rt = -1;
|
rt = -1;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (crypt_hello_verify(rh, &remote) < 0) {
|
if (crypt_hello_verify(rh, &remote) == -1) {
|
||||||
fprintf(stderr, "rh v fail");
|
fprintf(stderr, "rh v fail");
|
||||||
rt = -1;
|
rt = -1;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
@ -65,12 +65,12 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (crypt_session_init(&os, &own, &remote, nonce_own, true) < 0) {
|
if (crypt_session_init(&os, &own, &remote, nonce_own, true) == -1) {
|
||||||
rt = -1;
|
rt = -1;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (crypt_session_init(&rs, &remote, &own, nonce_rem, false) < 0) {
|
if (crypt_session_init(&rs, &remote, &own, nonce_rem, false) == -1) {
|
||||||
rt = -1;
|
rt = -1;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user