]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/common/digests/sha2.c
fb2bd95f5235a5af8d484f9d0c88ee5c7209304c
[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
14 static int sha256_final(void *ctx,
15 unsigned char *md, size_t *mdl, size_t mdsz)
16 {
17 if (mdsz >= SHA256_DIGEST_LENGTH
18 && SHA256_Final(md, ctx)) {
19 *mdl = SHA256_DIGEST_LENGTH;
20 return 1;
21 }
22
23 return 0;
24 }
25
26 static void *sha256_newctx(void)
27 {
28 SHA256_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
29
30 return ctx;
31 }
32
33 static void sha256_freectx(void *vctx)
34 {
35 SHA256_CTX *ctx = (SHA256_CTX *)vctx;
36
37 OPENSSL_clear_free(ctx, sizeof(*ctx));
38 }
39
40 static void *sha256_dupctx(void *ctx)
41 {
42 SHA256_CTX *in = (SHA256_CTX *)ctx;
43 SHA256_CTX *ret = OPENSSL_malloc(sizeof(*ret));
44
45 *ret = *in;
46
47 return ret;
48 }
49
50 static size_t sha256_size(void)
51 {
52 return SHA256_DIGEST_LENGTH;
53 }
54
55 static size_t sha256_block_size(void)
56 {
57 return SHA256_CBLOCK;
58 }
59
60 extern const OSSL_DISPATCH sha256_functions[];
61 const OSSL_DISPATCH sha256_functions[] = {
62 { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))sha256_newctx },
63 { OSSL_FUNC_DIGEST_INIT, (void (*)(void))SHA256_Init },
64 { OSSL_FUNC_DIGEST_UPDDATE, (void (*)(void))SHA256_Update },
65 { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))sha256_final },
66 { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))sha256_freectx },
67 { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))sha256_dupctx },
68 { OSSL_FUNC_DIGEST_SIZE, (void (*)(void))sha256_size },
69 { OSSL_FUNC_DIGEST_BLOCK_SIZE, (void (*)(void))sha256_block_size },
70 { 0, NULL }
71 };