]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/self_test_core.c
bee3161ed5e8054927032eee902b333805791548
[thirdparty/openssl.git] / crypto / self_test_core.c
1 /*
2 * Copyright 2019-2020 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 <openssl/core_names.h>
12 #include <openssl/params.h>
13 #include "internal/cryptlib.h"
14
15 typedef struct self_test_cb_st
16 {
17 OSSL_CALLBACK *cb;
18 void *cbarg;
19 } SELF_TEST_CB;
20
21 struct ossl_self_test_st
22 {
23 /* local state variables */
24 const char *phase;
25 const char *type;
26 const char *desc;
27 OSSL_CALLBACK *cb;
28
29 /* callback related variables used to pass the state back to the user */
30 OSSL_PARAM params[4];
31 void *cb_arg;
32 };
33
34 static void *self_test_set_callback_new(OPENSSL_CTX *ctx)
35 {
36 SELF_TEST_CB *stcb;
37
38 stcb = OPENSSL_zalloc(sizeof(*stcb));
39 return stcb;
40 }
41
42 static void self_test_set_callback_free(void *stcb)
43 {
44 OPENSSL_free(stcb);
45 }
46
47 static const OPENSSL_CTX_METHOD self_test_set_callback_method = {
48 self_test_set_callback_new,
49 self_test_set_callback_free,
50 };
51
52 static SELF_TEST_CB *get_self_test_callback(OPENSSL_CTX *libctx)
53 {
54 return openssl_ctx_get_data(libctx, OPENSSL_CTX_SELF_TEST_CB_INDEX,
55 &self_test_set_callback_method);
56 }
57
58 #ifndef FIPS_MODE
59 void OSSL_SELF_TEST_set_callback(OPENSSL_CTX *libctx, OSSL_CALLBACK *cb,
60 void *cbarg)
61 {
62 SELF_TEST_CB *stcb = get_self_test_callback(libctx);
63
64 if (stcb != NULL) {
65 stcb->cb = cb;
66 stcb->cbarg = cbarg;
67 }
68 }
69 #endif /* FIPS_MODE */
70
71 void OSSL_SELF_TEST_get_callback(OPENSSL_CTX *libctx, OSSL_CALLBACK **cb,
72 void **cbarg)
73 {
74 SELF_TEST_CB *stcb = get_self_test_callback(libctx);
75
76 if (cb != NULL)
77 *cb = (stcb != NULL ? stcb->cb : NULL);
78 if (cbarg != NULL)
79 *cbarg = (stcb != NULL ? stcb->cbarg : NULL);
80 }
81
82 static void self_test_setparams(OSSL_SELF_TEST *st)
83 {
84 size_t n = 0;
85
86 if (st->cb != NULL) {
87 st->params[n++] =
88 OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_SELF_TEST_PHASE,
89 (char *)st->phase, 0);
90 st->params[n++] =
91 OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_SELF_TEST_TYPE,
92 (char *)st->type, 0);
93 st->params[n++] =
94 OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_SELF_TEST_DESC,
95 (char *)st->desc, 0);
96 }
97 st->params[n++] = OSSL_PARAM_construct_end();
98 }
99
100 OSSL_SELF_TEST *OSSL_SELF_TEST_new(OSSL_CALLBACK *cb, void *cbarg)
101 {
102 OSSL_SELF_TEST *ret = OPENSSL_zalloc(sizeof(*ret));
103
104 if (ret == NULL)
105 return NULL;
106
107 ret->cb = cb;
108 ret->cb_arg = cbarg;
109 ret->phase = "";
110 ret->type = "";
111 ret->desc = "";
112 self_test_setparams(ret);
113 return ret;
114 }
115
116 void OSSL_SELF_TEST_free(OSSL_SELF_TEST *st)
117 {
118 OPENSSL_free(st);
119 }
120
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,
123 const char *desc)
124 {
125 if (st != NULL && st->cb != NULL) {
126 st->phase = OSSL_SELF_TEST_PHASE_START;
127 st->type = type;
128 st->desc = desc;
129 self_test_setparams(st);
130 (void)st->cb(st->params, st->cb_arg);
131 }
132 }
133
134 /*
135 * Can be used during application testing to log that a test has either
136 * passed or failed.
137 */
138 void OSSL_SELF_TEST_onend(OSSL_SELF_TEST *st, int ret)
139 {
140 if (st != NULL && st->cb != NULL) {
141 st->phase =
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);
145
146 st->phase = OSSL_SELF_TEST_PHASE_NONE;
147 st->type = OSSL_SELF_TEST_TYPE_NONE;
148 st->desc = OSSL_SELF_TEST_DESC_NONE;
149 }
150 }
151
152 /*
153 * Used for failure testing.
154 *
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.
159 */
160 void OSSL_SELF_TEST_oncorrupt_byte(OSSL_SELF_TEST *st, unsigned char *bytes)
161 {
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))
166 bytes[0] ^= 1;
167 }
168 }