rsa.h 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. /**
  2. * \file rsa.h
  3. *
  4. * \brief This file provides an API for the RSA public-key cryptosystem.
  5. *
  6. * The RSA public-key cryptosystem is defined in <em>Public-Key
  7. * Cryptography Standards (PKCS) #1 v1.5: RSA Encryption</em>
  8. * and <em>Public-Key Cryptography Standards (PKCS) #1 v2.1:
  9. * RSA Cryptography Specifications</em>.
  10. *
  11. */
  12. /*
  13. * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. *
  28. * This file is part of Mbed TLS (https://tls.mbed.org)
  29. */
  30. #ifndef MBEDTLS_RSA_H
  31. #define MBEDTLS_RSA_H
  32. #if !defined(MBEDTLS_CONFIG_FILE)
  33. #include "config.h"
  34. #else
  35. #include MBEDTLS_CONFIG_FILE
  36. #endif
  37. #include "bignum.h"
  38. #include "md.h"
  39. #if defined(MBEDTLS_THREADING_C)
  40. #include "threading.h"
  41. #endif
  42. /*
  43. * RSA Error codes
  44. */
  45. #define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
  46. #define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
  47. #define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
  48. #define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the validity check of the library. */
  49. #define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
  50. #define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
  51. #define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
  52. #define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
  53. #define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
  54. #define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500 /**< The implementation does not offer the requested operation, for example, because of security violations or lack of functionality. */
  55. #define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580 /**< RSA hardware accelerator failed. */
  56. /*
  57. * RSA constants
  58. */
  59. #define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
  60. #define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
  61. #define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
  62. #define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
  63. #define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
  64. #define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
  65. #define MBEDTLS_RSA_SALT_LEN_ANY -1
  66. /*
  67. * The above constants may be used even if the RSA module is compile out,
  68. * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
  69. */
  70. #ifdef __cplusplus
  71. extern "C" {
  72. #endif
  73. #if !defined(MBEDTLS_RSA_ALT)
  74. // Regular implementation
  75. //
  76. /**
  77. * \brief The RSA context structure.
  78. *
  79. * \note Direct manipulation of the members of this structure
  80. * is deprecated. All manipulation should instead be done through
  81. * the public interface functions.
  82. */
  83. typedef struct
  84. {
  85. int ver; /*!< Always 0.*/
  86. size_t len; /*!< The size of \p N in Bytes. */
  87. mbedtls_mpi N; /*!< The public modulus. */
  88. mbedtls_mpi E; /*!< The public exponent. */
  89. mbedtls_mpi D; /*!< The private exponent. */
  90. mbedtls_mpi P; /*!< The first prime factor. */
  91. mbedtls_mpi Q; /*!< The second prime factor. */
  92. mbedtls_mpi DP; /*!< <code>D % (P - 1)</code>. */
  93. mbedtls_mpi DQ; /*!< <code>D % (Q - 1)</code>. */
  94. mbedtls_mpi QP; /*!< <code>1 / (Q % P)</code>. */
  95. mbedtls_mpi RN; /*!< cached <code>R^2 mod N</code>. */
  96. mbedtls_mpi RP; /*!< cached <code>R^2 mod P</code>. */
  97. mbedtls_mpi RQ; /*!< cached <code>R^2 mod Q</code>. */
  98. mbedtls_mpi Vi; /*!< The cached blinding value. */
  99. mbedtls_mpi Vf; /*!< The cached un-blinding value. */
  100. int padding; /*!< Selects padding mode:
  101. #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
  102. #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
  103. int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
  104. as specified in md.h for use in the MGF
  105. mask generating function used in the
  106. EME-OAEP and EMSA-PSS encodings. */
  107. #if defined(MBEDTLS_THREADING_C)
  108. mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
  109. #endif
  110. }
  111. mbedtls_rsa_context;
  112. #else /* MBEDTLS_RSA_ALT */
  113. #include "rsa_alt.h"
  114. #endif /* MBEDTLS_RSA_ALT */
  115. /**
  116. * \brief This function initializes an RSA context.
  117. *
  118. * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
  119. * encryption scheme and the RSASSA-PSS signature scheme.
  120. *
  121. * \note The \p hash_id parameter is ignored when using
  122. * #MBEDTLS_RSA_PKCS_V15 padding.
  123. *
  124. * \note The choice of padding mode is strictly enforced for private key
  125. * operations, since there might be security concerns in
  126. * mixing padding modes. For public key operations it is
  127. * a default value, which can be overriden by calling specific
  128. * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
  129. *
  130. * \note The hash selected in \p hash_id is always used for OEAP
  131. * encryption. For PSS signatures, it is always used for
  132. * making signatures, but can be overriden for verifying them.
  133. * If set to #MBEDTLS_MD_NONE, it is always overriden.
  134. *
  135. * \param ctx The RSA context to initialize.
  136. * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
  137. * #MBEDTLS_RSA_PKCS_V21.
  138. * \param hash_id The hash identifier of #mbedtls_md_type_t type, if
  139. * \p padding is #MBEDTLS_RSA_PKCS_V21.
  140. */
  141. void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
  142. int padding,
  143. int hash_id);
  144. /**
  145. * \brief This function imports a set of core parameters into an
  146. * RSA context.
  147. *
  148. * \note This function can be called multiple times for successive
  149. * imports, if the parameters are not simultaneously present.
  150. *
  151. * Any sequence of calls to this function should be followed
  152. * by a call to mbedtls_rsa_complete(), which checks and
  153. * completes the provided information to a ready-for-use
  154. * public or private RSA key.
  155. *
  156. * \note See mbedtls_rsa_complete() for more information on which
  157. * parameters are necessary to set up a private or public
  158. * RSA key.
  159. *
  160. * \note The imported parameters are copied and need not be preserved
  161. * for the lifetime of the RSA context being set up.
  162. *
  163. * \param ctx The initialized RSA context to store the parameters in.
  164. * \param N The RSA modulus, or NULL.
  165. * \param P The first prime factor of \p N, or NULL.
  166. * \param Q The second prime factor of \p N, or NULL.
  167. * \param D The private exponent, or NULL.
  168. * \param E The public exponent, or NULL.
  169. *
  170. * \return \c 0 on success.
  171. * \return A non-zero error code on failure.
  172. */
  173. int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
  174. const mbedtls_mpi *N,
  175. const mbedtls_mpi *P, const mbedtls_mpi *Q,
  176. const mbedtls_mpi *D, const mbedtls_mpi *E );
  177. /**
  178. * \brief This function imports core RSA parameters, in raw big-endian
  179. * binary format, into an RSA context.
  180. *
  181. * \note This function can be called multiple times for successive
  182. * imports, if the parameters are not simultaneously present.
  183. *
  184. * Any sequence of calls to this function should be followed
  185. * by a call to mbedtls_rsa_complete(), which checks and
  186. * completes the provided information to a ready-for-use
  187. * public or private RSA key.
  188. *
  189. * \note See mbedtls_rsa_complete() for more information on which
  190. * parameters are necessary to set up a private or public
  191. * RSA key.
  192. *
  193. * \note The imported parameters are copied and need not be preserved
  194. * for the lifetime of the RSA context being set up.
  195. *
  196. * \param ctx The initialized RSA context to store the parameters in.
  197. * \param N The RSA modulus, or NULL.
  198. * \param N_len The Byte length of \p N, ignored if \p N == NULL.
  199. * \param P The first prime factor of \p N, or NULL.
  200. * \param P_len The Byte length of \p P, ignored if \p P == NULL.
  201. * \param Q The second prime factor of \p N, or NULL.
  202. * \param Q_len The Byte length of \p Q, ignored if \p Q == NULL.
  203. * \param D The private exponent, or NULL.
  204. * \param D_len The Byte length of \p D, ignored if \p D == NULL.
  205. * \param E The public exponent, or NULL.
  206. * \param E_len The Byte length of \p E, ignored if \p E == NULL.
  207. *
  208. * \return \c 0 on success.
  209. * \return A non-zero error code on failure.
  210. */
  211. int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
  212. unsigned char const *N, size_t N_len,
  213. unsigned char const *P, size_t P_len,
  214. unsigned char const *Q, size_t Q_len,
  215. unsigned char const *D, size_t D_len,
  216. unsigned char const *E, size_t E_len );
  217. /**
  218. * \brief This function completes an RSA context from
  219. * a set of imported core parameters.
  220. *
  221. * To setup an RSA public key, precisely \p N and \p E
  222. * must have been imported.
  223. *
  224. * To setup an RSA private key, sufficient information must
  225. * be present for the other parameters to be derivable.
  226. *
  227. * The default implementation supports the following:
  228. * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
  229. * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
  230. * Alternative implementations need not support these.
  231. *
  232. * If this function runs successfully, it guarantees that
  233. * the RSA context can be used for RSA operations without
  234. * the risk of failure or crash.
  235. *
  236. * \warning This function need not perform consistency checks
  237. * for the imported parameters. In particular, parameters that
  238. * are not needed by the implementation might be silently
  239. * discarded and left unchecked. To check the consistency
  240. * of the key material, see mbedtls_rsa_check_privkey().
  241. *
  242. * \param ctx The initialized RSA context holding imported parameters.
  243. *
  244. * \return \c 0 on success.
  245. * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
  246. * failed.
  247. *
  248. */
  249. int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
  250. /**
  251. * \brief This function exports the core parameters of an RSA key.
  252. *
  253. * If this function runs successfully, the non-NULL buffers
  254. * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
  255. * written, with additional unused space filled leading by
  256. * zero Bytes.
  257. *
  258. * Possible reasons for returning
  259. * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
  260. * <li>An alternative RSA implementation is in use, which
  261. * stores the key externally, and either cannot or should
  262. * not export it into RAM.</li>
  263. * <li>A SW or HW implementation might not support a certain
  264. * deduction. For example, \p P, \p Q from \p N, \p D,
  265. * and \p E if the former are not part of the
  266. * implementation.</li></ul>
  267. *
  268. * If the function fails due to an unsupported operation,
  269. * the RSA context stays intact and remains usable.
  270. *
  271. * \param ctx The initialized RSA context.
  272. * \param N The MPI to hold the RSA modulus, or NULL.
  273. * \param P The MPI to hold the first prime factor of \p N, or NULL.
  274. * \param Q The MPI to hold the second prime factor of \p N, or NULL.
  275. * \param D The MPI to hold the private exponent, or NULL.
  276. * \param E The MPI to hold the public exponent, or NULL.
  277. *
  278. * \return \c 0 on success.
  279. * \return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
  280. * requested parameters cannot be done due to missing
  281. * functionality or because of security policies.
  282. * \return A non-zero return code on any other failure.
  283. *
  284. */
  285. int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
  286. mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
  287. mbedtls_mpi *D, mbedtls_mpi *E );
  288. /**
  289. * \brief This function exports core parameters of an RSA key
  290. * in raw big-endian binary format.
  291. *
  292. * If this function runs successfully, the non-NULL buffers
  293. * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
  294. * written, with additional unused space filled leading by
  295. * zero Bytes.
  296. *
  297. * Possible reasons for returning
  298. * #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
  299. * <li>An alternative RSA implementation is in use, which
  300. * stores the key externally, and either cannot or should
  301. * not export it into RAM.</li>
  302. * <li>A SW or HW implementation might not support a certain
  303. * deduction. For example, \p P, \p Q from \p N, \p D,
  304. * and \p E if the former are not part of the
  305. * implementation.</li></ul>
  306. * If the function fails due to an unsupported operation,
  307. * the RSA context stays intact and remains usable.
  308. *
  309. * \note The length parameters are ignored if the corresponding
  310. * buffer pointers are NULL.
  311. *
  312. * \param ctx The initialized RSA context.
  313. * \param N The Byte array to store the RSA modulus, or NULL.
  314. * \param N_len The size of the buffer for the modulus.
  315. * \param P The Byte array to hold the first prime factor of \p N, or
  316. * NULL.
  317. * \param P_len The size of the buffer for the first prime factor.
  318. * \param Q The Byte array to hold the second prime factor of \p N, or
  319. * NULL.
  320. * \param Q_len The size of the buffer for the second prime factor.
  321. * \param D The Byte array to hold the private exponent, or NULL.
  322. * \param D_len The size of the buffer for the private exponent.
  323. * \param E The Byte array to hold the public exponent, or NULL.
  324. * \param E_len The size of the buffer for the public exponent.
  325. *
  326. * \return \c 0 on success.
  327. * \return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
  328. * requested parameters cannot be done due to missing
  329. * functionality or because of security policies.
  330. * \return A non-zero return code on any other failure.
  331. */
  332. int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
  333. unsigned char *N, size_t N_len,
  334. unsigned char *P, size_t P_len,
  335. unsigned char *Q, size_t Q_len,
  336. unsigned char *D, size_t D_len,
  337. unsigned char *E, size_t E_len );
  338. /**
  339. * \brief This function exports CRT parameters of a private RSA key.
  340. *
  341. * \note Alternative RSA implementations not using CRT-parameters
  342. * internally can implement this function based on
  343. * mbedtls_rsa_deduce_opt().
  344. *
  345. * \param ctx The initialized RSA context.
  346. * \param DP The MPI to hold D modulo P-1, or NULL.
  347. * \param DQ The MPI to hold D modulo Q-1, or NULL.
  348. * \param QP The MPI to hold modular inverse of Q modulo P, or NULL.
  349. *
  350. * \return \c 0 on success.
  351. * \return A non-zero error code on failure.
  352. *
  353. */
  354. int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
  355. mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
  356. /**
  357. * \brief This function sets padding for an already initialized RSA
  358. * context. See mbedtls_rsa_init() for details.
  359. *
  360. * \param ctx The RSA context to be set.
  361. * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
  362. * #MBEDTLS_RSA_PKCS_V21.
  363. * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
  364. */
  365. void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
  366. int hash_id);
  367. /**
  368. * \brief This function retrieves the length of RSA modulus in Bytes.
  369. *
  370. * \param ctx The initialized RSA context.
  371. *
  372. * \return The length of the RSA modulus in Bytes.
  373. *
  374. */
  375. size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
  376. /**
  377. * \brief This function generates an RSA keypair.
  378. *
  379. * \note mbedtls_rsa_init() must be called before this function,
  380. * to set up the RSA context.
  381. *
  382. * \param ctx The RSA context used to hold the key.
  383. * \param f_rng The RNG function.
  384. * \param p_rng The RNG context.
  385. * \param nbits The size of the public key in bits.
  386. * \param exponent The public exponent. For example, 65537.
  387. *
  388. * \return \c 0 on success.
  389. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  390. */
  391. int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
  392. int (*f_rng)(void *, unsigned char *, size_t),
  393. void *p_rng,
  394. unsigned int nbits, int exponent );
  395. /**
  396. * \brief This function checks if a context contains at least an RSA
  397. * public key.
  398. *
  399. * If the function runs successfully, it is guaranteed that
  400. * enough information is present to perform an RSA public key
  401. * operation using mbedtls_rsa_public().
  402. *
  403. * \param ctx The RSA context to check.
  404. *
  405. * \return \c 0 on success.
  406. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  407. *
  408. */
  409. int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
  410. /**
  411. * \brief This function checks if a context contains an RSA private key
  412. * and perform basic consistency checks.
  413. *
  414. * \note The consistency checks performed by this function not only
  415. * ensure that mbedtls_rsa_private() can be called successfully
  416. * on the given context, but that the various parameters are
  417. * mutually consistent with high probability, in the sense that
  418. * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
  419. *
  420. * \warning This function should catch accidental misconfigurations
  421. * like swapping of parameters, but it cannot establish full
  422. * trust in neither the quality nor the consistency of the key
  423. * material that was used to setup the given RSA context:
  424. * <ul><li>Consistency: Imported parameters that are irrelevant
  425. * for the implementation might be silently dropped. If dropped,
  426. * the current function does not have access to them,
  427. * and therefore cannot check them. See mbedtls_rsa_complete().
  428. * If you want to check the consistency of the entire
  429. * content of an PKCS1-encoded RSA private key, for example, you
  430. * should use mbedtls_rsa_validate_params() before setting
  431. * up the RSA context.
  432. * Additionally, if the implementation performs empirical checks,
  433. * these checks substantiate but do not guarantee consistency.</li>
  434. * <li>Quality: This function is not expected to perform
  435. * extended quality assessments like checking that the prime
  436. * factors are safe. Additionally, it is the responsibility of the
  437. * user to ensure the trustworthiness of the source of his RSA
  438. * parameters, which goes beyond what is effectively checkable
  439. * by the library.</li></ul>
  440. *
  441. * \param ctx The RSA context to check.
  442. *
  443. * \return \c 0 on success.
  444. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  445. */
  446. int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
  447. /**
  448. * \brief This function checks a public-private RSA key pair.
  449. *
  450. * It checks each of the contexts, and makes sure they match.
  451. *
  452. * \param pub The RSA context holding the public key.
  453. * \param prv The RSA context holding the private key.
  454. *
  455. * \return \c 0 on success.
  456. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  457. */
  458. int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
  459. const mbedtls_rsa_context *prv );
  460. /**
  461. * \brief This function performs an RSA public key operation.
  462. *
  463. * \note This function does not handle message padding.
  464. *
  465. * \note Make sure to set \p input[0] = 0 or ensure that
  466. * input is smaller than \p N.
  467. *
  468. * \note The input and output buffers must be large
  469. * enough. For example, 128 Bytes if RSA-1024 is used.
  470. *
  471. * \param ctx The RSA context.
  472. * \param input The input buffer.
  473. * \param output The output buffer.
  474. *
  475. * \return \c 0 on success.
  476. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  477. */
  478. int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
  479. const unsigned char *input,
  480. unsigned char *output );
  481. /**
  482. * \brief This function performs an RSA private key operation.
  483. *
  484. * \note The input and output buffers must be large
  485. * enough. For example, 128 Bytes if RSA-1024 is used.
  486. *
  487. * \note Blinding is used if and only if a PRNG is provided.
  488. *
  489. * \note If blinding is used, both the base of exponentation
  490. * and the exponent are blinded, providing protection
  491. * against some side-channel attacks.
  492. *
  493. * \warning It is deprecated and a security risk to not provide
  494. * a PRNG here and thereby prevent the use of blinding.
  495. * Future versions of the library may enforce the presence
  496. * of a PRNG.
  497. *
  498. * \param ctx The RSA context.
  499. * \param f_rng The RNG function. Needed for blinding.
  500. * \param p_rng The RNG context.
  501. * \param input The input buffer.
  502. * \param output The output buffer.
  503. *
  504. * \return \c 0 on success.
  505. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  506. *
  507. */
  508. int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
  509. int (*f_rng)(void *, unsigned char *, size_t),
  510. void *p_rng,
  511. const unsigned char *input,
  512. unsigned char *output );
  513. /**
  514. * \brief This function adds the message padding, then performs an RSA
  515. * operation.
  516. *
  517. * It is the generic wrapper for performing a PKCS#1 encryption
  518. * operation using the \p mode from the context.
  519. *
  520. * \note The input and output buffers must be as large as the size
  521. * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
  522. *
  523. * \deprecated It is deprecated and discouraged to call this function
  524. * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
  525. * are likely to remove the \p mode argument and have it
  526. * implicitly set to #MBEDTLS_RSA_PUBLIC.
  527. *
  528. * \note Alternative implementations of RSA need not support
  529. * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
  530. * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
  531. *
  532. * \param ctx The RSA context.
  533. * \param f_rng The RNG function. Needed for padding, PKCS#1 v2.1
  534. * encoding, and #MBEDTLS_RSA_PRIVATE.
  535. * \param p_rng The RNG context.
  536. * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
  537. * \param ilen The length of the plaintext.
  538. * \param input The buffer holding the data to encrypt.
  539. * \param output The buffer used to hold the ciphertext.
  540. *
  541. * \return \c 0 on success.
  542. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  543. */
  544. int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
  545. int (*f_rng)(void *, unsigned char *, size_t),
  546. void *p_rng,
  547. int mode, size_t ilen,
  548. const unsigned char *input,
  549. unsigned char *output );
  550. /**
  551. * \brief This function performs a PKCS#1 v1.5 encryption operation
  552. * (RSAES-PKCS1-v1_5-ENCRYPT).
  553. *
  554. * \note The output buffer must be as large as the size
  555. * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
  556. *
  557. * \deprecated It is deprecated and discouraged to call this function
  558. * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
  559. * are likely to remove the \p mode argument and have it
  560. * implicitly set to #MBEDTLS_RSA_PUBLIC.
  561. *
  562. * \note Alternative implementations of RSA need not support
  563. * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
  564. * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
  565. *
  566. * \param ctx The RSA context.
  567. * \param f_rng The RNG function. Needed for padding and
  568. * #MBEDTLS_RSA_PRIVATE.
  569. * \param p_rng The RNG context.
  570. * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
  571. * \param ilen The length of the plaintext.
  572. * \param input The buffer holding the data to encrypt.
  573. * \param output The buffer used to hold the ciphertext.
  574. *
  575. * \return \c 0 on success.
  576. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  577. */
  578. int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
  579. int (*f_rng)(void *, unsigned char *, size_t),
  580. void *p_rng,
  581. int mode, size_t ilen,
  582. const unsigned char *input,
  583. unsigned char *output );
  584. /**
  585. * \brief This function performs a PKCS#1 v2.1 OAEP encryption
  586. * operation (RSAES-OAEP-ENCRYPT).
  587. *
  588. * \note The output buffer must be as large as the size
  589. * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
  590. *
  591. * \deprecated It is deprecated and discouraged to call this function
  592. * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
  593. * are likely to remove the \p mode argument and have it
  594. * implicitly set to #MBEDTLS_RSA_PUBLIC.
  595. *
  596. * \note Alternative implementations of RSA need not support
  597. * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
  598. * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
  599. *
  600. * \param ctx The RSA context.
  601. * \param f_rng The RNG function. Needed for padding and PKCS#1 v2.1
  602. * encoding and #MBEDTLS_RSA_PRIVATE.
  603. * \param p_rng The RNG context.
  604. * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
  605. * \param label The buffer holding the custom label to use.
  606. * \param label_len The length of the label.
  607. * \param ilen The length of the plaintext.
  608. * \param input The buffer holding the data to encrypt.
  609. * \param output The buffer used to hold the ciphertext.
  610. *
  611. * \return \c 0 on success.
  612. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  613. */
  614. int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
  615. int (*f_rng)(void *, unsigned char *, size_t),
  616. void *p_rng,
  617. int mode,
  618. const unsigned char *label, size_t label_len,
  619. size_t ilen,
  620. const unsigned char *input,
  621. unsigned char *output );
  622. /**
  623. * \brief This function performs an RSA operation, then removes the
  624. * message padding.
  625. *
  626. * It is the generic wrapper for performing a PKCS#1 decryption
  627. * operation using the \p mode from the context.
  628. *
  629. * \note The output buffer length \c output_max_len should be
  630. * as large as the size \p ctx->len of \p ctx->N (for example,
  631. * 128 Bytes if RSA-1024 is used) to be able to hold an
  632. * arbitrary decrypted message. If it is not large enough to
  633. * hold the decryption of the particular ciphertext provided,
  634. * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
  635. *
  636. * \note The input buffer must be as large as the size
  637. * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
  638. *
  639. * \deprecated It is deprecated and discouraged to call this function
  640. * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
  641. * are likely to remove the \p mode argument and have it
  642. * implicitly set to #MBEDTLS_RSA_PRIVATE.
  643. *
  644. * \note Alternative implementations of RSA need not support
  645. * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
  646. * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
  647. *
  648. * \param ctx The RSA context.
  649. * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
  650. * \param p_rng The RNG context.
  651. * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
  652. * \param olen The length of the plaintext.
  653. * \param input The buffer holding the encrypted data.
  654. * \param output The buffer used to hold the plaintext.
  655. * \param output_max_len The maximum length of the output buffer.
  656. *
  657. * \return \c 0 on success.
  658. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  659. */
  660. int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
  661. int (*f_rng)(void *, unsigned char *, size_t),
  662. void *p_rng,
  663. int mode, size_t *olen,
  664. const unsigned char *input,
  665. unsigned char *output,
  666. size_t output_max_len );
  667. /**
  668. * \brief This function performs a PKCS#1 v1.5 decryption
  669. * operation (RSAES-PKCS1-v1_5-DECRYPT).
  670. *
  671. * \note The output buffer length \c output_max_len should be
  672. * as large as the size \p ctx->len of \p ctx->N, for example,
  673. * 128 Bytes if RSA-1024 is used, to be able to hold an
  674. * arbitrary decrypted message. If it is not large enough to
  675. * hold the decryption of the particular ciphertext provided,
  676. * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
  677. *
  678. * \note The input buffer must be as large as the size
  679. * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
  680. *
  681. * \deprecated It is deprecated and discouraged to call this function
  682. * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
  683. * are likely to remove the \p mode argument and have it
  684. * implicitly set to #MBEDTLS_RSA_PRIVATE.
  685. *
  686. * \note Alternative implementations of RSA need not support
  687. * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
  688. * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
  689. *
  690. * \param ctx The RSA context.
  691. * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
  692. * \param p_rng The RNG context.
  693. * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
  694. * \param olen The length of the plaintext.
  695. * \param input The buffer holding the encrypted data.
  696. * \param output The buffer to hold the plaintext.
  697. * \param output_max_len The maximum length of the output buffer.
  698. *
  699. * \return \c 0 on success.
  700. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  701. *
  702. */
  703. int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
  704. int (*f_rng)(void *, unsigned char *, size_t),
  705. void *p_rng,
  706. int mode, size_t *olen,
  707. const unsigned char *input,
  708. unsigned char *output,
  709. size_t output_max_len );
  710. /**
  711. * \brief This function performs a PKCS#1 v2.1 OAEP decryption
  712. * operation (RSAES-OAEP-DECRYPT).
  713. *
  714. * \note The output buffer length \c output_max_len should be
  715. * as large as the size \p ctx->len of \p ctx->N, for
  716. * example, 128 Bytes if RSA-1024 is used, to be able to
  717. * hold an arbitrary decrypted message. If it is not
  718. * large enough to hold the decryption of the particular
  719. * ciphertext provided, the function returns
  720. * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
  721. *
  722. * \note The input buffer must be as large as the size
  723. * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
  724. *
  725. * \deprecated It is deprecated and discouraged to call this function
  726. * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
  727. * are likely to remove the \p mode argument and have it
  728. * implicitly set to #MBEDTLS_RSA_PRIVATE.
  729. *
  730. * \note Alternative implementations of RSA need not support
  731. * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
  732. * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
  733. *
  734. * \param ctx The RSA context.
  735. * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
  736. * \param p_rng The RNG context.
  737. * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
  738. * \param label The buffer holding the custom label to use.
  739. * \param label_len The length of the label.
  740. * \param olen The length of the plaintext.
  741. * \param input The buffer holding the encrypted data.
  742. * \param output The buffer to hold the plaintext.
  743. * \param output_max_len The maximum length of the output buffer.
  744. *
  745. * \return \c 0 on success.
  746. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  747. */
  748. int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
  749. int (*f_rng)(void *, unsigned char *, size_t),
  750. void *p_rng,
  751. int mode,
  752. const unsigned char *label, size_t label_len,
  753. size_t *olen,
  754. const unsigned char *input,
  755. unsigned char *output,
  756. size_t output_max_len );
  757. /**
  758. * \brief This function performs a private RSA operation to sign
  759. * a message digest using PKCS#1.
  760. *
  761. * It is the generic wrapper for performing a PKCS#1
  762. * signature using the \p mode from the context.
  763. *
  764. * \note The \p sig buffer must be as large as the size
  765. * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
  766. *
  767. * \note For PKCS#1 v2.1 encoding, see comments on
  768. * mbedtls_rsa_rsassa_pss_sign() for details on
  769. * \p md_alg and \p hash_id.
  770. *
  771. * \deprecated It is deprecated and discouraged to call this function
  772. * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
  773. * are likely to remove the \p mode argument and have it
  774. * implicitly set to #MBEDTLS_RSA_PRIVATE.
  775. *
  776. * \note Alternative implementations of RSA need not support
  777. * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
  778. * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
  779. *
  780. * \param ctx The RSA context.
  781. * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
  782. * #MBEDTLS_RSA_PRIVATE.
  783. * \param p_rng The RNG context.
  784. * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
  785. * \param md_alg The message-digest algorithm used to hash the original data.
  786. * Use #MBEDTLS_MD_NONE for signing raw data.
  787. * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
  788. * \param hash The buffer holding the message digest.
  789. * \param sig The buffer to hold the ciphertext.
  790. *
  791. * \return \c 0 if the signing operation was successful.
  792. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  793. */
  794. int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
  795. int (*f_rng)(void *, unsigned char *, size_t),
  796. void *p_rng,
  797. int mode,
  798. mbedtls_md_type_t md_alg,
  799. unsigned int hashlen,
  800. const unsigned char *hash,
  801. unsigned char *sig );
  802. /**
  803. * \brief This function performs a PKCS#1 v1.5 signature
  804. * operation (RSASSA-PKCS1-v1_5-SIGN).
  805. *
  806. * \note The \p sig buffer must be as large as the size
  807. * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
  808. *
  809. * \deprecated It is deprecated and discouraged to call this function
  810. * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
  811. * are likely to remove the \p mode argument and have it
  812. * implicitly set to #MBEDTLS_RSA_PRIVATE.
  813. *
  814. * \note Alternative implementations of RSA need not support
  815. * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
  816. * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
  817. *
  818. * \param ctx The RSA context.
  819. * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
  820. * \param p_rng The RNG context.
  821. * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
  822. * \param md_alg The message-digest algorithm used to hash the original data.
  823. * Use #MBEDTLS_MD_NONE for signing raw data.
  824. * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
  825. * \param hash The buffer holding the message digest.
  826. * \param sig The buffer to hold the ciphertext.
  827. *
  828. * \return \c 0 if the signing operation was successful.
  829. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  830. */
  831. int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
  832. int (*f_rng)(void *, unsigned char *, size_t),
  833. void *p_rng,
  834. int mode,
  835. mbedtls_md_type_t md_alg,
  836. unsigned int hashlen,
  837. const unsigned char *hash,
  838. unsigned char *sig );
  839. /**
  840. * \brief This function performs a PKCS#1 v2.1 PSS signature
  841. * operation (RSASSA-PSS-SIGN).
  842. *
  843. * \note The \p sig buffer must be as large as the size
  844. * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
  845. *
  846. * \note The \p hash_id in the RSA context is the one used for the
  847. * encoding. \p md_alg in the function call is the type of hash
  848. * that is encoded. According to <em>RFC-3447: Public-Key
  849. * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
  850. * Specifications</em> it is advised to keep both hashes the
  851. * same.
  852. *
  853. * \deprecated It is deprecated and discouraged to call this function
  854. * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
  855. * are likely to remove the \p mode argument and have it
  856. * implicitly set to #MBEDTLS_RSA_PRIVATE.
  857. *
  858. * \note Alternative implementations of RSA need not support
  859. * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
  860. * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
  861. *
  862. * \param ctx The RSA context.
  863. * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
  864. * #MBEDTLS_RSA_PRIVATE.
  865. * \param p_rng The RNG context.
  866. * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
  867. * \param md_alg The message-digest algorithm used to hash the original data.
  868. * Use #MBEDTLS_MD_NONE for signing raw data.
  869. * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
  870. * \param hash The buffer holding the message digest.
  871. * \param sig The buffer to hold the ciphertext.
  872. *
  873. * \return \c 0 if the signing operation was successful.
  874. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  875. */
  876. int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
  877. int (*f_rng)(void *, unsigned char *, size_t),
  878. void *p_rng,
  879. int mode,
  880. mbedtls_md_type_t md_alg,
  881. unsigned int hashlen,
  882. const unsigned char *hash,
  883. unsigned char *sig );
  884. /**
  885. * \brief This function performs a public RSA operation and checks
  886. * the message digest.
  887. *
  888. * This is the generic wrapper for performing a PKCS#1
  889. * verification using the mode from the context.
  890. *
  891. * \note The \p sig buffer must be as large as the size
  892. * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
  893. *
  894. * \note For PKCS#1 v2.1 encoding, see comments on
  895. * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
  896. * \p hash_id.
  897. *
  898. * \deprecated It is deprecated and discouraged to call this function
  899. * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
  900. * are likely to remove the \p mode argument and have it
  901. * set to #MBEDTLS_RSA_PUBLIC.
  902. *
  903. * \note Alternative implementations of RSA need not support
  904. * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
  905. * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
  906. *
  907. * \param ctx The RSA public key context.
  908. * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
  909. * \param p_rng The RNG context.
  910. * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
  911. * \param md_alg The message-digest algorithm used to hash the original data.
  912. * Use #MBEDTLS_MD_NONE for signing raw data.
  913. * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
  914. * \param hash The buffer holding the message digest.
  915. * \param sig The buffer holding the ciphertext.
  916. *
  917. * \return \c 0 if the verify operation was successful.
  918. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  919. */
  920. int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
  921. int (*f_rng)(void *, unsigned char *, size_t),
  922. void *p_rng,
  923. int mode,
  924. mbedtls_md_type_t md_alg,
  925. unsigned int hashlen,
  926. const unsigned char *hash,
  927. const unsigned char *sig );
  928. /**
  929. * \brief This function performs a PKCS#1 v1.5 verification
  930. * operation (RSASSA-PKCS1-v1_5-VERIFY).
  931. *
  932. * \note The \p sig buffer must be as large as the size
  933. * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
  934. *
  935. * \deprecated It is deprecated and discouraged to call this function
  936. * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
  937. * are likely to remove the \p mode argument and have it
  938. * set to #MBEDTLS_RSA_PUBLIC.
  939. *
  940. * \note Alternative implementations of RSA need not support
  941. * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
  942. * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
  943. *
  944. * \param ctx The RSA public key context.
  945. * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
  946. * \param p_rng The RNG context.
  947. * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
  948. * \param md_alg The message-digest algorithm used to hash the original data.
  949. * Use #MBEDTLS_MD_NONE for signing raw data.
  950. * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
  951. * \param hash The buffer holding the message digest.
  952. * \param sig The buffer holding the ciphertext.
  953. *
  954. * \return \c 0 if the verify operation was successful.
  955. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  956. */
  957. int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
  958. int (*f_rng)(void *, unsigned char *, size_t),
  959. void *p_rng,
  960. int mode,
  961. mbedtls_md_type_t md_alg,
  962. unsigned int hashlen,
  963. const unsigned char *hash,
  964. const unsigned char *sig );
  965. /**
  966. * \brief This function performs a PKCS#1 v2.1 PSS verification
  967. * operation (RSASSA-PSS-VERIFY).
  968. *
  969. * The hash function for the MGF mask generating function
  970. * is that specified in the RSA context.
  971. *
  972. * \note The \p sig buffer must be as large as the size
  973. * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
  974. *
  975. * \note The \p hash_id in the RSA context is the one used for the
  976. * verification. \p md_alg in the function call is the type of
  977. * hash that is verified. According to <em>RFC-3447: Public-Key
  978. * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
  979. * Specifications</em> it is advised to keep both hashes the
  980. * same. If \p hash_id in the RSA context is unset,
  981. * the \p md_alg from the function call is used.
  982. *
  983. * \deprecated It is deprecated and discouraged to call this function
  984. * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
  985. * are likely to remove the \p mode argument and have it
  986. * implicitly set to #MBEDTLS_RSA_PUBLIC.
  987. *
  988. * \note Alternative implementations of RSA need not support
  989. * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
  990. * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
  991. *
  992. * \param ctx The RSA public key context.
  993. * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
  994. * \param p_rng The RNG context.
  995. * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
  996. * \param md_alg The message-digest algorithm used to hash the original data.
  997. * Use #MBEDTLS_MD_NONE for signing raw data.
  998. * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
  999. * \param hash The buffer holding the message digest.
  1000. * \param sig The buffer holding the ciphertext.
  1001. *
  1002. * \return \c 0 if the verify operation was successful.
  1003. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  1004. */
  1005. int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
  1006. int (*f_rng)(void *, unsigned char *, size_t),
  1007. void *p_rng,
  1008. int mode,
  1009. mbedtls_md_type_t md_alg,
  1010. unsigned int hashlen,
  1011. const unsigned char *hash,
  1012. const unsigned char *sig );
  1013. /**
  1014. * \brief This function performs a PKCS#1 v2.1 PSS verification
  1015. * operation (RSASSA-PSS-VERIFY).
  1016. *
  1017. * The hash function for the MGF mask generating function
  1018. * is that specified in \p mgf1_hash_id.
  1019. *
  1020. * \note The \p sig buffer must be as large as the size
  1021. * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
  1022. *
  1023. * \note The \p hash_id in the RSA context is ignored.
  1024. *
  1025. * \param ctx The RSA public key context.
  1026. * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
  1027. * \param p_rng The RNG context.
  1028. * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
  1029. * \param md_alg The message-digest algorithm used to hash the original data.
  1030. * Use #MBEDTLS_MD_NONE for signing raw data.
  1031. * \param hashlen The length of the message digest. Only used if \p md_alg is
  1032. * #MBEDTLS_MD_NONE.
  1033. * \param hash The buffer holding the message digest.
  1034. * \param mgf1_hash_id The message digest used for mask generation.
  1035. * \param expected_salt_len The length of the salt used in padding. Use
  1036. * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
  1037. * \param sig The buffer holding the ciphertext.
  1038. *
  1039. * \return \c 0 if the verify operation was successful.
  1040. * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
  1041. */
  1042. int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
  1043. int (*f_rng)(void *, unsigned char *, size_t),
  1044. void *p_rng,
  1045. int mode,
  1046. mbedtls_md_type_t md_alg,
  1047. unsigned int hashlen,
  1048. const unsigned char *hash,
  1049. mbedtls_md_type_t mgf1_hash_id,
  1050. int expected_salt_len,
  1051. const unsigned char *sig );
  1052. /**
  1053. * \brief This function copies the components of an RSA context.
  1054. *
  1055. * \param dst The destination context.
  1056. * \param src The source context.
  1057. *
  1058. * \return \c 0 on success.
  1059. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
  1060. */
  1061. int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
  1062. /**
  1063. * \brief This function frees the components of an RSA key.
  1064. *
  1065. * \param ctx The RSA Context to free.
  1066. */
  1067. void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
  1068. /**
  1069. * \brief The RSA checkup routine.
  1070. *
  1071. * \return \c 0 on success.
  1072. * \return \c 1 on failure.
  1073. */
  1074. int mbedtls_rsa_self_test( int verbose );
  1075. #ifdef __cplusplus
  1076. }
  1077. #endif
  1078. #endif /* rsa.h */