Skip to content

Commit 37da886

Browse files
Merge pull request #542 from anhu/aes_modes_ccm
AES modes extravaganza. CCM
2 parents dc54fd7 + 804bbf1 commit 37da886

1 file changed

Lines changed: 302 additions & 0 deletions

File tree

crypto/aes-modes/aes-ccm.c

Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
/* aes-ccm.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-CCM Example
23+
* This example demonstrates:
24+
* - One-shot encryption using wc_AesCcmEncrypt()
25+
* - One-shot decryption using wc_AesCcmDecrypt()
26+
* Note: CCM provides authenticated encryption (AEAD), no 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_AESCCM)
42+
43+
#define AES_KEY_SIZE AES_256_KEY_SIZE
44+
#define CCM_NONCE_SIZE 12 /* Nonce size (7-13 bytes allowed) */
45+
#define CCM_TAG_SIZE 16 /* Authentication tag size */
46+
47+
static int read_file(const char* filename, byte** data, word32* dataSz)
48+
{
49+
FILE* fp;
50+
long fileSz;
51+
52+
fp = fopen(filename, "rb");
53+
if (fp == NULL) {
54+
printf("Error: Cannot open file %s\n", filename);
55+
return -1;
56+
}
57+
58+
fseek(fp, 0, SEEK_END);
59+
fileSz = ftell(fp);
60+
fseek(fp, 0, SEEK_SET);
61+
62+
*data = (byte*)malloc(fileSz);
63+
if (*data == NULL) {
64+
fclose(fp);
65+
printf("Error: Memory allocation failed\n");
66+
return -1;
67+
}
68+
69+
*dataSz = (word32)fread(*data, 1, fileSz, fp);
70+
fclose(fp);
71+
72+
return 0;
73+
}
74+
75+
static int write_file(const char* filename, const byte* data, word32 dataSz)
76+
{
77+
FILE* fp;
78+
79+
fp = fopen(filename, "wb");
80+
if (fp == NULL) {
81+
printf("Error: Cannot create file %s\n", filename);
82+
return -1;
83+
}
84+
85+
fwrite(data, 1, dataSz, fp);
86+
fclose(fp);
87+
88+
return 0;
89+
}
90+
91+
/* One-shot encryption */
92+
static int encrypt_file(const char* inFile, const char* outFile,
93+
const byte* key, word32 keySz)
94+
{
95+
Aes aes;
96+
WC_RNG rng;
97+
byte nonce[CCM_NONCE_SIZE];
98+
byte authTag[CCM_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 = CCM_NONCE_SIZE + CCM_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, CCM_NONCE_SIZE);
125+
wc_FreeRng(&rng);
126+
if (ret != 0) {
127+
free(plaintext);
128+
free(output);
129+
return ret;
130+
}
131+
132+
/* Initialize AES-CCM */
133+
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
134+
if (ret != 0) {
135+
free(plaintext);
136+
free(output);
137+
return ret;
138+
}
139+
140+
ret = wc_AesCcmSetKey(&aes, key, keySz);
141+
if (ret != 0) {
142+
wc_AesFree(&aes);
143+
free(plaintext);
144+
free(output);
145+
return ret;
146+
}
147+
148+
/* One-shot encrypt with authentication */
149+
ret = wc_AesCcmEncrypt(&aes, output + CCM_NONCE_SIZE + CCM_TAG_SIZE,
150+
plaintext, plaintextSz,
151+
nonce, CCM_NONCE_SIZE,
152+
authTag, CCM_TAG_SIZE,
153+
NULL, 0); /* No additional authenticated data */
154+
wc_AesFree(&aes);
155+
156+
if (ret != 0) {
157+
free(plaintext);
158+
free(output);
159+
return ret;
160+
}
161+
162+
/* Prepend nonce and authTag to output */
163+
memcpy(output, nonce, CCM_NONCE_SIZE);
164+
memcpy(output + CCM_NONCE_SIZE, authTag, CCM_TAG_SIZE);
165+
166+
ret = write_file(outFile, output, outputSz);
167+
168+
free(plaintext);
169+
free(output);
170+
171+
printf("AES-CCM encryption complete (one-shot)\n");
172+
return ret;
173+
}
174+
175+
/* One-shot decryption (no streaming API available for CCM) */
176+
static int decrypt_file(const char* inFile, const char* outFile,
177+
const byte* key, word32 keySz)
178+
{
179+
Aes aes;
180+
byte nonce[CCM_NONCE_SIZE];
181+
byte authTag[CCM_TAG_SIZE];
182+
byte* input = NULL;
183+
byte* plaintext = NULL;
184+
word32 inputSz;
185+
word32 ciphertextSz;
186+
int ret;
187+
188+
ret = read_file(inFile, &input, &inputSz);
189+
if (ret != 0) return ret;
190+
191+
if (inputSz < CCM_NONCE_SIZE + CCM_TAG_SIZE) {
192+
free(input);
193+
printf("Error: File too small\n");
194+
return -1;
195+
}
196+
197+
/* Extract nonce and authTag from beginning */
198+
memcpy(nonce, input, CCM_NONCE_SIZE);
199+
memcpy(authTag, input + CCM_NONCE_SIZE, CCM_TAG_SIZE);
200+
201+
ciphertextSz = inputSz - CCM_NONCE_SIZE - CCM_TAG_SIZE;
202+
plaintext = (byte*)malloc(ciphertextSz);
203+
if (plaintext == NULL) {
204+
free(input);
205+
return -1;
206+
}
207+
208+
/* Initialize AES-CCM */
209+
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
210+
if (ret != 0) {
211+
free(input);
212+
free(plaintext);
213+
return ret;
214+
}
215+
216+
ret = wc_AesCcmSetKey(&aes, key, keySz);
217+
if (ret != 0) {
218+
wc_AesFree(&aes);
219+
free(input);
220+
free(plaintext);
221+
return ret;
222+
}
223+
224+
/* One-shot decrypt with authentication verification */
225+
ret = wc_AesCcmDecrypt(&aes, plaintext,
226+
input + CCM_NONCE_SIZE + CCM_TAG_SIZE, ciphertextSz,
227+
nonce, CCM_NONCE_SIZE,
228+
authTag, CCM_TAG_SIZE,
229+
NULL, 0);
230+
wc_AesFree(&aes);
231+
232+
if (ret != 0) {
233+
free(input);
234+
free(plaintext);
235+
if (ret == AES_CCM_AUTH_E) {
236+
printf("Error: Authentication failed!\n");
237+
}
238+
return ret;
239+
}
240+
241+
ret = write_file(outFile, plaintext, ciphertextSz);
242+
243+
free(input);
244+
free(plaintext);
245+
246+
printf("AES-CCM decryption complete (one-shot, no streaming API "
247+
"available)\n");
248+
return ret;
249+
}
250+
251+
int main(int argc, char** argv)
252+
{
253+
byte key[AES_KEY_SIZE];
254+
int ret;
255+
256+
if (argc != 3) {
257+
printf("Usage: %s <input file> <output file>\n", argv[0]);
258+
printf("Encrypts input file, then decrypts to output file\n");
259+
return 1;
260+
}
261+
262+
wolfCrypt_Init();
263+
264+
/* Use a fixed key for demonstration */
265+
memset(key, 0x0B, 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-CCM not compiled in. Enable with HAVE_AESCCM\n");
299+
return 0;
300+
}
301+
302+
#endif

0 commit comments

Comments
 (0)