]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/digests/blake2_prov.c
Copyright year updates
[thirdparty/openssl.git] / providers / implementations / digests / blake2_prov.c
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 "prov/blake2.h"
12 #include "prov/digestcommon.h"
13 #include "prov/implementations.h"
14
15 int ossl_blake2s256_init(void *ctx)
16 {
17 BLAKE2S_PARAM P;
18
19 ossl_blake2s_param_init(&P);
20 return ossl_blake2s_init((BLAKE2S_CTX *)ctx, &P);
21 }
22
23 int ossl_blake2b512_init(void *ctx)
24 {
25 struct blake2b_md_data_st *mdctx = ctx;
26
27 ossl_blake2b_param_init(&mdctx->params);
28 return ossl_blake2b_init(&mdctx->ctx, &mdctx->params);
29 }
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 {
48 return ossl_prov_is_running() && ossl_blake2b_set_ctx_params(ctx, params)
49 && ossl_blake2b512_init(ctx);
50 }
51
52 static void *blake2b512_newctx(void *prov_ctx)
53 {
54 struct blake2b_md_data_st *ctx;
55
56 ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL;
57 return ctx;
58 }
59
60 static void blake2b512_freectx(void *vctx)
61 {
62 struct blake2b_md_data_st *ctx;
63
64 ctx = (struct blake2b_md_data_st *)vctx;
65 OPENSSL_clear_free(ctx, sizeof(*ctx));
66 }
67
68 static void *blake2b512_dupctx(void *ctx)
69 {
70 struct blake2b_md_data_st *in, *ret;
71
72 in = (struct blake2b_md_data_st *)ctx;
73 ret = ossl_prov_is_running()? OPENSSL_malloc(sizeof(*ret)) : NULL;
74 if (ret != NULL)
75 *ret = *in;
76 return ret;
77 }
78
79 static int blake2b512_internal_final(void *ctx, unsigned char *out,
80 size_t *outl, size_t outsz)
81 {
82 struct blake2b_md_data_st *b_ctx;
83
84 b_ctx = (struct blake2b_md_data_st *)ctx;
85 *outl = b_ctx->ctx.outlen;
86
87 if (!ossl_prov_is_running())
88 return 0;
89
90 return (outsz > 0) ? ossl_blake2b_final(out, ctx) : 1;
91 }
92
93 static int blake2b512_get_params(OSSL_PARAM params[])
94 {
95 return ossl_digest_default_get_params(params, BLAKE2B_BLOCKBYTES, 64, 0);
96 }
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