self_test_core.c 4.6 KB

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