1 /* Copyright (C) 1995-1998 Eric Young ([email protected]) 2 * All rights reserved. 3 * 4 * This package is an SSL implementation written 5 * by Eric Young ([email protected]). 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 ([email protected]). 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 ([email protected])" 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 ([email protected])" 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 57 #ifndef OPENSSL_HEADER_RSA_H 58 #define OPENSSL_HEADER_RSA_H 59 60 #include <openssl/base.h> 61 62 #include <openssl/engine.h> 63 #include <openssl/ex_data.h> 64 #include <openssl/thread.h> 65 66 #if defined(__cplusplus) 67 extern "C" { 68 #endif 69 70 71 // rsa.h contains functions for handling encryption and signature using RSA. 72 73 74 // Allocation and destruction. 75 // 76 // An |RSA| object represents a public or private RSA key. A given object may be 77 // used concurrently on multiple threads by non-mutating functions, provided no 78 // other thread is concurrently calling a mutating function. Unless otherwise 79 // documented, functions which take a |const| pointer are non-mutating and 80 // functions which take a non-|const| pointer are mutating. 81 82 // RSA_new_public_key returns a new |RSA| object containing a public key with 83 // the specified parameters, or NULL on error or invalid input. 84 OPENSSL_EXPORT RSA *RSA_new_public_key(const BIGNUM *n, const BIGNUM *e); 85 86 // RSA_new_private_key returns a new |RSA| object containing a private key with 87 // the specified parameters, or NULL on error or invalid input. All parameters 88 // are mandatory and may not be NULL. 89 // 90 // This function creates standard RSA private keys with CRT parameters. 91 OPENSSL_EXPORT RSA *RSA_new_private_key(const BIGNUM *n, const BIGNUM *e, 92 const BIGNUM *d, const BIGNUM *p, 93 const BIGNUM *q, const BIGNUM *dmp1, 94 const BIGNUM *dmq1, const BIGNUM *iqmp); 95 96 // RSA_new returns a new, empty |RSA| object or NULL on error. Prefer using 97 // |RSA_new_public_key| or |RSA_new_private_key| to import an RSA key. 98 OPENSSL_EXPORT RSA *RSA_new(void); 99 100 // RSA_new_method acts the same as |RSA_new| but takes an explicit |ENGINE|. 101 OPENSSL_EXPORT RSA *RSA_new_method(const ENGINE *engine); 102 103 // RSA_free decrements the reference count of |rsa| and frees it if the 104 // reference count drops to zero. 105 OPENSSL_EXPORT void RSA_free(RSA *rsa); 106 107 // RSA_up_ref increments the reference count of |rsa| and returns one. It does 108 // not mutate |rsa| for thread-safety purposes and may be used concurrently. 109 OPENSSL_EXPORT int RSA_up_ref(RSA *rsa); 110 111 112 // Properties. 113 114 // RSA_bits returns the size of |rsa|, in bits. 115 OPENSSL_EXPORT unsigned RSA_bits(const RSA *rsa); 116 117 // RSA_get0_n returns |rsa|'s public modulus. 118 OPENSSL_EXPORT const BIGNUM *RSA_get0_n(const RSA *rsa); 119 120 // RSA_get0_e returns |rsa|'s public exponent. 121 OPENSSL_EXPORT const BIGNUM *RSA_get0_e(const RSA *rsa); 122 123 // RSA_get0_d returns |rsa|'s private exponent. If |rsa| is a public key, this 124 // value will be NULL. 125 OPENSSL_EXPORT const BIGNUM *RSA_get0_d(const RSA *rsa); 126 127 // RSA_get0_p returns |rsa|'s first private prime factor. If |rsa| is a public 128 // key or lacks its prime factors, this value will be NULL. 129 OPENSSL_EXPORT const BIGNUM *RSA_get0_p(const RSA *rsa); 130 131 // RSA_get0_q returns |rsa|'s second private prime factor. If |rsa| is a public 132 // key or lacks its prime factors, this value will be NULL. 133 OPENSSL_EXPORT const BIGNUM *RSA_get0_q(const RSA *rsa); 134 135 // RSA_get0_dmp1 returns d (mod p-1) for |rsa|. If |rsa| is a public key or 136 // lacks CRT parameters, this value will be NULL. 137 OPENSSL_EXPORT const BIGNUM *RSA_get0_dmp1(const RSA *rsa); 138 139 // RSA_get0_dmq1 returns d (mod q-1) for |rsa|. If |rsa| is a public key or 140 // lacks CRT parameters, this value will be NULL. 141 OPENSSL_EXPORT const BIGNUM *RSA_get0_dmq1(const RSA *rsa); 142 143 // RSA_get0_iqmp returns q^-1 (mod p). If |rsa| is a public key or lacks CRT 144 // parameters, this value will be NULL. 145 OPENSSL_EXPORT const BIGNUM *RSA_get0_iqmp(const RSA *rsa); 146 147 // RSA_get0_key sets |*out_n|, |*out_e|, and |*out_d|, if non-NULL, to |rsa|'s 148 // modulus, public exponent, and private exponent, respectively. If |rsa| is a 149 // public key, the private exponent will be set to NULL. 150 OPENSSL_EXPORT void RSA_get0_key(const RSA *rsa, const BIGNUM **out_n, 151 const BIGNUM **out_e, const BIGNUM **out_d); 152 153 // RSA_get0_factors sets |*out_p| and |*out_q|, if non-NULL, to |rsa|'s prime 154 // factors. If |rsa| is a public key, they will be set to NULL. 155 OPENSSL_EXPORT void RSA_get0_factors(const RSA *rsa, const BIGNUM **out_p, 156 const BIGNUM **out_q); 157 158 // RSA_get0_crt_params sets |*out_dmp1|, |*out_dmq1|, and |*out_iqmp|, if 159 // non-NULL, to |rsa|'s CRT parameters. These are d (mod p-1), d (mod q-1) and 160 // q^-1 (mod p), respectively. If |rsa| is a public key, each parameter will be 161 // set to NULL. 162 OPENSSL_EXPORT void RSA_get0_crt_params(const RSA *rsa, const BIGNUM **out_dmp1, 163 const BIGNUM **out_dmq1, 164 const BIGNUM **out_iqmp); 165 166 167 // Setting individual properties. 168 // 169 // These functions allow setting individual properties of an |RSA| object. This 170 // is typically used with |RSA_new| to construct an RSA key field by field. 171 // Prefer instead to use |RSA_new_public_key| and |RSA_new_private_key|. These 172 // functions defer some initialization to the first use of an |RSA| object. This 173 // means invalid inputs may be caught late. 174 // 175 // TODO(crbug.com/boringssl/316): This deferred initialization also causes 176 // performance problems in multi-threaded applications. The preferred APIs 177 // currently have the same issues, but they will initialize eagerly in the 178 // future. 179 180 // RSA_set0_key sets |rsa|'s modulus, public exponent, and private exponent to 181 // |n|, |e|, and |d| respectively, if non-NULL. On success, it takes ownership 182 // of each argument and returns one. Otherwise, it returns zero. 183 // 184 // |d| may be NULL, but |n| and |e| must either be non-NULL or already 185 // configured on |rsa|. 186 // 187 // It is an error to call this function after |rsa| has been used for a 188 // cryptographic operation. Construct a new |RSA| object instead. 189 OPENSSL_EXPORT int RSA_set0_key(RSA *rsa, BIGNUM *n, BIGNUM *e, BIGNUM *d); 190 191 // RSA_set0_factors sets |rsa|'s prime factors to |p| and |q|, if non-NULL, and 192 // takes ownership of them. On success, it takes ownership of each argument and 193 // returns one. Otherwise, it returns zero. 194 // 195 // Each argument must either be non-NULL or already configured on |rsa|. 196 // 197 // It is an error to call this function after |rsa| has been used for a 198 // cryptographic operation. Construct a new |RSA| object instead. 199 OPENSSL_EXPORT int RSA_set0_factors(RSA *rsa, BIGNUM *p, BIGNUM *q); 200 201 // RSA_set0_crt_params sets |rsa|'s CRT parameters to |dmp1|, |dmq1|, and 202 // |iqmp|, if non-NULL, and takes ownership of them. On success, it takes 203 // ownership of its parameters and returns one. Otherwise, it returns zero. 204 // 205 // Each argument must either be non-NULL or already configured on |rsa|. 206 // 207 // It is an error to call this function after |rsa| has been used for a 208 // cryptographic operation. Construct a new |RSA| object instead. 209 OPENSSL_EXPORT int RSA_set0_crt_params(RSA *rsa, BIGNUM *dmp1, BIGNUM *dmq1, 210 BIGNUM *iqmp); 211 212 213 // Key generation. 214 215 // RSA_generate_key_ex generates a new RSA key where the modulus has size 216 // |bits| and the public exponent is |e|. If unsure, |RSA_F4| is a good value 217 // for |e|. If |cb| is not NULL then it is called during the key generation 218 // process. In addition to the calls documented for |BN_generate_prime_ex|, it 219 // is called with event=2 when the n'th prime is rejected as unsuitable and 220 // with event=3 when a suitable value for |p| is found. 221 // 222 // It returns one on success or zero on error. 223 OPENSSL_EXPORT int RSA_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e, 224 BN_GENCB *cb); 225 226 // RSA_generate_key_fips behaves like |RSA_generate_key_ex| but performs 227 // additional checks for FIPS compliance. The public exponent is always 65537 228 // and |bits| must be either 2048 or 3072. 229 OPENSSL_EXPORT int RSA_generate_key_fips(RSA *rsa, int bits, BN_GENCB *cb); 230 231 232 // Encryption / Decryption 233 // 234 // These functions are considered non-mutating for thread-safety purposes and 235 // may be used concurrently. 236 237 // RSA_PKCS1_PADDING denotes PKCS#1 v1.5 padding. When used with encryption, 238 // this is RSAES-PKCS1-v1_5. When used with signing, this is RSASSA-PKCS1-v1_5. 239 // 240 // WARNING: The RSAES-PKCS1-v1_5 encryption scheme is vulnerable to a 241 // chosen-ciphertext attack. Decrypting attacker-supplied ciphertext with 242 // RSAES-PKCS1-v1_5 may give the attacker control over your private key. This 243 // does not impact the RSASSA-PKCS1-v1_5 signature scheme. See "Chosen 244 // Ciphertext Attacks Against Protocols Based on the RSA Encryption Standard 245 // PKCS #1", Daniel Bleichenbacher, Advances in Cryptology (Crypto '98). 246 #define RSA_PKCS1_PADDING 1 247 248 // RSA_NO_PADDING denotes a raw RSA operation. 249 #define RSA_NO_PADDING 3 250 251 // RSA_PKCS1_OAEP_PADDING denotes the RSAES-OAEP encryption scheme. 252 #define RSA_PKCS1_OAEP_PADDING 4 253 254 // RSA_PKCS1_PSS_PADDING denotes the RSASSA-PSS signature scheme. This value may 255 // not be passed into |RSA_sign_raw|, only |EVP_PKEY_CTX_set_rsa_padding|. See 256 // also |RSA_sign_pss_mgf1| and |RSA_verify_pss_mgf1|. 257 #define RSA_PKCS1_PSS_PADDING 6 258 259 // RSA_encrypt encrypts |in_len| bytes from |in| to the public key from |rsa| 260 // and writes, at most, |max_out| bytes of encrypted data to |out|. The 261 // |max_out| argument must be, at least, |RSA_size| in order to ensure success. 262 // 263 // It returns 1 on success or zero on error. 264 // 265 // The |padding| argument must be one of the |RSA_*_PADDING| values. If in 266 // doubt, use |RSA_PKCS1_OAEP_PADDING| for new protocols. 267 OPENSSL_EXPORT int RSA_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, 268 size_t max_out, const uint8_t *in, size_t in_len, 269 int padding); 270 271 // RSA_decrypt decrypts |in_len| bytes from |in| with the private key from 272 // |rsa| and writes, at most, |max_out| bytes of plaintext to |out|. The 273 // |max_out| argument must be, at least, |RSA_size| in order to ensure success. 274 // 275 // It returns 1 on success or zero on error. 276 // 277 // The |padding| argument must be one of the |RSA_*_PADDING| values. If in 278 // doubt, use |RSA_PKCS1_OAEP_PADDING| for new protocols. 279 // 280 // WARNING: Passing |RSA_PKCS1_PADDING| into this function is deprecated and 281 // insecure. RSAES-PKCS1-v1_5 is vulnerable to a chosen-ciphertext attack. 282 // Decrypting attacker-supplied ciphertext with RSAES-PKCS1-v1_5 may give the 283 // attacker control over your private key. See "Chosen Ciphertext Attacks 284 // Against Protocols Based on the RSA Encryption Standard PKCS #1", Daniel 285 // Bleichenbacher, Advances in Cryptology (Crypto '98). 286 // 287 // In some limited cases, such as TLS RSA key exchange, it is possible to 288 // mitigate this flaw with custom, protocol-specific padding logic. This 289 // should be implemented with |RSA_NO_PADDING|, not |RSA_PKCS1_PADDING|. 290 OPENSSL_EXPORT int RSA_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, 291 size_t max_out, const uint8_t *in, size_t in_len, 292 int padding); 293 294 // RSA_public_encrypt encrypts |flen| bytes from |from| to the public key in 295 // |rsa| and writes the encrypted data to |to|. The |to| buffer must have at 296 // least |RSA_size| bytes of space. It returns the number of bytes written, or 297 // -1 on error. The |padding| argument must be one of the |RSA_*_PADDING| 298 // values. If in doubt, use |RSA_PKCS1_OAEP_PADDING| for new protocols. 299 // 300 // WARNING: this function is dangerous because it breaks the usual return value 301 // convention. Use |RSA_encrypt| instead. 302 OPENSSL_EXPORT int RSA_public_encrypt(size_t flen, const uint8_t *from, 303 uint8_t *to, RSA *rsa, int padding); 304 305 // RSA_private_decrypt decrypts |flen| bytes from |from| with the public key in 306 // |rsa| and writes the plaintext to |to|. The |to| buffer must have at least 307 // |RSA_size| bytes of space. It returns the number of bytes written, or -1 on 308 // error. The |padding| argument must be one of the |RSA_*_PADDING| values. If 309 // in doubt, use |RSA_PKCS1_OAEP_PADDING| for new protocols. Passing 310 // |RSA_PKCS1_PADDING| into this function is deprecated and insecure. See 311 // |RSA_decrypt|. 312 // 313 // WARNING: this function is dangerous because it breaks the usual return value 314 // convention. Use |RSA_decrypt| instead. 315 OPENSSL_EXPORT int RSA_private_decrypt(size_t flen, const uint8_t *from, 316 uint8_t *to, RSA *rsa, int padding); 317 318 319 // Signing / Verification 320 // 321 // These functions are considered non-mutating for thread-safety purposes and 322 // may be used concurrently. 323 324 // RSA_sign signs |digest_len| bytes of digest from |digest| with |rsa| using 325 // RSASSA-PKCS1-v1_5. It writes, at most, |RSA_size(rsa)| bytes to |out|. On 326 // successful return, the actual number of bytes written is written to 327 // |*out_len|. 328 // 329 // The |hash_nid| argument identifies the hash function used to calculate 330 // |digest| and is embedded in the resulting signature. For example, it might be 331 // |NID_sha256|. 332 // 333 // It returns 1 on success and zero on error. 334 // 335 // WARNING: |digest| must be the result of hashing the data to be signed with 336 // |hash_nid|. Passing unhashed inputs will not result in a secure signature 337 // scheme. 338 OPENSSL_EXPORT int RSA_sign(int hash_nid, const uint8_t *digest, 339 size_t digest_len, uint8_t *out, unsigned *out_len, 340 RSA *rsa); 341 342 // RSA_sign_pss_mgf1 signs |digest_len| bytes from |digest| with the public key 343 // from |rsa| using RSASSA-PSS with MGF1 as the mask generation function. It 344 // writes, at most, |max_out| bytes of signature data to |out|. The |max_out| 345 // argument must be, at least, |RSA_size| in order to ensure success. It returns 346 // 1 on success or zero on error. 347 // 348 // The |md| and |mgf1_md| arguments identify the hash used to calculate |digest| 349 // and the MGF1 hash, respectively. If |mgf1_md| is NULL, |md| is 350 // used. 351 // 352 // |salt_len| specifies the expected salt length in bytes. If |salt_len| is -1, 353 // then the salt length is the same as the hash length. If -2, then the salt 354 // length is maximal given the size of |rsa|. If unsure, use -1. 355 // 356 // WARNING: |digest| must be the result of hashing the data to be signed with 357 // |md|. Passing unhashed inputs will not result in a secure signature scheme. 358 OPENSSL_EXPORT int RSA_sign_pss_mgf1(RSA *rsa, size_t *out_len, uint8_t *out, 359 size_t max_out, const uint8_t *digest, 360 size_t digest_len, const EVP_MD *md, 361 const EVP_MD *mgf1_md, int salt_len); 362 363 // RSA_sign_raw performs the private key portion of computing a signature with 364 // |rsa|. It writes, at most, |max_out| bytes of signature data to |out|. The 365 // |max_out| argument must be, at least, |RSA_size| in order to ensure the 366 // output fits. It returns 1 on success or zero on error. 367 // 368 // If |padding| is |RSA_PKCS1_PADDING|, this function wraps |in| with the 369 // padding portion of RSASSA-PKCS1-v1_5 and then performs the raw private key 370 // operation. The caller is responsible for hashing the input and wrapping it in 371 // a DigestInfo structure. 372 // 373 // If |padding| is |RSA_NO_PADDING|, this function only performs the raw private 374 // key operation, interpreting |in| as a integer modulo n. The caller is 375 // responsible for hashing the input and encoding it for the signature scheme 376 // being implemented. 377 // 378 // WARNING: This function is a building block for a signature scheme, not a 379 // complete one. |in| must be the result of hashing and encoding the data as 380 // needed for the scheme being implemented. Passing in arbitrary inputs will not 381 // result in a secure signature scheme. 382 OPENSSL_EXPORT int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, 383 size_t max_out, const uint8_t *in, 384 size_t in_len, int padding); 385 386 // RSA_verify verifies that |sig_len| bytes from |sig| are a valid, 387 // RSASSA-PKCS1-v1_5 signature of |digest_len| bytes at |digest| by |rsa|. 388 // 389 // The |hash_nid| argument identifies the hash function used to calculate 390 // |digest| and is embedded in the resulting signature in order to prevent hash 391 // confusion attacks. For example, it might be |NID_sha256|. 392 // 393 // It returns one if the signature is valid and zero otherwise. 394 // 395 // WARNING: this differs from the original, OpenSSL function which additionally 396 // returned -1 on error. 397 // 398 // WARNING: |digest| must be the result of hashing the data to be verified with 399 // |hash_nid|. Passing unhashed input will not result in a secure signature 400 // scheme. 401 OPENSSL_EXPORT int RSA_verify(int hash_nid, const uint8_t *digest, 402 size_t digest_len, const uint8_t *sig, 403 size_t sig_len, RSA *rsa); 404 405 // RSA_verify_pss_mgf1 verifies that |sig_len| bytes from |sig| are a valid, 406 // RSASSA-PSS signature of |digest_len| bytes at |digest| by |rsa|. It returns 407 // one if the signature is valid and zero otherwise. MGF1 is used as the mask 408 // generation function. 409 // 410 // The |md| and |mgf1_md| arguments identify the hash used to calculate |digest| 411 // and the MGF1 hash, respectively. If |mgf1_md| is NULL, |md| is 412 // used. |salt_len| specifies the expected salt length in bytes. 413 // 414 // If |salt_len| is -1, then the salt length is the same as the hash length. If 415 // -2, then the salt length is recovered and all values accepted. If unsure, use 416 // -1. 417 // 418 // WARNING: |digest| must be the result of hashing the data to be verified with 419 // |md|. Passing unhashed input will not result in a secure signature scheme. 420 OPENSSL_EXPORT int RSA_verify_pss_mgf1(RSA *rsa, const uint8_t *digest, 421 size_t digest_len, const EVP_MD *md, 422 const EVP_MD *mgf1_md, int salt_len, 423 const uint8_t *sig, size_t sig_len); 424 425 // RSA_verify_raw performs the public key portion of verifying |in_len| bytes of 426 // signature from |in| using the public key from |rsa|. On success, it returns 427 // one and writes, at most, |max_out| bytes of output to |out|. The |max_out| 428 // argument must be, at least, |RSA_size| in order to ensure the output fits. On 429 // failure or invalid input, it returns zero. 430 // 431 // If |padding| is |RSA_PKCS1_PADDING|, this function checks the padding portion 432 // of RSASSA-PKCS1-v1_5 and outputs the remainder of the encoded digest. The 433 // caller is responsible for checking the output is a DigestInfo-wrapped digest 434 // of the message. 435 // 436 // If |padding| is |RSA_NO_PADDING|, this function only performs the raw public 437 // key operation. The caller is responsible for checking the output is a valid 438 // result for the signature scheme being implemented. 439 // 440 // WARNING: This function is a building block for a signature scheme, not a 441 // complete one. Checking for arbitrary strings in |out| will not result in a 442 // secure signature scheme. 443 OPENSSL_EXPORT int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, 444 size_t max_out, const uint8_t *in, 445 size_t in_len, int padding); 446 447 // RSA_private_encrypt performs the private key portion of computing a signature 448 // with |rsa|. It takes |flen| bytes from |from| as input and writes the result 449 // to |to|. The |to| buffer must have at least |RSA_size| bytes of space. It 450 // returns the number of bytes written, or -1 on error. 451 // 452 // For the interpretation of |padding| and the input, see |RSA_sign_raw|. 453 // 454 // WARNING: This function is a building block for a signature scheme, not a 455 // complete one. See |RSA_sign_raw| for details. 456 // 457 // WARNING: This function is dangerous because it breaks the usual return value 458 // convention. Use |RSA_sign_raw| instead. 459 OPENSSL_EXPORT int RSA_private_encrypt(size_t flen, const uint8_t *from, 460 uint8_t *to, RSA *rsa, int padding); 461 462 // RSA_public_decrypt performs the public key portion of verifying |flen| bytes 463 // of signature from |from| using the public key from |rsa|. It writes the 464 // result to |to|, which must have at least |RSA_size| bytes of space. It 465 // returns the number of bytes written, or -1 on error. 466 // 467 // For the interpretation of |padding| and the result, see |RSA_verify_raw|. 468 // 469 // WARNING: This function is a building block for a signature scheme, not a 470 // complete one. See |RSA_verify_raw| for details. 471 // 472 // WARNING: This function is dangerous because it breaks the usual return value 473 // convention. Use |RSA_verify_raw| instead. 474 OPENSSL_EXPORT int RSA_public_decrypt(size_t flen, const uint8_t *from, 475 uint8_t *to, RSA *rsa, int padding); 476 477 478 // Utility functions. 479 480 // RSA_size returns the number of bytes in the modulus, which is also the size 481 // of a signature or encrypted value using |rsa|. 482 OPENSSL_EXPORT unsigned RSA_size(const RSA *rsa); 483 484 // RSA_is_opaque returns one if |rsa| is opaque and doesn't expose its key 485 // material. Otherwise it returns zero. 486 OPENSSL_EXPORT int RSA_is_opaque(const RSA *rsa); 487 488 // RSAPublicKey_dup allocates a fresh |RSA| and copies the public key from 489 // |rsa| into it. It returns the fresh |RSA| object, or NULL on error. 490 OPENSSL_EXPORT RSA *RSAPublicKey_dup(const RSA *rsa); 491 492 // RSAPrivateKey_dup allocates a fresh |RSA| and copies the private key from 493 // |rsa| into it. It returns the fresh |RSA| object, or NULL on error. 494 OPENSSL_EXPORT RSA *RSAPrivateKey_dup(const RSA *rsa); 495 496 // RSA_check_key performs basic validity tests on |rsa|. It returns one if 497 // they pass and zero otherwise. Opaque keys and public keys always pass. If it 498 // returns zero then a more detailed error is available on the error queue. 499 OPENSSL_EXPORT int RSA_check_key(const RSA *rsa); 500 501 // RSA_check_fips performs public key validity tests on |key|. It returns one if 502 // they pass and zero otherwise. Opaque keys always fail. This function does not 503 // mutate |rsa| for thread-safety purposes and may be used concurrently. 504 OPENSSL_EXPORT int RSA_check_fips(RSA *key); 505 506 // RSA_verify_PKCS1_PSS_mgf1 verifies that |EM| is a correct PSS padding of 507 // |mHash|, where |mHash| is a digest produced by |Hash|. |EM| must point to 508 // exactly |RSA_size(rsa)| bytes of data. The |mgf1Hash| argument specifies the 509 // hash function for generating the mask. If NULL, |Hash| is used. The |sLen| 510 // argument specifies the expected salt length in bytes. If |sLen| is -1 then 511 // the salt length is the same as the hash length. If -2, then the salt length 512 // is recovered and all values accepted. 513 // 514 // If unsure, use -1. 515 // 516 // It returns one on success or zero on error. 517 // 518 // This function implements only the low-level padding logic. Use 519 // |RSA_verify_pss_mgf1| instead. 520 OPENSSL_EXPORT int RSA_verify_PKCS1_PSS_mgf1(const RSA *rsa, 521 const uint8_t *mHash, 522 const EVP_MD *Hash, 523 const EVP_MD *mgf1Hash, 524 const uint8_t *EM, int sLen); 525 526 // RSA_padding_add_PKCS1_PSS_mgf1 writes a PSS padding of |mHash| to |EM|, 527 // where |mHash| is a digest produced by |Hash|. |RSA_size(rsa)| bytes of 528 // output will be written to |EM|. The |mgf1Hash| argument specifies the hash 529 // function for generating the mask. If NULL, |Hash| is used. The |sLen| 530 // argument specifies the expected salt length in bytes. If |sLen| is -1 then 531 // the salt length is the same as the hash length. If -2, then the salt length 532 // is maximal given the space in |EM|. 533 // 534 // It returns one on success or zero on error. 535 // 536 // This function implements only the low-level padding logic. Use 537 // |RSA_sign_pss_mgf1| instead. 538 OPENSSL_EXPORT int RSA_padding_add_PKCS1_PSS_mgf1(const RSA *rsa, uint8_t *EM, 539 const uint8_t *mHash, 540 const EVP_MD *Hash, 541 const EVP_MD *mgf1Hash, 542 int sLen); 543 544 // RSA_padding_add_PKCS1_OAEP_mgf1 writes an OAEP padding of |from| to |to| 545 // with the given parameters and hash functions. If |md| is NULL then SHA-1 is 546 // used. If |mgf1md| is NULL then the value of |md| is used (which means SHA-1 547 // if that, in turn, is NULL). 548 // 549 // It returns one on success or zero on error. 550 OPENSSL_EXPORT int RSA_padding_add_PKCS1_OAEP_mgf1( 551 uint8_t *to, size_t to_len, const uint8_t *from, size_t from_len, 552 const uint8_t *param, size_t param_len, const EVP_MD *md, 553 const EVP_MD *mgf1md); 554 555 // RSA_add_pkcs1_prefix builds a version of |digest| prefixed with the 556 // DigestInfo header for the given hash function and sets |out_msg| to point to 557 // it. On successful return, if |*is_alloced| is one, the caller must release 558 // |*out_msg| with |OPENSSL_free|. 559 OPENSSL_EXPORT int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len, 560 int *is_alloced, int hash_nid, 561 const uint8_t *digest, 562 size_t digest_len); 563 564 565 // ASN.1 functions. 566 567 // RSA_parse_public_key parses a DER-encoded RSAPublicKey structure (RFC 8017) 568 // from |cbs| and advances |cbs|. It returns a newly-allocated |RSA| or NULL on 569 // error. 570 OPENSSL_EXPORT RSA *RSA_parse_public_key(CBS *cbs); 571 572 // RSA_public_key_from_bytes parses |in| as a DER-encoded RSAPublicKey structure 573 // (RFC 8017). It returns a newly-allocated |RSA| or NULL on error. 574 OPENSSL_EXPORT RSA *RSA_public_key_from_bytes(const uint8_t *in, size_t in_len); 575 576 // RSA_marshal_public_key marshals |rsa| as a DER-encoded RSAPublicKey structure 577 // (RFC 8017) and appends the result to |cbb|. It returns one on success and 578 // zero on failure. 579 OPENSSL_EXPORT int RSA_marshal_public_key(CBB *cbb, const RSA *rsa); 580 581 // RSA_public_key_to_bytes marshals |rsa| as a DER-encoded RSAPublicKey 582 // structure (RFC 8017) and, on success, sets |*out_bytes| to a newly allocated 583 // buffer containing the result and returns one. Otherwise, it returns zero. The 584 // result should be freed with |OPENSSL_free|. 585 OPENSSL_EXPORT int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len, 586 const RSA *rsa); 587 588 // RSA_parse_private_key parses a DER-encoded RSAPrivateKey structure (RFC 8017) 589 // from |cbs| and advances |cbs|. It returns a newly-allocated |RSA| or NULL on 590 // error. 591 OPENSSL_EXPORT RSA *RSA_parse_private_key(CBS *cbs); 592 593 // RSA_private_key_from_bytes parses |in| as a DER-encoded RSAPrivateKey 594 // structure (RFC 8017). It returns a newly-allocated |RSA| or NULL on error. 595 OPENSSL_EXPORT RSA *RSA_private_key_from_bytes(const uint8_t *in, 596 size_t in_len); 597 598 // RSA_marshal_private_key marshals |rsa| as a DER-encoded RSAPrivateKey 599 // structure (RFC 8017) and appends the result to |cbb|. It returns one on 600 // success and zero on failure. 601 OPENSSL_EXPORT int RSA_marshal_private_key(CBB *cbb, const RSA *rsa); 602 603 // RSA_private_key_to_bytes marshals |rsa| as a DER-encoded RSAPrivateKey 604 // structure (RFC 8017) and, on success, sets |*out_bytes| to a newly allocated 605 // buffer containing the result and returns one. Otherwise, it returns zero. The 606 // result should be freed with |OPENSSL_free|. 607 OPENSSL_EXPORT int RSA_private_key_to_bytes(uint8_t **out_bytes, 608 size_t *out_len, const RSA *rsa); 609 610 611 // Obscure RSA variants. 612 // 613 // These functions allow creating RSA keys with obscure combinations of 614 // parameters. 615 616 // RSA_new_private_key_no_crt behaves like |RSA_new_private_key| but constructs 617 // an RSA key without CRT coefficients. 618 // 619 // Keys created by this function will be less performant and cannot be 620 // serialized. 621 OPENSSL_EXPORT RSA *RSA_new_private_key_no_crt(const BIGNUM *n, const BIGNUM *e, 622 const BIGNUM *d); 623 624 // RSA_new_private_key_no_e behaves like |RSA_new_private_key| but constructs an 625 // RSA key without CRT parameters or public exponent. 626 // 627 // Keys created by this function will be less performant, cannot be serialized, 628 // and lack hardening measures that protect against side channels and fault 629 // attacks. 630 OPENSSL_EXPORT RSA *RSA_new_private_key_no_e(const BIGNUM *n, const BIGNUM *d); 631 632 // RSA_new_public_key_large_e behaves like |RSA_new_public_key| but allows any 633 // |e| up to |n|. 634 // 635 // BoringSSL typically bounds public exponents as a denial-of-service 636 // mitigation. Keys created by this function may perform worse than those 637 // created by |RSA_new_public_key|. 638 OPENSSL_EXPORT RSA *RSA_new_public_key_large_e(const BIGNUM *n, 639 const BIGNUM *e); 640 641 // RSA_new_private_key_large_e behaves like |RSA_new_private_key| but allows any 642 // |e| up to |n|. 643 // 644 // BoringSSL typically bounds public exponents as a denial-of-service 645 // mitigation. Keys created by this function may perform worse than those 646 // created by |RSA_new_private_key|. 647 OPENSSL_EXPORT RSA *RSA_new_private_key_large_e( 648 const BIGNUM *n, const BIGNUM *e, const BIGNUM *d, const BIGNUM *p, 649 const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1, 650 const BIGNUM *iqmp); 651 652 653 // ex_data functions. 654 // 655 // See |ex_data.h| for details. 656 657 OPENSSL_EXPORT int RSA_get_ex_new_index(long argl, void *argp, 658 CRYPTO_EX_unused *unused, 659 CRYPTO_EX_dup *dup_unused, 660 CRYPTO_EX_free *free_func); 661 OPENSSL_EXPORT int RSA_set_ex_data(RSA *rsa, int idx, void *arg); 662 OPENSSL_EXPORT void *RSA_get_ex_data(const RSA *rsa, int idx); 663 664 665 // Flags. 666 667 // RSA_FLAG_OPAQUE specifies that this RSA_METHOD does not expose its key 668 // material. This may be set if, for instance, it is wrapping some other crypto 669 // API, like a platform key store. 670 #define RSA_FLAG_OPAQUE 1 671 672 // RSA_FLAG_NO_BLINDING disables blinding of private operations, which is a 673 // dangerous thing to do. It is deprecated and should not be used. It will 674 // be ignored whenever possible. 675 // 676 // This flag must be used if a key without the public exponent |e| is used for 677 // private key operations; avoid using such keys whenever possible. 678 #define RSA_FLAG_NO_BLINDING 8 679 680 // RSA_FLAG_EXT_PKEY is deprecated and ignored. 681 #define RSA_FLAG_EXT_PKEY 0x20 682 683 // RSA_FLAG_NO_PUBLIC_EXPONENT indicates that private keys without a public 684 // exponent are allowed. This is an internal constant. Use 685 // |RSA_new_private_key_no_e| to construct such keys. 686 #define RSA_FLAG_NO_PUBLIC_EXPONENT 0x40 687 688 // RSA_FLAG_LARGE_PUBLIC_EXPONENT indicates that keys with a large public 689 // exponent are allowed. This is an internal constant. Use 690 // |RSA_new_public_key_large_e| and |RSA_new_private_key_large_e| to construct 691 // such keys. 692 #define RSA_FLAG_LARGE_PUBLIC_EXPONENT 0x80 693 694 695 // RSA public exponent values. 696 697 #define RSA_3 0x3 698 #define RSA_F4 0x10001 699 700 701 // Deprecated functions. 702 703 #define RSA_METHOD_FLAG_NO_CHECK RSA_FLAG_OPAQUE 704 705 // RSA_flags returns the flags for |rsa|. These are a bitwise OR of |RSA_FLAG_*| 706 // constants. 707 OPENSSL_EXPORT int RSA_flags(const RSA *rsa); 708 709 // RSA_test_flags returns the subset of flags in |flags| which are set in |rsa|. 710 OPENSSL_EXPORT int RSA_test_flags(const RSA *rsa, int flags); 711 712 // RSA_blinding_on returns one. 713 OPENSSL_EXPORT int RSA_blinding_on(RSA *rsa, BN_CTX *ctx); 714 715 // RSA_generate_key behaves like |RSA_generate_key_ex|, which is what you 716 // should use instead. It returns NULL on error, or a newly-allocated |RSA| on 717 // success. This function is provided for compatibility only. The |callback| 718 // and |cb_arg| parameters must be NULL. 719 OPENSSL_EXPORT RSA *RSA_generate_key(int bits, uint64_t e, void *callback, 720 void *cb_arg); 721 722 // d2i_RSAPublicKey parses a DER-encoded RSAPublicKey structure (RFC 8017) from 723 // |len| bytes at |*inp|, as described in |d2i_SAMPLE|. 724 // 725 // Use |RSA_parse_public_key| instead. 726 OPENSSL_EXPORT RSA *d2i_RSAPublicKey(RSA **out, const uint8_t **inp, long len); 727 728 // i2d_RSAPublicKey marshals |in| to a DER-encoded RSAPublicKey structure (RFC 729 // 8017), as described in |i2d_SAMPLE|. 730 // 731 // Use |RSA_marshal_public_key| instead. 732 OPENSSL_EXPORT int i2d_RSAPublicKey(const RSA *in, uint8_t **outp); 733 734 // d2i_RSAPrivateKey parses a DER-encoded RSAPrivateKey structure (RFC 8017) 735 // from |len| bytes at |*inp|, as described in |d2i_SAMPLE|. 736 // 737 // Use |RSA_parse_private_key| instead. 738 OPENSSL_EXPORT RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len); 739 740 // i2d_RSAPrivateKey marshals |in| to a DER-encoded RSAPrivateKey structure (RFC 741 // 8017), as described in |i2d_SAMPLE|. 742 // 743 // Use |RSA_marshal_private_key| instead. 744 OPENSSL_EXPORT int i2d_RSAPrivateKey(const RSA *in, uint8_t **outp); 745 746 // RSA_padding_add_PKCS1_PSS acts like |RSA_padding_add_PKCS1_PSS_mgf1| but the 747 // |mgf1Hash| parameter of the latter is implicitly set to |Hash|. 748 // 749 // This function implements only the low-level padding logic. Use 750 // |RSA_sign_pss_mgf1| instead. 751 OPENSSL_EXPORT int RSA_padding_add_PKCS1_PSS(const RSA *rsa, uint8_t *EM, 752 const uint8_t *mHash, 753 const EVP_MD *Hash, int sLen); 754 755 // RSA_verify_PKCS1_PSS acts like |RSA_verify_PKCS1_PSS_mgf1| but the 756 // |mgf1Hash| parameter of the latter is implicitly set to |Hash|. 757 // 758 // This function implements only the low-level padding logic. Use 759 // |RSA_verify_pss_mgf1| instead. 760 OPENSSL_EXPORT int RSA_verify_PKCS1_PSS(const RSA *rsa, const uint8_t *mHash, 761 const EVP_MD *Hash, const uint8_t *EM, 762 int sLen); 763 764 // RSA_padding_add_PKCS1_OAEP acts like |RSA_padding_add_PKCS1_OAEP_mgf1| but 765 // the |md| and |mgf1md| parameters of the latter are implicitly set to NULL, 766 // which means SHA-1. 767 OPENSSL_EXPORT int RSA_padding_add_PKCS1_OAEP(uint8_t *to, size_t to_len, 768 const uint8_t *from, 769 size_t from_len, 770 const uint8_t *param, 771 size_t param_len); 772 773 // RSA_print prints a textual representation of |rsa| to |bio|. It returns one 774 // on success or zero otherwise. 775 OPENSSL_EXPORT int RSA_print(BIO *bio, const RSA *rsa, int indent); 776 777 // RSA_get0_pss_params returns NULL. In OpenSSL, this function retries RSA-PSS 778 // parameters associated with |RSA| objects, but BoringSSL does not support 779 // the id-RSASSA-PSS key encoding. 780 OPENSSL_EXPORT const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *rsa); 781 782 // RSA_new_method_no_e returns a newly-allocated |RSA| object backed by 783 // |engine|, with a public modulus of |n| and no known public exponent. 784 // 785 // Do not use this function. It exists only to support Conscrypt, whose use 786 // should be replaced with a more sound mechanism. See 787 // https://crbug.com/boringssl/602. 788 OPENSSL_EXPORT RSA *RSA_new_method_no_e(const ENGINE *engine, const BIGNUM *n); 789 790 791 struct rsa_meth_st { 792 struct openssl_method_common_st common; 793 794 void *app_data; 795 796 int (*init)(RSA *rsa); 797 int (*finish)(RSA *rsa); 798 799 // size returns the size of the RSA modulus in bytes. 800 size_t (*size)(const RSA *rsa); 801 802 int (*sign)(int type, const uint8_t *m, unsigned int m_length, 803 uint8_t *sigret, unsigned int *siglen, const RSA *rsa); 804 805 // These functions mirror the |RSA_*| functions of the same name. 806 int (*sign_raw)(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, 807 const uint8_t *in, size_t in_len, int padding); 808 int (*decrypt)(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, 809 const uint8_t *in, size_t in_len, int padding); 810 811 // private_transform takes a big-endian integer from |in|, calculates the 812 // d'th power of it, modulo the RSA modulus and writes the result as a 813 // big-endian integer to |out|. Both |in| and |out| are |len| bytes long and 814 // |len| is always equal to |RSA_size(rsa)|. If the result of the transform 815 // can be represented in fewer than |len| bytes, then |out| must be zero 816 // padded on the left. 817 // 818 // It returns one on success and zero otherwise. 819 // 820 // RSA decrypt and sign operations will call this, thus an ENGINE might wish 821 // to override it in order to avoid having to implement the padding 822 // functionality demanded by those, higher level, operations. 823 int (*private_transform)(RSA *rsa, uint8_t *out, const uint8_t *in, 824 size_t len); 825 826 int flags; 827 }; 828 829 830 #if defined(__cplusplus) 831 } // extern C 832 833 extern "C++" { 834 835 BSSL_NAMESPACE_BEGIN 836 837 BORINGSSL_MAKE_DELETER(RSA, RSA_free) 838 BORINGSSL_MAKE_UP_REF(RSA, RSA_up_ref) 839 840 BSSL_NAMESPACE_END 841 842 } // extern C++ 843 844 #endif 845 846 #define RSA_R_BAD_ENCODING 100 847 #define RSA_R_BAD_E_VALUE 101 848 #define RSA_R_BAD_FIXED_HEADER_DECRYPT 102 849 #define RSA_R_BAD_PAD_BYTE_COUNT 103 850 #define RSA_R_BAD_RSA_PARAMETERS 104 851 #define RSA_R_BAD_SIGNATURE 105 852 #define RSA_R_BAD_VERSION 106 853 #define RSA_R_BLOCK_TYPE_IS_NOT_01 107 854 #define RSA_R_BN_NOT_INITIALIZED 108 855 #define RSA_R_CANNOT_RECOVER_MULTI_PRIME_KEY 109 856 #define RSA_R_CRT_PARAMS_ALREADY_GIVEN 110 857 #define RSA_R_CRT_VALUES_INCORRECT 111 858 #define RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN 112 859 #define RSA_R_DATA_TOO_LARGE 113 860 #define RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 114 861 #define RSA_R_DATA_TOO_LARGE_FOR_MODULUS 115 862 #define RSA_R_DATA_TOO_SMALL 116 863 #define RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE 117 864 #define RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY 118 865 #define RSA_R_D_E_NOT_CONGRUENT_TO_1 119 866 #define RSA_R_EMPTY_PUBLIC_KEY 120 867 #define RSA_R_ENCODE_ERROR 121 868 #define RSA_R_FIRST_OCTET_INVALID 122 869 #define RSA_R_INCONSISTENT_SET_OF_CRT_VALUES 123 870 #define RSA_R_INTERNAL_ERROR 124 871 #define RSA_R_INVALID_MESSAGE_LENGTH 125 872 #define RSA_R_KEY_SIZE_TOO_SMALL 126 873 #define RSA_R_LAST_OCTET_INVALID 127 874 #define RSA_R_MODULUS_TOO_LARGE 128 875 #define RSA_R_MUST_HAVE_AT_LEAST_TWO_PRIMES 129 876 #define RSA_R_NO_PUBLIC_EXPONENT 130 877 #define RSA_R_NULL_BEFORE_BLOCK_MISSING 131 878 #define RSA_R_N_NOT_EQUAL_P_Q 132 879 #define RSA_R_OAEP_DECODING_ERROR 133 880 #define RSA_R_ONLY_ONE_OF_P_Q_GIVEN 134 881 #define RSA_R_OUTPUT_BUFFER_TOO_SMALL 135 882 #define RSA_R_PADDING_CHECK_FAILED 136 883 #define RSA_R_PKCS_DECODING_ERROR 137 884 #define RSA_R_SLEN_CHECK_FAILED 138 885 #define RSA_R_SLEN_RECOVERY_FAILED 139 886 #define RSA_R_TOO_LONG 140 887 #define RSA_R_TOO_MANY_ITERATIONS 141 888 #define RSA_R_UNKNOWN_ALGORITHM_TYPE 142 889 #define RSA_R_UNKNOWN_PADDING_TYPE 143 890 #define RSA_R_VALUE_MISSING 144 891 #define RSA_R_WRONG_SIGNATURE_LENGTH 145 892 #define RSA_R_PUBLIC_KEY_VALIDATION_FAILED 146 893 #define RSA_R_D_OUT_OF_RANGE 147 894 #define RSA_R_BLOCK_TYPE_IS_NOT_02 148 895 896 #endif // OPENSSL_HEADER_RSA_H 897