Skip to content

Commit 22ac9ac

Browse files
Added support for encrypting/decrypting large files (#6)
* Increased file buffer size and introduced PWCRYPT_CCRUSH_BUFFER_SIZE_KIB. * Rewrote pwcrypt_encrypt to write in chunks. * Had to introduce lib chillbuff to properly implement v4.4.0 of the pwcrypt_decrypt function. * Reduced argon2id params inside tests.c to speed things up... * Cleanup. * Upgraded lib/ccrush to v2.1.1 * Improved pwcrypt_encrypt function by using a chillbuff instead of output buffer size approximations.
1 parent d62d0fb commit 22ac9ac

File tree

6 files changed

+1072
-592
lines changed

6 files changed

+1072
-592
lines changed

CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ set(CMAKE_C_STANDARD 11)
1919
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake" ${CMAKE_MODULE_PATH})
2020

2121
set(${PROJECT_NAME}_MAJOR 4)
22-
set(${PROJECT_NAME}_MINOR 3)
23-
set(${PROJECT_NAME}_PATCH 1)
22+
set(${PROJECT_NAME}_MINOR 4)
23+
set(${PROJECT_NAME}_PATCH 0)
2424
set(${PROJECT_NAME}_VERSION_STRING "${${PROJECT_NAME}_MAJOR}.${${PROJECT_NAME}_MINOR}.${${PROJECT_NAME}_PATCH}")
2525

2626
option(ENABLE_TESTING "Build MbedTLS tests." OFF)
@@ -89,10 +89,15 @@ if (NOT TARGET ccrush)
8989
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/lib/ccrush ccrush)
9090
endif ()
9191

92+
if (NOT TARGET chillbuff)
93+
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/lib/ccrush/lib/chillbuff chillbuff)
94+
endif ()
95+
9296
set_property(TARGET ccrush PROPERTY POSITION_INDEPENDENT_CODE ON)
9397

9498
target_link_libraries(${PROJECT_NAME}
9599
PUBLIC ccrush
100+
PUBLIC chillbuff
96101
PUBLIC mbedcrypto
97102
)
98103

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ Append `--algorithm=chachapoly` at the end to use the [ChaCha20-Poly1305](https:
116116

117117
Since [v4.3.0](https://github.com/GlitchedPolygons/pwcrypt/releases/tag/4.3.0) it is now possible to make the pwcrypt CLI read the input from `stdin`.
118118

119-
> ⚠️ Warning: encrypting [files larger than 64 GiB will fail miserably when using AES-GCM](https://crypto.stackexchange.com/a/31798): please use the ChaCha20-Poly1305 algorithm in that case (pass the `--algorithm=chachapoly` argument to the CLI to do so).
120-
121119
To do so, just pass `-` as the input parameter after the `e` or `d` argument. The `--file=/output/file/path/here` still works (and if it's not set, the output will be written to `stdout`).
122120

123121
For example, to compress and encrypt a whole directory you could pipe the result of `tar` into `pwcrypt`. Such a command would look like this:

include/pwcrypt.h

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,12 @@ static const uint8_t EMPTY64[64] = {
7272
/**
7373
* Current version of the used pwcrypt library.
7474
*/
75-
#define PWCRYPT_VERSION 431
75+
#define PWCRYPT_VERSION 440
7676

7777
/**
7878
* Current version of the used pwcrypt library (nicely-formatted string).
7979
*/
80-
#define PWCRYPT_VERSION_STR "4.3.1"
81-
82-
#ifndef PWCRYPT_Z_CHUNKSIZE
83-
/**
84-
* Default chunksize to use for compressing and decompressing buffers.
85-
*/
86-
#define PWCRYPT_Z_CHUNKSIZE (1024 * 256)
87-
#endif
80+
#define PWCRYPT_VERSION_STR "4.4.0"
8881

8982
#ifndef PWCRYPT_ARGON2_T_COST
9083
/**
@@ -119,9 +112,16 @@ static const uint8_t EMPTY64[64] = {
119112

120113
#ifndef PWCRYPT_FILE_BUFFER_SIZE
121114
/**
122-
* The size in bytes of the file's background buffer.
115+
* The buffer size in bytes to use for reading/writing files.
116+
*/
117+
#define PWCRYPT_FILE_BUFFER_SIZE (1024 * 512)
118+
#endif
119+
120+
#ifndef PWCRYPT_CCRUSH_BUFFER_SIZE_KIB
121+
/**
122+
* The buffer size in KiB to use for compressing/decompressing via ccrush.
123123
*/
124-
#define PWCRYPT_FILE_BUFFER_SIZE (1024 * 256)
124+
#define PWCRYPT_CCRUSH_BUFFER_SIZE_KIB 256
125125
#endif
126126

127127
/**
@@ -175,6 +175,11 @@ static const uint8_t EMPTY64[64] = {
175175
*/
176176
#define PWCRYPT_ERROR_FILE_FAILURE 9000
177177

178+
/**
179+
* Error code for failures concerning the output buffer (which uses the chillbuff lib to grow dynamically).
180+
*/
181+
#define PWCRYPT_ERROR_CHILLBUFF_FAILURE 10000
182+
178183
/**
179184
* Picks the smaller of two numbers.
180185
*/
@@ -377,6 +382,15 @@ PWCRYPT_API void pwcrypt_free(void* ptr);
377382
*/
378383
PWCRYPT_API FILE* pwcrypt_fopen(const char* filename, const char* mode);
379384

385+
/**
386+
* Compares two blocks of memory against equality in a cryptographically safe manner (time-safe impl./constant-time comparison).
387+
* @param mem1 Memory block 1 to compare.
388+
* @param mem2 Memory block 2 to compare.
389+
* @param n How many bytes to compare.
390+
* @return Returns <code>0</code> if the two memory blocks are equal for the passed amount of bytes.
391+
*/
392+
PWCRYPT_API int pwcrypt_memcmp(const void* mem1, const void* mem2, size_t n);
393+
380394
#ifdef __cplusplus
381395
} // extern "C"
382396
#endif

0 commit comments

Comments
 (0)