pwcrypt
|
Encrypt and decrypt strings symmetrically using Argon2id key derivation + either AES-256 (GCM) or ChaCha20-Poly1305. More...
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
Go to the source code of this file.
Macros | |
#define | PWCRYPT_API |
#define | PWCRYPT_VERSION 430 |
#define | PWCRYPT_VERSION_STR "4.3.0" |
#define | PWCRYPT_Z_CHUNKSIZE (1024 * 256) |
#define | PWCRYPT_ARGON2_T_COST 4 |
#define | PWCRYPT_ARGON2_M_COST (1024 * 256) |
#define | PWCRYPT_ARGON2_PARALLELISM 2 |
#define | PWCRYPT_ALGO_ID_AES256_GCM 0 |
#define | PWCRYPT_ALGO_ID_CHACHA20_POLY1305 1 |
#define | PWCRYPT_FILE_BUFFER_SIZE (1024 * 256) |
#define | PWCRYPT_ERROR_INVALID_ARGS (-1) |
#define | PWCRYPT_ERROR_OOM 1000 |
#define | PWCRYPT_ERROR_PW_TOO_WEAK 2000 |
#define | PWCRYPT_ERROR_ARGON2_FAILURE 3000 |
#define | PWCRYPT_ERROR_ENCRYPTION_FAILURE 4000 |
#define | PWCRYPT_ERROR_DECRYPTION_FAILURE 5000 |
#define | PWCRYPT_ERROR_BASE64_FAILURE 6000 |
#define | PWCRYPT_ERROR_COMPRESSION_FAILURE 7000 |
#define | PWCRYPT_ERROR_DECOMPRESSION_FAILURE 8000 |
#define | PWCRYPT_ERROR_FILE_FAILURE 9000 |
#define | PWCRYPT_MIN(x, y) (((x) < (y)) ? (x) : (y)) |
#define | PWCRYPT_MAX(x, y) (((x) > (y)) ? (x) : (y)) |
#define | PWCRYPT_MAX_WIN_FILEPATH_LENGTH (1024 * 32) |
#define | pwcrypt_fprintf pwcrypt_fprintf_fptr |
Functions | |
PWCRYPT_API unsigned char | pwcrypt_is_fprintf_enabled () |
static int | pwcrypt_printvoid (FILE *stream, const char *format,...) |
PWCRYPT_API void | pwcrypt_enable_fprintf () |
PWCRYPT_API void | pwcrypt_disable_fprintf () |
PWCRYPT_API void | dev_urandom (uint8_t *output_buffer, size_t output_buffer_size) |
PWCRYPT_API void | pwcrypt_get_temp_filepath (char output_buffer[256]) |
PWCRYPT_API size_t | pwcrypt_get_filesize (const char *filepath) |
PWCRYPT_API int | pwcrypt_assess_password_strength (const uint8_t *password, size_t password_length) |
PWCRYPT_API int | pwcrypt_encrypt (const uint8_t *input, size_t input_length, uint32_t compress, const uint8_t *password, size_t password_length, uint32_t argon2_cost_t, uint32_t argon2_cost_m, uint32_t argon2_parallelism, uint32_t algo, uint8_t **output, size_t *output_length, uint32_t output_base64) |
PWCRYPT_API int | pwcrypt_encrypt_file (const char *input_file_path, size_t input_file_path_length, uint32_t compress, const uint8_t *password, size_t password_length, uint32_t argon2_cost_t, uint32_t argon2_cost_m, uint32_t argon2_parallelism, uint32_t algo, const char *output_file_path, size_t output_file_path_length) |
PWCRYPT_API int | pwcrypt_encrypt_file_raw (FILE *input_file, FILE *output_file, uint32_t compress, const uint8_t *password, size_t password_length, uint32_t argon2_cost_t, uint32_t argon2_cost_m, uint32_t argon2_parallelism, uint32_t algo, uint32_t close_input_file, uint32_t close_output_file) |
PWCRYPT_API int | pwcrypt_decrypt (const uint8_t *encrypted_data, size_t encrypted_data_length, const uint8_t *password, size_t password_length, uint8_t **output, size_t *output_length) |
PWCRYPT_API int | pwcrypt_decrypt_file (const char *input_file_path, size_t input_file_path_length, const uint8_t *password, size_t password_length, const char *output_file_path, size_t output_file_path_length) |
PWCRYPT_API int | pwcrypt_decrypt_file_raw (FILE *input_file, FILE *output_file, const uint8_t *password, size_t password_length, uint32_t close_input_file, uint32_t close_output_file) |
PWCRYPT_API uint32_t | pwcrypt_get_version_nr () |
PWCRYPT_API uint32_t | pwcrypt_get_argon2_version_nr () |
PWCRYPT_API char * | pwcrypt_get_version_nr_string () |
PWCRYPT_API void | pwcrypt_free (void *ptr) |
PWCRYPT_API FILE * | pwcrypt_fopen (const char *filename, const char *mode) |
Variables | |
static const char | PWCRYPT_INVALID_ARGS_ERROR_MSG [] = "pwcrypt: Invalid arguments! Please run \"pwcrypt --help\" to find out how to use this program.\n" |
static const uint8_t | EMPTY64 [64] |
Encrypt and decrypt strings symmetrically using Argon2id key derivation + either AES-256 (GCM) or ChaCha20-Poly1305.
#define PWCRYPT_ALGO_ID_AES256_GCM 0 |
Algo ID for the (default) AES256-GCM encryption algorithm.
#define PWCRYPT_ALGO_ID_CHACHA20_POLY1305 1 |
Algo ID for the ChaCha20-Poly1305 encryption algorithm.
#define PWCRYPT_ARGON2_M_COST (1024 * 256) |
Default Argon2 memory cost parameter to use for key derivation if nothing else was specified.
#define PWCRYPT_ARGON2_PARALLELISM 2 |
Default Argon2 degree of parallelism parameter if nothing else was specified.
#define PWCRYPT_ARGON2_T_COST 4 |
Default Argon2 time cost parameter to use for key derivation if nothing else was specified.
#define PWCRYPT_ERROR_ARGON2_FAILURE 3000 |
Error code for Argon2 key derivation failures.
#define PWCRYPT_ERROR_BASE64_FAILURE 6000 |
Base-64 encoding/decoding failure.
#define PWCRYPT_ERROR_COMPRESSION_FAILURE 7000 |
This error code is returned when encryption failed due to a failure to compress the input data (ccrush lib failure).
#define PWCRYPT_ERROR_DECOMPRESSION_FAILURE 8000 |
Error code for when decompressing data fails (ccrush lib failure)..
#define PWCRYPT_ERROR_DECRYPTION_FAILURE 5000 |
Error code for decryption failures.
Hint: If you're having this and you're using pwcrypt as a library, try to set a breakpoint and step through the code to see what exactly is failing
#define PWCRYPT_ERROR_ENCRYPTION_FAILURE 4000 |
Encryption failures return this error code.
#define PWCRYPT_ERROR_FILE_FAILURE 9000 |
Error code for failures while handling files.
#define PWCRYPT_ERROR_INVALID_ARGS (-1) |
Error code for invalid arguments passed to a pwcrypt function.
#define PWCRYPT_ERROR_OOM 1000 |
Error code "out of memory", uh oh...
#define PWCRYPT_ERROR_PW_TOO_WEAK 2000 |
Error code for passwords that are too weak.
#define PWCRYPT_FILE_BUFFER_SIZE (1024 * 256) |
The size in bytes of the file's background buffer.
#define PWCRYPT_MAX | ( | x, | |
y ) (((x) > (y)) ? (x) : (y)) |
Picks the bigger of two numbers.
#define PWCRYPT_MAX_WIN_FILEPATH_LENGTH (1024 * 32) |
Maximum file path length on NTFS.
#define PWCRYPT_MIN | ( | x, | |
y ) (((x) < (y)) ? (x) : (y)) |
Picks the smaller of two numbers.
#define PWCRYPT_VERSION 430 |
Current version of the used pwcrypt library.
#define PWCRYPT_VERSION_STR "4.3.0" |
Current version of the used pwcrypt library (nicely-formatted string).
#define PWCRYPT_Z_CHUNKSIZE (1024 * 256) |
Default chunksize to use for compressing and decompressing buffers.
PWCRYPT_API void dev_urandom | ( | uint8_t * | output_buffer, |
size_t | output_buffer_size ) |
(Tries to) read from /dev/urandom
(or Windows equivalent, yeah...) filling the given output_buffer
with output_buffer_size
random bytes.
output_buffer | Where to write the random bytes into. |
output_buffer_size | How many random bytes to write into output_buffer |
PWCRYPT_API int pwcrypt_assess_password_strength | ( | const uint8_t * | password, |
size_t | password_length ) |
Checks whether a given password is strong enough or not.
password | Password string to check (does not need to be NUL-terminated; only password_length characters will be checked!). |
password_length | Length of the password string. |
0
if the password is OK; a non-zero error code if the password is too weak. PWCRYPT_API int pwcrypt_decrypt | ( | const uint8_t * | encrypted_data, |
size_t | encrypted_data_length, | ||
const uint8_t * | password, | ||
size_t | password_length, | ||
uint8_t ** | output, | ||
size_t * | output_length ) |
Decrypts a string or a byte array that was encrypted using pwcrypt_encrypt().
encrypted_data | The ciphertext to decrypt. |
encrypted_data_length | Length of the encrypted_data argument (string length or byte array size). |
password | The decryption password. |
password_length | Length of the password argument. |
output | Pointer to the output buffer where to write the decrypted data into. This will be allocated and NUL-terminated automatically on success; if anything fails, this will be left untouched! So you only need to pwcrypt_free() this if decryption succeeds. |
output_length | [OPTIONAL] Where to write the output buffer length into. Pass NULL if you don't care. |
0
on success; non-zero error codes if something fails. PWCRYPT_API int pwcrypt_decrypt_file | ( | const char * | input_file_path, |
size_t | input_file_path_length, | ||
const uint8_t * | password, | ||
size_t | password_length, | ||
const char * | output_file_path, | ||
size_t | output_file_path_length ) |
Decrypts a file that was encrypted using pwcrypt_encrypt_file().
input_file_path | Full path to the file that needs to be decrypted. Must be UTF-8 encoded. Must be NUL-terminated and its strlen() must be equal to the input_file_path_length parameter. |
input_file_path_length | Length of the input_file_path string. |
password | The decryption password. |
password_length | Length of the password argument. |
output_file_path | This is the full output file path where to write the decrypted file into. |
output_file_path_length | Length of the output_file_path string. |
0
on success; non-zero error codes if something fails. PWCRYPT_API int pwcrypt_decrypt_file_raw | ( | FILE * | input_file, |
FILE * | output_file, | ||
const uint8_t * | password, | ||
size_t | password_length, | ||
uint32_t | close_input_file, | ||
uint32_t | close_output_file ) |
Decrypts a file that was encrypted using pwcrypt_encrypt_file() or pwcrypt_encrypt_file_raw().
input_file | File to decrypt. Must not be NULL |
output_file | File handle of the output file into which to write the decrypted result. Must not be NULL |
password | The decryption password. |
password_length | Length of the password argument. |
close_input_file | Should the input file handle be fclose 'd after usage? Pass 0 for "false" and anything else for "true". |
close_output_file | Should the output file handle be fclose 'd after usage? Pass 0 for "false" and anything else for "true". |
0
on success; non-zero error codes if something fails. PWCRYPT_API void pwcrypt_disable_fprintf | ( | ) |
Disables pwcrypts' use of fprintf().
PWCRYPT_API void pwcrypt_enable_fprintf | ( | ) |
Enables pwcrypts' use of fprintf().
PWCRYPT_API int pwcrypt_encrypt | ( | const uint8_t * | input, |
size_t | input_length, | ||
uint32_t | compress, | ||
const uint8_t * | password, | ||
size_t | password_length, | ||
uint32_t | argon2_cost_t, | ||
uint32_t | argon2_cost_m, | ||
uint32_t | argon2_parallelism, | ||
uint32_t | algo, | ||
uint8_t ** | output, | ||
size_t * | output_length, | ||
uint32_t | output_base64 ) |
Encrypts an input string of data symmetrically with a password.
The password string is fed into a customizable amount of Argon2id iterations to derive a 256-bit symmetric key, with which the input will be encrypted and written into the output buffer.
input | The input data to encrypt. |
input_length | Length of the input data array argument. |
compress | Should the input data be compressed before being encrypted? Pass 0 for no compression, or a compression level from 1 to 9 to pass to the deflate algorithm (6 is a healthy default value to use for this). |
password | The password string (ideally a UTF8-encoded byte array, but you can obviously also encrypt using a file) with which to encrypt the input argument (this will be used to derive a 256-bit symmetric encryption key (e.g. AES-256 key) using Argon2id). |
password_length | Length of the password string argument. |
argon2_cost_t | The Argon2 time cost parameter (number of iterations) to use for deriving the symmetric encryption key. Pass 0 to use the default value of PWCRYPT_ARGON2_T_COST. |
argon2_cost_m | The Argon2 memory cost parameter (in KiB) to use for key derivation. Pass 0 to use the default value of PWCRYPT_ARGON2_M_COST. |
argon2_parallelism | Degree of parallelism to use when deriving the symmetric encryption key from the password with Argon2 (number of parallel threads). Pass 0 to use the default value of PWCRYPT_ARGON2_PARALLELISM. |
algo | Which encryption algo to use (see the top of the pwcrypt.h header file for more infos). |
output | Pointer to the output buffer where to write the encrypted ciphertext into. This will be allocated and NUL-terminated on success; if anything fails, this will be left untouched! So you only need to pwcrypt_free() it on successful encryption. |
output_length | [OPTIONAL] Where to write the output buffer length into. Pass NULL if you don't care. |
output_base64 | Should the encrypted output bytes be base64-encoded for easy textual transmission (e.g. email)? If you decide to base64-encode the encrypted data buffer, please be aware that a NUL-terminator is appended at the end to allow usage as a C-string but it will not be counted in output_length . Pass 0 for raw binary output, or anything else for a human-readable, base64-encoded output string. |
0
on success; non-zero error codes if something fails. PWCRYPT_API int pwcrypt_encrypt_file | ( | const char * | input_file_path, |
size_t | input_file_path_length, | ||
uint32_t | compress, | ||
const uint8_t * | password, | ||
size_t | password_length, | ||
uint32_t | argon2_cost_t, | ||
uint32_t | argon2_cost_m, | ||
uint32_t | argon2_parallelism, | ||
uint32_t | algo, | ||
const char * | output_file_path, | ||
size_t | output_file_path_length ) |
Encrypts a file symmetrically with a password.
The password string is fed into a customizable amount of Argon2id iterations to derive a 256-bit symmetric key, with which the input will be encrypted and written into the output buffer.
input_file_path | Full path to the file that needs to be encrypted. Must be UTF-8 encoded. Must be NUL-terminated and its strlen() must be equal to the input_file_path_length parameter. |
input_file_path_length | Length of the input_file_path string. |
compress | Should the input data be compressed before being encrypted? Pass 0 for no compression, or a compression level from 1 to 9 to pass to the deflate algorithm (6 is a healthy default value to use for this). |
password | The password string (ideally a UTF8-encoded byte array, but you can obviously also encrypt using a file) with which to encrypt the input argument (this will be used to derive a 256-bit symmetric encryption key (e.g. AES-256 key) using Argon2id). |
password_length | Length of the password string argument. |
argon2_cost_t | The Argon2 time cost parameter (number of iterations) to use for deriving the symmetric encryption key. Pass 0 to use the default value of PWCRYPT_ARGON2_T_COST. |
argon2_cost_m | The Argon2 memory cost parameter (in KiB) to use for key derivation. Pass 0 to use the default value of PWCRYPT_ARGON2_M_COST. |
argon2_parallelism | Degree of parallelism to use when deriving the symmetric encryption key from the password with Argon2 (number of parallel threads). Pass 0 to use the default value of PWCRYPT_ARGON2_PARALLELISM. |
algo | Which encryption algo to use (see the top of the pwcrypt.h header file for more infos). |
output_file_path | This is the full output file path where to write the encrypted file into. |
output_file_path_length | Length of the output_file_path string. |
0
on success; non-zero error codes if something fails. PWCRYPT_API int pwcrypt_encrypt_file_raw | ( | FILE * | input_file, |
FILE * | output_file, | ||
uint32_t | compress, | ||
const uint8_t * | password, | ||
size_t | password_length, | ||
uint32_t | argon2_cost_t, | ||
uint32_t | argon2_cost_m, | ||
uint32_t | argon2_parallelism, | ||
uint32_t | algo, | ||
uint32_t | close_input_file, | ||
uint32_t | close_output_file ) |
Encrypts a file symmetrically with a password.
The password string is fed into a customizable amount of Argon2id iterations to derive a 256-bit symmetric key, with which the input will be encrypted and written into the output buffer.
input_file | File that needs to be encrypted. Must not be NULL |
output_file | File handle of the output file into which the encryption result should be written into. Must not be NULL |
compress | Should the input data be compressed before being encrypted? Pass 0 for no compression, or a compression level from 1 to 9 to pass to the deflate algorithm (6 is a healthy default value to use for this). |
password | The password string (ideally a UTF8-encoded byte array, but you can obviously also encrypt using a file) with which to encrypt the input argument (this will be used to derive a 256-bit symmetric encryption key (e.g. AES-256 key) using Argon2id). |
password_length | Length of the password string argument. |
argon2_cost_t | The Argon2 time cost parameter (number of iterations) to use for deriving the symmetric encryption key. Pass 0 to use the default value of PWCRYPT_ARGON2_T_COST. |
argon2_cost_m | The Argon2 memory cost parameter (in KiB) to use for key derivation. Pass 0 to use the default value of PWCRYPT_ARGON2_M_COST. |
argon2_parallelism | Degree of parallelism to use when deriving the symmetric encryption key from the password with Argon2 (number of parallel threads). Pass 0 to use the default value of PWCRYPT_ARGON2_PARALLELISM. |
algo | Which encryption algo to use (see the top of the pwcrypt.h header file for more infos). |
close_input_file | Should the input file handle be fclose 'd after usage? Pass 0 for "false" and anything else for "true". |
close_output_file | Should the output file handle be fclose 'd after usage? Pass 0 for "false" and anything else for "true". |
0
on success; non-zero error codes if something fails. PWCRYPT_API FILE * pwcrypt_fopen | ( | const char * | filename, |
const char * | mode ) |
Wrapper around fopen()
filename | File path. |
mode | File open mode ("r", "w", "rb", etc...) |
FILE*
or null
PWCRYPT_API void pwcrypt_free | ( | void * | ptr | ) |
Wrapper around free()
(mainly useful for C# interop).
ptr | The memory to free (typically the output of one of the two main pwcrypt functions). |
PWCRYPT_API uint32_t pwcrypt_get_argon2_version_nr | ( | ) |
Gets the current Argon2 version number used by pwcrypt (numeric).
PWCRYPT_API size_t pwcrypt_get_filesize | ( | const char * | filepath | ) |
Retrieve the size of a file.
filepath | The file path. |
0
if getting the file size failed for some reason. PWCRYPT_API void pwcrypt_get_temp_filepath | ( | char | output_buffer[256] | ) |
Gets a completely random, temporary file name (usually located within /var/tmp
).
output_buffer | Where to write the temporary file path into (must be a writeable char buffer of at least 256B). |
PWCRYPT_API uint32_t pwcrypt_get_version_nr | ( | ) |
Gets the current pwcrypt version number (numeric).
PWCRYPT_API char * pwcrypt_get_version_nr_string | ( | ) |
Gets the current pwcrypt version number as a nicely-formatted, human-readable string.
PWCRYPT_API unsigned char pwcrypt_is_fprintf_enabled | ( | ) |
Checks whether pwcrypt fprintf is enabled (whether errors are fprintfed into stderr).
|
inlinestatic |
Like fprintf() except it doesn't do anything. Like printing into /dev/null
:D lots of fun!
stream | [IGNORED] |
format | [IGNORED] |
... | [IGNORED] |
0
|
static |
An array of 64 bytes of value 0x00.
|
static |
Error message for invalid CLI arguments.