/src/openssl32/providers/implementations/digests/blake2_prov.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2023 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/crypto.h> |
11 | | #include <openssl/proverr.h> |
12 | | #include "prov/blake2.h" |
13 | | #include "prov/digestcommon.h" |
14 | | #include "prov/implementations.h" |
15 | | |
16 | | static int ossl_blake2s256_init(void *ctx) |
17 | | { |
18 | | BLAKE2S_PARAM P; |
19 | | |
20 | | ossl_blake2s_param_init(&P); |
21 | | return ossl_blake2s_init((BLAKE2S_CTX *)ctx, &P); |
22 | | } |
23 | | |
24 | | static int ossl_blake2b512_init(void *ctx) |
25 | 27.6k | { |
26 | 27.6k | struct blake2b_md_data_st *mdctx = ctx; |
27 | 27.6k | uint8_t digest_length = mdctx->params.digest_length; |
28 | | |
29 | 27.6k | ossl_blake2b_param_init(&mdctx->params); |
30 | 27.6k | if (digest_length != 0) |
31 | 27.4k | mdctx->params.digest_length = digest_length; |
32 | 27.6k | return ossl_blake2b_init(&mdctx->ctx, &mdctx->params); |
33 | 27.6k | } |
34 | | |
35 | | /* ossl_blake2s256_functions */ |
36 | | IMPLEMENT_digest_functions(blake2s256, BLAKE2S_CTX, |
37 | | BLAKE2S_BLOCKBYTES, BLAKE2S_DIGEST_LENGTH, 0, |
38 | | ossl_blake2s256_init, ossl_blake2s_update, |
39 | | ossl_blake2s_final) |
40 | | |
41 | | /* ossl_blake2b512_functions */ |
42 | | |
43 | | static OSSL_FUNC_digest_init_fn blake2b512_internal_init; |
44 | | static OSSL_FUNC_digest_newctx_fn blake2b512_newctx; |
45 | | static OSSL_FUNC_digest_freectx_fn blake2b512_freectx; |
46 | | static OSSL_FUNC_digest_dupctx_fn blake2b512_dupctx; |
47 | | static OSSL_FUNC_digest_final_fn blake2b512_internal_final; |
48 | | static OSSL_FUNC_digest_get_params_fn blake2b512_get_params; |
49 | | |
50 | | static int blake2b512_internal_init(void *ctx, const OSSL_PARAM params[]) |
51 | 27.6k | { |
52 | 27.6k | return ossl_prov_is_running() && ossl_blake2b_set_ctx_params(ctx, params) |
53 | 27.6k | && ossl_blake2b512_init(ctx); |
54 | 27.6k | } |
55 | | |
56 | | static void *blake2b512_newctx(void *prov_ctx) |
57 | 27.2k | { |
58 | 27.2k | struct blake2b_md_data_st *ctx; |
59 | | |
60 | 27.2k | ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; |
61 | 27.2k | return ctx; |
62 | 27.2k | } |
63 | | |
64 | | static void blake2b512_freectx(void *vctx) |
65 | 27.3k | { |
66 | 27.3k | struct blake2b_md_data_st *ctx; |
67 | | |
68 | 27.3k | ctx = (struct blake2b_md_data_st *)vctx; |
69 | 27.3k | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
70 | 27.3k | } |
71 | | |
72 | | static void *blake2b512_dupctx(void *ctx) |
73 | 35 | { |
74 | 35 | struct blake2b_md_data_st *in, *ret; |
75 | | |
76 | 35 | in = (struct blake2b_md_data_st *)ctx; |
77 | 35 | ret = ossl_prov_is_running()? OPENSSL_malloc(sizeof(*ret)) : NULL; |
78 | 35 | if (ret != NULL) |
79 | 35 | *ret = *in; |
80 | 35 | return ret; |
81 | 35 | } |
82 | | |
83 | | static int blake2b512_internal_final(void *ctx, unsigned char *out, |
84 | | size_t *outl, size_t outsz) |
85 | 27.6k | { |
86 | 27.6k | struct blake2b_md_data_st *b_ctx; |
87 | | |
88 | 27.6k | b_ctx = (struct blake2b_md_data_st *)ctx; |
89 | | |
90 | 27.6k | if (!ossl_prov_is_running()) |
91 | 0 | return 0; |
92 | | |
93 | 27.6k | *outl = b_ctx->ctx.outlen; |
94 | | |
95 | 27.6k | if (outsz == 0) |
96 | 0 | return 1; |
97 | | |
98 | 27.6k | if (outsz < *outl) { |
99 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); |
100 | 0 | return 0; |
101 | 0 | } |
102 | | |
103 | 27.6k | return ossl_blake2b_final(out, ctx); |
104 | 27.6k | } |
105 | | |
106 | | static int blake2b512_get_params(OSSL_PARAM params[]) |
107 | 43 | { |
108 | 43 | return ossl_digest_default_get_params(params, BLAKE2B_BLOCKBYTES, 64, 0); |
109 | 43 | } |
110 | | |
111 | | const OSSL_DISPATCH ossl_blake2b512_functions[] = { |
112 | | {OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))blake2b512_newctx}, |
113 | | {OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))ossl_blake2b_update}, |
114 | | {OSSL_FUNC_DIGEST_FINAL, (void (*)(void))blake2b512_internal_final}, |
115 | | {OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))blake2b512_freectx}, |
116 | | {OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))blake2b512_dupctx}, |
117 | | {OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))blake2b512_get_params}, |
118 | | {OSSL_FUNC_DIGEST_GETTABLE_PARAMS, |
119 | | (void (*)(void))ossl_digest_default_gettable_params}, |
120 | | {OSSL_FUNC_DIGEST_INIT, (void (*)(void))blake2b512_internal_init}, |
121 | | {OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS, |
122 | | (void (*)(void))ossl_blake2b_gettable_ctx_params}, |
123 | | {OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, |
124 | | (void (*)(void))ossl_blake2b_settable_ctx_params}, |
125 | | {OSSL_FUNC_DIGEST_GET_CTX_PARAMS, |
126 | | (void (*)(void))ossl_blake2b_get_ctx_params}, |
127 | | {OSSL_FUNC_DIGEST_SET_CTX_PARAMS, |
128 | | (void (*)(void))ossl_blake2b_set_ctx_params}, |
129 | | {0, NULL} |
130 | | }; |