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