Commit | Line | Data |
---|---|---|
80a47a2c TK |
1 | /** |
2 | * \file bignum.h | |
3 | * | |
62d3e98d TK |
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> | |
80a47a2c | 8 | * |
62d3e98d | 9 | * All rights reserved. |
80a47a2c TK |
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 | ||
67932e54 | 26 | /* $Cambridge: exim/src/src/pdkim/bignum.h,v 1.3 2009/12/07 13:05:07 tom Exp $ */ |
80a47a2c TK |
27 | |
28 | #ifndef POLARSSL_BIGNUM_H | |
29 | #define POLARSSL_BIGNUM_H | |
30 | ||
31 | #include <stdio.h> | |
32 | ||
67932e54 TK |
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 | |
80a47a2c TK |
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 | |
67932e54 TK |
63 | #if defined(POLARSSL_HAVE_LONGLONG) |
64 | typedef unsigned long long t_dbl; | |
65 | #endif | |
80a47a2c TK |
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 | * | |
67932e54 TK |
99 | * \param X MPI to grow |
100 | * \param nblimbs The target number of limbs | |
101 | * | |
80a47a2c TK |
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 | * | |
67932e54 TK |
110 | * \param X Destination MPI |
111 | * \param Y Source MPI | |
112 | * | |
80a47a2c TK |
113 | * \return 0 if successful, |
114 | * 1 if memory allocation failed | |
115 | */ | |
62d3e98d | 116 | int mpi_copy( mpi *X, const mpi *Y ); |
80a47a2c TK |
117 | |
118 | /** | |
119 | * \brief Swap the contents of X and Y | |
67932e54 TK |
120 | * |
121 | * \param X First MPI value | |
122 | * \param Y Second MPI value | |
80a47a2c TK |
123 | */ |
124 | void mpi_swap( mpi *X, mpi *Y ); | |
125 | ||
126 | /** | |
127 | * \brief Set value from integer | |
128 | * | |
67932e54 TK |
129 | * \param X MPI to set |
130 | * \param z Value to use | |
131 | * | |
80a47a2c TK |
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 | |
67932e54 TK |
139 | * |
140 | * \param X MPI to use | |
80a47a2c | 141 | */ |
62d3e98d | 142 | int mpi_lsb( const mpi *X ); |
80a47a2c TK |
143 | |
144 | /** | |
145 | * \brief Return the number of most significant bits | |
67932e54 TK |
146 | * |
147 | * \param X MPI to use | |
80a47a2c | 148 | */ |
62d3e98d | 149 | int mpi_msb( const mpi *X ); |
80a47a2c TK |
150 | |
151 | /** | |
152 | * \brief Return the total size in bytes | |
67932e54 TK |
153 | * |
154 | * \param X MPI to use | |
80a47a2c | 155 | */ |
62d3e98d | 156 | int mpi_size( const mpi *X ); |
80a47a2c TK |
157 | |
158 | /** | |
159 | * \brief Import from an ASCII string | |
160 | * | |
67932e54 TK |
161 | * \param X Destination MPI |
162 | * \param radix Input numeric base | |
163 | * \param s Null-terminated string buffer | |
80a47a2c TK |
164 | * |
165 | * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code | |
166 | */ | |
62d3e98d | 167 | int mpi_read_string( mpi *X, int radix, const char *s ); |
80a47a2c TK |
168 | |
169 | /** | |
170 | * \brief Export into an ASCII string | |
171 | * | |
67932e54 TK |
172 | * \param X Source MPI |
173 | * \param radix Output numeric base | |
174 | * \param s String buffer | |
175 | * \param slen String buffer size | |
80a47a2c | 176 | * |
62d3e98d TK |
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. | |
80a47a2c TK |
180 | * |
181 | * \note Call this function with *slen = 0 to obtain the | |
182 | * minimum required buffer size in *slen. | |
183 | */ | |
62d3e98d | 184 | int mpi_write_string( const mpi *X, int radix, char *s, int *slen ); |
80a47a2c TK |
185 | |
186 | /** | |
187 | * \brief Read X from an opened file | |
188 | * | |
67932e54 TK |
189 | * \param X Destination MPI |
190 | * \param radix Input numeric base | |
191 | * \param fin Input file handle | |
80a47a2c TK |
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 | /** | |
67932e54 | 198 | * \brief Write X into an opened file, or stdout if fout is NULL |
80a47a2c | 199 | * |
67932e54 TK |
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) | |
80a47a2c TK |
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 | */ | |
62d3e98d | 209 | int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout ); |
80a47a2c TK |
210 | |
211 | /** | |
212 | * \brief Import X from unsigned binary data, big endian | |
213 | * | |
67932e54 TK |
214 | * \param X Destination MPI |
215 | * \param buf Input buffer | |
216 | * \param buflen Input buffer size | |
80a47a2c TK |
217 | * |
218 | * \return 0 if successful, | |
219 | * 1 if memory allocation failed | |
220 | */ | |
62d3e98d | 221 | int mpi_read_binary( mpi *X, const unsigned char *buf, int buflen ); |
80a47a2c TK |
222 | |
223 | /** | |
224 | * \brief Export X into unsigned binary data, big endian | |
225 | * | |
67932e54 TK |
226 | * \param X Source MPI |
227 | * \param buf Output buffer | |
228 | * \param buflen Output buffer size | |
80a47a2c TK |
229 | * |
230 | * \return 0 if successful, | |
231 | * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough | |
80a47a2c | 232 | */ |
62d3e98d | 233 | int mpi_write_binary( const mpi *X, unsigned char *buf, int buflen ); |
80a47a2c TK |
234 | |
235 | /** | |
236 | * \brief Left-shift: X <<= count | |
237 | * | |
67932e54 TK |
238 | * \param X MPI to shift |
239 | * \param count Amount to shift | |
240 | * | |
80a47a2c TK |
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 | * | |
67932e54 TK |
249 | * \param X MPI to shift |
250 | * \param count Amount to shift | |
251 | * | |
80a47a2c TK |
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 | * | |
67932e54 TK |
260 | * \param X Left-hand MPI |
261 | * \param Y Right-hand MPI | |
262 | * | |
80a47a2c TK |
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 | */ | |
62d3e98d | 267 | int mpi_cmp_abs( const mpi *X, const mpi *Y ); |
80a47a2c TK |
268 | |
269 | /** | |
270 | * \brief Compare signed values | |
271 | * | |
67932e54 TK |
272 | * \param X Left-hand MPI |
273 | * \param Y Right-hand MPI | |
274 | * | |
80a47a2c TK |
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 | */ | |
62d3e98d | 279 | int mpi_cmp_mpi( const mpi *X, const mpi *Y ); |
80a47a2c TK |
280 | |
281 | /** | |
282 | * \brief Compare signed values | |
283 | * | |
67932e54 TK |
284 | * \param X Left-hand MPI |
285 | * \param z The integer value to compare to | |
286 | * | |
80a47a2c TK |
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 | */ | |
62d3e98d | 291 | int mpi_cmp_int( const mpi *X, int z ); |
80a47a2c TK |
292 | |
293 | /** | |
294 | * \brief Unsigned addition: X = |A| + |B| | |
295 | * | |
67932e54 TK |
296 | * \param X Destination MPI |
297 | * \param A Left-hand MPI | |
298 | * \param B Right-hand MPI | |
299 | * | |
80a47a2c TK |
300 | * \return 0 if successful, |
301 | * 1 if memory allocation failed | |
302 | */ | |
62d3e98d | 303 | int mpi_add_abs( mpi *X, const mpi *A, const mpi *B ); |
80a47a2c TK |
304 | |
305 | /** | |
306 | * \brief Unsigned substraction: X = |A| - |B| | |
307 | * | |
67932e54 TK |
308 | * \param X Destination MPI |
309 | * \param A Left-hand MPI | |
310 | * \param B Right-hand MPI | |
311 | * | |
80a47a2c TK |
312 | * \return 0 if successful, |
313 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A | |
314 | */ | |
62d3e98d | 315 | int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B ); |
80a47a2c TK |
316 | |
317 | /** | |
318 | * \brief Signed addition: X = A + B | |
319 | * | |
67932e54 TK |
320 | * \param X Destination MPI |
321 | * \param A Left-hand MPI | |
322 | * \param B Right-hand MPI | |
323 | * | |
80a47a2c TK |
324 | * \return 0 if successful, |
325 | * 1 if memory allocation failed | |
326 | */ | |
62d3e98d | 327 | int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B ); |
80a47a2c TK |
328 | |
329 | /** | |
330 | * \brief Signed substraction: X = A - B | |
331 | * | |
67932e54 TK |
332 | * \param X Destination MPI |
333 | * \param A Left-hand MPI | |
334 | * \param B Right-hand MPI | |
335 | * | |
80a47a2c TK |
336 | * \return 0 if successful, |
337 | * 1 if memory allocation failed | |
338 | */ | |
62d3e98d | 339 | int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B ); |
80a47a2c TK |
340 | |
341 | /** | |
342 | * \brief Signed addition: X = A + b | |
343 | * | |
67932e54 TK |
344 | * \param X Destination MPI |
345 | * \param A Left-hand MPI | |
346 | * \param b The integer value to add | |
347 | * | |
80a47a2c TK |
348 | * \return 0 if successful, |
349 | * 1 if memory allocation failed | |
350 | */ | |
62d3e98d | 351 | int mpi_add_int( mpi *X, const mpi *A, int b ); |
80a47a2c TK |
352 | |
353 | /** | |
354 | * \brief Signed substraction: X = A - b | |
355 | * | |
67932e54 TK |
356 | * \param X Destination MPI |
357 | * \param A Left-hand MPI | |
358 | * \param b The integer value to subtract | |
359 | * | |
80a47a2c TK |
360 | * \return 0 if successful, |
361 | * 1 if memory allocation failed | |
362 | */ | |
62d3e98d | 363 | int mpi_sub_int( mpi *X, const mpi *A, int b ); |
80a47a2c TK |
364 | |
365 | /** | |
366 | * \brief Baseline multiplication: X = A * B | |
367 | * | |
67932e54 TK |
368 | * \param X Destination MPI |
369 | * \param A Left-hand MPI | |
370 | * \param B Right-hand MPI | |
371 | * | |
80a47a2c TK |
372 | * \return 0 if successful, |
373 | * 1 if memory allocation failed | |
374 | */ | |
62d3e98d | 375 | int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B ); |
80a47a2c TK |
376 | |
377 | /** | |
378 | * \brief Baseline multiplication: X = A * b | |
67932e54 TK |
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 | |
80a47a2c TK |
385 | * |
386 | * \return 0 if successful, | |
387 | * 1 if memory allocation failed | |
388 | */ | |
62d3e98d | 389 | int mpi_mul_int( mpi *X, const mpi *A, t_int b ); |
80a47a2c TK |
390 | |
391 | /** | |
392 | * \brief Division by mpi: A = Q * B + R | |
393 | * | |
67932e54 TK |
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 | * | |
80a47a2c TK |
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 | */ | |
62d3e98d | 405 | int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B ); |
80a47a2c TK |
406 | |
407 | /** | |
408 | * \brief Division by int: A = Q * b + R | |
409 | * | |
67932e54 TK |
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 | * | |
80a47a2c TK |
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 | */ | |
62d3e98d | 421 | int mpi_div_int( mpi *Q, mpi *R, const mpi *A, int b ); |
80a47a2c TK |
422 | |
423 | /** | |
424 | * \brief Modulo: R = A mod B | |
425 | * | |
67932e54 TK |
426 | * \param R Destination MPI for the rest value |
427 | * \param A Left-hand MPI | |
428 | * \param B Right-hand MPI | |
429 | * | |
80a47a2c TK |
430 | * \return 0 if successful, |
431 | * 1 if memory allocation failed, | |
67932e54 TK |
432 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0, |
433 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0 | |
80a47a2c | 434 | */ |
62d3e98d | 435 | int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B ); |
80a47a2c TK |
436 | |
437 | /** | |
438 | * \brief Modulo: r = A mod b | |
439 | * | |
62d3e98d | 440 | * \param r Destination t_int |
67932e54 TK |
441 | * \param A Left-hand MPI |
442 | * \param b Integer to divide by | |
443 | * | |
80a47a2c TK |
444 | * \return 0 if successful, |
445 | * 1 if memory allocation failed, | |
67932e54 TK |
446 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0, |
447 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0 | |
80a47a2c | 448 | */ |
62d3e98d | 449 | int mpi_mod_int( t_int *r, const mpi *A, int b ); |
80a47a2c TK |
450 | |
451 | /** | |
452 | * \brief Sliding-window exponentiation: X = A^E mod N | |
453 | * | |
67932e54 TK |
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 | * | |
80a47a2c TK |
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 | */ | |
62d3e98d | 468 | int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ); |
80a47a2c TK |
469 | |
470 | /** | |
471 | * \brief Greatest common divisor: G = gcd(A, B) | |
472 | * | |
67932e54 TK |
473 | * \param G Destination MPI |
474 | * \param A Left-hand MPI | |
475 | * \param B Right-hand MPI | |
476 | * | |
80a47a2c TK |
477 | * \return 0 if successful, |
478 | * 1 if memory allocation failed | |
479 | */ | |
62d3e98d | 480 | int mpi_gcd( mpi *G, const mpi *A, const mpi *B ); |
80a47a2c TK |
481 | |
482 | /** | |
483 | * \brief Modular inverse: X = A^-1 mod N | |
484 | * | |
67932e54 TK |
485 | * \param X Destination MPI |
486 | * \param A Left-hand MPI | |
487 | * \param N Right-hand MPI | |
488 | * | |
80a47a2c TK |
489 | * \return 0 if successful, |
490 | * 1 if memory allocation failed, | |
491 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil | |
67932e54 | 492 | POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N |
80a47a2c | 493 | */ |
62d3e98d | 494 | int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N ); |
80a47a2c | 495 | |
67932e54 TK |
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 | ||
80a47a2c TK |
525 | #ifdef __cplusplus |
526 | } | |
527 | #endif | |
528 | ||
529 | #endif /* bignum.h */ |