blob: 7c17e560296d0d3e364e11a6374ac76d2b894040 [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 Follathaf3f39c2022-08-22 09:06:32 +010055 size_t input_length )
Janos Follath0ded6312022-08-09 13:34:54 +010056{
57 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
58
Janos Follath296ea662022-08-11 14:58:29 +010059 switch( m->ext_rep )
60 {
61 case MBEDTLS_MPI_MOD_EXT_REP_LE:
Janos Follathb7a88ec2022-08-19 12:24:40 +010062 ret = mbedtls_mpi_core_read_le( X, m->limbs,
Janos Follathaf3f39c2022-08-22 09:06:32 +010063 input, input_length );
Janos Follath296ea662022-08-11 14:58:29 +010064 break;
65 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Janos Follathb7a88ec2022-08-19 12:24:40 +010066 ret = mbedtls_mpi_core_read_be( X, m->limbs,
Janos Follathaf3f39c2022-08-22 09:06:32 +010067 input, input_length );
Janos Follath296ea662022-08-11 14:58:29 +010068 break;
69 default:
70 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
71 }
Janos Follath0ded6312022-08-09 13:34:54 +010072
73 if( ret != 0 )
74 goto cleanup;
75
Gabor Mezeifd65e822022-08-12 18:09:12 +020076 if( !mbedtls_mpi_core_lt_ct( X, m->p, m->limbs ) )
Janos Follath0ded6312022-08-09 13:34:54 +010077 {
78 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
79 goto cleanup;
80 }
81
82cleanup:
83
84 return( ret );
85}
86
Janos Follathb7a88ec2022-08-19 12:24:40 +010087int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A,
Janos Follath6b8a4ad2022-08-19 10:58:34 +010088 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +010089 unsigned char *output,
90 size_t output_length )
Janos Follath0ded6312022-08-09 13:34:54 +010091{
Janos Follath296ea662022-08-11 14:58:29 +010092 switch( m->ext_rep )
93 {
94 case MBEDTLS_MPI_MOD_EXT_REP_LE:
Janos Follathb7a88ec2022-08-19 12:24:40 +010095 return( mbedtls_mpi_core_write_le( A, m->limbs,
96 output, output_length ) );
Janos Follath296ea662022-08-11 14:58:29 +010097 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Janos Follathb7a88ec2022-08-19 12:24:40 +010098 return( mbedtls_mpi_core_write_be( A, m->limbs,
99 output, output_length ) );
Janos Follath296ea662022-08-11 14:58:29 +0100100 default:
101 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
102 }
Janos Follath0ded6312022-08-09 13:34:54 +0100103}
104
Janos Follath5933f692022-11-02 14:35:17 +0000105/* BEGIN MERGE SLOT 1 */
106
107/* END MERGE SLOT 1 */
108
109/* BEGIN MERGE SLOT 2 */
110
111/* END MERGE SLOT 2 */
112
113/* BEGIN MERGE SLOT 3 */
114
115/* END MERGE SLOT 3 */
116
117/* BEGIN MERGE SLOT 4 */
118
119/* END MERGE SLOT 4 */
120
121/* BEGIN MERGE SLOT 5 */
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000122void MPI_CORE(add_mod)( mbedtls_mpi_uint *X,
123 mbedtls_mpi_uint const *A,
124 mbedtls_mpi_uint const *B,
125 const mbedtls_mpi_uint *N,
126 size_t n )
127{
128 size_t carry, borrow = 0, fixup;
129 carry = mbedtls_mpi_core_add( X, A, B, n );
130 borrow = mbedtls_mpi_core_sub( X, X, N, n);
131 fixup = ( carry < borrow );
132 (void) mbedtls_mpi_core_add_if( X, N, n, fixup );
133}
Janos Follath5933f692022-11-02 14:35:17 +0000134/* END MERGE SLOT 5 */
135
136/* BEGIN MERGE SLOT 6 */
137
138/* END MERGE SLOT 6 */
139
140/* BEGIN MERGE SLOT 7 */
Minos Galanakisd9299c32022-11-01 16:19:07 +0000141int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X,
142 const mbedtls_mpi_mod_modulus *m )
Hanno Becker5ad4a932022-08-09 14:45:53 +0100143{
Minos Galanakisd9299c32022-11-01 16:19:07 +0000144 mbedtls_mpi_uint *T;
145 const size_t t_limbs = m->limbs * 2 + 1;
146
147 if( ( T = (mbedtls_mpi_uint *) mbedtls_calloc( t_limbs, ciL ) ) == NULL )
148 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
149
150 mbedtls_mpi_core_montmul( X, X, m->rep.mont.rr, m->limbs, m->p, m->limbs,
151 m->rep.mont.mm, T );
152
153 mbedtls_platform_zeroize( T, t_limbs * ciL );
154 mbedtls_free( T );
Hanno Becker5ad4a932022-08-09 14:45:53 +0100155 return( 0 );
156}
Janos Follath5933f692022-11-02 14:35:17 +0000157
Minos Galanakisd9299c32022-11-01 16:19:07 +0000158int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X,
159 const mbedtls_mpi_mod_modulus *m )
Hanno Becker5ad4a932022-08-09 14:45:53 +0100160{
Minos Galanakisd9299c32022-11-01 16:19:07 +0000161 const mbedtls_mpi_uint one = 1;
162 const size_t t_limbs = m->limbs * 2 + 1;
163 mbedtls_mpi_uint *T;
164
165 if( ( T = (mbedtls_mpi_uint *) mbedtls_calloc( t_limbs, ciL ) ) == NULL )
166 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
167
168 mbedtls_mpi_core_montmul( X, X, &one, 1, m->p, m->limbs,
169 m->rep.mont.mm, T );
170
171 mbedtls_platform_zeroize( T, t_limbs * ciL );
172 mbedtls_free( T );
Hanno Becker5ad4a932022-08-09 14:45:53 +0100173 return( 0 );
174}
Janos Follath5933f692022-11-02 14:35:17 +0000175/* END MERGE SLOT 7 */
176
177/* BEGIN MERGE SLOT 8 */
178
179/* END MERGE SLOT 8 */
180
181/* BEGIN MERGE SLOT 9 */
182
183/* END MERGE SLOT 9 */
184
185/* BEGIN MERGE SLOT 10 */
186
187/* END MERGE SLOT 10 */
188
Janos Follath0ded6312022-08-09 13:34:54 +0100189#endif /* MBEDTLS_BIGNUM_C */