blob: 63f917266aa138b023ee51d072eae1ac386ad0aa [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{
Janos Follath8b718b52022-07-25 11:31:02 +010050 if( p == NULL || m == NULL || r == NULL )
51 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
52
53 if( pn < m->n || !mbedtls_mpi_core_lt_ct( m->p, p, pn ) )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020054 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
55
56 r->n = m->n;
Janos Follath8b718b52022-07-25 11:31:02 +010057 r->p = p;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020058
59 return( 0 );
60}
61
Gabor Mezei37b06362022-08-02 17:22:18 +020062void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r )
63{
64 if ( r == NULL )
65 return;
66
67 r->n = 0;
68 r->p = NULL;
69}
70
Gabor Mezeif049dbf2022-07-18 23:02:33 +020071void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m )
72{
73 if ( m == NULL )
74 return;
75
Janos Follath281ccda2022-07-19 13:14:36 +010076 m->p = NULL;
77 m->n = 0;
78 m->plen = 0;
79 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
80 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020081}
82
83void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
84{
85 if ( m == NULL )
86 return;
87
Janos Follathba5c1392022-07-19 13:42:07 +010088 switch( m->int_rep )
89 {
90 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
91 mbedtls_free( m->rep.mont ); break;
92 case MBEDTLS_MPI_MOD_REP_OPT_RED:
Gabor Mezeid8f5bc22022-08-02 11:51:25 +020093 mbedtls_free( m->rep.ored ); break;
Janos Follathba5c1392022-07-19 13:42:07 +010094 default:
95 break;
96 }
97
Gabor Mezeif049dbf2022-07-18 23:02:33 +020098 m->p = NULL;
99 m->n = 0;
100 m->plen = 0;
Janos Follath281ccda2022-07-19 13:14:36 +0100101 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
102 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200103}
104
105int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Gabor Mezei535f36d2022-08-02 11:50:44 +0200106 mbedtls_mpi_uint *p,
107 size_t pn,
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200108 int ext_rep,
109 int int_rep )
110{
Janos Follathba5c1392022-07-19 13:42:07 +0100111 int ret = 0;
112
Gabor Mezei535f36d2022-08-02 11:50:44 +0200113 if ( p == NULL || m == NULL )
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200114 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
115
Gabor Mezei535f36d2022-08-02 11:50:44 +0200116 m->p = p;
117 m->n = pn;
118 m->plen = mbedtls_mpi_core_bitlen( p, pn );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200119
Janos Follathba5c1392022-07-19 13:42:07 +0100120 switch( ext_rep )
121 {
122 case MBEDTLS_MPI_MOD_EXT_REP_LE:
123 case MBEDTLS_MPI_MOD_EXT_REP_BE:
124 m->ext_rep = ext_rep; break;
125 default:
126 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
127 goto exit;
128 }
129
130 switch( int_rep )
131 {
132 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
133 m->int_rep = int_rep;
134 m->rep.mont = NULL; break;
135 case MBEDTLS_MPI_MOD_REP_OPT_RED:
136 m->int_rep = int_rep;
137 m->rep.ored = NULL; break;
138 default:
139 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
140 goto exit;
141 }
142
143exit:
144
145 if( ret != 0 )
146 {
147 mbedtls_mpi_mod_modulus_free( m );
148 }
149
150 return( ret );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200151}
152
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200153#endif /* MBEDTLS_BIGNUM_C */