xref: /openbsd-src/lib/libcrypto/rsa/rsa_blinding.c (revision 226d4252fa8a9f14200366f16956cf3f39000f7c)
1*226d4252Stb /* $OpenBSD: rsa_blinding.c,v 1.3 2023/08/09 12:09:06 tb Exp $ */
2f2606938Stb /* ====================================================================
3f2606938Stb  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
4f2606938Stb  *
5f2606938Stb  * Redistribution and use in source and binary forms, with or without
6f2606938Stb  * modification, are permitted provided that the following conditions
7f2606938Stb  * are met:
8f2606938Stb  *
9f2606938Stb  * 1. Redistributions of source code must retain the above copyright
10f2606938Stb  *    notice, this list of conditions and the following disclaimer.
11f2606938Stb  *
12f2606938Stb  * 2. Redistributions in binary form must reproduce the above copyright
13f2606938Stb  *    notice, this list of conditions and the following disclaimer in
14f2606938Stb  *    the documentation and/or other materials provided with the
15f2606938Stb  *    distribution.
16f2606938Stb  *
17f2606938Stb  * 3. All advertising materials mentioning features or use of this
18f2606938Stb  *    software must display the following acknowledgment:
19f2606938Stb  *    "This product includes software developed by the OpenSSL Project
20f2606938Stb  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21f2606938Stb  *
22f2606938Stb  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23f2606938Stb  *    endorse or promote products derived from this software without
24f2606938Stb  *    prior written permission. For written permission, please contact
25f2606938Stb  *    openssl-core@openssl.org.
26f2606938Stb  *
27f2606938Stb  * 5. Products derived from this software may not be called "OpenSSL"
28f2606938Stb  *    nor may "OpenSSL" appear in their names without prior written
29f2606938Stb  *    permission of the OpenSSL Project.
30f2606938Stb  *
31f2606938Stb  * 6. Redistributions of any form whatsoever must retain the following
32f2606938Stb  *    acknowledgment:
33f2606938Stb  *    "This product includes software developed by the OpenSSL Project
34f2606938Stb  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35f2606938Stb  *
36f2606938Stb  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37f2606938Stb  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38f2606938Stb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39f2606938Stb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40f2606938Stb  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41f2606938Stb  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42f2606938Stb  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43f2606938Stb  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44f2606938Stb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45f2606938Stb  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46f2606938Stb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47f2606938Stb  * OF THE POSSIBILITY OF SUCH DAMAGE.
48f2606938Stb  * ====================================================================
49f2606938Stb  *
50f2606938Stb  * This product includes cryptographic software written by Eric Young
51f2606938Stb  * (eay@cryptsoft.com).  This product includes software written by Tim
52f2606938Stb  * Hudson (tjh@cryptsoft.com).
53f2606938Stb  *
54f2606938Stb  */
55f2606938Stb /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
56f2606938Stb  * All rights reserved.
57f2606938Stb  *
58f2606938Stb  * This package is an SSL implementation written
59f2606938Stb  * by Eric Young (eay@cryptsoft.com).
60f2606938Stb  * The implementation was written so as to conform with Netscapes SSL.
61f2606938Stb  *
62f2606938Stb  * This library is free for commercial and non-commercial use as long as
63f2606938Stb  * the following conditions are aheared to.  The following conditions
64f2606938Stb  * apply to all code found in this distribution, be it the RC4, RSA,
65f2606938Stb  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
66f2606938Stb  * included with this distribution is covered by the same copyright terms
67f2606938Stb  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
68f2606938Stb  *
69f2606938Stb  * Copyright remains Eric Young's, and as such any Copyright notices in
70f2606938Stb  * the code are not to be removed.
71f2606938Stb  * If this package is used in a product, Eric Young should be given attribution
72f2606938Stb  * as the author of the parts of the library used.
73f2606938Stb  * This can be in the form of a textual message at program startup or
74f2606938Stb  * in documentation (online or textual) provided with the package.
75f2606938Stb  *
76f2606938Stb  * Redistribution and use in source and binary forms, with or without
77f2606938Stb  * modification, are permitted provided that the following conditions
78f2606938Stb  * are met:
79f2606938Stb  * 1. Redistributions of source code must retain the copyright
80f2606938Stb  *    notice, this list of conditions and the following disclaimer.
81f2606938Stb  * 2. Redistributions in binary form must reproduce the above copyright
82f2606938Stb  *    notice, this list of conditions and the following disclaimer in the
83f2606938Stb  *    documentation and/or other materials provided with the distribution.
84f2606938Stb  * 3. All advertising materials mentioning features or use of this software
85f2606938Stb  *    must display the following acknowledgement:
86f2606938Stb  *    "This product includes cryptographic software written by
87f2606938Stb  *     Eric Young (eay@cryptsoft.com)"
88f2606938Stb  *    The word 'cryptographic' can be left out if the rouines from the library
89f2606938Stb  *    being used are not cryptographic related :-).
90f2606938Stb  * 4. If you include any Windows specific code (or a derivative thereof) from
91f2606938Stb  *    the apps directory (application code) you must include an acknowledgement:
92f2606938Stb  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
93f2606938Stb  *
94f2606938Stb  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
95f2606938Stb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96f2606938Stb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97f2606938Stb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
98f2606938Stb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99f2606938Stb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
100f2606938Stb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101f2606938Stb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102f2606938Stb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103f2606938Stb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104f2606938Stb  * SUCH DAMAGE.
105f2606938Stb  *
106f2606938Stb  * The licence and distribution terms for any publically available version or
107f2606938Stb  * derivative of this code cannot be changed.  i.e. this code cannot simply be
108f2606938Stb  * copied and put under another distribution licence
109f2606938Stb  * [including the GNU Public Licence.]
110f2606938Stb  */
111f2606938Stb 
112*226d4252Stb #include <pthread.h>
113f2606938Stb #include <stdio.h>
114f2606938Stb 
115f2606938Stb #include <openssl/opensslconf.h>
116f2606938Stb 
117f2606938Stb #include <openssl/err.h>
118f2606938Stb #include <openssl/rsa.h>
119f2606938Stb 
120f2606938Stb #include "bn_local.h"
121f2606938Stb #include "rsa_local.h"
122f2606938Stb 
123f2606938Stb #define BN_BLINDING_COUNTER	32
124f2606938Stb 
125f2606938Stb struct bn_blinding_st {
126f2606938Stb 	BIGNUM *A;
127f2606938Stb 	BIGNUM *Ai;
128f2606938Stb 	BIGNUM *e;
129f2606938Stb 	BIGNUM *mod;
130*226d4252Stb 	pthread_t tid;
131f2606938Stb 	int counter;
132f2606938Stb 	BN_MONT_CTX *m_ctx;
133f2606938Stb 	int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
134f2606938Stb 	    const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
135f2606938Stb };
136f2606938Stb 
137f2606938Stb BN_BLINDING *
BN_BLINDING_new(const BIGNUM * e,const BIGNUM * mod,BN_CTX * ctx,int (* bn_mod_exp)(BIGNUM * r,const BIGNUM * a,const BIGNUM * p,const BIGNUM * m,BN_CTX * ctx,BN_MONT_CTX * m_ctx),BN_MONT_CTX * m_ctx)138f2606938Stb BN_BLINDING_new(const BIGNUM *e, const BIGNUM *mod, BN_CTX *ctx,
139f2606938Stb     int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
140f2606938Stb 	const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx), BN_MONT_CTX *m_ctx)
141f2606938Stb {
142f2606938Stb 	BN_BLINDING *ret = NULL;
143f2606938Stb 
144f2606938Stb 	if ((ret = calloc(1, sizeof(BN_BLINDING))) == NULL) {
145f2606938Stb 		BNerror(ERR_R_MALLOC_FAILURE);
146f2606938Stb 		goto err;
147f2606938Stb 	}
148f2606938Stb 	if ((ret->A = BN_new()) == NULL)
149f2606938Stb 		goto err;
150f2606938Stb 	if ((ret->Ai = BN_new()) == NULL)
151f2606938Stb 		goto err;
152f2606938Stb 	if ((ret->e = BN_dup(e)) == NULL)
153f2606938Stb 		goto err;
154f2606938Stb 	if ((ret->mod = BN_dup(mod)) == NULL)
155f2606938Stb 		goto err;
156f2606938Stb 	if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)
157f2606938Stb 		BN_set_flags(ret->mod, BN_FLG_CONSTTIME);
158f2606938Stb 
159f2606938Stb 	/* Update on first use. */
160f2606938Stb 	ret->counter = BN_BLINDING_COUNTER - 1;
161*226d4252Stb 	ret->tid = pthread_self();
162f2606938Stb 
163f2606938Stb 	if (bn_mod_exp != NULL)
164f2606938Stb 		ret->bn_mod_exp = bn_mod_exp;
165f2606938Stb 	if (m_ctx != NULL)
166f2606938Stb 		ret->m_ctx = m_ctx;
167f2606938Stb 
168f2606938Stb 	return ret;
169f2606938Stb 
170f2606938Stb  err:
171f2606938Stb 	BN_BLINDING_free(ret);
172f2606938Stb 
173f2606938Stb 	return NULL;
174f2606938Stb }
175f2606938Stb 
176f2606938Stb void
BN_BLINDING_free(BN_BLINDING * r)177f2606938Stb BN_BLINDING_free(BN_BLINDING *r)
178f2606938Stb {
179f2606938Stb 	if (r == NULL)
180f2606938Stb 		return;
181f2606938Stb 
182f2606938Stb 	BN_free(r->A);
183f2606938Stb 	BN_free(r->Ai);
184f2606938Stb 	BN_free(r->e);
185f2606938Stb 	BN_free(r->mod);
186f2606938Stb 	free(r);
187f2606938Stb }
188f2606938Stb 
189f2606938Stb static int
BN_BLINDING_setup(BN_BLINDING * b,BN_CTX * ctx)190f2606938Stb BN_BLINDING_setup(BN_BLINDING *b, BN_CTX *ctx)
191f2606938Stb {
192f2606938Stb 	if (!bn_rand_interval(b->A, 1, b->mod))
193f2606938Stb 		return 0;
194f2606938Stb 	if (BN_mod_inverse_ct(b->Ai, b->A, b->mod, ctx) == NULL)
195f2606938Stb 		return 0;
196f2606938Stb 
197f2606938Stb 	if (b->bn_mod_exp != NULL && b->m_ctx != NULL) {
198f2606938Stb 		if (!b->bn_mod_exp(b->A, b->A, b->e, b->mod, ctx, b->m_ctx))
199f2606938Stb 			return 0;
200f2606938Stb 	} else {
201f2606938Stb 		if (!BN_mod_exp_ct(b->A, b->A, b->e, b->mod, ctx))
202f2606938Stb 			return 0;
203f2606938Stb 	}
204f2606938Stb 
205f2606938Stb 	return 1;
206f2606938Stb }
207f2606938Stb 
208f2606938Stb static int
BN_BLINDING_update(BN_BLINDING * b,BN_CTX * ctx)209f2606938Stb BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx)
210f2606938Stb {
211f2606938Stb 	int ret = 0;
212f2606938Stb 
213f2606938Stb 	if (++b->counter >= BN_BLINDING_COUNTER) {
214f2606938Stb 		if (!BN_BLINDING_setup(b, ctx))
215f2606938Stb 			goto err;
216f2606938Stb 		b->counter = 0;
217f2606938Stb 	} else {
218f2606938Stb 		if (!BN_mod_sqr(b->A, b->A, b->mod, ctx))
219f2606938Stb 			goto err;
220f2606938Stb 		if (!BN_mod_sqr(b->Ai, b->Ai, b->mod, ctx))
221f2606938Stb 			goto err;
222f2606938Stb 	}
223f2606938Stb 
224f2606938Stb 	ret = 1;
225f2606938Stb 
226f2606938Stb  err:
227f2606938Stb 	return ret;
228f2606938Stb }
229f2606938Stb 
230f2606938Stb int
BN_BLINDING_convert(BIGNUM * n,BIGNUM * inv,BN_BLINDING * b,BN_CTX * ctx)231f2606938Stb BN_BLINDING_convert(BIGNUM *n, BIGNUM *inv, BN_BLINDING *b, BN_CTX *ctx)
232f2606938Stb {
233f2606938Stb 	int ret = 0;
234f2606938Stb 
235f2606938Stb 	if (!BN_BLINDING_update(b, ctx))
236f2606938Stb 		goto err;
237f2606938Stb 
238f2606938Stb 	if (inv != NULL) {
239f2606938Stb 		if (!bn_copy(inv, b->Ai))
240f2606938Stb 			goto err;
241f2606938Stb 	}
242f2606938Stb 
243f2606938Stb 	ret = BN_mod_mul(n, n, b->A, b->mod, ctx);
244f2606938Stb 
245f2606938Stb  err:
246f2606938Stb 	return ret;
247f2606938Stb }
248f2606938Stb 
249f2606938Stb int
BN_BLINDING_invert(BIGNUM * n,const BIGNUM * inv,BN_BLINDING * b,BN_CTX * ctx)250f2606938Stb BN_BLINDING_invert(BIGNUM *n, const BIGNUM *inv, BN_BLINDING *b, BN_CTX *ctx)
251f2606938Stb {
252f2606938Stb 	if (inv == NULL)
253f2606938Stb 		inv = b->Ai;
254f2606938Stb 
255f2606938Stb 	return BN_mod_mul(n, n, inv, b->mod, ctx);
256f2606938Stb }
257f2606938Stb 
258*226d4252Stb int
BN_BLINDING_is_local(BN_BLINDING * b)259*226d4252Stb BN_BLINDING_is_local(BN_BLINDING *b)
260f2606938Stb {
261*226d4252Stb 	return pthread_equal(pthread_self(), b->tid) != 0;
262f2606938Stb }
2630b43811cStb 
2640b43811cStb static BIGNUM *
rsa_get_public_exp(const BIGNUM * d,const BIGNUM * p,const BIGNUM * q,BN_CTX * ctx)2650b43811cStb rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p, const BIGNUM *q,
2660b43811cStb     BN_CTX *ctx)
2670b43811cStb {
2680b43811cStb 	BIGNUM *ret = NULL, *r0, *r1, *r2;
2690b43811cStb 
2700b43811cStb 	if (d == NULL || p == NULL || q == NULL)
2710b43811cStb 		return NULL;
2720b43811cStb 
2730b43811cStb 	BN_CTX_start(ctx);
2740b43811cStb 	if ((r0 = BN_CTX_get(ctx)) == NULL)
2750b43811cStb 		goto err;
2760b43811cStb 	if ((r1 = BN_CTX_get(ctx)) == NULL)
2770b43811cStb 		goto err;
2780b43811cStb 	if ((r2 = BN_CTX_get(ctx)) == NULL)
2790b43811cStb 		goto err;
2800b43811cStb 
2810b43811cStb 	if (!BN_sub(r1, p, BN_value_one()))
2820b43811cStb 		goto err;
2830b43811cStb 	if (!BN_sub(r2, q, BN_value_one()))
2840b43811cStb 		goto err;
2850b43811cStb 	if (!BN_mul(r0, r1, r2, ctx))
2860b43811cStb 		goto err;
2870b43811cStb 
2880b43811cStb 	ret = BN_mod_inverse_ct(NULL, d, r0, ctx);
2890b43811cStb err:
2900b43811cStb 	BN_CTX_end(ctx);
2910b43811cStb 	return ret;
2920b43811cStb }
2930b43811cStb 
2940b43811cStb BN_BLINDING *
RSA_setup_blinding(RSA * rsa,BN_CTX * in_ctx)2950b43811cStb RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx)
2960b43811cStb {
2970b43811cStb 	BIGNUM *e = NULL;
2980b43811cStb 	BIGNUM n;
2990b43811cStb 	BN_CTX *ctx = NULL;
3000b43811cStb 	BN_BLINDING *ret = NULL;
3010b43811cStb 
3020b43811cStb 	if ((ctx = in_ctx) == NULL)
3030b43811cStb 		ctx = BN_CTX_new();
3040b43811cStb 	if (ctx == NULL)
3050b43811cStb 		goto err;
3060b43811cStb 
3070b43811cStb 	BN_CTX_start(ctx);
3080b43811cStb 
3090b43811cStb 	if ((e = rsa->e) == NULL)
3100b43811cStb 		e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx);
3110b43811cStb 	if (e == NULL) {
3120b43811cStb 		RSAerror(RSA_R_NO_PUBLIC_EXPONENT);
3130b43811cStb 		goto err;
3140b43811cStb 	}
3150b43811cStb 
3160b43811cStb 	BN_init(&n);
3170b43811cStb 	BN_with_flags(&n, rsa->n, BN_FLG_CONSTTIME);
3180b43811cStb 
3190b43811cStb 	if ((ret = BN_BLINDING_new(e, &n, ctx, rsa->meth->bn_mod_exp,
3200b43811cStb 	    rsa->_method_mod_n)) == NULL) {
3210b43811cStb 		RSAerror(ERR_R_BN_LIB);
3220b43811cStb 		goto err;
3230b43811cStb 	}
3240b43811cStb 
3250b43811cStb  err:
3260b43811cStb 	BN_CTX_end(ctx);
3270b43811cStb 	if (ctx != in_ctx)
3280b43811cStb 		BN_CTX_free(ctx);
3290b43811cStb 	if (e != rsa->e)
3300b43811cStb 		BN_free(e);
3310b43811cStb 
3320b43811cStb 	return ret;
3330b43811cStb }
3340b43811cStb 
3350b43811cStb void
RSA_blinding_off(RSA * rsa)3360b43811cStb RSA_blinding_off(RSA *rsa)
3370b43811cStb {
3380b43811cStb 	BN_BLINDING_free(rsa->blinding);
3390b43811cStb 	rsa->blinding = NULL;
3400b43811cStb 	rsa->flags |= RSA_FLAG_NO_BLINDING;
3410b43811cStb }
3420b43811cStb LCRYPTO_ALIAS(RSA_blinding_off);
3430b43811cStb 
3440b43811cStb int
RSA_blinding_on(RSA * rsa,BN_CTX * ctx)3450b43811cStb RSA_blinding_on(RSA *rsa, BN_CTX *ctx)
3460b43811cStb {
3470b43811cStb 	int ret = 0;
3480b43811cStb 
3490b43811cStb 	if (rsa->blinding != NULL)
3500b43811cStb 		RSA_blinding_off(rsa);
3510b43811cStb 
3520b43811cStb 	rsa->blinding = RSA_setup_blinding(rsa, ctx);
3530b43811cStb 	if (rsa->blinding == NULL)
3540b43811cStb 		goto err;
3550b43811cStb 
3560b43811cStb 	rsa->flags &= ~RSA_FLAG_NO_BLINDING;
3570b43811cStb 	ret = 1;
3580b43811cStb err:
3590b43811cStb 	return (ret);
3600b43811cStb }
3610b43811cStb LCRYPTO_ALIAS(RSA_blinding_on);
362