xref: /freebsd/crypto/openssl/demos/digest/BIO_f_md.c (revision e0c4386e)
1*e0c4386eSCy Schubert /*-
2*e0c4386eSCy Schubert  * Copyright 2019-2021 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 /*-
11*e0c4386eSCy Schubert  * Example of using EVP_MD_fetch and EVP_Digest* methods to calculate
12*e0c4386eSCy Schubert  * a digest of static buffers
13*e0c4386eSCy Schubert  * You can find SHA3 test vectors from NIST here:
14*e0c4386eSCy Schubert  * https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/sha3/sha-3bytetestvectors.zip
15*e0c4386eSCy Schubert  * For example, contains these lines:
16*e0c4386eSCy Schubert     Len = 80
17*e0c4386eSCy Schubert     Msg = 1ca984dcc913344370cf
18*e0c4386eSCy Schubert     MD = 6915ea0eeffb99b9b246a0e34daf3947852684c3d618260119a22835659e4f23d4eb66a15d0affb8e93771578f5e8f25b7a5f2a55f511fb8b96325ba2cd14816
19*e0c4386eSCy Schubert  * use xxd convert the hex message string to binary input for BIO_f_md:
20*e0c4386eSCy Schubert  * echo "1ca984dcc913344370cf" | xxd -r -p | ./BIO_f_md
21*e0c4386eSCy Schubert  * and then verify the output matches MD above.
22*e0c4386eSCy Schubert  */
23*e0c4386eSCy Schubert 
24*e0c4386eSCy Schubert #include <string.h>
25*e0c4386eSCy Schubert #include <stdio.h>
26*e0c4386eSCy Schubert #include <openssl/err.h>
27*e0c4386eSCy Schubert #include <openssl/bio.h>
28*e0c4386eSCy Schubert #include <openssl/evp.h>
29*e0c4386eSCy Schubert 
30*e0c4386eSCy Schubert /*-
31*e0c4386eSCy Schubert  * This demonstration will show how to digest data using
32*e0c4386eSCy Schubert  * a BIO configured with a message digest
33*e0c4386eSCy Schubert  * A message digest name may be passed as an argument.
34*e0c4386eSCy Schubert  * The default digest is SHA3-512
35*e0c4386eSCy Schubert  */
36*e0c4386eSCy Schubert 
main(int argc,char * argv[])37*e0c4386eSCy Schubert int main(int argc, char * argv[])
38*e0c4386eSCy Schubert {
39*e0c4386eSCy Schubert     int result = 1;
40*e0c4386eSCy Schubert     OSSL_LIB_CTX *library_context = NULL;
41*e0c4386eSCy Schubert     BIO *input = NULL;
42*e0c4386eSCy Schubert     BIO *bio_digest = NULL;
43*e0c4386eSCy Schubert     EVP_MD *md = NULL;
44*e0c4386eSCy Schubert     unsigned char buffer[512];
45*e0c4386eSCy Schubert     size_t readct, writect;
46*e0c4386eSCy Schubert     size_t digest_size;
47*e0c4386eSCy Schubert     char *digest_value=NULL;
48*e0c4386eSCy Schubert     int j;
49*e0c4386eSCy Schubert 
50*e0c4386eSCy Schubert     input = BIO_new_fd( fileno(stdin), 1 );
51*e0c4386eSCy Schubert     if (input == NULL) {
52*e0c4386eSCy Schubert         fprintf(stderr, "BIO_new_fd() for stdin returned NULL\n");
53*e0c4386eSCy Schubert         goto cleanup;
54*e0c4386eSCy Schubert     }
55*e0c4386eSCy Schubert     library_context = OSSL_LIB_CTX_new();
56*e0c4386eSCy Schubert     if (library_context == NULL) {
57*e0c4386eSCy Schubert         fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
58*e0c4386eSCy Schubert         goto cleanup;
59*e0c4386eSCy Schubert     }
60*e0c4386eSCy Schubert 
61*e0c4386eSCy Schubert     /*
62*e0c4386eSCy Schubert      * Fetch a message digest by name
63*e0c4386eSCy Schubert      * The algorithm name is case insensitive.
64*e0c4386eSCy Schubert      * See providers(7) for details about algorithm fetching
65*e0c4386eSCy Schubert      */
66*e0c4386eSCy Schubert     md = EVP_MD_fetch( library_context, "SHA3-512", NULL );
67*e0c4386eSCy Schubert     if (md == NULL) {
68*e0c4386eSCy Schubert         fprintf(stderr, "EVP_MD_fetch did not find SHA3-512.\n");
69*e0c4386eSCy Schubert         goto cleanup;
70*e0c4386eSCy Schubert     }
71*e0c4386eSCy Schubert     digest_size = EVP_MD_get_size(md);
72*e0c4386eSCy Schubert     digest_value = OPENSSL_malloc(digest_size);
73*e0c4386eSCy Schubert     if (digest_value == NULL) {
74*e0c4386eSCy Schubert         fprintf(stderr, "Can't allocate %lu bytes for the digest value.\n", (unsigned long)digest_size);
75*e0c4386eSCy Schubert         goto cleanup;
76*e0c4386eSCy Schubert     }
77*e0c4386eSCy Schubert     /* Make a bio that uses the digest */
78*e0c4386eSCy Schubert     bio_digest = BIO_new(BIO_f_md());
79*e0c4386eSCy Schubert     if (bio_digest == NULL) {
80*e0c4386eSCy Schubert         fprintf(stderr, "BIO_new(BIO_f_md()) returned NULL\n");
81*e0c4386eSCy Schubert         goto cleanup;
82*e0c4386eSCy Schubert     }
83*e0c4386eSCy Schubert     /* set our bio_digest BIO to digest data */
84*e0c4386eSCy Schubert     if (BIO_set_md(bio_digest,md) != 1) {
85*e0c4386eSCy Schubert            fprintf(stderr, "BIO_set_md failed.\n");
86*e0c4386eSCy Schubert            goto cleanup;
87*e0c4386eSCy Schubert     }
88*e0c4386eSCy Schubert     /*-
89*e0c4386eSCy Schubert      * We will use BIO chaining so that as we read, the digest gets updated
90*e0c4386eSCy Schubert      * See the man page for BIO_push
91*e0c4386eSCy Schubert      */
92*e0c4386eSCy Schubert     BIO *reading = BIO_push( bio_digest, input );
93*e0c4386eSCy Schubert 
94*e0c4386eSCy Schubert     while( BIO_read(reading, buffer, sizeof(buffer)) > 0 )
95*e0c4386eSCy Schubert         ;
96*e0c4386eSCy Schubert 
97*e0c4386eSCy Schubert     /*-
98*e0c4386eSCy Schubert      * BIO_gets must be used to calculate the final
99*e0c4386eSCy Schubert      * digest value and then copy it to digest_value.
100*e0c4386eSCy Schubert      */
101*e0c4386eSCy Schubert     if (BIO_gets(bio_digest, digest_value, digest_size) != digest_size) {
102*e0c4386eSCy Schubert         fprintf(stderr, "BIO_gets(bio_digest) failed\n");
103*e0c4386eSCy Schubert         goto cleanup;
104*e0c4386eSCy Schubert     }
105*e0c4386eSCy Schubert     for (j=0; j<digest_size; j++) {
106*e0c4386eSCy Schubert         fprintf(stdout, "%02x", (unsigned char)digest_value[j]);
107*e0c4386eSCy Schubert     }
108*e0c4386eSCy Schubert     fprintf(stdout, "\n");
109*e0c4386eSCy Schubert     result = 0;
110*e0c4386eSCy Schubert 
111*e0c4386eSCy Schubert cleanup:
112*e0c4386eSCy Schubert     if (result != 0)
113*e0c4386eSCy Schubert         ERR_print_errors_fp(stderr);
114*e0c4386eSCy Schubert 
115*e0c4386eSCy Schubert     OPENSSL_free(digest_value);
116*e0c4386eSCy Schubert     BIO_free(input);
117*e0c4386eSCy Schubert     BIO_free(bio_digest);
118*e0c4386eSCy Schubert     EVP_MD_free(md);
119*e0c4386eSCy Schubert     OSSL_LIB_CTX_free(library_context);
120*e0c4386eSCy Schubert 
121*e0c4386eSCy Schubert     return result;
122*e0c4386eSCy Schubert }
123