rsa.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #ifndef OPENSSL_HEADER_RSA_H
  57. #define OPENSSL_HEADER_RSA_H
  58. #include <openssl/base.h>
  59. #include <openssl/engine.h>
  60. #include <openssl/ex_data.h>
  61. #include <openssl/thread.h>
  62. #if defined(__cplusplus)
  63. extern "C" {
  64. #endif
  65. // rsa.h contains functions for handling encryption and signature using RSA.
  66. // Allocation and destruction.
  67. //
  68. // An |RSA| object represents a public or private RSA key. A given object may be
  69. // used concurrently on multiple threads by non-mutating functions, provided no
  70. // other thread is concurrently calling a mutating function. Unless otherwise
  71. // documented, functions which take a |const| pointer are non-mutating and
  72. // functions which take a non-|const| pointer are mutating.
  73. // RSA_new returns a new, empty |RSA| object or NULL on error.
  74. OPENSSL_EXPORT RSA *RSA_new(void);
  75. // RSA_new_method acts the same as |RSA_new| but takes an explicit |ENGINE|.
  76. OPENSSL_EXPORT RSA *RSA_new_method(const ENGINE *engine);
  77. // RSA_free decrements the reference count of |rsa| and frees it if the
  78. // reference count drops to zero.
  79. OPENSSL_EXPORT void RSA_free(RSA *rsa);
  80. // RSA_up_ref increments the reference count of |rsa| and returns one. It does
  81. // not mutate |rsa| for thread-safety purposes and may be used concurrently.
  82. OPENSSL_EXPORT int RSA_up_ref(RSA *rsa);
  83. // Properties.
  84. // RSA_bits returns the size of |rsa|, in bits.
  85. OPENSSL_EXPORT unsigned RSA_bits(const RSA *rsa);
  86. // RSA_get0_n returns |rsa|'s public modulus.
  87. OPENSSL_EXPORT const BIGNUM *RSA_get0_n(const RSA *rsa);
  88. // RSA_get0_e returns |rsa|'s public exponent.
  89. OPENSSL_EXPORT const BIGNUM *RSA_get0_e(const RSA *rsa);
  90. // RSA_get0_d returns |rsa|'s private exponent. If |rsa| is a public key, this
  91. // value will be NULL.
  92. OPENSSL_EXPORT const BIGNUM *RSA_get0_d(const RSA *rsa);
  93. // RSA_get0_p returns |rsa|'s first private prime factor. If |rsa| is a public
  94. // key or lacks its prime factors, this value will be NULL.
  95. OPENSSL_EXPORT const BIGNUM *RSA_get0_p(const RSA *rsa);
  96. // RSA_get0_q returns |rsa|'s second private prime factor. If |rsa| is a public
  97. // key or lacks its prime factors, this value will be NULL.
  98. OPENSSL_EXPORT const BIGNUM *RSA_get0_q(const RSA *rsa);
  99. // RSA_get0_dmp1 returns d (mod p-1) for |rsa|. If |rsa| is a public key or
  100. // lacks CRT parameters, this value will be NULL.
  101. OPENSSL_EXPORT const BIGNUM *RSA_get0_dmp1(const RSA *rsa);
  102. // RSA_get0_dmq1 returns d (mod q-1) for |rsa|. If |rsa| is a public key or
  103. // lacks CRT parameters, this value will be NULL.
  104. OPENSSL_EXPORT const BIGNUM *RSA_get0_dmq1(const RSA *rsa);
  105. // RSA_get0_iqmp returns q^-1 (mod p). If |rsa| is a public key or lacks CRT
  106. // parameters, this value will be NULL.
  107. OPENSSL_EXPORT const BIGNUM *RSA_get0_iqmp(const RSA *rsa);
  108. // RSA_get0_key sets |*out_n|, |*out_e|, and |*out_d|, if non-NULL, to |rsa|'s
  109. // modulus, public exponent, and private exponent, respectively. If |rsa| is a
  110. // public key, the private exponent will be set to NULL.
  111. OPENSSL_EXPORT void RSA_get0_key(const RSA *rsa, const BIGNUM **out_n,
  112. const BIGNUM **out_e, const BIGNUM **out_d);
  113. // RSA_get0_factors sets |*out_p| and |*out_q|, if non-NULL, to |rsa|'s prime
  114. // factors. If |rsa| is a public key, they will be set to NULL.
  115. OPENSSL_EXPORT void RSA_get0_factors(const RSA *rsa, const BIGNUM **out_p,
  116. const BIGNUM **out_q);
  117. // RSA_get0_crt_params sets |*out_dmp1|, |*out_dmq1|, and |*out_iqmp|, if
  118. // non-NULL, to |rsa|'s CRT parameters. These are d (mod p-1), d (mod q-1) and
  119. // q^-1 (mod p), respectively. If |rsa| is a public key, each parameter will be
  120. // set to NULL.
  121. OPENSSL_EXPORT void RSA_get0_crt_params(const RSA *rsa, const BIGNUM **out_dmp1,
  122. const BIGNUM **out_dmq1,
  123. const BIGNUM **out_iqmp);
  124. // RSA_set0_key sets |rsa|'s modulus, public exponent, and private exponent to
  125. // |n|, |e|, and |d| respectively, if non-NULL. On success, it takes ownership
  126. // of each argument and returns one. Otherwise, it returns zero.
  127. //
  128. // |d| may be NULL, but |n| and |e| must either be non-NULL or already
  129. // configured on |rsa|.
  130. //
  131. // It is an error to call this function after |rsa| has been used for a
  132. // cryptographic operation. Construct a new |RSA| object instead.
  133. OPENSSL_EXPORT int RSA_set0_key(RSA *rsa, BIGNUM *n, BIGNUM *e, BIGNUM *d);
  134. // RSA_set0_factors sets |rsa|'s prime factors to |p| and |q|, if non-NULL, and
  135. // takes ownership of them. On success, it takes ownership of each argument and
  136. // returns one. Otherwise, it returns zero.
  137. //
  138. // Each argument must either be non-NULL or already configured on |rsa|.
  139. //
  140. // It is an error to call this function after |rsa| has been used for a
  141. // cryptographic operation. Construct a new |RSA| object instead.
  142. OPENSSL_EXPORT int RSA_set0_factors(RSA *rsa, BIGNUM *p, BIGNUM *q);
  143. // RSA_set0_crt_params sets |rsa|'s CRT parameters to |dmp1|, |dmq1|, and
  144. // |iqmp|, if non-NULL, and takes ownership of them. On success, it takes
  145. // ownership of its parameters and returns one. Otherwise, it returns zero.
  146. //
  147. // Each argument must either be non-NULL or already configured on |rsa|.
  148. //
  149. // It is an error to call this function after |rsa| has been used for a
  150. // cryptographic operation. Construct a new |RSA| object instead.
  151. OPENSSL_EXPORT int RSA_set0_crt_params(RSA *rsa, BIGNUM *dmp1, BIGNUM *dmq1,
  152. BIGNUM *iqmp);
  153. // Key generation.
  154. // RSA_generate_key_ex generates a new RSA key where the modulus has size
  155. // |bits| and the public exponent is |e|. If unsure, |RSA_F4| is a good value
  156. // for |e|. If |cb| is not NULL then it is called during the key generation
  157. // process. In addition to the calls documented for |BN_generate_prime_ex|, it
  158. // is called with event=2 when the n'th prime is rejected as unsuitable and
  159. // with event=3 when a suitable value for |p| is found.
  160. //
  161. // It returns one on success or zero on error.
  162. OPENSSL_EXPORT int RSA_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e,
  163. BN_GENCB *cb);
  164. // RSA_generate_key_fips behaves like |RSA_generate_key_ex| but performs
  165. // additional checks for FIPS compliance. The public exponent is always 65537
  166. // and |bits| must be either 2048 or 3072.
  167. OPENSSL_EXPORT int RSA_generate_key_fips(RSA *rsa, int bits, BN_GENCB *cb);
  168. // Encryption / Decryption
  169. //
  170. // These functions are considered non-mutating for thread-safety purposes and
  171. // may be used concurrently.
  172. // RSA_PKCS1_PADDING denotes PKCS#1 v1.5 padding. When used with encryption,
  173. // this is RSAES-PKCS1-v1_5. When used with signing, this is RSASSA-PKCS1-v1_5.
  174. #define RSA_PKCS1_PADDING 1
  175. // RSA_NO_PADDING denotes a raw RSA operation.
  176. #define RSA_NO_PADDING 3
  177. // RSA_PKCS1_OAEP_PADDING denotes the RSAES-OAEP encryption scheme.
  178. #define RSA_PKCS1_OAEP_PADDING 4
  179. // RSA_PKCS1_PSS_PADDING denotes the RSASSA-PSS signature scheme. This value may
  180. // not be passed into |RSA_sign_raw|, only |EVP_PKEY_CTX_set_rsa_padding|. See
  181. // also |RSA_sign_pss_mgf1| and |RSA_verify_pss_mgf1|.
  182. #define RSA_PKCS1_PSS_PADDING 6
  183. // RSA_encrypt encrypts |in_len| bytes from |in| to the public key from |rsa|
  184. // and writes, at most, |max_out| bytes of encrypted data to |out|. The
  185. // |max_out| argument must be, at least, |RSA_size| in order to ensure success.
  186. //
  187. // It returns 1 on success or zero on error.
  188. //
  189. // The |padding| argument must be one of the |RSA_*_PADDING| values. If in
  190. // doubt, use |RSA_PKCS1_OAEP_PADDING| for new protocols but
  191. // |RSA_PKCS1_PADDING| is most common.
  192. OPENSSL_EXPORT int RSA_encrypt(RSA *rsa, size_t *out_len, uint8_t *out,
  193. size_t max_out, const uint8_t *in, size_t in_len,
  194. int padding);
  195. // RSA_decrypt decrypts |in_len| bytes from |in| with the private key from
  196. // |rsa| and writes, at most, |max_out| bytes of plaintext to |out|. The
  197. // |max_out| argument must be, at least, |RSA_size| in order to ensure success.
  198. //
  199. // It returns 1 on success or zero on error.
  200. //
  201. // The |padding| argument must be one of the |RSA_*_PADDING| values. If in
  202. // doubt, use |RSA_PKCS1_OAEP_PADDING| for new protocols.
  203. //
  204. // Passing |RSA_PKCS1_PADDING| into this function is deprecated and insecure. If
  205. // implementing a protocol using RSAES-PKCS1-V1_5, use |RSA_NO_PADDING| and then
  206. // check padding in constant-time combined with a swap to a random session key
  207. // or other mitigation. See "Chosen Ciphertext Attacks Against Protocols Based
  208. // on the RSA Encryption Standard PKCS #1", Daniel Bleichenbacher, Advances in
  209. // Cryptology (Crypto '98).
  210. OPENSSL_EXPORT int RSA_decrypt(RSA *rsa, size_t *out_len, uint8_t *out,
  211. size_t max_out, const uint8_t *in, size_t in_len,
  212. int padding);
  213. // RSA_public_encrypt encrypts |flen| bytes from |from| to the public key in
  214. // |rsa| and writes the encrypted data to |to|. The |to| buffer must have at
  215. // least |RSA_size| bytes of space. It returns the number of bytes written, or
  216. // -1 on error. The |padding| argument must be one of the |RSA_*_PADDING|
  217. // values. If in doubt, use |RSA_PKCS1_OAEP_PADDING| for new protocols but
  218. // |RSA_PKCS1_PADDING| is most common.
  219. //
  220. // WARNING: this function is dangerous because it breaks the usual return value
  221. // convention. Use |RSA_encrypt| instead.
  222. OPENSSL_EXPORT int RSA_public_encrypt(size_t flen, const uint8_t *from,
  223. uint8_t *to, RSA *rsa, int padding);
  224. // RSA_private_decrypt decrypts |flen| bytes from |from| with the public key in
  225. // |rsa| and writes the plaintext to |to|. The |to| buffer must have at least
  226. // |RSA_size| bytes of space. It returns the number of bytes written, or -1 on
  227. // error. The |padding| argument must be one of the |RSA_*_PADDING| values. If
  228. // in doubt, use |RSA_PKCS1_OAEP_PADDING| for new protocols. Passing
  229. // |RSA_PKCS1_PADDING| into this function is deprecated and insecure. See
  230. // |RSA_decrypt|.
  231. //
  232. // WARNING: this function is dangerous because it breaks the usual return value
  233. // convention. Use |RSA_decrypt| instead.
  234. OPENSSL_EXPORT int RSA_private_decrypt(size_t flen, const uint8_t *from,
  235. uint8_t *to, RSA *rsa, int padding);
  236. // Signing / Verification
  237. //
  238. // These functions are considered non-mutating for thread-safety purposes and
  239. // may be used concurrently.
  240. // RSA_sign signs |digest_len| bytes of digest from |digest| with |rsa| using
  241. // RSASSA-PKCS1-v1_5. It writes, at most, |RSA_size(rsa)| bytes to |out|. On
  242. // successful return, the actual number of bytes written is written to
  243. // |*out_len|.
  244. //
  245. // The |hash_nid| argument identifies the hash function used to calculate
  246. // |digest| and is embedded in the resulting signature. For example, it might be
  247. // |NID_sha256|.
  248. //
  249. // It returns 1 on success and zero on error.
  250. //
  251. // WARNING: |digest| must be the result of hashing the data to be signed with
  252. // |hash_nid|. Passing unhashed inputs will not result in a secure signature
  253. // scheme.
  254. OPENSSL_EXPORT int RSA_sign(int hash_nid, const uint8_t *digest,
  255. unsigned digest_len, uint8_t *out,
  256. unsigned *out_len, RSA *rsa);
  257. // RSA_sign_pss_mgf1 signs |digest_len| bytes from |digest| with the public key
  258. // from |rsa| using RSASSA-PSS with MGF1 as the mask generation function. It
  259. // writes, at most, |max_out| bytes of signature data to |out|. The |max_out|
  260. // argument must be, at least, |RSA_size| in order to ensure success. It returns
  261. // 1 on success or zero on error.
  262. //
  263. // The |md| and |mgf1_md| arguments identify the hash used to calculate |digest|
  264. // and the MGF1 hash, respectively. If |mgf1_md| is NULL, |md| is
  265. // used.
  266. //
  267. // |salt_len| specifies the expected salt length in bytes. If |salt_len| is -1,
  268. // then the salt length is the same as the hash length. If -2, then the salt
  269. // length is maximal given the size of |rsa|. If unsure, use -1.
  270. //
  271. // WARNING: |digest| must be the result of hashing the data to be signed with
  272. // |md|. Passing unhashed inputs will not result in a secure signature scheme.
  273. OPENSSL_EXPORT int RSA_sign_pss_mgf1(RSA *rsa, size_t *out_len, uint8_t *out,
  274. size_t max_out, const uint8_t *digest,
  275. size_t digest_len, const EVP_MD *md,
  276. const EVP_MD *mgf1_md, int salt_len);
  277. // RSA_sign_raw performs the private key portion of computing a signature with
  278. // |rsa|. It writes, at most, |max_out| bytes of signature data to |out|. The
  279. // |max_out| argument must be, at least, |RSA_size| in order to ensure the
  280. // output fits. It returns 1 on success or zero on error.
  281. //
  282. // If |padding| is |RSA_PKCS1_PADDING|, this function wraps |in| with the
  283. // padding portion of RSASSA-PKCS1-v1_5 and then performs the raw private key
  284. // operation. The caller is responsible for hashing the input and wrapping it in
  285. // a DigestInfo structure.
  286. //
  287. // If |padding| is |RSA_NO_PADDING|, this function only performs the raw private
  288. // key operation, interpreting |in| as a integer modulo n. The caller is
  289. // responsible for hashing the input and encoding it for the signature scheme
  290. // being implemented.
  291. //
  292. // WARNING: This function is a building block for a signature scheme, not a
  293. // complete one. |in| must be the result of hashing and encoding the data as
  294. // needed for the scheme being implemented. Passing in arbitrary inputs will not
  295. // result in a secure signature scheme.
  296. OPENSSL_EXPORT int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out,
  297. size_t max_out, const uint8_t *in,
  298. size_t in_len, int padding);
  299. // RSA_verify verifies that |sig_len| bytes from |sig| are a valid,
  300. // RSASSA-PKCS1-v1_5 signature of |digest_len| bytes at |digest| by |rsa|.
  301. //
  302. // The |hash_nid| argument identifies the hash function used to calculate
  303. // |digest| and is embedded in the resulting signature in order to prevent hash
  304. // confusion attacks. For example, it might be |NID_sha256|.
  305. //
  306. // It returns one if the signature is valid and zero otherwise.
  307. //
  308. // WARNING: this differs from the original, OpenSSL function which additionally
  309. // returned -1 on error.
  310. //
  311. // WARNING: |digest| must be the result of hashing the data to be verified with
  312. // |hash_nid|. Passing unhashed input will not result in a secure signature
  313. // scheme.
  314. OPENSSL_EXPORT int RSA_verify(int hash_nid, const uint8_t *digest,
  315. size_t digest_len, const uint8_t *sig,
  316. size_t sig_len, RSA *rsa);
  317. // RSA_verify_pss_mgf1 verifies that |sig_len| bytes from |sig| are a valid,
  318. // RSASSA-PSS signature of |digest_len| bytes at |digest| by |rsa|. It returns
  319. // one if the signature is valid and zero otherwise. MGF1 is used as the mask
  320. // generation function.
  321. //
  322. // The |md| and |mgf1_md| arguments identify the hash used to calculate |digest|
  323. // and the MGF1 hash, respectively. If |mgf1_md| is NULL, |md| is
  324. // used. |salt_len| specifies the expected salt length in bytes.
  325. //
  326. // If |salt_len| is -1, then the salt length is the same as the hash length. If
  327. // -2, then the salt length is recovered and all values accepted. If unsure, use
  328. // -1.
  329. //
  330. // WARNING: |digest| must be the result of hashing the data to be verified with
  331. // |md|. Passing unhashed input will not result in a secure signature scheme.
  332. OPENSSL_EXPORT int RSA_verify_pss_mgf1(RSA *rsa, const uint8_t *digest,
  333. size_t digest_len, const EVP_MD *md,
  334. const EVP_MD *mgf1_md, int salt_len,
  335. const uint8_t *sig, size_t sig_len);
  336. // RSA_verify_raw performs the public key portion of verifying |in_len| bytes of
  337. // signature from |in| using the public key from |rsa|. On success, it returns
  338. // one and writes, at most, |max_out| bytes of output to |out|. The |max_out|
  339. // argument must be, at least, |RSA_size| in order to ensure the output fits. On
  340. // failure or invalid input, it returns zero.
  341. //
  342. // If |padding| is |RSA_PKCS1_PADDING|, this function checks the padding portion
  343. // of RSASSA-PKCS1-v1_5 and outputs the remainder of the encoded digest. The
  344. // caller is responsible for checking the output is a DigestInfo-wrapped digest
  345. // of the message.
  346. //
  347. // If |padding| is |RSA_NO_PADDING|, this function only performs the raw public
  348. // key operation. The caller is responsible for checking the output is a valid
  349. // result for the signature scheme being implemented.
  350. //
  351. // WARNING: This function is a building block for a signature scheme, not a
  352. // complete one. Checking for arbitrary strings in |out| will not result in a
  353. // secure signature scheme.
  354. OPENSSL_EXPORT int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out,
  355. size_t max_out, const uint8_t *in,
  356. size_t in_len, int padding);
  357. // RSA_private_encrypt performs the private key portion of computing a signature
  358. // with |rsa|. It takes |flen| bytes from |from| as input and writes the result
  359. // to |to|. The |to| buffer must have at least |RSA_size| bytes of space. It
  360. // returns the number of bytes written, or -1 on error.
  361. //
  362. // For the interpretation of |padding| and the input, see |RSA_sign_raw|.
  363. //
  364. // WARNING: This function is a building block for a signature scheme, not a
  365. // complete one. See |RSA_sign_raw| for details.
  366. //
  367. // WARNING: This function is dangerous because it breaks the usual return value
  368. // convention. Use |RSA_sign_raw| instead.
  369. OPENSSL_EXPORT int RSA_private_encrypt(size_t flen, const uint8_t *from,
  370. uint8_t *to, RSA *rsa, int padding);
  371. // RSA_public_decrypt performs the public key portion of verifying |flen| bytes
  372. // of signature from |from| using the public key from |rsa|. It writes the
  373. // result to |to|, which must have at least |RSA_size| bytes of space. It
  374. // returns the number of bytes written, or -1 on error.
  375. //
  376. // For the interpretation of |padding| and the result, see |RSA_verify_raw|.
  377. //
  378. // WARNING: This function is a building block for a signature scheme, not a
  379. // complete one. See |RSA_verify_raw| for details.
  380. //
  381. // WARNING: This function is dangerous because it breaks the usual return value
  382. // convention. Use |RSA_verify_raw| instead.
  383. OPENSSL_EXPORT int RSA_public_decrypt(size_t flen, const uint8_t *from,
  384. uint8_t *to, RSA *rsa, int padding);
  385. // Utility functions.
  386. // RSA_size returns the number of bytes in the modulus, which is also the size
  387. // of a signature or encrypted value using |rsa|.
  388. OPENSSL_EXPORT unsigned RSA_size(const RSA *rsa);
  389. // RSA_is_opaque returns one if |rsa| is opaque and doesn't expose its key
  390. // material. Otherwise it returns zero.
  391. OPENSSL_EXPORT int RSA_is_opaque(const RSA *rsa);
  392. // RSAPublicKey_dup allocates a fresh |RSA| and copies the public key from
  393. // |rsa| into it. It returns the fresh |RSA| object, or NULL on error.
  394. OPENSSL_EXPORT RSA *RSAPublicKey_dup(const RSA *rsa);
  395. // RSAPrivateKey_dup allocates a fresh |RSA| and copies the private key from
  396. // |rsa| into it. It returns the fresh |RSA| object, or NULL on error.
  397. OPENSSL_EXPORT RSA *RSAPrivateKey_dup(const RSA *rsa);
  398. // RSA_check_key performs basic validity tests on |rsa|. It returns one if
  399. // they pass and zero otherwise. Opaque keys and public keys always pass. If it
  400. // returns zero then a more detailed error is available on the error queue.
  401. OPENSSL_EXPORT int RSA_check_key(const RSA *rsa);
  402. // RSA_check_fips performs public key validity tests on |key|. It returns one if
  403. // they pass and zero otherwise. Opaque keys always fail. This function does not
  404. // mutate |rsa| for thread-safety purposes and may be used concurrently.
  405. OPENSSL_EXPORT int RSA_check_fips(RSA *key);
  406. // RSA_verify_PKCS1_PSS_mgf1 verifies that |EM| is a correct PSS padding of
  407. // |mHash|, where |mHash| is a digest produced by |Hash|. |EM| must point to
  408. // exactly |RSA_size(rsa)| bytes of data. The |mgf1Hash| argument specifies the
  409. // hash function for generating the mask. If NULL, |Hash| is used. The |sLen|
  410. // argument specifies the expected salt length in bytes. If |sLen| is -1 then
  411. // the salt length is the same as the hash length. If -2, then the salt length
  412. // is recovered and all values accepted.
  413. //
  414. // If unsure, use -1.
  415. //
  416. // It returns one on success or zero on error.
  417. //
  418. // This function implements only the low-level padding logic. Use
  419. // |RSA_verify_pss_mgf1| instead.
  420. OPENSSL_EXPORT int RSA_verify_PKCS1_PSS_mgf1(const RSA *rsa,
  421. const uint8_t *mHash,
  422. const EVP_MD *Hash,
  423. const EVP_MD *mgf1Hash,
  424. const uint8_t *EM, int sLen);
  425. // RSA_padding_add_PKCS1_PSS_mgf1 writes a PSS padding of |mHash| to |EM|,
  426. // where |mHash| is a digest produced by |Hash|. |RSA_size(rsa)| bytes of
  427. // output will be written to |EM|. The |mgf1Hash| argument specifies the hash
  428. // function for generating the mask. If NULL, |Hash| is used. The |sLen|
  429. // argument specifies the expected salt length in bytes. If |sLen| is -1 then
  430. // the salt length is the same as the hash length. If -2, then the salt length
  431. // is maximal given the space in |EM|.
  432. //
  433. // It returns one on success or zero on error.
  434. //
  435. // This function implements only the low-level padding logic. Use
  436. // |RSA_sign_pss_mgf1| instead.
  437. OPENSSL_EXPORT int RSA_padding_add_PKCS1_PSS_mgf1(const RSA *rsa, uint8_t *EM,
  438. const uint8_t *mHash,
  439. const EVP_MD *Hash,
  440. const EVP_MD *mgf1Hash,
  441. int sLen);
  442. // RSA_padding_add_PKCS1_OAEP_mgf1 writes an OAEP padding of |from| to |to|
  443. // with the given parameters and hash functions. If |md| is NULL then SHA-1 is
  444. // used. If |mgf1md| is NULL then the value of |md| is used (which means SHA-1
  445. // if that, in turn, is NULL).
  446. //
  447. // It returns one on success or zero on error.
  448. OPENSSL_EXPORT int RSA_padding_add_PKCS1_OAEP_mgf1(
  449. uint8_t *to, size_t to_len, const uint8_t *from, size_t from_len,
  450. const uint8_t *param, size_t param_len, const EVP_MD *md,
  451. const EVP_MD *mgf1md);
  452. // RSA_add_pkcs1_prefix builds a version of |digest| prefixed with the
  453. // DigestInfo header for the given hash function and sets |out_msg| to point to
  454. // it. On successful return, if |*is_alloced| is one, the caller must release
  455. // |*out_msg| with |OPENSSL_free|.
  456. OPENSSL_EXPORT int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len,
  457. int *is_alloced, int hash_nid,
  458. const uint8_t *digest,
  459. size_t digest_len);
  460. // ASN.1 functions.
  461. // RSA_parse_public_key parses a DER-encoded RSAPublicKey structure (RFC 8017)
  462. // from |cbs| and advances |cbs|. It returns a newly-allocated |RSA| or NULL on
  463. // error.
  464. OPENSSL_EXPORT RSA *RSA_parse_public_key(CBS *cbs);
  465. // RSA_public_key_from_bytes parses |in| as a DER-encoded RSAPublicKey structure
  466. // (RFC 8017). It returns a newly-allocated |RSA| or NULL on error.
  467. OPENSSL_EXPORT RSA *RSA_public_key_from_bytes(const uint8_t *in, size_t in_len);
  468. // RSA_marshal_public_key marshals |rsa| as a DER-encoded RSAPublicKey structure
  469. // (RFC 8017) and appends the result to |cbb|. It returns one on success and
  470. // zero on failure.
  471. OPENSSL_EXPORT int RSA_marshal_public_key(CBB *cbb, const RSA *rsa);
  472. // RSA_public_key_to_bytes marshals |rsa| as a DER-encoded RSAPublicKey
  473. // structure (RFC 8017) and, on success, sets |*out_bytes| to a newly allocated
  474. // buffer containing the result and returns one. Otherwise, it returns zero. The
  475. // result should be freed with |OPENSSL_free|.
  476. OPENSSL_EXPORT int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
  477. const RSA *rsa);
  478. // RSA_parse_private_key parses a DER-encoded RSAPrivateKey structure (RFC 8017)
  479. // from |cbs| and advances |cbs|. It returns a newly-allocated |RSA| or NULL on
  480. // error.
  481. OPENSSL_EXPORT RSA *RSA_parse_private_key(CBS *cbs);
  482. // RSA_private_key_from_bytes parses |in| as a DER-encoded RSAPrivateKey
  483. // structure (RFC 8017). It returns a newly-allocated |RSA| or NULL on error.
  484. OPENSSL_EXPORT RSA *RSA_private_key_from_bytes(const uint8_t *in,
  485. size_t in_len);
  486. // RSA_marshal_private_key marshals |rsa| as a DER-encoded RSAPrivateKey
  487. // structure (RFC 8017) and appends the result to |cbb|. It returns one on
  488. // success and zero on failure.
  489. OPENSSL_EXPORT int RSA_marshal_private_key(CBB *cbb, const RSA *rsa);
  490. // RSA_private_key_to_bytes marshals |rsa| as a DER-encoded RSAPrivateKey
  491. // structure (RFC 8017) and, on success, sets |*out_bytes| to a newly allocated
  492. // buffer containing the result and returns one. Otherwise, it returns zero. The
  493. // result should be freed with |OPENSSL_free|.
  494. OPENSSL_EXPORT int RSA_private_key_to_bytes(uint8_t **out_bytes,
  495. size_t *out_len, const RSA *rsa);
  496. // ex_data functions.
  497. //
  498. // See |ex_data.h| for details.
  499. OPENSSL_EXPORT int RSA_get_ex_new_index(long argl, void *argp,
  500. CRYPTO_EX_unused *unused,
  501. CRYPTO_EX_dup *dup_unused,
  502. CRYPTO_EX_free *free_func);
  503. OPENSSL_EXPORT int RSA_set_ex_data(RSA *rsa, int idx, void *arg);
  504. OPENSSL_EXPORT void *RSA_get_ex_data(const RSA *rsa, int idx);
  505. // Flags.
  506. // RSA_FLAG_OPAQUE specifies that this RSA_METHOD does not expose its key
  507. // material. This may be set if, for instance, it is wrapping some other crypto
  508. // API, like a platform key store.
  509. #define RSA_FLAG_OPAQUE 1
  510. // RSA_FLAG_NO_BLINDING disables blinding of private operations, which is a
  511. // dangerous thing to do. It is deprecated and should not be used. It will
  512. // be ignored whenever possible.
  513. //
  514. // This flag must be used if a key without the public exponent |e| is used for
  515. // private key operations; avoid using such keys whenever possible.
  516. #define RSA_FLAG_NO_BLINDING 8
  517. // RSA_FLAG_EXT_PKEY is deprecated and ignored.
  518. #define RSA_FLAG_EXT_PKEY 0x20
  519. // RSA public exponent values.
  520. #define RSA_3 0x3
  521. #define RSA_F4 0x10001
  522. // Deprecated functions.
  523. #define RSA_METHOD_FLAG_NO_CHECK RSA_FLAG_OPAQUE
  524. // RSA_flags returns the flags for |rsa|. These are a bitwise OR of |RSA_FLAG_*|
  525. // constants.
  526. OPENSSL_EXPORT int RSA_flags(const RSA *rsa);
  527. // RSA_test_flags returns the subset of flags in |flags| which are set in |rsa|.
  528. OPENSSL_EXPORT int RSA_test_flags(const RSA *rsa, int flags);
  529. // RSA_blinding_on returns one.
  530. OPENSSL_EXPORT int RSA_blinding_on(RSA *rsa, BN_CTX *ctx);
  531. // RSA_generate_key behaves like |RSA_generate_key_ex|, which is what you
  532. // should use instead. It returns NULL on error, or a newly-allocated |RSA| on
  533. // success. This function is provided for compatibility only. The |callback|
  534. // and |cb_arg| parameters must be NULL.
  535. OPENSSL_EXPORT RSA *RSA_generate_key(int bits, unsigned long e, void *callback,
  536. void *cb_arg);
  537. // d2i_RSAPublicKey parses a DER-encoded RSAPublicKey structure (RFC 8017) from
  538. // |len| bytes at |*inp|, as described in |d2i_SAMPLE|.
  539. //
  540. // Use |RSA_parse_public_key| instead.
  541. OPENSSL_EXPORT RSA *d2i_RSAPublicKey(RSA **out, const uint8_t **inp, long len);
  542. // i2d_RSAPublicKey marshals |in| to a DER-encoded RSAPublicKey structure (RFC
  543. // 8017), as described in |i2d_SAMPLE|.
  544. //
  545. // Use |RSA_marshal_public_key| instead.
  546. OPENSSL_EXPORT int i2d_RSAPublicKey(const RSA *in, uint8_t **outp);
  547. // d2i_RSAPrivateKey parses a DER-encoded RSAPrivateKey structure (RFC 8017)
  548. // from |len| bytes at |*inp|, as described in |d2i_SAMPLE|.
  549. //
  550. // Use |RSA_parse_private_key| instead.
  551. OPENSSL_EXPORT RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len);
  552. // i2d_RSAPrivateKey marshals |in| to a DER-encoded RSAPrivateKey structure (RFC
  553. // 8017), as described in |i2d_SAMPLE|.
  554. //
  555. // Use |RSA_marshal_private_key| instead.
  556. OPENSSL_EXPORT int i2d_RSAPrivateKey(const RSA *in, uint8_t **outp);
  557. // RSA_padding_add_PKCS1_PSS acts like |RSA_padding_add_PKCS1_PSS_mgf1| but the
  558. // |mgf1Hash| parameter of the latter is implicitly set to |Hash|.
  559. //
  560. // This function implements only the low-level padding logic. Use
  561. // |RSA_sign_pss_mgf1| instead.
  562. OPENSSL_EXPORT int RSA_padding_add_PKCS1_PSS(const RSA *rsa, uint8_t *EM,
  563. const uint8_t *mHash,
  564. const EVP_MD *Hash, int sLen);
  565. // RSA_verify_PKCS1_PSS acts like |RSA_verify_PKCS1_PSS_mgf1| but the
  566. // |mgf1Hash| parameter of the latter is implicitly set to |Hash|.
  567. //
  568. // This function implements only the low-level padding logic. Use
  569. // |RSA_verify_pss_mgf1| instead.
  570. OPENSSL_EXPORT int RSA_verify_PKCS1_PSS(const RSA *rsa, const uint8_t *mHash,
  571. const EVP_MD *Hash, const uint8_t *EM,
  572. int sLen);
  573. // RSA_padding_add_PKCS1_OAEP acts like |RSA_padding_add_PKCS1_OAEP_mgf1| but
  574. // the |md| and |mgf1md| parameters of the latter are implicitly set to NULL,
  575. // which means SHA-1.
  576. OPENSSL_EXPORT int RSA_padding_add_PKCS1_OAEP(uint8_t *to, size_t to_len,
  577. const uint8_t *from,
  578. size_t from_len,
  579. const uint8_t *param,
  580. size_t param_len);
  581. // RSA_print prints a textual representation of |rsa| to |bio|. It returns one
  582. // on success or zero otherwise.
  583. OPENSSL_EXPORT int RSA_print(BIO *bio, const RSA *rsa, int indent);
  584. // RSA_get0_pss_params returns NULL. In OpenSSL, this function retries RSA-PSS
  585. // parameters associated with |RSA| objects, but BoringSSL does not support
  586. // the id-RSASSA-PSS key encoding.
  587. OPENSSL_EXPORT const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *rsa);
  588. struct rsa_meth_st {
  589. struct openssl_method_common_st common;
  590. void *app_data;
  591. int (*init)(RSA *rsa);
  592. int (*finish)(RSA *rsa);
  593. // size returns the size of the RSA modulus in bytes.
  594. size_t (*size)(const RSA *rsa);
  595. int (*sign)(int type, const uint8_t *m, unsigned int m_length,
  596. uint8_t *sigret, unsigned int *siglen, const RSA *rsa);
  597. // These functions mirror the |RSA_*| functions of the same name.
  598. int (*sign_raw)(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  599. const uint8_t *in, size_t in_len, int padding);
  600. int (*decrypt)(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  601. const uint8_t *in, size_t in_len, int padding);
  602. // private_transform takes a big-endian integer from |in|, calculates the
  603. // d'th power of it, modulo the RSA modulus and writes the result as a
  604. // big-endian integer to |out|. Both |in| and |out| are |len| bytes long and
  605. // |len| is always equal to |RSA_size(rsa)|. If the result of the transform
  606. // can be represented in fewer than |len| bytes, then |out| must be zero
  607. // padded on the left.
  608. //
  609. // It returns one on success and zero otherwise.
  610. //
  611. // RSA decrypt and sign operations will call this, thus an ENGINE might wish
  612. // to override it in order to avoid having to implement the padding
  613. // functionality demanded by those, higher level, operations.
  614. int (*private_transform)(RSA *rsa, uint8_t *out, const uint8_t *in,
  615. size_t len);
  616. int flags;
  617. };
  618. // Private functions.
  619. typedef struct bn_blinding_st BN_BLINDING;
  620. struct rsa_st {
  621. RSA_METHOD *meth;
  622. // Access to the following fields was historically allowed, but
  623. // deprecated. Use |RSA_get0_*| and |RSA_set0_*| instead. Access to all other
  624. // fields is forbidden and will cause threading errors.
  625. BIGNUM *n;
  626. BIGNUM *e;
  627. BIGNUM *d;
  628. BIGNUM *p;
  629. BIGNUM *q;
  630. BIGNUM *dmp1;
  631. BIGNUM *dmq1;
  632. BIGNUM *iqmp;
  633. // be careful using this if the RSA structure is shared
  634. CRYPTO_EX_DATA ex_data;
  635. CRYPTO_refcount_t references;
  636. int flags;
  637. CRYPTO_MUTEX lock;
  638. // Used to cache montgomery values. The creation of these values is protected
  639. // by |lock|.
  640. BN_MONT_CTX *mont_n;
  641. BN_MONT_CTX *mont_p;
  642. BN_MONT_CTX *mont_q;
  643. // The following fields are copies of |d|, |dmp1|, and |dmq1|, respectively,
  644. // but with the correct widths to prevent side channels. These must use
  645. // separate copies due to threading concerns caused by OpenSSL's API
  646. // mistakes. See https://github.com/openssl/openssl/issues/5158 and
  647. // the |freeze_private_key| implementation.
  648. BIGNUM *d_fixed, *dmp1_fixed, *dmq1_fixed;
  649. // inv_small_mod_large_mont is q^-1 mod p in Montgomery form, using |mont_p|,
  650. // if |p| >= |q|. Otherwise, it is p^-1 mod q in Montgomery form, using
  651. // |mont_q|.
  652. BIGNUM *inv_small_mod_large_mont;
  653. // num_blindings contains the size of the |blindings| and |blindings_inuse|
  654. // arrays. This member and the |blindings_inuse| array are protected by
  655. // |lock|.
  656. unsigned num_blindings;
  657. // blindings is an array of BN_BLINDING structures that can be reserved by a
  658. // thread by locking |lock| and changing the corresponding element in
  659. // |blindings_inuse| from 0 to 1.
  660. BN_BLINDING **blindings;
  661. unsigned char *blindings_inuse;
  662. uint64_t blinding_fork_generation;
  663. // private_key_frozen is one if the key has been used for a private key
  664. // operation and may no longer be mutated.
  665. unsigned private_key_frozen:1;
  666. };
  667. #if defined(__cplusplus)
  668. } // extern C
  669. extern "C++" {
  670. BSSL_NAMESPACE_BEGIN
  671. BORINGSSL_MAKE_DELETER(RSA, RSA_free)
  672. BORINGSSL_MAKE_UP_REF(RSA, RSA_up_ref)
  673. BSSL_NAMESPACE_END
  674. } // extern C++
  675. #endif
  676. #define RSA_R_BAD_ENCODING 100
  677. #define RSA_R_BAD_E_VALUE 101
  678. #define RSA_R_BAD_FIXED_HEADER_DECRYPT 102
  679. #define RSA_R_BAD_PAD_BYTE_COUNT 103
  680. #define RSA_R_BAD_RSA_PARAMETERS 104
  681. #define RSA_R_BAD_SIGNATURE 105
  682. #define RSA_R_BAD_VERSION 106
  683. #define RSA_R_BLOCK_TYPE_IS_NOT_01 107
  684. #define RSA_R_BN_NOT_INITIALIZED 108
  685. #define RSA_R_CANNOT_RECOVER_MULTI_PRIME_KEY 109
  686. #define RSA_R_CRT_PARAMS_ALREADY_GIVEN 110
  687. #define RSA_R_CRT_VALUES_INCORRECT 111
  688. #define RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN 112
  689. #define RSA_R_DATA_TOO_LARGE 113
  690. #define RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 114
  691. #define RSA_R_DATA_TOO_LARGE_FOR_MODULUS 115
  692. #define RSA_R_DATA_TOO_SMALL 116
  693. #define RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE 117
  694. #define RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY 118
  695. #define RSA_R_D_E_NOT_CONGRUENT_TO_1 119
  696. #define RSA_R_EMPTY_PUBLIC_KEY 120
  697. #define RSA_R_ENCODE_ERROR 121
  698. #define RSA_R_FIRST_OCTET_INVALID 122
  699. #define RSA_R_INCONSISTENT_SET_OF_CRT_VALUES 123
  700. #define RSA_R_INTERNAL_ERROR 124
  701. #define RSA_R_INVALID_MESSAGE_LENGTH 125
  702. #define RSA_R_KEY_SIZE_TOO_SMALL 126
  703. #define RSA_R_LAST_OCTET_INVALID 127
  704. #define RSA_R_MODULUS_TOO_LARGE 128
  705. #define RSA_R_MUST_HAVE_AT_LEAST_TWO_PRIMES 129
  706. #define RSA_R_NO_PUBLIC_EXPONENT 130
  707. #define RSA_R_NULL_BEFORE_BLOCK_MISSING 131
  708. #define RSA_R_N_NOT_EQUAL_P_Q 132
  709. #define RSA_R_OAEP_DECODING_ERROR 133
  710. #define RSA_R_ONLY_ONE_OF_P_Q_GIVEN 134
  711. #define RSA_R_OUTPUT_BUFFER_TOO_SMALL 135
  712. #define RSA_R_PADDING_CHECK_FAILED 136
  713. #define RSA_R_PKCS_DECODING_ERROR 137
  714. #define RSA_R_SLEN_CHECK_FAILED 138
  715. #define RSA_R_SLEN_RECOVERY_FAILED 139
  716. #define RSA_R_TOO_LONG 140
  717. #define RSA_R_TOO_MANY_ITERATIONS 141
  718. #define RSA_R_UNKNOWN_ALGORITHM_TYPE 142
  719. #define RSA_R_UNKNOWN_PADDING_TYPE 143
  720. #define RSA_R_VALUE_MISSING 144
  721. #define RSA_R_WRONG_SIGNATURE_LENGTH 145
  722. #define RSA_R_PUBLIC_KEY_VALIDATION_FAILED 146
  723. #define RSA_R_D_OUT_OF_RANGE 147
  724. #define RSA_R_BLOCK_TYPE_IS_NOT_02 148
  725. #endif // OPENSSL_HEADER_RSA_H