Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Internal bignum functions |
| 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 | #if defined(MBEDTLS_PLATFORM_C) |
| 31 | #include "mbedtls/platform.h" |
| 32 | #else |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
Gabor Mezei | e66b1d4 | 2022-08-02 11:49:59 +0200 | [diff] [blame] | 35 | #define mbedtls_printf printf |
| 36 | #define mbedtls_calloc calloc |
| 37 | #define mbedtls_free free |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 38 | #endif |
| 39 | |
Janos Follath | d1baedb | 2022-08-09 13:44:53 +0100 | [diff] [blame] | 40 | #include "bignum_core.h" |
| 41 | #include "bignum_mod.h" |
| 42 | #include "bignum_mod_raw.h" |
| 43 | #include "constant_time_internal.h" |
| 44 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 45 | int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r, |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 46 | const mbedtls_mpi_mod_modulus *m, |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 47 | mbedtls_mpi_uint *p, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame^] | 48 | size_t p_limbs ) |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 49 | { |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame^] | 50 | 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] | 51 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 52 | |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 53 | r->limbs = m->limbs; |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 54 | r->p = p; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 55 | |
| 56 | return( 0 ); |
| 57 | } |
| 58 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 59 | void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r ) |
| 60 | { |
| 61 | if ( r == NULL ) |
| 62 | return; |
| 63 | |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 64 | r->limbs = 0; |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 65 | r->p = NULL; |
| 66 | } |
| 67 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 68 | void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m ) |
| 69 | { |
| 70 | if ( m == NULL ) |
| 71 | return; |
| 72 | |
Janos Follath | 281ccda | 2022-07-19 13:14:36 +0100 | [diff] [blame] | 73 | m->p = NULL; |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 74 | m->limbs = 0; |
| 75 | m->bits = 0; |
Janos Follath | 281ccda | 2022-07-19 13:14:36 +0100 | [diff] [blame] | 76 | m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID; |
| 77 | m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m ) |
| 81 | { |
| 82 | if ( m == NULL ) |
| 83 | return; |
| 84 | |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 85 | switch( m->int_rep ) |
| 86 | { |
| 87 | case MBEDTLS_MPI_MOD_REP_MONTGOMERY: |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 88 | mbedtls_free( m->rep.mont ); |
| 89 | break; |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 90 | case MBEDTLS_MPI_MOD_REP_OPT_RED: |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 91 | mbedtls_free( m->rep.ored ); |
| 92 | break; |
| 93 | case MBEDTLS_MPI_MOD_REP_INVALID: |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 94 | break; |
| 95 | } |
| 96 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 97 | m->p = NULL; |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 98 | m->limbs = 0; |
| 99 | m->bits = 0; |
Janos Follath | 281ccda | 2022-07-19 13:14:36 +0100 | [diff] [blame] | 100 | m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID; |
| 101 | m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m, |
Janos Follath | ed5c8d3 | 2022-08-15 11:50:22 +0100 | [diff] [blame] | 105 | const mbedtls_mpi_uint *p, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame^] | 106 | size_t p_limbs, |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 107 | mbedtls_mpi_mod_ext_rep ext_rep, |
| 108 | mbedtls_mpi_mod_rep_selector int_rep ) |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 109 | { |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 110 | int ret = 0; |
| 111 | |
Gabor Mezei | 535f36d | 2022-08-02 11:50:44 +0200 | [diff] [blame] | 112 | m->p = p; |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame^] | 113 | m->limbs = p_limbs; |
| 114 | m->bits = mbedtls_mpi_core_bitlen( p, p_limbs ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 115 | |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 116 | switch( ext_rep ) |
| 117 | { |
| 118 | case MBEDTLS_MPI_MOD_EXT_REP_LE: |
| 119 | case MBEDTLS_MPI_MOD_EXT_REP_BE: |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 120 | m->ext_rep = ext_rep; |
| 121 | break; |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 122 | default: |
| 123 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 124 | goto exit; |
| 125 | } |
| 126 | |
| 127 | switch( int_rep ) |
| 128 | { |
| 129 | case MBEDTLS_MPI_MOD_REP_MONTGOMERY: |
| 130 | m->int_rep = int_rep; |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 131 | m->rep.mont = NULL; |
| 132 | break; |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 133 | case MBEDTLS_MPI_MOD_REP_OPT_RED: |
| 134 | m->int_rep = int_rep; |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 135 | m->rep.ored = NULL; |
| 136 | break; |
Janos Follath | ba5c139 | 2022-07-19 13:42:07 +0100 | [diff] [blame] | 137 | default: |
| 138 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 139 | goto exit; |
| 140 | } |
| 141 | |
| 142 | exit: |
| 143 | |
| 144 | if( ret != 0 ) |
| 145 | { |
| 146 | mbedtls_mpi_mod_modulus_free( m ); |
| 147 | } |
| 148 | |
| 149 | return( ret ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 152 | #endif /* MBEDTLS_BIGNUM_C */ |