Path: blob/main/crypto/openssl/demos/cipher/aesccm.c
34879 views
/*1* Copyright 2013-2024 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/*10* Simple AES CCM authenticated encryption with additional data (AEAD)11* demonstration program.12*/1314#include <stdio.h>15#include <openssl/err.h>16#include <openssl/bio.h>17#include <openssl/evp.h>18#include <openssl/core_names.h>1920/* AES-CCM test data obtained from NIST public test vectors */2122/* AES key */23static const unsigned char ccm_key[] = {240xce, 0xb0, 0x09, 0xae, 0xa4, 0x45, 0x44, 0x51, 0xfe, 0xad, 0xf0, 0xe6,250xb3, 0x6f, 0x45, 0x55, 0x5d, 0xd0, 0x47, 0x23, 0xba, 0xa4, 0x48, 0xe826};2728/* Unique nonce to be used for this message */29static const unsigned char ccm_nonce[] = {300x76, 0x40, 0x43, 0xc4, 0x94, 0x60, 0xb731};3233/*34* Example of Additional Authenticated Data (AAD), i.e. unencrypted data35* which can be authenticated using the generated Tag value.36*/37static const unsigned char ccm_adata[] = {380x6e, 0x80, 0xdd, 0x7f, 0x1b, 0xad, 0xf3, 0xa1, 0xc9, 0xab, 0x25, 0xc7,390x5f, 0x10, 0xbd, 0xe7, 0x8c, 0x23, 0xfa, 0x0e, 0xb8, 0xf9, 0xaa, 0xa5,400x3a, 0xde, 0xfb, 0xf4, 0xcb, 0xf7, 0x8f, 0xe441};4243/* Example plaintext to encrypt */44static const unsigned char ccm_pt[] = {450xc8, 0xd2, 0x75, 0xf9, 0x19, 0xe1, 0x7d, 0x7f, 0xe6, 0x9c, 0x2a, 0x1f,460x58, 0x93, 0x9d, 0xfe, 0x4d, 0x40, 0x37, 0x91, 0xb5, 0xdf, 0x13, 0x1047};4849/* Expected ciphertext value */50static const unsigned char ccm_ct[] = {510x8a, 0x0f, 0x3d, 0x82, 0x29, 0xe4, 0x8e, 0x74, 0x87, 0xfd, 0x95, 0xa2,520x8a, 0xd3, 0x92, 0xc8, 0x0b, 0x36, 0x81, 0xd4, 0xfb, 0xc7, 0xbb, 0xfd53};5455/* Expected AEAD Tag value */56static const unsigned char ccm_tag[] = {570x2d, 0xd6, 0xef, 0x1c, 0x45, 0xd4, 0xcc, 0xb7, 0x23, 0xdc, 0x07, 0x44,580x14, 0xdb, 0x50, 0x6d59};6061/*62* A library context and property query can be used to select & filter63* algorithm implementations. If they are NULL then the default library64* context and properties are used.65*/66static OSSL_LIB_CTX *libctx = NULL;67static const char *propq = NULL;686970static int aes_ccm_encrypt(void)71{72int ret = 0;73EVP_CIPHER_CTX *ctx;74EVP_CIPHER *cipher = NULL;75int outlen, tmplen;76size_t ccm_nonce_len = sizeof(ccm_nonce);77size_t ccm_tag_len = sizeof(ccm_tag);78unsigned char outbuf[1024];79unsigned char outtag[16];80OSSL_PARAM params[3] = {81OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END82};8384printf("AES CCM Encrypt:\n");85printf("Plaintext:\n");86BIO_dump_fp(stdout, ccm_pt, sizeof(ccm_pt));8788/* Create a context for the encrypt operation */89if ((ctx = EVP_CIPHER_CTX_new()) == NULL)90goto err;9192/* Fetch the cipher implementation */93if ((cipher = EVP_CIPHER_fetch(libctx, "AES-192-CCM", propq)) == NULL)94goto err;9596/* Default nonce length for AES-CCM is 7 bytes (56 bits). */97params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,98&ccm_nonce_len);99/* Set tag length */100params[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,101NULL, ccm_tag_len);102103/*104* Initialise encrypt operation with the cipher & mode,105* nonce length and tag length parameters.106*/107if (!EVP_EncryptInit_ex2(ctx, cipher, NULL, NULL, params))108goto err;109110/* Initialise key and nonce */111if (!EVP_EncryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce))112goto err;113114/* Set plaintext length: only needed if AAD is used */115if (!EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_pt)))116goto err;117118/* Zero or one call to specify any AAD */119if (!EVP_EncryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata)))120goto err;121122/* Encrypt plaintext: can only be called once */123if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, ccm_pt, sizeof(ccm_pt)))124goto err;125126/* Output encrypted block */127printf("Ciphertext:\n");128BIO_dump_fp(stdout, outbuf, outlen);129130/* Finalise: note get no output for CCM */131if (!EVP_EncryptFinal_ex(ctx, NULL, &tmplen))132goto err;133134/* Get tag */135params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,136outtag, ccm_tag_len);137params[1] = OSSL_PARAM_construct_end();138139if (!EVP_CIPHER_CTX_get_params(ctx, params))140goto err;141142/* Output tag */143printf("Tag:\n");144BIO_dump_fp(stdout, outtag, ccm_tag_len);145146ret = 1;147err:148if (!ret)149ERR_print_errors_fp(stderr);150151EVP_CIPHER_free(cipher);152EVP_CIPHER_CTX_free(ctx);153154return ret;155}156157static int aes_ccm_decrypt(void)158{159int ret = 0;160EVP_CIPHER_CTX *ctx;161EVP_CIPHER *cipher = NULL;162int outlen, rv;163unsigned char outbuf[1024];164size_t ccm_nonce_len = sizeof(ccm_nonce);165OSSL_PARAM params[3] = {166OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END167};168169printf("AES CCM Decrypt:\n");170printf("Ciphertext:\n");171BIO_dump_fp(stdout, ccm_ct, sizeof(ccm_ct));172173if ((ctx = EVP_CIPHER_CTX_new()) == NULL)174goto err;175176/* Fetch the cipher implementation */177if ((cipher = EVP_CIPHER_fetch(libctx, "AES-192-CCM", propq)) == NULL)178goto err;179180/* Set nonce length if default 96 bits is not appropriate */181params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,182&ccm_nonce_len);183/* Set tag length */184params[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,185(unsigned char *)ccm_tag,186sizeof(ccm_tag));187/*188* Initialise decrypt operation with the cipher & mode,189* nonce length and expected tag parameters.190*/191if (!EVP_DecryptInit_ex2(ctx, cipher, NULL, NULL, params))192goto err;193194/* Specify key and IV */195if (!EVP_DecryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce))196goto err;197198/* Set ciphertext length: only needed if we have AAD */199if (!EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_ct)))200goto err;201202/* Zero or one call to specify any AAD */203if (!EVP_DecryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata)))204goto err;205206/* Decrypt plaintext, verify tag: can only be called once */207rv = EVP_DecryptUpdate(ctx, outbuf, &outlen, ccm_ct, sizeof(ccm_ct));208209/* Output decrypted block: if tag verify failed we get nothing */210if (rv > 0) {211printf("Tag verify successful!\nPlaintext:\n");212BIO_dump_fp(stdout, outbuf, outlen);213} else {214printf("Tag verify failed!\nPlaintext not available\n");215goto err;216}217ret = 1;218err:219if (!ret)220ERR_print_errors_fp(stderr);221222EVP_CIPHER_free(cipher);223EVP_CIPHER_CTX_free(ctx);224225return ret;226}227228int main(int argc, char **argv)229{230if (!aes_ccm_encrypt())231return EXIT_FAILURE;232233if (!aes_ccm_decrypt())234return EXIT_FAILURE;235236return EXIT_SUCCESS;237}238239240