Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/rsa/rsa_crpt.c

raw
1 /*
2 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 /*
11 * RSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14 #include "internal/deprecated.h"
15
16 #include <stdio.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"
22
23 int RSA_bits(const RSA *r)
24 {
25 return BN_num_bits(r->n);
26 }
27
28 int RSA_size(const RSA *r)
29 {
30 return BN_num_bytes(r->n);
31 }
32
33 int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
34 RSA *rsa, int padding)
35 {
36 return rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding);
37 }
38
39 int RSA_private_encrypt(int flen, const unsigned char *from,
40 unsigned char *to, RSA *rsa, int padding)
41 {
42 return rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding);
43 }
44
45 int RSA_private_decrypt(int flen, const unsigned char *from,
46 unsigned char *to, RSA *rsa, int padding)
47 {
48 return rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding);
49 }
50
51 int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to,
52 RSA *rsa, int padding)
53 {
54 return rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding);
55 }
56
57 int RSA_flags(const RSA *r)
58 {
59 return r == NULL ? 0 : r->meth->flags;
60 }
61
62 void RSA_blinding_off(RSA *rsa)
63 {
64 rsa->flags &= ~RSA_FLAG_BLINDING;
65 rsa->flags |= RSA_FLAG_NO_BLINDING;
66 }
67
68 int RSA_blinding_on(RSA *rsa, BN_CTX *ctx)
69 {
70
71 rsa->flags |= RSA_FLAG_BLINDING;
72 rsa->flags &= ~RSA_FLAG_NO_BLINDING;
73 return 1;
74 }
75
76 static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p,
77 const BIGNUM *q, BN_CTX *ctx)
78 {
79 BIGNUM *ret = NULL, *r0, *r1, *r2;
80
81 if (d == NULL || p == NULL || q == NULL)
82 return NULL;
83
84 BN_CTX_start(ctx);
85 r0 = BN_CTX_get(ctx);
86 r1 = BN_CTX_get(ctx);
87 r2 = BN_CTX_get(ctx);
88 if (r2 == NULL)
89 goto err;
90
91 if (!BN_sub(r1, p, BN_value_one()))
92 goto err;
93 if (!BN_sub(r2, q, BN_value_one()))
94 goto err;
95 if (!BN_mul(r0, r1, r2, ctx))
96 goto err;
97
98 ret = BN_mod_inverse(NULL, d, r0, ctx);
99 err:
100 BN_CTX_end(ctx);
101 return ret;
102 }
103
104 BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx)
105 {
106 BIGNUM *e;
107 BN_CTX *ctx;
108 BN_BLINDING *ret = NULL;
109
110 if (in_ctx == NULL) {
111 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
112 return 0;
113 } else {
114 ctx = in_ctx;
115 }
116
117 BN_CTX_start(ctx);
118 e = BN_CTX_get(ctx);
119 if (e == NULL) {
120 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
121 goto err;
122 }
123
124 if (rsa->e == NULL) {
125 e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx);
126 if (e == NULL) {
127 ERR_raise(ERR_LIB_RSA, RSA_R_NO_PUBLIC_EXPONENT);
128 goto err;
129 }
130 } else {
131 e = rsa->e;
132 }
133
134 {
135 BIGNUM *n = BN_new();
136
137 if (n == NULL) {
138 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
139 goto err;
140 }
141 BN_with_flags(n, rsa->n, BN_FLG_CONSTTIME);
142
143 ret = BN_BLINDING_create_param(NULL, e, n, ctx, rsa->meth->bn_mod_exp,
144 rsa->_method_mod_n);
145 /* We MUST free n before any further use of rsa->n */
146 BN_free(n);
147 }
148 if (ret == NULL) {
149 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
150 goto err;
151 }
152
153 err:
154 BN_CTX_end(ctx);
155 if (ctx != in_ctx)
156 BN_CTX_free(ctx);
157 if (e != rsa->e)
158 BN_free(e);
159
160 return ret;
161 }