blob: 7c89b57d7f41d0f6baa8382ade1ca1349ae5afff [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
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000195static int mbedtls_mpi_mod_inv_mont( mbedtls_mpi_mod_residue *X,
196 const mbedtls_mpi_mod_residue *A,
197 const mbedtls_mpi_mod_modulus *N,
198 mbedtls_mpi_uint *working_memory )
199{
200 /* Input already in Montgomery form, so there's little to do */
201 mbedtls_mpi_mod_raw_inv_prime( X->p, A->p,
202 N->p, N->limbs,
203 N->rep.mont.rr,
204 working_memory );
205 return( 0 );
206}
207
208static int mbedtls_mpi_mod_inv_non_mont( mbedtls_mpi_mod_residue *X,
209 const mbedtls_mpi_mod_residue *A,
210 const mbedtls_mpi_mod_modulus *N,
211 mbedtls_mpi_uint *working_memory )
212{
213 /* Need to convert input into Montgomery form */
214
215 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
216
217 mbedtls_mpi_mod_modulus Nmont;
218 mbedtls_mpi_mod_modulus_init( &Nmont );
219
220 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_modulus_setup( &Nmont, N->p, N->limbs,
221 MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
222
223 /* We'll use X->p to hold the Montgomery form of the input A->p */
224 mbedtls_mpi_core_to_mont_rep( X->p, A->p, Nmont.p, Nmont.limbs,
225 Nmont.rep.mont.mm, Nmont.rep.mont.rr,
226 working_memory );
227
228 mbedtls_mpi_mod_raw_inv_prime( X->p, X->p,
229 Nmont.p, Nmont.limbs,
230 Nmont.rep.mont.rr,
231 working_memory );
232
233 /* And convert back from Montgomery form */
234
235 mbedtls_mpi_core_from_mont_rep( X->p, X->p, Nmont.p, Nmont.limbs,
236 Nmont.rep.mont.mm, working_memory );
237
238cleanup:
239 mbedtls_mpi_mod_modulus_free( &Nmont );
240 return( ret );
241}
242
Tom Cosgrove4302d022022-12-13 10:46:39 +0000243int mbedtls_mpi_mod_inv( mbedtls_mpi_mod_residue *X,
244 const mbedtls_mpi_mod_residue *A,
245 const mbedtls_mpi_mod_modulus *N )
246{
247 if( X->limbs != N->limbs || A->limbs != N->limbs )
248 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
249
250 /* Zero has the same value regardless of Montgomery form or not */
251 if( mbedtls_mpi_core_check_zero_ct( A->p, A->limbs ) == 0 )
252 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
253
Tom Cosgrove4302d022022-12-13 10:46:39 +0000254 size_t working_limbs =
255 mbedtls_mpi_mod_raw_inv_prime_working_limbs( N->limbs );
256
257 mbedtls_mpi_uint *working_memory = mbedtls_calloc( working_limbs,
258 sizeof(mbedtls_mpi_uint) );
259 if( working_memory == NULL )
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000260 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
261
262 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
263
264 switch( N->int_rep )
Tom Cosgrove4302d022022-12-13 10:46:39 +0000265 {
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000266 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
267 ret = mbedtls_mpi_mod_inv_mont( X, A, N, working_memory );
268 break;
269 case MBEDTLS_MPI_MOD_REP_OPT_RED:
270 ret = mbedtls_mpi_mod_inv_non_mont( X, A, N, working_memory );
271 break;
272 default:
273 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
274 break;
Tom Cosgrove4302d022022-12-13 10:46:39 +0000275 }
276
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000277 mbedtls_platform_zeroize( working_memory,
278 working_limbs * sizeof(mbedtls_mpi_uint) );
279 free( working_memory );
Tom Cosgrove4302d022022-12-13 10:46:39 +0000280
Tom Cosgrovea9e0f952022-12-13 11:57:57 +0000281 return ret;
Tom Cosgrove4302d022022-12-13 10:46:39 +0000282}
Janos Follath5933f692022-11-02 14:35:17 +0000283/* END MERGE SLOT 3 */
284
285/* BEGIN MERGE SLOT 4 */
286
287/* END MERGE SLOT 4 */
288
289/* BEGIN MERGE SLOT 5 */
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000290int mbedtls_mpi_mod_add( mbedtls_mpi_mod_residue *X,
291 const mbedtls_mpi_mod_residue *A,
292 const mbedtls_mpi_mod_residue *B,
293 const mbedtls_mpi_mod_modulus *N )
294{
295 if( X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs )
296 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Janos Follath5933f692022-11-02 14:35:17 +0000297
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000298 mbedtls_mpi_mod_raw_add(X->p, A->p, B->p, N);
299
300 return( 0 );
301}
Janos Follath5933f692022-11-02 14:35:17 +0000302/* END MERGE SLOT 5 */
303
304/* BEGIN MERGE SLOT 6 */
305
306/* END MERGE SLOT 6 */
307
308/* BEGIN MERGE SLOT 7 */
Minos Galanakis81f4b112022-11-10 14:40:38 +0000309int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
Minos Galanakis8b375452022-11-24 11:04:11 +0000310 const mbedtls_mpi_mod_modulus *m,
311 const unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000312 size_t buflen,
313 mbedtls_mpi_mod_ext_rep ext_rep )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000314{
315 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Janos Follath5933f692022-11-02 14:35:17 +0000316
Janos Follath75b9f0f2022-11-26 14:28:50 +0000317 /* Do our best to check if r and m have been set up */
Janos Follath6eb92c02022-11-26 17:34:37 +0000318 if( r->limbs == 0 || m->limbs == 0 )
Janos Follath75b9f0f2022-11-26 14:28:50 +0000319 goto cleanup;
Janos Follath6eb92c02022-11-26 17:34:37 +0000320 if( r->limbs != m->limbs )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000321 goto cleanup;
322
Janos Follath3e3fc912022-11-24 18:02:46 +0000323 ret = mbedtls_mpi_mod_raw_read( r->p, m, buf, buflen, ext_rep );
Minos Galanakis81f4b112022-11-10 14:40:38 +0000324 if( ret != 0 )
325 goto cleanup;
326
Minos Galanakis8b375452022-11-24 11:04:11 +0000327 r->limbs = m->limbs;
328
Janos Follath6eb92c02022-11-26 17:34:37 +0000329 if( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
330 ret = mbedtls_mpi_mod_raw_to_mont_rep( r->p, m );
Minos Galanakis81f4b112022-11-10 14:40:38 +0000331
332cleanup:
333 return ( ret );
334}
335
Minos Galanakis8b375452022-11-24 11:04:11 +0000336int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
337 const mbedtls_mpi_mod_modulus *m,
Minos Galanakis81f4b112022-11-10 14:40:38 +0000338 unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000339 size_t buflen,
340 mbedtls_mpi_mod_ext_rep ext_rep )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000341{
342 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
343
Janos Follath75b9f0f2022-11-26 14:28:50 +0000344 /* Do our best to check if r and m have been set up */
Janos Follath6eb92c02022-11-26 17:34:37 +0000345 if( r->limbs == 0 || m->limbs == 0 )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000346 goto cleanup;
Janos Follath6eb92c02022-11-26 17:34:37 +0000347 if( r->limbs != m->limbs )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000348 goto cleanup;
349
Janos Follath6eb92c02022-11-26 17:34:37 +0000350 if( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
Janos Follath8dfc8c42022-11-26 15:39:02 +0000351 {
Janos Follath84bee4c2022-11-28 10:27:14 +0000352 ret = mbedtls_mpi_mod_raw_from_mont_rep( r->p, m );
353 if( ret != 0 )
Janos Follath8dfc8c42022-11-26 15:39:02 +0000354 goto cleanup;
355 }
Minos Galanakis81f4b112022-11-10 14:40:38 +0000356
Janos Follath3e3fc912022-11-24 18:02:46 +0000357 ret = mbedtls_mpi_mod_raw_write( r->p, m, buf, buflen, ext_rep );
Minos Galanakis81f4b112022-11-10 14:40:38 +0000358
Janos Follath6eb92c02022-11-26 17:34:37 +0000359 if( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
Janos Follath84bee4c2022-11-28 10:27:14 +0000360 {
361 /* If this fails, the value of r is corrupted and we want to return
362 * this error (as opposed to the error code from the write above) to
363 * let the caller know. If it succeeds, we want to return the error
364 * code from write above. */
365 int conv_ret = mbedtls_mpi_mod_raw_to_mont_rep( r->p, m );
366 if( ret == 0 )
367 ret = conv_ret;
368 }
Janos Follath8dfc8c42022-11-26 15:39:02 +0000369
Minos Galanakis81f4b112022-11-10 14:40:38 +0000370cleanup:
Janos Follath8dfc8c42022-11-26 15:39:02 +0000371
Minos Galanakis81f4b112022-11-10 14:40:38 +0000372 return ( ret );
373}
Janos Follath5933f692022-11-02 14:35:17 +0000374/* END MERGE SLOT 7 */
375
376/* BEGIN MERGE SLOT 8 */
377
378/* END MERGE SLOT 8 */
379
380/* BEGIN MERGE SLOT 9 */
381
382/* END MERGE SLOT 9 */
383
384/* BEGIN MERGE SLOT 10 */
385
386/* END MERGE SLOT 10 */
387
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200388#endif /* MBEDTLS_BIGNUM_C */