blob: 5f948a499aea954bf9ff81bd21e8f1ffeb9ea6dd [file] [log] [blame]
Gabor Mezeif049dbf2022-07-18 23:02:33 +02001/**
Janos Follath63184682022-08-11 17:42:59 +01002 * Modular bignum functions
Gilles Peskine7aab2fb2022-09-27 13:19:13 +02003 *
4 * This module implements operations on integers modulo some fixed modulus.
Gilles Peskine7f887bd2022-09-27 13:12:30 +02005 */
6
7/*
Gabor Mezeif049dbf2022-07-18 23:02:33 +02008 * Copyright The Mbed TLS Contributors
9 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24#ifndef MBEDTLS_BIGNUM_MOD_H
25#define MBEDTLS_BIGNUM_MOD_H
26
27#include "common.h"
28
29#if defined(MBEDTLS_BIGNUM_C)
30#include "mbedtls/bignum.h"
31#endif
32
Janos Follath296ea662022-08-11 14:58:29 +010033/* Skip 1 as it is slightly easier to accidentally pass to functions. */
34typedef enum
35{
36 MBEDTLS_MPI_MOD_REP_INVALID = 0,
37 MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2,
38 MBEDTLS_MPI_MOD_REP_OPT_RED
39} mbedtls_mpi_mod_rep_selector;
40
41/* Make mbedtls_mpi_mod_rep_selector and mbedtls_mpi_mod_ext_rep disjoint to
42 * make it easier to catch when they are accidentally swapped. */
43typedef enum
44{
45 MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0,
46 MBEDTLS_MPI_MOD_EXT_REP_LE = 8,
47 MBEDTLS_MPI_MOD_EXT_REP_BE
48} mbedtls_mpi_mod_ext_rep;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020049
50typedef struct
51{
Gabor Mezeif049dbf2022-07-18 23:02:33 +020052 mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +020053 size_t limbs;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020054} mbedtls_mpi_mod_residue;
55
Hanno Beckercd860df2022-08-18 16:23:05 +010056typedef struct {
57 mbedtls_mpi_uint const *rr; /* The residue for 2^{2*n*biL} mod N */
58 mbedtls_mpi_uint mm; /* Montgomery const for -N^{-1} mod 2^{ciL} */
59} mbedtls_mpi_mont_struct;
60
Gabor Mezei89e31462022-08-12 15:36:56 +020061typedef void *mbedtls_mpi_opt_red_struct;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020062
63typedef struct {
Janos Follathed5c8d32022-08-15 11:50:22 +010064 const mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +020065 size_t limbs; // number of limbs
66 size_t bits; // bitlen of p
Janos Follath296ea662022-08-11 14:58:29 +010067 mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union
Gabor Mezeif049dbf2022-07-18 23:02:33 +020068 union rep
69 {
70 mbedtls_mpi_mont_struct mont;
71 mbedtls_mpi_opt_red_struct ored;
72 } rep;
73} mbedtls_mpi_mod_modulus;
74
Gabor Mezei37b06362022-08-02 17:22:18 +020075/** Setup a residue structure.
76 *
Minos Galanakisaed832a2022-11-24 09:09:47 +000077 * \param[out] r The address of residue to setup. The resulting structure's
78 * size is determined by \p m.
Janos Follath6b8a4ad2022-08-19 10:58:34 +010079 * \param[in] m The address of the modulus related to \p r.
80 * \param[in] p The address of the limb array storing the value of \p r.
81 * The memory pointed to by \p p will be used by \p r and must
82 * not be modified in any way until after
Minos Galanakisaed832a2022-11-24 09:09:47 +000083 * mbedtls_mpi_mod_residue_release() is called. The data
84 * pointed by p should be compatible (in terms of size/endianness)
85 * with the representation used in \p m.
86 * \param p_limbs The number of limbs of \p p. It must have at most as
87 * many limbs as the modulus \p m.)
Gabor Mezei37b06362022-08-02 17:22:18 +020088 *
89 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +010090 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the
91 * limbs in \p m or if \p p is not less than \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +020092 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +020093int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
Janos Follath6b8a4ad2022-08-19 10:58:34 +010094 const mbedtls_mpi_mod_modulus *m,
Janos Follath8b718b52022-07-25 11:31:02 +010095 mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +010096 size_t p_limbs );
Gabor Mezeif049dbf2022-07-18 23:02:33 +020097
Gabor Mezei37b06362022-08-02 17:22:18 +020098/** Unbind elements of a residue structure.
99 *
Janos Follathdae11472022-08-08 11:50:02 +0100100 * This function removes the reference to the limb array that was passed to
101 * mbedtls_mpi_mod_residue_setup() to make it safe to free or use again.
102 *
103 * This function invalidates \p r and it must not be used until after
104 * mbedtls_mpi_mod_residue_setup() is called on it again.
105 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100106 * \param[out] r The address of residue to release.
Gabor Mezei37b06362022-08-02 17:22:18 +0200107 */
108void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r );
109
110/** Initialize a modulus structure.
111 *
Janos Follatha95f2042022-08-19 12:09:17 +0100112 * \param[out] m The address of the modulus structure to initialize.
Gabor Mezei37b06362022-08-02 17:22:18 +0200113 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200114void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m );
115
Janos Follath63184682022-08-11 17:42:59 +0100116/** Setup a modulus structure.
Gabor Mezei37b06362022-08-02 17:22:18 +0200117 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100118 * \param[out] m The address of the modulus structure to populate.
119 * \param[in] p The address of the limb array storing the value of \p m.
120 * The memory pointed to by \p p will be used by \p m and must
121 * not be modified in any way until after
Janos Follathdae11472022-08-08 11:50:02 +0100122 * mbedtls_mpi_mod_modulus_free() is called.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100123 * \param p_limbs The number of limbs of \p p.
Janos Follathdae11472022-08-08 11:50:02 +0100124 * \param int_rep The internal representation to be used for residues
125 * associated with \p m (see #mbedtls_mpi_mod_rep_selector).
Gabor Mezei37b06362022-08-02 17:22:18 +0200126 *
127 * \return \c 0 if successful.
Janos Follath56a10f92022-08-11 15:19:00 +0100128 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep or \p int_rep is
129 * invalid.
Gabor Mezei37b06362022-08-02 17:22:18 +0200130 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200131int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Janos Follathed5c8d32022-08-15 11:50:22 +0100132 const mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100133 size_t p_limbs,
Janos Follath296ea662022-08-11 14:58:29 +0100134 mbedtls_mpi_mod_rep_selector int_rep );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200135
Janos Follathdae11472022-08-08 11:50:02 +0100136/** Free elements of a modulus structure.
137 *
138 * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup().
139 *
140 * \warning This function does not free the limb array passed to
141 * mbedtls_mpi_mod_modulus_setup() only removes the reference to it,
142 * making it safe to free or to use it again.
Gabor Mezei37b06362022-08-02 17:22:18 +0200143 *
Janos Follatha95f2042022-08-19 12:09:17 +0100144 * \param[in,out] m The address of the modulus structure to free.
Gabor Mezei37b06362022-08-02 17:22:18 +0200145 */
146void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m );
147
Janos Follath5933f692022-11-02 14:35:17 +0000148/* BEGIN MERGE SLOT 1 */
149
150/* END MERGE SLOT 1 */
151
152/* BEGIN MERGE SLOT 2 */
153
154/* END MERGE SLOT 2 */
155
156/* BEGIN MERGE SLOT 3 */
157
158/* END MERGE SLOT 3 */
159
160/* BEGIN MERGE SLOT 4 */
161
162/* END MERGE SLOT 4 */
163
164/* BEGIN MERGE SLOT 5 */
165
166/* END MERGE SLOT 5 */
167
168/* BEGIN MERGE SLOT 6 */
169
170/* END MERGE SLOT 6 */
171
172/* BEGIN MERGE SLOT 7 */
Minos Galanakis81f4b112022-11-10 14:40:38 +0000173/** Read public representation data stored in a buffer into a residue structure.
174 *
175 * The `mbedtls_mpi_mod_residue` and `mbedtls_mpi_mod_modulus` structures must
Minos Galanakis8b375452022-11-24 11:04:11 +0000176 * be compatible (Data in public representation is assumed to be in the m->ext_rep
177 * and will be padded to m->limbs). The data will be automatically converted
178 * into the appropriate internal representation based on the value of `m->int_rep`.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000179 *
Janos Follath3e3fc912022-11-24 18:02:46 +0000180 * \param r The address of the residue related to \p m. It must have as
181 * many limbs as the modulus \p m.
182 * \param m The address of the modulus.
183 * \param buf The input buffer to import from.
184 * \param buflen The length in bytes of \p buf.
185 * \param ext_rep The endianness of the number in the input buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000186 *
187 * \return \c 0 if successful.
188 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
189 * large enough to hold the value in \p buf.
190 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
191 * of \p m is invalid or \p X is not less than \p m.
192 */
193int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
Minos Galanakis8b375452022-11-24 11:04:11 +0000194 const mbedtls_mpi_mod_modulus *m,
195 const unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000196 size_t buflen,
197 mbedtls_mpi_mod_ext_rep ext_rep );
Janos Follath5933f692022-11-02 14:35:17 +0000198
Minos Galanakis81f4b112022-11-10 14:40:38 +0000199/** Write residue data onto a buffer using public representation data.
200 *
201 * The `mbedtls_mpi_mod_residue` and `mbedtls_mpi_mod_modulus` structures must
Minos Galanakis8b375452022-11-24 11:04:11 +0000202 * be compatible (Data will be exported onto the bufer using the m->ext_rep
203 * and will be read as of m->limbs length).The data will be automatically
204 * converted from the appropriate internal representation based on the
205 * value of `m->int_rep field`.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000206 *
Janos Follath3e3fc912022-11-24 18:02:46 +0000207 * \param r The address of the residue related to \p m. It must have as
208 * many limbs as the modulus \p m.
209 * \param m The address of the modulus.
210 * \param buf The output buffer to export to.
211 * \param buflen The length in bytes of \p buf.
212 * \param ext_rep The endianness in which the number should be written into the output buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000213 *
214 * \return \c 0 if successful.
215 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
216 * large enough to hold the value of \p X.
217 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
218 * of \p m is invalid.
219 */
Minos Galanakis8b375452022-11-24 11:04:11 +0000220int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
221 const mbedtls_mpi_mod_modulus *m,
Minos Galanakis81f4b112022-11-10 14:40:38 +0000222 unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000223 size_t buflen,
224 mbedtls_mpi_mod_ext_rep ext_rep );
Janos Follath5933f692022-11-02 14:35:17 +0000225/* END MERGE SLOT 7 */
226
227/* BEGIN MERGE SLOT 8 */
228
229/* END MERGE SLOT 8 */
230
231/* BEGIN MERGE SLOT 9 */
232
233/* END MERGE SLOT 9 */
234
235/* BEGIN MERGE SLOT 10 */
236
237/* END MERGE SLOT 10 */
238
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200239#endif /* MBEDTLS_BIGNUM_MOD_H */