blob: fa4831c7ac43584bd4e413d63c6a496e7d9394d7 [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{
53 if ( r == NULL )
54 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{
62 if ( m == NULL )
63 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->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
69 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020070}
71
72void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
73{
74 if ( m == NULL )
75 return;
76
Janos Follathba5c1392022-07-19 13:42:07 +010077 switch( m->int_rep )
78 {
79 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
Minos Galanakis4d4c98b2022-10-27 15:58:02 +010080 if (m->rep.mont.rr != NULL)
81 {
82 mbedtls_platform_zeroize( (mbedtls_mpi_uint *) m->rep.mont.rr,
83 m->limbs );
84 mbedtls_free( (mbedtls_mpi_uint *)m->rep.mont.rr );
85 m->rep.mont.rr = NULL;
86 }
Minos Galanakis771c4702022-10-27 12:22:22 +010087 m->rep.mont.mm = 0;
88 break;
Janos Follathba5c1392022-07-19 13:42:07 +010089 case MBEDTLS_MPI_MOD_REP_OPT_RED:
Janos Follath296ea662022-08-11 14:58:29 +010090 mbedtls_free( m->rep.ored );
91 break;
92 case MBEDTLS_MPI_MOD_REP_INVALID:
Janos Follathba5c1392022-07-19 13:42:07 +010093 break;
94 }
95
Gabor Mezeif049dbf2022-07-18 23:02:33 +020096 m->p = NULL;
Gabor Mezeifd65e822022-08-12 18:09:12 +020097 m->limbs = 0;
98 m->bits = 0;
Janos Follath281ccda2022-07-19 13:14:36 +010099 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
100 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200101}
102
Minos Galanakis8b333632022-10-11 11:28:24 +0100103static int set_mont_const_square( const mbedtls_mpi_uint **X,
104 const mbedtls_mpi_uint *A,
105 size_t limbs )
106{
107 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
108 mbedtls_mpi N;
109 mbedtls_mpi RR;
Minos Galanakis4d4c98b2022-10-27 15:58:02 +0100110 *X = NULL;
Minos Galanakis8b333632022-10-11 11:28:24 +0100111
112 mbedtls_mpi_init( &N );
113 mbedtls_mpi_init( &RR );
114
115 if ( A == NULL || limbs == 0 || limbs >= ( MBEDTLS_MPI_MAX_LIMBS / 2 ) - 2 )
116 goto cleanup;
117
Minos Galanakis4d4c98b2022-10-27 15:58:02 +0100118 if ( mbedtls_mpi_grow( &N, limbs ) )
Minos Galanakis8b333632022-10-11 11:28:24 +0100119 goto cleanup;
120
Minos Galanakis4d4c98b2022-10-27 15:58:02 +0100121 memcpy( N.p, A, sizeof(mbedtls_mpi_uint) * limbs );
Minos Galanakis771c4702022-10-27 12:22:22 +0100122
Minos Galanakis4d4c98b2022-10-27 15:58:02 +0100123 ret = mbedtls_mpi_core_get_mont_r2_unsafe(&RR, &N);
Minos Galanakis8b333632022-10-11 11:28:24 +0100124
Minos Galanakis4d4c98b2022-10-27 15:58:02 +0100125 if ( ret == 0 )
126 {
127 *X = RR.p;
128 RR.p = NULL;
129 }
Minos Galanakis8b333632022-10-11 11:28:24 +0100130
131cleanup:
132 mbedtls_mpi_free(&N);
133 mbedtls_mpi_free(&RR);
134 ret = ( ret != 0 ) ? MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED : 0;
135 return( ret );
136}
137
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200138int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Janos Follathed5c8d32022-08-15 11:50:22 +0100139 const mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100140 size_t p_limbs,
Janos Follath296ea662022-08-11 14:58:29 +0100141 mbedtls_mpi_mod_ext_rep ext_rep,
142 mbedtls_mpi_mod_rep_selector int_rep )
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200143{
Janos Follathba5c1392022-07-19 13:42:07 +0100144 int ret = 0;
145
Gabor Mezei535f36d2022-08-02 11:50:44 +0200146 m->p = p;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100147 m->limbs = p_limbs;
148 m->bits = mbedtls_mpi_core_bitlen( p, p_limbs );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200149
Janos Follathba5c1392022-07-19 13:42:07 +0100150 switch( ext_rep )
151 {
152 case MBEDTLS_MPI_MOD_EXT_REP_LE:
153 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Janos Follath296ea662022-08-11 14:58:29 +0100154 m->ext_rep = ext_rep;
155 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100156 default:
157 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
158 goto exit;
159 }
160
161 switch( int_rep )
162 {
163 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
164 m->int_rep = int_rep;
Minos Galanakis8b333632022-10-11 11:28:24 +0100165 m->rep.mont.mm = mbedtls_mpi_core_montmul_init( m->p );
Minos Galanakis4d4c98b2022-10-27 15:58:02 +0100166 ret = set_mont_const_square( &m->rep.mont.rr, m->p, m->limbs );
Minos Galanakis8b333632022-10-11 11:28:24 +0100167 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100168 case MBEDTLS_MPI_MOD_REP_OPT_RED:
169 m->int_rep = int_rep;
Janos Follath296ea662022-08-11 14:58:29 +0100170 m->rep.ored = NULL;
171 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100172 default:
173 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
174 goto exit;
175 }
176
177exit:
178
179 if( ret != 0 )
180 {
181 mbedtls_mpi_mod_modulus_free( m );
182 }
183
184 return( ret );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200185}
186
Janos Follath5933f692022-11-02 14:35:17 +0000187/* BEGIN MERGE SLOT 1 */
188
189/* END MERGE SLOT 1 */
190
191/* BEGIN MERGE SLOT 2 */
192
193/* END MERGE SLOT 2 */
194
195/* BEGIN MERGE SLOT 3 */
196
197/* END MERGE SLOT 3 */
198
199/* BEGIN MERGE SLOT 4 */
200
201/* END MERGE SLOT 4 */
202
203/* BEGIN MERGE SLOT 5 */
204
205/* END MERGE SLOT 5 */
206
207/* BEGIN MERGE SLOT 6 */
208
209/* END MERGE SLOT 6 */
210
211/* BEGIN MERGE SLOT 7 */
Minos Galanakis81f4b112022-11-10 14:40:38 +0000212int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
Minos Galanakis8b375452022-11-24 11:04:11 +0000213 const mbedtls_mpi_mod_modulus *m,
214 const unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000215 size_t buflen,
216 mbedtls_mpi_mod_ext_rep ext_rep )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000217{
218 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Janos Follath5933f692022-11-02 14:35:17 +0000219
Minos Galanakis81f4b112022-11-10 14:40:38 +0000220 if ( r == NULL || m == NULL )
221 goto cleanup;
222
Minos Galanakis8b375452022-11-24 11:04:11 +0000223 if ( r->p == NULL || m->p == NULL || r->limbs > m->limbs ||
Minos Galanakis81f4b112022-11-10 14:40:38 +0000224 r->limbs == 0 || m->limbs == 0 )
225 goto cleanup;
226
Janos Follath3e3fc912022-11-24 18:02:46 +0000227 ret = mbedtls_mpi_mod_raw_read( r->p, m, buf, buflen, ext_rep );
Minos Galanakis81f4b112022-11-10 14:40:38 +0000228
229 if( ret != 0 )
230 goto cleanup;
231
Minos Galanakis8b375452022-11-24 11:04:11 +0000232 r->limbs = m->limbs;
233
Minos Galanakis81f4b112022-11-10 14:40:38 +0000234 if (m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY)
235 ret = mbedtls_mpi_mod_raw_to_mont_rep(r->p, m);
236
237cleanup:
238 return ( ret );
239}
240
Minos Galanakis8b375452022-11-24 11:04:11 +0000241int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
242 const mbedtls_mpi_mod_modulus *m,
Minos Galanakis81f4b112022-11-10 14:40:38 +0000243 unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000244 size_t buflen,
245 mbedtls_mpi_mod_ext_rep ext_rep )
Minos Galanakis81f4b112022-11-10 14:40:38 +0000246{
247 int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
248
249 if ( r == NULL || m == NULL )
250 goto cleanup;
251
Minos Galanakis8b375452022-11-24 11:04:11 +0000252 if ( r->p == NULL || m->p == NULL || r->limbs > m->limbs ||
Minos Galanakis81f4b112022-11-10 14:40:38 +0000253 r->limbs == 0 || m->limbs == 0 )
254 goto cleanup;
255
256 if ( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY)
257 ret = mbedtls_mpi_mod_raw_from_mont_rep( r->p, m );
258
Janos Follath3e3fc912022-11-24 18:02:46 +0000259 ret = mbedtls_mpi_mod_raw_write( r->p, m, buf, buflen, ext_rep );
Minos Galanakis81f4b112022-11-10 14:40:38 +0000260
261cleanup:
262 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 */