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 #include <stdio.h>
11*e0c4386eSCy Schubert #include <string.h>
12*e0c4386eSCy Schubert #include <openssl/err.h>
13*e0c4386eSCy Schubert #include <openssl/evp.h>
14*e0c4386eSCy Schubert #include <openssl/core_names.h>
15*e0c4386eSCy Schubert
16*e0c4386eSCy Schubert /*
17*e0c4386eSCy Schubert * Example of using an extendable-output hash function (XOF). A XOF is a hash
18*e0c4386eSCy Schubert * function with configurable output length and which can generate an
19*e0c4386eSCy Schubert * arbitrarily large output.
20*e0c4386eSCy Schubert *
21*e0c4386eSCy Schubert * This example uses SHAKE256, an extendable output variant of SHA3 (Keccak).
22*e0c4386eSCy Schubert *
23*e0c4386eSCy Schubert * To generate different output lengths, you can pass a single integer argument
24*e0c4386eSCy Schubert * on the command line, which is the output size in bytes. By default, a 20-byte
25*e0c4386eSCy Schubert * output is generated and (for this length only) a known answer test is
26*e0c4386eSCy Schubert * performed.
27*e0c4386eSCy Schubert */
28*e0c4386eSCy Schubert
29*e0c4386eSCy Schubert /* Our input to the XOF hash function. */
30*e0c4386eSCy Schubert const char message[] = "This is a test message.";
31*e0c4386eSCy Schubert
32*e0c4386eSCy Schubert /* Expected output when an output length of 20 bytes is used. */
33*e0c4386eSCy Schubert static const char known_answer[] = {
34*e0c4386eSCy Schubert 0x52, 0x97, 0x93, 0x78, 0x27, 0x58, 0x7d, 0x62,
35*e0c4386eSCy Schubert 0x8b, 0x00, 0x25, 0xb5, 0xec, 0x39, 0x5e, 0x2d,
36*e0c4386eSCy Schubert 0x7f, 0x3e, 0xd4, 0x19
37*e0c4386eSCy Schubert };
38*e0c4386eSCy Schubert
39*e0c4386eSCy Schubert /*
40*e0c4386eSCy Schubert * A property query used for selecting the SHAKE256 implementation.
41*e0c4386eSCy Schubert */
42*e0c4386eSCy Schubert static const char *propq = NULL;
43*e0c4386eSCy Schubert
main(int argc,char ** argv)44*e0c4386eSCy Schubert int main(int argc, char **argv)
45*e0c4386eSCy Schubert {
46*e0c4386eSCy Schubert int rv = 1;
47*e0c4386eSCy Schubert OSSL_LIB_CTX *libctx = NULL;
48*e0c4386eSCy Schubert EVP_MD *md = NULL;
49*e0c4386eSCy Schubert EVP_MD_CTX *ctx = NULL;
50*e0c4386eSCy Schubert unsigned int digest_len = 20;
51*e0c4386eSCy Schubert int digest_len_i;
52*e0c4386eSCy Schubert unsigned char *digest = NULL;
53*e0c4386eSCy Schubert
54*e0c4386eSCy Schubert /* Allow digest length to be changed for demonstration purposes. */
55*e0c4386eSCy Schubert if (argc > 1) {
56*e0c4386eSCy Schubert digest_len_i = atoi(argv[1]);
57*e0c4386eSCy Schubert if (digest_len_i <= 0) {
58*e0c4386eSCy Schubert fprintf(stderr, "Specify a non-negative digest length\n");
59*e0c4386eSCy Schubert goto end;
60*e0c4386eSCy Schubert }
61*e0c4386eSCy Schubert
62*e0c4386eSCy Schubert digest_len = (unsigned int)digest_len_i;
63*e0c4386eSCy Schubert }
64*e0c4386eSCy Schubert
65*e0c4386eSCy Schubert /*
66*e0c4386eSCy Schubert * Retrieve desired algorithm. This must be a hash algorithm which supports
67*e0c4386eSCy Schubert * XOF.
68*e0c4386eSCy Schubert */
69*e0c4386eSCy Schubert md = EVP_MD_fetch(libctx, "SHAKE256", propq);
70*e0c4386eSCy Schubert if (md == NULL) {
71*e0c4386eSCy Schubert fprintf(stderr, "Failed to retrieve SHAKE256 algorithm\n");
72*e0c4386eSCy Schubert goto end;
73*e0c4386eSCy Schubert }
74*e0c4386eSCy Schubert
75*e0c4386eSCy Schubert /* Create context. */
76*e0c4386eSCy Schubert ctx = EVP_MD_CTX_new();
77*e0c4386eSCy Schubert if (ctx == NULL) {
78*e0c4386eSCy Schubert fprintf(stderr, "Failed to create digest context\n");
79*e0c4386eSCy Schubert goto end;
80*e0c4386eSCy Schubert }
81*e0c4386eSCy Schubert
82*e0c4386eSCy Schubert /* Initialize digest context. */
83*e0c4386eSCy Schubert if (EVP_DigestInit(ctx, md) == 0) {
84*e0c4386eSCy Schubert fprintf(stderr, "Failed to initialize digest\n");
85*e0c4386eSCy Schubert goto end;
86*e0c4386eSCy Schubert }
87*e0c4386eSCy Schubert
88*e0c4386eSCy Schubert /*
89*e0c4386eSCy Schubert * Feed our message into the digest function.
90*e0c4386eSCy Schubert * This may be called multiple times.
91*e0c4386eSCy Schubert */
92*e0c4386eSCy Schubert if (EVP_DigestUpdate(ctx, message, sizeof(message)) == 0) {
93*e0c4386eSCy Schubert fprintf(stderr, "Failed to hash input message\n");
94*e0c4386eSCy Schubert goto end;
95*e0c4386eSCy Schubert }
96*e0c4386eSCy Schubert
97*e0c4386eSCy Schubert /* Allocate enough memory for our digest length. */
98*e0c4386eSCy Schubert digest = OPENSSL_malloc(digest_len);
99*e0c4386eSCy Schubert if (digest == NULL) {
100*e0c4386eSCy Schubert fprintf(stderr, "Failed to allocate memory for digest\n");
101*e0c4386eSCy Schubert goto end;
102*e0c4386eSCy Schubert }
103*e0c4386eSCy Schubert
104*e0c4386eSCy Schubert /* Get computed digest. The digest will be of whatever length we specify. */
105*e0c4386eSCy Schubert if (EVP_DigestFinalXOF(ctx, digest, digest_len) == 0) {
106*e0c4386eSCy Schubert fprintf(stderr, "Failed to finalize hash\n");
107*e0c4386eSCy Schubert goto end;
108*e0c4386eSCy Schubert }
109*e0c4386eSCy Schubert
110*e0c4386eSCy Schubert printf("Output digest:\n");
111*e0c4386eSCy Schubert BIO_dump_indent_fp(stdout, digest, digest_len, 2);
112*e0c4386eSCy Schubert
113*e0c4386eSCy Schubert /* If digest length is 20 bytes, check it matches our known answer. */
114*e0c4386eSCy Schubert if (digest_len == 20) {
115*e0c4386eSCy Schubert /*
116*e0c4386eSCy Schubert * Always use a constant-time function such as CRYPTO_memcmp
117*e0c4386eSCy Schubert * when comparing cryptographic values. Do not use memcmp(3).
118*e0c4386eSCy Schubert */
119*e0c4386eSCy Schubert if (CRYPTO_memcmp(digest, known_answer, sizeof(known_answer)) != 0) {
120*e0c4386eSCy Schubert fprintf(stderr, "Output does not match expected result\n");
121*e0c4386eSCy Schubert goto end;
122*e0c4386eSCy Schubert }
123*e0c4386eSCy Schubert }
124*e0c4386eSCy Schubert
125*e0c4386eSCy Schubert rv = 0;
126*e0c4386eSCy Schubert end:
127*e0c4386eSCy Schubert OPENSSL_free(digest);
128*e0c4386eSCy Schubert EVP_MD_CTX_free(ctx);
129*e0c4386eSCy Schubert EVP_MD_free(md);
130*e0c4386eSCy Schubert OSSL_LIB_CTX_free(libctx);
131*e0c4386eSCy Schubert return rv;
132*e0c4386eSCy Schubert }
133