blob: 216b20f8f720e9c6fde2e2475343e827bf2c4d87 [file] [log] [blame]
Gabor Mezeif049dbf2022-07-18 23:02:33 +02001/**
Janos Follatha95f2042022-08-19 12:09:17 +01002 * Modular bignum functions
Gabor Mezeif049dbf2022-07-18 23:02:33 +02003 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include "common.h"
21
22#if defined(MBEDTLS_BIGNUM_C)
23
Gabor Mezeib9030702022-07-18 23:09:45 +020024#include <string.h>
25
26#include "mbedtls/platform_util.h"
Gabor Mezeif049dbf2022-07-18 23:02:33 +020027#include "mbedtls/error.h"
28#include "mbedtls/bignum.h"
Gabor Mezeif049dbf2022-07-18 23:02:33 +020029
Janos Follathba5c1392022-07-19 13:42:07 +010030#include "mbedtls/platform.h"
Janos Follathba5c1392022-07-19 13:42:07 +010031
Janos Follathd1baedb2022-08-09 13:44:53 +010032#include "bignum_core.h"
33#include "bignum_mod.h"
34#include "bignum_mod_raw.h"
35#include "constant_time_internal.h"
36
Gabor Mezeif049dbf2022-07-18 23:02:33 +020037int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
Janos Follath6b8a4ad2022-08-19 10:58:34 +010038 const mbedtls_mpi_mod_modulus *m,
Janos Follath8b718b52022-07-25 11:31:02 +010039 mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +010040 size_t p_limbs )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020041{
Janos Follath50cd4b82022-11-24 17:08:13 +000042 if( p_limbs != m->limbs || !mbedtls_mpi_core_lt_ct( p, m->p, m->limbs ) )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020043 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
44
Gabor Mezeifd65e822022-08-12 18:09:12 +020045 r->limbs = m->limbs;
Janos Follath8b718b52022-07-25 11:31:02 +010046 r->p = p;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020047
48 return( 0 );
49}
50
Gabor Mezei37b06362022-08-02 17:22:18 +020051void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r )
52{
Janos Follath6eb92c02022-11-26 17:34:37 +000053 if( r == NULL )
Gabor Mezei37b06362022-08-02 17:22:18 +020054 return;
55
Gabor Mezeifd65e822022-08-12 18:09:12 +020056 r->limbs = 0;
Gabor Mezei37b06362022-08-02 17:22:18 +020057 r->p = NULL;
58}
59
Gabor Mezeif049dbf2022-07-18 23:02:33 +020060void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m )
61{
Janos Follath6eb92c02022-11-26 17:34:37 +000062 if( m == NULL )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020063 return;
64
Janos Follath281ccda2022-07-19 13:14:36 +010065 m->p = NULL;
Gabor Mezeifd65e822022-08-12 18:09:12 +020066 m->limbs = 0;
67 m->bits = 0;
Janos Follath281ccda2022-07-19 13:14:36 +010068 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020069}
70
71void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
72{
Janos Follath6eb92c02022-11-26 17:34:37 +000073 if( m == NULL )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020074 return;
75
Janos Follathba5c1392022-07-19 13:42:07 +010076 switch( m->int_rep )
77 {
78 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
Minos Galanakis4d4c98b2022-10-27 15:58:02 +010079 if (m->rep.mont.rr != NULL)
80 {
81 mbedtls_platform_zeroize( (mbedtls_mpi_uint *) m->rep.mont.rr,
Tom Cosgrovee9ffb6c2022-12-12 11:26:02 +000082 m->limbs * sizeof(mbedtls_mpi_uint) );
Minos Galanakis4d4c98b2022-10-27 15:58:02 +010083 mbedtls_free( (mbedtls_mpi_uint *)m->rep.mont.rr );
84 m->rep.mont.rr = NULL;
85 }
Minos Galanakis771c4702022-10-27 12:22:22 +010086 m->rep.mont.mm = 0;
87 break;
Janos Follathba5c1392022-07-19 13:42:07 +010088 case MBEDTLS_MPI_MOD_REP_OPT_RED:
Janos Follath296ea662022-08-11 14:58:29 +010089 mbedtls_free( m->rep.ored );
90 break;
91 case MBEDTLS_MPI_MOD_REP_INVALID:
Janos Follathba5c1392022-07-19 13:42:07 +010092 break;
93 }
94
Gabor Mezeif049dbf2022-07-18 23:02:33 +020095 m->p = NULL;
Gabor Mezeifd65e822022-08-12 18:09:12 +020096 m->limbs = 0;
97 m->bits = 0;
Janos Follath281ccda2022-07-19 13:14:36 +010098 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020099}
100
Minos Galanakis8b333632022-10-11 11:28:24 +0100101static int set_mont_const_square( const mbedtls_mpi_uint **X,
102 const mbedtls_mpi_uint *A,
103 size_t limbs )
104{
105 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
106 mbedtls_mpi N;
107 mbedtls_mpi RR;
Minos Galanakis4d4c98b2022-10-27 15:58:02 +0100108 *X = NULL;
Minos Galanakis8b333632022-10-11 11:28:24 +0100109
110 mbedtls_mpi_init( &N );
111 mbedtls_mpi_init( &RR );
112
Janos Follath6eb92c02022-11-26 17:34:37 +0000113 if( A == NULL || limbs == 0 || limbs >= ( MBEDTLS_MPI_MAX_LIMBS / 2 ) - 2 )
Minos Galanakis8b333632022-10-11 11:28:24 +0100114 goto cleanup;
115
Janos Follath6eb92c02022-11-26 17:34:37 +0000116 if( mbedtls_mpi_grow( &N, limbs ) )
Minos Galanakis8b333632022-10-11 11:28:24 +0100117 goto cleanup;
118
Minos Galanakis4d4c98b2022-10-27 15:58:02 +0100119 memcpy( N.p, A, sizeof(mbedtls_mpi_uint) * limbs );
Minos Galanakis771c4702022-10-27 12:22:22 +0100120
Minos Galanakis4d4c98b2022-10-27 15:58:02 +0100121 ret = mbedtls_mpi_core_get_mont_r2_unsafe(&RR, &N);
Minos Galanakis8b333632022-10-11 11:28:24 +0100122
Janos Follath6eb92c02022-11-26 17:34:37 +0000123 if( ret == 0 )
Minos Galanakis4d4c98b2022-10-27 15:58:02 +0100124 {
125 *X = RR.p;
126 RR.p = NULL;
127 }
Minos Galanakis8b333632022-10-11 11:28:24 +0100128
129cleanup:
130 mbedtls_mpi_free(&N);
131 mbedtls_mpi_free(&RR);
132 ret = ( ret != 0 ) ? MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED : 0;
133 return( ret );
134}
135
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200136int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Janos Follathed5c8d32022-08-15 11:50:22 +0100137 const mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100138 size_t p_limbs,
Janos Follath296ea662022-08-11 14:58:29 +0100139 mbedtls_mpi_mod_rep_selector int_rep )
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200140{
Janos Follathba5c1392022-07-19 13:42:07 +0100141 int ret = 0;
142
Gabor Mezei535f36d2022-08-02 11:50:44 +0200143 m->p = p;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100144 m->limbs = p_limbs;
145 m->bits = mbedtls_mpi_core_bitlen( p, p_limbs );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200146
Janos Follathba5c1392022-07-19 13:42:07 +0100147 switch( int_rep )
148 {
149 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
150 m->int_rep = int_rep;
Minos Galanakis8b333632022-10-11 11:28:24 +0100151 m->rep.mont.mm = mbedtls_mpi_core_montmul_init( m->p );
Minos Galanakis4d4c98b2022-10-27 15:58:02 +0100152 ret = set_mont_const_square( &m->rep.mont.rr, m->p, m->limbs );
Minos Galanakis8b333632022-10-11 11:28:24 +0100153 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100154 case MBEDTLS_MPI_MOD_REP_OPT_RED:
155 m->int_rep = int_rep;
Janos Follath296ea662022-08-11 14:58:29 +0100156 m->rep.ored = NULL;
157 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100158 default:
159 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
160 goto exit;
161 }
162
163exit:
164
165 if( ret != 0 )
166 {
167 mbedtls_mpi_mod_modulus_free( m );
168 }
169
170 return( ret );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200171}
172
Janos Follath5933f692022-11-02 14:35:17 +0000173/* BEGIN MERGE SLOT 1 */
174
175/* END MERGE SLOT 1 */
176
177/* BEGIN MERGE SLOT 2 */
178
179/* END MERGE SLOT 2 */
180
181/* BEGIN MERGE SLOT 3 */
Tom Cosgrove62b20482022-12-01 14:27:37 +0000182int mbedtls_mpi_mod_sub( mbedtls_mpi_mod_residue *X,
183 const mbedtls_mpi_mod_residue *A,
184 const mbedtls_mpi_mod_residue *B,
185 const mbedtls_mpi_mod_modulus *N )
186{
187 if( X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs )
188 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Janos Follath5933f692022-11-02 14:35:17 +0000189
Tom Cosgrove62b20482022-12-01 14:27:37 +0000190 mbedtls_mpi_mod_raw_sub( X->p, A->p, B->p, N );
191
192 return( 0 );
193}
Tom Cosgrove4302d022022-12-13 10:46:39 +0000194
195int mbedtls_mpi_mod_inv( mbedtls_mpi_mod_residue *X,
196 const mbedtls_mpi_mod_residue *A,
197 const mbedtls_mpi_mod_modulus *N )
198{
199 if( X->limbs != N->limbs || A->limbs != N->limbs )
200 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
201
202 /* Zero has the same value regardless of Montgomery form or not */
203 if( mbedtls_mpi_core_check_zero_ct( A->p, A->limbs ) == 0 )
204 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
205
206 /* Will we need to do Montgomery conversion? */
207 int mont_conv_needed;
208 switch( N->int_rep )
209 {
210 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
211 mont_conv_needed = 0;
212 break;
213 case MBEDTLS_MPI_MOD_REP_OPT_RED:
214 mont_conv_needed = 1;
215 break;
216 default:
217 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
218 }
219
220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
221
222 /* If the input is already in Montgomery form, we have little to do but
223 * allocate working memory and call mbedtls_mpi_mod_raw_inv_prime().
224 *
225 * If it's not, we need to
226 * 1. Create a Montgomery version of the modulus;
227 * 2. Convert the input into Mont. form, using X->p to hold it;
228 * 3. (allocate and convert, same as if already in Mont. form);
229 * 4. Convert the inverted output back from Mont. form.
230 *
231 * Since the Montgomery conversion functions are in-place, we'll need to
232 * copy A into X before we start working on it (which could be avoided if
233 * there was a not-in-place function to convert to Montgomery form.
234 */
235
236 /* Montgomery version of modulus (if not already in Mont. form).
237 * We will only call setup if the input is not already in Montgomery form.
238 * We will re-use N->p from input modulus, and make use of the fact that
239 * mbedtls_mpi_mod_raw_to_mont_rep() won't free it. */
240 mbedtls_mpi_mod_modulus Nmont;
241 mbedtls_mpi_mod_modulus_init( &Nmont );
242
243 size_t working_limbs =
244 mbedtls_mpi_mod_raw_inv_prime_working_limbs( N->limbs );
245
246 mbedtls_mpi_uint *working_memory = mbedtls_calloc( working_limbs,
247 sizeof(mbedtls_mpi_uint) );
248 if( working_memory == NULL )
249 {
250 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
251 goto cleanup;
252 }
253
254 const mbedtls_mpi_uint *to_invert; /* Will alias A->p or X->p */
255 const mbedtls_mpi_mod_modulus *Nuse; /* Which of N and Nmont to use */
256
257 if( mont_conv_needed )
258 {
259 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_modulus_setup( &Nmont, N->p, N->limbs,
260 MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
261
262 mbedtls_mpi_core_to_mont_rep( X->p, A->p, Nmont.p, Nmont.limbs,
263 Nmont.rep.mont.mm, Nmont.rep.mont.rr,
264 working_memory );
265 to_invert = X->p;
266 Nuse = &Nmont;
267 }
268 else
269 {
270 to_invert = A->p;
271 Nuse = N;
272 }
273
274 mbedtls_mpi_mod_raw_inv_prime( X->p, to_invert,
275 Nuse->p, Nuse->limbs,
276 Nuse->rep.mont.rr,
277 working_memory );
278
279 if( mont_conv_needed )
280 mbedtls_mpi_core_from_mont_rep( X->p, X->p, Nmont.p, Nmont.limbs,
281 Nmont.rep.mont.mm, working_memory );
282
283cleanup:
284 mbedtls_mpi_mod_modulus_free( &Nmont );
285
286 if (working_memory != NULL )
287 {
288 mbedtls_platform_zeroize( working_memory,
289 working_limbs * sizeof(mbedtls_mpi_uint) );
290 mbedtls_free( working_memory );
291 }
292
293 return( ret );
294}
Janos Follath5933f692022-11-02 14:35:17 +0000295/* END MERGE SLOT 3 */
296
297/* BEGIN MERGE SLOT 4 */
298
299/* END MERGE SLOT 4 */
300
301/* BEGIN MERGE SLOT 5 */
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000302int mbedtls_mpi_mod_add( mbedtls_mpi_mod_residue *X,
303 const mbedtls_mpi_mod_residue *A,
304 const mbedtls_mpi_mod_residue *B,
305 const mbedtls_mpi_mod_modulus *N )
306{
307 if( X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs )
308 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Janos Follath5933f692022-11-02 14:35:17 +0000309
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000310 mbedtls_mpi_mod_raw_add(X->p, A->p, B->p, N);
311
312 return( 0 );
313}
Janos Follath5933f692022-11-02 14:35:17 +0000314/* END MERGE SLOT 5 */
315
316/* BEGIN MERGE SLOT 6 */
317
318/* END MERGE SLOT 6 */
319
320/* BEGIN MERGE SLOT 7 */
Minos Galanakis81f4b112022-11-10 14:40:38 +0000321int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
Minos Galanakis8b375452022-11-24 11:04:11 +0000322 const mbedtls_mpi_mod_modulus *m,
323 const unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000324 size_t buflen,
325 mbedtls_mpi_mod_ext_rep ext_rep )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000326{
327 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Janos Follath5933f692022-11-02 14:35:17 +0000328
Janos Follath75b9f0f2022-11-26 14:28:50 +0000329 /* Do our best to check if r and m have been set up */
Janos Follath6eb92c02022-11-26 17:34:37 +0000330 if( r->limbs == 0 || m->limbs == 0 )
Janos Follath75b9f0f2022-11-26 14:28:50 +0000331 goto cleanup;
Janos Follath6eb92c02022-11-26 17:34:37 +0000332 if( r->limbs != m->limbs )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000333 goto cleanup;
334
Janos Follath3e3fc912022-11-24 18:02:46 +0000335 ret = mbedtls_mpi_mod_raw_read( r->p, m, buf, buflen, ext_rep );
Minos Galanakis81f4b112022-11-10 14:40:38 +0000336 if( ret != 0 )
337 goto cleanup;
338
Minos Galanakis8b375452022-11-24 11:04:11 +0000339 r->limbs = m->limbs;
340
Janos Follath6eb92c02022-11-26 17:34:37 +0000341 if( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
342 ret = mbedtls_mpi_mod_raw_to_mont_rep( r->p, m );
Minos Galanakis81f4b112022-11-10 14:40:38 +0000343
344cleanup:
345 return ( ret );
346}
347
Minos Galanakis8b375452022-11-24 11:04:11 +0000348int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
349 const mbedtls_mpi_mod_modulus *m,
Minos Galanakis81f4b112022-11-10 14:40:38 +0000350 unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000351 size_t buflen,
352 mbedtls_mpi_mod_ext_rep ext_rep )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000353{
354 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
355
Janos Follath75b9f0f2022-11-26 14:28:50 +0000356 /* Do our best to check if r and m have been set up */
Janos Follath6eb92c02022-11-26 17:34:37 +0000357 if( r->limbs == 0 || m->limbs == 0 )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000358 goto cleanup;
Janos Follath6eb92c02022-11-26 17:34:37 +0000359 if( r->limbs != m->limbs )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000360 goto cleanup;
361
Janos Follath6eb92c02022-11-26 17:34:37 +0000362 if( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
Janos Follath8dfc8c42022-11-26 15:39:02 +0000363 {
Janos Follath84bee4c2022-11-28 10:27:14 +0000364 ret = mbedtls_mpi_mod_raw_from_mont_rep( r->p, m );
365 if( ret != 0 )
Janos Follath8dfc8c42022-11-26 15:39:02 +0000366 goto cleanup;
367 }
Minos Galanakis81f4b112022-11-10 14:40:38 +0000368
Janos Follath3e3fc912022-11-24 18:02:46 +0000369 ret = mbedtls_mpi_mod_raw_write( r->p, m, buf, buflen, ext_rep );
Minos Galanakis81f4b112022-11-10 14:40:38 +0000370
Janos Follath6eb92c02022-11-26 17:34:37 +0000371 if( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
Janos Follath84bee4c2022-11-28 10:27:14 +0000372 {
373 /* If this fails, the value of r is corrupted and we want to return
374 * this error (as opposed to the error code from the write above) to
375 * let the caller know. If it succeeds, we want to return the error
376 * code from write above. */
377 int conv_ret = mbedtls_mpi_mod_raw_to_mont_rep( r->p, m );
378 if( ret == 0 )
379 ret = conv_ret;
380 }
Janos Follath8dfc8c42022-11-26 15:39:02 +0000381
Minos Galanakis81f4b112022-11-10 14:40:38 +0000382cleanup:
Janos Follath8dfc8c42022-11-26 15:39:02 +0000383
Minos Galanakis81f4b112022-11-10 14:40:38 +0000384 return ( ret );
385}
Janos Follath5933f692022-11-02 14:35:17 +0000386/* END MERGE SLOT 7 */
387
388/* BEGIN MERGE SLOT 8 */
389
390/* END MERGE SLOT 8 */
391
392/* BEGIN MERGE SLOT 9 */
393
394/* END MERGE SLOT 9 */
395
396/* BEGIN MERGE SLOT 10 */
397
398/* END MERGE SLOT 10 */
399
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200400#endif /* MBEDTLS_BIGNUM_C */