2 * Copyright 2013-2021 The OpenSSL Project Authors. All Rights Reserved.
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
11 * Simple AES CCM authenticated encryption with additional data (AEAD)
12 * demonstration program.
16 #include <openssl/err.h>
17 #include <openssl/bio.h>
18 #include <openssl/evp.h>
19 #include <openssl/core_names.h>
21 /* AES-CCM test data obtained from NIST public test vectors */
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
29 /* Unique nonce to be used for this message */
30 static const unsigned char ccm_nonce
[] = {
31 0x76, 0x40, 0x43, 0xc4, 0x94, 0x60, 0xb7
35 * Example of Additional Authenticated Data (AAD), i.e. unencrypted data
36 * which can be authenticated using the generated Tag value.
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
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
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
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
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.
67 OSSL_LIB_CTX
*libctx
= NULL
;
68 const char *propq
= NULL
;
71 int aes_ccm_encrypt(void)
75 EVP_CIPHER
*cipher
= NULL
;
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
85 printf("AES CCM Encrypt:\n");
86 printf("Plaintext:\n");
87 BIO_dump_fp(stdout
, ccm_pt
, sizeof(ccm_pt
));
89 /* Create a context for the encrypt operation */
90 if ((ctx
= EVP_CIPHER_CTX_new()) == NULL
)
93 /* Fetch the cipher implementation */
94 if ((cipher
= EVP_CIPHER_fetch(libctx
, "AES-192-CCM", propq
)) == NULL
)
97 /* Set nonce length if default 96 bits is not appropriate */
98 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN
,
101 params
[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG
,
105 * Initialise encrypt operation with the cipher & mode,
106 * nonce length and tag length parameters.
108 if (!EVP_EncryptInit_ex2(ctx
, cipher
, NULL
, NULL
, params
))
111 /* Initialise key and nonce */
112 if (!EVP_EncryptInit_ex(ctx
, NULL
, NULL
, ccm_key
, ccm_nonce
))
115 /* Set plaintext length: only needed if AAD is used */
116 if (!EVP_EncryptUpdate(ctx
, NULL
, &outlen
, NULL
, sizeof(ccm_pt
)))
119 /* Zero or one call to specify any AAD */
120 if (!EVP_EncryptUpdate(ctx
, NULL
, &outlen
, ccm_adata
, sizeof(ccm_adata
)))
123 /* Encrypt plaintext: can only be called once */
124 if (!EVP_EncryptUpdate(ctx
, outbuf
, &outlen
, ccm_pt
, sizeof(ccm_pt
)))
127 /* Output encrypted block */
128 printf("Ciphertext:\n");
129 BIO_dump_fp(stdout
, outbuf
, outlen
);
131 /* Finalise: note get no output for CCM */
132 if (!EVP_EncryptFinal_ex(ctx
, NULL
, &tmplen
))
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();
140 if (!EVP_CIPHER_CTX_get_params(ctx
, params
))
145 BIO_dump_fp(stdout
, outtag
, ccm_tag_len
);
150 ERR_print_errors_fp(stderr
);
152 EVP_CIPHER_free(cipher
);
153 EVP_CIPHER_CTX_free(ctx
);
158 int aes_ccm_decrypt(void)
162 EVP_CIPHER
*cipher
= NULL
;
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
170 printf("AES CCM Decrypt:\n");
171 printf("Ciphertext:\n");
172 BIO_dump_fp(stdout
, ccm_ct
, sizeof(ccm_ct
));
174 if ((ctx
= EVP_CIPHER_CTX_new()) == NULL
)
177 /* Fetch the cipher implementation */
178 if ((cipher
= EVP_CIPHER_fetch(libctx
, "AES-192-CCM", propq
)) == NULL
)
181 /* Set nonce length if default 96 bits is not appropriate */
182 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN
,
185 params
[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG
,
186 (unsigned char *)ccm_tag
,
189 * Initialise decrypt operation with the cipher & mode,
190 * nonce length and expected tag parameters.
192 if (!EVP_DecryptInit_ex2(ctx
, cipher
, NULL
, NULL
, params
))
195 /* Specify key and IV */
196 if (!EVP_DecryptInit_ex(ctx
, NULL
, NULL
, ccm_key
, ccm_nonce
))
199 /* Set ciphertext length: only needed if we have AAD */
200 if (!EVP_DecryptUpdate(ctx
, NULL
, &outlen
, NULL
, sizeof(ccm_ct
)))
203 /* Zero or one call to specify any AAD */
204 if (!EVP_DecryptUpdate(ctx
, NULL
, &outlen
, ccm_adata
, sizeof(ccm_adata
)))
207 /* Decrypt plaintext, verify tag: can only be called once */
208 rv
= EVP_DecryptUpdate(ctx
, outbuf
, &outlen
, ccm_ct
, sizeof(ccm_ct
));
210 /* Output decrypted block: if tag verify failed we get nothing */
212 printf("Tag verify successful!\nPlaintext:\n");
213 BIO_dump_fp(stdout
, outbuf
, outlen
);
215 printf("Tag verify failed!\nPlaintext not available\n");
221 ERR_print_errors_fp(stderr
);
223 EVP_CIPHER_free(cipher
);
224 EVP_CIPHER_CTX_free(ctx
);
229 int main(int argc
, char **argv
)
231 if (!aes_ccm_encrypt())
234 if (!aes_ccm_decrypt())