self_test_core.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright 2019-2022 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. #include <openssl/self_test.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/params.h>
  12. #include "internal/cryptlib.h"
  13. #include "crypto/context.h"
  14. typedef struct self_test_cb_st
  15. {
  16. OSSL_CALLBACK *cb;
  17. void *cbarg;
  18. } SELF_TEST_CB;
  19. struct ossl_self_test_st
  20. {
  21. /* local state variables */
  22. const char *phase;
  23. const char *type;
  24. const char *desc;
  25. OSSL_CALLBACK *cb;
  26. /* callback related variables used to pass the state back to the user */
  27. OSSL_PARAM params[4];
  28. void *cb_arg;
  29. };
  30. #ifndef FIPS_MODULE
  31. void *ossl_self_test_set_callback_new(OSSL_LIB_CTX *ctx)
  32. {
  33. SELF_TEST_CB *stcb;
  34. stcb = OPENSSL_zalloc(sizeof(*stcb));
  35. return stcb;
  36. }
  37. void ossl_self_test_set_callback_free(void *stcb)
  38. {
  39. OPENSSL_free(stcb);
  40. }
  41. static SELF_TEST_CB *get_self_test_callback(OSSL_LIB_CTX *libctx)
  42. {
  43. return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_SELF_TEST_CB_INDEX);
  44. }
  45. void OSSL_SELF_TEST_set_callback(OSSL_LIB_CTX *libctx, OSSL_CALLBACK *cb,
  46. void *cbarg)
  47. {
  48. SELF_TEST_CB *stcb = get_self_test_callback(libctx);
  49. if (stcb != NULL) {
  50. stcb->cb = cb;
  51. stcb->cbarg = cbarg;
  52. }
  53. }
  54. void OSSL_SELF_TEST_get_callback(OSSL_LIB_CTX *libctx, OSSL_CALLBACK **cb,
  55. void **cbarg)
  56. {
  57. SELF_TEST_CB *stcb = get_self_test_callback(libctx);
  58. if (cb != NULL)
  59. *cb = (stcb != NULL ? stcb->cb : NULL);
  60. if (cbarg != NULL)
  61. *cbarg = (stcb != NULL ? stcb->cbarg : NULL);
  62. }
  63. #endif /* FIPS_MODULE */
  64. static void self_test_setparams(OSSL_SELF_TEST *st)
  65. {
  66. size_t n = 0;
  67. if (st->cb != NULL) {
  68. st->params[n++] =
  69. OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_SELF_TEST_PHASE,
  70. (char *)st->phase, 0);
  71. st->params[n++] =
  72. OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_SELF_TEST_TYPE,
  73. (char *)st->type, 0);
  74. st->params[n++] =
  75. OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_SELF_TEST_DESC,
  76. (char *)st->desc, 0);
  77. }
  78. st->params[n++] = OSSL_PARAM_construct_end();
  79. }
  80. OSSL_SELF_TEST *OSSL_SELF_TEST_new(OSSL_CALLBACK *cb, void *cbarg)
  81. {
  82. OSSL_SELF_TEST *ret = OPENSSL_zalloc(sizeof(*ret));
  83. if (ret == NULL)
  84. return NULL;
  85. ret->cb = cb;
  86. ret->cb_arg = cbarg;
  87. ret->phase = "";
  88. ret->type = "";
  89. ret->desc = "";
  90. self_test_setparams(ret);
  91. return ret;
  92. }
  93. void OSSL_SELF_TEST_free(OSSL_SELF_TEST *st)
  94. {
  95. OPENSSL_free(st);
  96. }
  97. /* Can be used during application testing to log that a test has started. */
  98. void OSSL_SELF_TEST_onbegin(OSSL_SELF_TEST *st, const char *type,
  99. const char *desc)
  100. {
  101. if (st != NULL && st->cb != NULL) {
  102. st->phase = OSSL_SELF_TEST_PHASE_START;
  103. st->type = type;
  104. st->desc = desc;
  105. self_test_setparams(st);
  106. (void)st->cb(st->params, st->cb_arg);
  107. }
  108. }
  109. /*
  110. * Can be used during application testing to log that a test has either
  111. * passed or failed.
  112. */
  113. void OSSL_SELF_TEST_onend(OSSL_SELF_TEST *st, int ret)
  114. {
  115. if (st != NULL && st->cb != NULL) {
  116. st->phase =
  117. (ret == 1 ? OSSL_SELF_TEST_PHASE_PASS : OSSL_SELF_TEST_PHASE_FAIL);
  118. self_test_setparams(st);
  119. (void)st->cb(st->params, st->cb_arg);
  120. st->phase = OSSL_SELF_TEST_PHASE_NONE;
  121. st->type = OSSL_SELF_TEST_TYPE_NONE;
  122. st->desc = OSSL_SELF_TEST_DESC_NONE;
  123. }
  124. }
  125. /*
  126. * Used for failure testing.
  127. *
  128. * Call the applications SELF_TEST_cb() if it exists.
  129. * If the application callback decides to return 0 then the first byte of 'bytes'
  130. * is modified (corrupted). This is used to modify output signatures or
  131. * ciphertext before they are verified or decrypted.
  132. */
  133. int OSSL_SELF_TEST_oncorrupt_byte(OSSL_SELF_TEST *st, unsigned char *bytes)
  134. {
  135. if (st != NULL && st->cb != NULL) {
  136. st->phase = OSSL_SELF_TEST_PHASE_CORRUPT;
  137. self_test_setparams(st);
  138. if (!st->cb(st->params, st->cb_arg)) {
  139. bytes[0] ^= 1;
  140. return 1;
  141. }
  142. }
  143. return 0;
  144. }