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