]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/common/digests/sha2.c
Add an EVP_MD_CTX_md() test
[thirdparty/openssl.git] / providers / common / digests / sha2.c
CommitLineData
de29ff17
MC
1/*
2 * Copyright 2019 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#include <openssl/sha.h>
11#include <openssl/crypto.h>
12#include <openssl/core_numbers.h>
13
14static int sha256_final(void *ctx, unsigned char *md, size_t *size)
15{
16 if (SHA256_Final(md, ctx)) {
17 *size = SHA256_DIGEST_LENGTH;
18 return 1;
19 }
20
21 return 0;
22}
23
24static void *sha256_newctx(void)
25{
26 SHA256_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
27
28 return ctx;
29}
30
31static void sha256_freectx(void *vctx)
32{
33 SHA256_CTX *ctx = (SHA256_CTX *)vctx;
34
35 OPENSSL_clear_free(ctx, sizeof(*ctx));
36}
37
38static void *sha256_dupctx(void *ctx)
39{
40 SHA256_CTX *in = (SHA256_CTX *)ctx;
41 SHA256_CTX *ret = OPENSSL_malloc(sizeof(*ret));
42
43 *ret = *in;
44
45 return ret;
46}
47
48static size_t sha256_size(void)
49{
50 return SHA256_DIGEST_LENGTH;
51}
52
53extern const OSSL_DISPATCH sha256_functions[];
54const OSSL_DISPATCH sha256_functions[] = {
55 { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))sha256_newctx },
56 { OSSL_FUNC_DIGEST_INIT, (void (*)(void))SHA256_Init },
57 { OSSL_FUNC_DIGEST_UPDDATE, (void (*)(void))SHA256_Update },
58 { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))sha256_final },
59 { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))sha256_freectx },
60 { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))sha256_dupctx },
61 { OSSL_FUNC_DIGEST_SIZE, (void (*)(void))sha256_size },
62 { 0, NULL }
63};