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