2
0

async_tls.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* async-tls.c
  2. *
  3. * Copyright (C) 2006-2024 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  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-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #ifndef WOLFSSL_USER_SETTINGS
  25. #include <wolfssl/options.h>
  26. #endif
  27. #include <wolfssl/ssl.h>
  28. #include <wolfssl/wolfio.h>
  29. #include <wolfssl/wolfcrypt/error-crypt.h>
  30. #include "examples/async/async_tls.h"
  31. /* ---------------------------------------------------------------------------*/
  32. /* --- Example Crypto Callback --- */
  33. /* ---------------------------------------------------------------------------*/
  34. #ifdef WOLF_CRYPTO_CB
  35. /* Example custom context for crypto callback */
  36. #ifndef TEST_PEND_COUNT
  37. #define TEST_PEND_COUNT 2
  38. #endif
  39. /* Example crypto dev callback function that calls software version */
  40. /* This is where you would plug-in calls to your own hardware crypto */
  41. int AsyncTlsCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  42. {
  43. int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); /* bypass HW by default */
  44. AsyncTlsCryptoCbCtx* myCtx = (AsyncTlsCryptoCbCtx*)ctx;
  45. if (info == NULL)
  46. return BAD_FUNC_ARG;
  47. #ifdef DEBUG_CRYPTOCB
  48. wc_CryptoCb_InfoString(info);
  49. #endif
  50. if (info->algo_type == WC_ALGO_TYPE_PK) {
  51. #ifdef WOLFSSL_ASYNC_CRYPT
  52. /* Test pending response */
  53. if (info->pk.type == WC_PK_TYPE_RSA ||
  54. info->pk.type == WC_PK_TYPE_EC_KEYGEN ||
  55. info->pk.type == WC_PK_TYPE_ECDSA_SIGN ||
  56. info->pk.type == WC_PK_TYPE_ECDSA_VERIFY ||
  57. info->pk.type == WC_PK_TYPE_ECDH)
  58. {
  59. if (myCtx->pendingCount++ < TEST_PEND_COUNT) return WC_PENDING_E;
  60. myCtx->pendingCount = 0;
  61. }
  62. #endif
  63. #ifndef NO_RSA
  64. if (info->pk.type == WC_PK_TYPE_RSA) {
  65. /* set devId to invalid, so software is used */
  66. info->pk.rsa.key->devId = INVALID_DEVID;
  67. switch (info->pk.rsa.type) {
  68. case RSA_PUBLIC_ENCRYPT:
  69. case RSA_PUBLIC_DECRYPT:
  70. /* perform software based RSA public op */
  71. ret = wc_RsaFunction(
  72. info->pk.rsa.in, info->pk.rsa.inLen,
  73. info->pk.rsa.out, info->pk.rsa.outLen,
  74. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  75. break;
  76. case RSA_PRIVATE_ENCRYPT:
  77. case RSA_PRIVATE_DECRYPT:
  78. /* perform software based RSA private op */
  79. ret = wc_RsaFunction(
  80. info->pk.rsa.in, info->pk.rsa.inLen,
  81. info->pk.rsa.out, info->pk.rsa.outLen,
  82. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  83. break;
  84. }
  85. /* reset devId */
  86. info->pk.rsa.key->devId = devIdArg;
  87. }
  88. #endif
  89. #ifdef HAVE_ECC
  90. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  91. /* set devId to invalid, so software is used */
  92. info->pk.eckg.key->devId = INVALID_DEVID;
  93. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  94. info->pk.eckg.key, info->pk.eckg.curveId);
  95. /* reset devId */
  96. info->pk.eckg.key->devId = devIdArg;
  97. }
  98. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  99. /* set devId to invalid, so software is used */
  100. info->pk.eccsign.key->devId = INVALID_DEVID;
  101. ret = wc_ecc_sign_hash(
  102. info->pk.eccsign.in, info->pk.eccsign.inlen,
  103. info->pk.eccsign.out, info->pk.eccsign.outlen,
  104. info->pk.eccsign.rng, info->pk.eccsign.key);
  105. /* reset devId */
  106. info->pk.eccsign.key->devId = devIdArg;
  107. }
  108. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  109. /* set devId to invalid, so software is used */
  110. info->pk.eccverify.key->devId = INVALID_DEVID;
  111. ret = wc_ecc_verify_hash(
  112. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  113. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  114. info->pk.eccverify.res, info->pk.eccverify.key);
  115. /* reset devId */
  116. info->pk.eccverify.key->devId = devIdArg;
  117. }
  118. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  119. /* set devId to invalid, so software is used */
  120. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  121. ret = wc_ecc_shared_secret(
  122. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  123. info->pk.ecdh.out, info->pk.ecdh.outlen);
  124. /* reset devId */
  125. info->pk.ecdh.private_key->devId = devIdArg;
  126. }
  127. #endif /* HAVE_ECC */
  128. }
  129. (void)devIdArg;
  130. (void)myCtx;
  131. return ret;
  132. }
  133. #endif /* WOLF_CRYPTO_CB */
  134. /* ---------------------------------------------------------------------------*/
  135. /* --- Example PK (Public Key) Callback --- */
  136. /* ---------------------------------------------------------------------------*/
  137. #ifdef HAVE_PK_CALLBACKS
  138. #endif