]>
git.ipfire.org Git - thirdparty/openssl.git/blob - demos/digest/EVP_MD_xof.c
2 * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/err.h>
13 #include <openssl/evp.h>
14 #include <openssl/core_names.h>
17 * Example of using an extendable-output hash function (XOF). A XOF is a hash
18 * function with configurable output length and which can generate an
19 * arbitrarily large output.
21 * This example uses SHAKE256, an extendable output variant of SHA3 (Keccak).
23 * To generate different output lengths, you can pass a single integer argument
24 * on the command line, which is the output size in bytes. By default, a 20-byte
25 * output is generated and (for this length only) a known answer test is
29 /* Our input to the XOF hash function. */
30 const char message
[] = "This is a test message.";
32 /* Expected output when an output length of 20 bytes is used. */
33 static const char known_answer
[] = {
34 0x52, 0x97, 0x93, 0x78, 0x27, 0x58, 0x7d, 0x62,
35 0x8b, 0x00, 0x25, 0xb5, 0xec, 0x39, 0x5e, 0x2d,
36 0x7f, 0x3e, 0xd4, 0x19
40 * A property query used for selecting the SHAKE256 implementation.
42 static const char *propq
= NULL
;
44 int main(int argc
, char **argv
)
46 int ret
= EXIT_FAILURE
;
47 OSSL_LIB_CTX
*libctx
= NULL
;
49 EVP_MD_CTX
*ctx
= NULL
;
50 unsigned int digest_len
= 20;
52 unsigned char *digest
= NULL
;
54 /* Allow digest length to be changed for demonstration purposes. */
56 digest_len_i
= atoi(argv
[1]);
57 if (digest_len_i
<= 0) {
58 fprintf(stderr
, "Specify a non-negative digest length\n");
62 digest_len
= (unsigned int)digest_len_i
;
66 * Retrieve desired algorithm. This must be a hash algorithm which supports
69 md
= EVP_MD_fetch(libctx
, "SHAKE256", propq
);
71 fprintf(stderr
, "Failed to retrieve SHAKE256 algorithm\n");
76 ctx
= EVP_MD_CTX_new();
78 fprintf(stderr
, "Failed to create digest context\n");
82 /* Initialize digest context. */
83 if (EVP_DigestInit(ctx
, md
) == 0) {
84 fprintf(stderr
, "Failed to initialize digest\n");
89 * Feed our message into the digest function.
90 * This may be called multiple times.
92 if (EVP_DigestUpdate(ctx
, message
, sizeof(message
)) == 0) {
93 fprintf(stderr
, "Failed to hash input message\n");
97 /* Allocate enough memory for our digest length. */
98 digest
= OPENSSL_malloc(digest_len
);
100 fprintf(stderr
, "Failed to allocate memory for digest\n");
104 /* Get computed digest. The digest will be of whatever length we specify. */
105 if (EVP_DigestFinalXOF(ctx
, digest
, digest_len
) == 0) {
106 fprintf(stderr
, "Failed to finalize hash\n");
110 printf("Output digest:\n");
111 BIO_dump_indent_fp(stdout
, digest
, digest_len
, 2);
113 /* If digest length is 20 bytes, check it matches our known answer. */
114 if (digest_len
== 20) {
116 * Always use a constant-time function such as CRYPTO_memcmp
117 * when comparing cryptographic values. Do not use memcmp(3).
119 if (CRYPTO_memcmp(digest
, known_answer
, sizeof(known_answer
)) != 0) {
120 fprintf(stderr
, "Output does not match expected result\n");
127 OPENSSL_free(digest
);
128 EVP_MD_CTX_free(ctx
);
130 OSSL_LIB_CTX_free(libctx
);