2 * Copyright 2019-2020 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 */
34 static void *self_test_set_callback_new(OPENSSL_CTX *ctx)
38 stcb = OPENSSL_zalloc(sizeof(*stcb));
42 static void self_test_set_callback_free(void *stcb)
47 static const OPENSSL_CTX_METHOD self_test_set_callback_method = {
48 self_test_set_callback_new,
49 self_test_set_callback_free,
52 static SELF_TEST_CB *get_self_test_callback(OPENSSL_CTX *libctx)
54 return openssl_ctx_get_data(libctx, OPENSSL_CTX_SELF_TEST_CB_INDEX,
55 &self_test_set_callback_method);
59 void OSSL_SELF_TEST_set_callback(OPENSSL_CTX *libctx, OSSL_CALLBACK *cb,
62 SELF_TEST_CB *stcb = get_self_test_callback(libctx);
69 #endif /* FIPS_MODE */
71 void OSSL_SELF_TEST_get_callback(OPENSSL_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);
82 static void self_test_setparams(OSSL_SELF_TEST *st)
88 OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_SELF_TEST_PHASE,
89 (char *)st->phase, 0);
91 OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_SELF_TEST_TYPE,
94 OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_SELF_TEST_DESC,
97 st->params[n++] = OSSL_PARAM_construct_end();
100 OSSL_SELF_TEST *OSSL_SELF_TEST_new(OSSL_CALLBACK *cb, void *cbarg)
102 OSSL_SELF_TEST *ret = OPENSSL_zalloc(sizeof(*ret));
112 self_test_setparams(ret);
116 void OSSL_SELF_TEST_free(OSSL_SELF_TEST *st)
121 /* Can be used during application testing to log that a test has started. */
122 void OSSL_SELF_TEST_onbegin(OSSL_SELF_TEST *st, const char *type,
125 if (st != NULL && st->cb != NULL) {
126 st->phase = OSSL_SELF_TEST_PHASE_START;
129 self_test_setparams(st);
130 (void)st->cb(st->params, st->cb_arg);
135 * Can be used during application testing to log that a test has either
138 void OSSL_SELF_TEST_onend(OSSL_SELF_TEST *st, int ret)
140 if (st != NULL && st->cb != NULL) {
142 (ret == 1 ? OSSL_SELF_TEST_PHASE_PASS : OSSL_SELF_TEST_PHASE_FAIL);
143 self_test_setparams(st);
144 (void)st->cb(st->params, st->cb_arg);
146 st->phase = OSSL_SELF_TEST_PHASE_NONE;
147 st->type = OSSL_SELF_TEST_TYPE_NONE;
148 st->desc = OSSL_SELF_TEST_DESC_NONE;
153 * Used for failure testing.
155 * Call the applications SELF_TEST_cb() if it exists.
156 * If the application callback decides to return 0 then the first byte of 'bytes'
157 * is modified (corrupted). This is used to modify output signatures or
158 * ciphertext before they are verified or decrypted.
160 void OSSL_SELF_TEST_oncorrupt_byte(OSSL_SELF_TEST *st, unsigned char *bytes)
162 if (st != NULL && st->cb != NULL) {
163 st->phase = OSSL_SELF_TEST_PHASE_CORRUPT;
164 self_test_setparams(st);
165 if (!st->cb(st->params, st->cb_arg))