doc/doc-txt/NewStuff: note the ratelimit changes.
[exim.git] / src / src / pdkim / bignum.h
1 /**
2 * \file bignum.h
3 *
4 * Copyright (C) 2006-2010, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
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.
15 *
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.
20 *
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.
24 */
25
26 /* $Cambridge: exim/src/src/pdkim/bignum.h,v 1.3 2009/12/07 13:05:07 tom Exp $ */
27
28 #ifndef POLARSSL_BIGNUM_H
29 #define POLARSSL_BIGNUM_H
30
31 #include <stdio.h>
32
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
40
41 #define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
42
43 /*
44 * Define the base integer type, architecture-wise
45 */
46 #if defined(POLARSSL_HAVE_INT8)
47 typedef unsigned char t_int;
48 typedef unsigned short t_dbl;
49 #else
50 #if defined(POLARSSL_HAVE_INT16)
51 typedef unsigned short t_int;
52 typedef unsigned long t_dbl;
53 #else
54 typedef unsigned long t_int;
55 #if defined(_MSC_VER) && defined(_M_IX86)
56 typedef unsigned __int64 t_dbl;
57 #else
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)));
62 #else
63 #if defined(POLARSSL_HAVE_LONGLONG)
64 typedef unsigned long long t_dbl;
65 #endif
66 #endif
67 #endif
68 #endif
69 #endif
70
71 /**
72 * \brief MPI structure
73 */
74 typedef struct
75 {
76 int s; /*!< integer sign */
77 int n; /*!< total # of limbs */
78 t_int *p; /*!< pointer to limbs */
79 }
80 mpi;
81
82 #ifdef __cplusplus
83 extern "C" {
84 #endif
85
86 /**
87 * \brief Initialize one or more mpi
88 */
89 void mpi_init( mpi *X, ... );
90
91 /**
92 * \brief Unallocate one or more mpi
93 */
94 void mpi_free( mpi *X, ... );
95
96 /**
97 * \brief Enlarge to the specified number of limbs
98 *
99 * \param X MPI to grow
100 * \param nblimbs The target number of limbs
101 *
102 * \return 0 if successful,
103 * 1 if memory allocation failed
104 */
105 int mpi_grow( mpi *X, int nblimbs );
106
107 /**
108 * \brief Copy the contents of Y into X
109 *
110 * \param X Destination MPI
111 * \param Y Source MPI
112 *
113 * \return 0 if successful,
114 * 1 if memory allocation failed
115 */
116 int mpi_copy( mpi *X, const mpi *Y );
117
118 /**
119 * \brief Swap the contents of X and Y
120 *
121 * \param X First MPI value
122 * \param Y Second MPI value
123 */
124 void mpi_swap( mpi *X, mpi *Y );
125
126 /**
127 * \brief Set value from integer
128 *
129 * \param X MPI to set
130 * \param z Value to use
131 *
132 * \return 0 if successful,
133 * 1 if memory allocation failed
134 */
135 int mpi_lset( mpi *X, int z );
136
137 /**
138 * \brief Return the number of least significant bits
139 *
140 * \param X MPI to use
141 */
142 int mpi_lsb( const mpi *X );
143
144 /**
145 * \brief Return the number of most significant bits
146 *
147 * \param X MPI to use
148 */
149 int mpi_msb( const mpi *X );
150
151 /**
152 * \brief Return the total size in bytes
153 *
154 * \param X MPI to use
155 */
156 int mpi_size( const mpi *X );
157
158 /**
159 * \brief Import from an ASCII string
160 *
161 * \param X Destination MPI
162 * \param radix Input numeric base
163 * \param s Null-terminated string buffer
164 *
165 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
166 */
167 int mpi_read_string( mpi *X, int radix, const char *s );
168
169 /**
170 * \brief Export into an ASCII string
171 *
172 * \param X Source MPI
173 * \param radix Output numeric base
174 * \param s String buffer
175 * \param slen String buffer size
176 *
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.
180 *
181 * \note Call this function with *slen = 0 to obtain the
182 * minimum required buffer size in *slen.
183 */
184 int mpi_write_string( const mpi *X, int radix, char *s, int *slen );
185
186 /**
187 * \brief Read X from an opened file
188 *
189 * \param X Destination MPI
190 * \param radix Input numeric base
191 * \param fin Input file handle
192 *
193 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
194 */
195 int mpi_read_file( mpi *X, int radix, FILE *fin );
196
197 /**
198 * \brief Write X into an opened file, or stdout if fout is NULL
199 *
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)
204 *
205 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
206 *
207 * \note Set fout == NULL to print X on the console.
208 */
209 int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
210
211 /**
212 * \brief Import X from unsigned binary data, big endian
213 *
214 * \param X Destination MPI
215 * \param buf Input buffer
216 * \param buflen Input buffer size
217 *
218 * \return 0 if successful,
219 * 1 if memory allocation failed
220 */
221 int mpi_read_binary( mpi *X, const unsigned char *buf, int buflen );
222
223 /**
224 * \brief Export X into unsigned binary data, big endian
225 *
226 * \param X Source MPI
227 * \param buf Output buffer
228 * \param buflen Output buffer size
229 *
230 * \return 0 if successful,
231 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
232 */
233 int mpi_write_binary( const mpi *X, unsigned char *buf, int buflen );
234
235 /**
236 * \brief Left-shift: X <<= count
237 *
238 * \param X MPI to shift
239 * \param count Amount to shift
240 *
241 * \return 0 if successful,
242 * 1 if memory allocation failed
243 */
244 int mpi_shift_l( mpi *X, int count );
245
246 /**
247 * \brief Right-shift: X >>= count
248 *
249 * \param X MPI to shift
250 * \param count Amount to shift
251 *
252 * \return 0 if successful,
253 * 1 if memory allocation failed
254 */
255 int mpi_shift_r( mpi *X, int count );
256
257 /**
258 * \brief Compare unsigned values
259 *
260 * \param X Left-hand MPI
261 * \param Y Right-hand MPI
262 *
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|
266 */
267 int mpi_cmp_abs( const mpi *X, const mpi *Y );
268
269 /**
270 * \brief Compare signed values
271 *
272 * \param X Left-hand MPI
273 * \param Y Right-hand MPI
274 *
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
278 */
279 int mpi_cmp_mpi( const mpi *X, const mpi *Y );
280
281 /**
282 * \brief Compare signed values
283 *
284 * \param X Left-hand MPI
285 * \param z The integer value to compare to
286 *
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
290 */
291 int mpi_cmp_int( const mpi *X, int z );
292
293 /**
294 * \brief Unsigned addition: X = |A| + |B|
295 *
296 * \param X Destination MPI
297 * \param A Left-hand MPI
298 * \param B Right-hand MPI
299 *
300 * \return 0 if successful,
301 * 1 if memory allocation failed
302 */
303 int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
304
305 /**
306 * \brief Unsigned substraction: X = |A| - |B|
307 *
308 * \param X Destination MPI
309 * \param A Left-hand MPI
310 * \param B Right-hand MPI
311 *
312 * \return 0 if successful,
313 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
314 */
315 int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
316
317 /**
318 * \brief Signed addition: X = A + B
319 *
320 * \param X Destination MPI
321 * \param A Left-hand MPI
322 * \param B Right-hand MPI
323 *
324 * \return 0 if successful,
325 * 1 if memory allocation failed
326 */
327 int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
328
329 /**
330 * \brief Signed substraction: X = A - B
331 *
332 * \param X Destination MPI
333 * \param A Left-hand MPI
334 * \param B Right-hand MPI
335 *
336 * \return 0 if successful,
337 * 1 if memory allocation failed
338 */
339 int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
340
341 /**
342 * \brief Signed addition: X = A + b
343 *
344 * \param X Destination MPI
345 * \param A Left-hand MPI
346 * \param b The integer value to add
347 *
348 * \return 0 if successful,
349 * 1 if memory allocation failed
350 */
351 int mpi_add_int( mpi *X, const mpi *A, int b );
352
353 /**
354 * \brief Signed substraction: X = A - b
355 *
356 * \param X Destination MPI
357 * \param A Left-hand MPI
358 * \param b The integer value to subtract
359 *
360 * \return 0 if successful,
361 * 1 if memory allocation failed
362 */
363 int mpi_sub_int( mpi *X, const mpi *A, int b );
364
365 /**
366 * \brief Baseline multiplication: X = A * B
367 *
368 * \param X Destination MPI
369 * \param A Left-hand MPI
370 * \param B Right-hand MPI
371 *
372 * \return 0 if successful,
373 * 1 if memory allocation failed
374 */
375 int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
376
377 /**
378 * \brief Baseline multiplication: X = A * b
379 * Note: b is an unsigned integer type, thus
380 * Negative values of b are ignored.
381 *
382 * \param X Destination MPI
383 * \param A Left-hand MPI
384 * \param b The integer value to multiply with
385 *
386 * \return 0 if successful,
387 * 1 if memory allocation failed
388 */
389 int mpi_mul_int( mpi *X, const mpi *A, t_int b );
390
391 /**
392 * \brief Division by mpi: A = Q * B + R
393 *
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
398 *
399 * \return 0 if successful,
400 * 1 if memory allocation failed,
401 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
402 *
403 * \note Either Q or R can be NULL.
404 */
405 int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
406
407 /**
408 * \brief Division by int: A = Q * b + R
409 *
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
414 *
415 * \return 0 if successful,
416 * 1 if memory allocation failed,
417 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
418 *
419 * \note Either Q or R can be NULL.
420 */
421 int mpi_div_int( mpi *Q, mpi *R, const mpi *A, int b );
422
423 /**
424 * \brief Modulo: R = A mod B
425 *
426 * \param R Destination MPI for the rest value
427 * \param A Left-hand MPI
428 * \param B Right-hand MPI
429 *
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
434 */
435 int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
436
437 /**
438 * \brief Modulo: r = A mod b
439 *
440 * \param r Destination t_int
441 * \param A Left-hand MPI
442 * \param b Integer to divide by
443 *
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
448 */
449 int mpi_mod_int( t_int *r, const mpi *A, int b );
450
451 /**
452 * \brief Sliding-window exponentiation: X = A^E mod N
453 *
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
459 *
460 * \return 0 if successful,
461 * 1 if memory allocation failed,
462 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
463 *
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.
467 */
468 int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
469
470 /**
471 * \brief Greatest common divisor: G = gcd(A, B)
472 *
473 * \param G Destination MPI
474 * \param A Left-hand MPI
475 * \param B Right-hand MPI
476 *
477 * \return 0 if successful,
478 * 1 if memory allocation failed
479 */
480 int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
481
482 /**
483 * \brief Modular inverse: X = A^-1 mod N
484 *
485 * \param X Destination MPI
486 * \param A Left-hand MPI
487 * \param N Right-hand MPI
488 *
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
493 */
494 int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
495
496 /**
497 * \brief Miller-Rabin primality test
498 *
499 * \param X MPI to check
500 * \param f_rng RNG function
501 * \param p_rng RNG parameter
502 *
503 * \return 0 if successful (probably prime),
504 * 1 if memory allocation failed,
505 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
506 */
507 int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
508
509 /**
510 * \brief Prime number generation
511 *
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
517 *
518 * \return 0 if successful (probably prime),
519 * 1 if memory allocation failed,
520 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
521 */
522 int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
523 int (*f_rng)(void *), void *p_rng );
524
525 #ifdef __cplusplus
526 }
527 #endif
528
529 #endif /* bignum.h */