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"
14 #include "crypto/context.h"
16 typedef struct self_test_cb_st
22 struct ossl_self_test_st
24 /* local state variables */
30 /* callback related variables used to pass the state back to the user */
36 void *ossl_self_test_set_callback_new(OSSL_LIB_CTX
*ctx
)
40 stcb
= OPENSSL_zalloc(sizeof(*stcb
));
44 void ossl_self_test_set_callback_free(void *stcb
)
49 static SELF_TEST_CB
*get_self_test_callback(OSSL_LIB_CTX
*libctx
)
51 return ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_SELF_TEST_CB_INDEX
);
54 void OSSL_SELF_TEST_set_callback(OSSL_LIB_CTX
*libctx
, OSSL_CALLBACK
*cb
,
57 SELF_TEST_CB
*stcb
= get_self_test_callback(libctx
);
65 void OSSL_SELF_TEST_get_callback(OSSL_LIB_CTX
*libctx
, OSSL_CALLBACK
**cb
,
68 SELF_TEST_CB
*stcb
= get_self_test_callback(libctx
);
71 *cb
= (stcb
!= NULL
? stcb
->cb
: NULL
);
73 *cbarg
= (stcb
!= NULL
? stcb
->cbarg
: NULL
);
75 #endif /* FIPS_MODULE */
77 static void self_test_setparams(OSSL_SELF_TEST
*st
)
83 OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_SELF_TEST_PHASE
,
84 (char *)st
->phase
, 0);
86 OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_SELF_TEST_TYPE
,
89 OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_SELF_TEST_DESC
,
92 st
->params
[n
++] = OSSL_PARAM_construct_end();
95 OSSL_SELF_TEST
*OSSL_SELF_TEST_new(OSSL_CALLBACK
*cb
, void *cbarg
)
97 OSSL_SELF_TEST
*ret
= OPENSSL_zalloc(sizeof(*ret
));
107 self_test_setparams(ret
);
111 void OSSL_SELF_TEST_free(OSSL_SELF_TEST
*st
)
116 /* Can be used during application testing to log that a test has started. */
117 void OSSL_SELF_TEST_onbegin(OSSL_SELF_TEST
*st
, const char *type
,
120 if (st
!= NULL
&& st
->cb
!= NULL
) {
121 st
->phase
= OSSL_SELF_TEST_PHASE_START
;
124 self_test_setparams(st
);
125 (void)st
->cb(st
->params
, st
->cb_arg
);
130 * Can be used during application testing to log that a test has either
133 void OSSL_SELF_TEST_onend(OSSL_SELF_TEST
*st
, int ret
)
135 if (st
!= NULL
&& st
->cb
!= NULL
) {
137 (ret
== 1 ? OSSL_SELF_TEST_PHASE_PASS
: OSSL_SELF_TEST_PHASE_FAIL
);
138 self_test_setparams(st
);
139 (void)st
->cb(st
->params
, st
->cb_arg
);
141 st
->phase
= OSSL_SELF_TEST_PHASE_NONE
;
142 st
->type
= OSSL_SELF_TEST_TYPE_NONE
;
143 st
->desc
= OSSL_SELF_TEST_DESC_NONE
;
148 * Used for failure testing.
150 * Call the applications SELF_TEST_cb() if it exists.
151 * If the application callback decides to return 0 then the first byte of 'bytes'
152 * is modified (corrupted). This is used to modify output signatures or
153 * ciphertext before they are verified or decrypted.
155 int OSSL_SELF_TEST_oncorrupt_byte(OSSL_SELF_TEST
*st
, unsigned char *bytes
)
157 if (st
!= NULL
&& st
->cb
!= NULL
) {
158 st
->phase
= OSSL_SELF_TEST_PHASE_CORRUPT
;
159 self_test_setparams(st
);
160 if (!st
->cb(st
->params
, st
->cb_arg
)) {