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 | |
| 29 | #if defined(MBEDTLS_PLATFORM_C) |
| 30 | #include "mbedtls/platform.h" |
| 31 | #else |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #define mbedtls_printf printf |
| 35 | #define mbedtls_calloc calloc |
| 36 | #define mbedtls_free free |
| 37 | #endif |
| 38 | |
| 39 | #include "bignum_core.h" |
| 40 | #include "bignum_mod_raw.h" |
| 41 | #include "bignum_mod.h" |
| 42 | #include "constant_time_internal.h" |
| 43 | |
Gabor Mezei | 63c3282 | 2022-09-15 20:01:31 +0200 | [diff] [blame^] | 44 | void mbedtls_mpi_mod_raw_cond_assign( mbedtls_mpi_uint *X, |
| 45 | mbedtls_mpi_uint *Y, |
| 46 | const mbedtls_mpi_mod_modulus *m, |
| 47 | unsigned char assign ) |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 48 | { |
Gabor Mezei | 63c3282 | 2022-09-15 20:01:31 +0200 | [diff] [blame^] | 49 | mbedtls_mpi_core_cond_assign( X, m->limbs, |
| 50 | Y, m->limbs, assign ); |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Gabor Mezei | 63c3282 | 2022-09-15 20:01:31 +0200 | [diff] [blame^] | 53 | void mbedtls_mpi_mod_raw_cond_swap( mbedtls_mpi_uint *X, |
| 54 | mbedtls_mpi_uint *Y, |
| 55 | const mbedtls_mpi_mod_modulus *m, |
| 56 | unsigned char swap ) |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 57 | { |
Gabor Mezei | 63c3282 | 2022-09-15 20:01:31 +0200 | [diff] [blame^] | 58 | mbedtls_mpi_core_cond_swap( X, m->limbs, |
| 59 | Y, m->limbs, swap ); |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 62 | int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X, |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 63 | const mbedtls_mpi_mod_modulus *m, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 64 | const unsigned char *input, |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 65 | size_t input_length ) |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 66 | { |
| 67 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 68 | |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 69 | switch( m->ext_rep ) |
| 70 | { |
| 71 | case MBEDTLS_MPI_MOD_EXT_REP_LE: |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 72 | ret = mbedtls_mpi_core_read_le( X, m->limbs, |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 73 | input, input_length ); |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 74 | break; |
| 75 | case MBEDTLS_MPI_MOD_EXT_REP_BE: |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 76 | ret = mbedtls_mpi_core_read_be( X, m->limbs, |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 77 | input, input_length ); |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 78 | break; |
| 79 | default: |
| 80 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 81 | } |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 82 | |
| 83 | if( ret != 0 ) |
| 84 | goto cleanup; |
| 85 | |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 86 | if( !mbedtls_mpi_core_lt_ct( X, m->p, m->limbs ) ) |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 87 | { |
| 88 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 89 | goto cleanup; |
| 90 | } |
| 91 | |
| 92 | cleanup: |
| 93 | |
| 94 | return( ret ); |
| 95 | } |
| 96 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 97 | int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A, |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 98 | const mbedtls_mpi_mod_modulus *m, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 99 | unsigned char *output, |
| 100 | size_t output_length ) |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 101 | { |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 102 | switch( m->ext_rep ) |
| 103 | { |
| 104 | case MBEDTLS_MPI_MOD_EXT_REP_LE: |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 105 | return( mbedtls_mpi_core_write_le( A, m->limbs, |
| 106 | output, output_length ) ); |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 107 | case MBEDTLS_MPI_MOD_EXT_REP_BE: |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 108 | return( mbedtls_mpi_core_write_be( A, m->limbs, |
| 109 | output, output_length ) ); |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 110 | default: |
| 111 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 112 | } |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | #endif /* MBEDTLS_BIGNUM_C */ |