Skip to content

Add AES GCM file encryptions#371

Merged
dgarske merged 4 commits intowolfSSL:masterfrom
tmael:file_enc
May 3, 2023
Merged

Add AES GCM file encryptions#371
dgarske merged 4 commits intowolfSSL:masterfrom
tmael:file_enc

Conversation

@tmael
Copy link
Copy Markdown
Contributor

@tmael tmael commented Feb 28, 2023

This PR adds file encryption/decryption using AES GCM.

int encrypt_file_AesGCM(const char* in_file, const char* out_file, const char* key_str, const char* iv_str)
int decrypt_file_AesGCM(const char* in_file, const char* out_file, const char* key_str)

The code uses POSIX APIs, 32 Bytes key size, and an IV length of 16 Bytes. The code is optimized to use a large buffer size of up to 1G of RAM or input file size to minimize file I/O overhead.

The encrypt_file_AesGCM() API will encrypt a given file and write it to the supplied cipher output file.

The cipher output file will contain a header at the beginning:
WOLFSSL (7 Bytes) magic/identifier | TAG (16Bytes) | IV (16Bytes) | cipher data (= size of the plane file) ...

decrypt_file_AesGCM() API extracts/uses the header and decrypts the cipher data.

When encrypting a file, read and write buffers are used to read data from the file, encrypt it, and then place it into a buffer before writing it to another file. The buffer size is crucial for determining the speed of the encryption process as it significantly impacts performance. You can change the default value of MIN_BUFFER_SIZE and MAX_BUFFER_SIZE.

make CPPFLAGS="-DMAX_BUFFER_SIZE=1073741824 -DMIN_BUFFER_SIZE=1024"

Tested with the following:

make
./aesgcm-file-encrypt.sh

and
./aesgcm-file-encrypt 254
$ ./aesgcm-file-encrypt -h
This program accepts several switches:
  -e <num>   encryption. 256, 192, 128 
  -d <num>   decryption. 256, 192, 128
  -m <num>   method to use.  GCM(1), EVP GCM (2), 
  -i <file>  Set the input filename to 'file'
  -o <file>  Set the output filename to 'file'
  -t <num>   Sanity test with the given file size in Bytes. The test will create three files:text.bin, cipher, decrypted plain. 
   Example, ./aesgcm-file-encrypt -t 256

tmael and others added 3 commits March 10, 2023 15:30
… struct stat). Allow building aesgcm-file-encrypt even if AES GCM streaming is not enabled, but show message on run.
@dgarske dgarske merged commit 2f24e6d into wolfSSL:master May 3, 2023
yota22721 pushed a commit to yota22721/wolfssl-examples that referenced this pull request Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants