Path: blob/main/crypto/openssl/demos/digest/BIO_f_md.c
34889 views
/*-1* Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/*-10* Example of using EVP_MD_fetch and EVP_Digest* methods to calculate11* a digest of static buffers12* You can find SHA3 test vectors from NIST here:13* https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/sha3/sha-3bytetestvectors.zip14* For example, contains these lines:15Len = 8016Msg = 1ca984dcc913344370cf17MD = 6915ea0eeffb99b9b246a0e34daf3947852684c3d618260119a22835659e4f23d4eb66a15d0affb8e93771578f5e8f25b7a5f2a55f511fb8b96325ba2cd1481618* use xxd convert the hex message string to binary input for BIO_f_md:19* echo "1ca984dcc913344370cf" | xxd -r -p | ./BIO_f_md20* and then verify the output matches MD above.21*/2223#include <string.h>24#include <stdio.h>25#include <openssl/err.h>26#include <openssl/bio.h>27#include <openssl/evp.h>2829/*-30* This demonstration will show how to digest data using31* a BIO configured with a message digest32* A message digest name may be passed as an argument.33* The default digest is SHA3-51234*/3536int main(int argc, char *argv[])37{38int ret = EXIT_FAILURE;39OSSL_LIB_CTX *library_context = NULL;40BIO *input = NULL;41BIO *bio_digest = NULL, *reading = NULL;42EVP_MD *md = NULL;43unsigned char buffer[512];44int digest_size;45char *digest_value = NULL;46int j;4748input = BIO_new_fd(fileno(stdin), 1);49if (input == NULL) {50fprintf(stderr, "BIO_new_fd() for stdin returned NULL\n");51goto cleanup;52}53library_context = OSSL_LIB_CTX_new();54if (library_context == NULL) {55fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");56goto cleanup;57}5859/*60* Fetch a message digest by name61* The algorithm name is case insensitive.62* See providers(7) for details about algorithm fetching63*/64md = EVP_MD_fetch(library_context, "SHA3-512", NULL);65if (md == NULL) {66fprintf(stderr, "EVP_MD_fetch did not find SHA3-512.\n");67goto cleanup;68}69digest_size = EVP_MD_get_size(md);70if (digest_size <= 0) {71fprintf(stderr, "EVP_MD_get_size returned invalid size.\n");72goto cleanup;73}7475digest_value = OPENSSL_malloc(digest_size);76if (digest_value == NULL) {77fprintf(stderr, "Can't allocate %lu bytes for the digest value.\n", (unsigned long)digest_size);78goto cleanup;79}80/* Make a bio that uses the digest */81bio_digest = BIO_new(BIO_f_md());82if (bio_digest == NULL) {83fprintf(stderr, "BIO_new(BIO_f_md()) returned NULL\n");84goto cleanup;85}86/* set our bio_digest BIO to digest data */87if (BIO_set_md(bio_digest, md) != 1) {88fprintf(stderr, "BIO_set_md failed.\n");89goto cleanup;90}91/*-92* We will use BIO chaining so that as we read, the digest gets updated93* See the man page for BIO_push94*/95reading = BIO_push(bio_digest, input);9697while (BIO_read(reading, buffer, sizeof(buffer)) > 0)98;99100/*-101* BIO_gets must be used to calculate the final102* digest value and then copy it to digest_value.103*/104if (BIO_gets(bio_digest, digest_value, digest_size) != digest_size) {105fprintf(stderr, "BIO_gets(bio_digest) failed\n");106goto cleanup;107}108for (j = 0; j < digest_size; j++) {109fprintf(stdout, "%02x", (unsigned char)digest_value[j]);110}111fprintf(stdout, "\n");112ret = EXIT_SUCCESS;113114cleanup:115if (ret != EXIT_SUCCESS)116ERR_print_errors_fp(stderr);117118OPENSSL_free(digest_value);119BIO_free(input);120BIO_free(bio_digest);121EVP_MD_free(md);122OSSL_LIB_CTX_free(library_context);123124return ret;125}126127128