2 * Copyright (C) 1998 Free Software Foundation, Inc.
4 * This file is part of GNUPG.
6 * GNUPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GNUPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
23 #define DBG_CIPHER g10c_debug_mode
26 #include "../cipher/random.h"
29 #define CIPHER_ALGO_NONE 0
30 #define CIPHER_ALGO_IDEA 1
31 #define CIPHER_ALGO_3DES 2
32 #define CIPHER_ALGO_CAST5 3
33 #define CIPHER_ALGO_BLOWFISH 4 /* blowfish 128 bit key */
34 #define CIPHER_ALGO_SAFER_SK128 5
35 #define CIPHER_ALGO_DES_SK 6
36 #define CIPHER_ALGO_TWOFISH 10 /* twofish 256 bit */
37 #define CIPHER_ALGO_BLOWFISH160 42 /* blowfish 160 bit key (not in OpenPGP)*/
38 #define CIPHER_ALGO_SKIPJACK 101 /* experimental: skipjack */
39 #define CIPHER_ALGO_TWOFISH_OLD 102 /* experimental: twofish 128 bit */
40 #define CIPHER_ALGO_DUMMY 110 /* no encryption at all */
42 #define PUBKEY_ALGO_RSA 1
43 #define PUBKEY_ALGO_RSA_E 2 /* RSA encrypt only */
44 #define PUBKEY_ALGO_RSA_S 3 /* RSA sign only */
45 #define PUBKEY_ALGO_ELGAMAL_E 16 /* encrypt only ElGamal (but not vor v3)*/
46 #define PUBKEY_ALGO_DSA 17
47 #define PUBKEY_ALGO_ELGAMAL 20 /* sign and encrypt elgamal */
49 #define PUBKEY_USAGE_SIG 1 /* key is good for signatures */
50 #define PUBKEY_USAGE_ENC 2 /* key is good for encryption */
52 #define DIGEST_ALGO_MD5 1
53 #define DIGEST_ALGO_SHA1 2
54 #define DIGEST_ALGO_RMD160 3
55 #define DIGEST_ALGO_TIGER 6
57 #define is_RSA(a) ((a)==PUBKEY_ALGO_RSA || (a)==PUBKEY_ALGO_RSA_E \
58 || (a)==PUBKEY_ALGO_RSA_S )
59 #define is_ELGAMAL(a) ((a)==PUBKEY_ALGO_ELGAMAL || (a)==PUBKEY_ALGO_ELGAMAL_E)
64 byte key[32]; /* this is the largest used keylen (256 bit) */
67 struct cipher_handle_s;
68 typedef struct cipher_handle_s *CIPHER_HANDLE;
71 #define CIPHER_MODE_ECB 1
72 #define CIPHER_MODE_CFB 2
73 #define CIPHER_MODE_PHILS_CFB 3
74 #define CIPHER_MODE_AUTO_CFB 4
75 #define CIPHER_MODE_DUMMY 5 /* used with algo DUMMY for no encryption */
77 struct md_digest_list_s;
82 struct md_digest_list_s *list;
91 const char *g10_opt_homedir;
94 void register_cipher_extension( const char *mainpgm, const char *fname );
97 int string_to_digest_algo( const char *string );
98 const char * digest_algo_to_string( int algo );
99 int check_digest_algo( int algo );
100 MD_HANDLE md_open( int algo, int secure );
101 void md_enable( MD_HANDLE hd, int algo );
102 MD_HANDLE md_copy( MD_HANDLE a );
103 void md_reset( MD_HANDLE a );
104 void md_close(MD_HANDLE a);
105 void md_write( MD_HANDLE a, byte *inbuf, size_t inlen);
106 void md_final(MD_HANDLE a);
107 byte *md_read( MD_HANDLE a, int algo );
108 int md_digest( MD_HANDLE a, int algo, byte *buffer, int buflen );
109 int md_get_algo( MD_HANDLE a );
110 int md_digest_length( int algo );
111 const byte *md_asn_oid( int algo, size_t *asnlen, size_t *mdlen );
112 void md_start_debug( MD_HANDLE a, const char *suffix );
113 void md_stop_debug( MD_HANDLE a );
114 #define md_is_secure(a) ((a)->secure)
115 #define md_putc(h,c) \
117 if( (h)->bufcount == (h)->bufsize ) \
118 md_write( (h), NULL, 0 ); \
119 (h)->buffer[(h)->bufcount++] = (c) & 0xff; \
122 void rmd160_hash_buffer( char *outbuf, const char *buffer, size_t length );
126 int string_to_cipher_algo( const char *string );
127 const char * cipher_algo_to_string( int algo );
128 int check_cipher_algo( int algo );
129 unsigned cipher_get_keylen( int algo );
130 unsigned cipher_get_blocksize( int algo );
131 CIPHER_HANDLE cipher_open( int algo, int mode, int secure );
132 void cipher_close( CIPHER_HANDLE c );
133 int cipher_setkey( CIPHER_HANDLE c, byte *key, unsigned keylen );
134 void cipher_setiv( CIPHER_HANDLE c, const byte *iv );
135 void cipher_encrypt( CIPHER_HANDLE c, byte *out, byte *in, unsigned nbytes );
136 void cipher_decrypt( CIPHER_HANDLE c, byte *out, byte *in, unsigned nbytes );
137 void cipher_sync( CIPHER_HANDLE c );
140 #define PUBKEY_MAX_NPKEY 4
141 #define PUBKEY_MAX_NSKEY 6
142 #define PUBKEY_MAX_NSIG 2
143 #define PUBKEY_MAX_NENC 2
145 int string_to_pubkey_algo( const char *string );
146 const char * pubkey_algo_to_string( int algo );
147 int check_pubkey_algo( int algo );
148 int check_pubkey_algo2( int algo, unsigned use );
149 int pubkey_get_npkey( int algo );
150 int pubkey_get_nskey( int algo );
151 int pubkey_get_nsig( int algo );
152 int pubkey_get_nenc( int algo );
153 unsigned pubkey_nbits( int algo, MPI *pkey );
154 int pubkey_generate( int algo, unsigned nbits, MPI *skey, MPI **retfactors );
155 int pubkey_check_secret_key( int algo, MPI *skey );
156 int pubkey_encrypt( int algo, MPI *resarr, MPI data, MPI *pkey );
157 int pubkey_decrypt( int algo, MPI *result, MPI *data, MPI *skey );
158 int pubkey_sign( int algo, MPI *resarr, MPI hash, MPI *skey );
159 int pubkey_verify( int algo, MPI hash, MPI *data, MPI *pkey,
160 int (*cmp)(void *, MPI), void *opaque );
162 /*-- smallprime.c --*/
163 extern ushort small_prime_numbers[];
166 MPI generate_secret_prime( unsigned nbits );
167 MPI generate_public_prime( unsigned nbits );
168 MPI generate_elg_prime( int mode, unsigned pbits, unsigned qbits,
169 MPI g, MPI **factors );
172 #endif /*G10_CIPHER_H*/