Path: blob/main/crypto/openssl/demos/cipher/aesccm.c
106294 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;6869static int aes_ccm_encrypt(void)70{71int ret = 0;72EVP_CIPHER_CTX *ctx;73EVP_CIPHER *cipher = NULL;74int outlen, tmplen;75size_t ccm_nonce_len = sizeof(ccm_nonce);76size_t ccm_tag_len = sizeof(ccm_tag);77unsigned char outbuf[1024];78unsigned char outtag[16];79OSSL_PARAM params[3] = {80OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END81};8283printf("AES CCM Encrypt:\n");84printf("Plaintext:\n");85BIO_dump_fp(stdout, ccm_pt, sizeof(ccm_pt));8687/* Create a context for the encrypt operation */88if ((ctx = EVP_CIPHER_CTX_new()) == NULL)89goto err;9091/* Fetch the cipher implementation */92if ((cipher = EVP_CIPHER_fetch(libctx, "AES-192-CCM", propq)) == NULL)93goto err;9495/* Default nonce length for AES-CCM is 7 bytes (56 bits). */96params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,97&ccm_nonce_len);98/* Set tag length */99params[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,100NULL, ccm_tag_len);101102/*103* Initialise encrypt operation with the cipher & mode,104* nonce length and tag length parameters.105*/106if (!EVP_EncryptInit_ex2(ctx, cipher, NULL, NULL, params))107goto err;108109/* Initialise key and nonce */110if (!EVP_EncryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce))111goto err;112113/* Set plaintext length: only needed if AAD is used */114if (!EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_pt)))115goto err;116117/* Zero or one call to specify any AAD */118if (!EVP_EncryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata)))119goto err;120121/* Encrypt plaintext: can only be called once */122if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, ccm_pt, sizeof(ccm_pt)))123goto err;124125/* Output encrypted block */126printf("Ciphertext:\n");127BIO_dump_fp(stdout, outbuf, outlen);128129/* Finalise: note get no output for CCM */130if (!EVP_EncryptFinal_ex(ctx, NULL, &tmplen))131goto err;132133/* Get tag */134params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,135outtag, ccm_tag_len);136params[1] = OSSL_PARAM_construct_end();137138if (!EVP_CIPHER_CTX_get_params(ctx, params))139goto err;140141/* Output tag */142printf("Tag:\n");143BIO_dump_fp(stdout, outtag, ccm_tag_len);144145ret = 1;146err:147if (!ret)148ERR_print_errors_fp(stderr);149150EVP_CIPHER_free(cipher);151EVP_CIPHER_CTX_free(ctx);152153return ret;154}155156static int aes_ccm_decrypt(void)157{158int ret = 0;159EVP_CIPHER_CTX *ctx;160EVP_CIPHER *cipher = NULL;161int outlen, rv;162unsigned char outbuf[1024];163size_t ccm_nonce_len = sizeof(ccm_nonce);164OSSL_PARAM params[3] = {165OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END166};167168printf("AES CCM Decrypt:\n");169printf("Ciphertext:\n");170BIO_dump_fp(stdout, ccm_ct, sizeof(ccm_ct));171172if ((ctx = EVP_CIPHER_CTX_new()) == NULL)173goto err;174175/* Fetch the cipher implementation */176if ((cipher = EVP_CIPHER_fetch(libctx, "AES-192-CCM", propq)) == NULL)177goto err;178179/* Set nonce length if default 96 bits is not appropriate */180params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,181&ccm_nonce_len);182/* Set tag length */183params[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,184(unsigned char *)ccm_tag,185sizeof(ccm_tag));186/*187* Initialise decrypt operation with the cipher & mode,188* nonce length and expected tag parameters.189*/190if (!EVP_DecryptInit_ex2(ctx, cipher, NULL, NULL, params))191goto err;192193/* Specify key and IV */194if (!EVP_DecryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce))195goto err;196197/* Set ciphertext length: only needed if we have AAD */198if (!EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_ct)))199goto err;200201/* Zero or one call to specify any AAD */202if (!EVP_DecryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata)))203goto err;204205/* Decrypt plaintext, verify tag: can only be called once */206rv = EVP_DecryptUpdate(ctx, outbuf, &outlen, ccm_ct, sizeof(ccm_ct));207208/* Output decrypted block: if tag verify failed we get nothing */209if (rv > 0) {210printf("Tag verify successful!\nPlaintext:\n");211BIO_dump_fp(stdout, outbuf, outlen);212} else {213printf("Tag verify failed!\nPlaintext not available\n");214goto err;215}216ret = 1;217err:218if (!ret)219ERR_print_errors_fp(stderr);220221EVP_CIPHER_free(cipher);222EVP_CIPHER_CTX_free(ctx);223224return ret;225}226227int main(int argc, char **argv)228{229if (!aes_ccm_encrypt())230return EXIT_FAILURE;231232if (!aes_ccm_decrypt())233return EXIT_FAILURE;234235return EXIT_SUCCESS;236}237238239