OSSL_SELF_TEST_new.pod 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. =pod
  2. =head1 NAME
  3. OSSL_SELF_TEST_new,
  4. OSSL_SELF_TEST_free,
  5. OSSL_SELF_TEST_onbegin,
  6. OSSL_SELF_TEST_oncorrupt_byte,
  7. OSSL_SELF_TEST_onend - functionality to trigger a callback during a self test
  8. =head1 SYNOPSIS
  9. #include <openssl/self_test.h>
  10. OSSL_SELF_TEST *OSSL_SELF_TEST_new(OSSL_CALLBACK *cb, void *cbarg);
  11. void OSSL_SELF_TEST_free(OSSL_SELF_TEST *st);
  12. void OSSL_SELF_TEST_onbegin(OSSL_SELF_TEST *st, const char *type,
  13. const char *desc);
  14. int OSSL_SELF_TEST_oncorrupt_byte(OSSL_SELF_TEST *st, unsigned char *bytes);
  15. void OSSL_SELF_TEST_onend(OSSL_SELF_TEST *st, int ret);
  16. =head1 DESCRIPTION
  17. These methods are intended for use by provider implementers, to display
  18. diagnostic information during self testing.
  19. OSSL_SELF_TEST_new() allocates an opaque B<OSSL_SELF_TEST> object that has a
  20. callback and callback argument associated with it.
  21. The callback I<cb> may be triggered multiple times by a self test to indicate
  22. different phases.
  23. OSSL_SELF_TEST_free() frees the space allocated by OSSL_SELF_TEST_new().
  24. If the argument is NULL, nothing is done.
  25. OSSL_SELF_TEST_onbegin() may be inserted at the start of a block of self test
  26. code. It can be used for diagnostic purposes.
  27. If this method is called the callback I<cb> will receive the following
  28. L<OSSL_PARAM(3)> object.
  29. =over 4
  30. =item "st-phase" (B<OSSL_PROV_PARAM_SELF_TEST_PHASE>) <UTF8 string>
  31. The value is the string "Start"
  32. =back
  33. OSSL_SELF_TEST_oncorrupt_byte() may be inserted just after the known answer is
  34. calculated, but before the self test compares the result. The first byte in the
  35. passed in array of I<bytes> will be corrupted if the callback returns 0,
  36. otherwise it leaves the array unaltered. It can be used for failure testing.
  37. The I<type> and I<desc> can be used to identify an individual self test to
  38. target for failure testing.
  39. If this method is called the callback I<cb> will receive the following
  40. L<OSSL_PARAM(3)> object.
  41. =over 4
  42. =item "st-phase" (B<OSSL_PROV_PARAM_SELF_TEST_PHASE>) <UTF8 string>
  43. The value is the string "Corrupt"
  44. =back
  45. OSSL_SELF_TEST_onend() may be inserted at the end of a block of self test code
  46. just before cleanup to indicate if the test passed or failed. It can be used for
  47. diagnostic purposes.
  48. If this method is called the callback I<cb> will receive the following
  49. L<OSSL_PARAM(3)> object.
  50. =over 4
  51. =item "st-phase" (B<OSSL_PROV_PARAM_SELF_TEST_PHASE>) <UTF8 string>
  52. The value of the string is "Pass" if I<ret> is non zero, otherwise it has the
  53. value "Fail".
  54. =back
  55. After the callback I<cb> has been called the values that were set by
  56. OSSL_SELF_TEST_onbegin() for I<type> and I<desc> are set to the value "None".
  57. If OSSL_SELF_TEST_onbegin(), OSSL_SELF_TEST_oncorrupt_byte() or
  58. OSSL_SELF_TEST_onend() is called the following additional L<OSSL_PARAM(3)> are
  59. passed to the callback.
  60. =over 4
  61. =item "st-type" (B<OSSL_PROV_PARAM_SELF_TEST_TYPE>) <UTF8 string>
  62. The value is setup by the I<type> passed to OSSL_SELF_TEST_onbegin().
  63. This allows the callback to identify the type of test being run.
  64. =item "st-desc" (B<OSSL_PROV_PARAM_SELF_TEST_DESC>) <UTF8 string>
  65. The value is setup by the I<type> passed to OSSL_SELF_TEST_onbegin().
  66. This allows the callback to identify the sub category of the test being run.
  67. =back
  68. =head1 RETURN VALUES
  69. OSSL_SELF_TEST_new() returns the allocated B<OSSL_SELF_TEST> object, or NULL if
  70. it fails.
  71. OSSL_SELF_TEST_oncorrupt_byte() returns 1 if corruption occurs, otherwise it
  72. returns 0.
  73. =head1 EXAMPLES
  74. A single self test could be set up in the following way:
  75. OSSL_SELF_TEST *st = NULL;
  76. OSSL_CALLBACK *cb;
  77. void *cbarg;
  78. int ok = 0;
  79. unsigned char out[EVP_MAX_MD_SIZE];
  80. unsigned int out_len = 0;
  81. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  82. EVP_MD *md = EVP_MD_fetch(libctx, t->algorithm, NULL);
  83. /*
  84. * Retrieve the callback - will be NULL if not set by the application via
  85. * OSSL_SELF_TEST_set_callback().
  86. */
  87. OSSL_SELF_TEST_get_callback(libctx, &cb, &cbarg);
  88. st = OSSL_SELF_TEST_new(cb, cb_arg);
  89. /* Trigger the optional callback */
  90. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_DIGEST,
  91. OSSL_SELF_TEST_DESC_MD_SHA2);
  92. if (!EVP_DigestInit_ex(ctx, md, NULL)
  93. || !EVP_DigestUpdate(ctx, pt, pt_len)
  94. || !EVP_DigestFinal(ctx, out, &out_len))
  95. goto err;
  96. /* Optional corruption - If the application callback returns 0 */
  97. OSSL_SELF_TEST_oncorrupt_byte(st, out);
  98. if (out_len != t->expected_len
  99. || memcmp(out, t->expected, out_len) != 0)
  100. goto err;
  101. ok = 1;
  102. err:
  103. OSSL_SELF_TEST_onend(st, ok);
  104. EVP_MD_free(md);
  105. EVP_MD_CTX_free(ctx);
  106. Multiple self test's can be set up in a similar way by repeating the pattern of
  107. OSSL_SELF_TEST_onbegin(), OSSL_SELF_TEST_oncorrupt_byte(), OSSL_SELF_TEST_onend()
  108. for each test.
  109. =head1 SEE ALSO
  110. L<OSSL_SELF_TEST_set_callback(3)>,
  111. L<openssl-core.h(7)>,
  112. L<OSSL_PROVIDER-FIPS(7)>
  113. =head1 HISTORY
  114. The functions described here were added in OpenSSL 3.0.
  115. =head1 COPYRIGHT
  116. Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.
  117. Licensed under the Apache License 2.0 (the "License"). You may not use
  118. this file except in compliance with the License. You can obtain a copy
  119. in the file LICENSE in the source distribution or at
  120. L<https://www.openssl.org/source/license.html>.
  121. =cut