xref: /freebsd/crypto/openssl/doc/man3/OSSL_SELF_TEST_new.pod (revision a7148ab39c03abd4d1a84997c70bf96f15dd2a09)
1b077aed3SPierre Pronchery=pod
2b077aed3SPierre Pronchery
3b077aed3SPierre Pronchery=head1 NAME
4b077aed3SPierre Pronchery
5b077aed3SPierre ProncheryOSSL_SELF_TEST_new,
6b077aed3SPierre ProncheryOSSL_SELF_TEST_free,
7b077aed3SPierre ProncheryOSSL_SELF_TEST_onbegin,
8b077aed3SPierre ProncheryOSSL_SELF_TEST_oncorrupt_byte,
9b077aed3SPierre ProncheryOSSL_SELF_TEST_onend - functionality to trigger a callback during a self test
10b077aed3SPierre Pronchery
11b077aed3SPierre Pronchery=head1 SYNOPSIS
12b077aed3SPierre Pronchery
13b077aed3SPierre Pronchery #include <openssl/self_test.h>
14b077aed3SPierre Pronchery
15b077aed3SPierre Pronchery OSSL_SELF_TEST *OSSL_SELF_TEST_new(OSSL_CALLBACK *cb, void *cbarg);
16b077aed3SPierre Pronchery void OSSL_SELF_TEST_free(OSSL_SELF_TEST *st);
17b077aed3SPierre Pronchery
18b077aed3SPierre Pronchery void OSSL_SELF_TEST_onbegin(OSSL_SELF_TEST *st, const char *type,
19b077aed3SPierre Pronchery                             const char *desc);
20b077aed3SPierre Pronchery int OSSL_SELF_TEST_oncorrupt_byte(OSSL_SELF_TEST *st, unsigned char *bytes);
21b077aed3SPierre Pronchery void OSSL_SELF_TEST_onend(OSSL_SELF_TEST *st, int ret);
22b077aed3SPierre Pronchery
23b077aed3SPierre Pronchery=head1 DESCRIPTION
24b077aed3SPierre Pronchery
25aa795734SPierre ProncheryThese methods are intended for use by provider implementers, to display
26b077aed3SPierre Proncherydiagnostic information during self testing.
27b077aed3SPierre Pronchery
28b077aed3SPierre ProncheryOSSL_SELF_TEST_new() allocates an opaque B<OSSL_SELF_TEST> object that has a
29b077aed3SPierre Proncherycallback and callback argument associated with it.
30b077aed3SPierre Pronchery
31b077aed3SPierre ProncheryThe callback I<cb> may be triggered multiple times by a self test to indicate
32b077aed3SPierre Proncherydifferent phases.
33b077aed3SPierre Pronchery
34b077aed3SPierre ProncheryOSSL_SELF_TEST_free() frees the space allocated by OSSL_SELF_TEST_new().
35*a7148ab3SEnji CooperIf the argument is NULL, nothing is done.
36b077aed3SPierre Pronchery
37b077aed3SPierre ProncheryOSSL_SELF_TEST_onbegin() may be inserted at the start of a block of self test
38b077aed3SPierre Proncherycode. It can be used for diagnostic purposes.
39b077aed3SPierre ProncheryIf this method is called the callback I<cb> will receive the following
40b077aed3SPierre ProncheryL<OSSL_PARAM(3)> object.
41b077aed3SPierre Pronchery
42b077aed3SPierre Pronchery=over 4
43b077aed3SPierre Pronchery
44b077aed3SPierre Pronchery=item "st-phase" (B<OSSL_PROV_PARAM_SELF_TEST_PHASE>) <UTF8 string>
45b077aed3SPierre Pronchery
46b077aed3SPierre ProncheryThe value is the string "Start"
47b077aed3SPierre Pronchery
48b077aed3SPierre Pronchery=back
49b077aed3SPierre Pronchery
50b077aed3SPierre ProncheryOSSL_SELF_TEST_oncorrupt_byte() may be inserted just after the known answer is
51b077aed3SPierre Proncherycalculated, but before the self test compares the result. The first byte in the
52b077aed3SPierre Proncherypassed in array of I<bytes> will be corrupted if the callback returns 0,
53b077aed3SPierre Proncheryotherwise it leaves the array unaltered. It can be used for failure testing.
54b077aed3SPierre ProncheryThe I<type> and I<desc> can be used to identify an individual self test to
55b077aed3SPierre Proncherytarget for failure testing.
56b077aed3SPierre ProncheryIf this method is called the callback I<cb> will receive the following
57b077aed3SPierre ProncheryL<OSSL_PARAM(3)> object.
58b077aed3SPierre Pronchery
59b077aed3SPierre Pronchery=over 4
60b077aed3SPierre Pronchery
61b077aed3SPierre Pronchery=item "st-phase" (B<OSSL_PROV_PARAM_SELF_TEST_PHASE>) <UTF8 string>
62b077aed3SPierre Pronchery
63b077aed3SPierre ProncheryThe value is the string "Corrupt"
64b077aed3SPierre Pronchery
65b077aed3SPierre Pronchery=back
66b077aed3SPierre Pronchery
67b077aed3SPierre ProncheryOSSL_SELF_TEST_onend() may be inserted at the end of a block of self test code
68b077aed3SPierre Proncheryjust before cleanup to indicate if the test passed or failed. It can be used for
69b077aed3SPierre Proncherydiagnostic purposes.
70b077aed3SPierre ProncheryIf this method is called the callback I<cb> will receive the following
71b077aed3SPierre ProncheryL<OSSL_PARAM(3)> object.
72b077aed3SPierre Pronchery
73b077aed3SPierre Pronchery=over 4
74b077aed3SPierre Pronchery
75b077aed3SPierre Pronchery=item "st-phase" (B<OSSL_PROV_PARAM_SELF_TEST_PHASE>) <UTF8 string>
76b077aed3SPierre Pronchery
77b077aed3SPierre ProncheryThe value of the string is "Pass" if I<ret> is non zero, otherwise it has the
78b077aed3SPierre Proncheryvalue "Fail".
79b077aed3SPierre Pronchery
80b077aed3SPierre Pronchery=back
81b077aed3SPierre Pronchery
82b077aed3SPierre ProncheryAfter the callback I<cb> has been called the values that were set by
83b077aed3SPierre ProncheryOSSL_SELF_TEST_onbegin() for I<type> and I<desc> are set to the value "None".
84b077aed3SPierre Pronchery
85b077aed3SPierre ProncheryIf OSSL_SELF_TEST_onbegin(), OSSL_SELF_TEST_oncorrupt_byte() or
86b077aed3SPierre ProncheryOSSL_SELF_TEST_onend() is called the following additional L<OSSL_PARAM(3)> are
87b077aed3SPierre Proncherypassed to the callback.
88b077aed3SPierre Pronchery
89b077aed3SPierre Pronchery=over 4
90b077aed3SPierre Pronchery
91b077aed3SPierre Pronchery=item "st-type" (B<OSSL_PROV_PARAM_SELF_TEST_TYPE>) <UTF8 string>
92b077aed3SPierre Pronchery
93b077aed3SPierre ProncheryThe value is setup by the I<type> passed to OSSL_SELF_TEST_onbegin().
94b077aed3SPierre ProncheryThis allows the callback to identify the type of test being run.
95b077aed3SPierre Pronchery
96b077aed3SPierre Pronchery=item "st-desc" (B<OSSL_PROV_PARAM_SELF_TEST_DESC>) <UTF8 string>
97b077aed3SPierre Pronchery
98b077aed3SPierre ProncheryThe value is setup by the I<type> passed to OSSL_SELF_TEST_onbegin().
99b077aed3SPierre ProncheryThis allows the callback to identify the sub category of the test being run.
100b077aed3SPierre Pronchery
101b077aed3SPierre Pronchery=back
102b077aed3SPierre Pronchery
103b077aed3SPierre Pronchery=head1 RETURN VALUES
104b077aed3SPierre Pronchery
105b077aed3SPierre ProncheryOSSL_SELF_TEST_new() returns the allocated B<OSSL_SELF_TEST> object, or NULL if
106b077aed3SPierre Proncheryit fails.
107b077aed3SPierre Pronchery
108b077aed3SPierre ProncheryOSSL_SELF_TEST_oncorrupt_byte() returns 1 if corruption occurs, otherwise it
109b077aed3SPierre Proncheryreturns 0.
110b077aed3SPierre Pronchery
111b077aed3SPierre Pronchery=head1 EXAMPLES
112b077aed3SPierre Pronchery
113b077aed3SPierre ProncheryA single self test could be set up in the following way:
114b077aed3SPierre Pronchery
115b077aed3SPierre Pronchery    OSSL_SELF_TEST *st = NULL;
116b077aed3SPierre Pronchery    OSSL_CALLBACK *cb;
117b077aed3SPierre Pronchery    void *cbarg;
118b077aed3SPierre Pronchery    int ok = 0;
119b077aed3SPierre Pronchery    unsigned char out[EVP_MAX_MD_SIZE];
120b077aed3SPierre Pronchery    unsigned int out_len = 0;
121b077aed3SPierre Pronchery    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
122b077aed3SPierre Pronchery    EVP_MD *md = EVP_MD_fetch(libctx, t->algorithm, NULL);
123b077aed3SPierre Pronchery
124b077aed3SPierre Pronchery    /*
125b077aed3SPierre Pronchery     * Retrieve the callback - will be NULL if not set by the application via
126b077aed3SPierre Pronchery     * OSSL_SELF_TEST_set_callback().
127b077aed3SPierre Pronchery     */
128b077aed3SPierre Pronchery    OSSL_SELF_TEST_get_callback(libctx, &cb, &cbarg);
129b077aed3SPierre Pronchery
130b077aed3SPierre Pronchery    st = OSSL_SELF_TEST_new(cb, cb_arg);
131b077aed3SPierre Pronchery
132b077aed3SPierre Pronchery    /* Trigger the optional callback */
133b077aed3SPierre Pronchery    OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_DIGEST,
134b077aed3SPierre Pronchery                           OSSL_SELF_TEST_DESC_MD_SHA2);
135b077aed3SPierre Pronchery
136b077aed3SPierre Pronchery    if (!EVP_DigestInit_ex(ctx, md, NULL)
137b077aed3SPierre Pronchery        || !EVP_DigestUpdate(ctx, pt, pt_len)
138b077aed3SPierre Pronchery        || !EVP_DigestFinal(ctx, out, &out_len))
139b077aed3SPierre Pronchery        goto err;
140b077aed3SPierre Pronchery
141b077aed3SPierre Pronchery    /* Optional corruption - If the application callback returns 0 */
142b077aed3SPierre Pronchery    OSSL_SELF_TEST_oncorrupt_byte(st, out);
143b077aed3SPierre Pronchery
144b077aed3SPierre Pronchery    if (out_len != t->expected_len
145b077aed3SPierre Pronchery        || memcmp(out, t->expected, out_len) != 0)
146b077aed3SPierre Pronchery        goto err;
147b077aed3SPierre Pronchery    ok = 1;
148b077aed3SPierre Pronchery  err:
149b077aed3SPierre Pronchery    OSSL_SELF_TEST_onend(st, ok);
150b077aed3SPierre Pronchery    EVP_MD_free(md);
151b077aed3SPierre Pronchery    EVP_MD_CTX_free(ctx);
152b077aed3SPierre Pronchery
153b077aed3SPierre ProncheryMultiple self test's can be set up in a similar way by repeating the pattern of
154b077aed3SPierre ProncheryOSSL_SELF_TEST_onbegin(), OSSL_SELF_TEST_oncorrupt_byte(), OSSL_SELF_TEST_onend()
155b077aed3SPierre Proncheryfor each test.
156b077aed3SPierre Pronchery
157b077aed3SPierre Pronchery=head1 SEE ALSO
158b077aed3SPierre Pronchery
159b077aed3SPierre ProncheryL<OSSL_SELF_TEST_set_callback(3)>,
160b077aed3SPierre ProncheryL<openssl-core.h(7)>,
161b077aed3SPierre ProncheryL<OSSL_PROVIDER-FIPS(7)>
162b077aed3SPierre Pronchery
163b077aed3SPierre Pronchery=head1 HISTORY
164b077aed3SPierre Pronchery
165b077aed3SPierre ProncheryThe functions described here were added in OpenSSL 3.0.
166b077aed3SPierre Pronchery
167b077aed3SPierre Pronchery=head1 COPYRIGHT
168b077aed3SPierre Pronchery
169*a7148ab3SEnji CooperCopyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.
170b077aed3SPierre Pronchery
171b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
172b077aed3SPierre Proncherythis file except in compliance with the License.  You can obtain a copy
173b077aed3SPierre Proncheryin the file LICENSE in the source distribution or at
174b077aed3SPierre ProncheryL<https://www.openssl.org/source/license.html>.
175b077aed3SPierre Pronchery
176b077aed3SPierre Pronchery=cut
177