blob: e1c96d612b83f82466d1038d693e206e6b846723 [file] [log] [blame]
Janos Follath0ded6312022-08-09 13:34:54 +01001/*
Janos Follatha95f2042022-08-19 12:09:17 +01002 * Low-level modular bignum functions
Janos Follath0ded6312022-08-09 13:34:54 +01003 *
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 Follath0ded6312022-08-09 13:34:54 +010029#include "mbedtls/platform.h"
Janos Follath0ded6312022-08-09 13:34:54 +010030
31#include "bignum_core.h"
32#include "bignum_mod_raw.h"
33#include "bignum_mod.h"
34#include "constant_time_internal.h"
35
36int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
Janos Follath6b8a4ad2022-08-19 10:58:34 +010037 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +010038 const unsigned char *input,
Janos Follathaf3f39c2022-08-22 09:06:32 +010039 size_t input_length )
Janos Follath0ded6312022-08-09 13:34:54 +010040{
41 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
42
Janos Follath296ea662022-08-11 14:58:29 +010043 switch( m->ext_rep )
44 {
45 case MBEDTLS_MPI_MOD_EXT_REP_LE:
Janos Follathb7a88ec2022-08-19 12:24:40 +010046 ret = mbedtls_mpi_core_read_le( X, m->limbs,
Janos Follathaf3f39c2022-08-22 09:06:32 +010047 input, input_length );
Janos Follath296ea662022-08-11 14:58:29 +010048 break;
49 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Janos Follathb7a88ec2022-08-19 12:24:40 +010050 ret = mbedtls_mpi_core_read_be( X, m->limbs,
Janos Follathaf3f39c2022-08-22 09:06:32 +010051 input, input_length );
Janos Follath296ea662022-08-11 14:58:29 +010052 break;
53 default:
54 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
55 }
Janos Follath0ded6312022-08-09 13:34:54 +010056
57 if( ret != 0 )
58 goto cleanup;
59
Gabor Mezeifd65e822022-08-12 18:09:12 +020060 if( !mbedtls_mpi_core_lt_ct( X, m->p, m->limbs ) )
Janos Follath0ded6312022-08-09 13:34:54 +010061 {
62 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
63 goto cleanup;
64 }
65
66cleanup:
67
68 return( ret );
69}
70
Janos Follathb7a88ec2022-08-19 12:24:40 +010071int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A,
Janos Follath6b8a4ad2022-08-19 10:58:34 +010072 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +010073 unsigned char *output,
74 size_t output_length )
Janos Follath0ded6312022-08-09 13:34:54 +010075{
Janos Follath296ea662022-08-11 14:58:29 +010076 switch( m->ext_rep )
77 {
78 case MBEDTLS_MPI_MOD_EXT_REP_LE:
Janos Follathb7a88ec2022-08-19 12:24:40 +010079 return( mbedtls_mpi_core_write_le( A, m->limbs,
80 output, output_length ) );
Janos Follath296ea662022-08-11 14:58:29 +010081 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Janos Follathb7a88ec2022-08-19 12:24:40 +010082 return( mbedtls_mpi_core_write_be( A, m->limbs,
83 output, output_length ) );
Janos Follath296ea662022-08-11 14:58:29 +010084 default:
85 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
86 }
Janos Follath0ded6312022-08-09 13:34:54 +010087}
88
89#endif /* MBEDTLS_BIGNUM_C */