Skip to content

Commit c7d7d37

Browse files
author
tb
committed
Move RSA blinding API from rsa_crpt.c to rsa_blinding.c
1 parent 740758f commit c7d7d37

File tree

2 files changed

+102
-102
lines changed

2 files changed

+102
-102
lines changed

src/lib/libcrypto/rsa/rsa_blinding.c

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: rsa_blinding.c,v 1.1 2023/08/09 09:23:03 tb Exp $ */
1+
/* $OpenBSD: rsa_blinding.c,v 1.2 2023/08/09 09:26:43 tb Exp $ */
22
/* ====================================================================
33
* Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
44
*
@@ -259,3 +259,103 @@ BN_BLINDING_thread_id(BN_BLINDING *b)
259259
{
260260
return &b->tid;
261261
}
262+
263+
static BIGNUM *
264+
rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p, const BIGNUM *q,
265+
BN_CTX *ctx)
266+
{
267+
BIGNUM *ret = NULL, *r0, *r1, *r2;
268+
269+
if (d == NULL || p == NULL || q == NULL)
270+
return NULL;
271+
272+
BN_CTX_start(ctx);
273+
if ((r0 = BN_CTX_get(ctx)) == NULL)
274+
goto err;
275+
if ((r1 = BN_CTX_get(ctx)) == NULL)
276+
goto err;
277+
if ((r2 = BN_CTX_get(ctx)) == NULL)
278+
goto err;
279+
280+
if (!BN_sub(r1, p, BN_value_one()))
281+
goto err;
282+
if (!BN_sub(r2, q, BN_value_one()))
283+
goto err;
284+
if (!BN_mul(r0, r1, r2, ctx))
285+
goto err;
286+
287+
ret = BN_mod_inverse_ct(NULL, d, r0, ctx);
288+
err:
289+
BN_CTX_end(ctx);
290+
return ret;
291+
}
292+
293+
BN_BLINDING *
294+
RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx)
295+
{
296+
BIGNUM *e = NULL;
297+
BIGNUM n;
298+
BN_CTX *ctx = NULL;
299+
BN_BLINDING *ret = NULL;
300+
301+
if ((ctx = in_ctx) == NULL)
302+
ctx = BN_CTX_new();
303+
if (ctx == NULL)
304+
goto err;
305+
306+
BN_CTX_start(ctx);
307+
308+
if ((e = rsa->e) == NULL)
309+
e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx);
310+
if (e == NULL) {
311+
RSAerror(RSA_R_NO_PUBLIC_EXPONENT);
312+
goto err;
313+
}
314+
315+
BN_init(&n);
316+
BN_with_flags(&n, rsa->n, BN_FLG_CONSTTIME);
317+
318+
if ((ret = BN_BLINDING_new(e, &n, ctx, rsa->meth->bn_mod_exp,
319+
rsa->_method_mod_n)) == NULL) {
320+
RSAerror(ERR_R_BN_LIB);
321+
goto err;
322+
}
323+
CRYPTO_THREADID_current(BN_BLINDING_thread_id(ret));
324+
325+
err:
326+
BN_CTX_end(ctx);
327+
if (ctx != in_ctx)
328+
BN_CTX_free(ctx);
329+
if (e != rsa->e)
330+
BN_free(e);
331+
332+
return ret;
333+
}
334+
335+
void
336+
RSA_blinding_off(RSA *rsa)
337+
{
338+
BN_BLINDING_free(rsa->blinding);
339+
rsa->blinding = NULL;
340+
rsa->flags |= RSA_FLAG_NO_BLINDING;
341+
}
342+
LCRYPTO_ALIAS(RSA_blinding_off);
343+
344+
int
345+
RSA_blinding_on(RSA *rsa, BN_CTX *ctx)
346+
{
347+
int ret = 0;
348+
349+
if (rsa->blinding != NULL)
350+
RSA_blinding_off(rsa);
351+
352+
rsa->blinding = RSA_setup_blinding(rsa, ctx);
353+
if (rsa->blinding == NULL)
354+
goto err;
355+
356+
rsa->flags &= ~RSA_FLAG_NO_BLINDING;
357+
ret = 1;
358+
err:
359+
return (ret);
360+
}
361+
LCRYPTO_ALIAS(RSA_blinding_on);

src/lib/libcrypto/rsa/rsa_crpt.c

Lines changed: 1 addition & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: rsa_crpt.c,v 1.27 2023/08/09 09:25:13 tb Exp $ */
1+
/* $OpenBSD: rsa_crpt.c,v 1.28 2023/08/09 09:26:43 tb Exp $ */
22
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33
* All rights reserved.
44
*
@@ -125,103 +125,3 @@ RSA_flags(const RSA *r)
125125
return r == NULL ? 0 : r->meth->flags;
126126
}
127127
LCRYPTO_ALIAS(RSA_flags);
128-
129-
static BIGNUM *
130-
rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p, const BIGNUM *q,
131-
BN_CTX *ctx)
132-
{
133-
BIGNUM *ret = NULL, *r0, *r1, *r2;
134-
135-
if (d == NULL || p == NULL || q == NULL)
136-
return NULL;
137-
138-
BN_CTX_start(ctx);
139-
if ((r0 = BN_CTX_get(ctx)) == NULL)
140-
goto err;
141-
if ((r1 = BN_CTX_get(ctx)) == NULL)
142-
goto err;
143-
if ((r2 = BN_CTX_get(ctx)) == NULL)
144-
goto err;
145-
146-
if (!BN_sub(r1, p, BN_value_one()))
147-
goto err;
148-
if (!BN_sub(r2, q, BN_value_one()))
149-
goto err;
150-
if (!BN_mul(r0, r1, r2, ctx))
151-
goto err;
152-
153-
ret = BN_mod_inverse_ct(NULL, d, r0, ctx);
154-
err:
155-
BN_CTX_end(ctx);
156-
return ret;
157-
}
158-
159-
BN_BLINDING *
160-
RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx)
161-
{
162-
BIGNUM *e = NULL;
163-
BIGNUM n;
164-
BN_CTX *ctx = NULL;
165-
BN_BLINDING *ret = NULL;
166-
167-
if ((ctx = in_ctx) == NULL)
168-
ctx = BN_CTX_new();
169-
if (ctx == NULL)
170-
goto err;
171-
172-
BN_CTX_start(ctx);
173-
174-
if ((e = rsa->e) == NULL)
175-
e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx);
176-
if (e == NULL) {
177-
RSAerror(RSA_R_NO_PUBLIC_EXPONENT);
178-
goto err;
179-
}
180-
181-
BN_init(&n);
182-
BN_with_flags(&n, rsa->n, BN_FLG_CONSTTIME);
183-
184-
if ((ret = BN_BLINDING_new(e, &n, ctx, rsa->meth->bn_mod_exp,
185-
rsa->_method_mod_n)) == NULL) {
186-
RSAerror(ERR_R_BN_LIB);
187-
goto err;
188-
}
189-
CRYPTO_THREADID_current(BN_BLINDING_thread_id(ret));
190-
191-
err:
192-
BN_CTX_end(ctx);
193-
if (ctx != in_ctx)
194-
BN_CTX_free(ctx);
195-
if (e != rsa->e)
196-
BN_free(e);
197-
198-
return ret;
199-
}
200-
201-
void
202-
RSA_blinding_off(RSA *rsa)
203-
{
204-
BN_BLINDING_free(rsa->blinding);
205-
rsa->blinding = NULL;
206-
rsa->flags |= RSA_FLAG_NO_BLINDING;
207-
}
208-
LCRYPTO_ALIAS(RSA_blinding_off);
209-
210-
int
211-
RSA_blinding_on(RSA *rsa, BN_CTX *ctx)
212-
{
213-
int ret = 0;
214-
215-
if (rsa->blinding != NULL)
216-
RSA_blinding_off(rsa);
217-
218-
rsa->blinding = RSA_setup_blinding(rsa, ctx);
219-
if (rsa->blinding == NULL)
220-
goto err;
221-
222-
rsa->flags &= ~RSA_FLAG_NO_BLINDING;
223-
ret = 1;
224-
err:
225-
return (ret);
226-
}
227-
LCRYPTO_ALIAS(RSA_blinding_on);

0 commit comments

Comments
 (0)