blob: 98994ab36192c77ec07916b93fe62549b9c8210f [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
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 Mezei63c32822022-09-15 20:01:31 +020044void mbedtls_mpi_mod_raw_cond_assign( mbedtls_mpi_uint *X,
Gabor Mezei81e57022022-09-26 17:13:33 +020045 const mbedtls_mpi_uint *Y,
Gabor Mezei63c32822022-09-15 20:01:31 +020046 const mbedtls_mpi_mod_modulus *m,
47 unsigned char assign )
Gabor Mezei12071d42022-09-12 16:35:58 +020048{
Gabor Mezei63c32822022-09-15 20:01:31 +020049 mbedtls_mpi_core_cond_assign( X, m->limbs,
50 Y, m->limbs, assign );
Gabor Mezei12071d42022-09-12 16:35:58 +020051}
52
Gabor Mezei63c32822022-09-15 20:01:31 +020053void 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 Mezei12071d42022-09-12 16:35:58 +020057{
Gabor Mezeicfc0eb82022-09-15 20:15:34 +020058 mbedtls_mpi_core_cond_swap( X, Y, m->limbs, swap );
Gabor Mezei12071d42022-09-12 16:35:58 +020059}
60
Janos Follath0ded6312022-08-09 13:34:54 +010061int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
Janos Follath6b8a4ad2022-08-19 10:58:34 +010062 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +010063 const unsigned char *input,
Janos Follathaf3f39c2022-08-22 09:06:32 +010064 size_t input_length )
Janos Follath0ded6312022-08-09 13:34:54 +010065{
66 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
67
Janos Follath296ea662022-08-11 14:58:29 +010068 switch( m->ext_rep )
69 {
70 case MBEDTLS_MPI_MOD_EXT_REP_LE:
Janos Follathb7a88ec2022-08-19 12:24:40 +010071 ret = mbedtls_mpi_core_read_le( X, m->limbs,
Janos Follathaf3f39c2022-08-22 09:06:32 +010072 input, input_length );
Janos Follath296ea662022-08-11 14:58:29 +010073 break;
74 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Janos Follathb7a88ec2022-08-19 12:24:40 +010075 ret = mbedtls_mpi_core_read_be( X, m->limbs,
Janos Follathaf3f39c2022-08-22 09:06:32 +010076 input, input_length );
Janos Follath296ea662022-08-11 14:58:29 +010077 break;
78 default:
79 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
80 }
Janos Follath0ded6312022-08-09 13:34:54 +010081
82 if( ret != 0 )
83 goto cleanup;
84
Gabor Mezeifd65e822022-08-12 18:09:12 +020085 if( !mbedtls_mpi_core_lt_ct( X, m->p, m->limbs ) )
Janos Follath0ded6312022-08-09 13:34:54 +010086 {
87 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
88 goto cleanup;
89 }
90
91cleanup:
92
93 return( ret );
94}
95
Janos Follathb7a88ec2022-08-19 12:24:40 +010096int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A,
Janos Follath6b8a4ad2022-08-19 10:58:34 +010097 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +010098 unsigned char *output,
99 size_t output_length )
Janos Follath0ded6312022-08-09 13:34:54 +0100100{
Janos Follath296ea662022-08-11 14:58:29 +0100101 switch( m->ext_rep )
102 {
103 case MBEDTLS_MPI_MOD_EXT_REP_LE:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100104 return( mbedtls_mpi_core_write_le( A, m->limbs,
105 output, output_length ) );
Janos Follath296ea662022-08-11 14:58:29 +0100106 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Janos Follathb7a88ec2022-08-19 12:24:40 +0100107 return( mbedtls_mpi_core_write_be( A, m->limbs,
108 output, output_length ) );
Janos Follath296ea662022-08-11 14:58:29 +0100109 default:
110 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
111 }
Janos Follath0ded6312022-08-09 13:34:54 +0100112}
113
114#endif /* MBEDTLS_BIGNUM_C */