2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/crypto.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/lhash.h>
14 #include "internal/bn_int.h"
15 #include <openssl/rand.h>
18 int RSA_bits(const RSA *r)
20 return (BN_num_bits(r->n));
23 int RSA_size(const RSA *r)
25 return (BN_num_bytes(r->n));
28 int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
29 RSA *rsa, int padding)
31 return (rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding));
34 int RSA_private_encrypt(int flen, const unsigned char *from,
35 unsigned char *to, RSA *rsa, int padding)
37 return (rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding));
40 int RSA_private_decrypt(int flen, const unsigned char *from,
41 unsigned char *to, RSA *rsa, int padding)
43 return (rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding));
46 int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to,
47 RSA *rsa, int padding)
49 return (rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding));
52 int RSA_flags(const RSA *r)
54 return ((r == NULL) ? 0 : r->meth->flags);
57 void RSA_blinding_off(RSA *rsa)
59 BN_BLINDING_free(rsa->blinding);
61 rsa->flags &= ~RSA_FLAG_BLINDING;
62 rsa->flags |= RSA_FLAG_NO_BLINDING;
65 int RSA_blinding_on(RSA *rsa, BN_CTX *ctx)
69 if (rsa->blinding != NULL)
70 RSA_blinding_off(rsa);
72 rsa->blinding = RSA_setup_blinding(rsa, ctx);
73 if (rsa->blinding == NULL)
76 rsa->flags |= RSA_FLAG_BLINDING;
77 rsa->flags &= ~RSA_FLAG_NO_BLINDING;
83 static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p,
84 const BIGNUM *q, BN_CTX *ctx)
86 BIGNUM *ret = NULL, *r0, *r1, *r2;
88 if (d == NULL || p == NULL || q == NULL)
98 if (!BN_sub(r1, p, BN_value_one()))
100 if (!BN_sub(r2, q, BN_value_one()))
102 if (!BN_mul(r0, r1, r2, ctx))
105 ret = BN_mod_inverse(NULL, d, r0, ctx);
111 BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx)
115 BN_BLINDING *ret = NULL;
117 if (in_ctx == NULL) {
118 if ((ctx = BN_CTX_new()) == NULL)
126 RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_MALLOC_FAILURE);
130 if (rsa->e == NULL) {
131 e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx);
133 RSAerr(RSA_F_RSA_SETUP_BLINDING, RSA_R_NO_PUBLIC_EXPONENT);
139 if ((RAND_status() == 0) && rsa->d != NULL
140 && bn_get_words(rsa->d) != NULL) {
142 * if PRNG is not properly seeded, resort to secret exponent as
145 RAND_add(bn_get_words(rsa->d), bn_get_dmax(rsa->d) * sizeof(BN_ULONG),
150 BIGNUM *n = BN_new();
153 RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_MALLOC_FAILURE);
156 BN_with_flags(n, rsa->n, BN_FLG_CONSTTIME);
158 ret = BN_BLINDING_create_param(NULL, e, n, ctx, rsa->meth->bn_mod_exp,
160 /* We MUST free n before any further use of rsa->n */
164 RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_BN_LIB);
168 BN_BLINDING_set_current_thread(ret);