pwcrypt
Loading...
Searching...
No Matches
pwcrypt.h
Go to the documentation of this file.
1/*
2 Copyright 2020 Raphael Beck
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
23#ifndef PWCRYPT_H
24#define PWCRYPT_H
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30#if defined(_WIN32) && defined(PWCRYPT_DLL)
31#ifdef PWCRYPT_BUILD_DLL
32#define PWCRYPT_API __declspec(dllexport)
33#else
34#define PWCRYPT_API __declspec(dllimport)
35#endif
36#else
37#define PWCRYPT_API
38#endif
39
40#include <stdio.h>
41#include <stdint.h>
42#include <stddef.h>
43
47static const char PWCRYPT_INVALID_ARGS_ERROR_MSG[] = "pwcrypt: Invalid arguments! Please run \"pwcrypt --help\" to find out how to use this program.\n";
48
52static const uint8_t EMPTY64[64] = {
53 //
54 0x00, 0x00, 0x00, 0x00, //
55 0x00, 0x00, 0x00, 0x00, //
56 0x00, 0x00, 0x00, 0x00, //
57 0x00, 0x00, 0x00, 0x00, //
58 0x00, 0x00, 0x00, 0x00, //
59 0x00, 0x00, 0x00, 0x00, //
60 0x00, 0x00, 0x00, 0x00, //
61 0x00, 0x00, 0x00, 0x00, //
62 0x00, 0x00, 0x00, 0x00, //
63 0x00, 0x00, 0x00, 0x00, //
64 0x00, 0x00, 0x00, 0x00, //
65 0x00, 0x00, 0x00, 0x00, //
66 0x00, 0x00, 0x00, 0x00, //
67 0x00, 0x00, 0x00, 0x00, //
68 0x00, 0x00, 0x00, 0x00, //
69 0x00, 0x00, 0x00, 0x00, //
70};
71
75#define PWCRYPT_VERSION 430
76
80#define PWCRYPT_VERSION_STR "4.3.0"
81
82#ifndef PWCRYPT_Z_CHUNKSIZE
86#define PWCRYPT_Z_CHUNKSIZE (1024 * 256)
87#endif
88
89#ifndef PWCRYPT_ARGON2_T_COST
93#define PWCRYPT_ARGON2_T_COST 4
94#endif
95
96#ifndef PWCRYPT_ARGON2_M_COST
100#define PWCRYPT_ARGON2_M_COST (1024 * 256)
101#endif
102
103#ifndef PWCRYPT_ARGON2_PARALLELISM
107#define PWCRYPT_ARGON2_PARALLELISM 2
108#endif
109
113#define PWCRYPT_ALGO_ID_AES256_GCM 0
114
118#define PWCRYPT_ALGO_ID_CHACHA20_POLY1305 1
119
120#ifndef PWCRYPT_FILE_BUFFER_SIZE
124#define PWCRYPT_FILE_BUFFER_SIZE (1024 * 256)
125#endif
126
130#define PWCRYPT_ERROR_INVALID_ARGS (-1)
131
135#define PWCRYPT_ERROR_OOM 1000
136
140#define PWCRYPT_ERROR_PW_TOO_WEAK 2000
141
145#define PWCRYPT_ERROR_ARGON2_FAILURE 3000
146
150#define PWCRYPT_ERROR_ENCRYPTION_FAILURE 4000
151
156#define PWCRYPT_ERROR_DECRYPTION_FAILURE 5000
157
161#define PWCRYPT_ERROR_BASE64_FAILURE 6000
162
166#define PWCRYPT_ERROR_COMPRESSION_FAILURE 7000
167
171#define PWCRYPT_ERROR_DECOMPRESSION_FAILURE 8000
172
176#define PWCRYPT_ERROR_FILE_FAILURE 9000
177
181#define PWCRYPT_MIN(x, y) (((x) < (y)) ? (x) : (y))
182
186#define PWCRYPT_MAX(x, y) (((x) > (y)) ? (x) : (y))
187
188#ifndef PWCRYPT_MAX_WIN_FILEPATH_LENGTH
192#define PWCRYPT_MAX_WIN_FILEPATH_LENGTH (1024 * 32)
193#endif
194
199PWCRYPT_API unsigned char pwcrypt_is_fprintf_enabled();
200
208static inline int pwcrypt_printvoid(FILE* stream, const char* format, ...)
209{
210 return 0;
211}
212
214extern int (*pwcrypt_fprintf_fptr)(FILE* stream, const char* format, ...);
215
219PWCRYPT_API void pwcrypt_enable_fprintf();
220
224PWCRYPT_API void pwcrypt_disable_fprintf();
225
227#define pwcrypt_fprintf pwcrypt_fprintf_fptr
228
234PWCRYPT_API void dev_urandom(uint8_t* output_buffer, size_t output_buffer_size);
235
240PWCRYPT_API void pwcrypt_get_temp_filepath(char output_buffer[256]);
241
247PWCRYPT_API size_t pwcrypt_get_filesize(const char* filepath);
248
255PWCRYPT_API int pwcrypt_assess_password_strength(const uint8_t* password, size_t password_length);
256
274PWCRYPT_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);
275
292PWCRYPT_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);
293
310PWCRYPT_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);
311
322PWCRYPT_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);
323
334PWCRYPT_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);
335
346PWCRYPT_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);
347
352PWCRYPT_API uint32_t pwcrypt_get_version_nr();
353
358PWCRYPT_API uint32_t pwcrypt_get_argon2_version_nr();
359
365
370PWCRYPT_API void pwcrypt_free(void* ptr);
371
378PWCRYPT_API FILE* pwcrypt_fopen(const char* filename, const char* mode);
379
380#ifdef __cplusplus
381} // extern "C"
382#endif
383
384#endif // PWCRYPT_H
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)
static const uint8_t EMPTY64[64]
Definition pwcrypt.h:52
PWCRYPT_API uint32_t pwcrypt_get_argon2_version_nr()
PWCRYPT_API void pwcrypt_disable_fprintf()
PWCRYPT_API void pwcrypt_enable_fprintf()
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)
static int pwcrypt_printvoid(FILE *stream, const char *format,...)
Definition pwcrypt.h:208
PWCRYPT_API void pwcrypt_free(void *ptr)
PWCRYPT_API size_t pwcrypt_get_filesize(const char *filepath)
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 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 uint32_t pwcrypt_get_version_nr()
PWCRYPT_API int pwcrypt_assess_password_strength(const uint8_t *password, size_t password_length)
PWCRYPT_API char * pwcrypt_get_version_nr_string()
static const char PWCRYPT_INVALID_ARGS_ERROR_MSG[]
Definition pwcrypt.h:47
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 void dev_urandom(uint8_t *output_buffer, size_t output_buffer_size)
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 unsigned char pwcrypt_is_fprintf_enabled()
PWCRYPT_API void pwcrypt_get_temp_filepath(char output_buffer[256])
PWCRYPT_API FILE * pwcrypt_fopen(const char *filename, const char *mode)