Skip to content

Commit 0ecf716

Browse files
Merge pull request #547 from anhu/aes_modes_eax_ecb
AES modes extravaganza. eax and ecb
2 parents dbceabd + 2de853e commit 0ecf716

2 files changed

Lines changed: 586 additions & 0 deletions

File tree

crypto/aes-modes/aes-eax.c

Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
/* aes-eax.c
2+
*
3+
* Copyright (C) 2006-2025 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
/* AES-EAX Example
23+
* This example demonstrates:
24+
* - One-shot encryption using wc_AesEaxEncryptAuth()
25+
* - Streaming decryption using wc_AesEaxInit/DecryptUpdate/DecryptFinal()
26+
* Note: EAX provides authenticated encryption (AEAD)
27+
*/
28+
29+
#include <stdio.h>
30+
#include <stdlib.h>
31+
#include <string.h>
32+
33+
#ifndef WOLFSSL_USER_SETTINGS
34+
#include <wolfssl/options.h>
35+
#endif
36+
#include <wolfssl/wolfcrypt/settings.h>
37+
#include <wolfssl/wolfcrypt/aes.h>
38+
#include <wolfssl/wolfcrypt/random.h>
39+
#include <wolfssl/wolfcrypt/error-crypt.h>
40+
41+
#if !defined(NO_AES) && defined(WOLFSSL_AES_EAX)
42+
43+
#define AES_KEY_SIZE AES_256_KEY_SIZE
44+
#define EAX_NONCE_SIZE 16 /* Nonce size */
45+
#define EAX_TAG_SIZE 16 /* Authentication tag size */
46+
#define CHUNK_SIZE 64 /* Chunk size for streaming demo */
47+
48+
static int read_file(const char* filename, byte** data, word32* dataSz)
49+
{
50+
FILE* fp;
51+
long fileSz;
52+
53+
fp = fopen(filename, "rb");
54+
if (fp == NULL) {
55+
printf("Error: Cannot open file %s\n", filename);
56+
return -1;
57+
}
58+
59+
fseek(fp, 0, SEEK_END);
60+
fileSz = ftell(fp);
61+
fseek(fp, 0, SEEK_SET);
62+
63+
*data = (byte*)malloc(fileSz);
64+
if (*data == NULL) {
65+
fclose(fp);
66+
printf("Error: Memory allocation failed\n");
67+
return -1;
68+
}
69+
70+
*dataSz = (word32)fread(*data, 1, fileSz, fp);
71+
fclose(fp);
72+
73+
return 0;
74+
}
75+
76+
static int write_file(const char* filename, const byte* data, word32 dataSz)
77+
{
78+
FILE* fp;
79+
80+
fp = fopen(filename, "wb");
81+
if (fp == NULL) {
82+
printf("Error: Cannot create file %s\n", filename);
83+
return -1;
84+
}
85+
86+
fwrite(data, 1, dataSz, fp);
87+
fclose(fp);
88+
89+
return 0;
90+
}
91+
92+
/* One-shot encryption */
93+
static int encrypt_file(const char* inFile, const char* outFile,
94+
const byte* key, word32 keySz)
95+
{
96+
WC_RNG rng;
97+
byte nonce[EAX_NONCE_SIZE];
98+
byte authTag[EAX_TAG_SIZE];
99+
byte* plaintext = NULL;
100+
byte* output = NULL;
101+
word32 plaintextSz;
102+
word32 outputSz;
103+
int ret;
104+
105+
ret = read_file(inFile, &plaintext, &plaintextSz);
106+
if (ret != 0) return ret;
107+
108+
/* Output: nonce + authTag + ciphertext */
109+
outputSz = EAX_NONCE_SIZE + EAX_TAG_SIZE + plaintextSz;
110+
output = (byte*)malloc(outputSz);
111+
if (output == NULL) {
112+
free(plaintext);
113+
return -1;
114+
}
115+
116+
/* Generate random nonce */
117+
ret = wc_InitRng(&rng);
118+
if (ret != 0) {
119+
free(plaintext);
120+
free(output);
121+
return ret;
122+
}
123+
124+
ret = wc_RNG_GenerateBlock(&rng, nonce, EAX_NONCE_SIZE);
125+
wc_FreeRng(&rng);
126+
if (ret != 0) {
127+
free(plaintext);
128+
free(output);
129+
return ret;
130+
}
131+
132+
/* One-shot encrypt with authentication */
133+
/* Note: authIn must be non-NULL even when authInSz is 0 */
134+
{
135+
static const byte emptyAad = 0;
136+
ret = wc_AesEaxEncryptAuth(key, keySz,
137+
output + EAX_NONCE_SIZE + EAX_TAG_SIZE,
138+
plaintext, plaintextSz,
139+
nonce, EAX_NONCE_SIZE,
140+
authTag, EAX_TAG_SIZE,
141+
&emptyAad, 0); /* No additional auth data */
142+
}
143+
144+
if (ret != 0) {
145+
free(plaintext);
146+
free(output);
147+
return ret;
148+
}
149+
150+
/* Prepend nonce and authTag to output */
151+
memcpy(output, nonce, EAX_NONCE_SIZE);
152+
memcpy(output + EAX_NONCE_SIZE, authTag, EAX_TAG_SIZE);
153+
154+
ret = write_file(outFile, output, outputSz);
155+
156+
free(plaintext);
157+
free(output);
158+
159+
printf("AES-EAX encryption complete (one-shot)\n");
160+
return ret;
161+
}
162+
163+
/* Streaming decryption using Init/Update/Final API */
164+
static int decrypt_file(const char* inFile, const char* outFile,
165+
const byte* key, word32 keySz)
166+
{
167+
AesEax eax;
168+
byte nonce[EAX_NONCE_SIZE];
169+
byte authTag[EAX_TAG_SIZE];
170+
byte* input = NULL;
171+
byte* plaintext = NULL;
172+
word32 inputSz;
173+
word32 ciphertextSz;
174+
word32 offset;
175+
word32 chunkSz;
176+
int ret;
177+
178+
ret = read_file(inFile, &input, &inputSz);
179+
if (ret != 0) return ret;
180+
181+
if (inputSz < EAX_NONCE_SIZE + EAX_TAG_SIZE) {
182+
free(input);
183+
printf("Error: File too small\n");
184+
return -1;
185+
}
186+
187+
/* Extract nonce and authTag from beginning */
188+
memcpy(nonce, input, EAX_NONCE_SIZE);
189+
memcpy(authTag, input + EAX_NONCE_SIZE, EAX_TAG_SIZE);
190+
191+
ciphertextSz = inputSz - EAX_NONCE_SIZE - EAX_TAG_SIZE;
192+
plaintext = (byte*)malloc(ciphertextSz);
193+
if (plaintext == NULL) {
194+
free(input);
195+
return -1;
196+
}
197+
198+
/* Initialize AES-EAX for streaming decryption */
199+
ret = wc_AesEaxInit(&eax, key, keySz, nonce, EAX_NONCE_SIZE, NULL, 0);
200+
if (ret != 0) {
201+
free(input);
202+
free(plaintext);
203+
return ret;
204+
}
205+
206+
/* Streaming decrypt - process in chunks */
207+
printf("AES-EAX streaming decryption:\n");
208+
offset = 0;
209+
while (offset < ciphertextSz) {
210+
chunkSz = ciphertextSz - offset;
211+
if (chunkSz > CHUNK_SIZE) {
212+
chunkSz = CHUNK_SIZE;
213+
}
214+
215+
ret = wc_AesEaxDecryptUpdate(&eax, plaintext + offset,
216+
input + EAX_NONCE_SIZE + EAX_TAG_SIZE +
217+
offset, chunkSz, NULL, 0);
218+
if (ret != 0) {
219+
wc_AesEaxFree(&eax);
220+
free(input);
221+
free(plaintext);
222+
return ret;
223+
}
224+
225+
printf(" Decrypted chunk: offset=%u, size=%u\n", offset, chunkSz);
226+
offset += chunkSz;
227+
}
228+
229+
/* Finalize and verify authentication tag */
230+
ret = wc_AesEaxDecryptFinal(&eax, authTag, EAX_TAG_SIZE);
231+
wc_AesEaxFree(&eax);
232+
233+
if (ret != 0) {
234+
free(input);
235+
free(plaintext);
236+
printf("Error: Authentication failed!\n");
237+
return ret;
238+
}
239+
240+
ret = write_file(outFile, plaintext, ciphertextSz);
241+
242+
free(input);
243+
free(plaintext);
244+
245+
printf("AES-EAX decryption complete (streaming) - authentication "
246+
"verified\n");
247+
return ret;
248+
}
249+
250+
int main(int argc, char** argv)
251+
{
252+
byte key[AES_KEY_SIZE];
253+
int ret;
254+
255+
if (argc != 3) {
256+
printf("Usage: %s <input file> <output file>\n", argv[0]);
257+
printf("Encrypts input file (one-shot), then decrypts to output file "
258+
"(streaming)\n");
259+
return 1;
260+
}
261+
262+
wolfCrypt_Init();
263+
264+
/* Use a fixed key for demonstration */
265+
memset(key, 0x0F, AES_KEY_SIZE);
266+
267+
/* Encrypt to temporary file */
268+
ret = encrypt_file(argv[1], "temp_encrypted.bin", key, AES_KEY_SIZE);
269+
if (ret != 0) {
270+
printf("Encryption failed: %d\n", ret);
271+
wolfCrypt_Cleanup();
272+
return 1;
273+
}
274+
275+
/* Decrypt to output file */
276+
ret = decrypt_file("temp_encrypted.bin", argv[2], key, AES_KEY_SIZE);
277+
if (ret != 0) {
278+
printf("Decryption failed: %d\n", ret);
279+
wolfCrypt_Cleanup();
280+
return 1;
281+
}
282+
283+
/* Clean up temp file */
284+
remove("temp_encrypted.bin");
285+
286+
printf("Success! Decrypted file written to %s\n", argv[2]);
287+
288+
wolfCrypt_Cleanup();
289+
return 0;
290+
}
291+
292+
#else
293+
294+
int main(int argc, char** argv)
295+
{
296+
(void)argc;
297+
(void)argv;
298+
printf("AES-EAX not compiled in. Enable with WOLFSSL_AES_EAX\n");
299+
return 0;
300+
}
301+
302+
#endif

0 commit comments

Comments
 (0)