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