]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/common/digests/sha2.c
Add the provider_algs.h internal header file
[thirdparty/openssl.git] / providers / common / digests / sha2.c
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 #include "internal/provider_algs.h"
14
15 /*
16 * Forward declaration of everything implemented here. This is not strictly
17 * necessary for the compiler, but provides an assurance that the signatures
18 * of the functions in the dispatch table are correct.
19 */
20 static OSSL_OP_digest_newctx_fn sha256_newctx;
21 #if 0 /* Not defined here */
22 static OSSL_OP_digest_init_fn sha256_init;
23 static OSSL_OP_digest_update_fn sha256_update;
24 #endif
25 static OSSL_OP_digest_final_fn sha256_final;
26 static OSSL_OP_digest_freectx_fn sha256_freectx;
27 static OSSL_OP_digest_dupctx_fn sha256_dupctx;
28 static OSSL_OP_digest_size_fn sha256_size;
29 static OSSL_OP_digest_block_size_fn sha256_size;
30
31 static int sha256_final(void *ctx,
32 unsigned char *md, size_t *mdl, size_t mdsz)
33 {
34 if (mdsz >= SHA256_DIGEST_LENGTH
35 && SHA256_Final(md, ctx)) {
36 *mdl = SHA256_DIGEST_LENGTH;
37 return 1;
38 }
39
40 return 0;
41 }
42
43 static void *sha256_newctx(void)
44 {
45 SHA256_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
46
47 return ctx;
48 }
49
50 static void sha256_freectx(void *vctx)
51 {
52 SHA256_CTX *ctx = (SHA256_CTX *)vctx;
53
54 OPENSSL_clear_free(ctx, sizeof(*ctx));
55 }
56
57 static void *sha256_dupctx(void *ctx)
58 {
59 SHA256_CTX *in = (SHA256_CTX *)ctx;
60 SHA256_CTX *ret = OPENSSL_malloc(sizeof(*ret));
61
62 *ret = *in;
63
64 return ret;
65 }
66
67 static size_t sha256_size(void)
68 {
69 return SHA256_DIGEST_LENGTH;
70 }
71
72 static size_t sha256_block_size(void)
73 {
74 return SHA256_CBLOCK;
75 }
76
77 const OSSL_DISPATCH sha256_functions[] = {
78 { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))sha256_newctx },
79 { OSSL_FUNC_DIGEST_INIT, (void (*)(void))SHA256_Init },
80 { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))SHA256_Update },
81 { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))sha256_final },
82 { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))sha256_freectx },
83 { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))sha256_dupctx },
84 { OSSL_FUNC_DIGEST_SIZE, (void (*)(void))sha256_size },
85 { OSSL_FUNC_DIGEST_BLOCK_SIZE, (void (*)(void))sha256_block_size },
86 { 0, NULL }
87 };