Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 1 | /** |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 2 | * Modular bignum functions |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 3 | * |
| 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 Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 24 | #include <string.h> |
| 25 | |
| 26 | #include "mbedtls/platform_util.h" |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 27 | #include "mbedtls/error.h" |
| 28 | #include "mbedtls/bignum.h" |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 29 | |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 30 | #include "mbedtls/platform.h" |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 31 | |
Janos Follath | d1baedb | 2022-08-09 13:44:53 +0100 | [diff] [blame] | 32 | #include "bignum_core.h" |
| 33 | #include "bignum_mod.h" |
| 34 | #include "bignum_mod_raw.h" |
| 35 | #include "constant_time_internal.h" |
| 36 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 37 | int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r, |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 38 | const mbedtls_mpi_mod_modulus *m, |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 39 | mbedtls_mpi_uint *p, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 40 | size_t p_limbs ) |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 41 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 42 | if( p_limbs < m->limbs || !mbedtls_mpi_core_lt_ct( m->p, p, p_limbs ) ) |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 43 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 44 | |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 45 | r->limbs = m->limbs; |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 46 | r->p = p; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 47 | |
| 48 | return( 0 ); |
| 49 | } |
| 50 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 51 | void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r ) |
| 52 | { |
| 53 | if ( r == NULL ) |
| 54 | return; |
| 55 | |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 56 | r->limbs = 0; |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 57 | r->p = NULL; |
| 58 | } |
| 59 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 60 | void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m ) |
| 61 | { |
| 62 | if ( m == NULL ) |
| 63 | return; |
| 64 | |
Janos Follath | 281ccda | 2022-07-19 13:14:36 +0100 | [diff] [blame] | 65 | m->p = NULL; |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 66 | m->limbs = 0; |
| 67 | m->bits = 0; |
Janos Follath | 281ccda | 2022-07-19 13:14:36 +0100 | [diff] [blame] | 68 | m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID; |
| 69 | m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m ) |
| 73 | { |
| 74 | if ( m == NULL ) |
| 75 | return; |
| 76 | |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 77 | switch( m->int_rep ) |
| 78 | { |
| 79 | case MBEDTLS_MPI_MOD_REP_MONTGOMERY: |
Minos Galanakis | 8b33363 | 2022-10-11 11:28:24 +0100 | [diff] [blame] | 80 | mbedtls_platform_zeroize( (mbedtls_mpi_uint *) m->rep.mont.rr, |
| 81 | m->limbs ); |
| 82 | mbedtls_free( (mbedtls_mpi_uint *)m->rep.mont.rr ); |
Minos Galanakis | 760f5d6 | 2022-08-11 12:21:09 +0100 | [diff] [blame] | 83 | m->rep.mont.rr = NULL; |
Minos Galanakis | 771c470 | 2022-10-27 12:22:22 +0100 | [diff] [blame^] | 84 | m->rep.mont.mm = 0; |
| 85 | break; |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 86 | case MBEDTLS_MPI_MOD_REP_OPT_RED: |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 87 | mbedtls_free( m->rep.ored ); |
| 88 | break; |
| 89 | case MBEDTLS_MPI_MOD_REP_INVALID: |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 90 | break; |
| 91 | } |
| 92 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 93 | m->p = NULL; |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 94 | m->limbs = 0; |
| 95 | m->bits = 0; |
Janos Follath | 281ccda | 2022-07-19 13:14:36 +0100 | [diff] [blame] | 96 | m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID; |
| 97 | m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Minos Galanakis | 8b33363 | 2022-10-11 11:28:24 +0100 | [diff] [blame] | 100 | static 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 Galanakis | 771c470 | 2022-10-27 12:22:22 +0100 | [diff] [blame^] | 114 | if ( mbedtls_mpi_grow( &N, limbs )) |
Minos Galanakis | 8b33363 | 2022-10-11 11:28:24 +0100 | [diff] [blame] | 115 | goto cleanup; |
| 116 | |
Minos Galanakis | 771c470 | 2022-10-27 12:22:22 +0100 | [diff] [blame^] | 117 | memcpy( N.p, A, sizeof(mbedtls_mpi_uint) * limbs ); |
| 118 | |
Minos Galanakis | 8b33363 | 2022-10-11 11:28:24 +0100 | [diff] [blame] | 119 | mbedtls_mpi_core_get_mont_r2_unsafe(&RR, &N); |
| 120 | |
| 121 | *X = RR.p; |
| 122 | RR.p = NULL; |
| 123 | ret = 0; |
| 124 | |
| 125 | cleanup: |
| 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 Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 132 | int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m, |
Janos Follath | ed5c8d3 | 2022-08-15 11:50:22 +0100 | [diff] [blame] | 133 | const mbedtls_mpi_uint *p, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 134 | size_t p_limbs, |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 135 | mbedtls_mpi_mod_ext_rep ext_rep, |
| 136 | mbedtls_mpi_mod_rep_selector int_rep ) |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 137 | { |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 138 | int ret = 0; |
| 139 | |
Gabor Mezei | 535f36d | 2022-08-02 11:50:44 +0200 | [diff] [blame] | 140 | m->p = p; |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 141 | m->limbs = p_limbs; |
| 142 | m->bits = mbedtls_mpi_core_bitlen( p, p_limbs ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 143 | |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 144 | switch( ext_rep ) |
| 145 | { |
| 146 | case MBEDTLS_MPI_MOD_EXT_REP_LE: |
| 147 | case MBEDTLS_MPI_MOD_EXT_REP_BE: |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 148 | m->ext_rep = ext_rep; |
| 149 | break; |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 150 | 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 Galanakis | 8b33363 | 2022-10-11 11:28:24 +0100 | [diff] [blame] | 159 | 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 Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 162 | case MBEDTLS_MPI_MOD_REP_OPT_RED: |
| 163 | m->int_rep = int_rep; |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 164 | m->rep.ored = NULL; |
| 165 | break; |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 166 | default: |
| 167 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 168 | goto exit; |
| 169 | } |
| 170 | |
| 171 | exit: |
| 172 | |
| 173 | if( ret != 0 ) |
| 174 | { |
| 175 | mbedtls_mpi_mod_modulus_free( m ); |
| 176 | } |
| 177 | |
| 178 | return( ret ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 181 | #endif /* MBEDTLS_BIGNUM_C */ |