4 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
7 * Joined copyright on original XySSL code with: Christophe Devine
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 /* $Cambridge: exim/src/src/pdkim/bignum.h,v 1.3 2009/12/07 13:05:07 tom Exp $ */
26 #ifndef POLARSSL_BIGNUM_H
27 #define POLARSSL_BIGNUM_H
31 #define POLARSSL_ERR_MPI_FILE_IO_ERROR 0x0002
32 #define POLARSSL_ERR_MPI_BAD_INPUT_DATA 0x0004
33 #define POLARSSL_ERR_MPI_INVALID_CHARACTER 0x0006
34 #define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL 0x0008
35 #define POLARSSL_ERR_MPI_NEGATIVE_VALUE 0x000A
36 #define POLARSSL_ERR_MPI_DIVISION_BY_ZERO 0x000C
37 #define POLARSSL_ERR_MPI_NOT_ACCEPTABLE 0x000E
39 #define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
42 * Define the base integer type, architecture-wise
44 #if defined(POLARSSL_HAVE_INT8)
45 typedef unsigned char t_int;
46 typedef unsigned short t_dbl;
48 #if defined(POLARSSL_HAVE_INT16)
49 typedef unsigned short t_int;
50 typedef unsigned long t_dbl;
52 typedef unsigned long t_int;
53 #if defined(_MSC_VER) && defined(_M_IX86)
54 typedef unsigned __int64 t_dbl;
56 #if defined(__amd64__) || defined(__x86_64__) || \
57 defined(__ppc64__) || defined(__powerpc64__) || \
58 defined(__ia64__) || defined(__alpha__)
59 typedef unsigned int t_dbl __attribute__((mode(TI)));
61 #if defined(POLARSSL_HAVE_LONGLONG)
62 typedef unsigned long long t_dbl;
70 * \brief MPI structure
74 int s; /*!< integer sign */
75 int n; /*!< total # of limbs */
76 t_int *p; /*!< pointer to limbs */
85 * \brief Initialize one or more mpi
87 void mpi_init( mpi *X, ... );
90 * \brief Unallocate one or more mpi
92 void mpi_free( mpi *X, ... );
95 * \brief Enlarge to the specified number of limbs
97 * \param X MPI to grow
98 * \param nblimbs The target number of limbs
100 * \return 0 if successful,
101 * 1 if memory allocation failed
103 int mpi_grow( mpi *X, int nblimbs );
106 * \brief Copy the contents of Y into X
108 * \param X Destination MPI
109 * \param Y Source MPI
111 * \return 0 if successful,
112 * 1 if memory allocation failed
114 int mpi_copy( mpi *X, mpi *Y );
117 * \brief Swap the contents of X and Y
119 * \param X First MPI value
120 * \param Y Second MPI value
122 void mpi_swap( mpi *X, mpi *Y );
125 * \brief Set value from integer
127 * \param X MPI to set
128 * \param z Value to use
130 * \return 0 if successful,
131 * 1 if memory allocation failed
133 int mpi_lset( mpi *X, int z );
136 * \brief Return the number of least significant bits
138 * \param X MPI to use
140 int mpi_lsb( mpi *X );
143 * \brief Return the number of most significant bits
145 * \param X MPI to use
147 int mpi_msb( mpi *X );
150 * \brief Return the total size in bytes
152 * \param X MPI to use
154 int mpi_size( mpi *X );
157 * \brief Import from an ASCII string
159 * \param X Destination MPI
160 * \param radix Input numeric base
161 * \param s Null-terminated string buffer
163 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
165 int mpi_read_string( mpi *X, int radix, char *s );
168 * \brief Export into an ASCII string
170 * \param X Source MPI
171 * \param radix Output numeric base
172 * \param s String buffer
173 * \param slen String buffer size
175 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
177 * \note Call this function with *slen = 0 to obtain the
178 * minimum required buffer size in *slen.
180 int mpi_write_string( mpi *X, int radix, char *s, int *slen );
183 * \brief Read X from an opened file
185 * \param X Destination MPI
186 * \param radix Input numeric base
187 * \param fin Input file handle
189 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
191 int mpi_read_file( mpi *X, int radix, FILE *fin );
194 * \brief Write X into an opened file, or stdout if fout is NULL
196 * \param p Prefix, can be NULL
197 * \param X Source MPI
198 * \param radix Output numeric base
199 * \param fout Output file handle (can be NULL)
201 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
203 * \note Set fout == NULL to print X on the console.
205 int mpi_write_file( const char *p, mpi *X, int radix, FILE *fout );
208 * \brief Import X from unsigned binary data, big endian
210 * \param X Destination MPI
211 * \param buf Input buffer
212 * \param buflen Input buffer size
214 * \return 0 if successful,
215 * 1 if memory allocation failed
217 int mpi_read_binary( mpi *X, unsigned char *buf, int buflen );
220 * \brief Export X into unsigned binary data, big endian
222 * \param X Source MPI
223 * \param buf Output buffer
224 * \param buflen Output buffer size
226 * \return 0 if successful,
227 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
229 int mpi_write_binary( mpi *X, unsigned char *buf, int buflen );
232 * \brief Left-shift: X <<= count
234 * \param X MPI to shift
235 * \param count Amount to shift
237 * \return 0 if successful,
238 * 1 if memory allocation failed
240 int mpi_shift_l( mpi *X, int count );
243 * \brief Right-shift: X >>= count
245 * \param X MPI to shift
246 * \param count Amount to shift
248 * \return 0 if successful,
249 * 1 if memory allocation failed
251 int mpi_shift_r( mpi *X, int count );
254 * \brief Compare unsigned values
256 * \param X Left-hand MPI
257 * \param Y Right-hand MPI
259 * \return 1 if |X| is greater than |Y|,
260 * -1 if |X| is lesser than |Y| or
261 * 0 if |X| is equal to |Y|
263 int mpi_cmp_abs( mpi *X, mpi *Y );
266 * \brief Compare signed values
268 * \param X Left-hand MPI
269 * \param Y Right-hand MPI
271 * \return 1 if X is greater than Y,
272 * -1 if X is lesser than Y or
273 * 0 if X is equal to Y
275 int mpi_cmp_mpi( mpi *X, mpi *Y );
278 * \brief Compare signed values
280 * \param X Left-hand MPI
281 * \param z The integer value to compare to
283 * \return 1 if X is greater than z,
284 * -1 if X is lesser than z or
285 * 0 if X is equal to z
287 int mpi_cmp_int( mpi *X, int z );
290 * \brief Unsigned addition: X = |A| + |B|
292 * \param X Destination MPI
293 * \param A Left-hand MPI
294 * \param B Right-hand MPI
296 * \return 0 if successful,
297 * 1 if memory allocation failed
299 int mpi_add_abs( mpi *X, mpi *A, mpi *B );
302 * \brief Unsigned substraction: X = |A| - |B|
304 * \param X Destination MPI
305 * \param A Left-hand MPI
306 * \param B Right-hand MPI
308 * \return 0 if successful,
309 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
311 int mpi_sub_abs( mpi *X, mpi *A, mpi *B );
314 * \brief Signed addition: X = A + B
316 * \param X Destination MPI
317 * \param A Left-hand MPI
318 * \param B Right-hand MPI
320 * \return 0 if successful,
321 * 1 if memory allocation failed
323 int mpi_add_mpi( mpi *X, mpi *A, mpi *B );
326 * \brief Signed substraction: X = A - B
328 * \param X Destination MPI
329 * \param A Left-hand MPI
330 * \param B Right-hand MPI
332 * \return 0 if successful,
333 * 1 if memory allocation failed
335 int mpi_sub_mpi( mpi *X, mpi *A, mpi *B );
338 * \brief Signed addition: X = A + b
340 * \param X Destination MPI
341 * \param A Left-hand MPI
342 * \param b The integer value to add
344 * \return 0 if successful,
345 * 1 if memory allocation failed
347 int mpi_add_int( mpi *X, mpi *A, int b );
350 * \brief Signed substraction: X = A - b
352 * \param X Destination MPI
353 * \param A Left-hand MPI
354 * \param b The integer value to subtract
356 * \return 0 if successful,
357 * 1 if memory allocation failed
359 int mpi_sub_int( mpi *X, mpi *A, int b );
362 * \brief Baseline multiplication: X = A * B
364 * \param X Destination MPI
365 * \param A Left-hand MPI
366 * \param B Right-hand MPI
368 * \return 0 if successful,
369 * 1 if memory allocation failed
371 int mpi_mul_mpi( mpi *X, mpi *A, mpi *B );
374 * \brief Baseline multiplication: X = A * b
375 * Note: b is an unsigned integer type, thus
376 * Negative values of b are ignored.
378 * \param X Destination MPI
379 * \param A Left-hand MPI
380 * \param b The integer value to multiply with
382 * \return 0 if successful,
383 * 1 if memory allocation failed
385 int mpi_mul_int( mpi *X, mpi *A, t_int b );
388 * \brief Division by mpi: A = Q * B + R
390 * \param Q Destination MPI for the quotient
391 * \param R Destination MPI for the rest value
392 * \param A Left-hand MPI
393 * \param B Right-hand MPI
395 * \return 0 if successful,
396 * 1 if memory allocation failed,
397 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
399 * \note Either Q or R can be NULL.
401 int mpi_div_mpi( mpi *Q, mpi *R, mpi *A, mpi *B );
404 * \brief Division by int: A = Q * b + R
406 * \param Q Destination MPI for the quotient
407 * \param R Destination MPI for the rest value
408 * \param A Left-hand MPI
409 * \param b Integer to divide by
411 * \return 0 if successful,
412 * 1 if memory allocation failed,
413 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
415 * \note Either Q or R can be NULL.
417 int mpi_div_int( mpi *Q, mpi *R, mpi *A, int b );
420 * \brief Modulo: R = A mod B
422 * \param R Destination MPI for the rest value
423 * \param A Left-hand MPI
424 * \param B Right-hand MPI
426 * \return 0 if successful,
427 * 1 if memory allocation failed,
428 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
429 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
431 int mpi_mod_mpi( mpi *R, mpi *A, mpi *B );
434 * \brief Modulo: r = A mod b
436 * \param a Destination t_int
437 * \param A Left-hand MPI
438 * \param b Integer to divide by
440 * \return 0 if successful,
441 * 1 if memory allocation failed,
442 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
443 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
445 int mpi_mod_int( t_int *r, mpi *A, int b );
448 * \brief Sliding-window exponentiation: X = A^E mod N
450 * \param X Destination MPI
451 * \param A Left-hand MPI
452 * \param E Exponent MPI
453 * \param N Modular MPI
454 * \param _RR Speed-up MPI used for recalculations
456 * \return 0 if successful,
457 * 1 if memory allocation failed,
458 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
460 * \note _RR is used to avoid re-computing R*R mod N across
461 * multiple calls, which speeds up things a bit. It can
462 * be set to NULL if the extra performance is unneeded.
464 int mpi_exp_mod( mpi *X, mpi *A, mpi *E, mpi *N, mpi *_RR );
467 * \brief Greatest common divisor: G = gcd(A, B)
469 * \param G Destination MPI
470 * \param A Left-hand MPI
471 * \param B Right-hand MPI
473 * \return 0 if successful,
474 * 1 if memory allocation failed
476 int mpi_gcd( mpi *G, mpi *A, mpi *B );
479 * \brief Modular inverse: X = A^-1 mod N
481 * \param X Destination MPI
482 * \param A Left-hand MPI
483 * \param N Right-hand MPI
485 * \return 0 if successful,
486 * 1 if memory allocation failed,
487 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
488 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
490 int mpi_inv_mod( mpi *X, mpi *A, mpi *N );
493 * \brief Miller-Rabin primality test
495 * \param X MPI to check
496 * \param f_rng RNG function
497 * \param p_rng RNG parameter
499 * \return 0 if successful (probably prime),
500 * 1 if memory allocation failed,
501 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
503 int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
506 * \brief Prime number generation
508 * \param X Destination MPI
509 * \param nbits Required size of X in bits
510 * \param dh_flag If 1, then (X-1)/2 will be prime too
511 * \param f_rng RNG function
512 * \param p_rng RNG parameter
514 * \return 0 if successful (probably prime),
515 * 1 if memory allocation failed,
516 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
518 int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
519 int (*f_rng)(void *), void *p_rng );
525 #endif /* bignum.h */