VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.7/demos/digest/BIO_f_md.c@ 106683

Last change on this file since 106683 was 104078, checked in by vboxsync, 10 months ago

openssl-3.1.5: Applied and adjusted our OpenSSL changes to 3.1.4. bugref:10638

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette