/src/openssl/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 | 0 | { |
18 | 0 | BLAKE2S_PARAM P; |
19 | |
|
20 | 0 | ossl_blake2s_param_init(&P); |
21 | 0 | return ossl_blake2s_init((BLAKE2S_CTX *)ctx, &P); |
22 | 0 | } |
23 | | |
24 | | static int ossl_blake2b512_init(void *ctx) |
25 | 0 | { |
26 | 0 | struct blake2b_md_data_st *mdctx = ctx; |
27 | 0 | uint8_t digest_length = mdctx->params.digest_length; |
28 | |
|
29 | 0 | ossl_blake2b_param_init(&mdctx->params); |
30 | 0 | if (digest_length != 0) |
31 | 0 | mdctx->params.digest_length = digest_length; |
32 | 0 | return ossl_blake2b_init(&mdctx->ctx, &mdctx->params); |
33 | 0 | } |
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 | 0 | { |
52 | 0 | return ossl_prov_is_running() && ossl_blake2b_set_ctx_params(ctx, params) |
53 | 0 | && ossl_blake2b512_init(ctx); |
54 | 0 | } |
55 | | |
56 | | static void *blake2b512_newctx(void *prov_ctx) |
57 | 0 | { |
58 | 0 | struct blake2b_md_data_st *ctx; |
59 | |
|
60 | 0 | ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; |
61 | 0 | return ctx; |
62 | 0 | } |
63 | | |
64 | | static void blake2b512_freectx(void *vctx) |
65 | 0 | { |
66 | 0 | struct blake2b_md_data_st *ctx; |
67 | |
|
68 | 0 | ctx = (struct blake2b_md_data_st *)vctx; |
69 | 0 | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
70 | 0 | } |
71 | | |
72 | | static void *blake2b512_dupctx(void *ctx) |
73 | 0 | { |
74 | 0 | struct blake2b_md_data_st *in, *ret; |
75 | |
|
76 | 0 | in = (struct blake2b_md_data_st *)ctx; |
77 | 0 | ret = ossl_prov_is_running()? OPENSSL_malloc(sizeof(*ret)) : NULL; |
78 | 0 | if (ret != NULL) |
79 | 0 | *ret = *in; |
80 | 0 | return ret; |
81 | 0 | } |
82 | | |
83 | | static int blake2b512_internal_final(void *ctx, unsigned char *out, |
84 | | size_t *outl, size_t outsz) |
85 | 0 | { |
86 | 0 | struct blake2b_md_data_st *b_ctx; |
87 | |
|
88 | 0 | b_ctx = (struct blake2b_md_data_st *)ctx; |
89 | |
|
90 | 0 | if (!ossl_prov_is_running()) |
91 | 0 | return 0; |
92 | | |
93 | 0 | *outl = b_ctx->ctx.outlen; |
94 | |
|
95 | 0 | if (outsz == 0) |
96 | 0 | return 1; |
97 | | |
98 | 0 | if (outsz < *outl) { |
99 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); |
100 | 0 | return 0; |
101 | 0 | } |
102 | | |
103 | 0 | return ossl_blake2b_final(out, ctx); |
104 | 0 | } |
105 | | |
106 | | static int blake2b512_get_params(OSSL_PARAM params[]) |
107 | 11 | { |
108 | 11 | return ossl_digest_default_get_params(params, BLAKE2B_BLOCKBYTES, 64, 0); |
109 | 11 | } |
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 | | }; |