cc-hash.c: New file containing hash-related code from hashsum and dsig.
[u/mdw/catacomb] / cc.h
CommitLineData
5c3f75ec 1/* -*-c-*-
2 *
c65df279 3 * $Id$
5c3f75ec 4 *
5 * Catcrypt common stuff
6 *
7 * (c) 2004 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
5c3f75ec 11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
45c0fd36 18 *
5c3f75ec 19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
45c0fd36 23 *
5c3f75ec 24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30#ifndef CATACOMB_CC_H
31#define CATACOMB_CC_H
32
33#ifdef __cplusplus
34 extern "C" {
35#endif
36
37/*----- Header files ------------------------------------------------------*/
38
cd6eca43
MW
39#if _FILE_OFFSET_BITS != 64
40# error "Must set _FILE_OFFSET_BITS to 64."
41#endif
42
5c3f75ec 43#include <stdio.h>
c65df279 44#include <string.h>
cd6eca43 45#include <time.h>
5c3f75ec 46
47#include <mLib/dstr.h>
48
49#include "key.h"
50#include "gcipher.h"
51#include "ghash.h"
52#include "gmac.h"
53
a1e745ad 54/*----- Cryptographic object tables ---------------------------------------*/
cd6eca43 55
5c3f75ec 56/* --- Key encapsulation --- */
57
58typedef struct kem {
59 const struct kemops *ops;
60 key_packdef *kp;
61 void *kd;
62 const gchash *h;
63 const gccipher *c, *cx;
64 const gcmac *m;
65} kem;
66
67typedef struct kemops {
68 const key_fetchdef *kf; /* Key fetching structure */
69 size_t kdsz; /* Size of the key-data structure */
70 kem *(*init)(key */*k*/, void */*kd*/);
71 int (*doit)(kem */*k*/, dstr */*d*/, ghash */*h*/);
72 const char *(*check)(kem */*k*/);
73 void (*destroy)(kem */*k*/);
74} kemops;
75
c65df279 76struct kemtab {
77 const char *name;
78 const kemops *encops;
79 const kemops *decops;
80};
81
82extern const struct kemtab kemtab[];
83
5c3f75ec 84/* --- @getkem@ --- *
85 *
86 * Arguments: @key *k@ = the key to load
87 * @const char *app@ = application name
88 * @int wantpriv@ = nonzero if we want to decrypt
89 *
90 * Returns: A key-encapsulating thing.
91 *
92 * Use: Loads a key.
93 */
94
95extern kem *getkem(key */*k*/, const char */*app*/, int /*wantpriv*/);
96
97/* --- @setupkem@ --- *
98 *
99 * Arguments: @kem *k@ = key-encapsulation thing
100 * @dstr *d@ = key-encapsulation data
101 * @gcipher **cx@ = key-expansion function (for IVs)
102 * @gcipher **c@ = where to put initialized encryption scheme
103 * @gmac **m@ = where to put initialized MAC
104 *
105 * Returns: Zero for success, nonzero on faliure.
106 *
107 * Use: Initializes all the various symmetric things from a KEM.
108 */
109
110extern int setupkem(kem */*k*/, dstr */*d*/,
111 gcipher **/*cx*/, gcipher **/*c*/, gmac **/*m*/);
112
113/* --- @freekem@ --- *
114 *
115 * Arguments: @kem *k@ = key-encapsulation thing
116 *
117 * Returns: ---
118 *
119 * Use: Frees up a key-encapsulation thing.
120 */
121
122extern void freekem(kem */*k*/);
123
a1e745ad
MW
124/* --- Signing --- */
125
126typedef struct sig {
127 const struct sigops *ops;
128 key_packdef *kp;
129 void *kd;
130 const gchash *ch;
131 ghash *h;
132} sig;
133
134typedef struct sigops {
135 const key_fetchdef *kf; /* Key fetching structure */
136 size_t kdsz; /* Size of the key-data structure */
137 sig *(*init)(key */*k*/, void */*kd*/, const gchash */*hc*/);
138 int (*doit)(sig */*s*/, dstr */*d*/);
139 const char *(*check)(sig */*s*/);
140 void (*destroy)(sig */*s*/);
141} sigops;
142
143struct sigtab {
144 const char *name;
145 const sigops *signops;
146 const sigops *verifyops;
147 const gchash *ch;
148};
149
150extern const struct sigtab sigtab[];
151
5c3f75ec 152/* --- @getsig@ --- *
153 *
154 * Arguments: @key *k@ = the key to load
155 * @const char *app@ = application name
156 * @int wantpriv@ = nonzero if we want to sign
157 *
158 * Returns: A signature-making thing.
159 *
160 * Use: Loads a key and starts hashing.
161 */
162
163extern sig *getsig(key */*k*/, const char */*app*/, int /*wantpriv*/);
164
165/* --- @freesig@ --- *
166 *
167 * Arguments: @sig *s@ = signature-making thing
168 *
169 * Returns: ---
170 *
171 * Use: Frees up a signature-making thing
172 */
173
174extern void freesig(sig */*s*/);
175
a1e745ad
MW
176/*----- File encodings ----------------------------------------------------*/
177
178/* --- Data encoding --- */
179
180typedef struct enc {
181 const struct encops *ops;
182 FILE *fp;
183} enc;
184
185typedef struct encops {
186 const char *name;
187 const char *rmode, *wmode;
188 int nraw, ncook;
189 enc *(*initenc)(FILE */*fp*/, const char */*msg*/);
190 enc *(*initdec)(FILE */*fp*/,
191 int (*/*func*/)(const char *, void *), void */*p*/);
192 int (*read)(enc */*e*/, void */*p*/, size_t /*sz*/);
193 int (*write)(enc */*e*/, const void */*p*/, size_t /*sz*/);
194 int (*encdone)(enc */*e*/);
195 int (*decdone)(enc */*e*/);
196 void (*destroy)(enc */*e*/);
197} encops;
198
199extern const encops enctab[];
200
5c3f75ec 201/* --- @getenc@ --- *
202 *
203 * Arguments: @const char *enc@ = name of wanted encoding
204 *
205 * Returns: Pointer to encoder operations.
206 *
207 * Use: Finds a named encoder or decoder.
208 */
209
210extern const encops *getenc(const char */*enc*/);
211
fa54fe1e 212/* --- @checkbdry@ --- *
213 *
214 * Arguments: @const char *b@ = boundary string found
215 * @void *p@ = boundary string wanted
216 *
217 * Returns: Nonzero if the boundary string is the one we wanted.
218 *
219 * Use: Pass as @func@ to @initdec@ if you just want a simple life.
220 */
221
222extern int checkbdry(const char */*b*/, void */*p*/);
223
5c3f75ec 224/* --- @initenc@ --- *
225 *
226 * Arguments: @const encops *eo@ = operations (from @getenc@)
227 * @FILE *fp@ = file handle to attach
228 * @const char *msg@ = banner message
5c3f75ec 229 *
230 * Returns: The encoder object.
231 *
232 * Use: Initializes an encoder.
233 */
234
fa54fe1e 235extern enc *initenc(const encops */*eo*/, FILE */*fp*/, const char */*msg*/);
236
237/* --- @initdec@ --- *
238 *
239 * Arguments: @const encops *eo@ = operations (from @getenc@)
240 * @FILE *fp@ = file handle to attach
241 * @int (*func)(const char *, void *)@ = banner check function
242 * @void *p@ = argument for @func@
243 *
244 * Returns: The encoder object.
245 *
246 * Use: Initializes an encoder.
247 */
248
249extern enc *initdec(const encops */*eo*/, FILE */*fp*/,
250 int (*/*func*/)(const char *, void *), void */*p*/);
5c3f75ec 251
252/* --- @freeenc@ --- *
253 *
254 * Arguments: @enc *e@ = encoder object
255 *
256 * Returns: ---
257 *
258 * Use: Frees an encoder object.
259 */
260
261extern void freeenc(enc */*e*/);
262
fa54fe1e 263/* --- @cmd_encode@, @cmd_decode@ --- */
264
265#define CMD_ENCODE { \
266 "encode", cmd_encode, \
cd6eca43 267 "encode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
fa54fe1e 268 "\
269Options:\n\
270\n\
271-f, --format=FORMAT Encode to FORMAT.\n\
272-b, --boundary=LABEL PEM boundary is LABEL.\n\
273-o, --output=FILE Write output to FILE.\n\
cd6eca43 274-p, --progress Show progress on large files.\n\
fa54fe1e 275" }
276
277#define CMD_DECODE { \
278 "decode", cmd_decode, \
cd6eca43 279 "decode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
fa54fe1e 280 "\
281Options:\n\
282\n\
283-f, --format=FORMAT Decode from FORMAT.\n\
284-b, --boundary=LABEL PEM boundary is LABEL.\n\
285-o, --output=FILE Write output to FILE.\n\
cd6eca43 286-p, --progress Show progress on large files.\n\
fa54fe1e 287" }
288
289extern int cmd_encode(int /*argc*/, char */*argv*/[]);
290extern int cmd_decode(int /*argc*/, char */*argv*/[]);
291
18b3351a
MW
292/*----- Hash encoding functions -------------------------------------------*/
293
294/* --- Table --- */
295
296#define ENCODINGS(_) \
297 _(HEX, hex) \
298 _(BASE64, base64) \
299 _(BASE32, base32)
300
301enum {
302#define ENUM(tag, name) ENC_##tag,
303 ENCODINGS(ENUM)
304#undef ENUM
305 ENC_LIMIT
306};
307
308typedef struct encodeops {
309 const char *name;
310 void (*put)(const octet *, size_t, FILE *);
311 size_t (*get)(const char *, octet *, size_t, char **);
312} encodeops;
313
314extern const encodeops encodingtab[];
315
316/* --- @getencoding@ --- *
317 *
318 * Arguments: @const char *ename@ = encoding name
319 *
320 * Returns: Pointer to encoding table entry, or null.
321 *
322 * Use: Finds an encoding entry given its name.
323 */
324
325extern const encodeops *getencoding(const char */*ename*/);
326
327/*----- File hashing ------------------------------------------------------*/
328
329/* --- @fhash@ --- *
330 *
331 * Arguments: @const gchash *gch@ = pointer to hash function to use
332 * @unsigned f@ = flags to set
333 * @const char *file@ = file name to be hashed (null for stdin)
334 * @void *buf@ = pointer to hash output buffer
335 *
336 * Returns: Zero if it worked, nonzero on error.
337 *
338 * Use: Hashes a file.
339 */
340
341#define FHF_BINARY 256u
342#define FHF_PROGRESS 512u
343
344#define FHF_MASK 3840
345
346extern int fhash(const gchash */*gch*/, unsigned /*f*/,
347 const char */*file*/, void */*buf*/);
348
349/*----- String I/O --------------------------------------------------------*/
350
351#define GSF_RAW 4096u
352#define GSF_FILE 0u
353#define GSF_STRING 8192u
354
355#define GSF_MASK 61440u
356
357/* --- @getstring@ --- *
358 *
359 * Arguments: @void *in@ = input source
360 * @dstr *d@ = destination string
361 * @unsigned f@ = input flags
362 *
363 * Returns: Zero if OK, nonzero on end-of-file.
364 *
365 * Use: Reads a filename (or something similar) from a stream.
366 */
367
368extern int getstring(void */*in*/, dstr */*d*/, unsigned /*f*/);
369
370/* --- @putstring@ --- *
371 *
372 * Arguments: @FILE *fp@ = stream to write on
373 * @const char *p@ = pointer to text
374 * @unsigned f@ = output flags
375 *
376 * Returns: ---
377 *
378 * Use: Emits a string to a stream.
379 */
380
381extern void putstring(FILE */*fp*/, const char */*p*/, unsigned /*f*/);
382
a1e745ad
MW
383/*----- Lists of things ---------------------------------------------------*/
384
c65df279 385/* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- *
386 *
387 * Produce list of things. Requires @i@ and @w@ variables in scope.
388 * END-TEST and NAME-EXPR are in terms of @i@.
389 */
390
391#define LIST(what, fp, end, name) do { \
392 fputs(what ":\n ", fp); \
393 w = 2; \
394 for (i = 0; end; i++) { \
395 if (w == 2) \
396 w += strlen(name); \
397 else { \
398 if (strlen(name) + w > 76) { \
399 fputs("\n ", fp); \
400 w = 2 + strlen(name); \
401 } else { \
402 fputc(' ', fp); \
403 w += strlen(name) + 1; \
404 } \
405 } \
406 fputs(name, fp); \
407 } \
408 fputc('\n', fp); \
409} while (0)
410
411#define STDLISTS(LI) \
412 LI("Hash functions", hash, \
413 ghashtab[i], ghashtab[i]->name) \
414 LI("Encryption schemes", enc, \
415 gciphertab[i], gciphertab[i]->name) \
416 LI("Message authentication schemes", mac, \
417 gmactab[i], gmactab[i]->name) \
418 LI("Elliptic curves", ec, \
419 ectab[i].name, ectab[i].name) \
420 LI("Diffie-Hellman groups", dh, \
421 ptab[i].name, ptab[i].name)
422
423#define LIDECL(text, tag, test, name) \
424 static void show_##tag(void);
425
426#define LIDEF(text, tag, test, name) \
427 static void show_##tag(void) \
428 { \
429 unsigned i, w; \
430 LIST(text, stdout, test, name); \
431 }
432
433#define LIENT(text, tag, test, name) \
434 { #tag, show_##tag },
435
436struct listent {
437 const char *name;
438 void (*list)(void);
439};
440
441#define MAKELISTTAB(listtab, LISTS) \
442 LISTS(LIDECL) \
443 static const struct listent listtab[] = { \
444 LISTS(LIENT) \
445 { 0, 0 } \
446 }; \
447 LISTS(LIDEF)
448
449extern int displaylists(const struct listent */*listtab*/,
450 char *const /*argv*/[]);
451
cd6eca43
MW
452/*----- Progress indicators -----------------------------------------------*/
453
a1e745ad
MW
454typedef struct fprogress {
455 const char *bp;
456 off_t o, sz, olast;
457 time_t start, last;
458 char name[24];
459} fprogress;
460
cd6eca43
MW
461/* --- @fprogress_init@ --- *
462 *
463 * Arguments: @fprogress *f@ = progress context to be initialized
464 * @const char *name@ = file name string to show
465 * @FILE *fp@ = file we're reading from
466 *
467 * Returns: Zero on success, nonzero if the file's state is now broken.
468 *
469 * Use: Initializes a progress context. Nothing is actually
470 * displayed yet.
471 */
472
473extern int fprogress_init(fprogress */*f*/,
474 const char */*name*/, FILE */*fp*/);
475
476/* --- @fprogress_update@ --- *
477 *
478 * Arguments: @fprogress *f@ = progress context
479 * @size_t n@ = how much progress has been made
480 *
481 * Returns: ---
482 *
483 * Use: Maybe updates the display to show that some progress has been
484 * made.
485 */
486
487extern void fprogress_update(fprogress */*f*/, size_t /*n*/);
488
489/* --- @fprogress_clear@ --- *
490 *
491 * Arguments: @fprogress *f@ = progress context
492 *
493 * Returns: ---
494 *
495 * Use: Clears the progress display from the screen.
496 */
497
498extern void fprogress_clear(fprogress */*f*/);
499
500/* --- @fprogress_done@ --- *
501 *
502 * Arguments: @fprogress *f@ = progress context
503 *
504 * Returns: ---
505 *
506 * Use: Clear up the progress context and removes any display.
507 */
508
509extern void fprogress_done(fprogress */*f*/);
510
a1e745ad
MW
511/*----- Subcommand dispatch -----------------------------------------------*/
512
513typedef struct cmd {
514 const char *name;
515 int (*cmd)(int /*argc*/, char */*argv*/[]);
516 const char *usage;
517 const char *help;
518} cmd;
519
520extern void version(FILE */*fp*/);
521extern void help_global(FILE */*fp*/);
522
523/* --- @findcmd@ --- *
524 *
525 * Arguments: @const cmd *cmds@ = pointer to command table
526 * @const char *name@ = a command name
527 *
528 * Returns: Pointer to the command structure.
529 *
530 * Use: Looks up a command by name. If the command isn't found, an
531 * error is reported and the program is terminated.
532 */
533
534const cmd *findcmd(const cmd */*cmds*/, const char */*name*/);
535
536/* --- @sc_help@ --- *
537 *
538 * Arguments: @const cmd *cmds@ = pointer to command table
539 * @FILE *fp@ = output file handle
540 * @char *const *argv@ = remaining arguments
541 *
542 * Returns: ---
543 *
544 * Use: Prints a help message, maybe with help about subcommands.
545 */
546
547extern void sc_help(const cmd */*cmds*/, FILE */*fp*/,
548 char *const */*argv*/);
549
5c3f75ec 550/*----- That's all, folks -------------------------------------------------*/
551
552#ifdef __cplusplus
553 }
554#endif
555
556#endif