blob: 0f511335d70735ab33d3eb6eeb3b4416452fbe2f [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
Gabor Mezei9db81e92022-12-13 10:51:37 +0100179int mbedtls_mpi_mod_mul( mbedtls_mpi_mod_residue *X,
180 const mbedtls_mpi_mod_residue *A,
181 const mbedtls_mpi_mod_residue *B,
182 const mbedtls_mpi_mod_modulus *N )
183{
184 if( N->limbs == 0 )
185 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
186
187 if( X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs )
188 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
189
190 mbedtls_mpi_uint *T = mbedtls_calloc( N->limbs * 2 + 1, ciL );
191 if( !T )
192 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
193
194 mbedtls_mpi_mod_raw_mul( X->p, A->p, B->p, N, T );
195
196 mbedtls_free( T );
197
198 return( 0 );
199}
200
Janos Follath5933f692022-11-02 14:35:17 +0000201/* END MERGE SLOT 2 */
202
203/* BEGIN MERGE SLOT 3 */
Tom Cosgrove62b20482022-12-01 14:27:37 +0000204int mbedtls_mpi_mod_sub( mbedtls_mpi_mod_residue *X,
205 const mbedtls_mpi_mod_residue *A,
206 const mbedtls_mpi_mod_residue *B,
207 const mbedtls_mpi_mod_modulus *N )
208{
209 if( X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs )
210 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Janos Follath5933f692022-11-02 14:35:17 +0000211
Tom Cosgrove62b20482022-12-01 14:27:37 +0000212 mbedtls_mpi_mod_raw_sub( X->p, A->p, B->p, N );
213
214 return( 0 );
215}
Tom Cosgrove4302d022022-12-13 10:46:39 +0000216
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000217static int mbedtls_mpi_mod_inv_mont( mbedtls_mpi_mod_residue *X,
218 const mbedtls_mpi_mod_residue *A,
219 const mbedtls_mpi_mod_modulus *N,
220 mbedtls_mpi_uint *working_memory )
221{
222 /* Input already in Montgomery form, so there's little to do */
223 mbedtls_mpi_mod_raw_inv_prime( X->p, A->p,
224 N->p, N->limbs,
225 N->rep.mont.rr,
226 working_memory );
227 return( 0 );
228}
229
230static int mbedtls_mpi_mod_inv_non_mont( mbedtls_mpi_mod_residue *X,
231 const mbedtls_mpi_mod_residue *A,
232 const mbedtls_mpi_mod_modulus *N,
233 mbedtls_mpi_uint *working_memory )
234{
235 /* Need to convert input into Montgomery form */
236
237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
238
239 mbedtls_mpi_mod_modulus Nmont;
240 mbedtls_mpi_mod_modulus_init( &Nmont );
241
242 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_modulus_setup( &Nmont, N->p, N->limbs,
243 MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
244
245 /* We'll use X->p to hold the Montgomery form of the input A->p */
246 mbedtls_mpi_core_to_mont_rep( X->p, A->p, Nmont.p, Nmont.limbs,
247 Nmont.rep.mont.mm, Nmont.rep.mont.rr,
248 working_memory );
249
250 mbedtls_mpi_mod_raw_inv_prime( X->p, X->p,
251 Nmont.p, Nmont.limbs,
252 Nmont.rep.mont.rr,
253 working_memory );
254
255 /* And convert back from Montgomery form */
256
257 mbedtls_mpi_core_from_mont_rep( X->p, X->p, Nmont.p, Nmont.limbs,
258 Nmont.rep.mont.mm, working_memory );
259
260cleanup:
261 mbedtls_mpi_mod_modulus_free( &Nmont );
262 return( ret );
263}
264
Tom Cosgrove4302d022022-12-13 10:46:39 +0000265int mbedtls_mpi_mod_inv( mbedtls_mpi_mod_residue *X,
266 const mbedtls_mpi_mod_residue *A,
267 const mbedtls_mpi_mod_modulus *N )
268{
269 if( X->limbs != N->limbs || A->limbs != N->limbs )
270 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
271
272 /* Zero has the same value regardless of Montgomery form or not */
273 if( mbedtls_mpi_core_check_zero_ct( A->p, A->limbs ) == 0 )
274 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
275
Tom Cosgrove4302d022022-12-13 10:46:39 +0000276 size_t working_limbs =
277 mbedtls_mpi_mod_raw_inv_prime_working_limbs( N->limbs );
278
279 mbedtls_mpi_uint *working_memory = mbedtls_calloc( working_limbs,
280 sizeof(mbedtls_mpi_uint) );
281 if( working_memory == NULL )
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000282 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
283
284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
285
286 switch( N->int_rep )
Tom Cosgrove4302d022022-12-13 10:46:39 +0000287 {
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000288 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
289 ret = mbedtls_mpi_mod_inv_mont( X, A, N, working_memory );
290 break;
291 case MBEDTLS_MPI_MOD_REP_OPT_RED:
292 ret = mbedtls_mpi_mod_inv_non_mont( X, A, N, working_memory );
293 break;
294 default:
295 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
296 break;
Tom Cosgrove4302d022022-12-13 10:46:39 +0000297 }
298
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000299 mbedtls_platform_zeroize( working_memory,
300 working_limbs * sizeof(mbedtls_mpi_uint) );
Tom Cosgrove342d00b2022-12-16 11:02:06 +0000301 mbedtls_free( working_memory );
Tom Cosgrove4302d022022-12-13 10:46:39 +0000302
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000303 return ret;
Tom Cosgrove4302d022022-12-13 10:46:39 +0000304}
Janos Follath5933f692022-11-02 14:35:17 +0000305/* END MERGE SLOT 3 */
306
307/* BEGIN MERGE SLOT 4 */
308
309/* END MERGE SLOT 4 */
310
311/* BEGIN MERGE SLOT 5 */
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000312int mbedtls_mpi_mod_add( mbedtls_mpi_mod_residue *X,
313 const mbedtls_mpi_mod_residue *A,
314 const mbedtls_mpi_mod_residue *B,
315 const mbedtls_mpi_mod_modulus *N )
316{
317 if( X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs )
318 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Janos Follath5933f692022-11-02 14:35:17 +0000319
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000320 mbedtls_mpi_mod_raw_add(X->p, A->p, B->p, N);
321
322 return( 0 );
323}
Janos Follath5933f692022-11-02 14:35:17 +0000324/* END MERGE SLOT 5 */
325
326/* BEGIN MERGE SLOT 6 */
327
328/* END MERGE SLOT 6 */
329
330/* BEGIN MERGE SLOT 7 */
Minos Galanakis81f4b112022-11-10 14:40:38 +0000331int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
Minos Galanakis8b375452022-11-24 11:04:11 +0000332 const mbedtls_mpi_mod_modulus *m,
333 const unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000334 size_t buflen,
335 mbedtls_mpi_mod_ext_rep ext_rep )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000336{
337 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Janos Follath5933f692022-11-02 14:35:17 +0000338
Janos Follath75b9f0f2022-11-26 14:28:50 +0000339 /* Do our best to check if r and m have been set up */
Janos Follath6eb92c02022-11-26 17:34:37 +0000340 if( r->limbs == 0 || m->limbs == 0 )
Janos Follath75b9f0f2022-11-26 14:28:50 +0000341 goto cleanup;
Janos Follath6eb92c02022-11-26 17:34:37 +0000342 if( r->limbs != m->limbs )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000343 goto cleanup;
344
Janos Follath3e3fc912022-11-24 18:02:46 +0000345 ret = mbedtls_mpi_mod_raw_read( r->p, m, buf, buflen, ext_rep );
Minos Galanakis81f4b112022-11-10 14:40:38 +0000346 if( ret != 0 )
347 goto cleanup;
348
Minos Galanakis8b375452022-11-24 11:04:11 +0000349 r->limbs = m->limbs;
350
Janos Follath6eb92c02022-11-26 17:34:37 +0000351 if( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
352 ret = mbedtls_mpi_mod_raw_to_mont_rep( r->p, m );
Minos Galanakis81f4b112022-11-10 14:40:38 +0000353
354cleanup:
355 return ( ret );
356}
357
Minos Galanakis8b375452022-11-24 11:04:11 +0000358int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
359 const mbedtls_mpi_mod_modulus *m,
Minos Galanakis81f4b112022-11-10 14:40:38 +0000360 unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000361 size_t buflen,
362 mbedtls_mpi_mod_ext_rep ext_rep )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000363{
364 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
365
Janos Follath75b9f0f2022-11-26 14:28:50 +0000366 /* Do our best to check if r and m have been set up */
Janos Follath6eb92c02022-11-26 17:34:37 +0000367 if( r->limbs == 0 || m->limbs == 0 )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000368 goto cleanup;
Janos Follath6eb92c02022-11-26 17:34:37 +0000369 if( r->limbs != m->limbs )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000370 goto cleanup;
371
Janos Follath6eb92c02022-11-26 17:34:37 +0000372 if( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
Janos Follath8dfc8c42022-11-26 15:39:02 +0000373 {
Janos Follath84bee4c2022-11-28 10:27:14 +0000374 ret = mbedtls_mpi_mod_raw_from_mont_rep( r->p, m );
375 if( ret != 0 )
Janos Follath8dfc8c42022-11-26 15:39:02 +0000376 goto cleanup;
377 }
Minos Galanakis81f4b112022-11-10 14:40:38 +0000378
Janos Follath3e3fc912022-11-24 18:02:46 +0000379 ret = mbedtls_mpi_mod_raw_write( r->p, m, buf, buflen, ext_rep );
Minos Galanakis81f4b112022-11-10 14:40:38 +0000380
Janos Follath6eb92c02022-11-26 17:34:37 +0000381 if( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
Janos Follath84bee4c2022-11-28 10:27:14 +0000382 {
383 /* If this fails, the value of r is corrupted and we want to return
384 * this error (as opposed to the error code from the write above) to
385 * let the caller know. If it succeeds, we want to return the error
386 * code from write above. */
387 int conv_ret = mbedtls_mpi_mod_raw_to_mont_rep( r->p, m );
388 if( ret == 0 )
389 ret = conv_ret;
390 }
Janos Follath8dfc8c42022-11-26 15:39:02 +0000391
Minos Galanakis81f4b112022-11-10 14:40:38 +0000392cleanup:
Janos Follath8dfc8c42022-11-26 15:39:02 +0000393
Minos Galanakis81f4b112022-11-10 14:40:38 +0000394 return ( ret );
395}
Janos Follath5933f692022-11-02 14:35:17 +0000396/* END MERGE SLOT 7 */
397
398/* BEGIN MERGE SLOT 8 */
399
400/* END MERGE SLOT 8 */
401
402/* BEGIN MERGE SLOT 9 */
403
404/* END MERGE SLOT 9 */
405
406/* BEGIN MERGE SLOT 10 */
407
408/* END MERGE SLOT 10 */
409
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200410#endif /* MBEDTLS_BIGNUM_C */