async_tls.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* async-tls.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL. (formerly known as CyaSSL)
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #ifndef WOLFSSL_USER_SETTINGS
  22. #include <wolfssl/options.h>
  23. #endif
  24. #include <wolfssl/ssl.h>
  25. #include <wolfssl/wolfio.h>
  26. #include <wolfssl/wolfcrypt/error-crypt.h>
  27. #include "examples/async/async_tls.h"
  28. /* ---------------------------------------------------------------------------*/
  29. /* --- Example Crypto Callback --- */
  30. /* ---------------------------------------------------------------------------*/
  31. #ifdef WOLF_CRYPTO_CB
  32. /* Example custom context for crypto callback */
  33. #ifndef TEST_PEND_COUNT
  34. #define TEST_PEND_COUNT 2
  35. #endif
  36. /* Example crypto dev callback function that calls software version */
  37. /* This is where you would plug-in calls to your own hardware crypto */
  38. int AsyncTlsCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  39. {
  40. int ret = CRYPTOCB_UNAVAILABLE; /* return this to bypass HW and use SW */
  41. AsyncTlsCryptoCbCtx* myCtx = (AsyncTlsCryptoCbCtx*)ctx;
  42. if (info == NULL)
  43. return BAD_FUNC_ARG;
  44. #ifdef DEBUG_CRYPTOCB
  45. wc_CryptoCb_InfoString(info);
  46. #endif
  47. if (info->algo_type == WC_ALGO_TYPE_PK) {
  48. #ifdef WOLFSSL_ASYNC_CRYPT
  49. /* Test pending response */
  50. if (info->pk.type == WC_PK_TYPE_RSA ||
  51. info->pk.type == WC_PK_TYPE_EC_KEYGEN ||
  52. info->pk.type == WC_PK_TYPE_ECDSA_SIGN ||
  53. info->pk.type == WC_PK_TYPE_ECDSA_VERIFY ||
  54. info->pk.type == WC_PK_TYPE_ECDH)
  55. {
  56. if (myCtx->pendingCount++ < TEST_PEND_COUNT) return WC_PENDING_E;
  57. myCtx->pendingCount = 0;
  58. }
  59. #endif
  60. #ifndef NO_RSA
  61. if (info->pk.type == WC_PK_TYPE_RSA) {
  62. /* set devId to invalid, so software is used */
  63. info->pk.rsa.key->devId = INVALID_DEVID;
  64. switch (info->pk.rsa.type) {
  65. case RSA_PUBLIC_ENCRYPT:
  66. case RSA_PUBLIC_DECRYPT:
  67. /* perform software based RSA public op */
  68. ret = wc_RsaFunction(
  69. info->pk.rsa.in, info->pk.rsa.inLen,
  70. info->pk.rsa.out, info->pk.rsa.outLen,
  71. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  72. break;
  73. case RSA_PRIVATE_ENCRYPT:
  74. case RSA_PRIVATE_DECRYPT:
  75. /* perform software based RSA private op */
  76. ret = wc_RsaFunction(
  77. info->pk.rsa.in, info->pk.rsa.inLen,
  78. info->pk.rsa.out, info->pk.rsa.outLen,
  79. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  80. break;
  81. }
  82. /* reset devId */
  83. info->pk.rsa.key->devId = devIdArg;
  84. }
  85. #endif
  86. #ifdef HAVE_ECC
  87. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  88. /* set devId to invalid, so software is used */
  89. info->pk.eckg.key->devId = INVALID_DEVID;
  90. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  91. info->pk.eckg.key, info->pk.eckg.curveId);
  92. /* reset devId */
  93. info->pk.eckg.key->devId = devIdArg;
  94. }
  95. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  96. /* set devId to invalid, so software is used */
  97. info->pk.eccsign.key->devId = INVALID_DEVID;
  98. ret = wc_ecc_sign_hash(
  99. info->pk.eccsign.in, info->pk.eccsign.inlen,
  100. info->pk.eccsign.out, info->pk.eccsign.outlen,
  101. info->pk.eccsign.rng, info->pk.eccsign.key);
  102. /* reset devId */
  103. info->pk.eccsign.key->devId = devIdArg;
  104. }
  105. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  106. /* set devId to invalid, so software is used */
  107. info->pk.eccverify.key->devId = INVALID_DEVID;
  108. ret = wc_ecc_verify_hash(
  109. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  110. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  111. info->pk.eccverify.res, info->pk.eccverify.key);
  112. /* reset devId */
  113. info->pk.eccverify.key->devId = devIdArg;
  114. }
  115. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  116. /* set devId to invalid, so software is used */
  117. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  118. ret = wc_ecc_shared_secret(
  119. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  120. info->pk.ecdh.out, info->pk.ecdh.outlen);
  121. /* reset devId */
  122. info->pk.ecdh.private_key->devId = devIdArg;
  123. }
  124. #endif /* HAVE_ECC */
  125. }
  126. (void)devIdArg;
  127. (void)myCtx;
  128. return ret;
  129. }
  130. #endif /* WOLF_CRYPTO_CB */
  131. /* ---------------------------------------------------------------------------*/
  132. /* --- Example PK (Public Key) Callback --- */
  133. /* ---------------------------------------------------------------------------*/
  134. #ifdef HAVE_PK_CALLBACKS
  135. #endif