Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 1 | /* |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 2 | * Low-level modular bignum functions |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [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 | |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "mbedtls/error.h" |
| 27 | #include "mbedtls/platform_util.h" |
| 28 | |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 29 | #include "mbedtls/platform.h" |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 30 | |
| 31 | #include "bignum_core.h" |
| 32 | #include "bignum_mod_raw.h" |
| 33 | #include "bignum_mod.h" |
| 34 | #include "constant_time_internal.h" |
| 35 | |
Gabor Mezei | 63c3282 | 2022-09-15 20:01:31 +0200 | [diff] [blame] | 36 | void mbedtls_mpi_mod_raw_cond_assign( mbedtls_mpi_uint *X, |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame] | 37 | const mbedtls_mpi_uint *A, |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 38 | const mbedtls_mpi_mod_modulus *N, |
Gabor Mezei | 63c3282 | 2022-09-15 20:01:31 +0200 | [diff] [blame] | 39 | unsigned char assign ) |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 40 | { |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 41 | mbedtls_mpi_core_cond_assign( X, A, N->limbs, assign ); |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 44 | void mbedtls_mpi_mod_raw_cond_swap( mbedtls_mpi_uint *X, |
| 45 | mbedtls_mpi_uint *Y, |
| 46 | const mbedtls_mpi_mod_modulus *N, |
Gabor Mezei | 63c3282 | 2022-09-15 20:01:31 +0200 | [diff] [blame] | 47 | unsigned char swap ) |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 48 | { |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 49 | mbedtls_mpi_core_cond_swap( X, Y, N->limbs, swap ); |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 50 | } |
| 51 | |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 52 | int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X, |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 53 | const mbedtls_mpi_mod_modulus *m, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 54 | const unsigned char *input, |
Janos Follath | d3eed33 | 2022-11-24 17:42:02 +0000 | [diff] [blame] | 55 | size_t input_length, |
| 56 | mbedtls_mpi_mod_ext_rep ext_rep ) |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 57 | { |
| 58 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 59 | |
Janos Follath | d3eed33 | 2022-11-24 17:42:02 +0000 | [diff] [blame] | 60 | switch( ext_rep ) |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 61 | { |
| 62 | case MBEDTLS_MPI_MOD_EXT_REP_LE: |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 63 | ret = mbedtls_mpi_core_read_le( X, m->limbs, |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 64 | input, input_length ); |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 65 | break; |
| 66 | case MBEDTLS_MPI_MOD_EXT_REP_BE: |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 67 | ret = mbedtls_mpi_core_read_be( X, m->limbs, |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 68 | input, input_length ); |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 69 | break; |
| 70 | default: |
| 71 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 72 | } |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 73 | |
| 74 | if( ret != 0 ) |
| 75 | goto cleanup; |
| 76 | |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 77 | if( !mbedtls_mpi_core_lt_ct( X, m->p, m->limbs ) ) |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 78 | { |
| 79 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 80 | goto cleanup; |
| 81 | } |
| 82 | |
| 83 | cleanup: |
| 84 | |
| 85 | return( ret ); |
| 86 | } |
| 87 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 88 | int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A, |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 89 | const mbedtls_mpi_mod_modulus *m, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 90 | unsigned char *output, |
Janos Follath | d3eed33 | 2022-11-24 17:42:02 +0000 | [diff] [blame] | 91 | size_t output_length, |
| 92 | mbedtls_mpi_mod_ext_rep ext_rep ) |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 93 | { |
Janos Follath | d3eed33 | 2022-11-24 17:42:02 +0000 | [diff] [blame] | 94 | switch( ext_rep ) |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 95 | { |
| 96 | case MBEDTLS_MPI_MOD_EXT_REP_LE: |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 97 | return( mbedtls_mpi_core_write_le( A, m->limbs, |
| 98 | output, output_length ) ); |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 99 | case MBEDTLS_MPI_MOD_EXT_REP_BE: |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 100 | return( mbedtls_mpi_core_write_be( A, m->limbs, |
| 101 | output, output_length ) ); |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 102 | default: |
| 103 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 104 | } |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 107 | /* BEGIN MERGE SLOT 1 */ |
| 108 | |
| 109 | /* END MERGE SLOT 1 */ |
| 110 | |
| 111 | /* BEGIN MERGE SLOT 2 */ |
| 112 | |
Gabor Mezei | 4c7cf7d | 2022-11-09 14:07:43 +0100 | [diff] [blame] | 113 | void 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 Mezei | 3411e94 | 2022-11-16 11:31:00 +0100 | [diff] [blame] | 120 | (void) mbedtls_mpi_core_add_if( X, N->p, N->limbs, (unsigned) c ); |
Gabor Mezei | 4c7cf7d | 2022-11-09 14:07:43 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Gabor Mezei | 979d34c | 2022-12-07 16:02:33 +0100 | [diff] [blame] | 123 | void mbedtls_mpi_mod_raw_mul( mbedtls_mpi_uint *X, |
| 124 | const mbedtls_mpi_uint *A, |
| 125 | const mbedtls_mpi_uint *B, |
| 126 | const mbedtls_mpi_mod_modulus *N, |
| 127 | mbedtls_mpi_uint *T ) |
| 128 | { |
| 129 | mbedtls_mpi_core_montmul( X, A, B, N->limbs, N->p, N->limbs, |
| 130 | N->rep.mont.mm, T ); |
| 131 | } |
| 132 | |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 133 | /* END MERGE SLOT 2 */ |
| 134 | |
| 135 | /* BEGIN MERGE SLOT 3 */ |
| 136 | |
Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 137 | size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs( size_t AN_limbs ) |
| 138 | { |
| 139 | /* mbedtls_mpi_mod_raw_inv_prime() needs a temporary for the exponent, |
| 140 | * which will be the same size as the modulus and input (AN_limbs), |
| 141 | * and additional space to pass to mbedtls_mpi_core_exp_mod(). */ |
| 142 | return( AN_limbs + |
| 143 | mbedtls_mpi_core_exp_mod_working_limbs( AN_limbs, AN_limbs ) ); |
| 144 | } |
| 145 | |
| 146 | void mbedtls_mpi_mod_raw_inv_prime( mbedtls_mpi_uint *X, |
| 147 | const mbedtls_mpi_uint *A, |
| 148 | const mbedtls_mpi_uint *N, |
| 149 | size_t AN_limbs, |
| 150 | const mbedtls_mpi_uint *RR, |
| 151 | mbedtls_mpi_uint *T ) |
| 152 | { |
| 153 | /* Inversion by power: g^|G| = 1 => g^(-1) = g^(|G|-1), and |
| 154 | * |G| = N - 1, so we want |
| 155 | * g^(|G|-1) = g^(N - 2) |
| 156 | */ |
Tom Cosgrove | 5f09930 | 2022-12-09 10:58:15 +0000 | [diff] [blame] | 157 | |
| 158 | /* Use the first AN_limbs of T to hold N - 2 */ |
Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 159 | mbedtls_mpi_uint *Nminus2 = T; |
| 160 | (void) mbedtls_mpi_core_sub_int( Nminus2, N, 2, AN_limbs ); |
| 161 | |
Tom Cosgrove | 5f09930 | 2022-12-09 10:58:15 +0000 | [diff] [blame] | 162 | /* Rest of T is given to exp_mod for its working space */ |
Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 163 | mbedtls_mpi_core_exp_mod( X, |
| 164 | A, N, AN_limbs, Nminus2, AN_limbs, |
| 165 | RR, T + AN_limbs ); |
| 166 | } |
| 167 | |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 168 | /* END MERGE SLOT 3 */ |
| 169 | |
| 170 | /* BEGIN MERGE SLOT 4 */ |
| 171 | |
| 172 | /* END MERGE SLOT 4 */ |
| 173 | |
| 174 | /* BEGIN MERGE SLOT 5 */ |
Werner Lewis | 0eea827 | 2022-11-01 13:27:29 +0000 | [diff] [blame] | 175 | void mbedtls_mpi_mod_raw_add( mbedtls_mpi_uint *X, |
Werner Lewis | d391b8c | 2022-11-08 15:53:47 +0000 | [diff] [blame] | 176 | const mbedtls_mpi_uint *A, |
| 177 | const mbedtls_mpi_uint *B, |
Werner Lewis | 9fa91eb | 2022-11-01 13:36:51 +0000 | [diff] [blame] | 178 | const mbedtls_mpi_mod_modulus *N ) |
Hanno Becker | a45b6fe | 2022-11-01 13:14:28 +0000 | [diff] [blame] | 179 | { |
Werner Lewis | d391b8c | 2022-11-08 15:53:47 +0000 | [diff] [blame] | 180 | mbedtls_mpi_uint carry, borrow; |
Werner Lewis | 9fa91eb | 2022-11-01 13:36:51 +0000 | [diff] [blame] | 181 | carry = mbedtls_mpi_core_add( X, A, B, N->limbs ); |
| 182 | borrow = mbedtls_mpi_core_sub( X, X, N->p, N->limbs ); |
Werner Lewis | e4c0a6c | 2022-11-17 11:19:58 +0000 | [diff] [blame] | 183 | (void) mbedtls_mpi_core_add_if( X, N->p, N->limbs, (unsigned) ( carry ^ borrow ) ); |
Hanno Becker | a45b6fe | 2022-11-01 13:14:28 +0000 | [diff] [blame] | 184 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 185 | /* END MERGE SLOT 5 */ |
| 186 | |
| 187 | /* BEGIN MERGE SLOT 6 */ |
| 188 | |
Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 189 | int mbedtls_mpi_mod_raw_canonical_to_modulus_rep( |
| 190 | mbedtls_mpi_uint *X, |
| 191 | const mbedtls_mpi_mod_modulus *N ) |
| 192 | { |
| 193 | switch( N->int_rep ) |
| 194 | { |
| 195 | case MBEDTLS_MPI_MOD_REP_MONTGOMERY: |
| 196 | return( mbedtls_mpi_mod_raw_to_mont_rep( X, N ) ); |
| 197 | case MBEDTLS_MPI_MOD_REP_OPT_RED: |
| 198 | return( 0 ); |
| 199 | default: |
| 200 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | int mbedtls_mpi_mod_raw_modulus_to_canonical_rep( |
| 205 | mbedtls_mpi_uint *X, |
| 206 | const mbedtls_mpi_mod_modulus *N ) |
| 207 | { |
| 208 | switch( N->int_rep ) |
| 209 | { |
| 210 | case MBEDTLS_MPI_MOD_REP_MONTGOMERY: |
| 211 | return( mbedtls_mpi_mod_raw_from_mont_rep( X, N ) ); |
| 212 | case MBEDTLS_MPI_MOD_REP_OPT_RED: |
| 213 | return( 0 ); |
| 214 | default: |
| 215 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 216 | } |
| 217 | } |
| 218 | |
Gilles Peskine | a57cf98 | 2022-12-06 22:54:09 +0100 | [diff] [blame] | 219 | int mbedtls_mpi_mod_raw_random( mbedtls_mpi_uint *X, |
| 220 | mbedtls_mpi_uint min, |
| 221 | const mbedtls_mpi_mod_modulus *N, |
| 222 | int (*f_rng)(void *, unsigned char *, size_t), |
| 223 | void *p_rng ) |
| 224 | { |
| 225 | int ret = mbedtls_mpi_core_random( X, min, N->p, N->limbs, f_rng, p_rng ); |
| 226 | if( ret != 0 ) |
| 227 | return( ret ); |
Gilles Peskine | e1d8326 | 2022-12-20 19:31:09 +0100 | [diff] [blame] | 228 | return( mbedtls_mpi_mod_raw_canonical_to_modulus_rep( X, N ) ); |
Gilles Peskine | a57cf98 | 2022-12-06 22:54:09 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 231 | /* END MERGE SLOT 6 */ |
| 232 | |
| 233 | /* BEGIN MERGE SLOT 7 */ |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 234 | int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X, |
| 235 | const mbedtls_mpi_mod_modulus *m ) |
Hanno Becker | 5ad4a93 | 2022-08-09 14:45:53 +0100 | [diff] [blame] | 236 | { |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 237 | mbedtls_mpi_uint *T; |
Tom Cosgrove | 28ff92c | 2022-12-12 17:06:27 +0000 | [diff] [blame] | 238 | const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs( m->limbs ); |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 239 | |
| 240 | if( ( T = (mbedtls_mpi_uint *) mbedtls_calloc( t_limbs, ciL ) ) == NULL ) |
| 241 | return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); |
| 242 | |
Tom Cosgrove | 786848b | 2022-12-13 10:45:19 +0000 | [diff] [blame] | 243 | mbedtls_mpi_core_to_mont_rep( X, X, m->p, m->limbs, |
| 244 | m->rep.mont.mm, m->rep.mont.rr, T ); |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 245 | |
| 246 | mbedtls_platform_zeroize( T, t_limbs * ciL ); |
| 247 | mbedtls_free( T ); |
Hanno Becker | 5ad4a93 | 2022-08-09 14:45:53 +0100 | [diff] [blame] | 248 | return( 0 ); |
| 249 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 250 | |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 251 | int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X, |
| 252 | const mbedtls_mpi_mod_modulus *m ) |
Hanno Becker | 5ad4a93 | 2022-08-09 14:45:53 +0100 | [diff] [blame] | 253 | { |
Tom Cosgrove | 28ff92c | 2022-12-12 17:06:27 +0000 | [diff] [blame] | 254 | const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs( m->limbs ); |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 255 | mbedtls_mpi_uint *T; |
| 256 | |
| 257 | if( ( T = (mbedtls_mpi_uint *) mbedtls_calloc( t_limbs, ciL ) ) == NULL ) |
| 258 | return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); |
| 259 | |
Tom Cosgrove | 786848b | 2022-12-13 10:45:19 +0000 | [diff] [blame] | 260 | mbedtls_mpi_core_from_mont_rep( X, X, m->p, m->limbs, m->rep.mont.mm, T ); |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 261 | |
| 262 | mbedtls_platform_zeroize( T, t_limbs * ciL ); |
| 263 | mbedtls_free( T ); |
Hanno Becker | 5ad4a93 | 2022-08-09 14:45:53 +0100 | [diff] [blame] | 264 | return( 0 ); |
| 265 | } |
Minos Galanakis | 21fe8bd | 2022-12-07 18:06:05 +0000 | [diff] [blame] | 266 | |
| 267 | void mbedtls_mpi_mod_raw_neg( mbedtls_mpi_uint *X, |
| 268 | const mbedtls_mpi_uint *A, |
| 269 | const mbedtls_mpi_mod_modulus *m ) |
| 270 | { |
| 271 | mbedtls_mpi_core_sub( X, m->p, A, m->limbs ); |
| 272 | |
| 273 | /* If A=0 initially, then X=N now. Detect this by |
| 274 | * subtracting N and catching the carry. */ |
| 275 | mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, X, m->p, m->limbs ); |
| 276 | (void) mbedtls_mpi_core_add_if( X, m->p, m->limbs, (unsigned) borrow ); |
| 277 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 278 | /* END MERGE SLOT 7 */ |
| 279 | |
| 280 | /* BEGIN MERGE SLOT 8 */ |
| 281 | |
| 282 | /* END MERGE SLOT 8 */ |
| 283 | |
| 284 | /* BEGIN MERGE SLOT 9 */ |
| 285 | |
| 286 | /* END MERGE SLOT 9 */ |
| 287 | |
| 288 | /* BEGIN MERGE SLOT 10 */ |
| 289 | |
| 290 | /* END MERGE SLOT 10 */ |
| 291 | |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 292 | #endif /* MBEDTLS_BIGNUM_C */ |