]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rsa/rsa_crpt.c
2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 * RSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include <openssl/crypto.h>
18 #include "internal/cryptlib.h"
19 #include "crypto/bn.h"
20 #include <openssl/rand.h>
21 #include "rsa_local.h"
23 int RSA_bits(const RSA
*r
)
25 return BN_num_bits(r
->n
);
28 int RSA_size(const RSA
*r
)
30 return BN_num_bytes(r
->n
);
33 int RSA_public_encrypt(int flen
, const unsigned char *from
, unsigned char *to
,
34 RSA
*rsa
, int padding
)
36 return rsa
->meth
->rsa_pub_enc(flen
, from
, to
, rsa
, padding
);
39 int RSA_private_encrypt(int flen
, const unsigned char *from
,
40 unsigned char *to
, RSA
*rsa
, int padding
)
42 return rsa
->meth
->rsa_priv_enc(flen
, from
, to
, rsa
, padding
);
45 int RSA_private_decrypt(int flen
, const unsigned char *from
,
46 unsigned char *to
, RSA
*rsa
, int padding
)
48 return rsa
->meth
->rsa_priv_dec(flen
, from
, to
, rsa
, padding
);
51 int RSA_public_decrypt(int flen
, const unsigned char *from
, unsigned char *to
,
52 RSA
*rsa
, int padding
)
54 return rsa
->meth
->rsa_pub_dec(flen
, from
, to
, rsa
, padding
);
57 int RSA_flags(const RSA
*r
)
59 return r
== NULL
? 0 : r
->meth
->flags
;
62 void RSA_blinding_off(RSA
*rsa
)
64 BN_BLINDING_free(rsa
->blinding
);
66 rsa
->flags
&= ~RSA_FLAG_BLINDING
;
67 rsa
->flags
|= RSA_FLAG_NO_BLINDING
;
70 int RSA_blinding_on(RSA
*rsa
, BN_CTX
*ctx
)
74 if (rsa
->blinding
!= NULL
)
75 RSA_blinding_off(rsa
);
77 rsa
->blinding
= RSA_setup_blinding(rsa
, ctx
);
78 if (rsa
->blinding
== NULL
)
81 rsa
->flags
|= RSA_FLAG_BLINDING
;
82 rsa
->flags
&= ~RSA_FLAG_NO_BLINDING
;
88 static BIGNUM
*rsa_get_public_exp(const BIGNUM
*d
, const BIGNUM
*p
,
89 const BIGNUM
*q
, BN_CTX
*ctx
)
91 BIGNUM
*ret
= NULL
, *r0
, *r1
, *r2
;
93 if (d
== NULL
|| p
== NULL
|| q
== NULL
)
103 if (!BN_sub(r1
, p
, BN_value_one()))
105 if (!BN_sub(r2
, q
, BN_value_one()))
107 if (!BN_mul(r0
, r1
, r2
, ctx
))
110 ret
= BN_mod_inverse(NULL
, d
, r0
, ctx
);
116 BN_BLINDING
*RSA_setup_blinding(RSA
*rsa
, BN_CTX
*in_ctx
)
120 BN_BLINDING
*ret
= NULL
;
122 if (in_ctx
== NULL
) {
123 if ((ctx
= BN_CTX_new_ex(rsa
->libctx
)) == NULL
)
132 ERR_raise(ERR_LIB_RSA
, ERR_R_BN_LIB
);
136 if (rsa
->e
== NULL
) {
137 e
= rsa_get_public_exp(rsa
->d
, rsa
->p
, rsa
->q
, ctx
);
139 ERR_raise(ERR_LIB_RSA
, RSA_R_NO_PUBLIC_EXPONENT
);
147 BIGNUM
*n
= BN_new();
150 ERR_raise(ERR_LIB_RSA
, ERR_R_BN_LIB
);
153 BN_with_flags(n
, rsa
->n
, BN_FLG_CONSTTIME
);
155 ret
= BN_BLINDING_create_param(NULL
, e
, n
, ctx
, rsa
->meth
->bn_mod_exp
,
157 /* We MUST free n before any further use of rsa->n */
161 ERR_raise(ERR_LIB_RSA
, ERR_R_BN_LIB
);
165 BN_BLINDING_set_current_thread(ret
);