xref: /freebsd/crypto/openssl/demos/cipher/aesccm.c (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 /*
2  * Copyright 2013-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 /*
11  * Simple AES CCM authenticated encryption with additional data (AEAD)
12  * demonstration program.
13  */
14 
15 #include <stdio.h>
16 #include <openssl/err.h>
17 #include <openssl/bio.h>
18 #include <openssl/evp.h>
19 #include <openssl/core_names.h>
20 
21 /* AES-CCM test data obtained from NIST public test vectors */
22 
23 /* AES key */
24 static const unsigned char ccm_key[] = {
25     0xce, 0xb0, 0x09, 0xae, 0xa4, 0x45, 0x44, 0x51, 0xfe, 0xad, 0xf0, 0xe6,
26     0xb3, 0x6f, 0x45, 0x55, 0x5d, 0xd0, 0x47, 0x23, 0xba, 0xa4, 0x48, 0xe8
27 };
28 
29 /* Unique nonce to be used for this message */
30 static const unsigned char ccm_nonce[] = {
31     0x76, 0x40, 0x43, 0xc4, 0x94, 0x60, 0xb7
32 };
33 
34 /*
35  * Example of Additional Authenticated Data (AAD), i.e. unencrypted data
36  * which can be authenticated using the generated Tag value.
37  */
38 static const unsigned char ccm_adata[] = {
39     0x6e, 0x80, 0xdd, 0x7f, 0x1b, 0xad, 0xf3, 0xa1, 0xc9, 0xab, 0x25, 0xc7,
40     0x5f, 0x10, 0xbd, 0xe7, 0x8c, 0x23, 0xfa, 0x0e, 0xb8, 0xf9, 0xaa, 0xa5,
41     0x3a, 0xde, 0xfb, 0xf4, 0xcb, 0xf7, 0x8f, 0xe4
42 };
43 
44 /* Example plaintext to encrypt */
45 static const unsigned char ccm_pt[] = {
46     0xc8, 0xd2, 0x75, 0xf9, 0x19, 0xe1, 0x7d, 0x7f, 0xe6, 0x9c, 0x2a, 0x1f,
47     0x58, 0x93, 0x9d, 0xfe, 0x4d, 0x40, 0x37, 0x91, 0xb5, 0xdf, 0x13, 0x10
48 };
49 
50 /* Expected ciphertext value */
51 static const unsigned char ccm_ct[] = {
52     0x8a, 0x0f, 0x3d, 0x82, 0x29, 0xe4, 0x8e, 0x74, 0x87, 0xfd, 0x95, 0xa2,
53     0x8a, 0xd3, 0x92, 0xc8, 0x0b, 0x36, 0x81, 0xd4, 0xfb, 0xc7, 0xbb, 0xfd
54 };
55 
56 /* Expected AEAD Tag value */
57 static const unsigned char ccm_tag[] = {
58     0x2d, 0xd6, 0xef, 0x1c, 0x45, 0xd4, 0xcc, 0xb7, 0x23, 0xdc, 0x07, 0x44,
59     0x14, 0xdb, 0x50, 0x6d
60 };
61 
62 /*
63  * A library context and property query can be used to select & filter
64  * algorithm implementations. If they are NULL then the default library
65  * context and properties are used.
66  */
67 OSSL_LIB_CTX *libctx = NULL;
68 const char *propq = NULL;
69 
70 
71 int aes_ccm_encrypt(void)
72 {
73     int ret = 0;
74     EVP_CIPHER_CTX *ctx;
75     EVP_CIPHER *cipher = NULL;
76     int outlen, tmplen;
77     size_t ccm_nonce_len = sizeof(ccm_nonce);
78     size_t ccm_tag_len = sizeof(ccm_tag);
79     unsigned char outbuf[1024];
80     unsigned char outtag[16];
81     OSSL_PARAM params[3] = {
82         OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
83     };
84 
85     printf("AES CCM Encrypt:\n");
86     printf("Plaintext:\n");
87     BIO_dump_fp(stdout, ccm_pt, sizeof(ccm_pt));
88 
89     /* Create a context for the encrypt operation */
90     if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
91         goto err;
92 
93     /* Fetch the cipher implementation */
94     if ((cipher = EVP_CIPHER_fetch(libctx, "AES-192-CCM", propq)) == NULL)
95         goto err;
96 
97     /* Set nonce length if default 96 bits is not appropriate */
98     params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
99                                             &ccm_nonce_len);
100     /* Set tag length */
101     params[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
102                                                   NULL, ccm_tag_len);
103 
104     /*
105      * Initialise encrypt operation with the cipher & mode,
106      * nonce length and tag length parameters.
107      */
108     if (!EVP_EncryptInit_ex2(ctx, cipher, NULL, NULL, params))
109         goto err;
110 
111     /* Initialise key and nonce */
112     if (!EVP_EncryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce))
113         goto err;
114 
115     /* Set plaintext length: only needed if AAD is used */
116     if (!EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_pt)))
117         goto err;
118 
119     /* Zero or one call to specify any AAD */
120     if (!EVP_EncryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata)))
121         goto err;
122 
123     /* Encrypt plaintext: can only be called once */
124     if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, ccm_pt, sizeof(ccm_pt)))
125         goto err;
126 
127     /* Output encrypted block */
128     printf("Ciphertext:\n");
129     BIO_dump_fp(stdout, outbuf, outlen);
130 
131     /* Finalise: note get no output for CCM */
132     if (!EVP_EncryptFinal_ex(ctx, NULL, &tmplen))
133         goto err;
134 
135     /* Get tag */
136     params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
137                                                   outtag, ccm_tag_len);
138     params[1] = OSSL_PARAM_construct_end();
139 
140     if (!EVP_CIPHER_CTX_get_params(ctx, params))
141         goto err;
142 
143     /* Output tag */
144     printf("Tag:\n");
145     BIO_dump_fp(stdout, outtag, ccm_tag_len);
146 
147     ret = 1;
148 err:
149     if (!ret)
150         ERR_print_errors_fp(stderr);
151 
152     EVP_CIPHER_free(cipher);
153     EVP_CIPHER_CTX_free(ctx);
154 
155     return ret;
156 }
157 
158 int aes_ccm_decrypt(void)
159 {
160     int ret = 0;
161     EVP_CIPHER_CTX *ctx;
162     EVP_CIPHER *cipher = NULL;
163     int outlen, rv;
164     unsigned char outbuf[1024];
165     size_t ccm_nonce_len = sizeof(ccm_nonce);
166     OSSL_PARAM params[3] = {
167         OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
168     };
169 
170     printf("AES CCM Decrypt:\n");
171     printf("Ciphertext:\n");
172     BIO_dump_fp(stdout, ccm_ct, sizeof(ccm_ct));
173 
174     if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
175         goto err;
176 
177     /* Fetch the cipher implementation */
178     if ((cipher = EVP_CIPHER_fetch(libctx, "AES-192-CCM", propq)) == NULL)
179         goto err;
180 
181     /* Set nonce length if default 96 bits is not appropriate */
182     params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
183                                             &ccm_nonce_len);
184     /* Set tag length */
185     params[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
186                                                   (unsigned char *)ccm_tag,
187                                                   sizeof(ccm_tag));
188     /*
189      * Initialise decrypt operation with the cipher & mode,
190      * nonce length and expected tag parameters.
191      */
192     if (!EVP_DecryptInit_ex2(ctx, cipher, NULL, NULL, params))
193         goto err;
194 
195     /* Specify key and IV */
196     if (!EVP_DecryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce))
197         goto err;
198 
199     /* Set ciphertext length: only needed if we have AAD */
200     if (!EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_ct)))
201         goto err;
202 
203     /* Zero or one call to specify any AAD */
204     if (!EVP_DecryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata)))
205         goto err;
206 
207     /* Decrypt plaintext, verify tag: can only be called once */
208     rv = EVP_DecryptUpdate(ctx, outbuf, &outlen, ccm_ct, sizeof(ccm_ct));
209 
210     /* Output decrypted block: if tag verify failed we get nothing */
211     if (rv > 0) {
212         printf("Tag verify successful!\nPlaintext:\n");
213         BIO_dump_fp(stdout, outbuf, outlen);
214     } else {
215         printf("Tag verify failed!\nPlaintext not available\n");
216         goto err;
217     }
218     ret = 1;
219 err:
220     if (!ret)
221         ERR_print_errors_fp(stderr);
222 
223     EVP_CIPHER_free(cipher);
224     EVP_CIPHER_CTX_free(ctx);
225 
226     return ret;
227 }
228 
229 int main(int argc, char **argv)
230 {
231     if (!aes_ccm_encrypt())
232         return 1;
233 
234     if (!aes_ccm_decrypt())
235         return 1;
236 
237     return 0;
238 }
239