openssl3.2 - 官方demo学习 - digest - BIO_f_md.c

发布时间:2024年01月14日

openssl3.2 - 官方demo学习 - digest - BIO_f_md.c

概述

计算buffer的摘要(例子中使用的摘要算法为 SHA3-512)
add _CRT_NONSTDC_NO_WARNINGS to VS2019 option

笔记

/*!
\file BIO_f_md.c
\note openssl3.2 - 官方demo学习 - digest - BIO_f_md.c
计算buffer的摘要(例子中使用的摘要算法为 SHA3-512)
add _CRT_NONSTDC_NO_WARNINGS to VS2019 option
*/

/*-
 * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

/*-
 * Example of using EVP_MD_fetch and EVP_Digest* methods to calculate
 * a digest of static buffers
 * You can find SHA3 test vectors from NIST here:
 * https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/sha3/sha-3bytetestvectors.zip
 * For example, contains these lines:
    Len = 80
    Msg = 1ca984dcc913344370cf
    MD = 6915ea0eeffb99b9b246a0e34daf3947852684c3d618260119a22835659e4f23d4eb66a15d0affb8e93771578f5e8f25b7a5f2a55f511fb8b96325ba2cd14816
 * use xxd convert the hex message string to binary input for BIO_f_md:
 * echo "1ca984dcc913344370cf" | xxd -r -p | ./BIO_f_md
 * and then verify the output matches MD above.
 */

#include <string.h>
#include <stdio.h>
#include <stdbool.h> // for true/flase

#include <openssl/err.h>
#include <openssl/bio.h>
#include <openssl/evp.h>

#include "my_openSSL_lib.h"

/*-
 * This demonstration will show how to digest data using
 * a BIO configured with a message digest
 * A message digest name may be passed as an argument.
 * The default digest is SHA3-512
 */

int main(int argc, char *argv[])
{
    int ret = EXIT_FAILURE;
    OSSL_LIB_CTX *_ossl_lib_ctx = NULL;
    BIO *_bio_input = NULL;
    BIO *_bio_digest = NULL, *_bio_reading = NULL;
    EVP_MD *_evp_md = NULL;
    unsigned char buffer[512];
    size_t _sz_digest;
    char *_psz_digest_value = NULL;
    int j;

    _bio_input = BIO_new_fd(fileno(stdin), 1);
    if (_bio_input == NULL) {
        fprintf(stderr, "BIO_new_fd() for stdin returned NULL\n");
        goto cleanup;
    }
    _ossl_lib_ctx = OSSL_LIB_CTX_new();
    if (_ossl_lib_ctx == NULL) {
        fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
        goto cleanup;
    }

    /*
     * Fetch a message digest by name
     * The algorithm name is case insensitive. 
     * See providers(7) for details about algorithm fetching
     */
    _evp_md = EVP_MD_fetch(_ossl_lib_ctx, "SHA3-512", NULL);
    if (_evp_md == NULL) {
        fprintf(stderr, "EVP_MD_fetch did not find SHA3-512.\n");
        goto cleanup;
    }
    _sz_digest = EVP_MD_get_size(_evp_md);
    _psz_digest_value = OPENSSL_malloc(_sz_digest);
    if (_psz_digest_value == NULL) {
        fprintf(stderr, "Can't allocate %lu bytes for the digest value.\n", (unsigned long)_sz_digest);
        goto cleanup;
    }
    /* Make a bio that uses the digest */
    _bio_digest = BIO_new(BIO_f_md());
    if (_bio_digest == NULL) {
        fprintf(stderr, "BIO_new(BIO_f_md()) returned NULL\n");
        goto cleanup;
    }
    /* set our bio_digest BIO to digest data */
    if (BIO_set_md(_bio_digest, _evp_md) != 1) {
           fprintf(stderr, "BIO_set_md failed.\n");
           goto cleanup;
    }
    /*-
     * We will use BIO chaining so that as we read, the digest gets updated
     * See the man page for BIO_push
     */
    _bio_reading = BIO_push(_bio_digest, _bio_input);

    // while (BIO_read(reading, buffer, sizeof(buffer)) > 0); // 这么写有问题, 一直在死等stdin人工输入
    do {
        if (BIO_read(_bio_reading, buffer, sizeof(buffer)) > 0)
        {
            break; // 只要用户输入了东西, 就跳出.
        }
    } while (true);

    /*-
     * BIO_gets must be used to calculate the final
     * digest value and then copy it to digest_value.
     */
    if (BIO_gets(_bio_digest, _psz_digest_value, (int)_sz_digest) != (int)_sz_digest) {
        fprintf(stderr, "BIO_gets(bio_digest) failed\n");
        goto cleanup;
    }
    for (j = 0; j < _sz_digest; j++) {
        fprintf(stdout, "%02x", (unsigned char)_psz_digest_value[j]);
    }
    fprintf(stdout, "\n");
    ret = EXIT_SUCCESS;

cleanup:
    if (ret != EXIT_SUCCESS)
        ERR_print_errors_fp(stderr);

    OPENSSL_free(_psz_digest_value);
    BIO_free(_bio_input);
    BIO_free(_bio_digest);
    EVP_MD_free(_evp_md);
    OSSL_LIB_CTX_free(_ossl_lib_ctx);

    return ret;
}

END

文章来源:https://blog.csdn.net/LostSpeed/article/details/135580065
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。