blob: 721efc8538a94282f9f35ea19ff30b0967150e98 [file] [log] [blame]
Janos Follath0ded6312022-08-09 13:34:54 +01001/*
Janos Follatha95f2042022-08-19 12:09:17 +01002 * Low-level modular bignum functions
Janos Follath0ded6312022-08-09 13:34:54 +01003 *
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
24#include <string.h>
25
26#include "mbedtls/error.h"
27#include "mbedtls/platform_util.h"
28
Janos Follath0ded6312022-08-09 13:34:54 +010029#include "mbedtls/platform.h"
Janos Follath0ded6312022-08-09 13:34:54 +010030
31#include "bignum_core.h"
32#include "bignum_mod_raw.h"
33#include "bignum_mod.h"
34#include "constant_time_internal.h"
35
Gabor Mezei63c32822022-09-15 20:01:31 +020036void mbedtls_mpi_mod_raw_cond_assign( mbedtls_mpi_uint *X,
Gabor Mezei1c628d52022-09-27 12:13:51 +020037 const mbedtls_mpi_uint *A,
Gabor Mezeie5b85852022-09-30 13:54:02 +020038 const mbedtls_mpi_mod_modulus *N,
Gabor Mezei63c32822022-09-15 20:01:31 +020039 unsigned char assign )
Gabor Mezei12071d42022-09-12 16:35:58 +020040{
Gabor Mezeie5b85852022-09-30 13:54:02 +020041 mbedtls_mpi_core_cond_assign( X, A, N->limbs, assign );
Gabor Mezei12071d42022-09-12 16:35:58 +020042}
43
Gabor Mezeie5b85852022-09-30 13:54:02 +020044void mbedtls_mpi_mod_raw_cond_swap( mbedtls_mpi_uint *X,
45 mbedtls_mpi_uint *Y,
46 const mbedtls_mpi_mod_modulus *N,
Gabor Mezei63c32822022-09-15 20:01:31 +020047 unsigned char swap )
Gabor Mezei12071d42022-09-12 16:35:58 +020048{
Gabor Mezeie5b85852022-09-30 13:54:02 +020049 mbedtls_mpi_core_cond_swap( X, Y, N->limbs, swap );
Gabor Mezei12071d42022-09-12 16:35:58 +020050}
51
Janos Follath0ded6312022-08-09 13:34:54 +010052int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
Janos Follath6b8a4ad2022-08-19 10:58:34 +010053 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +010054 const unsigned char *input,
Janos Follathd3eed332022-11-24 17:42:02 +000055 size_t input_length,
56 mbedtls_mpi_mod_ext_rep ext_rep )
Janos Follath0ded6312022-08-09 13:34:54 +010057{
58 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
59
Janos Follathd3eed332022-11-24 17:42:02 +000060 switch( ext_rep )
Janos Follath296ea662022-08-11 14:58:29 +010061 {
62 case MBEDTLS_MPI_MOD_EXT_REP_LE:
Janos Follathb7a88ec2022-08-19 12:24:40 +010063 ret = mbedtls_mpi_core_read_le( X, m->limbs,
Janos Follathaf3f39c2022-08-22 09:06:32 +010064 input, input_length );
Janos Follath296ea662022-08-11 14:58:29 +010065 break;
66 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Janos Follathb7a88ec2022-08-19 12:24:40 +010067 ret = mbedtls_mpi_core_read_be( X, m->limbs,
Janos Follathaf3f39c2022-08-22 09:06:32 +010068 input, input_length );
Janos Follath296ea662022-08-11 14:58:29 +010069 break;
70 default:
71 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
72 }
Janos Follath0ded6312022-08-09 13:34:54 +010073
74 if( ret != 0 )
75 goto cleanup;
76
Gabor Mezeifd65e822022-08-12 18:09:12 +020077 if( !mbedtls_mpi_core_lt_ct( X, m->p, m->limbs ) )
Janos Follath0ded6312022-08-09 13:34:54 +010078 {
79 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
80 goto cleanup;
81 }
82
83cleanup:
84
85 return( ret );
86}
87
Janos Follathb7a88ec2022-08-19 12:24:40 +010088int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A,
Janos Follath6b8a4ad2022-08-19 10:58:34 +010089 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +010090 unsigned char *output,
Janos Follathd3eed332022-11-24 17:42:02 +000091 size_t output_length,
92 mbedtls_mpi_mod_ext_rep ext_rep )
Janos Follath0ded6312022-08-09 13:34:54 +010093{
Janos Follathd3eed332022-11-24 17:42:02 +000094 switch( ext_rep )
Janos Follath296ea662022-08-11 14:58:29 +010095 {
96 case MBEDTLS_MPI_MOD_EXT_REP_LE:
Janos Follathb7a88ec2022-08-19 12:24:40 +010097 return( mbedtls_mpi_core_write_le( A, m->limbs,
98 output, output_length ) );
Janos Follath296ea662022-08-11 14:58:29 +010099 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100100 return( mbedtls_mpi_core_write_be( A, m->limbs,
101 output, output_length ) );
Janos Follath296ea662022-08-11 14:58:29 +0100102 default:
103 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
104 }
Janos Follath0ded6312022-08-09 13:34:54 +0100105}
106
Janos Follath5933f692022-11-02 14:35:17 +0000107/* BEGIN MERGE SLOT 1 */
108
109/* END MERGE SLOT 1 */
110
111/* BEGIN MERGE SLOT 2 */
112
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100113void mbedtls_mpi_mod_raw_sub( mbedtls_mpi_uint *X,
114 const mbedtls_mpi_uint *A,
115 const mbedtls_mpi_uint *B,
116 const mbedtls_mpi_mod_modulus *N )
117{
118 mbedtls_mpi_uint c = mbedtls_mpi_core_sub( X, A, B, N->limbs );
119
Gabor Mezei3411e942022-11-16 11:31:00 +0100120 (void) mbedtls_mpi_core_add_if( X, N->p, N->limbs, (unsigned) c );
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100121}
122
Janos Follath5933f692022-11-02 14:35:17 +0000123/* END MERGE SLOT 2 */
124
125/* BEGIN MERGE SLOT 3 */
126
Tom Cosgrove61292682022-12-08 09:44:10 +0000127size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs( size_t AN_limbs )
128{
129 /* mbedtls_mpi_mod_raw_inv_prime() needs a temporary for the exponent,
130 * which will be the same size as the modulus and input (AN_limbs),
131 * and additional space to pass to mbedtls_mpi_core_exp_mod(). */
132 return( AN_limbs +
133 mbedtls_mpi_core_exp_mod_working_limbs( AN_limbs, AN_limbs ) );
134}
135
136void mbedtls_mpi_mod_raw_inv_prime( mbedtls_mpi_uint *X,
137 const mbedtls_mpi_uint *A,
138 const mbedtls_mpi_uint *N,
139 size_t AN_limbs,
140 const mbedtls_mpi_uint *RR,
141 mbedtls_mpi_uint *T )
142{
143 /* Inversion by power: g^|G| = 1 => g^(-1) = g^(|G|-1), and
144 * |G| = N - 1, so we want
145 * g^(|G|-1) = g^(N - 2)
146 */
Tom Cosgrove5f099302022-12-09 10:58:15 +0000147
148 /* Use the first AN_limbs of T to hold N - 2 */
Tom Cosgrove61292682022-12-08 09:44:10 +0000149 mbedtls_mpi_uint *Nminus2 = T;
150 (void) mbedtls_mpi_core_sub_int( Nminus2, N, 2, AN_limbs );
151
Tom Cosgrove5f099302022-12-09 10:58:15 +0000152 /* Rest of T is given to exp_mod for its working space */
Tom Cosgrove61292682022-12-08 09:44:10 +0000153 mbedtls_mpi_core_exp_mod( X,
154 A, N, AN_limbs, Nminus2, AN_limbs,
155 RR, T + AN_limbs );
156}
157
Janos Follath5933f692022-11-02 14:35:17 +0000158/* END MERGE SLOT 3 */
159
160/* BEGIN MERGE SLOT 4 */
161
162/* END MERGE SLOT 4 */
163
164/* BEGIN MERGE SLOT 5 */
Werner Lewis0eea8272022-11-01 13:27:29 +0000165void mbedtls_mpi_mod_raw_add( mbedtls_mpi_uint *X,
Werner Lewisd391b8c2022-11-08 15:53:47 +0000166 const mbedtls_mpi_uint *A,
167 const mbedtls_mpi_uint *B,
Werner Lewis9fa91eb2022-11-01 13:36:51 +0000168 const mbedtls_mpi_mod_modulus *N )
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000169{
Werner Lewisd391b8c2022-11-08 15:53:47 +0000170 mbedtls_mpi_uint carry, borrow;
Werner Lewis9fa91eb2022-11-01 13:36:51 +0000171 carry = mbedtls_mpi_core_add( X, A, B, N->limbs );
172 borrow = mbedtls_mpi_core_sub( X, X, N->p, N->limbs );
Werner Lewise4c0a6c2022-11-17 11:19:58 +0000173 (void) mbedtls_mpi_core_add_if( X, N->p, N->limbs, (unsigned) ( carry ^ borrow ) );
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000174}
Janos Follath5933f692022-11-02 14:35:17 +0000175/* END MERGE SLOT 5 */
176
177/* BEGIN MERGE SLOT 6 */
178
Gilles Peskine1e2a4d42022-12-20 19:21:17 +0100179int mbedtls_mpi_mod_raw_canonical_to_modulus_rep(
180 mbedtls_mpi_uint *X,
181 const mbedtls_mpi_mod_modulus *N )
182{
183 switch( N->int_rep )
184 {
185 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
186 return( mbedtls_mpi_mod_raw_to_mont_rep( X, N ) );
187 case MBEDTLS_MPI_MOD_REP_OPT_RED:
188 return( 0 );
189 default:
190 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
191 }
192}
193
194int mbedtls_mpi_mod_raw_modulus_to_canonical_rep(
195 mbedtls_mpi_uint *X,
196 const mbedtls_mpi_mod_modulus *N )
197{
198 switch( N->int_rep )
199 {
200 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
201 return( mbedtls_mpi_mod_raw_from_mont_rep( X, N ) );
202 case MBEDTLS_MPI_MOD_REP_OPT_RED:
203 return( 0 );
204 default:
205 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
206 }
207}
208
Gilles Peskinea57cf982022-12-06 22:54:09 +0100209int mbedtls_mpi_mod_raw_random( mbedtls_mpi_uint *X,
210 mbedtls_mpi_uint min,
211 const mbedtls_mpi_mod_modulus *N,
212 int (*f_rng)(void *, unsigned char *, size_t),
213 void *p_rng )
214{
215 int ret = mbedtls_mpi_core_random( X, min, N->p, N->limbs, f_rng, p_rng );
216 if( ret != 0 )
217 return( ret );
218 return( mbedtls_mpi_mod_raw_to_mont_rep( X, N ) );
219}
220
Janos Follath5933f692022-11-02 14:35:17 +0000221/* END MERGE SLOT 6 */
222
223/* BEGIN MERGE SLOT 7 */
Minos Galanakisd9299c32022-11-01 16:19:07 +0000224int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X,
225 const mbedtls_mpi_mod_modulus *m )
Hanno Becker5ad4a932022-08-09 14:45:53 +0100226{
Minos Galanakisd9299c32022-11-01 16:19:07 +0000227 mbedtls_mpi_uint *T;
228 const size_t t_limbs = m->limbs * 2 + 1;
229
230 if( ( T = (mbedtls_mpi_uint *) mbedtls_calloc( t_limbs, ciL ) ) == NULL )
231 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
232
233 mbedtls_mpi_core_montmul( X, X, m->rep.mont.rr, m->limbs, m->p, m->limbs,
234 m->rep.mont.mm, T );
235
236 mbedtls_platform_zeroize( T, t_limbs * ciL );
237 mbedtls_free( T );
Hanno Becker5ad4a932022-08-09 14:45:53 +0100238 return( 0 );
239}
Janos Follath5933f692022-11-02 14:35:17 +0000240
Minos Galanakisd9299c32022-11-01 16:19:07 +0000241int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X,
242 const mbedtls_mpi_mod_modulus *m )
Hanno Becker5ad4a932022-08-09 14:45:53 +0100243{
Minos Galanakisd9299c32022-11-01 16:19:07 +0000244 const mbedtls_mpi_uint one = 1;
245 const size_t t_limbs = m->limbs * 2 + 1;
246 mbedtls_mpi_uint *T;
247
248 if( ( T = (mbedtls_mpi_uint *) mbedtls_calloc( t_limbs, ciL ) ) == NULL )
249 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
250
251 mbedtls_mpi_core_montmul( X, X, &one, 1, m->p, m->limbs,
252 m->rep.mont.mm, T );
253
254 mbedtls_platform_zeroize( T, t_limbs * ciL );
255 mbedtls_free( T );
Hanno Becker5ad4a932022-08-09 14:45:53 +0100256 return( 0 );
257}
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000258
259void mbedtls_mpi_mod_raw_neg( mbedtls_mpi_uint *X,
260 const mbedtls_mpi_uint *A,
261 const mbedtls_mpi_mod_modulus *m )
262{
263 mbedtls_mpi_core_sub( X, m->p, A, m->limbs );
264
265 /* If A=0 initially, then X=N now. Detect this by
266 * subtracting N and catching the carry. */
267 mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, X, m->p, m->limbs );
268 (void) mbedtls_mpi_core_add_if( X, m->p, m->limbs, (unsigned) borrow );
269}
Janos Follath5933f692022-11-02 14:35:17 +0000270/* END MERGE SLOT 7 */
271
272/* BEGIN MERGE SLOT 8 */
273
274/* END MERGE SLOT 8 */
275
276/* BEGIN MERGE SLOT 9 */
277
278/* END MERGE SLOT 9 */
279
280/* BEGIN MERGE SLOT 10 */
281
282/* END MERGE SLOT 10 */
283
Janos Follath0ded6312022-08-09 13:34:54 +0100284#endif /* MBEDTLS_BIGNUM_C */