rsa.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /*!
  2. \ingroup RSA
  3. \brief This function initializes a provided RsaKey struct. It also takes
  4. in a heap identifier, for use with user defined memory overrides
  5. (see XMALLOC, XFREE, XREALLOC).
  6. \return 0 Returned upon successfully initializing the RSA structure for
  7. use with encryption and decryption
  8. \return BAD_FUNC_ARGS Returned if the RSA key pointer evaluates to NULL
  9. \param key pointer to the RsaKey structure to initialize
  10. \param heap pointer to a heap identifier, for use with memory overrides,
  11. allowing custom handling of memory allocation. This heap will be the
  12. default used when allocating memory for use with this RSA object
  13. _Example_
  14. \code
  15. RsaKey enc;
  16. int ret;
  17. ret = wc_InitRsaKey(&enc, NULL); // not using heap hint. No custom memory
  18. if ( ret != 0 ) {
  19. // error initializing RSA key
  20. }
  21. \endcode
  22. \sa wc_RsaInitCavium
  23. \sa wc_FreeRsaKey
  24. */
  25. WOLFSSL_API int wc_InitRsaKey(RsaKey* key, void* heap);
  26. /*!
  27. \ingroup RSA
  28. \brief This function initializes a provided RsaKey struct. The id and
  29. len are used to identify the key on the device while the devId identifies
  30. the device. It also takes in a heap identifier, for use with user defined
  31. memory overrides (see XMALLOC, XFREE, XREALLOC).
  32. \return 0 Returned upon successfully initializing the RSA structure for
  33. use with encryption and decryption
  34. \return BAD_FUNC_ARGS Returned if the RSA key pointer evaluates to NULL
  35. \return BUFFER_E Returned if len is less than 0 or greater than
  36. RSA_MAX_ID_LEN.
  37. \param key pointer to the RsaKey structure to initialize
  38. \param id identifier of key on device
  39. \param len length of identifier in bytes
  40. \param heap pointer to a heap identifier, for use with memory overrides,
  41. allowing custom handling of memory allocation. This heap will be the
  42. default used when allocating memory for use with this RSA object
  43. \param devId ID to use with hardware device
  44. _Example_
  45. \code
  46. RsaKey enc;
  47. unsigned char* id = (unsigned char*)"RSA2048";
  48. int len = 6;
  49. int devId = 1;
  50. int ret;
  51. ret = wc_CryptoDev_RegisterDevice(devId, wc_Pkcs11_CryptoDevCb,
  52. &token);
  53. if ( ret != 0) {
  54. // error associating callback and token with device id
  55. }
  56. ret = wc_InitRsaKey_Id(&enc, id, len, NULL, devId); // not using heap hint
  57. if ( ret != 0 ) {
  58. // error initializing RSA key
  59. }
  60. \endcode
  61. \sa wc_InitRsaKey
  62. \sa wc_RsaInitCavium
  63. \sa wc_FreeRsaKey
  64. */
  65. WOLFSSL_API int wc_InitRsaKey_Id(RsaKey* key, unsigned char* id, int len,
  66. void* heap, int devId);
  67. /*!
  68. \ingroup RSA
  69. \brief This function frees a provided RsaKey struct using mp_clear.
  70. \return 0 Returned upon successfully freeing the key
  71. \param key pointer to the RsaKey structure to free
  72. _Example_
  73. \code
  74. RsaKey enc;
  75. wc_RsaInitKey(&enc, NULL); // not using heap hint. No custom memory
  76. ... set key, do encryption
  77. wc_FreeRsaKey(&enc);
  78. \endcode
  79. \sa wc_InitRsaKey
  80. */
  81. WOLFSSL_API int wc_FreeRsaKey(RsaKey* key);
  82. /*!
  83. \ingroup RSA
  84. \brief This function encrypts a message from in and stores the result
  85. in out. It requires an initialized public key and a random number
  86. generator. As a side effect, this function will return the bytes written
  87. to out in outLen.
  88. \return Success Upon successfully encrypting the input message, returns
  89. the number bytes written to out
  90. \return -1 Returned if there is an error during RSA encryption and
  91. hardware acceleration via Cavium is enabled
  92. \return BAD_FUNC_ARG Returned if any of the input parameters are invalid
  93. \return RSA_BUFFER_E Returned if the output buffer is too small to store
  94. the ciphertext
  95. \return RNG_FAILURE_E Returned if there is an error generating a random
  96. block using the provided RNG structure
  97. \return MP_INIT_E May be returned if there is an error in the math
  98. library used while encrypting the message
  99. \return MP_READ_E May be returned if there is an error in the math
  100. library used while encrypting the message
  101. \return MP_CMP_E May be returned if there is an error in the math
  102. library used while encrypting the message
  103. \return MP_INVMOD_E May be returned if there is an error in the math
  104. library used while encrypting the message
  105. \return MP_EXPTMOD_E May be returned if there is an error in the math
  106. library used while encrypting the message
  107. \return MP_MOD_E May be returned if there is an error in the math
  108. library used while encrypting the message
  109. \return MP_MUL_E May be returned if there is an error in the math
  110. library used while encrypting the message
  111. \return MP_ADD_E May be returned if there is an error in the math
  112. library used while encrypting the message
  113. \return MP_MULMOD_E May be returned if there is an error in the math
  114. library used while encrypting the message
  115. \return MP_TO_E May be returned if there is an error in the math
  116. library used while encrypting the message
  117. \return MP_MEM May be returned if there is an error in the math
  118. library used while encrypting the message
  119. \return MP_ZERO_E May be returned if there is an error in the math
  120. library used while encrypting the message
  121. \param in pointer to a buffer containing the input message to encrypt
  122. \param inLen the length of the message to encrypt
  123. \param out pointer to the buffer in which to store the output ciphertext
  124. \param outLen the length of the output buffer
  125. \param key pointer to the RsaKey structure containing the public
  126. key to use for encryption
  127. \param rng The RNG structure with which to generate random block padding
  128. _Example_
  129. \code
  130. RsaKey pub;
  131. int ret = 0;
  132. byte n[] = { // initialize with received n component of public key };
  133. byte e[] = { // initialize with received e component of public key };
  134. byte msg[] = { // initialize with plaintext of message to encrypt };
  135. byte cipher[256]; // 256 bytes is large enough to store 2048 bit RSA
  136. ciphertext
  137. wc_InitRsaKey(&pub, NULL); // not using heap hint. No custom memory
  138. wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), &pub);
  139. // initialize with received public key parameters
  140. ret = wc_RsaPublicEncrypt(msg, sizeof(msg), out, sizeof(out), &pub, &rng);
  141. if ( ret != 0 ) {
  142. // error encrypting message
  143. }
  144. \endcode
  145. \sa wc_RsaPrivateDecrypt
  146. */
  147. WOLFSSL_API int wc_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out,
  148. word32 outLen, RsaKey* key, WC_RNG* rng);
  149. /*!
  150. \ingroup RSA
  151. \brief This functions is utilized by the wc_RsaPrivateDecrypt function
  152. for decrypting.
  153. \return Success Length of decrypted data.
  154. \return RSA_PAD_E RsaUnPad error, bad formatting
  155. \param in The byte array to be decrypted.
  156. \param inLen The length of in.
  157. \param out The byte array for the decrypted data to be stored.
  158. \param key The key to use for decryption.
  159. _Example_
  160. \code
  161. none
  162. \endcode
  163. \sa wc_RsaPrivateDecrypt
  164. */
  165. WOLFSSL_API int wc_RsaPrivateDecryptInline(byte* in, word32 inLen, byte** out,
  166. RsaKey* key);
  167. /*!
  168. \ingroup RSA
  169. \brief This functions provides private RSA decryption.
  170. \return Success length of decrypted data.
  171. \return MEMORY_E -125, out of memory error
  172. \return BAD_FUNC_ARG -173, Bad function argument provided
  173. \param in The byte array to be decrypted.
  174. \param inLen The length of in.
  175. \param out The byte array for the decrypted data to be stored.
  176. \param outLen The length of out.
  177. \param key The key to use for decryption.
  178. _Example_
  179. \code
  180. ret = wc_RsaPublicEncrypt(in, inLen, out, sizeof(out), &key, &rng);
  181. if (ret < 0) {
  182. return -1;
  183. }
  184. ret = wc_RsaPrivateDecrypt(out, ret, plain, sizeof(plain), &key);
  185. if (ret < 0) {
  186. return -1;
  187. }
  188. \endcode
  189. \sa RsaUnPad
  190. \sa wc_RsaFunction
  191. \sa wc_RsaPrivateDecryptInline
  192. */
  193. WOLFSSL_API int wc_RsaPrivateDecrypt(const byte* in, word32 inLen, byte* out,
  194. word32 outLen, RsaKey* key);
  195. /*!
  196. \ingroup RSA
  197. \brief Signs the provided array with the private key.
  198. \return RSA_BUFFER_E: -131, RSA buffer error, output too small or
  199. input too large
  200. \param in The byte array to be encrypted.
  201. \param inLen The length of in.
  202. \param out The byte array for the encrypted data to be stored.
  203. \param outLen The length of out.
  204. \param key The key to use for encryption.
  205. \param RNG The RNG struct to use for random number purposes.
  206. _Example_
  207. \code
  208. ret = wc_RsaSSL_Sign(in, inLen, out, sizeof(out), &key, &rng);
  209. if (ret < 0) {
  210. return -1;
  211. }
  212. memset(plain, 0, sizeof(plain));
  213. ret = wc_RsaSSL_Verify(out, ret, plain, sizeof(plain), &key);
  214. if (ret < 0) {
  215. return -1;
  216. }
  217. \endcode
  218. \sa wc_RsaPad
  219. */
  220. WOLFSSL_API int wc_RsaSSL_Sign(const byte* in, word32 inLen, byte* out,
  221. word32 outLen, RsaKey* key, WC_RNG* rng);
  222. /*!
  223. \ingroup RSA
  224. \brief Used to verify that the message was signed by RSA key. The output
  225. uses the same byte array as the input.
  226. \return >0 Length of text.
  227. \return <0 An error occurred.
  228. \param in Byte array to be decrypted.
  229. \param inLen Length of the buffer input.
  230. \param out Pointer to a pointer for decrypted information.
  231. \param key RsaKey to use.
  232. _Example_
  233. \code
  234. RsaKey key;
  235. WC_WC_RNG rng;
  236. int ret = 0;
  237. long e = 65537; // standard value to use for exponent
  238. wc_InitRsaKey(&key, NULL); // not using heap hint. No custom memory
  239. wc_InitRng(&rng);
  240. wc_MakeRsaKey(&key, 2048, e, &rng);
  241. byte in[] = { // Initialize with some RSA encrypted information }
  242. byte* out;
  243. if(wc_RsaSSL_VerifyInline(in, sizeof(in), &out, &key) < 0)
  244. {
  245. // handle error
  246. }
  247. \endcode
  248. \sa wc_RsaSSL_Verify
  249. \sa wc_RsaSSL_Sign
  250. */
  251. WOLFSSL_API int wc_RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out,
  252. RsaKey* key);
  253. /*!
  254. \ingroup RSA
  255. \brief Used to verify that the message was signed by key.
  256. \return Success Length of text on no error.
  257. \return MEMORY_E memory exception.
  258. \param in The byte array to be decrypted.
  259. \param inLen The length of in.
  260. \param out The byte array for the decrypted data to be stored.
  261. \param outLen The length of out.
  262. \param key The key to use for verification.
  263. _Example_
  264. \code
  265. ret = wc_RsaSSL_Sign(in, inLen, out, sizeof(out), &key, &rng);
  266. if (ret < 0) {
  267. return -1;
  268. }
  269. memset(plain, 0, sizeof(plain));
  270. ret = wc_RsaSSL_Verify(out, ret, plain, sizeof(plain), &key);
  271. if (ret < 0) {
  272. return -1;
  273. }
  274. \endcode
  275. \sa wc_RsaSSL_Sign
  276. */
  277. WOLFSSL_API int wc_RsaSSL_Verify(const byte* in, word32 inLen, byte* out,
  278. word32 outLen, RsaKey* key);
  279. /*!
  280. \ingroup RSA
  281. \brief Returns the encryption size for the provided key structure.
  282. \return Success Encryption size for the provided key structure.
  283. \param key The key to use for verification.
  284. _Example_
  285. \code
  286. int sz = wc_RsaEncryptSize(&key);
  287. \endcode
  288. \sa wc_InitRsaKey
  289. \sa wc_InitRsaKey_ex
  290. \sa wc_MakeRsaKey
  291. \sa XMEMSET
  292. */
  293. WOLFSSL_API int wc_RsaEncryptSize(RsaKey* key);
  294. /*!
  295. \ingroup RSA
  296. \brief This function parses a DER-formatted RSA private key, extracts the
  297. private key and stores it in the given RsaKey structure. It also sets the
  298. distance parsed in idx.
  299. \return 0 Returned upon successfully parsing the private key from the DER
  300. encoded input
  301. \return ASN_PARSE_E Returned if there is an error parsing the private key
  302. from the input buffer. This may happen if the input private key is not
  303. properly formatted according to ASN.1 standards
  304. \return ASN_RSA_KEY_E Returned if there is an error reading the private
  305. key elements of the RSA key input
  306. \param input pointer to the buffer containing the DER formatted private
  307. key to decode
  308. \param inOutIdx pointer to the index in the buffer at which the key begins
  309. (usually 0). As a side effect of this function, inOutIdx will store the
  310. distance parsed through the input buffer
  311. \param key pointer to the RsaKey structure in which to store the decoded
  312. private key
  313. \param inSz size of the input buffer
  314. _Example_
  315. \code
  316. RsaKey enc;
  317. word32 idx = 0;
  318. int ret = 0;
  319. byte der[] = { // initialize with DER-encoded RSA private key };
  320. wc_InitRsaKey(&enc, NULL); // not using heap hint. No custom memory
  321. ret = wc_RsaPrivateKeyDecode(der, &idx, &enc, sizeof(der));
  322. if( ret != 0 ) {
  323. // error parsing private key
  324. }
  325. \endcode
  326. \sa wc_RsaPublicKeyDecode
  327. \sa wc_MakeRsaKey
  328. */
  329. WOLFSSL_API int wc_RsaPrivateKeyDecode(const byte* input, word32* inOutIdx,
  330. RsaKey*, word32);
  331. /*!
  332. \ingroup RSA
  333. \brief This function parses a DER-formatted RSA public key, extracts the
  334. public key and stores it in the given RsaKey structure. It also sets the
  335. distance parsed in idx.
  336. \return 0 Returned upon successfully parsing the public key from the DER
  337. encoded input
  338. \return ASN_PARSE_E Returned if there is an error parsing the public key
  339. from the input buffer. This may happen if the input public key is not
  340. properly formatted according to ASN.1 standards
  341. \return ASN_OBJECT_ID_E Returned if the ASN.1 Object ID does not match
  342. that of a RSA public key
  343. \return ASN_EXPECT_0_E Returned if the input key is not correctly
  344. formatted according to ASN.1 standards
  345. \return ASN_BITSTR_E Returned if the input key is not correctly formatted
  346. according to ASN.1 standards
  347. \return ASN_RSA_KEY_E Returned if there is an error reading the public key
  348. elements of the RSA key input
  349. \param input pointer to the buffer containing the input DER-encoded RSA
  350. public key to decode
  351. \param inOutIdx pointer to the index in the buffer at which the key
  352. begins (usually 0). As a side effect of this function, inOutIdx will
  353. store the distance parsed through the input buffer
  354. \param key pointer to the RsaKey structure in which to store the decoded
  355. public key
  356. \param inSz size of the input buffer
  357. _Example_
  358. \code
  359. RsaKey pub;
  360. word32 idx = 0;
  361. int ret = 0;
  362. byte der[] = { // initialize with DER-encoded RSA public key };
  363. wc_InitRsaKey(&pub, NULL); // not using heap hint. No custom memory
  364. ret = wc_RsaPublicKeyDecode(der, &idx, &pub, sizeof(der));
  365. if( ret != 0 ) {
  366. // error parsing public key
  367. }
  368. \endcode
  369. \sa wc_RsaPublicKeyDecodeRaw
  370. */
  371. WOLFSSL_API int wc_RsaPublicKeyDecode(const byte* input, word32* inOutIdx,
  372. RsaKey*, word32);
  373. /*!
  374. \ingroup RSA
  375. \brief This function decodes the raw elements of an RSA public key, taking
  376. in the public modulus (n) and exponent (e). It stores these raw elements
  377. in the provided RsaKey structure, allowing one to use them in the
  378. encryption/decryption process.
  379. \return 0 Returned upon successfully decoding the raw elements of the
  380. public key into the RsaKey structure
  381. \return BAD_FUNC_ARG Returned if any of the input arguments evaluates to
  382. NULL
  383. \return MP_INIT_E Returned if there is an error initializing an integer
  384. for use with the multiple precision integer (mp_int) library
  385. \return ASN_GETINT_E Returned if there is an error reading one of the
  386. provided RSA key elements, n or e
  387. \param n pointer to a buffer containing the raw modulus parameter of the
  388. public RSA key
  389. \param nSz size of the buffer containing n
  390. \param e pointer to a buffer containing the raw exponent parameter of
  391. the public RSA key
  392. \param eSz size of the buffer containing e
  393. \param key pointer to the RsaKey struct to initialize with the provided
  394. public key elements
  395. _Example_
  396. \code
  397. RsaKey pub;
  398. int ret = 0;
  399. byte n[] = { // initialize with received n component of public key };
  400. byte e[] = { // initialize with received e component of public key };
  401. wc_InitRsaKey(&pub, NULL); // not using heap hint. No custom memory
  402. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), &pub);
  403. if( ret != 0 ) {
  404. // error parsing public key elements
  405. }
  406. \endcode
  407. \sa wc_RsaPublicKeyDecode
  408. */
  409. WOLFSSL_API int wc_RsaPublicKeyDecodeRaw(const byte* n, word32 nSz,
  410. const byte* e, word32 eSz, RsaKey* key);
  411. /*!
  412. \ingroup RSA
  413. \brief This function converts an RsaKey key to DER format. The result is
  414. written to output and it returns the number of bytes written.
  415. \return 0 Success
  416. \return BAD_FUNC_ARG Returned if key or output is null, or if key->type
  417. is not RSA_PRIVATE, or if inLen isn't large enough for output buffer.
  418. \return MEMORY_E Returned if there is an error allocating memory.
  419. \param key Initialized RsaKey structure.
  420. \param output Pointer to output buffer.
  421. \param inLen Size of output buffer.
  422. _Example_
  423. \code
  424. byte* der;
  425. // Allocate memory for der
  426. int derSz = // Amount of memory allocated for der;
  427. RsaKey key;
  428. WC_WC_RNG rng;
  429. long e = 65537; // standard value to use for exponent
  430. ret = wc_MakeRsaKey(&key, 2048, e, &rng); // generate 2048 bit long
  431. private key
  432. wc_InitRsaKey(&key, NULL);
  433. wc_InitRng(&rng);
  434. if(wc_RsaKeyToDer(&key, der, derSz) != 0)
  435. {
  436. // Handle the error thrown
  437. }
  438. \endcode
  439. \sa wc_RsaKeyToPublicDer
  440. \sa wc_InitRsaKey
  441. \sa wc_MakeRsaKey
  442. \sa wc_InitRng
  443. */
  444. WOLFSSL_API int wc_RsaKeyToDer(RsaKey*, byte* output, word32 inLen);
  445. /*!
  446. \ingroup RSA
  447. \brief This function performs RSA encrypt while allowing the choice of
  448. which padding to use.
  449. \return size On successfully encryption the size of the encrypted buffer
  450. is returned
  451. \return RSA_BUFFER_E RSA buffer error, output too small or input too large
  452. \param in pointer to the buffer for encryption
  453. \param inLen length of the buffer to encrypt
  454. \param out encrypted msg created
  455. \param outLen length of buffer available to hold encrypted msg
  456. \param key initialized RSA key struct
  457. \param rng initialized WC_RNG struct
  458. \param type type of padding to use (WC_RSA_OAEP_PAD or WC_RSA_PKCSV15_PAD)
  459. \param hash type of hash to use (choices can be found in hash.h)
  460. \param mgf type of mask generation function to use
  461. \param label an optional label to associate with encrypted message
  462. \param labelSz size of the optional label used
  463. _Example_
  464. \code
  465. WC_WC_WC_RNG rng;
  466. RsaKey key;
  467. byte in[] = “I use Turing Machines to ask questions”
  468. byte out[256];
  469. int ret;
  470. ret = wc_RsaPublicEncrypt_ex(in, sizeof(in), out, sizeof(out), &key, &rng,
  471. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  472. if (ret < 0) {
  473. //handle error
  474. }
  475. \endcode
  476. \sa wc_RsaPublicEncrypt
  477. \sa wc_RsaPrivateDecrypt_ex
  478. */
  479. WOLFSSL_API int wc_RsaPublicEncrypt_ex(const byte* in, word32 inLen, byte* out,
  480. word32 outLen, RsaKey* key, WC_RNG* rng, int type,
  481. enum wc_HashType hash, int mgf, byte* label, word32 lableSz);
  482. /*!
  483. \ingroup RSA
  484. \brief This function uses RSA to decrypt a message and gives the
  485. option of what padding type.
  486. \return size On successful decryption, the size of the decrypted message
  487. is returned.
  488. \return MEMORY_E Returned if not enough memory on system to malloc a
  489. needed array.
  490. \return BAD_FUNC_ARG Returned if a bad argument was passed into the
  491. function.
  492. \param in pointer to the buffer for decryption
  493. \param inLen length of the buffer to decrypt
  494. \param out decrypted msg created
  495. \param outLen length of buffer available to hold decrypted msg
  496. \param key initialized RSA key struct
  497. \param type type of padding to use (WC_RSA_OAEP_PAD or WC_RSA_PKCSV15_PAD)
  498. \param hash type of hash to use (choices can be found in hash.h)
  499. \param mgf type of mask generation function to use
  500. \param label an optional label to associate with encrypted message
  501. \param labelSz size of the optional label used
  502. _Example_
  503. \code
  504. WC_WC_WC_RNG rng;
  505. RsaKey key;
  506. byte in[] = “I use Turing Machines to ask questions”
  507. byte out[256];
  508. byte plain[256];
  509. int ret;
  510. ret = wc_RsaPublicEncrypt_ex(in, sizeof(in), out, sizeof(out), &key,
  511. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  512. if (ret < 0) {
  513. //handle error
  514. }
  515. ret = wc_RsaPrivateDecrypt_ex(out, ret, plain, sizeof(plain), &key,
  516. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  517. if (ret < 0) {
  518. //handle error
  519. }
  520. \endcode
  521. \sa none
  522. */
  523. WOLFSSL_API int wc_RsaPrivateDecrypt_ex(const byte* in, word32 inLen,
  524. byte* out, word32 outLen, RsaKey* key, int type,
  525. enum wc_HashType hash, int mgf, byte* label, word32 lableSz);
  526. /*!
  527. \ingroup RSA
  528. \brief This function uses RSA to decrypt a message inline and gives the
  529. option of what padding type. The in buffer will contain the decrypted
  530. message after being called and the out byte pointer will point to the
  531. location in the “in” buffer where the plain text is.
  532. \return size On successful decryption, the size of the decrypted message
  533. is returned.
  534. \return MEMORY_E: Returned if not enough memory on system to malloc a
  535. needed array.
  536. \return RSA_PAD_E: Returned if an error in the padding was encountered.
  537. \return BAD_PADDING_E: Returned if an error happened during parsing past
  538. padding.
  539. \return BAD_FUNC_ARG: Returned if a bad argument was passed into the
  540. function.
  541. \param in pointer to the buffer for decryption
  542. \param inLen length of the buffer to decrypt
  543. \param out pointer to location of decrypted message in “in” buffer
  544. \param key initialized RSA key struct
  545. \param type type of padding to use (WC_RSA_OAEP_PAD or WC_RSA_PKCSV15_PAD)
  546. \param hash type of hash to use (choices can be found in hash.h)
  547. \param mgf type of mask generation function to use
  548. \param label an optional label to associate with encrypted message
  549. \param labelSz size of the optional label used
  550. _Example_
  551. \code
  552. WC_WC_WC_RNG rng;
  553. RsaKey key;
  554. byte in[] = “I use Turing Machines to ask questions”
  555. byte out[256];
  556. byte* plain;
  557. int ret;
  558. ret = wc_RsaPublicEncrypt_ex(in, sizeof(in), out, sizeof(out), &key,
  559. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  560. if (ret < 0) {
  561. //handle error
  562. }
  563. ret = wc_RsaPrivateDecryptInline_ex(out, ret, &plain, &key,
  564. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  565. if (ret < 0) {
  566. //handle error
  567. }
  568. \endcode
  569. \sa none
  570. */
  571. WOLFSSL_API int wc_RsaPrivateDecryptInline_ex(byte* in, word32 inLen,
  572. byte** out, RsaKey* key, int type, enum wc_HashType hash,
  573. int mgf, byte* label, word32 lableSz);
  574. /*!
  575. \ingroup RSA
  576. \brief Flattens the RsaKey structure into individual elements (e, n)
  577. used for the RSA algorithm.
  578. \return 0 Returned if the function executed normally, without error.
  579. \return BAD_FUNC_ARG: Returned if any of the parameters are passed in
  580. with a null value.
  581. \return RSA_BUFFER_E: Returned if the e or n buffers passed in are not
  582. the correct size.
  583. \return MP_MEM: Returned if an internal function has memory errors.
  584. \return MP_VAL: Returned if an internal function argument is not valid.
  585. \param key The key to use for verification.
  586. \param e a buffer for the value of e. e is a large positive integer in
  587. the RSA modular arithmetic operation.
  588. \param eSz the size of the e buffer.
  589. \param n a buffer for the value of n. n is a large positive integer in
  590. the RSA modular arithmetic operation.
  591. \param nSz the size of the n buffer.
  592. _Example_
  593. \code
  594. Rsa key; // A valid RSA key.
  595. byte e[ buffer sz E.g. 256 ];
  596. byte n[256];
  597. int ret;
  598. word32 eSz = sizeof(e);
  599. word32 nSz = sizeof(n);
  600. ...
  601. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
  602. if (ret != 0) {
  603. // Failure case.
  604. }
  605. \endcode
  606. \sa wc_InitRsaKey
  607. \sa wc_InitRsaKey_ex
  608. \sa wc_MakeRsaKey
  609. \sa XMEMSET
  610. */
  611. WOLFSSL_API int wc_RsaFlattenPublicKey(RsaKey*, byte*, word32*, byte*,
  612. word32*);
  613. /*!
  614. \ingroup RSA
  615. \brief Convert Rsa Public key to DER format. Writes to output, and
  616. returns count of bytes written.
  617. \return >0 Success, number of bytes written.
  618. \return BAD_FUNC_ARG Returned if key or output is null.
  619. \return MEMORY_E Returned when an error allocating memory occurs.
  620. \return <0 Error
  621. \param key The RSA key structure to convert.
  622. \param output Output buffer to hold DER.
  623. \param inLen Length of buffer.
  624. _Example_
  625. \code
  626. RsaKey key;
  627. wc_RsaInitKey(&key, NULL);
  628. // Use key
  629. int BUFFER_SIZE = // Some adequate size for the buffer
  630. byte output[BUFFER_SIZE];
  631. if(wc_RsaKeyToPublicDer(&key, output, sizeof(output)) != 0)
  632. {
  633. // Handle Error
  634. }
  635. \endcode
  636. \sa wc_RsaKeyToPublicDer
  637. \sa wc_RsaInitKey
  638. */
  639. WOLFSSL_API int wc_RsaKeyToPublicDer(RsaKey*, byte* output, word32 inLen);
  640. /*!
  641. \ingroup RSA
  642. \brief This function generates a RSA private key of length size (in bits)
  643. and given exponent (e). It then stores this key in the provided RsaKey
  644. structure, so that it may be used for encryption/decryption. A secure
  645. number to use for e is 65537. size is required to be greater than
  646. RSA_MIN_SIZE and less than RSA_MAX_SIZE. For this function to be
  647. available, the option WOLFSSL_KEY_GEN must be enabled at compile time.
  648. This can be accomplished with --enable-keygen if using ./configure.
  649. \return 0 Returned upon successfully generating a RSA private key
  650. \return BAD_FUNC_ARG Returned if any of the input arguments are NULL,
  651. the size parameter falls outside of the necessary bounds, or e is
  652. incorrectly chosen
  653. \return RNG_FAILURE_E Returned if there is an error generating a random
  654. block using the provided RNG structure
  655. \return MP_INIT_E
  656. \return MP_READ_E May be May be returned if there is an error in the math
  657. library used while generating the RSA key returned if there is an error
  658. in the math library used while generating the RSA key
  659. \return MP_CMP_E May be returned if there is an error in the math library
  660. used while generating the RSA key
  661. \return MP_INVMOD_E May be returned if there is an error in the math
  662. library used while generating the RSA key
  663. \return MP_EXPTMOD_E May be returned if there is an error in the math
  664. library used while generating the RSA key
  665. \return MP_MOD_E May be returned if there is an error in the math
  666. library used while generating the RSA key
  667. \return MP_MUL_E May be returned if there is an error in the math
  668. library used while generating the RSA key
  669. \return MP_ADD_E May be returned if there is an error in the math
  670. library used while generating the RSA key
  671. \return MP_MULMOD_E May be returned if there is an error in the math
  672. library used while generating the RSA key
  673. \return MP_TO_E May be returned if there is an error in the math
  674. library used while generating the RSA key
  675. \return MP_MEM May be returned if there is an error in the math
  676. library used while generating the RSA key
  677. \return MP_ZERO_E May be returned if there is an error in the math
  678. library used while generating the RSA key
  679. \param key pointer to the RsaKey structure in which to store the
  680. generated private key
  681. \param size desired keylenth, in bits. Required to be greater than
  682. RSA_MIN_SIZE and less than RSA_MAX_SIZE
  683. \param e exponent parameter to use for generating the key. A secure
  684. choice is 65537
  685. \param rng pointer to an RNG structure to use for random number generation
  686. while making the ke
  687. _Example_
  688. \code
  689. RsaKey priv;
  690. WC_WC_RNG rng;
  691. int ret = 0;
  692. long e = 65537; // standard value to use for exponent
  693. wc_InitRsaKey(&priv, NULL); // not using heap hint. No custom memory
  694. wc_InitRng(&rng);
  695. // generate 2048 bit long private key
  696. ret = wc_MakeRsaKey(&priv, 2048, e, &rng);
  697. if( ret != 0 ) {
  698. // error generating private key
  699. }
  700. \endcode
  701. \sa none
  702. */
  703. WOLFSSL_API int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng);
  704. /*!
  705. \ingroup RSA
  706. \brief This function sets the non-blocking RSA context. When a RsaNb context
  707. is set it enables fast math based non-blocking exptmod, which splits the RSA
  708. function into many smaller operations.
  709. Enabled when WC_RSA_NONBLOCK is defined.
  710. \return 0 Success
  711. \return BAD_FUNC_ARG Returned if key or nb is null.
  712. \param key The RSA key structure
  713. \param nb The RSA non-blocking structure for this RSA key to use.
  714. _Example_
  715. \code
  716. int ret, count = 0;
  717. RsaKey key;
  718. RsaNb nb;
  719. wc_RsaInitKey(&key, NULL);
  720. // Enable non-blocking RSA mode - provide context
  721. ret = wc_RsaSetNonBlock(key, &nb);
  722. if (ret != 0)
  723. return ret;
  724. do {
  725. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  726. count++; // track number of would blocks
  727. if (ret == FP_WOULDBLOCK) {
  728. // do "other" work here
  729. }
  730. } while (ret == FP_WOULDBLOCK);
  731. if (ret < 0) {
  732. return ret;
  733. }
  734. printf("RSA non-block sign: size %d, %d times\n", ret, count);
  735. \endcode
  736. \sa wc_RsaSetNonBlockTime
  737. */
  738. WOLFSSL_API int wc_RsaSetNonBlock(RsaKey* key, RsaNb* nb);
  739. /*!
  740. \ingroup RSA
  741. \brief This function configures the maximum amount of blocking time in
  742. microseconds. It uses a pre-computed table (see tfm.c exptModNbInst) along
  743. with the CPU speed in megahertz to determine if the next operation can be
  744. completed within the maximum blocking time provided.
  745. Enabled when WC_RSA_NONBLOCK_TIME is defined.
  746. \return 0 Success
  747. \return BAD_FUNC_ARG Returned if key is null or wc_RsaSetNonBlock was not
  748. previously called and key->nb is null.
  749. \param key The RSA key structure.
  750. \param maxBlockUs Maximum time to block microseconds.
  751. \param cpuMHz CPU speed in megahertz.
  752. _Example_
  753. \code
  754. RsaKey key;
  755. RsaNb nb;
  756. wc_RsaInitKey(&key, NULL);
  757. wc_RsaSetNonBlock(key, &nb);
  758. wc_RsaSetNonBlockTime(&key, 4000, 160); // Block Max = 4 ms, CPU = 160MHz
  759. \endcode
  760. \sa wc_RsaSetNonBlock
  761. */
  762. WOLFSSL_API int wc_RsaSetNonBlockTime(RsaKey* key, word32 maxBlockUs,
  763. word32 cpuMHz);