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