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 | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 55 | size_t input_length ) |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 56 | { |
| 57 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 58 | |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 59 | switch( m->ext_rep ) |
| 60 | { |
| 61 | case MBEDTLS_MPI_MOD_EXT_REP_LE: |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 62 | ret = mbedtls_mpi_core_read_le( X, m->limbs, |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 63 | input, input_length ); |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 64 | break; |
| 65 | case MBEDTLS_MPI_MOD_EXT_REP_BE: |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 66 | ret = mbedtls_mpi_core_read_be( X, m->limbs, |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 67 | input, input_length ); |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 68 | break; |
| 69 | default: |
| 70 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 71 | } |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 72 | |
| 73 | if( ret != 0 ) |
| 74 | goto cleanup; |
| 75 | |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 76 | if( !mbedtls_mpi_core_lt_ct( X, m->p, m->limbs ) ) |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 77 | { |
| 78 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 79 | goto cleanup; |
| 80 | } |
| 81 | |
| 82 | cleanup: |
| 83 | |
| 84 | return( ret ); |
| 85 | } |
| 86 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 87 | int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A, |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 88 | const mbedtls_mpi_mod_modulus *m, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 89 | unsigned char *output, |
| 90 | size_t output_length ) |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 91 | { |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 92 | switch( m->ext_rep ) |
| 93 | { |
| 94 | case MBEDTLS_MPI_MOD_EXT_REP_LE: |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 95 | return( mbedtls_mpi_core_write_le( A, m->limbs, |
| 96 | output, output_length ) ); |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 97 | case MBEDTLS_MPI_MOD_EXT_REP_BE: |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 98 | return( mbedtls_mpi_core_write_be( A, m->limbs, |
| 99 | output, output_length ) ); |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 100 | default: |
| 101 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 102 | } |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 105 | /* 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 */ |
| 122 | |
| 123 | /* END MERGE SLOT 5 */ |
| 124 | |
| 125 | /* BEGIN MERGE SLOT 6 */ |
| 126 | |
| 127 | /* END MERGE SLOT 6 */ |
| 128 | |
| 129 | /* BEGIN MERGE SLOT 7 */ |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 130 | int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X, |
| 131 | const mbedtls_mpi_mod_modulus *m ) |
Hanno Becker | 5ad4a93 | 2022-08-09 14:45:53 +0100 | [diff] [blame] | 132 | { |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 133 | mbedtls_mpi_uint *T; |
| 134 | const size_t t_limbs = m->limbs * 2 + 1; |
| 135 | |
| 136 | if( ( T = (mbedtls_mpi_uint *) mbedtls_calloc( t_limbs, ciL ) ) == NULL ) |
| 137 | return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); |
| 138 | |
| 139 | mbedtls_mpi_core_montmul( X, X, m->rep.mont.rr, m->limbs, m->p, m->limbs, |
| 140 | m->rep.mont.mm, T ); |
| 141 | |
| 142 | mbedtls_platform_zeroize( T, t_limbs * ciL ); |
| 143 | mbedtls_free( T ); |
Hanno Becker | 5ad4a93 | 2022-08-09 14:45:53 +0100 | [diff] [blame] | 144 | return( 0 ); |
| 145 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 146 | |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 147 | int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X, |
| 148 | const mbedtls_mpi_mod_modulus *m ) |
Hanno Becker | 5ad4a93 | 2022-08-09 14:45:53 +0100 | [diff] [blame] | 149 | { |
Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 150 | const mbedtls_mpi_uint one = 1; |
| 151 | const size_t t_limbs = m->limbs * 2 + 1; |
| 152 | mbedtls_mpi_uint *T; |
| 153 | |
| 154 | if( ( T = (mbedtls_mpi_uint *) mbedtls_calloc( t_limbs, ciL ) ) == NULL ) |
| 155 | return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); |
| 156 | |
| 157 | mbedtls_mpi_core_montmul( X, X, &one, 1, m->p, m->limbs, |
| 158 | m->rep.mont.mm, T ); |
| 159 | |
| 160 | mbedtls_platform_zeroize( T, t_limbs * ciL ); |
| 161 | mbedtls_free( T ); |
Hanno Becker | 5ad4a93 | 2022-08-09 14:45:53 +0100 | [diff] [blame] | 162 | return( 0 ); |
| 163 | } |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 164 | /* END MERGE SLOT 7 */ |
| 165 | |
| 166 | /* BEGIN MERGE SLOT 8 */ |
| 167 | |
| 168 | /* END MERGE SLOT 8 */ |
| 169 | |
| 170 | /* BEGIN MERGE SLOT 9 */ |
| 171 | |
| 172 | /* END MERGE SLOT 9 */ |
| 173 | |
| 174 | /* BEGIN MERGE SLOT 10 */ |
| 175 | |
| 176 | /* END MERGE SLOT 10 */ |
| 177 | |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 178 | #endif /* MBEDTLS_BIGNUM_C */ |