blob: 4382bf85a6485d3aa0512283e01322b66cfddc7e [file] [log] [blame]
Gabor Mezeif049dbf2022-07-18 23:02:33 +02001/**
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 Mezeib9030702022-07-18 23:09:45 +020024#include <string.h>
25
26#include "mbedtls/platform_util.h"
Gabor Mezeif049dbf2022-07-18 23:02:33 +020027#include "mbedtls/error.h"
28#include "mbedtls/bignum.h"
Gabor Mezeif049dbf2022-07-18 23:02:33 +020029
Janos Follathba5c1392022-07-19 13:42:07 +010030#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
33#include <stdio.h>
34#include <stdlib.h>
Gabor Mezeie66b1d42022-08-02 11:49:59 +020035#define mbedtls_printf printf
36#define mbedtls_calloc calloc
37#define mbedtls_free free
Janos Follathba5c1392022-07-19 13:42:07 +010038#endif
39
Janos Follathd1baedb2022-08-09 13:44:53 +010040#include "bignum_core.h"
41#include "bignum_mod.h"
42#include "bignum_mod_raw.h"
43#include "constant_time_internal.h"
44
Gabor Mezeif049dbf2022-07-18 23:02:33 +020045int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
46 mbedtls_mpi_mod_modulus *m,
Janos Follath8b718b52022-07-25 11:31:02 +010047 mbedtls_mpi_uint *p,
48 size_t pn )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020049{
Gabor Mezeifd65e822022-08-12 18:09:12 +020050 if( pn < m->limbs || !mbedtls_mpi_core_lt_ct( m->p, p, pn ) )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020051 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
52
Gabor Mezeifd65e822022-08-12 18:09:12 +020053 r->limbs = m->limbs;
Janos Follath8b718b52022-07-25 11:31:02 +010054 r->p = p;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020055
56 return( 0 );
57}
58
Gabor Mezei37b06362022-08-02 17:22:18 +020059void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r )
60{
61 if ( r == NULL )
62 return;
63
Gabor Mezeifd65e822022-08-12 18:09:12 +020064 r->limbs = 0;
Gabor Mezei37b06362022-08-02 17:22:18 +020065 r->p = NULL;
66}
67
Gabor Mezeif049dbf2022-07-18 23:02:33 +020068void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m )
69{
70 if ( m == NULL )
71 return;
72
Janos Follath281ccda2022-07-19 13:14:36 +010073 m->p = NULL;
Gabor Mezeifd65e822022-08-12 18:09:12 +020074 m->limbs = 0;
75 m->bits = 0;
Janos Follath281ccda2022-07-19 13:14:36 +010076 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
77 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020078}
79
80void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
81{
82 if ( m == NULL )
83 return;
84
Janos Follathba5c1392022-07-19 13:42:07 +010085 switch( m->int_rep )
86 {
87 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
Janos Follath296ea662022-08-11 14:58:29 +010088 mbedtls_free( m->rep.mont );
89 break;
Janos Follathba5c1392022-07-19 13:42:07 +010090 case MBEDTLS_MPI_MOD_REP_OPT_RED:
Janos Follath296ea662022-08-11 14:58:29 +010091 mbedtls_free( m->rep.ored );
92 break;
93 case MBEDTLS_MPI_MOD_REP_INVALID:
Janos Follathba5c1392022-07-19 13:42:07 +010094 break;
95 }
96
Gabor Mezeif049dbf2022-07-18 23:02:33 +020097 m->p = NULL;
Gabor Mezeifd65e822022-08-12 18:09:12 +020098 m->limbs = 0;
99 m->bits = 0;
Janos Follath281ccda2022-07-19 13:14:36 +0100100 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
101 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200102}
103
104int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Janos Follathed5c8d32022-08-15 11:50:22 +0100105 const mbedtls_mpi_uint *p,
Gabor Mezei535f36d2022-08-02 11:50:44 +0200106 size_t pn,
Janos Follath296ea662022-08-11 14:58:29 +0100107 mbedtls_mpi_mod_ext_rep ext_rep,
108 mbedtls_mpi_mod_rep_selector int_rep )
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200109{
Janos Follathba5c1392022-07-19 13:42:07 +0100110 int ret = 0;
111
Gabor Mezei535f36d2022-08-02 11:50:44 +0200112 m->p = p;
Gabor Mezeifd65e822022-08-12 18:09:12 +0200113 m->limbs = pn;
114 m->bits = mbedtls_mpi_core_bitlen( p, pn );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200115
Janos Follathba5c1392022-07-19 13:42:07 +0100116 switch( ext_rep )
117 {
118 case MBEDTLS_MPI_MOD_EXT_REP_LE:
119 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Janos Follath296ea662022-08-11 14:58:29 +0100120 m->ext_rep = ext_rep;
121 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100122 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 Follath296ea662022-08-11 14:58:29 +0100131 m->rep.mont = NULL;
132 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100133 case MBEDTLS_MPI_MOD_REP_OPT_RED:
134 m->int_rep = int_rep;
Janos Follath296ea662022-08-11 14:58:29 +0100135 m->rep.ored = NULL;
136 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100137 default:
138 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
139 goto exit;
140 }
141
142exit:
143
144 if( ret != 0 )
145 {
146 mbedtls_mpi_mod_modulus_free( m );
147 }
148
149 return( ret );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200150}
151
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200152#endif /* MBEDTLS_BIGNUM_C */