user_rsa.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* user_rsa.h
  2. *
  3. * Copyright (C) 2006-2023 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. /*
  22. Created to use intel's IPP see their license for linking to intel's IPP library
  23. */
  24. #ifndef USER_WOLF_CRYPT_RSA_H
  25. #define USER_WOLF_CRYPT_RSA_H
  26. #include <wolfssl/wolfcrypt/settings.h>
  27. #ifndef NO_RSA
  28. #include <wolfssl/wolfcrypt/types.h>
  29. #include <wolfssl/wolfcrypt/random.h>
  30. /* intels crypto */
  31. #include <ipp.h>
  32. #include <ippcp.h>
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /* needed for WOLFSSL_RSA type but use macro guard against redefine */
  37. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TYPES_DEFINED) \
  38. && !defined(WOLFSSL_RSA_TYPE_DEFINED)
  39. struct WOLFSSL_RSA;
  40. typedef struct WOLFSSL_RSA WOLFSSL_RSA;
  41. #define WOLFSSL_RSA_TYPE_DEFINED
  42. #endif
  43. enum {
  44. RSA_PUBLIC = 0,
  45. RSA_PRIVATE = 1,
  46. };
  47. /* RSA */
  48. struct RsaKey {
  49. IppsBigNumState* n;
  50. IppsBigNumState* e;
  51. IppsBigNumState* dipp;
  52. IppsBigNumState* pipp;
  53. IppsBigNumState* qipp;
  54. IppsBigNumState* dPipp;
  55. IppsBigNumState* dQipp;
  56. IppsBigNumState* uipp;
  57. int nSz, eSz, dSz;
  58. IppsRSAPublicKeyState* pPub;
  59. IppsRSAPrivateKeyState* pPrv;
  60. word32 prvSz; /* size of private key */
  61. word32 sz; /* size of signature */
  62. int type; /* public or private */
  63. void* heap; /* for user memory overrides */
  64. };
  65. #ifndef WC_RSAKEY_TYPE_DEFINED
  66. typedef struct RsaKey RsaKey;
  67. #define WC_RSAKEY_TYPE_DEFINED
  68. #endif
  69. WOLFSSL_API int wc_InitRsaKey(RsaKey* key, void*);
  70. WOLFSSL_API int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId);
  71. WOLFSSL_API int wc_FreeRsaKey(RsaKey* key);
  72. WOLFSSL_API int wc_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out,
  73. word32 outLen, RsaKey* key, WC_RNG* rng);
  74. WOLFSSL_API int wc_RsaPrivateDecryptInline(byte* in, word32 inLen, byte** out,
  75. RsaKey* key);
  76. WOLFSSL_API int wc_RsaPrivateDecrypt(const byte* in, word32 inLen, byte* out,
  77. word32 outLen, RsaKey* key);
  78. WOLFSSL_API int wc_RsaSSL_Sign(const byte* in, word32 inLen, byte* out,
  79. word32 outLen, RsaKey* key, WC_RNG* rng);
  80. WOLFSSL_API int wc_RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out,
  81. RsaKey* key);
  82. WOLFSSL_API int wc_RsaSSL_Verify(const byte* in, word32 inLen, byte* out,
  83. word32 outLen, RsaKey* key);
  84. WOLFSSL_API int wc_RsaEncryptSize(RsaKey* key);
  85. WOLFSSL_API int wc_RsaPrivateKeyDecode(const byte* input, word32* inOutIdx,
  86. RsaKey*, word32);
  87. WOLFSSL_API int wc_RsaPublicKeyDecode_ex(const byte* input, word32* inOutIdx,
  88. word32 inSz, const byte** n, word32* nSz, const byte** e, word32* eSz);
  89. WOLFSSL_API int wc_RsaPublicKeyDecode(const byte* input, word32* inOutIdx,
  90. RsaKey*, word32);
  91. WOLFSSL_API int wc_RsaPublicKeyDecodeRaw(const byte* n, word32 nSz,
  92. const byte* e, word32 eSz, RsaKey* key);
  93. WOLFSSL_API int wc_RsaKeyToDer(RsaKey*, byte* output, word32 inLen);
  94. WOLFSSL_API int wc_RsaKeyToPublicDer(RsaKey*, byte* output, word32 inLen);
  95. #ifdef WOLFSSL_KEY_GEN
  96. WOLFSSL_API int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng);
  97. #endif
  98. WOLFSSL_API int wc_RsaFlattenPublicKey(RsaKey*, byte*, word32*, byte*,
  99. word32*);
  100. WOLFSSL_API int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng);
  101. #if defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA)
  102. /* abstracted BN operations with RSA key */
  103. WOLFSSL_API int wc_Rsa_leading_bit(void* BN);
  104. WOLFSSL_API int wc_Rsa_unsigned_bin_size(void* BN);
  105. /* return MP_OKAY on success */
  106. WOLFSSL_API int wc_Rsa_to_unsigned_bin(void* BN, byte* in, int inLen);
  107. #endif
  108. #ifdef OPENSSL_EXTRA /* abstracted functions to deal with rsa key */
  109. WOLFSSL_API int SetRsaExternal(WOLFSSL_RSA* rsa);
  110. WOLFSSL_API int SetRsaInternal(WOLFSSL_RSA* rsa);
  111. #endif
  112. #ifdef __cplusplus
  113. } /* extern "C" */
  114. #endif
  115. #endif /* NO_RSA */
  116. #endif /* USER_WOLF_CRYPT_RSA_H */