4 * Copyright (C) 2006-2010, Brainspark B.V.
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 /* $Cambridge: exim/src/src/pdkim/bignum.h,v 1.3 2009/12/07 13:05:07 tom Exp $ */
28 #ifndef POLARSSL_BIGNUM_H
29 #define POLARSSL_BIGNUM_H
33 #define POLARSSL_ERR_MPI_FILE_IO_ERROR 0x0002
34 #define POLARSSL_ERR_MPI_BAD_INPUT_DATA 0x0004
35 #define POLARSSL_ERR_MPI_INVALID_CHARACTER 0x0006
36 #define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL 0x0008
37 #define POLARSSL_ERR_MPI_NEGATIVE_VALUE 0x000A
38 #define POLARSSL_ERR_MPI_DIVISION_BY_ZERO 0x000C
39 #define POLARSSL_ERR_MPI_NOT_ACCEPTABLE 0x000E
41 #define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
44 * Define the base integer type, architecture-wise
46 #if defined(POLARSSL_HAVE_INT8)
47 typedef unsigned char t_int;
48 typedef unsigned short t_dbl;
50 #if defined(POLARSSL_HAVE_INT16)
51 typedef unsigned short t_int;
52 typedef unsigned long t_dbl;
54 typedef unsigned long t_int;
55 #if defined(_MSC_VER) && defined(_M_IX86)
56 typedef unsigned __int64 t_dbl;
58 #if defined(__amd64__) || defined(__x86_64__) || \
59 defined(__ppc64__) || defined(__powerpc64__) || \
60 defined(__ia64__) || defined(__alpha__)
61 typedef unsigned int t_dbl __attribute__((mode(TI)));
63 #if defined(POLARSSL_HAVE_LONGLONG)
64 typedef unsigned long long t_dbl;
72 * \brief MPI structure
76 int s; /*!< integer sign */
77 int n; /*!< total # of limbs */
78 t_int *p; /*!< pointer to limbs */
87 * \brief Initialize one or more mpi
89 void mpi_init( mpi *X, ... );
92 * \brief Unallocate one or more mpi
94 void mpi_free( mpi *X, ... );
97 * \brief Enlarge to the specified number of limbs
99 * \param X MPI to grow
100 * \param nblimbs The target number of limbs
102 * \return 0 if successful,
103 * 1 if memory allocation failed
105 int mpi_grow( mpi *X, int nblimbs );
108 * \brief Copy the contents of Y into X
110 * \param X Destination MPI
111 * \param Y Source MPI
113 * \return 0 if successful,
114 * 1 if memory allocation failed
116 int mpi_copy( mpi *X, const mpi *Y );
119 * \brief Swap the contents of X and Y
121 * \param X First MPI value
122 * \param Y Second MPI value
124 void mpi_swap( mpi *X, mpi *Y );
127 * \brief Set value from integer
129 * \param X MPI to set
130 * \param z Value to use
132 * \return 0 if successful,
133 * 1 if memory allocation failed
135 int mpi_lset( mpi *X, int z );
138 * \brief Return the number of least significant bits
140 * \param X MPI to use
142 int mpi_lsb( const mpi *X );
145 * \brief Return the number of most significant bits
147 * \param X MPI to use
149 int mpi_msb( const mpi *X );
152 * \brief Return the total size in bytes
154 * \param X MPI to use
156 int mpi_size( const mpi *X );
159 * \brief Import from an ASCII string
161 * \param X Destination MPI
162 * \param radix Input numeric base
163 * \param s Null-terminated string buffer
165 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
167 int mpi_read_string( mpi *X, int radix, const char *s );
170 * \brief Export into an ASCII string
172 * \param X Source MPI
173 * \param radix Output numeric base
174 * \param s String buffer
175 * \param slen String buffer size
177 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code.
178 * *slen is always updated to reflect the amount
179 * of data that has (or would have) been written.
181 * \note Call this function with *slen = 0 to obtain the
182 * minimum required buffer size in *slen.
184 int mpi_write_string( const mpi *X, int radix, char *s, int *slen );
187 * \brief Read X from an opened file
189 * \param X Destination MPI
190 * \param radix Input numeric base
191 * \param fin Input file handle
193 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
195 int mpi_read_file( mpi *X, int radix, FILE *fin );
198 * \brief Write X into an opened file, or stdout if fout is NULL
200 * \param p Prefix, can be NULL
201 * \param X Source MPI
202 * \param radix Output numeric base
203 * \param fout Output file handle (can be NULL)
205 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
207 * \note Set fout == NULL to print X on the console.
209 int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
212 * \brief Import X from unsigned binary data, big endian
214 * \param X Destination MPI
215 * \param buf Input buffer
216 * \param buflen Input buffer size
218 * \return 0 if successful,
219 * 1 if memory allocation failed
221 int mpi_read_binary( mpi *X, const unsigned char *buf, int buflen );
224 * \brief Export X into unsigned binary data, big endian
226 * \param X Source MPI
227 * \param buf Output buffer
228 * \param buflen Output buffer size
230 * \return 0 if successful,
231 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
233 int mpi_write_binary( const mpi *X, unsigned char *buf, int buflen );
236 * \brief Left-shift: X <<= count
238 * \param X MPI to shift
239 * \param count Amount to shift
241 * \return 0 if successful,
242 * 1 if memory allocation failed
244 int mpi_shift_l( mpi *X, int count );
247 * \brief Right-shift: X >>= count
249 * \param X MPI to shift
250 * \param count Amount to shift
252 * \return 0 if successful,
253 * 1 if memory allocation failed
255 int mpi_shift_r( mpi *X, int count );
258 * \brief Compare unsigned values
260 * \param X Left-hand MPI
261 * \param Y Right-hand MPI
263 * \return 1 if |X| is greater than |Y|,
264 * -1 if |X| is lesser than |Y| or
265 * 0 if |X| is equal to |Y|
267 int mpi_cmp_abs( const mpi *X, const mpi *Y );
270 * \brief Compare signed values
272 * \param X Left-hand MPI
273 * \param Y Right-hand MPI
275 * \return 1 if X is greater than Y,
276 * -1 if X is lesser than Y or
277 * 0 if X is equal to Y
279 int mpi_cmp_mpi( const mpi *X, const mpi *Y );
282 * \brief Compare signed values
284 * \param X Left-hand MPI
285 * \param z The integer value to compare to
287 * \return 1 if X is greater than z,
288 * -1 if X is lesser than z or
289 * 0 if X is equal to z
291 int mpi_cmp_int( const mpi *X, int z );
294 * \brief Unsigned addition: X = |A| + |B|
296 * \param X Destination MPI
297 * \param A Left-hand MPI
298 * \param B Right-hand MPI
300 * \return 0 if successful,
301 * 1 if memory allocation failed
303 int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
306 * \brief Unsigned substraction: X = |A| - |B|
308 * \param X Destination MPI
309 * \param A Left-hand MPI
310 * \param B Right-hand MPI
312 * \return 0 if successful,
313 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
315 int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
318 * \brief Signed addition: X = A + B
320 * \param X Destination MPI
321 * \param A Left-hand MPI
322 * \param B Right-hand MPI
324 * \return 0 if successful,
325 * 1 if memory allocation failed
327 int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
330 * \brief Signed substraction: X = A - B
332 * \param X Destination MPI
333 * \param A Left-hand MPI
334 * \param B Right-hand MPI
336 * \return 0 if successful,
337 * 1 if memory allocation failed
339 int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
342 * \brief Signed addition: X = A + b
344 * \param X Destination MPI
345 * \param A Left-hand MPI
346 * \param b The integer value to add
348 * \return 0 if successful,
349 * 1 if memory allocation failed
351 int mpi_add_int( mpi *X, const mpi *A, int b );
354 * \brief Signed substraction: X = A - b
356 * \param X Destination MPI
357 * \param A Left-hand MPI
358 * \param b The integer value to subtract
360 * \return 0 if successful,
361 * 1 if memory allocation failed
363 int mpi_sub_int( mpi *X, const mpi *A, int b );
366 * \brief Baseline multiplication: X = A * B
368 * \param X Destination MPI
369 * \param A Left-hand MPI
370 * \param B Right-hand MPI
372 * \return 0 if successful,
373 * 1 if memory allocation failed
375 int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
378 * \brief Baseline multiplication: X = A * b
379 * Note: b is an unsigned integer type, thus
380 * Negative values of b are ignored.
382 * \param X Destination MPI
383 * \param A Left-hand MPI
384 * \param b The integer value to multiply with
386 * \return 0 if successful,
387 * 1 if memory allocation failed
389 int mpi_mul_int( mpi *X, const mpi *A, t_int b );
392 * \brief Division by mpi: A = Q * B + R
394 * \param Q Destination MPI for the quotient
395 * \param R Destination MPI for the rest value
396 * \param A Left-hand MPI
397 * \param B Right-hand MPI
399 * \return 0 if successful,
400 * 1 if memory allocation failed,
401 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
403 * \note Either Q or R can be NULL.
405 int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
408 * \brief Division by int: A = Q * b + R
410 * \param Q Destination MPI for the quotient
411 * \param R Destination MPI for the rest value
412 * \param A Left-hand MPI
413 * \param b Integer to divide by
415 * \return 0 if successful,
416 * 1 if memory allocation failed,
417 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
419 * \note Either Q or R can be NULL.
421 int mpi_div_int( mpi *Q, mpi *R, const mpi *A, int b );
424 * \brief Modulo: R = A mod B
426 * \param R Destination MPI for the rest value
427 * \param A Left-hand MPI
428 * \param B Right-hand MPI
430 * \return 0 if successful,
431 * 1 if memory allocation failed,
432 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
433 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
435 int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
438 * \brief Modulo: r = A mod b
440 * \param r Destination t_int
441 * \param A Left-hand MPI
442 * \param b Integer to divide by
444 * \return 0 if successful,
445 * 1 if memory allocation failed,
446 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
447 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
449 int mpi_mod_int( t_int *r, const mpi *A, int b );
452 * \brief Sliding-window exponentiation: X = A^E mod N
454 * \param X Destination MPI
455 * \param A Left-hand MPI
456 * \param E Exponent MPI
457 * \param N Modular MPI
458 * \param _RR Speed-up MPI used for recalculations
460 * \return 0 if successful,
461 * 1 if memory allocation failed,
462 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
464 * \note _RR is used to avoid re-computing R*R mod N across
465 * multiple calls, which speeds up things a bit. It can
466 * be set to NULL if the extra performance is unneeded.
468 int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
471 * \brief Greatest common divisor: G = gcd(A, B)
473 * \param G Destination MPI
474 * \param A Left-hand MPI
475 * \param B Right-hand MPI
477 * \return 0 if successful,
478 * 1 if memory allocation failed
480 int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
483 * \brief Modular inverse: X = A^-1 mod N
485 * \param X Destination MPI
486 * \param A Left-hand MPI
487 * \param N Right-hand MPI
489 * \return 0 if successful,
490 * 1 if memory allocation failed,
491 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
492 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
494 int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
497 * \brief Miller-Rabin primality test
499 * \param X MPI to check
500 * \param f_rng RNG function
501 * \param p_rng RNG parameter
503 * \return 0 if successful (probably prime),
504 * 1 if memory allocation failed,
505 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
507 int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
510 * \brief Prime number generation
512 * \param X Destination MPI
513 * \param nbits Required size of X in bits
514 * \param dh_flag If 1, then (X-1)/2 will be prime too
515 * \param f_rng RNG function
516 * \param p_rng RNG parameter
518 * \return 0 if successful (probably prime),
519 * 1 if memory allocation failed,
520 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
522 int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
523 int (*f_rng)(void *), void *p_rng );
529 #endif /* bignum.h */