Path: blob/main/crypto/openssl/demos/mac/cmac-aes256.c
34869 views
/*-1* Copyright 2022-2023 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* Example of using EVP_MAC_ methods to calculate11* a CMAC of static buffers12*/1314#include <string.h>15#include <stdio.h>16#include <openssl/crypto.h>17#include <openssl/core_names.h>18#include <openssl/err.h>19#include <openssl/evp.h>20#include <openssl/cmac.h>21#include <openssl/params.h>2223/*24* Hard coding the key into an application is very bad.25* It is done here solely for educational purposes.26*/27static unsigned char key[] = {280x6c, 0xde, 0x14, 0xf5, 0xd5, 0x2a, 0x4a, 0xdf,290x12, 0x39, 0x1e, 0xbf, 0x36, 0xf9, 0x6a, 0x46,300x48, 0xd0, 0xb6, 0x51, 0x89, 0xfc, 0x24, 0x85,310xa8, 0x8d, 0xdf, 0x7e, 0x80, 0x14, 0xc8, 0xce,32};3334static const unsigned char data[] =35"To be, or not to be, that is the question,\n"36"Whether tis nobler in the minde to suffer\n"37"The ſlings and arrowes of outragious fortune,\n"38"Or to take Armes again in a sea of troubles,\n"39"And by opposing, end them, to die to sleep;\n"40"No more, and by a sleep, to say we end\n"41"The heart-ache, and the thousand natural shocks\n"42"That flesh is heir to? tis a consumation\n"43"Devoutly to be wished. To die to sleep,\n"44"To sleepe, perchance to dreame, Aye, there's the rub,\n"45"For in that sleep of death what dreams may come\n"46"When we haue shuffled off this mortal coil\n"47"Must give us pause. There's the respect\n"48"That makes calamity of so long life:\n"49"For who would bear the Ships and Scorns of time,\n"50"The oppressor's wrong, the proud man's Contumely,\n"51"The pangs of dispised love, the Law's delay,\n"52;5354/* The known value of the CMAC/AES256 MAC of the above soliloqy */55static const unsigned char expected_output[] = {560x67, 0x92, 0x32, 0x23, 0x50, 0x3d, 0xc5, 0xba,570x78, 0xd4, 0x6d, 0x63, 0xf2, 0x2b, 0xe9, 0x56,58};5960/*61* A property query used for selecting the MAC implementation.62*/63static const char *propq = NULL;6465int main(void)66{67int ret = EXIT_FAILURE;68OSSL_LIB_CTX *library_context = NULL;69EVP_MAC *mac = NULL;70EVP_MAC_CTX *mctx = NULL;71unsigned char *out = NULL;72size_t out_len = 0;73OSSL_PARAM params[4], *p = params;74char cipher_name[] = "AES-256-CBC";7576library_context = OSSL_LIB_CTX_new();77if (library_context == NULL) {78fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");79goto end;80}8182/* Fetch the CMAC implementation */83mac = EVP_MAC_fetch(library_context, "CMAC", propq);84if (mac == NULL) {85fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");86goto end;87}8889/* Create a context for the CMAC operation */90mctx = EVP_MAC_CTX_new(mac);91if (mctx == NULL) {92fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");93goto end;94}9596/* The underlying cipher to be used */97*p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, cipher_name,98sizeof(cipher_name));99*p = OSSL_PARAM_construct_end();100101/* Initialise the CMAC operation */102if (!EVP_MAC_init(mctx, key, sizeof(key), params)) {103fprintf(stderr, "EVP_MAC_init() failed\n");104goto end;105}106107/* Make one or more calls to process the data to be authenticated */108if (!EVP_MAC_update(mctx, data, sizeof(data))) {109fprintf(stderr, "EVP_MAC_update() failed\n");110goto end;111}112113/* Make a call to the final with a NULL buffer to get the length of the MAC */114if (!EVP_MAC_final(mctx, NULL, &out_len, 0)) {115fprintf(stderr, "EVP_MAC_final() failed\n");116goto end;117}118out = OPENSSL_malloc(out_len);119if (out == NULL) {120fprintf(stderr, "malloc failed\n");121goto end;122}123/* Make one call to the final to get the MAC */124if (!EVP_MAC_final(mctx, out, &out_len, out_len)) {125fprintf(stderr, "EVP_MAC_final() failed\n");126goto end;127}128129printf("Generated MAC:\n");130BIO_dump_indent_fp(stdout, out, out_len, 2);131putchar('\n');132133if (out_len != sizeof(expected_output)) {134fprintf(stderr, "Generated MAC has an unexpected length\n");135goto end;136}137138if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {139fprintf(stderr, "Generated MAC does not match expected value\n");140goto end;141}142143ret = EXIT_SUCCESS;144end:145if (ret != EXIT_SUCCESS)146ERR_print_errors_fp(stderr);147/* OpenSSL free functions will ignore NULL arguments */148OPENSSL_free(out);149EVP_MAC_CTX_free(mctx);150EVP_MAC_free(mac);151OSSL_LIB_CTX_free(library_context);152return ret;153}154155156