Path: blob/main/crypto/openssl/demos/mac/hmac-sha512.c
34878 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 HMAC 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/hmac.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[] = {280x25, 0xfd, 0x12, 0x99, 0xdf, 0xad, 0x1a, 0x03,290x0a, 0x81, 0x3c, 0x2d, 0xcc, 0x05, 0xd1, 0x5c,300x17, 0x7a, 0x36, 0x73, 0x17, 0xef, 0x41, 0x75,310x71, 0x18, 0xe0, 0x1a, 0xda, 0x99, 0xc3, 0x61,320x38, 0xb5, 0xb1, 0xe0, 0x82, 0x2c, 0x70, 0xa4,330xc0, 0x8e, 0x5e, 0xf9, 0x93, 0x9f, 0xcf, 0xf7,340x32, 0x4d, 0x0c, 0xbd, 0x31, 0x12, 0x0f, 0x9a,350x15, 0xee, 0x82, 0xdb, 0x8d, 0x29, 0x54, 0x14,36};3738static const unsigned char data[] =39"To be, or not to be, that is the question,\n"40"Whether tis nobler in the minde to suffer\n"41"The ſlings and arrowes of outragious fortune,\n"42"Or to take Armes again in a sea of troubles,\n"43"And by opposing, end them, to die to sleep;\n"44"No more, and by a sleep, to say we end\n"45"The heart-ache, and the thousand natural shocks\n"46"That flesh is heir to? tis a consumation\n"47"Devoutly to be wished. To die to sleep,\n"48"To sleepe, perchance to dreame, Aye, there's the rub,\n"49"For in that sleep of death what dreams may come\n"50"When we haue shuffled off this mortal coil\n"51"Must give us pause. There's the respect\n"52"That makes calamity of so long life:\n"53"For who would bear the Ships and Scorns of time,\n"54"The oppressor's wrong, the proud man's Contumely,\n"55"The pangs of dispised love, the Law's delay,\n"56;5758/* The known value of the HMAC/SHA3-512 MAC of the above soliloqy */59static const unsigned char expected_output[] = {600x3b, 0x77, 0x5f, 0xf1, 0x4f, 0x9e, 0xb9, 0x23,610x8f, 0xdc, 0xa0, 0x68, 0x15, 0x7b, 0x8a, 0xf1,620x96, 0x23, 0xaa, 0x3c, 0x1f, 0xe9, 0xdc, 0x89,630x11, 0x7d, 0x58, 0x07, 0xe7, 0x96, 0x17, 0xe3,640x44, 0x8b, 0x03, 0x37, 0x91, 0xc0, 0x6e, 0x06,650x7c, 0x54, 0xe4, 0xa4, 0xcc, 0xd5, 0x16, 0xbb,660x5e, 0x4d, 0x64, 0x7d, 0x88, 0x23, 0xc9, 0xb7,670x25, 0xda, 0xbe, 0x4b, 0xe4, 0xd5, 0x34, 0x30,68};6970/*71* A property query used for selecting the MAC implementation.72*/73static const char *propq = NULL;7475int main(void)76{77int ret = EXIT_FAILURE;78OSSL_LIB_CTX *library_context = NULL;79EVP_MAC *mac = NULL;80EVP_MAC_CTX *mctx = NULL;81EVP_MD_CTX *digest_context = NULL;82unsigned char *out = NULL;83size_t out_len = 0;84OSSL_PARAM params[4], *p = params;85char digest_name[] = "SHA3-512";8687library_context = OSSL_LIB_CTX_new();88if (library_context == NULL) {89fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");90goto end;91}9293/* Fetch the HMAC implementation */94mac = EVP_MAC_fetch(library_context, "HMAC", propq);95if (mac == NULL) {96fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");97goto end;98}99100/* Create a context for the HMAC operation */101mctx = EVP_MAC_CTX_new(mac);102if (mctx == NULL) {103fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");104goto end;105}106107/* The underlying digest to be used */108*p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, digest_name,109sizeof(digest_name));110*p = OSSL_PARAM_construct_end();111112/* Initialise the HMAC operation */113if (!EVP_MAC_init(mctx, key, sizeof(key), params)) {114fprintf(stderr, "EVP_MAC_init() failed\n");115goto end;116}117118/* Make one or more calls to process the data to be authenticated */119if (!EVP_MAC_update(mctx, data, sizeof(data))) {120fprintf(stderr, "EVP_MAC_update() failed\n");121goto end;122}123124/* Make a call to the final with a NULL buffer to get the length of the MAC */125if (!EVP_MAC_final(mctx, NULL, &out_len, 0)) {126fprintf(stderr, "EVP_MAC_final() failed\n");127goto end;128}129out = OPENSSL_malloc(out_len);130if (out == NULL) {131fprintf(stderr, "malloc failed\n");132goto end;133}134/* Make one call to the final to get the MAC */135if (!EVP_MAC_final(mctx, out, &out_len, out_len)) {136fprintf(stderr, "EVP_MAC_final() failed\n");137goto end;138}139140printf("Generated MAC:\n");141BIO_dump_indent_fp(stdout, out, out_len, 2);142putchar('\n');143144if (out_len != sizeof(expected_output)) {145fprintf(stderr, "Generated MAC has an unexpected length\n");146goto end;147}148149if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {150fprintf(stderr, "Generated MAC does not match expected value\n");151goto end;152}153154ret = EXIT_SUCCESS;155end:156if (ret != EXIT_SUCCESS)157ERR_print_errors_fp(stderr);158/* OpenSSL free functions will ignore NULL arguments */159OPENSSL_free(out);160EVP_MD_CTX_free(digest_context);161EVP_MAC_CTX_free(mctx);162EVP_MAC_free(mac);163OSSL_LIB_CTX_free(library_context);164return ret;165}166167168