blob: 67c48498e13be425b41b14244ff062aa29667dd4 [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 *
Janos Follathfc6fbb42022-11-25 15:43:17 +000077 * The residue will be set up with the buffer \p p and modulus \p m.
Janos Follath41427de2022-11-24 19:04:54 +000078 *
Janos Follathfc6fbb42022-11-25 15:43:17 +000079 * The memory pointed to by \p p will be used by the resulting residue structure.
80 * The value at the pointed-to memory will be the initial value of \p r and must
81 * hold a value that is less than the modulus. This value will be used as-is
Janos Follath41427de2022-11-24 19:04:54 +000082 * and interpreted according to the value of the `m->int_rep` field.
83 *
84 * The modulus \p m will be the modulus associated with \p r. The residue \p r
85 * should only be used in operations where the modulus is \p m or a modulus
Janos Follathfc6fbb42022-11-25 15:43:17 +000086 * equivalent to \p m (in the sense that all their fields or memory pointed to by
Janos Follath41427de2022-11-24 19:04:54 +000087 * their fields hold the same value).
88 *
Janos Follathee530cc2022-11-25 15:54:40 +000089 * \param[out] r The address of the residue to setup.
Janos Follath6b8a4ad2022-08-19 10:58:34 +010090 * \param[in] m The address of the modulus related to \p r.
Janos Follathfc6fbb42022-11-25 15:43:17 +000091 * \param[in] p The address of the limb array containing the value of \p r.
Janos Follath6b8a4ad2022-08-19 10:58:34 +010092 * The memory pointed to by \p p will be used by \p r and must
93 * not be modified in any way until after
Minos Galanakisaed832a2022-11-24 09:09:47 +000094 * mbedtls_mpi_mod_residue_release() is called. The data
Janos Follathfc6fbb42022-11-25 15:43:17 +000095 * pointed to by \p p must be less than the modulus (the value
96 * pointed to by `m->p`) and already in the representation
Janos Follath41427de2022-11-24 19:04:54 +000097 * indicated by `m->int_rep`.
Janos Follathee530cc2022-11-25 15:54:40 +000098 * \param p_limbs The number of limbs of \p p. Must be the same as the number
99 * of limbs in the modulus \p m.)
Gabor Mezei37b06362022-08-02 17:22:18 +0200100 *
101 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100102 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the
103 * limbs in \p m or if \p p is not less than \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +0200104 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200105int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100106 const mbedtls_mpi_mod_modulus *m,
Janos Follath8b718b52022-07-25 11:31:02 +0100107 mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100108 size_t p_limbs );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200109
Gabor Mezei37b06362022-08-02 17:22:18 +0200110/** Unbind elements of a residue structure.
111 *
Janos Follathdae11472022-08-08 11:50:02 +0100112 * This function removes the reference to the limb array that was passed to
113 * mbedtls_mpi_mod_residue_setup() to make it safe to free or use again.
114 *
115 * This function invalidates \p r and it must not be used until after
116 * mbedtls_mpi_mod_residue_setup() is called on it again.
117 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100118 * \param[out] r The address of residue to release.
Gabor Mezei37b06362022-08-02 17:22:18 +0200119 */
120void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r );
121
122/** Initialize a modulus structure.
123 *
Janos Follatha95f2042022-08-19 12:09:17 +0100124 * \param[out] m The address of the modulus structure to initialize.
Gabor Mezei37b06362022-08-02 17:22:18 +0200125 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200126void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m );
127
Janos Follath63184682022-08-11 17:42:59 +0100128/** Setup a modulus structure.
Gabor Mezei37b06362022-08-02 17:22:18 +0200129 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100130 * \param[out] m The address of the modulus structure to populate.
131 * \param[in] p The address of the limb array storing the value of \p m.
132 * The memory pointed to by \p p will be used by \p m and must
133 * not be modified in any way until after
Janos Follathdae11472022-08-08 11:50:02 +0100134 * mbedtls_mpi_mod_modulus_free() is called.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100135 * \param p_limbs The number of limbs of \p p.
Janos Follathdae11472022-08-08 11:50:02 +0100136 * \param int_rep The internal representation to be used for residues
137 * associated with \p m (see #mbedtls_mpi_mod_rep_selector).
Gabor Mezei37b06362022-08-02 17:22:18 +0200138 *
139 * \return \c 0 if successful.
Janos Follathee530cc2022-11-25 15:54:40 +0000140 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p int_rep is invalid.
Gabor Mezei37b06362022-08-02 17:22:18 +0200141 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200142int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Janos Follathed5c8d32022-08-15 11:50:22 +0100143 const mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100144 size_t p_limbs,
Janos Follath296ea662022-08-11 14:58:29 +0100145 mbedtls_mpi_mod_rep_selector int_rep );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200146
Janos Follathdae11472022-08-08 11:50:02 +0100147/** Free elements of a modulus structure.
148 *
149 * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup().
150 *
151 * \warning This function does not free the limb array passed to
152 * mbedtls_mpi_mod_modulus_setup() only removes the reference to it,
153 * making it safe to free or to use it again.
Gabor Mezei37b06362022-08-02 17:22:18 +0200154 *
Janos Follatha95f2042022-08-19 12:09:17 +0100155 * \param[in,out] m The address of the modulus structure to free.
Gabor Mezei37b06362022-08-02 17:22:18 +0200156 */
157void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m );
158
Janos Follath5933f692022-11-02 14:35:17 +0000159/* BEGIN MERGE SLOT 1 */
160
161/* END MERGE SLOT 1 */
162
163/* BEGIN MERGE SLOT 2 */
164
165/* END MERGE SLOT 2 */
166
167/* BEGIN MERGE SLOT 3 */
168
169/* END MERGE SLOT 3 */
170
171/* BEGIN MERGE SLOT 4 */
172
173/* END MERGE SLOT 4 */
174
175/* BEGIN MERGE SLOT 5 */
176
177/* END MERGE SLOT 5 */
178
179/* BEGIN MERGE SLOT 6 */
180
181/* END MERGE SLOT 6 */
182
183/* BEGIN MERGE SLOT 7 */
Janos Follath41427de2022-11-24 19:04:54 +0000184/** Read a residue from a byte buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000185 *
Janos Follath41427de2022-11-24 19:04:54 +0000186 * The residue will be automatically converted to the internal representation
Janos Follathfc6fbb42022-11-25 15:43:17 +0000187 * based on the value of the `m->int_rep` field.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000188 *
Janos Follath41427de2022-11-24 19:04:54 +0000189 * The modulus \p m will be the modulus associated with \p r. The residue \p r
190 * should only be used in operations where the modulus is \p m or a modulus
191 * equivalent to \p m (in the sense that all their fields or memory pointed by
192 * their fields hold the same value).
193 *
Janos Follathfc6fbb42022-11-25 15:43:17 +0000194 * \param r The address of the residue. It must have exactly the same
195 * number of limbs as the modulus \p m.
Janos Follath3e3fc912022-11-24 18:02:46 +0000196 * \param m The address of the modulus.
197 * \param buf The input buffer to import from.
198 * \param buflen The length in bytes of \p buf.
199 * \param ext_rep The endianness of the number in the input buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000200 *
201 * \return \c 0 if successful.
Janos Follath41427de2022-11-24 19:04:54 +0000202 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p r isn't
Minos Galanakis81f4b112022-11-10 14:40:38 +0000203 * large enough to hold the value in \p buf.
Janos Follath41427de2022-11-24 19:04:54 +0000204 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep
205 * is invalid or the value in the buffer is not less than \p m.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000206 */
207int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
Minos Galanakis8b375452022-11-24 11:04:11 +0000208 const mbedtls_mpi_mod_modulus *m,
209 const unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000210 size_t buflen,
211 mbedtls_mpi_mod_ext_rep ext_rep );
Janos Follath5933f692022-11-02 14:35:17 +0000212
Janos Follath41427de2022-11-24 19:04:54 +0000213/** Write a residue into a byte buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000214 *
Janos Follath41427de2022-11-24 19:04:54 +0000215 * The modulus \p m must be the modulus associated with \p r (see
216 * mbedtls_mpi_mod_residue_setup() and mbedtls_mpi_mod_read()).
Minos Galanakis81f4b112022-11-10 14:40:38 +0000217 *
Janos Follath41427de2022-11-24 19:04:54 +0000218 * The residue will be automatically converted from the internal representation
219 * based on the value of `m->int_rep` field.
220 *
221 * \warning If the buffer is smaller than `m->bits`, the number of
222 * leading zeroes is leaked through side channels. If \p r is
223 * secret, the caller must ensure that \p buflen is at least
224 * (`m->bits`+7)/8.
225 *
226 * \param r The address of the residue. It must have as many limbs as
227 * the modulus \p m.
228 * \param m The address of the modulus associated with \r.
Janos Follath3e3fc912022-11-24 18:02:46 +0000229 * \param buf The output buffer to export to.
230 * \param buflen The length in bytes of \p buf.
Janos Follath41427de2022-11-24 19:04:54 +0000231 * \param ext_rep The endianness in which the number should be written into
232 * the output buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000233 *
234 * \return \c 0 if successful.
235 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
Janos Follath41427de2022-11-24 19:04:54 +0000236 * large enough to hold the value of \p r (without leading
237 * zeroes).
Janos Follathfc6fbb42022-11-25 15:43:17 +0000238 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep is invalid.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000239 */
Minos Galanakis8b375452022-11-24 11:04:11 +0000240int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
241 const mbedtls_mpi_mod_modulus *m,
Minos Galanakis81f4b112022-11-10 14:40:38 +0000242 unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000243 size_t buflen,
244 mbedtls_mpi_mod_ext_rep ext_rep );
Janos Follath5933f692022-11-02 14:35:17 +0000245/* END MERGE SLOT 7 */
246
247/* BEGIN MERGE SLOT 8 */
248
249/* END MERGE SLOT 8 */
250
251/* BEGIN MERGE SLOT 9 */
252
253/* END MERGE SLOT 9 */
254
255/* BEGIN MERGE SLOT 10 */
256
257/* END MERGE SLOT 10 */
258
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200259#endif /* MBEDTLS_BIGNUM_MOD_H */