Merge pull request #549 from anhu/aes_modes_ofb_siv_xts
AES modes extravaganza. ofb, siv, xtspull/552/head
commit
b8e2445c52
|
|
@ -0,0 +1,303 @@
|
|||
/* aes-ofb.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
/* AES-OFB Example
|
||||
* This example demonstrates:
|
||||
* - One-shot encryption using wc_AesOfbEncrypt()
|
||||
* - Streaming decryption using wc_AesOfbDecrypt() in chunks
|
||||
* Note: OFB mode inherently supports streaming (no padding required)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef WOLFSSL_USER_SETTINGS
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/aes.h>
|
||||
#include <wolfssl/wolfcrypt/random.h>
|
||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||
|
||||
#if !defined(NO_AES) && defined(WOLFSSL_AES_OFB)
|
||||
|
||||
#define AES_KEY_SIZE AES_256_KEY_SIZE
|
||||
|
||||
/* Small chunk size to demonstrate streaming */
|
||||
#define CHUNK_SIZE 64
|
||||
|
||||
static int read_file(const char* filename, byte** data, word32* dataSz)
|
||||
{
|
||||
FILE* fp;
|
||||
long fileSz;
|
||||
|
||||
fp = fopen(filename, "rb");
|
||||
if (fp == NULL) {
|
||||
printf("Error: Cannot open file %s\n", filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
fileSz = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
*data = (byte*)malloc(fileSz);
|
||||
if (*data == NULL) {
|
||||
fclose(fp);
|
||||
printf("Error: Memory allocation failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*dataSz = (word32)fread(*data, 1, fileSz, fp);
|
||||
fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int write_file(const char* filename, const byte* data, word32 dataSz)
|
||||
{
|
||||
FILE* fp;
|
||||
|
||||
fp = fopen(filename, "wb");
|
||||
if (fp == NULL) {
|
||||
printf("Error: Cannot create file %s\n", filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fwrite(data, 1, dataSz, fp);
|
||||
fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* One-shot encryption */
|
||||
static int encrypt_file(const char* inFile, const char* outFile,
|
||||
const byte* key, word32 keySz)
|
||||
{
|
||||
Aes aes;
|
||||
WC_RNG rng;
|
||||
byte iv[AES_BLOCK_SIZE];
|
||||
byte* plaintext = NULL;
|
||||
byte* ciphertext = NULL;
|
||||
word32 plaintextSz;
|
||||
int ret;
|
||||
|
||||
ret = read_file(inFile, &plaintext, &plaintextSz);
|
||||
if (ret != 0) return ret;
|
||||
|
||||
/* IV + ciphertext */
|
||||
ciphertext = (byte*)malloc(plaintextSz + AES_BLOCK_SIZE);
|
||||
if (ciphertext == NULL) {
|
||||
free(plaintext);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Generate random IV */
|
||||
ret = wc_InitRng(&rng);
|
||||
if (ret != 0) {
|
||||
free(plaintext);
|
||||
free(ciphertext);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = wc_RNG_GenerateBlock(&rng, iv, AES_BLOCK_SIZE);
|
||||
wc_FreeRng(&rng);
|
||||
if (ret != 0) {
|
||||
free(plaintext);
|
||||
free(ciphertext);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Initialize AES */
|
||||
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
|
||||
if (ret != 0) {
|
||||
free(plaintext);
|
||||
free(ciphertext);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = wc_AesSetKey(&aes, key, keySz, iv, AES_ENCRYPTION);
|
||||
if (ret != 0) {
|
||||
wc_AesFree(&aes);
|
||||
free(plaintext);
|
||||
free(ciphertext);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* One-shot encrypt */
|
||||
ret = wc_AesOfbEncrypt(&aes, ciphertext + AES_BLOCK_SIZE, plaintext,
|
||||
plaintextSz);
|
||||
wc_AesFree(&aes);
|
||||
|
||||
if (ret != 0) {
|
||||
free(plaintext);
|
||||
free(ciphertext);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Prepend IV to output */
|
||||
memcpy(ciphertext, iv, AES_BLOCK_SIZE);
|
||||
|
||||
ret = write_file(outFile, ciphertext, plaintextSz + AES_BLOCK_SIZE);
|
||||
|
||||
free(plaintext);
|
||||
free(ciphertext);
|
||||
|
||||
printf("AES-OFB encryption complete (one-shot)\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Streaming decryption - process in chunks */
|
||||
static int decrypt_file(const char* inFile, const char* outFile,
|
||||
const byte* key, word32 keySz)
|
||||
{
|
||||
Aes aes;
|
||||
byte iv[AES_BLOCK_SIZE];
|
||||
byte* ciphertext = NULL;
|
||||
byte* plaintext = NULL;
|
||||
word32 ciphertextSz;
|
||||
word32 plaintextSz;
|
||||
word32 offset;
|
||||
word32 chunkSz;
|
||||
int ret;
|
||||
|
||||
ret = read_file(inFile, &ciphertext, &ciphertextSz);
|
||||
if (ret != 0) return ret;
|
||||
|
||||
if (ciphertextSz < AES_BLOCK_SIZE) {
|
||||
free(ciphertext);
|
||||
printf("Error: File too small\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Extract IV from beginning */
|
||||
memcpy(iv, ciphertext, AES_BLOCK_SIZE);
|
||||
|
||||
plaintextSz = ciphertextSz - AES_BLOCK_SIZE;
|
||||
plaintext = (byte*)malloc(plaintextSz);
|
||||
if (plaintext == NULL) {
|
||||
free(ciphertext);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Initialize AES */
|
||||
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
|
||||
if (ret != 0) {
|
||||
free(ciphertext);
|
||||
free(plaintext);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = wc_AesSetKey(&aes, key, keySz, iv, AES_ENCRYPTION);
|
||||
if (ret != 0) {
|
||||
wc_AesFree(&aes);
|
||||
free(ciphertext);
|
||||
free(plaintext);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Streaming decrypt - process in chunks */
|
||||
printf("AES-OFB streaming decryption:\n");
|
||||
offset = 0;
|
||||
while (offset < plaintextSz) {
|
||||
chunkSz = plaintextSz - offset;
|
||||
if (chunkSz > CHUNK_SIZE) {
|
||||
chunkSz = CHUNK_SIZE;
|
||||
}
|
||||
|
||||
ret = wc_AesOfbDecrypt(&aes, plaintext + offset,
|
||||
ciphertext + AES_BLOCK_SIZE + offset, chunkSz);
|
||||
if (ret != 0) {
|
||||
wc_AesFree(&aes);
|
||||
free(ciphertext);
|
||||
free(plaintext);
|
||||
return ret;
|
||||
}
|
||||
|
||||
printf(" Decrypted chunk: offset=%u, size=%u\n", offset, chunkSz);
|
||||
offset += chunkSz;
|
||||
}
|
||||
|
||||
wc_AesFree(&aes);
|
||||
|
||||
ret = write_file(outFile, plaintext, plaintextSz);
|
||||
|
||||
free(ciphertext);
|
||||
free(plaintext);
|
||||
|
||||
printf("AES-OFB decryption complete (streaming)\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
byte key[AES_KEY_SIZE];
|
||||
int ret;
|
||||
|
||||
if (argc != 3) {
|
||||
printf("Usage: %s <input file> <output file>\n", argv[0]);
|
||||
printf("Encrypts input file (one-shot), then decrypts to output file "
|
||||
"(streaming)\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
wolfCrypt_Init();
|
||||
|
||||
/* Use a fixed key for demonstration */
|
||||
memset(key, 0x05, AES_KEY_SIZE);
|
||||
|
||||
/* Encrypt to temporary file */
|
||||
ret = encrypt_file(argv[1], "temp_encrypted.bin", key, AES_KEY_SIZE);
|
||||
if (ret != 0) {
|
||||
printf("Encryption failed: %d\n", ret);
|
||||
wolfCrypt_Cleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Decrypt to output file */
|
||||
ret = decrypt_file("temp_encrypted.bin", argv[2], key, AES_KEY_SIZE);
|
||||
if (ret != 0) {
|
||||
printf("Decryption failed: %d\n", ret);
|
||||
wolfCrypt_Cleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Clean up temp file */
|
||||
remove("temp_encrypted.bin");
|
||||
|
||||
printf("Success! Decrypted file written to %s\n", argv[2]);
|
||||
|
||||
wolfCrypt_Cleanup();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
printf("AES-OFB not compiled in. Enable with WOLFSSL_AES_OFB\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,268 @@
|
|||
/* aes-siv.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
/* AES-SIV Example
|
||||
* This example demonstrates:
|
||||
* - One-shot encryption using wc_AesSivEncrypt()
|
||||
* - One-shot decryption using wc_AesSivDecrypt()
|
||||
* Note: SIV (Synthetic Initialization Vector) provides deterministic
|
||||
* authenticated encryption - no streaming API available
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef WOLFSSL_USER_SETTINGS
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/aes.h>
|
||||
#include <wolfssl/wolfcrypt/random.h>
|
||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||
|
||||
#if !defined(NO_AES) && defined(WOLFSSL_AES_SIV)
|
||||
|
||||
/* SIV uses two keys */
|
||||
#define AES_SIV_KEY_SIZE (AES_256_KEY_SIZE * 2)
|
||||
|
||||
/* SIV tag size */
|
||||
#define SIV_SIZE 16
|
||||
|
||||
static int read_file(const char* filename, byte** data, word32* dataSz)
|
||||
{
|
||||
FILE* fp;
|
||||
long fileSz;
|
||||
|
||||
fp = fopen(filename, "rb");
|
||||
if (fp == NULL) {
|
||||
printf("Error: Cannot open file %s\n", filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
fileSz = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
*data = (byte*)malloc(fileSz);
|
||||
if (*data == NULL) {
|
||||
fclose(fp);
|
||||
printf("Error: Memory allocation failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*dataSz = (word32)fread(*data, 1, fileSz, fp);
|
||||
fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int write_file(const char* filename, const byte* data, word32 dataSz)
|
||||
{
|
||||
FILE* fp;
|
||||
|
||||
fp = fopen(filename, "wb");
|
||||
if (fp == NULL) {
|
||||
printf("Error: Cannot create file %s\n", filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fwrite(data, 1, dataSz, fp);
|
||||
fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* One-shot encryption */
|
||||
static int encrypt_file(const char* inFile, const char* outFile,
|
||||
const byte* key, word32 keySz)
|
||||
{
|
||||
WC_RNG rng;
|
||||
byte nonce[16];
|
||||
byte siv[SIV_SIZE];
|
||||
byte* plaintext = NULL;
|
||||
byte* output = NULL;
|
||||
word32 plaintextSz;
|
||||
word32 outputSz;
|
||||
int ret;
|
||||
|
||||
ret = read_file(inFile, &plaintext, &plaintextSz);
|
||||
if (ret != 0) return ret;
|
||||
|
||||
/* Output: nonce + siv + ciphertext */
|
||||
outputSz = 16 + SIV_SIZE + plaintextSz;
|
||||
output = (byte*)malloc(outputSz);
|
||||
if (output == NULL) {
|
||||
free(plaintext);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Generate random nonce */
|
||||
ret = wc_InitRng(&rng);
|
||||
if (ret != 0) {
|
||||
free(plaintext);
|
||||
free(output);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = wc_RNG_GenerateBlock(&rng, nonce, sizeof(nonce));
|
||||
wc_FreeRng(&rng);
|
||||
if (ret != 0) {
|
||||
free(plaintext);
|
||||
free(output);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* One-shot encrypt with SIV */
|
||||
ret = wc_AesSivEncrypt(key, keySz, NULL, 0,
|
||||
nonce, sizeof(nonce),
|
||||
plaintext, plaintextSz,
|
||||
siv, output + 16 + SIV_SIZE);
|
||||
|
||||
if (ret != 0) {
|
||||
free(plaintext);
|
||||
free(output);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Prepend nonce and SIV to output */
|
||||
memcpy(output, nonce, 16);
|
||||
memcpy(output + 16, siv, SIV_SIZE);
|
||||
|
||||
ret = write_file(outFile, output, outputSz);
|
||||
|
||||
free(plaintext);
|
||||
free(output);
|
||||
|
||||
printf("AES-SIV encryption complete (one-shot)\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* One-shot decryption (no streaming API available for SIV) */
|
||||
static int decrypt_file(const char* inFile, const char* outFile,
|
||||
const byte* key, word32 keySz)
|
||||
{
|
||||
byte nonce[16];
|
||||
byte siv[SIV_SIZE];
|
||||
byte* input = NULL;
|
||||
byte* plaintext = NULL;
|
||||
word32 inputSz;
|
||||
word32 ciphertextSz;
|
||||
int ret;
|
||||
|
||||
ret = read_file(inFile, &input, &inputSz);
|
||||
if (ret != 0) return ret;
|
||||
|
||||
if (inputSz < 16 + SIV_SIZE) {
|
||||
free(input);
|
||||
printf("Error: File too small\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Extract nonce and SIV from beginning */
|
||||
memcpy(nonce, input, 16);
|
||||
memcpy(siv, input + 16, SIV_SIZE);
|
||||
|
||||
ciphertextSz = inputSz - 16 - SIV_SIZE;
|
||||
plaintext = (byte*)malloc(ciphertextSz);
|
||||
if (plaintext == NULL) {
|
||||
free(input);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* One-shot decrypt with SIV verification */
|
||||
ret = wc_AesSivDecrypt(key, keySz, NULL, 0,
|
||||
nonce, sizeof(nonce),
|
||||
input + 16 + SIV_SIZE, ciphertextSz,
|
||||
siv, plaintext);
|
||||
|
||||
if (ret != 0) {
|
||||
free(input);
|
||||
free(plaintext);
|
||||
if (ret == AES_SIV_AUTH_E) {
|
||||
printf("Error: Authentication failed!\n");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = write_file(outFile, plaintext, ciphertextSz);
|
||||
|
||||
free(input);
|
||||
free(plaintext);
|
||||
|
||||
printf("AES-SIV decryption complete (one-shot, no streaming API "
|
||||
"available)\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
byte key[AES_SIV_KEY_SIZE];
|
||||
int ret;
|
||||
|
||||
if (argc != 3) {
|
||||
printf("Usage: %s <input file> <output file>\n", argv[0]);
|
||||
printf("Encrypts input file, then decrypts to output file\n");
|
||||
printf("Note: SIV provides deterministic authenticated encryption\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
wolfCrypt_Init();
|
||||
|
||||
/* Use a fixed key for demonstration (SIV uses two keys) */
|
||||
memset(key, 0x0E, AES_SIV_KEY_SIZE);
|
||||
|
||||
/* Encrypt to temporary file */
|
||||
ret = encrypt_file(argv[1], "temp_encrypted.bin", key, AES_SIV_KEY_SIZE);
|
||||
if (ret != 0) {
|
||||
printf("Encryption failed: %d\n", ret);
|
||||
wolfCrypt_Cleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Decrypt to output file */
|
||||
ret = decrypt_file("temp_encrypted.bin", argv[2], key, AES_SIV_KEY_SIZE);
|
||||
if (ret != 0) {
|
||||
printf("Decryption failed: %d\n", ret);
|
||||
wolfCrypt_Cleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Clean up temp file */
|
||||
remove("temp_encrypted.bin");
|
||||
|
||||
printf("Success! Decrypted file written to %s\n", argv[2]);
|
||||
|
||||
wolfCrypt_Cleanup();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
printf("AES-SIV not compiled in. Enable with WOLFSSL_AES_SIV\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,402 @@
|
|||
/* aes-xts.c
|
||||
*
|
||||
* Copyright (C) 2006-2025 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
/* AES-XTS Example
|
||||
* This example demonstrates:
|
||||
* - One-shot encryption using wc_AesXtsEncrypt()
|
||||
* - Streaming decryption using wc_AesXtsDecryptInit/Update/Final()
|
||||
* Note: XTS is designed for disk encryption (sector-based)
|
||||
* Requires minimum 16 bytes input
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef WOLFSSL_USER_SETTINGS
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/aes.h>
|
||||
#include <wolfssl/wolfcrypt/random.h>
|
||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||
|
||||
#if !defined(NO_AES) && defined(WOLFSSL_AES_XTS)
|
||||
|
||||
/* XTS uses two keys */
|
||||
#define AES_XTS_KEY_SIZE (AES_256_KEY_SIZE * 2)
|
||||
|
||||
/* Tweak/IV size */
|
||||
#define XTS_TWEAK_SIZE 16
|
||||
|
||||
/* Chunk size for streaming demo */
|
||||
#define CHUNK_SIZE 64
|
||||
|
||||
static int read_file(const char* filename, byte** data, word32* dataSz)
|
||||
{
|
||||
FILE* fp;
|
||||
long fileSz;
|
||||
|
||||
fp = fopen(filename, "rb");
|
||||
if (fp == NULL) {
|
||||
printf("Error: Cannot open file %s\n", filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
fileSz = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
*data = (byte*)malloc(fileSz);
|
||||
if (*data == NULL) {
|
||||
fclose(fp);
|
||||
printf("Error: Memory allocation failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*dataSz = (word32)fread(*data, 1, fileSz, fp);
|
||||
fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int write_file(const char* filename, const byte* data, word32 dataSz)
|
||||
{
|
||||
FILE* fp;
|
||||
|
||||
fp = fopen(filename, "wb");
|
||||
if (fp == NULL) {
|
||||
printf("Error: Cannot create file %s\n", filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fwrite(data, 1, dataSz, fp);
|
||||
fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* One-shot encryption */
|
||||
static int encrypt_file(const char* inFile, const char* outFile,
|
||||
const byte* key, word32 keySz)
|
||||
{
|
||||
XtsAes xts;
|
||||
WC_RNG rng;
|
||||
byte tweak[XTS_TWEAK_SIZE];
|
||||
byte* plaintext = NULL;
|
||||
byte* output = NULL;
|
||||
word32 plaintextSz;
|
||||
word32 outputSz;
|
||||
int ret;
|
||||
|
||||
ret = read_file(inFile, &plaintext, &plaintextSz);
|
||||
if (ret != 0) return ret;
|
||||
|
||||
/* XTS requires minimum 16 bytes */
|
||||
if (plaintextSz < AES_BLOCK_SIZE) {
|
||||
printf("Error: Input must be at least %d bytes for XTS\n",
|
||||
AES_BLOCK_SIZE);
|
||||
free(plaintext);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Output: tweak + ciphertext */
|
||||
outputSz = XTS_TWEAK_SIZE + plaintextSz;
|
||||
output = (byte*)malloc(outputSz);
|
||||
if (output == NULL) {
|
||||
free(plaintext);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Generate random tweak */
|
||||
ret = wc_InitRng(&rng);
|
||||
if (ret != 0) {
|
||||
free(plaintext);
|
||||
free(output);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = wc_RNG_GenerateBlock(&rng, tweak, XTS_TWEAK_SIZE);
|
||||
wc_FreeRng(&rng);
|
||||
if (ret != 0) {
|
||||
free(plaintext);
|
||||
free(output);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Initialize AES-XTS */
|
||||
ret = wc_AesXtsSetKey(&xts, key, keySz, AES_ENCRYPTION, NULL,
|
||||
INVALID_DEVID);
|
||||
if (ret != 0) {
|
||||
free(plaintext);
|
||||
free(output);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* One-shot encrypt */
|
||||
ret = wc_AesXtsEncrypt(&xts, output + XTS_TWEAK_SIZE, plaintext,
|
||||
plaintextSz, tweak, XTS_TWEAK_SIZE);
|
||||
wc_AesXtsFree(&xts);
|
||||
|
||||
if (ret != 0) {
|
||||
free(plaintext);
|
||||
free(output);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Prepend tweak to output */
|
||||
memcpy(output, tweak, XTS_TWEAK_SIZE);
|
||||
|
||||
ret = write_file(outFile, output, outputSz);
|
||||
|
||||
free(plaintext);
|
||||
free(output);
|
||||
|
||||
printf("AES-XTS encryption complete (one-shot)\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef WOLFSSL_AESXTS_STREAM
|
||||
/* Streaming decryption using Init/Update/Final API */
|
||||
static int decrypt_file(const char* inFile, const char* outFile,
|
||||
const byte* key, word32 keySz)
|
||||
{
|
||||
XtsAes xts;
|
||||
struct XtsAesStreamData stream;
|
||||
byte tweak[XTS_TWEAK_SIZE];
|
||||
byte* input = NULL;
|
||||
byte* plaintext = NULL;
|
||||
word32 inputSz;
|
||||
word32 ciphertextSz;
|
||||
word32 offset;
|
||||
word32 chunkSz;
|
||||
word32 remaining;
|
||||
int ret;
|
||||
|
||||
ret = read_file(inFile, &input, &inputSz);
|
||||
if (ret != 0) return ret;
|
||||
|
||||
if (inputSz < XTS_TWEAK_SIZE + AES_BLOCK_SIZE) {
|
||||
free(input);
|
||||
printf("Error: File too small\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Extract tweak from beginning */
|
||||
memcpy(tweak, input, XTS_TWEAK_SIZE);
|
||||
|
||||
ciphertextSz = inputSz - XTS_TWEAK_SIZE;
|
||||
plaintext = (byte*)malloc(ciphertextSz);
|
||||
if (plaintext == NULL) {
|
||||
free(input);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Initialize AES-XTS for streaming decryption */
|
||||
ret = wc_AesXtsSetKey(&xts, key, keySz, AES_DECRYPTION, NULL,
|
||||
INVALID_DEVID);
|
||||
if (ret != 0) {
|
||||
free(input);
|
||||
free(plaintext);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = wc_AesXtsDecryptInit(&xts, tweak, XTS_TWEAK_SIZE, &stream);
|
||||
if (ret != 0) {
|
||||
wc_AesXtsFree(&xts);
|
||||
free(input);
|
||||
free(plaintext);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Streaming decrypt - process in chunks */
|
||||
printf("AES-XTS streaming decryption:\n");
|
||||
offset = 0;
|
||||
remaining = ciphertextSz;
|
||||
|
||||
while (remaining > AES_BLOCK_SIZE) {
|
||||
/* Leave at least one block for final */
|
||||
chunkSz = remaining - AES_BLOCK_SIZE;
|
||||
if (chunkSz > CHUNK_SIZE) {
|
||||
chunkSz = CHUNK_SIZE;
|
||||
}
|
||||
/* Ensure chunk is block-aligned for Update */
|
||||
chunkSz = (chunkSz / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
|
||||
if (chunkSz == 0) break;
|
||||
|
||||
ret = wc_AesXtsDecryptUpdate(&xts, plaintext + offset,
|
||||
input + XTS_TWEAK_SIZE + offset,
|
||||
chunkSz, &stream);
|
||||
if (ret != 0) {
|
||||
wc_AesXtsFree(&xts);
|
||||
free(input);
|
||||
free(plaintext);
|
||||
return ret;
|
||||
}
|
||||
|
||||
printf(" Decrypted chunk: offset=%u, size=%u\n", offset, chunkSz);
|
||||
offset += chunkSz;
|
||||
remaining -= chunkSz;
|
||||
}
|
||||
|
||||
/* Final chunk */
|
||||
ret = wc_AesXtsDecryptFinal(&xts, plaintext + offset,
|
||||
input + XTS_TWEAK_SIZE + offset,
|
||||
remaining, &stream);
|
||||
wc_AesXtsFree(&xts);
|
||||
|
||||
if (ret != 0) {
|
||||
free(input);
|
||||
free(plaintext);
|
||||
return ret;
|
||||
}
|
||||
|
||||
printf(" Decrypted final chunk: offset=%u, size=%u\n", offset, remaining);
|
||||
|
||||
ret = write_file(outFile, plaintext, ciphertextSz);
|
||||
|
||||
free(input);
|
||||
free(plaintext);
|
||||
|
||||
printf("AES-XTS decryption complete (streaming)\n");
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
/* Fallback: One-shot decryption when streaming not available */
|
||||
static int decrypt_file(const char* inFile, const char* outFile,
|
||||
const byte* key, word32 keySz)
|
||||
{
|
||||
XtsAes xts;
|
||||
byte tweak[XTS_TWEAK_SIZE];
|
||||
byte* input = NULL;
|
||||
byte* plaintext = NULL;
|
||||
word32 inputSz;
|
||||
word32 ciphertextSz;
|
||||
int ret;
|
||||
|
||||
ret = read_file(inFile, &input, &inputSz);
|
||||
if (ret != 0) return ret;
|
||||
|
||||
if (inputSz < XTS_TWEAK_SIZE + AES_BLOCK_SIZE) {
|
||||
free(input);
|
||||
printf("Error: File too small\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Extract tweak from beginning */
|
||||
memcpy(tweak, input, XTS_TWEAK_SIZE);
|
||||
|
||||
ciphertextSz = inputSz - XTS_TWEAK_SIZE;
|
||||
plaintext = (byte*)malloc(ciphertextSz);
|
||||
if (plaintext == NULL) {
|
||||
free(input);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Initialize AES-XTS */
|
||||
ret = wc_AesXtsSetKey(&xts, key, keySz, AES_DECRYPTION, NULL, INVALID_DEVID);
|
||||
if (ret != 0) {
|
||||
free(input);
|
||||
free(plaintext);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* One-shot decrypt */
|
||||
ret = wc_AesXtsDecrypt(&xts, plaintext, input + XTS_TWEAK_SIZE,
|
||||
ciphertextSz, tweak, XTS_TWEAK_SIZE);
|
||||
wc_AesXtsFree(&xts);
|
||||
|
||||
if (ret != 0) {
|
||||
free(input);
|
||||
free(plaintext);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = write_file(outFile, plaintext, ciphertextSz);
|
||||
|
||||
free(input);
|
||||
free(plaintext);
|
||||
|
||||
printf("AES-XTS decryption complete (one-shot, streaming not available)\n");
|
||||
return ret;
|
||||
}
|
||||
#endif /* WOLFSSL_AESXTS_STREAM */
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
byte key[AES_XTS_KEY_SIZE];
|
||||
int ret;
|
||||
|
||||
if (argc != 3) {
|
||||
printf("Usage: %s <input file> <output file>\n", argv[0]);
|
||||
printf("Encrypts input file (one-shot), then decrypts to output "
|
||||
"file\n");
|
||||
printf("Note: Input must be at least 16 bytes\n");
|
||||
#ifdef WOLFSSL_AESXTS_STREAM
|
||||
printf("Streaming decryption enabled\n");
|
||||
#else
|
||||
printf("Streaming decryption not available "
|
||||
"(enable WOLFSSL_AESXTS_STREAM)\n");
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
||||
wolfCrypt_Init();
|
||||
|
||||
/* Use a fixed key for demonstration (XTS uses two keys) */
|
||||
memset(key, 0x0D, AES_XTS_KEY_SIZE);
|
||||
|
||||
/* Encrypt to temporary file */
|
||||
ret = encrypt_file(argv[1], "temp_encrypted.bin", key, AES_XTS_KEY_SIZE);
|
||||
if (ret != 0) {
|
||||
printf("Encryption failed: %d\n", ret);
|
||||
wolfCrypt_Cleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Decrypt to output file */
|
||||
ret = decrypt_file("temp_encrypted.bin", argv[2], key, AES_XTS_KEY_SIZE);
|
||||
if (ret != 0) {
|
||||
printf("Decryption failed: %d\n", ret);
|
||||
wolfCrypt_Cleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Clean up temp file */
|
||||
remove("temp_encrypted.bin");
|
||||
|
||||
printf("Success! Decrypted file written to %s\n", argv[2]);
|
||||
|
||||
wolfCrypt_Cleanup();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
printf("AES-XTS not compiled in. Enable with WOLFSSL_AES_XTS\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
Reference in New Issue