Skip to content

Commit 4aa0d37

Browse files
committed
AES-GCM one shot example
Add a simple example of using the AES-GCM oneshot API for encryption and decryption.
1 parent f707ad8 commit 4aa0d37

3 files changed

Lines changed: 149 additions & 2 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ crypto/3des/3des-file-encrypt
118118
crypto/aes/aes-file-encrypt
119119
crypto/aes/aescfb-file-encrypt
120120
crypto/aes/aesctr-file-encrypt
121+
crypto/aes/aesgcm-file-encrypt
122+
crypto/aes/aesgcm-oneshot
121123
crypto/camellia/camellia-encrypt
122124
crypto/pkcs12/pkcs12-create-example
123125
crypto/pkcs12/pkcs12-example

crypto/aes/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ CFLAGS=-Wall
33
WOLFSSL_INSTALL_DIR=/usr/local
44
LIBS=-L$(WOLFSSL_INSTALL_DIR)/lib -lwolfssl -lm
55

6-
all: aes-file-encrypt aescfb-file-encrypt aesctr-file-encrypt aesgcm-file-encrypt
6+
all: aes-file-encrypt aescfb-file-encrypt aesctr-file-encrypt aesgcm-file-encrypt aesgcm-oneshot
77

88
aes-file-encrypt: aes-file-encrypt.o
99
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
@@ -17,7 +17,10 @@ aesctr-file-encrypt: aesctr-file-encrypt.o
1717
aesgcm-file-encrypt: aesgcm-file-encrypt.o
1818
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
1919

20+
aesgcm-oneshot: aesgcm-oneshot.o
21+
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
22+
2023
.PHONY: clean
2124

2225
clean:
23-
rm -f *.o aes-file-encrypt aescfb-file-encrypt aesctr-file-encrypt aesgcm-file-encrypt text*
26+
rm -f *.o aes-file-encrypt aescfb-file-encrypt aesctr-file-encrypt aesgcm-file-encrypt text* aesgcm-oneshot

crypto/aes/aesgcm-oneshot.c

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/* aesgcm-oneshot.c
2+
*
3+
* Copyright (C) 2006-2024 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+
23+
#include <wolfssl/options.h>
24+
#include <wolfssl/wolfcrypt/settings.h>
25+
#include <wolfssl/wolfcrypt/error-crypt.h>
26+
#include <wolfssl/wolfcrypt/logging.h>
27+
#include <wolfssl/wolfcrypt/aes.h>
28+
29+
#if !defined(NO_AES) && defined(HAVE_AESGCM)
30+
int aesgcm_enc_dec()
31+
{
32+
Aes aesEnc;
33+
Aes aesDec;
34+
unsigned char key[AES_256_KEY_SIZE];
35+
int ret = 0;
36+
unsigned char data[33];
37+
unsigned char enc[33];
38+
unsigned char dec[33];
39+
unsigned char iv[GCM_NONCE_MID_SZ];
40+
unsigned char authTag[AES_BLOCK_SIZE];
41+
size_t i;
42+
43+
memset(key, 9, sizeof(key));
44+
memset(data, 9, sizeof(data));
45+
memset(iv, 9, sizeof(iv));
46+
47+
fprintf(stderr, "Encrypt with AES128-GCM\n");
48+
/* Initialize AES encryption object. */
49+
ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
50+
if (ret == 0) {
51+
/* Set GCM key into AES encryption object. */
52+
ret = wc_AesGcmSetKey(&aesEnc, key, AES_128_KEY_SIZE);
53+
if (ret != 0)
54+
fprintf(stderr, "Set Key failed: %d\n", ret);
55+
}
56+
if (ret == 0) {
57+
printf(" Plaintext: ");
58+
for (i = 0; i < sizeof(data); i++)
59+
printf("%02x", data[i]);
60+
printf("\n");
61+
62+
/* Encrypt data with AES encryption object and get ciphertext and
63+
* authentication tag. No additional authentication data. */
64+
ret = wc_AesGcmEncrypt(&aesEnc, enc, data, sizeof(data), iv, sizeof(iv),
65+
authTag, sizeof(authTag), NULL, 0);
66+
if (ret != 0)
67+
fprintf(stderr, "Encrypt failed: %d\n", ret);
68+
}
69+
if (ret == 0) {
70+
printf("Ciphertext: ");
71+
for (i = 0; i < sizeof(data); i++)
72+
printf("%02x", enc[i]);
73+
printf("\n");
74+
printf(" Auth Tag: ");
75+
for (i = 0; i < sizeof(authTag); i++)
76+
printf("%02x", authTag[i]);
77+
printf("\n");
78+
}
79+
80+
if (ret == 0) {
81+
fprintf(stderr, "Decrypt with AES128-GCM\n");
82+
/* Initialize AES decryption object. */
83+
ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
84+
}
85+
if (ret == 0) {
86+
/* Set GCM key into AES decryption object. */
87+
ret = wc_AesGcmSetKey(&aesDec, key, AES_128_KEY_SIZE);
88+
if (ret != 0)
89+
fprintf(stderr, "Set Key failed: %d\n", ret);
90+
}
91+
if (ret == 0) {
92+
/* Check authentication tag with ciphertext and decrypt ciphertext with
93+
* AES decryption object and get decrypted data. No additional
94+
* authentication data. */
95+
ret = wc_AesGcmDecrypt(&aesDec, dec, enc, sizeof(enc), iv, sizeof(iv),
96+
authTag, sizeof(authTag), NULL, 0);
97+
if (ret == AES_GCM_AUTH_E)
98+
fprintf(stderr, "Authentication failed: %d\n", ret);
99+
else if (ret != 0)
100+
fprintf(stderr, "Decrypt failed: %d\n", ret);
101+
}
102+
if (ret == 0) {
103+
printf(" Decrypted: ");
104+
for (i = 0; i < sizeof(data); i++)
105+
printf("%02x", dec[i]);
106+
printf("\n");
107+
}
108+
109+
return ret;
110+
}
111+
#endif
112+
113+
int main(int argc, char* argv[])
114+
{
115+
int ret = 0;
116+
117+
if (argc != 1) {
118+
fprintf(stderr, "Usage: %s\n", argv[0]);
119+
return 1;
120+
}
121+
122+
#if !defined(NO_AES) && defined(HAVE_AESGCM)
123+
124+
#if defined(DEBUG_WOLFSSL)
125+
wolfSSL_Debugging_ON();
126+
#endif
127+
wolfCrypt_Init();
128+
129+
if (aesgcm_enc_dec() != 0)
130+
ret = 1;
131+
132+
wolfCrypt_Cleanup();
133+
134+
#else
135+
136+
printf("AES-GCM not built into wolfSSL\n");
137+
138+
#endif
139+
140+
return ret;
141+
}
142+

0 commit comments

Comments
 (0)