self_test_core.c 4.6 KB

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