Add FIPS Self test kats for digests
[openssl.git] / crypto / self_test_core.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/self_test.h>
11 #include "internal/cryptlib.h"
12
13 typedef struct self_test_cb_st
14 {
15     OSSL_CALLBACK *cb;
16     void *cbarg;
17 } SELF_TEST_CB;
18
19 static void *self_test_set_callback_new(OPENSSL_CTX *ctx)
20 {
21     SELF_TEST_CB *stcb;
22
23     stcb = OPENSSL_zalloc(sizeof(*stcb));
24     return stcb;
25 }
26
27 static void self_test_set_callback_free(void *stcb)
28 {
29     OPENSSL_free(stcb);
30 }
31
32 static const OPENSSL_CTX_METHOD self_test_set_callback_method = {
33     self_test_set_callback_new,
34     self_test_set_callback_free,
35 };
36
37 static SELF_TEST_CB *get_self_test_callback(OPENSSL_CTX *libctx)
38 {
39     return openssl_ctx_get_data(libctx, OPENSSL_CTX_SELF_TEST_CB_INDEX,
40                                 &self_test_set_callback_method);
41 }
42
43 void OSSL_SELF_TEST_set_callback(OPENSSL_CTX *libctx, OSSL_CALLBACK *cb,
44                                  void *cbarg)
45 {
46     SELF_TEST_CB *stcb = get_self_test_callback(libctx);
47
48     if (stcb != NULL) {
49         stcb->cb = cb;
50         stcb->cbarg = cbarg;
51     }
52 }
53 void OSSL_SELF_TEST_get_callback(OPENSSL_CTX *libctx, OSSL_CALLBACK **cb,
54                                  void **cbarg)
55 {
56     SELF_TEST_CB *stcb = get_self_test_callback(libctx);
57
58     if (cb != NULL)
59         *cb = (stcb != NULL ? stcb->cb : NULL);
60     if (cbarg != NULL)
61         *cbarg = (stcb != NULL ? stcb->cbarg : NULL);
62 }