blob: 705b2dbae6a36373a235044875c142cc0a3c0734 [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;
Minos Galanakis771c4702022-10-27 12:22:22 +010084 m->rep.mont.mm = 0;
85 break;
Janos Follathba5c1392022-07-19 13:42:07 +010086 case MBEDTLS_MPI_MOD_REP_OPT_RED:
Janos Follath296ea662022-08-11 14:58:29 +010087 mbedtls_free( m->rep.ored );
88 break;
89 case MBEDTLS_MPI_MOD_REP_INVALID:
Janos Follathba5c1392022-07-19 13:42:07 +010090 break;
91 }
92
Gabor Mezeif049dbf2022-07-18 23:02:33 +020093 m->p = NULL;
Gabor Mezeifd65e822022-08-12 18:09:12 +020094 m->limbs = 0;
95 m->bits = 0;
Janos Follath281ccda2022-07-19 13:14:36 +010096 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
97 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020098}
99
Minos Galanakis8b333632022-10-11 11:28:24 +0100100static int set_mont_const_square( const mbedtls_mpi_uint **X,
101 const mbedtls_mpi_uint *A,
102 size_t limbs )
103{
104 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
105 mbedtls_mpi N;
106 mbedtls_mpi RR;
107
108 mbedtls_mpi_init( &N );
109 mbedtls_mpi_init( &RR );
110
111 if ( A == NULL || limbs == 0 || limbs >= ( MBEDTLS_MPI_MAX_LIMBS / 2 ) - 2 )
112 goto cleanup;
113
Minos Galanakis771c4702022-10-27 12:22:22 +0100114 if ( mbedtls_mpi_grow( &N, limbs ))
Minos Galanakis8b333632022-10-11 11:28:24 +0100115 goto cleanup;
116
Minos Galanakis771c4702022-10-27 12:22:22 +0100117 memcpy( N.p, A, sizeof(mbedtls_mpi_uint) * limbs );
118
Minos Galanakis8b333632022-10-11 11:28:24 +0100119 mbedtls_mpi_core_get_mont_r2_unsafe(&RR, &N);
120
121 *X = RR.p;
122 RR.p = NULL;
123 ret = 0;
124
125cleanup:
126 mbedtls_mpi_free(&N);
127 mbedtls_mpi_free(&RR);
128 ret = ( ret != 0 ) ? MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED : 0;
129 return( ret );
130}
131
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200132int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Janos Follathed5c8d32022-08-15 11:50:22 +0100133 const mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100134 size_t p_limbs,
Janos Follath296ea662022-08-11 14:58:29 +0100135 mbedtls_mpi_mod_ext_rep ext_rep,
136 mbedtls_mpi_mod_rep_selector int_rep )
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200137{
Janos Follathba5c1392022-07-19 13:42:07 +0100138 int ret = 0;
139
Gabor Mezei535f36d2022-08-02 11:50:44 +0200140 m->p = p;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100141 m->limbs = p_limbs;
142 m->bits = mbedtls_mpi_core_bitlen( p, p_limbs );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200143
Janos Follathba5c1392022-07-19 13:42:07 +0100144 switch( ext_rep )
145 {
146 case MBEDTLS_MPI_MOD_EXT_REP_LE:
147 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Janos Follath296ea662022-08-11 14:58:29 +0100148 m->ext_rep = ext_rep;
149 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100150 default:
151 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
152 goto exit;
153 }
154
155 switch( int_rep )
156 {
157 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
158 m->int_rep = int_rep;
Minos Galanakis8b333632022-10-11 11:28:24 +0100159 m->rep.mont.mm = mbedtls_mpi_core_montmul_init( m->p );
160 set_mont_const_square( &m->rep.mont.rr, m->p, m->limbs );
161 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100162 case MBEDTLS_MPI_MOD_REP_OPT_RED:
163 m->int_rep = int_rep;
Janos Follath296ea662022-08-11 14:58:29 +0100164 m->rep.ored = NULL;
165 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100166 default:
167 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
168 goto exit;
169 }
170
171exit:
172
173 if( ret != 0 )
174 {
175 mbedtls_mpi_mod_modulus_free( m );
176 }
177
178 return( ret );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200179}
180
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200181#endif /* MBEDTLS_BIGNUM_C */