blob: 0f2d7e23afdf397fb7e97a605dcbc324b78f0499 [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,
82 m->limbs );
83 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 */
182
183/* END MERGE SLOT 3 */
184
185/* BEGIN MERGE SLOT 4 */
186
187/* END MERGE SLOT 4 */
188
189/* BEGIN MERGE SLOT 5 */
190
191/* END MERGE SLOT 5 */
192
193/* BEGIN MERGE SLOT 6 */
194
195/* END MERGE SLOT 6 */
196
197/* BEGIN MERGE SLOT 7 */
Minos Galanakis81f4b112022-11-10 14:40:38 +0000198int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
Minos Galanakis8b375452022-11-24 11:04:11 +0000199 const mbedtls_mpi_mod_modulus *m,
200 const unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000201 size_t buflen,
202 mbedtls_mpi_mod_ext_rep ext_rep )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000203{
204 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Janos Follath5933f692022-11-02 14:35:17 +0000205
Minos Galanakis81f4b112022-11-10 14:40:38 +0000206
Janos Follath75b9f0f2022-11-26 14:28:50 +0000207 /* Do our best to check if r and m have been set up */
Janos Follath6eb92c02022-11-26 17:34:37 +0000208 if( r->limbs == 0 || m->limbs == 0 )
Janos Follath75b9f0f2022-11-26 14:28:50 +0000209 goto cleanup;
Janos Follath6eb92c02022-11-26 17:34:37 +0000210 if( r->limbs != m->limbs )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000211 goto cleanup;
212
Janos Follath3e3fc912022-11-24 18:02:46 +0000213 ret = mbedtls_mpi_mod_raw_read( r->p, m, buf, buflen, ext_rep );
Minos Galanakis81f4b112022-11-10 14:40:38 +0000214 if( ret != 0 )
215 goto cleanup;
216
Minos Galanakis8b375452022-11-24 11:04:11 +0000217 r->limbs = m->limbs;
218
Janos Follath6eb92c02022-11-26 17:34:37 +0000219 if( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
220 ret = mbedtls_mpi_mod_raw_to_mont_rep( r->p, m );
Minos Galanakis81f4b112022-11-10 14:40:38 +0000221
222cleanup:
223 return ( ret );
224}
225
Minos Galanakis8b375452022-11-24 11:04:11 +0000226int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
227 const mbedtls_mpi_mod_modulus *m,
Minos Galanakis81f4b112022-11-10 14:40:38 +0000228 unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000229 size_t buflen,
230 mbedtls_mpi_mod_ext_rep ext_rep )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000231{
232 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
233
Janos Follath75b9f0f2022-11-26 14:28:50 +0000234 /* Do our best to check if r and m have been set up */
Janos Follath6eb92c02022-11-26 17:34:37 +0000235 if( r->limbs == 0 || m->limbs == 0 )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000236 goto cleanup;
Janos Follath6eb92c02022-11-26 17:34:37 +0000237 if( r->limbs != m->limbs )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000238 goto cleanup;
239
Janos Follath6eb92c02022-11-26 17:34:37 +0000240 if( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
Janos Follath8dfc8c42022-11-26 15:39:02 +0000241 {
Janos Follath84bee4c2022-11-28 10:27:14 +0000242 ret = mbedtls_mpi_mod_raw_from_mont_rep( r->p, m );
243 if( ret != 0 )
Janos Follath8dfc8c42022-11-26 15:39:02 +0000244 goto cleanup;
245 }
Minos Galanakis81f4b112022-11-10 14:40:38 +0000246
Janos Follath3e3fc912022-11-24 18:02:46 +0000247 ret = mbedtls_mpi_mod_raw_write( r->p, m, buf, buflen, ext_rep );
Minos Galanakis81f4b112022-11-10 14:40:38 +0000248
Janos Follath6eb92c02022-11-26 17:34:37 +0000249 if( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
Janos Follath84bee4c2022-11-28 10:27:14 +0000250 {
251 /* If this fails, the value of r is corrupted and we want to return
252 * this error (as opposed to the error code from the write above) to
253 * let the caller know. If it succeeds, we want to return the error
254 * code from write above. */
255 int conv_ret = mbedtls_mpi_mod_raw_to_mont_rep( r->p, m );
256 if( ret == 0 )
257 ret = conv_ret;
258 }
Janos Follath8dfc8c42022-11-26 15:39:02 +0000259
Minos Galanakis81f4b112022-11-10 14:40:38 +0000260cleanup:
Janos Follath8dfc8c42022-11-26 15:39:02 +0000261
Minos Galanakis81f4b112022-11-10 14:40:38 +0000262 return ( ret );
263}
Janos Follath5933f692022-11-02 14:35:17 +0000264/* END MERGE SLOT 7 */
265
266/* BEGIN MERGE SLOT 8 */
267
268/* END MERGE SLOT 8 */
269
270/* BEGIN MERGE SLOT 9 */
271
272/* END MERGE SLOT 9 */
273
274/* BEGIN MERGE SLOT 10 */
275
276/* END MERGE SLOT 10 */
277
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200278#endif /* MBEDTLS_BIGNUM_C */