1 /*  libGringotts - generic data encoding (crypto+compression) library
2  *  (c) 2002, Germano Rizzo <mano@pluto.linux.it>
3  *
4  *  libgringotts.h - general header file for libgringotts
5  *  Author: Germano Rizzo
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Library General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 #ifndef LIBGRG_H
22 #define LIBGRG_H
23 
24 #include <sys/types.h>
25 
26 // if you feel a wee bit confused please
27 // read the manual, tipically found at
28 // /usr/share/doc/libgringotts-<version>/manual.htm
29 
30 // TYPEDEFS & ENUMERATIONS
31 
32 //encryption algorithms
33 typedef enum
34 {
35 	GRG_RIJNDAEL_128 = 0x00,	//00000000
36 	GRG_AES = 0x00,		//alias for GRG_RIJNDAEL_128
37 	GRG_SERPENT = 0x10,	//00010000 (default)
38 	GRG_TWOFISH = 0x20,	//00100000
39 	GRG_CAST_256 = 0x30,	//00110000
40 	GRG_SAFERPLUS = 0x40,	//01000000
41 	GRG_LOKI97 = 0x50,	//01010000
42 	GRG_3DES = 0x60,	//01100000
43 	GRG_RIJNDAEL_256 = 0x70	//01110000
44 }
45 grg_crypt_algo;
46 
47 //hashing algorithms
48 typedef enum
49 {
50 	GRG_SHA1 = 0x00,	//00000000
51 	GRG_RIPEMD_160 = 0x08	//00001000 (default)
52 }
53 grg_hash_algo;
54 
55 //compression algorithm
56 typedef enum
57 {
58 	GRG_ZLIB = 0x00,	//00000000 (default)
59 	GRG_BZIP = 0x04		//00000100
60 }
61 grg_comp_algo;
62 
63 //compression level
64 typedef enum
65 {
66 	GRG_LVL_NONE = 0x00,	//00000000
67 	GRG_LVL_FAST = 0x01,	//00000001
68 	GRG_LVL_GOOD = 0x02,	//00000010
69 	GRG_LVL_BEST = 0x03	//00000011 (default)
70 }
71 grg_comp_ratio;
72 
73 //security level
74 typedef enum
75 {
76 	GRG_SEC_NORMAL,		//default
77 	GRG_SEC_PARANOIA
78 }
79 grg_security_lvl;
80 
81 // ERROR CODES
82 
83 //I/O Ok
84 #define GRG_OK							0
85 
86 //I/O Errors
87 //error codes in writing
88 #define GRG_WRITE_COMP_ERR				-2
89 #define GRG_WRITE_ENC_INIT_ERR			-4
90 #define GRG_WRITE_FILE_ERR				-6
91 //unused since 1.2.1 (don't use!)		-8
92 #define GRG_TMP_NOT_WRITEABLE			-10
93 
94 //error codes in reading
95 #define GRG_READ_FILE_ERR				-1
96 #define GRG_READ_MMAP_ERR				-19
97 #define GRG_READ_MAGIC_ERR				-3
98 #define GRG_READ_CRC_ERR				-5
99 #define GRG_READ_PWD_ERR				-7
100 #define GRG_READ_ENC_INIT_ERR			-9
101 //unused since 1.2.1 (don't use!)		-11
102 #define GRG_READ_UNSUPPORTED_VERSION	-13
103 #define GRG_READ_COMP_ERR				-15
104 #define GRG_TMP_NOT_YET_WRITTEN			-17
105 
106 //error codes in file shredding
107 #define	GRG_SHRED_CANT_OPEN_FILE		-51
108 #define GRG_SHRED_YET_LINKED			-52
109 #define GRG_SHRED_CANT_MMAP				-53
110 
111 //generic error codes
112 #define GRG_MEM_ALLOCATION_ERR			-71
113 #define GRG_ARGUMENT_ERR				-72
114 
115 typedef struct _grg_context *GRG_CTX;
116 typedef struct _grg_key *GRG_KEY;
117 typedef struct _grg_tmpfile *GRG_TMPFILE;
118 
119 // General purpose functions
120 
121 unsigned char *grg_get_version (void);
122 unsigned int grg_get_int_version (void);
123 
124 // Security related functions
125 
126 unsigned char *grg_rnd_seq (const GRG_CTX gctx, const unsigned int size);
127 void grg_rnd_seq_direct (const GRG_CTX gctx, unsigned char *toOverwrite,
128 	const unsigned int size);
129 unsigned char grg_rnd_chr (const GRG_CTX gctx);
130 void grg_free (const GRG_CTX gctx, void *alloc_data, const long dim);
131 double grg_ascii_pwd_quality (const unsigned char *pwd, const long pwd_len);
132 double grg_file_pwd_quality (const unsigned char *pwd_path);
133 
134 // libGringotts context (GRG_CTX) related functions
135 
136 GRG_CTX grg_context_initialize (const unsigned char *header,
137 				const grg_crypt_algo crypt_algo, const grg_hash_algo hash_algo,
138 				const grg_comp_algo comp_algo, const grg_comp_ratio comp_lvl,
139 				const grg_security_lvl sec_lvl);
140 GRG_CTX grg_context_initialize_defaults (const unsigned char *header);
141 void grg_context_free (GRG_CTX gctx);
142 
143 grg_crypt_algo grg_ctx_get_crypt_algo (const GRG_CTX gctx);
144 grg_hash_algo grg_ctx_get_hash_algo (const GRG_CTX gctx);
145 grg_comp_algo grg_ctx_get_comp_algo (const GRG_CTX gctx);
146 grg_comp_ratio grg_ctx_get_comp_ratio (const GRG_CTX gctx);
147 grg_security_lvl grg_ctx_get_security_lvl (const GRG_CTX gctx);
148 
149 void grg_ctx_set_crypt_algo (GRG_CTX gctx, const grg_crypt_algo crypt_algo);
150 void grg_ctx_set_hash_algo (GRG_CTX gctx, const grg_hash_algo hash_algo);
151 void grg_ctx_set_comp_algo (GRG_CTX gctx, const grg_comp_algo comp_algo);
152 void grg_ctx_set_comp_ratio (GRG_CTX gctx, const grg_comp_ratio comp_ratio);
153 void grg_ctx_set_security_lvl (GRG_CTX gctx,
154 			       const grg_security_lvl sec_level);
155 
156 unsigned int grg_get_key_size_static (const grg_crypt_algo crypt_algo);
157 unsigned int grg_get_key_size (const GRG_CTX gctx);
158 unsigned int grg_get_block_size_static (const grg_crypt_algo crypt_algo);
159 unsigned int grg_get_block_size (const GRG_CTX gctx);
160 
161 // libGringotts keyholder (GRG_KEY) related functions
162 
163 GRG_KEY grg_key_gen (const unsigned char *pwd, const int pwd_len);
164 GRG_KEY grg_key_clone (const GRG_KEY src);
165 int grg_key_compare (const GRG_KEY k1, const GRG_KEY k2);
166 void grg_key_free (const GRG_CTX gctx, GRG_KEY key);
167 
168 // File encryption/decryption functions
169 int grg_validate_file (const GRG_CTX gctx, const unsigned char *path);
170 int grg_update_gctx_from_file (GRG_CTX gctx, const unsigned char *path);
171 int grg_decrypt_file (const GRG_CTX gctx, const GRG_KEY keystruct,
172 		      const unsigned char *path, unsigned char **origData,
173 		      long *origDim);
174 int grg_encrypt_file (const GRG_CTX gctx, const GRG_KEY keystruct,
175 		      const unsigned char *path,
176 		      const unsigned char *origData, const long origDim);
177 
178 // Their "direct" versions, requiring a file descriptor instead of a path
179 int grg_validate_file_direct (const GRG_CTX gctx, const int fd);
180 int grg_update_gctx_from_file_direct (GRG_CTX gctx, const int fd);
181 int grg_decrypt_file_direct (const GRG_CTX gctx, const GRG_KEY keystruct,
182 			     const int fd, unsigned char **origData,
183 			     long *origDim);
184 int grg_encrypt_file_direct (const GRG_CTX gctx, const GRG_KEY keystruct,
185 			     const int fd, const unsigned char *origData,
186 			     const long origDim);
187 
188 // Memory encryption/decryption functions
189 int grg_validate_mem (const GRG_CTX gctx, const void *mem, const long memDim);
190 int grg_update_gctx_from_mem (GRG_CTX gctx, const void *mem,
191 			      const long memDim);
192 int grg_decrypt_mem (const GRG_CTX gctx, const GRG_KEY keystruct,
193 		     const void *mem, const long memDim,
194 		     unsigned char **origData, long *origDim);
195 int grg_encrypt_mem (const GRG_CTX gctx, const GRG_KEY keystruct, void **mem,
196 		     long *memDim, const unsigned char *origData,
197 		     const long origDim);
198 
199 // Encrypted temporary files functions
200 GRG_TMPFILE grg_tmpfile_gen (const GRG_CTX gctx);
201 int grg_tmpfile_write (const GRG_CTX gctx, GRG_TMPFILE tf,
202 		       const unsigned char *data, const long data_len);
203 int grg_tmpfile_read (const GRG_CTX gctx, const GRG_TMPFILE tf,
204 		      unsigned char **data, long *data_len);
205 void grg_tmpfile_close (const GRG_CTX gctx, GRG_TMPFILE tf);
206 
207 // Miscellaneous file functions
208 unsigned char *grg_encode64 (const unsigned char *in,
209 			     const int inlen, unsigned int *outlen);
210 unsigned char *grg_decode64 (const unsigned char *in,
211 			     const int inlen, unsigned int *outlen);
212 
213 int grg_file_shred (const char *path, const int npasses);
214 
215 #endif
216