X-Git-Url: https://git.exim.org/exim.git/blobdiff_plain/2f1c4d4cc07565389135d9ecaa8390b46d043d86..67932e542eb65f681779f5d49974afe8369a9b9a:/src/src/pdkim/bignum.h diff --git a/src/src/pdkim/bignum.h b/src/src/pdkim/bignum.h index 28e746d4d..aaca67ff8 100644 --- a/src/src/pdkim/bignum.h +++ b/src/src/pdkim/bignum.h @@ -1,9 +1,10 @@ /** * \file bignum.h * - * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine + * Copyright (C) 2006-2009, Paul Bakker + * All rights reserved. * - * Copyright (C) 2009 Paul Bakker + * Joined copyright on original XySSL code with: Christophe Devine * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,20 +21,20 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -/* $Cambridge: exim/src/src/pdkim/bignum.h,v 1.2 2009/06/10 07:34:05 tom Exp $ */ +/* $Cambridge: exim/src/src/pdkim/bignum.h,v 1.3 2009/12/07 13:05:07 tom Exp $ */ #ifndef POLARSSL_BIGNUM_H #define POLARSSL_BIGNUM_H #include -#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 -#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 -#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 -#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 -#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A -#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C -#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E +#define POLARSSL_ERR_MPI_FILE_IO_ERROR 0x0002 +#define POLARSSL_ERR_MPI_BAD_INPUT_DATA 0x0004 +#define POLARSSL_ERR_MPI_INVALID_CHARACTER 0x0006 +#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL 0x0008 +#define POLARSSL_ERR_MPI_NEGATIVE_VALUE 0x000A +#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO 0x000C +#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE 0x000E #define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup @@ -57,7 +58,9 @@ typedef unsigned long t_dbl; defined(__ia64__) || defined(__alpha__) typedef unsigned int t_dbl __attribute__((mode(TI))); #else - typedef unsigned long long t_dbl; + #if defined(POLARSSL_HAVE_LONGLONG) + typedef unsigned long long t_dbl; + #endif #endif #endif #endif @@ -91,6 +94,9 @@ void mpi_free( mpi *X, ... ); /** * \brief Enlarge to the specified number of limbs * + * \param X MPI to grow + * \param nblimbs The target number of limbs + * * \return 0 if successful, * 1 if memory allocation failed */ @@ -99,6 +105,9 @@ int mpi_grow( mpi *X, int nblimbs ); /** * \brief Copy the contents of Y into X * + * \param X Destination MPI + * \param Y Source MPI + * * \return 0 if successful, * 1 if memory allocation failed */ @@ -106,12 +115,18 @@ int mpi_copy( mpi *X, mpi *Y ); /** * \brief Swap the contents of X and Y + * + * \param X First MPI value + * \param Y Second MPI value */ void mpi_swap( mpi *X, mpi *Y ); /** * \brief Set value from integer * + * \param X MPI to set + * \param z Value to use + * * \return 0 if successful, * 1 if memory allocation failed */ @@ -119,25 +134,31 @@ int mpi_lset( mpi *X, int z ); /** * \brief Return the number of least significant bits + * + * \param X MPI to use */ int mpi_lsb( mpi *X ); /** * \brief Return the number of most significant bits + * + * \param X MPI to use */ int mpi_msb( mpi *X ); /** * \brief Return the total size in bytes + * + * \param X MPI to use */ int mpi_size( mpi *X ); /** * \brief Import from an ASCII string * - * \param X destination mpi - * \param radix input numeric base - * \param s null-terminated string buffer + * \param X Destination MPI + * \param radix Input numeric base + * \param s Null-terminated string buffer * * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code */ @@ -146,10 +167,10 @@ int mpi_read_string( mpi *X, int radix, char *s ); /** * \brief Export into an ASCII string * - * \param X source mpi - * \param radix output numeric base - * \param s string buffer - * \param slen string buffer size + * \param X Source MPI + * \param radix Output numeric base + * \param s String buffer + * \param slen String buffer size * * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code * @@ -161,21 +182,21 @@ int mpi_write_string( mpi *X, int radix, char *s, int *slen ); /** * \brief Read X from an opened file * - * \param X destination mpi - * \param radix input numeric base - * \param fin input file handle + * \param X Destination MPI + * \param radix Input numeric base + * \param fin Input file handle * * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code */ int mpi_read_file( mpi *X, int radix, FILE *fin ); /** - * \brief Write X into an opened file, or stdout + * \brief Write X into an opened file, or stdout if fout is NULL * - * \param p prefix, can be NULL - * \param X source mpi - * \param radix output numeric base - * \param fout output file handle + * \param p Prefix, can be NULL + * \param X Source MPI + * \param radix Output numeric base + * \param fout Output file handle (can be NULL) * * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code * @@ -186,9 +207,9 @@ int mpi_write_file( char *p, mpi *X, int radix, FILE *fout ); /** * \brief Import X from unsigned binary data, big endian * - * \param X destination mpi - * \param buf input buffer - * \param buflen input buffer size + * \param X Destination MPI + * \param buf Input buffer + * \param buflen Input buffer size * * \return 0 if successful, * 1 if memory allocation failed @@ -198,21 +219,21 @@ int mpi_read_binary( mpi *X, unsigned char *buf, int buflen ); /** * \brief Export X into unsigned binary data, big endian * - * \param X source mpi - * \param buf output buffer - * \param buflen output buffer size + * \param X Source MPI + * \param buf Output buffer + * \param buflen Output buffer size * * \return 0 if successful, * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough - * - * \note Call this function with *buflen = 0 to obtain the - * minimum required buffer size in *buflen. */ int mpi_write_binary( mpi *X, unsigned char *buf, int buflen ); /** * \brief Left-shift: X <<= count * + * \param X MPI to shift + * \param count Amount to shift + * * \return 0 if successful, * 1 if memory allocation failed */ @@ -221,6 +242,9 @@ int mpi_shift_l( mpi *X, int count ); /** * \brief Right-shift: X >>= count * + * \param X MPI to shift + * \param count Amount to shift + * * \return 0 if successful, * 1 if memory allocation failed */ @@ -229,6 +253,9 @@ int mpi_shift_r( mpi *X, int count ); /** * \brief Compare unsigned values * + * \param X Left-hand MPI + * \param Y Right-hand MPI + * * \return 1 if |X| is greater than |Y|, * -1 if |X| is lesser than |Y| or * 0 if |X| is equal to |Y| @@ -238,6 +265,9 @@ int mpi_cmp_abs( mpi *X, mpi *Y ); /** * \brief Compare signed values * + * \param X Left-hand MPI + * \param Y Right-hand MPI + * * \return 1 if X is greater than Y, * -1 if X is lesser than Y or * 0 if X is equal to Y @@ -247,6 +277,9 @@ int mpi_cmp_mpi( mpi *X, mpi *Y ); /** * \brief Compare signed values * + * \param X Left-hand MPI + * \param z The integer value to compare to + * * \return 1 if X is greater than z, * -1 if X is lesser than z or * 0 if X is equal to z @@ -256,6 +289,10 @@ int mpi_cmp_int( mpi *X, int z ); /** * \brief Unsigned addition: X = |A| + |B| * + * \param X Destination MPI + * \param A Left-hand MPI + * \param B Right-hand MPI + * * \return 0 if successful, * 1 if memory allocation failed */ @@ -264,6 +301,10 @@ int mpi_add_abs( mpi *X, mpi *A, mpi *B ); /** * \brief Unsigned substraction: X = |A| - |B| * + * \param X Destination MPI + * \param A Left-hand MPI + * \param B Right-hand MPI + * * \return 0 if successful, * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A */ @@ -272,6 +313,10 @@ int mpi_sub_abs( mpi *X, mpi *A, mpi *B ); /** * \brief Signed addition: X = A + B * + * \param X Destination MPI + * \param A Left-hand MPI + * \param B Right-hand MPI + * * \return 0 if successful, * 1 if memory allocation failed */ @@ -280,6 +325,10 @@ int mpi_add_mpi( mpi *X, mpi *A, mpi *B ); /** * \brief Signed substraction: X = A - B * + * \param X Destination MPI + * \param A Left-hand MPI + * \param B Right-hand MPI + * * \return 0 if successful, * 1 if memory allocation failed */ @@ -288,6 +337,10 @@ int mpi_sub_mpi( mpi *X, mpi *A, mpi *B ); /** * \brief Signed addition: X = A + b * + * \param X Destination MPI + * \param A Left-hand MPI + * \param b The integer value to add + * * \return 0 if successful, * 1 if memory allocation failed */ @@ -296,6 +349,10 @@ int mpi_add_int( mpi *X, mpi *A, int b ); /** * \brief Signed substraction: X = A - b * + * \param X Destination MPI + * \param A Left-hand MPI + * \param b The integer value to subtract + * * \return 0 if successful, * 1 if memory allocation failed */ @@ -304,6 +361,10 @@ int mpi_sub_int( mpi *X, mpi *A, int b ); /** * \brief Baseline multiplication: X = A * B * + * \param X Destination MPI + * \param A Left-hand MPI + * \param B Right-hand MPI + * * \return 0 if successful, * 1 if memory allocation failed */ @@ -311,6 +372,12 @@ int mpi_mul_mpi( mpi *X, mpi *A, mpi *B ); /** * \brief Baseline multiplication: X = A * b + * Note: b is an unsigned integer type, thus + * Negative values of b are ignored. + * + * \param X Destination MPI + * \param A Left-hand MPI + * \param b The integer value to multiply with * * \return 0 if successful, * 1 if memory allocation failed @@ -320,6 +387,11 @@ int mpi_mul_int( mpi *X, mpi *A, t_int b ); /** * \brief Division by mpi: A = Q * B + R * + * \param Q Destination MPI for the quotient + * \param R Destination MPI for the rest value + * \param A Left-hand MPI + * \param B Right-hand MPI + * * \return 0 if successful, * 1 if memory allocation failed, * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0 @@ -331,6 +403,11 @@ int mpi_div_mpi( mpi *Q, mpi *R, mpi *A, mpi *B ); /** * \brief Division by int: A = Q * b + R * + * \param Q Destination MPI for the quotient + * \param R Destination MPI for the rest value + * \param A Left-hand MPI + * \param b Integer to divide by + * * \return 0 if successful, * 1 if memory allocation failed, * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0 @@ -342,24 +419,40 @@ int mpi_div_int( mpi *Q, mpi *R, mpi *A, int b ); /** * \brief Modulo: R = A mod B * + * \param R Destination MPI for the rest value + * \param A Left-hand MPI + * \param B Right-hand MPI + * * \return 0 if successful, * 1 if memory allocation failed, - * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0 + * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0, + * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0 */ int mpi_mod_mpi( mpi *R, mpi *A, mpi *B ); /** * \brief Modulo: r = A mod b * + * \param a Destination t_int + * \param A Left-hand MPI + * \param b Integer to divide by + * * \return 0 if successful, * 1 if memory allocation failed, - * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0 + * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0, + * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0 */ int mpi_mod_int( t_int *r, mpi *A, int b ); /** * \brief Sliding-window exponentiation: X = A^E mod N * + * \param X Destination MPI + * \param A Left-hand MPI + * \param E Exponent MPI + * \param N Modular MPI + * \param _RR Speed-up MPI used for recalculations + * * \return 0 if successful, * 1 if memory allocation failed, * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even @@ -373,6 +466,10 @@ int mpi_exp_mod( mpi *X, mpi *A, mpi *E, mpi *N, mpi *_RR ); /** * \brief Greatest common divisor: G = gcd(A, B) * + * \param G Destination MPI + * \param A Left-hand MPI + * \param B Right-hand MPI + * * \return 0 if successful, * 1 if memory allocation failed */ @@ -381,13 +478,46 @@ int mpi_gcd( mpi *G, mpi *A, mpi *B ); /** * \brief Modular inverse: X = A^-1 mod N * + * \param X Destination MPI + * \param A Left-hand MPI + * \param N Right-hand MPI + * * \return 0 if successful, * 1 if memory allocation failed, * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil - * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N + POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N */ int mpi_inv_mod( mpi *X, mpi *A, mpi *N ); +/** + * \brief Miller-Rabin primality test + * + * \param X MPI to check + * \param f_rng RNG function + * \param p_rng RNG parameter + * + * \return 0 if successful (probably prime), + * 1 if memory allocation failed, + * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime + */ +int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng ); + +/** + * \brief Prime number generation + * + * \param X Destination MPI + * \param nbits Required size of X in bits + * \param dh_flag If 1, then (X-1)/2 will be prime too + * \param f_rng RNG function + * \param p_rng RNG parameter + * + * \return 0 if successful (probably prime), + * 1 if memory allocation failed, + * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3 + */ +int mpi_gen_prime( mpi *X, int nbits, int dh_flag, + int (*f_rng)(void *), void *p_rng ); + #ifdef __cplusplus } #endif