1e0c4386eSCy Schubert /*- 2*44096ebdSEnji Cooper * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved. 3e0c4386eSCy Schubert * 4e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use 5e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy 6e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at 7e0c4386eSCy Schubert * https://www.openssl.org/source/license.html 8e0c4386eSCy Schubert */ 9e0c4386eSCy Schubert 10e0c4386eSCy Schubert /*- 11e0c4386eSCy Schubert * Example of using EVP_MD_fetch and EVP_Digest* methods to calculate 12e0c4386eSCy Schubert * a digest of static buffers 13e0c4386eSCy Schubert * You can find SHA3 test vectors from NIST here: 14e0c4386eSCy Schubert * https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/sha3/sha-3bytetestvectors.zip 15e0c4386eSCy Schubert * For example, contains these lines: 16e0c4386eSCy Schubert Len = 80 17e0c4386eSCy Schubert Msg = 1ca984dcc913344370cf 18e0c4386eSCy Schubert MD = 6915ea0eeffb99b9b246a0e34daf3947852684c3d618260119a22835659e4f23d4eb66a15d0affb8e93771578f5e8f25b7a5f2a55f511fb8b96325ba2cd14816 19e0c4386eSCy Schubert * use xxd convert the hex message string to binary input for EVP_MD_stdin: 20e0c4386eSCy Schubert * echo "1ca984dcc913344370cf" | xxd -r -p | ./EVP_MD_stdin 21e0c4386eSCy Schubert * and then verify the output matches MD above. 22e0c4386eSCy Schubert */ 23e0c4386eSCy Schubert 24e0c4386eSCy Schubert #include <string.h> 25e0c4386eSCy Schubert #include <stdio.h> 26e0c4386eSCy Schubert #include <openssl/err.h> 27e0c4386eSCy Schubert #include <openssl/evp.h> 28e0c4386eSCy Schubert 29e0c4386eSCy Schubert /*- 30e0c4386eSCy Schubert * This demonstration will show how to digest data using 31e0c4386eSCy Schubert * a BIO created to read from stdin 32e0c4386eSCy Schubert */ 33e0c4386eSCy Schubert 34e0c4386eSCy Schubert int demonstrate_digest(BIO *input) 35e0c4386eSCy Schubert { 36e0c4386eSCy Schubert OSSL_LIB_CTX *library_context = NULL; 37e0c4386eSCy Schubert int result = 0; 38e0c4386eSCy Schubert const char * option_properties = NULL; 39e0c4386eSCy Schubert EVP_MD *message_digest = NULL; 40e0c4386eSCy Schubert EVP_MD_CTX *digest_context = NULL; 41*44096ebdSEnji Cooper int digest_length; 42e0c4386eSCy Schubert unsigned char *digest_value = NULL; 43e0c4386eSCy Schubert unsigned char buffer[512]; 44e0c4386eSCy Schubert int ii; 45e0c4386eSCy Schubert 46e0c4386eSCy Schubert library_context = OSSL_LIB_CTX_new(); 47e0c4386eSCy Schubert if (library_context == NULL) { 48e0c4386eSCy Schubert fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n"); 49e0c4386eSCy Schubert goto cleanup; 50e0c4386eSCy Schubert } 51e0c4386eSCy Schubert 52e0c4386eSCy Schubert /* 53e0c4386eSCy Schubert * Fetch a message digest by name 54e0c4386eSCy Schubert * The algorithm name is case insensitive. 55e0c4386eSCy Schubert * See providers(7) for details about algorithm fetching 56e0c4386eSCy Schubert */ 57e0c4386eSCy Schubert message_digest = EVP_MD_fetch(library_context, 58e0c4386eSCy Schubert "SHA3-512", option_properties); 59e0c4386eSCy Schubert if (message_digest == NULL) { 60e0c4386eSCy Schubert fprintf(stderr, "EVP_MD_fetch could not find SHA3-512."); 61e0c4386eSCy Schubert ERR_print_errors_fp(stderr); 62e0c4386eSCy Schubert OSSL_LIB_CTX_free(library_context); 63e0c4386eSCy Schubert return 0; 64e0c4386eSCy Schubert } 65e0c4386eSCy Schubert /* Determine the length of the fetched digest type */ 66e0c4386eSCy Schubert digest_length = EVP_MD_get_size(message_digest); 67e0c4386eSCy Schubert if (digest_length <= 0) { 68e0c4386eSCy Schubert fprintf(stderr, "EVP_MD_get_size returned invalid size.\n"); 69e0c4386eSCy Schubert goto cleanup; 70e0c4386eSCy Schubert } 71e0c4386eSCy Schubert 72e0c4386eSCy Schubert digest_value = OPENSSL_malloc(digest_length); 73e0c4386eSCy Schubert if (digest_value == NULL) { 74e0c4386eSCy Schubert fprintf(stderr, "No memory.\n"); 75e0c4386eSCy Schubert goto cleanup; 76e0c4386eSCy Schubert } 77e0c4386eSCy Schubert /* 78e0c4386eSCy Schubert * Make a message digest context to hold temporary state 79e0c4386eSCy Schubert * during digest creation 80e0c4386eSCy Schubert */ 81e0c4386eSCy Schubert digest_context = EVP_MD_CTX_new(); 82e0c4386eSCy Schubert if (digest_context == NULL) { 83e0c4386eSCy Schubert fprintf(stderr, "EVP_MD_CTX_new failed.\n"); 84e0c4386eSCy Schubert ERR_print_errors_fp(stderr); 85e0c4386eSCy Schubert goto cleanup; 86e0c4386eSCy Schubert } 87e0c4386eSCy Schubert /* 88e0c4386eSCy Schubert * Initialize the message digest context to use the fetched 89e0c4386eSCy Schubert * digest provider 90e0c4386eSCy Schubert */ 91e0c4386eSCy Schubert if (EVP_DigestInit(digest_context, message_digest) != 1) { 92e0c4386eSCy Schubert fprintf(stderr, "EVP_DigestInit failed.\n"); 93e0c4386eSCy Schubert ERR_print_errors_fp(stderr); 94e0c4386eSCy Schubert goto cleanup; 95e0c4386eSCy Schubert } 96e0c4386eSCy Schubert while ((ii = BIO_read(input, buffer, sizeof(buffer))) > 0) { 97e0c4386eSCy Schubert if (EVP_DigestUpdate(digest_context, buffer, ii) != 1) { 98e0c4386eSCy Schubert fprintf(stderr, "EVP_DigestUpdate() failed.\n"); 99e0c4386eSCy Schubert goto cleanup; 100e0c4386eSCy Schubert } 101e0c4386eSCy Schubert } 102e0c4386eSCy Schubert if (EVP_DigestFinal(digest_context, digest_value, &digest_length) != 1) { 103e0c4386eSCy Schubert fprintf(stderr, "EVP_DigestFinal() failed.\n"); 104e0c4386eSCy Schubert goto cleanup; 105e0c4386eSCy Schubert } 106e0c4386eSCy Schubert result = 1; 107e0c4386eSCy Schubert for (ii=0; ii<digest_length; ii++) { 108e0c4386eSCy Schubert fprintf(stdout, "%02x", digest_value[ii]); 109e0c4386eSCy Schubert } 110e0c4386eSCy Schubert fprintf(stdout, "\n"); 111e0c4386eSCy Schubert 112e0c4386eSCy Schubert cleanup: 113e0c4386eSCy Schubert if (result != 1) 114e0c4386eSCy Schubert ERR_print_errors_fp(stderr); 115e0c4386eSCy Schubert /* OpenSSL free functions will ignore NULL arguments */ 116e0c4386eSCy Schubert EVP_MD_CTX_free(digest_context); 117e0c4386eSCy Schubert OPENSSL_free(digest_value); 118e0c4386eSCy Schubert EVP_MD_free(message_digest); 119e0c4386eSCy Schubert 120e0c4386eSCy Schubert OSSL_LIB_CTX_free(library_context); 121e0c4386eSCy Schubert return result; 122e0c4386eSCy Schubert } 123e0c4386eSCy Schubert 124e0c4386eSCy Schubert int main(void) 125e0c4386eSCy Schubert { 126e0c4386eSCy Schubert int result = 1; 127e0c4386eSCy Schubert BIO *input = BIO_new_fd( fileno(stdin), 1 ); 128e0c4386eSCy Schubert 129e0c4386eSCy Schubert if (input != NULL) { 130e0c4386eSCy Schubert result = demonstrate_digest(input); 131e0c4386eSCy Schubert BIO_free(input); 132e0c4386eSCy Schubert } 133e0c4386eSCy Schubert return result; 134e0c4386eSCy Schubert } 135