xref: /freebsd/crypto/openssl/demos/cipher/aesgcm.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2012-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert /*
11*e0c4386eSCy Schubert  * Simple AES GCM authenticated encryption with additional data (AEAD)
12*e0c4386eSCy Schubert  * demonstration program.
13*e0c4386eSCy Schubert  */
14*e0c4386eSCy Schubert 
15*e0c4386eSCy Schubert #include <stdio.h>
16*e0c4386eSCy Schubert #include <openssl/err.h>
17*e0c4386eSCy Schubert #include <openssl/bio.h>
18*e0c4386eSCy Schubert #include <openssl/evp.h>
19*e0c4386eSCy Schubert #include <openssl/core_names.h>
20*e0c4386eSCy Schubert 
21*e0c4386eSCy Schubert /* AES-GCM test data obtained from NIST public test vectors */
22*e0c4386eSCy Schubert 
23*e0c4386eSCy Schubert /* AES key */
24*e0c4386eSCy Schubert static const unsigned char gcm_key[] = {
25*e0c4386eSCy Schubert     0xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66,
26*e0c4386eSCy Schubert     0x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69,
27*e0c4386eSCy Schubert     0xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f
28*e0c4386eSCy Schubert };
29*e0c4386eSCy Schubert 
30*e0c4386eSCy Schubert /* Unique initialisation vector */
31*e0c4386eSCy Schubert static const unsigned char gcm_iv[] = {
32*e0c4386eSCy Schubert     0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84
33*e0c4386eSCy Schubert };
34*e0c4386eSCy Schubert 
35*e0c4386eSCy Schubert /* Example plaintext to encrypt */
36*e0c4386eSCy Schubert static const unsigned char gcm_pt[] = {
37*e0c4386eSCy Schubert     0xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e, 0xeb, 0x31, 0xb2, 0xea,
38*e0c4386eSCy Schubert     0xcc, 0x2b, 0xf2, 0xa5
39*e0c4386eSCy Schubert };
40*e0c4386eSCy Schubert 
41*e0c4386eSCy Schubert /*
42*e0c4386eSCy Schubert  * Example of Additional Authenticated Data (AAD), i.e. unencrypted data
43*e0c4386eSCy Schubert  * which can be authenticated using the generated Tag value.
44*e0c4386eSCy Schubert  */
45*e0c4386eSCy Schubert static const unsigned char gcm_aad[] = {
46*e0c4386eSCy Schubert     0x4d, 0x23, 0xc3, 0xce, 0xc3, 0x34, 0xb4, 0x9b, 0xdb, 0x37, 0x0c, 0x43,
47*e0c4386eSCy Schubert     0x7f, 0xec, 0x78, 0xde
48*e0c4386eSCy Schubert };
49*e0c4386eSCy Schubert 
50*e0c4386eSCy Schubert /* Expected ciphertext value */
51*e0c4386eSCy Schubert static const unsigned char gcm_ct[] = {
52*e0c4386eSCy Schubert     0xf7, 0x26, 0x44, 0x13, 0xa8, 0x4c, 0x0e, 0x7c, 0xd5, 0x36, 0x86, 0x7e,
53*e0c4386eSCy Schubert     0xb9, 0xf2, 0x17, 0x36
54*e0c4386eSCy Schubert };
55*e0c4386eSCy Schubert 
56*e0c4386eSCy Schubert /* Expected AEAD Tag value */
57*e0c4386eSCy Schubert static const unsigned char gcm_tag[] = {
58*e0c4386eSCy Schubert     0x67, 0xba, 0x05, 0x10, 0x26, 0x2a, 0xe4, 0x87, 0xd7, 0x37, 0xee, 0x62,
59*e0c4386eSCy Schubert     0x98, 0xf7, 0x7e, 0x0c
60*e0c4386eSCy Schubert };
61*e0c4386eSCy Schubert 
62*e0c4386eSCy Schubert /*
63*e0c4386eSCy Schubert  * A library context and property query can be used to select & filter
64*e0c4386eSCy Schubert  * algorithm implementations. If they are NULL then the default library
65*e0c4386eSCy Schubert  * context and properties are used.
66*e0c4386eSCy Schubert  */
67*e0c4386eSCy Schubert OSSL_LIB_CTX *libctx = NULL;
68*e0c4386eSCy Schubert const char *propq = NULL;
69*e0c4386eSCy Schubert 
aes_gcm_encrypt(void)70*e0c4386eSCy Schubert int aes_gcm_encrypt(void)
71*e0c4386eSCy Schubert {
72*e0c4386eSCy Schubert     int ret = 0;
73*e0c4386eSCy Schubert     EVP_CIPHER_CTX *ctx;
74*e0c4386eSCy Schubert     EVP_CIPHER *cipher = NULL;
75*e0c4386eSCy Schubert     int outlen, tmplen;
76*e0c4386eSCy Schubert     size_t gcm_ivlen = sizeof(gcm_iv);
77*e0c4386eSCy Schubert     unsigned char outbuf[1024];
78*e0c4386eSCy Schubert     unsigned char outtag[16];
79*e0c4386eSCy Schubert     OSSL_PARAM params[2] = {
80*e0c4386eSCy Schubert         OSSL_PARAM_END, OSSL_PARAM_END
81*e0c4386eSCy Schubert     };
82*e0c4386eSCy Schubert 
83*e0c4386eSCy Schubert     printf("AES GCM Encrypt:\n");
84*e0c4386eSCy Schubert     printf("Plaintext:\n");
85*e0c4386eSCy Schubert     BIO_dump_fp(stdout, gcm_pt, sizeof(gcm_pt));
86*e0c4386eSCy Schubert 
87*e0c4386eSCy Schubert     /* Create a context for the encrypt operation */
88*e0c4386eSCy Schubert     if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
89*e0c4386eSCy Schubert         goto err;
90*e0c4386eSCy Schubert 
91*e0c4386eSCy Schubert     /* Fetch the cipher implementation */
92*e0c4386eSCy Schubert     if ((cipher = EVP_CIPHER_fetch(libctx, "AES-256-GCM", propq)) == NULL)
93*e0c4386eSCy Schubert         goto err;
94*e0c4386eSCy Schubert 
95*e0c4386eSCy Schubert     /* Set IV length if default 96 bits is not appropriate */
96*e0c4386eSCy Schubert     params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
97*e0c4386eSCy Schubert                                             &gcm_ivlen);
98*e0c4386eSCy Schubert 
99*e0c4386eSCy Schubert     /*
100*e0c4386eSCy Schubert      * Initialise an encrypt operation with the cipher/mode, key, IV and
101*e0c4386eSCy Schubert      * IV length parameter.
102*e0c4386eSCy Schubert      * For demonstration purposes the IV is being set here. In a compliant
103*e0c4386eSCy Schubert      * application the IV would be generated internally so the iv passed in
104*e0c4386eSCy Schubert      * would be NULL.
105*e0c4386eSCy Schubert      */
106*e0c4386eSCy Schubert     if (!EVP_EncryptInit_ex2(ctx, cipher, gcm_key, gcm_iv, params))
107*e0c4386eSCy Schubert         goto err;
108*e0c4386eSCy Schubert 
109*e0c4386eSCy Schubert     /* Zero or more calls to specify any AAD */
110*e0c4386eSCy Schubert     if (!EVP_EncryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad)))
111*e0c4386eSCy Schubert         goto err;
112*e0c4386eSCy Schubert 
113*e0c4386eSCy Schubert     /* Encrypt plaintext */
114*e0c4386eSCy Schubert     if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, gcm_pt, sizeof(gcm_pt)))
115*e0c4386eSCy Schubert         goto err;
116*e0c4386eSCy Schubert 
117*e0c4386eSCy Schubert     /* Output encrypted block */
118*e0c4386eSCy Schubert     printf("Ciphertext:\n");
119*e0c4386eSCy Schubert     BIO_dump_fp(stdout, outbuf, outlen);
120*e0c4386eSCy Schubert 
121*e0c4386eSCy Schubert     /* Finalise: note get no output for GCM */
122*e0c4386eSCy Schubert     if (!EVP_EncryptFinal_ex(ctx, outbuf, &tmplen))
123*e0c4386eSCy Schubert         goto err;
124*e0c4386eSCy Schubert 
125*e0c4386eSCy Schubert     /* Get tag */
126*e0c4386eSCy Schubert     params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
127*e0c4386eSCy Schubert                                                   outtag, 16);
128*e0c4386eSCy Schubert 
129*e0c4386eSCy Schubert     if (!EVP_CIPHER_CTX_get_params(ctx, params))
130*e0c4386eSCy Schubert         goto err;
131*e0c4386eSCy Schubert 
132*e0c4386eSCy Schubert     /* Output tag */
133*e0c4386eSCy Schubert     printf("Tag:\n");
134*e0c4386eSCy Schubert     BIO_dump_fp(stdout, outtag, 16);
135*e0c4386eSCy Schubert 
136*e0c4386eSCy Schubert     ret = 1;
137*e0c4386eSCy Schubert err:
138*e0c4386eSCy Schubert     if (!ret)
139*e0c4386eSCy Schubert         ERR_print_errors_fp(stderr);
140*e0c4386eSCy Schubert 
141*e0c4386eSCy Schubert     EVP_CIPHER_free(cipher);
142*e0c4386eSCy Schubert     EVP_CIPHER_CTX_free(ctx);
143*e0c4386eSCy Schubert 
144*e0c4386eSCy Schubert     return ret;
145*e0c4386eSCy Schubert }
146*e0c4386eSCy Schubert 
aes_gcm_decrypt(void)147*e0c4386eSCy Schubert int aes_gcm_decrypt(void)
148*e0c4386eSCy Schubert {
149*e0c4386eSCy Schubert     int ret = 0;
150*e0c4386eSCy Schubert     EVP_CIPHER_CTX *ctx;
151*e0c4386eSCy Schubert     EVP_CIPHER *cipher = NULL;
152*e0c4386eSCy Schubert     int outlen, rv;
153*e0c4386eSCy Schubert     size_t gcm_ivlen = sizeof(gcm_iv);
154*e0c4386eSCy Schubert     unsigned char outbuf[1024];
155*e0c4386eSCy Schubert     OSSL_PARAM params[2] = {
156*e0c4386eSCy Schubert         OSSL_PARAM_END, OSSL_PARAM_END
157*e0c4386eSCy Schubert     };
158*e0c4386eSCy Schubert 
159*e0c4386eSCy Schubert     printf("AES GCM Decrypt:\n");
160*e0c4386eSCy Schubert     printf("Ciphertext:\n");
161*e0c4386eSCy Schubert     BIO_dump_fp(stdout, gcm_ct, sizeof(gcm_ct));
162*e0c4386eSCy Schubert 
163*e0c4386eSCy Schubert     if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
164*e0c4386eSCy Schubert         goto err;
165*e0c4386eSCy Schubert 
166*e0c4386eSCy Schubert     /* Fetch the cipher implementation */
167*e0c4386eSCy Schubert     if ((cipher = EVP_CIPHER_fetch(libctx, "AES-256-GCM", propq)) == NULL)
168*e0c4386eSCy Schubert         goto err;
169*e0c4386eSCy Schubert 
170*e0c4386eSCy Schubert     /* Set IV length if default 96 bits is not appropriate */
171*e0c4386eSCy Schubert     params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
172*e0c4386eSCy Schubert                                             &gcm_ivlen);
173*e0c4386eSCy Schubert 
174*e0c4386eSCy Schubert     /*
175*e0c4386eSCy Schubert      * Initialise an encrypt operation with the cipher/mode, key, IV and
176*e0c4386eSCy Schubert      * IV length parameter.
177*e0c4386eSCy Schubert      */
178*e0c4386eSCy Schubert     if (!EVP_DecryptInit_ex2(ctx, cipher, gcm_key, gcm_iv, params))
179*e0c4386eSCy Schubert         goto err;
180*e0c4386eSCy Schubert 
181*e0c4386eSCy Schubert     /* Zero or more calls to specify any AAD */
182*e0c4386eSCy Schubert     if (!EVP_DecryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad)))
183*e0c4386eSCy Schubert         goto err;
184*e0c4386eSCy Schubert 
185*e0c4386eSCy Schubert     /* Decrypt plaintext */
186*e0c4386eSCy Schubert     if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, gcm_ct, sizeof(gcm_ct)))
187*e0c4386eSCy Schubert         goto err;
188*e0c4386eSCy Schubert 
189*e0c4386eSCy Schubert     /* Output decrypted block */
190*e0c4386eSCy Schubert     printf("Plaintext:\n");
191*e0c4386eSCy Schubert     BIO_dump_fp(stdout, outbuf, outlen);
192*e0c4386eSCy Schubert 
193*e0c4386eSCy Schubert     /* Set expected tag value. */
194*e0c4386eSCy Schubert     params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
195*e0c4386eSCy Schubert                                                   (void*)gcm_tag, sizeof(gcm_tag));
196*e0c4386eSCy Schubert 
197*e0c4386eSCy Schubert     if (!EVP_CIPHER_CTX_set_params(ctx, params))
198*e0c4386eSCy Schubert         goto err;
199*e0c4386eSCy Schubert 
200*e0c4386eSCy Schubert     /* Finalise: note get no output for GCM */
201*e0c4386eSCy Schubert     rv = EVP_DecryptFinal_ex(ctx, outbuf, &outlen);
202*e0c4386eSCy Schubert     /*
203*e0c4386eSCy Schubert      * Print out return value. If this is not successful authentication
204*e0c4386eSCy Schubert      * failed and plaintext is not trustworthy.
205*e0c4386eSCy Schubert      */
206*e0c4386eSCy Schubert     printf("Tag Verify %s\n", rv > 0 ? "Successful!" : "Failed!");
207*e0c4386eSCy Schubert 
208*e0c4386eSCy Schubert     ret = 1;
209*e0c4386eSCy Schubert err:
210*e0c4386eSCy Schubert     if (!ret)
211*e0c4386eSCy Schubert         ERR_print_errors_fp(stderr);
212*e0c4386eSCy Schubert 
213*e0c4386eSCy Schubert     EVP_CIPHER_free(cipher);
214*e0c4386eSCy Schubert     EVP_CIPHER_CTX_free(ctx);
215*e0c4386eSCy Schubert 
216*e0c4386eSCy Schubert     return ret;
217*e0c4386eSCy Schubert }
218*e0c4386eSCy Schubert 
main(int argc,char ** argv)219*e0c4386eSCy Schubert int main(int argc, char **argv)
220*e0c4386eSCy Schubert {
221*e0c4386eSCy Schubert     if (!aes_gcm_encrypt())
222*e0c4386eSCy Schubert         return 1;
223*e0c4386eSCy Schubert 
224*e0c4386eSCy Schubert     if (!aes_gcm_decrypt())
225*e0c4386eSCy Schubert         return 1;
226*e0c4386eSCy Schubert 
227*e0c4386eSCy Schubert     return 0;
228*e0c4386eSCy Schubert }
229