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