diff options
Diffstat (limited to 'src/lib.c')
-rw-r--r-- | src/lib.c | 292 |
1 files changed, 292 insertions, 0 deletions
diff --git a/src/lib.c b/src/lib.c new file mode 100644 index 0000000..001ff7d --- /dev/null +++ b/src/lib.c @@ -0,0 +1,292 @@ +#include <s.h> + +#include <assert.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +#include "random.h" +#include "impl.h" + +#include "lib.h" + + + +int +tweetnacl_main(const int argc, const char *const *const argv) { + int rc = EXIT_FAILURE; + + if (printf("%s %s %s\n", "NAME", "VERSION", "DATE") < 0) { + perror("printf()"); + goto out; + } + + for (int i = 0; i < argc; i++) { + if (printf("argv[%i]: %s\n", i, argv[i]) < 0) { + perror("printf()"); + goto out; + } + } + + rc = EXIT_SUCCESS; +out: + return rc; +} + + + +bool +crypt_verify( + const unsigned char buffer1[crypt_verify_BYTES], + const unsigned char buffer2[crypt_verify_BYTES] +) { + return crypto_verify_16(buffer1, buffer2) == 0; +} + +void +crypt_hash( + const unsigned long long length, + const unsigned char *data, + unsigned char out[crypt_hash_BYTES] +) { + const int ret = crypto_hash(out, data, length); + assert(ret == 0); +} + +void +crypt_onetimeauth( + const unsigned long long length, + const unsigned char *const data, + const unsigned char secret_key[crypt_onetimeauth_KEYBYTES], + unsigned char authenticator_out[crypt_onetimeauth_BYTES] +) { + const int ret = crypto_onetimeauth( + authenticator_out, + data, + length, + secret_key + ); + assert(ret == 0); +} + +bool +crypt_onetimeauth_verify( + const unsigned char authenticator[crypt_onetimeauth_BYTES], + const unsigned char secret_key[crypt_onetimeauth_KEYBYTES], + const unsigned long long length, + const unsigned char *const data +) { + return crypto_onetimeauth_verify( + authenticator, + data, + length, + secret_key + ); +} + +int +crypt_secretbox( + const unsigned char secret_key[crypt_secretbox_KEYBYTES], + const unsigned long long length, + const unsigned char *const clear_data, + unsigned char *const cypher_out +) { + for (int i = 0; i < crypt_secretbox_ZEROBYTES; i++) { + if (clear_data[i] != 0) { + return -2; + } + } + + assert(length >= 32); + unsigned char nonce[crypt_secretbox_NONCEBYTES]; + random_bytes(nonce, crypt_secretbox_NONCEBYTES); + const int ret = crypto_secretbox( + cypher_out, + clear_data, + length, + nonce, + secret_key + ); + assert(ret == 0); + return 0; +} + +int +crypt_secretbox_open( + const unsigned char secret_key[crypt_secretbox_KEYBYTES], + const unsigned long long length, + const unsigned char *const cypher_data, + unsigned char *const clean_out +) { + for (int i = 0; i < crypt_secretbox_BOXZEROBYTES; i++) { + if (cypher_data[i] != 0) { + return -2; + } + } + + unsigned char nonce[crypt_secretbox_NONCEBYTES]; + random_bytes(nonce, crypt_secretbox_NONCEBYTES); + return crypto_secretbox_open( + clean_out, + cypher_data, + length, + nonce, + secret_key + ); +} + +void +crypt_sign_keypair( + unsigned char public_key_out[crypt_sign_PUBLICKEYBYTES], + unsigned char secret_key_out[crypt_sign_SECRETKEYBYTES] +) { + const int ret = crypto_sign_keypair(public_key_out, secret_key_out); + assert(ret == 0); +} + +void +crypt_sign( + const unsigned char secret_key[crypt_sign_SECRETKEYBYTES], + const unsigned long long length, + const unsigned char *const data, + unsigned long long *const outlen, + unsigned char *const signed_out +) { + const int ret = crypto_sign( + signed_out, + outlen, + data, + length, + secret_key + ); + assert(ret == 0); +} + +int +crypt_sign_open( + const unsigned char public_key[crypt_sign_PUBLICKEYBYTES], + const unsigned long long signed_message_length, + const unsigned char *const signed_message, + unsigned long long *const inoutlen, + unsigned char *const out +) { + assert(signed_message_length >= crypt_sign_BYTES); + assert(*inoutlen == signed_message_length); + return crypto_sign_open( + out, + inoutlen, + signed_message, + signed_message_length, + public_key + ); +} + +void +crypt_box_keypair( + unsigned char public_key_out[crypt_box_PUBLICKEYBYTES], + unsigned char secret_key_out[crypt_box_SECRETKEYBYTES] +) { + const int ret = crypto_box_keypair(public_key_out, secret_key_out); + assert(ret == 0); +} + +int +crypt_box( + const unsigned char receiver_public_key[crypt_box_PUBLICKEYBYTES], + const unsigned char sender_secret_key[crypt_box_PUBLICKEYBYTES], + const unsigned long long length, + const unsigned char *const clear_data, + unsigned char *const cypher_out +) { + for (int i = 0U; i < crypt_box_ZEROBYTES; i++) { + if (clear_data[i] != 0) { + return -2; + } + } + + unsigned char nonce[crypt_box_NONCEBYTES]; + random_bytes(nonce, crypt_box_NONCEBYTES); + const int ret = crypto_box( + cypher_out, + clear_data, + length, + nonce, + receiver_public_key, + sender_secret_key + ); + assert(ret == 0); + return 0; +} + +int +crypt_box_open( + const unsigned char sender_public_key[crypt_box_PUBLICKEYBYTES], + const unsigned char receiver_secret_key[crypt_box_SECRETKEYBYTES], + const unsigned long long length, + const unsigned char *const cypher_data, + unsigned char *const clear_out +) { + for (int i = 0U; i < crypt_box_BOXZEROBYTES; i++) { + if (cypher_data[i] != 0) { + return -2; + } + } + + unsigned char nonce[crypt_box_NONCEBYTES]; + random_bytes(nonce, crypt_box_NONCEBYTES); + return crypto_box_open( + clear_out, + cypher_data, + length, + nonce, + sender_public_key, + receiver_secret_key + ); +} + +void +crypt_box_beforenm( + const unsigned char public_key[crypt_box_PUBLICKEYBYTES], + const unsigned char secret_key[crypt_box_PUBLICKEYBYTES], + unsigned char out[crypt_box_BEFORENMBYTES] +) { + const int ret = crypto_box_beforenm(out, public_key, secret_key); + assert(ret == 0); +} + +void +crypt_box_afternm( + const unsigned char beforenm_intermediate[crypt_box_BEFORENMBYTES], + const unsigned long long length, + const unsigned char *const clear_data, + unsigned char *const cypher_out +) { + unsigned char nonce[crypt_box_NONCEBYTES]; + random_bytes(nonce, crypt_box_NONCEBYTES); + const int ret = crypto_box_afternm( + cypher_out, + clear_data, + length, + nonce, + beforenm_intermediate + ); + assert(ret == 0); +} + +int +crypt_box_open_afternm( + const unsigned char beforenm_intermediate[crypt_box_BEFORENMBYTES], + const unsigned long long length, + const unsigned char *const cypher_data, + unsigned char *const clear_out +) { + unsigned char nonce[crypt_box_NONCEBYTES]; + random_bytes(nonce, crypt_box_NONCEBYTES); + return crypto_box_open_afternm( + clear_out, + cypher_data, + length, + nonce, + beforenm_intermediate + ); +} |