rsa.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /**
  2. * \file rsa.h
  3. *
  4. * \brief The RSA public-key cryptosystem
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: Apache-2.0
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10. * not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #ifndef MBEDTLS_RSA_H
  24. #define MBEDTLS_RSA_H
  25. #if !defined(MBEDTLS_CONFIG_FILE)
  26. #include "config.h"
  27. #else
  28. #include MBEDTLS_CONFIG_FILE
  29. #endif
  30. #include "bignum.h"
  31. #include "md.h"
  32. #if defined(MBEDTLS_THREADING_C)
  33. #include "threading.h"
  34. #endif
  35. /*
  36. * RSA Error codes
  37. */
  38. #define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
  39. #define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
  40. #define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
  41. #define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the library's validity check. */
  42. #define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
  43. #define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
  44. #define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
  45. #define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
  46. #define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
  47. /*
  48. * RSA constants
  49. */
  50. #define MBEDTLS_RSA_PUBLIC 0
  51. #define MBEDTLS_RSA_PRIVATE 1
  52. #define MBEDTLS_RSA_PKCS_V15 0
  53. #define MBEDTLS_RSA_PKCS_V21 1
  54. #define MBEDTLS_RSA_SIGN 1
  55. #define MBEDTLS_RSA_CRYPT 2
  56. #define MBEDTLS_RSA_SALT_LEN_ANY -1
  57. /*
  58. * The above constants may be used even if the RSA module is compile out,
  59. * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
  60. */
  61. #if defined(MBEDTLS_RSA_C)
  62. #ifdef __cplusplus
  63. extern "C" {
  64. #endif
  65. /**
  66. * \brief RSA context structure
  67. */
  68. typedef struct
  69. {
  70. int ver; /*!< always 0 */
  71. size_t len; /*!< size(N) in chars */
  72. mbedtls_mpi N; /*!< public modulus */
  73. mbedtls_mpi E; /*!< public exponent */
  74. mbedtls_mpi D; /*!< private exponent */
  75. mbedtls_mpi P; /*!< 1st prime factor */
  76. mbedtls_mpi Q; /*!< 2nd prime factor */
  77. mbedtls_mpi DP; /*!< D % (P - 1) */
  78. mbedtls_mpi DQ; /*!< D % (Q - 1) */
  79. mbedtls_mpi QP; /*!< 1 / (Q % P) */
  80. mbedtls_mpi RN; /*!< cached R^2 mod N */
  81. mbedtls_mpi RP; /*!< cached R^2 mod P */
  82. mbedtls_mpi RQ; /*!< cached R^2 mod Q */
  83. mbedtls_mpi Vi; /*!< cached blinding value */
  84. mbedtls_mpi Vf; /*!< cached un-blinding value */
  85. int padding; /*!< MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
  86. MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */
  87. int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
  88. specified in the mbedtls_md.h header file
  89. for the EME-OAEP and EMSA-PSS
  90. encoding */
  91. #if defined(MBEDTLS_THREADING_C)
  92. mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */
  93. #endif
  94. }
  95. mbedtls_rsa_context;
  96. /**
  97. * \brief Initialize an RSA context
  98. *
  99. * Note: Set padding to MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
  100. * encryption scheme and the RSASSA-PSS signature scheme.
  101. *
  102. * \param ctx RSA context to be initialized
  103. * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
  104. * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
  105. *
  106. * \note The hash_id parameter is actually ignored
  107. * when using MBEDTLS_RSA_PKCS_V15 padding.
  108. *
  109. * \note Choice of padding mode is strictly enforced for private key
  110. * operations, since there might be security concerns in
  111. * mixing padding modes. For public key operations it's merely
  112. * a default value, which can be overriden by calling specific
  113. * rsa_rsaes_xxx or rsa_rsassa_xxx functions.
  114. *
  115. * \note The chosen hash is always used for OEAP encryption.
  116. * For PSS signatures, it's always used for making signatures,
  117. * but can be overriden (and always is, if set to
  118. * MBEDTLS_MD_NONE) for verifying them.
  119. */
  120. void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
  121. int padding,
  122. int hash_id);
  123. /**
  124. * \brief Set padding for an already initialized RSA context
  125. * See \c mbedtls_rsa_init() for details.
  126. *
  127. * \param ctx RSA context to be set
  128. * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
  129. * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier
  130. */
  131. void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id);
  132. /**
  133. * \brief Generate an RSA keypair
  134. *
  135. * \param ctx RSA context that will hold the key
  136. * \param f_rng RNG function
  137. * \param p_rng RNG parameter
  138. * \param nbits size of the public key in bits
  139. * \param exponent public exponent (e.g., 65537)
  140. *
  141. * \note mbedtls_rsa_init() must be called beforehand to setup
  142. * the RSA context.
  143. *
  144. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  145. */
  146. int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
  147. int (*f_rng)(void *, unsigned char *, size_t),
  148. void *p_rng,
  149. unsigned int nbits, int exponent );
  150. /**
  151. * \brief Check a public RSA key
  152. *
  153. * \param ctx RSA context to be checked
  154. *
  155. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  156. */
  157. int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
  158. /**
  159. * \brief Check a private RSA key
  160. *
  161. * \param ctx RSA context to be checked
  162. *
  163. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  164. */
  165. int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
  166. /**
  167. * \brief Check a public-private RSA key pair.
  168. * Check each of the contexts, and make sure they match.
  169. *
  170. * \param pub RSA context holding the public key
  171. * \param prv RSA context holding the private key
  172. *
  173. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  174. */
  175. int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv );
  176. /**
  177. * \brief Do an RSA public key operation
  178. *
  179. * \param ctx RSA context
  180. * \param input input buffer
  181. * \param output output buffer
  182. *
  183. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  184. *
  185. * \note This function does NOT take care of message
  186. * padding. Also, be sure to set input[0] = 0 or ensure that
  187. * input is smaller than N.
  188. *
  189. * \note The input and output buffers must be large
  190. * enough (eg. 128 bytes if RSA-1024 is used).
  191. */
  192. int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
  193. const unsigned char *input,
  194. unsigned char *output );
  195. /**
  196. * \brief Do an RSA private key operation
  197. *
  198. * \param ctx RSA context
  199. * \param f_rng RNG function (Needed for blinding)
  200. * \param p_rng RNG parameter
  201. * \param input input buffer
  202. * \param output output buffer
  203. *
  204. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  205. *
  206. * \note The input and output buffers must be large
  207. * enough (eg. 128 bytes if RSA-1024 is used).
  208. */
  209. int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
  210. int (*f_rng)(void *, unsigned char *, size_t),
  211. void *p_rng,
  212. const unsigned char *input,
  213. unsigned char *output );
  214. /**
  215. * \brief Generic wrapper to perform a PKCS#1 encryption using the
  216. * mode from the context. Add the message padding, then do an
  217. * RSA operation.
  218. *
  219. * \param ctx RSA context
  220. * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
  221. * and MBEDTLS_RSA_PRIVATE)
  222. * \param p_rng RNG parameter
  223. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  224. * \param ilen contains the plaintext length
  225. * \param input buffer holding the data to be encrypted
  226. * \param output buffer that will hold the ciphertext
  227. *
  228. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  229. *
  230. * \note The output buffer must be as large as the size
  231. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  232. */
  233. int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
  234. int (*f_rng)(void *, unsigned char *, size_t),
  235. void *p_rng,
  236. int mode, size_t ilen,
  237. const unsigned char *input,
  238. unsigned char *output );
  239. /**
  240. * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
  241. *
  242. * \param ctx RSA context
  243. * \param f_rng RNG function (Needed for padding and MBEDTLS_RSA_PRIVATE)
  244. * \param p_rng RNG parameter
  245. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  246. * \param ilen contains the plaintext length
  247. * \param input buffer holding the data to be encrypted
  248. * \param output buffer that will hold the ciphertext
  249. *
  250. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  251. *
  252. * \note The output buffer must be as large as the size
  253. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  254. */
  255. int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
  256. int (*f_rng)(void *, unsigned char *, size_t),
  257. void *p_rng,
  258. int mode, size_t ilen,
  259. const unsigned char *input,
  260. unsigned char *output );
  261. /**
  262. * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
  263. *
  264. * \param ctx RSA context
  265. * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding
  266. * and MBEDTLS_RSA_PRIVATE)
  267. * \param p_rng RNG parameter
  268. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  269. * \param label buffer holding the custom label to use
  270. * \param label_len contains the label length
  271. * \param ilen contains the plaintext length
  272. * \param input buffer holding the data to be encrypted
  273. * \param output buffer that will hold the ciphertext
  274. *
  275. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  276. *
  277. * \note The output buffer must be as large as the size
  278. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  279. */
  280. int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
  281. int (*f_rng)(void *, unsigned char *, size_t),
  282. void *p_rng,
  283. int mode,
  284. const unsigned char *label, size_t label_len,
  285. size_t ilen,
  286. const unsigned char *input,
  287. unsigned char *output );
  288. /**
  289. * \brief Generic wrapper to perform a PKCS#1 decryption using the
  290. * mode from the context. Do an RSA operation, then remove
  291. * the message padding
  292. *
  293. * \param ctx RSA context
  294. * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
  295. * \param p_rng RNG parameter
  296. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  297. * \param olen will contain the plaintext length
  298. * \param input buffer holding the encrypted data
  299. * \param output buffer that will hold the plaintext
  300. * \param output_max_len maximum length of the output buffer
  301. *
  302. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  303. *
  304. * \note The output buffer must be as large as the size
  305. * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
  306. * an error is thrown.
  307. */
  308. int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
  309. int (*f_rng)(void *, unsigned char *, size_t),
  310. void *p_rng,
  311. int mode, size_t *olen,
  312. const unsigned char *input,
  313. unsigned char *output,
  314. size_t output_max_len );
  315. /**
  316. * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
  317. *
  318. * \param ctx RSA context
  319. * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
  320. * \param p_rng RNG parameter
  321. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  322. * \param olen will contain the plaintext length
  323. * \param input buffer holding the encrypted data
  324. * \param output buffer that will hold the plaintext
  325. * \param output_max_len maximum length of the output buffer
  326. *
  327. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  328. *
  329. * \note The output buffer must be as large as the size
  330. * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
  331. * an error is thrown.
  332. */
  333. int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
  334. int (*f_rng)(void *, unsigned char *, size_t),
  335. void *p_rng,
  336. int mode, size_t *olen,
  337. const unsigned char *input,
  338. unsigned char *output,
  339. size_t output_max_len );
  340. /**
  341. * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
  342. *
  343. * \param ctx RSA context
  344. * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
  345. * \param p_rng RNG parameter
  346. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  347. * \param label buffer holding the custom label to use
  348. * \param label_len contains the label length
  349. * \param olen will contain the plaintext length
  350. * \param input buffer holding the encrypted data
  351. * \param output buffer that will hold the plaintext
  352. * \param output_max_len maximum length of the output buffer
  353. *
  354. * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  355. *
  356. * \note The output buffer must be as large as the size
  357. * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
  358. * an error is thrown.
  359. */
  360. int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
  361. int (*f_rng)(void *, unsigned char *, size_t),
  362. void *p_rng,
  363. int mode,
  364. const unsigned char *label, size_t label_len,
  365. size_t *olen,
  366. const unsigned char *input,
  367. unsigned char *output,
  368. size_t output_max_len );
  369. #if defined MBEDTLS_PKCS1
  370. /**
  371. * \brief Generic wrapper to perform a PKCS#1 signature using the
  372. * mode from the context. Do a private RSA operation to sign
  373. * a message digest
  374. *
  375. * \param ctx RSA context
  376. * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
  377. * MBEDTLS_RSA_PRIVATE)
  378. * \param p_rng RNG parameter
  379. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  380. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  381. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  382. * \param hash buffer holding the message digest
  383. * \param sig buffer that will hold the ciphertext
  384. *
  385. * \return 0 if the signing operation was successful,
  386. * or an MBEDTLS_ERR_RSA_XXX error code
  387. *
  388. * \note The "sig" buffer must be as large as the size
  389. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  390. *
  391. * \note In case of PKCS#1 v2.1 encoding, see comments on
  392. * \note \c mbedtls_rsa_rsassa_pss_sign() for details on md_alg and hash_id.
  393. */
  394. int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
  395. int (*f_rng)(void *, unsigned char *, size_t),
  396. void *p_rng,
  397. int mode,
  398. mbedtls_md_type_t md_alg,
  399. unsigned int hashlen,
  400. const unsigned char *hash,
  401. unsigned char *sig );
  402. /**
  403. * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
  404. *
  405. * \param ctx RSA context
  406. * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
  407. * \param p_rng RNG parameter
  408. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  409. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  410. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  411. * \param hash buffer holding the message digest
  412. * \param sig buffer that will hold the ciphertext
  413. *
  414. * \return 0 if the signing operation was successful,
  415. * or an MBEDTLS_ERR_RSA_XXX error code
  416. *
  417. * \note The "sig" buffer must be as large as the size
  418. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  419. */
  420. int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
  421. int (*f_rng)(void *, unsigned char *, size_t),
  422. void *p_rng,
  423. int mode,
  424. mbedtls_md_type_t md_alg,
  425. unsigned int hashlen,
  426. const unsigned char *hash,
  427. unsigned char *sig );
  428. /**
  429. * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
  430. *
  431. * \param ctx RSA context
  432. * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for
  433. * MBEDTLS_RSA_PRIVATE)
  434. * \param p_rng RNG parameter
  435. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  436. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  437. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  438. * \param hash buffer holding the message digest
  439. * \param sig buffer that will hold the ciphertext
  440. *
  441. * \return 0 if the signing operation was successful,
  442. * or an MBEDTLS_ERR_RSA_XXX error code
  443. *
  444. * \note The "sig" buffer must be as large as the size
  445. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  446. *
  447. * \note The hash_id in the RSA context is the one used for the
  448. * encoding. md_alg in the function call is the type of hash
  449. * that is encoded. According to RFC 3447 it is advised to
  450. * keep both hashes the same.
  451. */
  452. int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
  453. int (*f_rng)(void *, unsigned char *, size_t),
  454. void *p_rng,
  455. int mode,
  456. mbedtls_md_type_t md_alg,
  457. unsigned int hashlen,
  458. const unsigned char *hash,
  459. unsigned char *sig );
  460. /**
  461. * \brief Generic wrapper to perform a PKCS#1 verification using the
  462. * mode from the context. Do a public RSA operation and check
  463. * the message digest
  464. *
  465. * \param ctx points to an RSA public key
  466. * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
  467. * \param p_rng RNG parameter
  468. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  469. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  470. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  471. * \param hash buffer holding the message digest
  472. * \param sig buffer holding the ciphertext
  473. *
  474. * \return 0 if the verify operation was successful,
  475. * or an MBEDTLS_ERR_RSA_XXX error code
  476. *
  477. * \note The "sig" buffer must be as large as the size
  478. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  479. *
  480. * \note In case of PKCS#1 v2.1 encoding, see comments on
  481. * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
  482. */
  483. int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
  484. int (*f_rng)(void *, unsigned char *, size_t),
  485. void *p_rng,
  486. int mode,
  487. mbedtls_md_type_t md_alg,
  488. unsigned int hashlen,
  489. const unsigned char *hash,
  490. const unsigned char *sig );
  491. /**
  492. * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
  493. *
  494. * \param ctx points to an RSA public key
  495. * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
  496. * \param p_rng RNG parameter
  497. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  498. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  499. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  500. * \param hash buffer holding the message digest
  501. * \param sig buffer holding the ciphertext
  502. *
  503. * \return 0 if the verify operation was successful,
  504. * or an MBEDTLS_ERR_RSA_XXX error code
  505. *
  506. * \note The "sig" buffer must be as large as the size
  507. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  508. */
  509. int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
  510. int (*f_rng)(void *, unsigned char *, size_t),
  511. void *p_rng,
  512. int mode,
  513. mbedtls_md_type_t md_alg,
  514. unsigned int hashlen,
  515. const unsigned char *hash,
  516. const unsigned char *sig );
  517. /**
  518. * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
  519. * (This is the "simple" version.)
  520. *
  521. * \param ctx points to an RSA public key
  522. * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
  523. * \param p_rng RNG parameter
  524. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  525. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  526. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  527. * \param hash buffer holding the message digest
  528. * \param sig buffer holding the ciphertext
  529. *
  530. * \return 0 if the verify operation was successful,
  531. * or an MBEDTLS_ERR_RSA_XXX error code
  532. *
  533. * \note The "sig" buffer must be as large as the size
  534. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  535. *
  536. * \note The hash_id in the RSA context is the one used for the
  537. * verification. md_alg in the function call is the type of
  538. * hash that is verified. According to RFC 3447 it is advised to
  539. * keep both hashes the same. If hash_id in the RSA context is
  540. * unset, the md_alg from the function call is used.
  541. */
  542. int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
  543. int (*f_rng)(void *, unsigned char *, size_t),
  544. void *p_rng,
  545. int mode,
  546. mbedtls_md_type_t md_alg,
  547. unsigned int hashlen,
  548. const unsigned char *hash,
  549. const unsigned char *sig );
  550. /**
  551. * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
  552. * (This is the version with "full" options.)
  553. *
  554. * \param ctx points to an RSA public key
  555. * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE)
  556. * \param p_rng RNG parameter
  557. * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
  558. * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data)
  559. * \param hashlen message digest length (for MBEDTLS_MD_NONE only)
  560. * \param hash buffer holding the message digest
  561. * \param mgf1_hash_id message digest used for mask generation
  562. * \param expected_salt_len Length of the salt used in padding, use
  563. * MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length
  564. * \param sig buffer holding the ciphertext
  565. *
  566. * \return 0 if the verify operation was successful,
  567. * or an MBEDTLS_ERR_RSA_XXX error code
  568. *
  569. * \note The "sig" buffer must be as large as the size
  570. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  571. *
  572. * \note The hash_id in the RSA context is ignored.
  573. */
  574. int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
  575. int (*f_rng)(void *, unsigned char *, size_t),
  576. void *p_rng,
  577. int mode,
  578. mbedtls_md_type_t md_alg,
  579. unsigned int hashlen,
  580. const unsigned char *hash,
  581. mbedtls_md_type_t mgf1_hash_id,
  582. int expected_salt_len,
  583. const unsigned char *sig );
  584. #endif
  585. /**
  586. * \brief Copy the components of an RSA context
  587. *
  588. * \param dst Destination context
  589. * \param src Source context
  590. *
  591. * \return 0 on success,
  592. * MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
  593. */
  594. int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
  595. /**
  596. * \brief Free the components of an RSA key
  597. *
  598. * \param ctx RSA Context to free
  599. */
  600. void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
  601. /**
  602. * \brief Checkup routine
  603. *
  604. * \return 0 if successful, or 1 if the test failed
  605. */
  606. int mbedtls_rsa_self_test( int verbose );
  607. #ifdef __cplusplus
  608. }
  609. #endif
  610. #endif /* MBEDTLS_RSA_C */
  611. #endif /* rsa.h */