Skip to content

Commit dc54fd7

Browse files
Merge pull request #541 from anhu/aes_modes_cbc
AES modes extravaganza. CBC
2 parents 13282ce + ee87548 commit dc54fd7

1 file changed

Lines changed: 308 additions & 0 deletions

File tree

crypto/aes-modes/aes-cbc.c

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
/* aes-cbc.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-CBC Example
23+
* This example demonstrates:
24+
* - One-shot encryption using wc_AesCbcEncrypt()
25+
* - One-shot decryption using wc_AesCbcDecrypt()
26+
* Note: AES-CBC does not have a streaming API
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(HAVE_AES_CBC)
42+
43+
#define AES_KEY_SIZE AES_256_KEY_SIZE
44+
#define BUFFER_SIZE 4096
45+
46+
static int read_file(const char* filename, byte** data, word32* dataSz)
47+
{
48+
FILE* fp;
49+
long fileSz;
50+
51+
fp = fopen(filename, "rb");
52+
if (fp == NULL) {
53+
printf("Error: Cannot open file %s\n", filename);
54+
return -1;
55+
}
56+
57+
fseek(fp, 0, SEEK_END);
58+
fileSz = ftell(fp);
59+
fseek(fp, 0, SEEK_SET);
60+
61+
*data = (byte*)malloc(fileSz);
62+
if (*data == NULL) {
63+
fclose(fp);
64+
printf("Error: Memory allocation failed\n");
65+
return -1;
66+
}
67+
68+
*dataSz = (word32)fread(*data, 1, fileSz, fp);
69+
fclose(fp);
70+
71+
return 0;
72+
}
73+
74+
static int write_file(const char* filename, const byte* data, word32 dataSz)
75+
{
76+
FILE* fp;
77+
78+
fp = fopen(filename, "wb");
79+
if (fp == NULL) {
80+
printf("Error: Cannot create file %s\n", filename);
81+
return -1;
82+
}
83+
84+
fwrite(data, 1, dataSz, fp);
85+
fclose(fp);
86+
87+
return 0;
88+
}
89+
90+
/* One-shot encryption */
91+
static int encrypt_file(const char* inFile, const char* outFile,
92+
const byte* key, word32 keySz)
93+
{
94+
Aes aes;
95+
WC_RNG rng;
96+
byte iv[AES_BLOCK_SIZE];
97+
byte* plaintext = NULL;
98+
byte* ciphertext = NULL;
99+
word32 plaintextSz;
100+
word32 paddedSz;
101+
word32 padLen;
102+
int ret;
103+
104+
ret = read_file(inFile, &plaintext, &plaintextSz);
105+
if (ret != 0) return ret;
106+
107+
/* Calculate padded size (PKCS#7 padding) */
108+
padLen = AES_BLOCK_SIZE - (plaintextSz % AES_BLOCK_SIZE);
109+
paddedSz = plaintextSz + padLen;
110+
111+
/* IV + ciphertext */
112+
ciphertext = (byte*)malloc(paddedSz + AES_BLOCK_SIZE);
113+
if (ciphertext == NULL) {
114+
free(plaintext);
115+
return -1;
116+
}
117+
118+
/* Generate random IV */
119+
ret = wc_InitRng(&rng);
120+
if (ret != 0) {
121+
free(plaintext);
122+
free(ciphertext);
123+
return ret;
124+
}
125+
126+
ret = wc_RNG_GenerateBlock(&rng, iv, AES_BLOCK_SIZE);
127+
wc_FreeRng(&rng);
128+
if (ret != 0) {
129+
free(plaintext);
130+
free(ciphertext);
131+
return ret;
132+
}
133+
134+
/* Apply PKCS#7 padding */
135+
plaintext = (byte*)realloc(plaintext, paddedSz);
136+
memset(plaintext + plaintextSz, (int)padLen, padLen);
137+
138+
/* Initialize AES */
139+
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
140+
if (ret != 0) {
141+
free(plaintext);
142+
free(ciphertext);
143+
return ret;
144+
}
145+
146+
ret = wc_AesSetKey(&aes, key, keySz, iv, AES_ENCRYPTION);
147+
if (ret != 0) {
148+
wc_AesFree(&aes);
149+
free(plaintext);
150+
free(ciphertext);
151+
return ret;
152+
}
153+
154+
/* One-shot encrypt */
155+
ret = wc_AesCbcEncrypt(&aes, ciphertext + AES_BLOCK_SIZE, plaintext,
156+
paddedSz);
157+
wc_AesFree(&aes);
158+
159+
if (ret != 0) {
160+
free(plaintext);
161+
free(ciphertext);
162+
return ret;
163+
}
164+
165+
/* Prepend IV to output */
166+
memcpy(ciphertext, iv, AES_BLOCK_SIZE);
167+
168+
ret = write_file(outFile, ciphertext, paddedSz + AES_BLOCK_SIZE);
169+
170+
free(plaintext);
171+
free(ciphertext);
172+
173+
printf("AES-CBC encryption complete (one-shot)\n");
174+
return ret;
175+
}
176+
177+
/* One-shot decryption (no streaming API available for CBC) */
178+
static int decrypt_file(const char* inFile, const char* outFile,
179+
const byte* key, word32 keySz)
180+
{
181+
Aes aes;
182+
byte iv[AES_BLOCK_SIZE];
183+
byte* ciphertext = NULL;
184+
byte* plaintext = NULL;
185+
word32 ciphertextSz;
186+
word32 plaintextSz;
187+
byte padLen;
188+
int ret;
189+
190+
ret = read_file(inFile, &ciphertext, &ciphertextSz);
191+
if (ret != 0) return ret;
192+
193+
if (ciphertextSz < AES_BLOCK_SIZE * 2) {
194+
free(ciphertext);
195+
printf("Error: File too small\n");
196+
return -1;
197+
}
198+
199+
/* Extract IV from beginning */
200+
memcpy(iv, ciphertext, AES_BLOCK_SIZE);
201+
202+
plaintextSz = ciphertextSz - AES_BLOCK_SIZE;
203+
plaintext = (byte*)malloc(plaintextSz);
204+
if (plaintext == NULL) {
205+
free(ciphertext);
206+
return -1;
207+
}
208+
209+
/* Initialize AES */
210+
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
211+
if (ret != 0) {
212+
free(ciphertext);
213+
free(plaintext);
214+
return ret;
215+
}
216+
217+
ret = wc_AesSetKey(&aes, key, keySz, iv, AES_DECRYPTION);
218+
if (ret != 0) {
219+
wc_AesFree(&aes);
220+
free(ciphertext);
221+
free(plaintext);
222+
return ret;
223+
}
224+
225+
/* One-shot decrypt */
226+
ret = wc_AesCbcDecrypt(&aes, plaintext, ciphertext + AES_BLOCK_SIZE,
227+
plaintextSz);
228+
wc_AesFree(&aes);
229+
230+
if (ret != 0) {
231+
free(ciphertext);
232+
free(plaintext);
233+
return ret;
234+
}
235+
236+
/* Remove PKCS#7 padding */
237+
padLen = plaintext[plaintextSz - 1];
238+
if (padLen > AES_BLOCK_SIZE || padLen == 0) {
239+
free(ciphertext);
240+
free(plaintext);
241+
printf("Error: Invalid padding\n");
242+
return -1;
243+
}
244+
plaintextSz -= padLen;
245+
246+
ret = write_file(outFile, plaintext, plaintextSz);
247+
248+
free(ciphertext);
249+
free(plaintext);
250+
251+
printf("AES-CBC decryption complete (one-shot, no streaming API "
252+
"available)\n");
253+
return ret;
254+
}
255+
256+
int main(int argc, char** argv)
257+
{
258+
byte key[AES_KEY_SIZE];
259+
int ret;
260+
261+
if (argc != 3) {
262+
printf("Usage: %s <input file> <output file>\n", argv[0]);
263+
printf("Encrypts input file, then decrypts to output file\n");
264+
return 1;
265+
}
266+
267+
wolfCrypt_Init();
268+
269+
/* Use a fixed key for demonstration (in production, use secure key
270+
* management) */
271+
memset(key, 0x01, AES_KEY_SIZE);
272+
273+
/* Encrypt to temporary file */
274+
ret = encrypt_file(argv[1], "temp_encrypted.bin", key, AES_KEY_SIZE);
275+
if (ret != 0) {
276+
printf("Encryption failed: %d\n", ret);
277+
wolfCrypt_Cleanup();
278+
return 1;
279+
}
280+
281+
/* Decrypt to output file */
282+
ret = decrypt_file("temp_encrypted.bin", argv[2], key, AES_KEY_SIZE);
283+
if (ret != 0) {
284+
printf("Decryption failed: %d\n", ret);
285+
wolfCrypt_Cleanup();
286+
return 1;
287+
}
288+
289+
/* Clean up temp file */
290+
remove("temp_encrypted.bin");
291+
292+
printf("Success! Decrypted file written to %s\n", argv[2]);
293+
294+
wolfCrypt_Cleanup();
295+
return 0;
296+
}
297+
298+
#else
299+
300+
int main(int argc, char** argv)
301+
{
302+
(void)argc;
303+
(void)argv;
304+
printf("AES-CBC not compiled in. Enable with HAVE_AES_CBC\n");
305+
return 0;
306+
}
307+
308+
#endif

0 commit comments

Comments
 (0)