summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/rsa/rsa_crpt.c
diff options
context:
space:
mode:
authormiod <>2014-07-09 08:20:08 +0000
committermiod <>2014-07-09 08:20:08 +0000
commit8cbe58f0d357b14b0ce292d336469d0554a567bc (patch)
tree07872a7ef59da8cea3b3b4a101fa3580e4d658c0 /src/lib/libcrypto/rsa/rsa_crpt.c
parentbc1209e388500a20f5e75cab35d1b543ce0bbe74 (diff)
downloadopenbsd-8cbe58f0d357b14b0ce292d336469d0554a567bc.tar.gz
openbsd-8cbe58f0d357b14b0ce292d336469d0554a567bc.tar.bz2
openbsd-8cbe58f0d357b14b0ce292d336469d0554a567bc.zip
KNF
Diffstat (limited to 'src/lib/libcrypto/rsa/rsa_crpt.c')
-rw-r--r--src/lib/libcrypto/rsa/rsa_crpt.c151
1 files changed, 78 insertions, 73 deletions
diff --git a/src/lib/libcrypto/rsa/rsa_crpt.c b/src/lib/libcrypto/rsa/rsa_crpt.c
index 2122e7baa4..16679cfd14 100644
--- a/src/lib/libcrypto/rsa/rsa_crpt.c
+++ b/src/lib/libcrypto/rsa/rsa_crpt.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: rsa_crpt.c,v 1.4 2014/06/12 15:49:30 deraadt Exp $ */ 1/* $OpenBSD: rsa_crpt.c,v 1.5 2014/07/09 08:20:08 miod Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -67,54 +67,61 @@
67#include <openssl/engine.h> 67#include <openssl/engine.h>
68#endif 68#endif
69 69
70int RSA_size(const RSA *r) 70int
71 { 71RSA_size(const RSA *r)
72 return(BN_num_bytes(r->n)); 72{
73 } 73 return BN_num_bytes(r->n);
74}
74 75
75int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to, 76int
76 RSA *rsa, int padding) 77RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
77 { 78 RSA *rsa, int padding)
78 return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding)); 79{
79 } 80 return rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding);
81}
80 82
81int RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to, 83int
82 RSA *rsa, int padding) 84RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to,
83 { 85 RSA *rsa, int padding)
84 return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding)); 86{
85 } 87 return rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding);
88}
86 89
87int RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to, 90int
88 RSA *rsa, int padding) 91RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to,
89 { 92 RSA *rsa, int padding)
90 return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding)); 93{
91 } 94 return rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding);
95}
92 96
93int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to, 97int
94 RSA *rsa, int padding) 98RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to,
95 { 99 RSA *rsa, int padding)
96 return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding)); 100{
97 } 101 return rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding);
102}
98 103
99int RSA_flags(const RSA *r) 104int
100 { 105RSA_flags(const RSA *r)
101 return((r == NULL)?0:r->meth->flags); 106{
102 } 107 return r == NULL ? 0 : r->meth->flags;
108}
103 109
104void RSA_blinding_off(RSA *rsa) 110void
105 { 111RSA_blinding_off(RSA *rsa)
106 if (rsa->blinding != NULL) 112{
107 { 113 if (rsa->blinding != NULL) {
108 BN_BLINDING_free(rsa->blinding); 114 BN_BLINDING_free(rsa->blinding);
109 rsa->blinding=NULL; 115 rsa->blinding = NULL;
110 } 116 }
111 rsa->flags &= ~RSA_FLAG_BLINDING; 117 rsa->flags &= ~RSA_FLAG_BLINDING;
112 rsa->flags |= RSA_FLAG_NO_BLINDING; 118 rsa->flags |= RSA_FLAG_NO_BLINDING;
113 } 119}
114 120
115int RSA_blinding_on(RSA *rsa, BN_CTX *ctx) 121int
116 { 122RSA_blinding_on(RSA *rsa, BN_CTX *ctx)
117 int ret=0; 123{
124 int ret = 0;
118 125
119 if (rsa->blinding != NULL) 126 if (rsa->blinding != NULL)
120 RSA_blinding_off(rsa); 127 RSA_blinding_off(rsa);
@@ -125,13 +132,14 @@ int RSA_blinding_on(RSA *rsa, BN_CTX *ctx)
125 132
126 rsa->flags |= RSA_FLAG_BLINDING; 133 rsa->flags |= RSA_FLAG_BLINDING;
127 rsa->flags &= ~RSA_FLAG_NO_BLINDING; 134 rsa->flags &= ~RSA_FLAG_NO_BLINDING;
128 ret=1; 135 ret = 1;
129err: 136err:
130 return(ret); 137 return(ret);
131 } 138}
132 139
133static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p, 140static BIGNUM *
134 const BIGNUM *q, BN_CTX *ctx) 141rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p, const BIGNUM *q,
142 BN_CTX *ctx)
135{ 143{
136 BIGNUM *ret = NULL, *r0, *r1, *r2; 144 BIGNUM *ret = NULL, *r0, *r1, *r2;
137 145
@@ -145,9 +153,12 @@ static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p,
145 if (r2 == NULL) 153 if (r2 == NULL)
146 goto err; 154 goto err;
147 155
148 if (!BN_sub(r1, p, BN_value_one())) goto err; 156 if (!BN_sub(r1, p, BN_value_one()))
149 if (!BN_sub(r2, q, BN_value_one())) goto err; 157 goto err;
150 if (!BN_mul(r0, r1, r2, ctx)) goto err; 158 if (!BN_sub(r2, q, BN_value_one()))
159 goto err;
160 if (!BN_mul(r0, r1, r2, ctx))
161 goto err;
151 162
152 ret = BN_mod_inverse(NULL, d, r0, ctx); 163 ret = BN_mod_inverse(NULL, d, r0, ctx);
153err: 164err:
@@ -155,62 +166,56 @@ err:
155 return ret; 166 return ret;
156} 167}
157 168
158BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx) 169BN_BLINDING *
170RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx)
159{ 171{
160 BIGNUM local_n; 172 BIGNUM local_n;
161 BIGNUM *e,*n; 173 BIGNUM *e, *n;
162 BN_CTX *ctx; 174 BN_CTX *ctx;
163 BN_BLINDING *ret = NULL; 175 BN_BLINDING *ret = NULL;
164 176
165 if (in_ctx == NULL) 177 if (in_ctx == NULL) {
166 { 178 if ((ctx = BN_CTX_new()) == NULL)
167 if ((ctx = BN_CTX_new()) == NULL) return 0; 179 return 0;
168 } 180 } else
169 else
170 ctx = in_ctx; 181 ctx = in_ctx;
171 182
172 BN_CTX_start(ctx); 183 BN_CTX_start(ctx);
173 e = BN_CTX_get(ctx); 184 e = BN_CTX_get(ctx);
174 if (e == NULL) 185 if (e == NULL) {
175 {
176 RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_MALLOC_FAILURE); 186 RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_MALLOC_FAILURE);
177 goto err; 187 goto err;
178 } 188 }
179 189
180 if (rsa->e == NULL) 190 if (rsa->e == NULL) {
181 {
182 e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx); 191 e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx);
183 if (e == NULL) 192 if (e == NULL) {
184 { 193 RSAerr(RSA_F_RSA_SETUP_BLINDING,
185 RSAerr(RSA_F_RSA_SETUP_BLINDING, RSA_R_NO_PUBLIC_EXPONENT); 194 RSA_R_NO_PUBLIC_EXPONENT);
186 goto err; 195 goto err;
187 }
188 } 196 }
189 else 197 } else
190 e = rsa->e; 198 e = rsa->e;
191 199
192 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) 200 if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
193 {
194 /* Set BN_FLG_CONSTTIME flag */ 201 /* Set BN_FLG_CONSTTIME flag */
195 n = &local_n; 202 n = &local_n;
196 BN_with_flags(n, rsa->n, BN_FLG_CONSTTIME); 203 BN_with_flags(n, rsa->n, BN_FLG_CONSTTIME);
197 } 204 } else
198 else
199 n = rsa->n; 205 n = rsa->n;
200 206
201 ret = BN_BLINDING_create_param(NULL, e, n, ctx, 207 ret = BN_BLINDING_create_param(NULL, e, n, ctx, rsa->meth->bn_mod_exp,
202 rsa->meth->bn_mod_exp, rsa->_method_mod_n); 208 rsa->_method_mod_n);
203 if (ret == NULL) 209 if (ret == NULL) {
204 {
205 RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_BN_LIB); 210 RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_BN_LIB);
206 goto err; 211 goto err;
207 } 212 }
208 CRYPTO_THREADID_current(BN_BLINDING_thread_id(ret)); 213 CRYPTO_THREADID_current(BN_BLINDING_thread_id(ret));
209err: 214err:
210 BN_CTX_end(ctx); 215 BN_CTX_end(ctx);
211 if (in_ctx == NULL) 216 if (in_ctx == NULL)
212 BN_CTX_free(ctx); 217 BN_CTX_free(ctx);
213 if(rsa->e == NULL) 218 if (rsa->e == NULL)
214 BN_free(e); 219 BN_free(e);
215 220
216 return ret; 221 return ret;