blob: 92c011cffe341496782e05d20fd4c80021340cd4 [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 Follathb7a88ec2022-08-19 12:24:40 +010042 if( p_limbs < m->limbs || !mbedtls_mpi_core_lt_ct( m->p, p, p_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 Galanakis8b333632022-10-11 11:28:24 +010080 mbedtls_platform_zeroize( (mbedtls_mpi_uint *) m->rep.mont.rr,
81 m->limbs );
82 mbedtls_free( (mbedtls_mpi_uint *)m->rep.mont.rr );
Minos Galanakis760f5d62022-08-11 12:21:09 +010083 m->rep.mont.rr = NULL;
84 m->rep.mont.mm = 0; break;
Janos Follathba5c1392022-07-19 13:42:07 +010085 case MBEDTLS_MPI_MOD_REP_OPT_RED:
Janos Follath296ea662022-08-11 14:58:29 +010086 mbedtls_free( m->rep.ored );
87 break;
88 case MBEDTLS_MPI_MOD_REP_INVALID:
Janos Follathba5c1392022-07-19 13:42:07 +010089 break;
90 }
91
Gabor Mezeif049dbf2022-07-18 23:02:33 +020092 m->p = NULL;
Gabor Mezeifd65e822022-08-12 18:09:12 +020093 m->limbs = 0;
94 m->bits = 0;
Janos Follath281ccda2022-07-19 13:14:36 +010095 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
96 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020097}
98
Minos Galanakis8b333632022-10-11 11:28:24 +010099static int set_mont_const_square( const mbedtls_mpi_uint **X,
100 const mbedtls_mpi_uint *A,
101 size_t limbs )
102{
103 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
104 mbedtls_mpi N;
105 mbedtls_mpi RR;
106
107 mbedtls_mpi_init( &N );
108 mbedtls_mpi_init( &RR );
109
110 if ( A == NULL || limbs == 0 || limbs >= ( MBEDTLS_MPI_MAX_LIMBS / 2 ) - 2 )
111 goto cleanup;
112
113 if ( !mbedtls_mpi_grow( &N, limbs ))
114 memcpy( N.p, A, sizeof(mbedtls_mpi_uint) * limbs );
115 else
116 goto cleanup;
117
118 mbedtls_mpi_core_get_mont_r2_unsafe(&RR, &N);
119
120 *X = RR.p;
121 RR.p = NULL;
122 ret = 0;
123
124cleanup:
125 mbedtls_mpi_free(&N);
126 mbedtls_mpi_free(&RR);
127 ret = ( ret != 0 ) ? MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED : 0;
128 return( ret );
129}
130
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200131int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Janos Follathed5c8d32022-08-15 11:50:22 +0100132 const mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100133 size_t p_limbs,
Janos Follath296ea662022-08-11 14:58:29 +0100134 mbedtls_mpi_mod_ext_rep ext_rep,
135 mbedtls_mpi_mod_rep_selector int_rep )
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200136{
Janos Follathba5c1392022-07-19 13:42:07 +0100137 int ret = 0;
138
Gabor Mezei535f36d2022-08-02 11:50:44 +0200139 m->p = p;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100140 m->limbs = p_limbs;
141 m->bits = mbedtls_mpi_core_bitlen( p, p_limbs );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200142
Janos Follathba5c1392022-07-19 13:42:07 +0100143 switch( ext_rep )
144 {
145 case MBEDTLS_MPI_MOD_EXT_REP_LE:
146 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Janos Follath296ea662022-08-11 14:58:29 +0100147 m->ext_rep = ext_rep;
148 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100149 default:
150 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
151 goto exit;
152 }
153
154 switch( int_rep )
155 {
156 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
157 m->int_rep = int_rep;
Minos Galanakis8b333632022-10-11 11:28:24 +0100158 m->rep.mont.mm = mbedtls_mpi_core_montmul_init( m->p );
159 set_mont_const_square( &m->rep.mont.rr, m->p, m->limbs );
160 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100161 case MBEDTLS_MPI_MOD_REP_OPT_RED:
162 m->int_rep = int_rep;
Janos Follath296ea662022-08-11 14:58:29 +0100163 m->rep.ored = NULL;
164 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100165 default:
166 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
167 goto exit;
168 }
169
170exit:
171
172 if( ret != 0 )
173 {
174 mbedtls_mpi_mod_modulus_free( m );
175 }
176
177 return( ret );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200178}
179
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200180#endif /* MBEDTLS_BIGNUM_C */