OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / pluto / gcryptfix.h
1 /* Definitions to make gcrypt routines feel at home in Pluto.
2  * Copyright (C) 1999  D. Hugh Redelmeier.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * RCSID $Id: gcryptfix.h,v 1.4 2000/05/20 19:43:55 dhr Exp $
15  */
16
17 #define DBG_CIPHER  1   /* some day we'll do this right */
18
19 /* Simulate MPI routines with gmp routines.
20  * gmp's MP_INT is a stuct; MPI's MPI is a pointer to an analogous struct.
21  * gmp's mpz_t is an array of one of these structs to enable magic pointer
22  * conversions to make the notation convenient (but confusing).
23  */
24 typedef u_char byte;
25 typedef MP_INT *MPI;
26
27 #define BITS_PER_MPI_LIMB mp_bits_per_limb
28
29 extern MPI mpi_alloc( unsigned nlimbs );
30 extern MPI mpi_alloc_secure( unsigned nlimbs );
31 #define mpi_alloc_like(n) mpi_alloc(mpi_get_nlimbs(n))
32 extern MPI mpi_alloc_set_ui( unsigned long u);
33 #define mpi_set_ui(w, u) mpz_set_ui(w, u)
34 #define mpi_set(w, u) mpz_set(w, u)
35 extern void mpi_free( MPI a );
36 extern MPI  mpi_copy( MPI a );
37 extern unsigned mpi_get_nbits( MPI a );
38 #define mpi_get_nlimbs(a)     ((a)->_mp_alloc)  /* dirty, but useless */
39 extern void  mpi_set_buffer( MPI a, const u_char *buffer, unsigned nbytes, int sign );
40 extern unsigned mpi_trailing_zeros( MPI a );
41 extern int  mpi_test_bit( MPI a, unsigned n );
42 extern void mpi_set_bit( MPI a, unsigned n );
43 extern void mpi_clear_bit( MPI a, unsigned n );
44 extern void mpi_clear_highbit( MPI a, unsigned n );
45 extern void mpi_set_highbit( MPI a, unsigned n );
46 #define mpi_cmp_ui(u, v) mpz_cmp_ui((u), (v))
47 #define mpi_cmp(u, v) mpz_cmp((u), (v))
48 #define mpi_is_neg(n) (mpz_sgn(n) < 0)
49 #define mpi_add(w, u, v) mpz_add((w), (u), (v))
50 #define mpi_add_ui(w, u, v) mpz_add_ui((w), (u), (v))
51 #define mpi_sub_ui(w, u, v) mpz_sub_ui((w), (u), (v))
52 #define mpi_subm( w, u, v, m) { mpz_sub( (w), (u), (v)) ; mpz_fdiv_r((w), (w), (m)); }
53 #define mpi_mul( w, u, v) mpz_mul( (w), (u), (v))
54 #define mpi_mul_ui( w, u, v) mpz_mul_ui( (w), (u), (v))
55 #define mpi_mulm( w, u, v, m) { mpz_mul( (w), (u), (v)) ; mpz_fdiv_r((w), (w), (m)); }
56 #define mpi_fdiv_q(quot, dividend, divisor) mpz_fdiv_q((quot), (dividend), (divisor))
57 #define mpi_fdiv_r( rem, dividend, divisor ) mpz_fdiv_r( (rem), (dividend), (divisor) )
58 #define mpi_fdiv_r_ui( rem, dividend, divisor )  mpz_fdiv_r_ui( (rem), (dividend), (divisor) )
59 #define mpi_tdiv_q_2exp( w, u, count ) mpz_tdiv_q_2exp( (w), (u), (count) )
60 extern int   mpi_divisible_ui(MPI dividend, ulong divisor );
61 #define mpi_powm( res, base, exp, mod) mpz_powm( res, base, exp, mod)
62 extern void mpi_mulpowm( MPI res, MPI *basearray, MPI *exparray, MPI mod);
63 #define mpi_gcd( g, a, b ) ( mpz_gcd( (g), (a), (b) ), !mpi_cmp_ui( (g), 1))
64 #define mpi_invm( x, a, n ) mpz_invert( (x), (a), (n) )
65
66 #ifdef DEBUG
67 # define log_debug(f...)  DBG_log(f)
68 #else
69 # define log_debug(f...)  do ; while (0)        /* do nothing, carefully */
70 #endif
71 #define log_fatal(f...)  exit_log(f)    /* overreaction? */
72 extern void log_mpidump( const char *text, MPI a );
73
74 #define assert(p) passert(p)
75 #define BUG() passert(FALSE)
76
77 #define m_alloc_ptrs_clear(pp, n) { \
78         int c = (n); \
79         (pp) = alloc_bytes((n) * sizeof(*(pp)), "m_alloc_ptrs_clear"); \
80         while (c > 0) (pp)[--c] = NULL; \
81     }
82
83 extern u_char *get_random_bits(size_t nbits, int level, int secure);
84 #define m_alloc(sz) alloc_bytes((sz), "m_alloc")        /* not initialized */
85 #define m_free(n) pfree(n)  /* always freeing something from get_random_bits */
86
87 /* declarations from gnupg-1.0.0/include/cipher.h */
88 /*-- primegen.c --*/
89 MPI generate_secret_prime( unsigned nbits );
90 MPI generate_public_prime( unsigned nbits );
91 MPI generate_elg_prime( int mode, unsigned pbits, unsigned qbits,
92                                            MPI g, MPI **factors );
93
94 #define PUBKEY_ALGO_ELGAMAL_E 16     /* encrypt only ElGamal (but not for v3)*/
95 #define PUBKEY_ALGO_DSA       17
96 #define PUBKEY_ALGO_ELGAMAL   20     /* sign and encrypt elgamal */
97
98 #define is_ELGAMAL(a) ((a)==PUBKEY_ALGO_ELGAMAL || (a)==PUBKEY_ALGO_ELGAMAL_E)
99
100 #define PUBKEY_USAGE_SIG     1      /* key is good for signatures */
101 #define PUBKEY_USAGE_ENC     2      /* key is good for encryption */
102
103 /* from gnupg-1.0.0/include/errors.h */
104
105 #define G10ERR_PUBKEY_ALGO     4 /* Unknown pubkey algorithm */
106 #define G10ERR_BAD_SECKEY      7 /* Bad secret key */
107 #define G10ERR_BAD_SIGN        8 /* Bad signature */
108 #define G10ERR_BAD_MPI        30
109
110 /*-- smallprime.c --*/
111 extern ushort small_prime_numbers[];