2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/self_test.h>
11 #include <openssl/core_names.h>
12 #include <openssl/params.h>
13 #include "internal/cryptlib.h"
15 typedef struct self_test_cb_st
21 struct ossl_self_test_st
23 /* local state variables */
29 /* callback related variables used to pass the state back to the user */
35 static void *self_test_set_callback_new(OSSL_LIB_CTX *ctx)
39 stcb = OPENSSL_zalloc(sizeof(*stcb));
43 static void self_test_set_callback_free(void *stcb)
48 static const OSSL_LIB_CTX_METHOD self_test_set_callback_method = {
49 OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
50 self_test_set_callback_new,
51 self_test_set_callback_free,
54 static SELF_TEST_CB *get_self_test_callback(OSSL_LIB_CTX *libctx)
56 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_SELF_TEST_CB_INDEX,
57 &self_test_set_callback_method);
60 void OSSL_SELF_TEST_set_callback(OSSL_LIB_CTX *libctx, OSSL_CALLBACK *cb,
63 SELF_TEST_CB *stcb = get_self_test_callback(libctx);
71 void OSSL_SELF_TEST_get_callback(OSSL_LIB_CTX *libctx, OSSL_CALLBACK **cb,
74 SELF_TEST_CB *stcb = get_self_test_callback(libctx);
77 *cb = (stcb != NULL ? stcb->cb : NULL);
79 *cbarg = (stcb != NULL ? stcb->cbarg : NULL);
81 #endif /* FIPS_MODULE */
83 static void self_test_setparams(OSSL_SELF_TEST *st)
89 OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_SELF_TEST_PHASE,
90 (char *)st->phase, 0);
92 OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_SELF_TEST_TYPE,
95 OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_SELF_TEST_DESC,
98 st->params[n++] = OSSL_PARAM_construct_end();
101 OSSL_SELF_TEST *OSSL_SELF_TEST_new(OSSL_CALLBACK *cb, void *cbarg)
103 OSSL_SELF_TEST *ret = OPENSSL_zalloc(sizeof(*ret));
113 self_test_setparams(ret);
117 void OSSL_SELF_TEST_free(OSSL_SELF_TEST *st)
122 /* Can be used during application testing to log that a test has started. */
123 void OSSL_SELF_TEST_onbegin(OSSL_SELF_TEST *st, const char *type,
126 if (st != NULL && st->cb != NULL) {
127 st->phase = OSSL_SELF_TEST_PHASE_START;
130 self_test_setparams(st);
131 (void)st->cb(st->params, st->cb_arg);
136 * Can be used during application testing to log that a test has either
139 void OSSL_SELF_TEST_onend(OSSL_SELF_TEST *st, int ret)
141 if (st != NULL && st->cb != NULL) {
143 (ret == 1 ? OSSL_SELF_TEST_PHASE_PASS : OSSL_SELF_TEST_PHASE_FAIL);
144 self_test_setparams(st);
145 (void)st->cb(st->params, st->cb_arg);
147 st->phase = OSSL_SELF_TEST_PHASE_NONE;
148 st->type = OSSL_SELF_TEST_TYPE_NONE;
149 st->desc = OSSL_SELF_TEST_DESC_NONE;
154 * Used for failure testing.
156 * Call the applications SELF_TEST_cb() if it exists.
157 * If the application callback decides to return 0 then the first byte of 'bytes'
158 * is modified (corrupted). This is used to modify output signatures or
159 * ciphertext before they are verified or decrypted.
161 int OSSL_SELF_TEST_oncorrupt_byte(OSSL_SELF_TEST *st, unsigned char *bytes)
163 if (st != NULL && st->cb != NULL) {
164 st->phase = OSSL_SELF_TEST_PHASE_CORRUPT;
165 self_test_setparams(st);
166 if (!st->cb(st->params, st->cb_arg)) {