2
0

OSSL_SELF_TEST_new.pod 5.2 KB

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