1*e0c4386eSCy Schubert /*-
2*e0c4386eSCy Schubert * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert /*
11*e0c4386eSCy Schubert * Example of using EVP_MAC_ methods to calculate
12*e0c4386eSCy Schubert * a HMAC of static buffers
13*e0c4386eSCy Schubert */
14*e0c4386eSCy Schubert
15*e0c4386eSCy Schubert #include <string.h>
16*e0c4386eSCy Schubert #include <stdio.h>
17*e0c4386eSCy Schubert #include <openssl/crypto.h>
18*e0c4386eSCy Schubert #include <openssl/core_names.h>
19*e0c4386eSCy Schubert #include <openssl/err.h>
20*e0c4386eSCy Schubert #include <openssl/evp.h>
21*e0c4386eSCy Schubert #include <openssl/hmac.h>
22*e0c4386eSCy Schubert #include <openssl/params.h>
23*e0c4386eSCy Schubert
24*e0c4386eSCy Schubert /*
25*e0c4386eSCy Schubert * Hard coding the key into an application is very bad.
26*e0c4386eSCy Schubert * It is done here solely for educational purposes.
27*e0c4386eSCy Schubert */
28*e0c4386eSCy Schubert static unsigned char key[] = {
29*e0c4386eSCy Schubert 0x25, 0xfd, 0x12, 0x99, 0xdf, 0xad, 0x1a, 0x03,
30*e0c4386eSCy Schubert 0x0a, 0x81, 0x3c, 0x2d, 0xcc, 0x05, 0xd1, 0x5c,
31*e0c4386eSCy Schubert 0x17, 0x7a, 0x36, 0x73, 0x17, 0xef, 0x41, 0x75,
32*e0c4386eSCy Schubert 0x71, 0x18, 0xe0, 0x1a, 0xda, 0x99, 0xc3, 0x61,
33*e0c4386eSCy Schubert 0x38, 0xb5, 0xb1, 0xe0, 0x82, 0x2c, 0x70, 0xa4,
34*e0c4386eSCy Schubert 0xc0, 0x8e, 0x5e, 0xf9, 0x93, 0x9f, 0xcf, 0xf7,
35*e0c4386eSCy Schubert 0x32, 0x4d, 0x0c, 0xbd, 0x31, 0x12, 0x0f, 0x9a,
36*e0c4386eSCy Schubert 0x15, 0xee, 0x82, 0xdb, 0x8d, 0x29, 0x54, 0x14,
37*e0c4386eSCy Schubert };
38*e0c4386eSCy Schubert
39*e0c4386eSCy Schubert static const unsigned char data[] =
40*e0c4386eSCy Schubert "To be, or not to be, that is the question,\n"
41*e0c4386eSCy Schubert "Whether tis nobler in the minde to suffer\n"
42*e0c4386eSCy Schubert "The ſlings and arrowes of outragious fortune,\n"
43*e0c4386eSCy Schubert "Or to take Armes again in a sea of troubles,\n"
44*e0c4386eSCy Schubert "And by opposing, end them, to die to sleep;\n"
45*e0c4386eSCy Schubert "No more, and by a sleep, to say we end\n"
46*e0c4386eSCy Schubert "The heart-ache, and the thousand natural shocks\n"
47*e0c4386eSCy Schubert "That flesh is heir to? tis a consumation\n"
48*e0c4386eSCy Schubert "Devoutly to be wished. To die to sleep,\n"
49*e0c4386eSCy Schubert "To sleepe, perchance to dreame, Aye, there's the rub,\n"
50*e0c4386eSCy Schubert "For in that sleep of death what dreams may come\n"
51*e0c4386eSCy Schubert "When we haue shuffled off this mortal coil\n"
52*e0c4386eSCy Schubert "Must give us pause. There's the respect\n"
53*e0c4386eSCy Schubert "That makes calamity of so long life:\n"
54*e0c4386eSCy Schubert "For who would bear the Ships and Scorns of time,\n"
55*e0c4386eSCy Schubert "The oppressor's wrong, the proud man's Contumely,\n"
56*e0c4386eSCy Schubert "The pangs of dispised love, the Law's delay,\n"
57*e0c4386eSCy Schubert ;
58*e0c4386eSCy Schubert
59*e0c4386eSCy Schubert /* The known value of the HMAC/SHA3-512 MAC of the above soliloqy */
60*e0c4386eSCy Schubert static const unsigned char expected_output[] = {
61*e0c4386eSCy Schubert 0x3b, 0x77, 0x5f, 0xf1, 0x4f, 0x9e, 0xb9, 0x23,
62*e0c4386eSCy Schubert 0x8f, 0xdc, 0xa0, 0x68, 0x15, 0x7b, 0x8a, 0xf1,
63*e0c4386eSCy Schubert 0x96, 0x23, 0xaa, 0x3c, 0x1f, 0xe9, 0xdc, 0x89,
64*e0c4386eSCy Schubert 0x11, 0x7d, 0x58, 0x07, 0xe7, 0x96, 0x17, 0xe3,
65*e0c4386eSCy Schubert 0x44, 0x8b, 0x03, 0x37, 0x91, 0xc0, 0x6e, 0x06,
66*e0c4386eSCy Schubert 0x7c, 0x54, 0xe4, 0xa4, 0xcc, 0xd5, 0x16, 0xbb,
67*e0c4386eSCy Schubert 0x5e, 0x4d, 0x64, 0x7d, 0x88, 0x23, 0xc9, 0xb7,
68*e0c4386eSCy Schubert 0x25, 0xda, 0xbe, 0x4b, 0xe4, 0xd5, 0x34, 0x30,
69*e0c4386eSCy Schubert };
70*e0c4386eSCy Schubert
71*e0c4386eSCy Schubert /*
72*e0c4386eSCy Schubert * A property query used for selecting the MAC implementation.
73*e0c4386eSCy Schubert */
74*e0c4386eSCy Schubert static const char *propq = NULL;
75*e0c4386eSCy Schubert
main(void)76*e0c4386eSCy Schubert int main(void)
77*e0c4386eSCy Schubert {
78*e0c4386eSCy Schubert int rv = EXIT_FAILURE;
79*e0c4386eSCy Schubert OSSL_LIB_CTX *library_context = NULL;
80*e0c4386eSCy Schubert EVP_MAC *mac = NULL;
81*e0c4386eSCy Schubert EVP_MAC_CTX *mctx = NULL;
82*e0c4386eSCy Schubert EVP_MD_CTX *digest_context = NULL;
83*e0c4386eSCy Schubert unsigned char *out = NULL;
84*e0c4386eSCy Schubert size_t out_len = 0;
85*e0c4386eSCy Schubert OSSL_PARAM params[4], *p = params;
86*e0c4386eSCy Schubert char digest_name[] = "SHA3-512";
87*e0c4386eSCy Schubert
88*e0c4386eSCy Schubert library_context = OSSL_LIB_CTX_new();
89*e0c4386eSCy Schubert if (library_context == NULL) {
90*e0c4386eSCy Schubert fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
91*e0c4386eSCy Schubert goto end;
92*e0c4386eSCy Schubert }
93*e0c4386eSCy Schubert
94*e0c4386eSCy Schubert /* Fetch the HMAC implementation */
95*e0c4386eSCy Schubert mac = EVP_MAC_fetch(library_context, "HMAC", propq);
96*e0c4386eSCy Schubert if (mac == NULL) {
97*e0c4386eSCy Schubert fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");
98*e0c4386eSCy Schubert goto end;
99*e0c4386eSCy Schubert }
100*e0c4386eSCy Schubert
101*e0c4386eSCy Schubert /* Create a context for the HMAC operation */
102*e0c4386eSCy Schubert mctx = EVP_MAC_CTX_new(mac);
103*e0c4386eSCy Schubert if (mctx == NULL) {
104*e0c4386eSCy Schubert fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");
105*e0c4386eSCy Schubert goto end;
106*e0c4386eSCy Schubert }
107*e0c4386eSCy Schubert
108*e0c4386eSCy Schubert /* The underlying digest to be used */
109*e0c4386eSCy Schubert *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, digest_name,
110*e0c4386eSCy Schubert sizeof(digest_name));
111*e0c4386eSCy Schubert *p = OSSL_PARAM_construct_end();
112*e0c4386eSCy Schubert
113*e0c4386eSCy Schubert /* Initialise the HMAC operation */
114*e0c4386eSCy Schubert if (!EVP_MAC_init(mctx, key, sizeof(key), params)) {
115*e0c4386eSCy Schubert fprintf(stderr, "EVP_MAC_init() failed\n");
116*e0c4386eSCy Schubert goto end;
117*e0c4386eSCy Schubert }
118*e0c4386eSCy Schubert
119*e0c4386eSCy Schubert /* Make one or more calls to process the data to be authenticated */
120*e0c4386eSCy Schubert if (!EVP_MAC_update(mctx, data, sizeof(data))) {
121*e0c4386eSCy Schubert fprintf(stderr, "EVP_MAC_update() failed\n");
122*e0c4386eSCy Schubert goto end;
123*e0c4386eSCy Schubert }
124*e0c4386eSCy Schubert
125*e0c4386eSCy Schubert /* Make a call to the final with a NULL buffer to get the length of the MAC */
126*e0c4386eSCy Schubert if (!EVP_MAC_final(mctx, NULL, &out_len, 0)) {
127*e0c4386eSCy Schubert fprintf(stderr, "EVP_MAC_final() failed\n");
128*e0c4386eSCy Schubert goto end;
129*e0c4386eSCy Schubert }
130*e0c4386eSCy Schubert out = OPENSSL_malloc(out_len);
131*e0c4386eSCy Schubert if (out == NULL) {
132*e0c4386eSCy Schubert fprintf(stderr, "malloc failed\n");
133*e0c4386eSCy Schubert goto end;
134*e0c4386eSCy Schubert }
135*e0c4386eSCy Schubert /* Make one call to the final to get the MAC */
136*e0c4386eSCy Schubert if (!EVP_MAC_final(mctx, out, &out_len, out_len)) {
137*e0c4386eSCy Schubert fprintf(stderr, "EVP_MAC_final() failed\n");
138*e0c4386eSCy Schubert goto end;
139*e0c4386eSCy Schubert }
140*e0c4386eSCy Schubert
141*e0c4386eSCy Schubert printf("Generated MAC:\n");
142*e0c4386eSCy Schubert BIO_dump_indent_fp(stdout, out, out_len, 2);
143*e0c4386eSCy Schubert putchar('\n');
144*e0c4386eSCy Schubert
145*e0c4386eSCy Schubert if (out_len != sizeof(expected_output)) {
146*e0c4386eSCy Schubert fprintf(stderr, "Generated MAC has an unexpected length\n");
147*e0c4386eSCy Schubert goto end;
148*e0c4386eSCy Schubert }
149*e0c4386eSCy Schubert
150*e0c4386eSCy Schubert if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
151*e0c4386eSCy Schubert fprintf(stderr, "Generated MAC does not match expected value\n");
152*e0c4386eSCy Schubert goto end;
153*e0c4386eSCy Schubert }
154*e0c4386eSCy Schubert
155*e0c4386eSCy Schubert rv = EXIT_SUCCESS;
156*e0c4386eSCy Schubert end:
157*e0c4386eSCy Schubert if (rv != EXIT_SUCCESS)
158*e0c4386eSCy Schubert ERR_print_errors_fp(stderr);
159*e0c4386eSCy Schubert /* OpenSSL free functions will ignore NULL arguments */
160*e0c4386eSCy Schubert OPENSSL_free(out);
161*e0c4386eSCy Schubert EVP_MD_CTX_free(digest_context);
162*e0c4386eSCy Schubert EVP_MAC_CTX_free(mctx);
163*e0c4386eSCy Schubert EVP_MAC_free(mac);
164*e0c4386eSCy Schubert OSSL_LIB_CTX_free(library_context);
165*e0c4386eSCy Schubert return rv;
166*e0c4386eSCy Schubert }
167