rsa.h 55 KB

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