X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/cd6eca4375f46a35b93e2fea4b0428a23b451aa3..813390c45f438f411662b1a55678e63f11681eb4:/cc.h diff --git a/cc.h b/cc.h index 6f92978..f3a03f3 100644 --- a/cc.h +++ b/cc.h @@ -44,6 +44,9 @@ #include #include +#include +#include + #include #include "key.h" @@ -51,16 +54,7 @@ #include "ghash.h" #include "gmac.h" -/*----- Data structures ---------------------------------------------------*/ - -/* --- Progress indicators --- */ - -typedef struct fprogress { - const char *bp; - off_t o, sz, olast; - time_t start, last; - char name[24]; -} fprogress; +/*----- Cryptographic object tables ---------------------------------------*/ /* --- Key encapsulation --- */ @@ -90,59 +84,6 @@ struct kemtab { extern const struct kemtab kemtab[]; -/* --- Signing --- */ - -typedef struct sig { - const struct sigops *ops; - key_packdef *kp; - void *kd; - const gchash *ch; - ghash *h; -} sig; - -typedef struct sigops { - const key_fetchdef *kf; /* Key fetching structure */ - size_t kdsz; /* Size of the key-data structure */ - sig *(*init)(key */*k*/, void */*kd*/, const gchash */*hc*/); - int (*doit)(sig */*s*/, dstr */*d*/); - const char *(*check)(sig */*s*/); - void (*destroy)(sig */*s*/); -} sigops; - -struct sigtab { - const char *name; - const sigops *signops; - const sigops *verifyops; - const gchash *ch; -}; - -extern const struct sigtab sigtab[]; - -/* --- Data encoding --- */ - -typedef struct enc { - const struct encops *ops; - FILE *fp; -} enc; - -typedef struct encops { - const char *name; - const char *rmode, *wmode; - int nraw, ncook; - enc *(*initenc)(FILE */*fp*/, const char */*msg*/); - enc *(*initdec)(FILE */*fp*/, - int (*/*func*/)(const char *, void *), void */*p*/); - int (*read)(enc */*e*/, void */*p*/, size_t /*sz*/); - int (*write)(enc */*e*/, const void */*p*/, size_t /*sz*/); - int (*encdone)(enc */*e*/); - int (*decdone)(enc */*e*/); - void (*destroy)(enc */*e*/); -} encops; - -extern const encops enctab[]; - -/*----- Functions provided ------------------------------------------------*/ - /* --- @getkem@ --- * * * Arguments: @key *k@ = the key to load @@ -183,6 +124,34 @@ extern int setupkem(kem */*k*/, dstr */*d*/, extern void freekem(kem */*k*/); +/* --- Signing --- */ + +typedef struct sig { + const struct sigops *ops; + key_packdef *kp; + void *kd; + const gchash *ch; + ghash *h; +} sig; + +typedef struct sigops { + const key_fetchdef *kf; /* Key fetching structure */ + size_t kdsz; /* Size of the key-data structure */ + sig *(*init)(key */*k*/, void */*kd*/, const gchash */*hc*/); + int (*doit)(sig */*s*/, dstr */*d*/); + const char *(*check)(sig */*s*/); + void (*destroy)(sig */*s*/); +} sigops; + +struct sigtab { + const char *name; + const sigops *signops; + const sigops *verifyops; + const gchash *ch; +}; + +extern const struct sigtab sigtab[]; + /* --- @getsig@ --- * * * Arguments: @key *k@ = the key to load @@ -207,6 +176,31 @@ extern sig *getsig(key */*k*/, const char */*app*/, int /*wantpriv*/); extern void freesig(sig */*s*/); +/*----- File encodings ----------------------------------------------------*/ + +/* --- Data encoding --- */ + +typedef struct enc { + const struct encops *ops; + FILE *fp; +} enc; + +typedef struct encops { + const char *name; + const char *rmode, *wmode; + int nraw, ncook; + enc *(*initenc)(FILE */*fp*/, const char */*msg*/); + enc *(*initdec)(FILE */*fp*/, + int (*/*func*/)(const char *, void *), void */*p*/); + int (*read)(enc */*e*/, void */*p*/, size_t /*sz*/); + int (*write)(enc */*e*/, const void */*p*/, size_t /*sz*/); + int (*encdone)(enc */*e*/); + int (*decdone)(enc */*e*/); + void (*destroy)(enc */*e*/); +} encops; + +extern const encops enctab[]; + /* --- @getenc@ --- * * * Arguments: @const char *enc@ = name of wanted encoding @@ -298,6 +292,199 @@ Options:\n\ extern int cmd_encode(int /*argc*/, char */*argv*/[]); extern int cmd_decode(int /*argc*/, char */*argv*/[]); +/*----- Hash encoding functions -------------------------------------------*/ + +/* --- Table --- */ + +#define ENCODINGS(_) \ + _(HEX, hex) \ + _(BASE64, base64) \ + _(BASE32, base32) + +enum { +#define ENUM(tag, name) ENC_##tag, + ENCODINGS(ENUM) +#undef ENUM + ENC_LIMIT +}; + +typedef struct encodeops { + const char *name; + void (*put)(const octet *, size_t, FILE *); + size_t (*get)(const char *, octet *, size_t, char **); +} encodeops; + +extern const encodeops encodingtab[]; + +/* --- @getencoding@ --- * + * + * Arguments: @const char *ename@ = encoding name + * + * Returns: Pointer to encoding table entry, or null. + * + * Use: Finds an encoding entry given its name. + */ + +extern const encodeops *getencoding(const char */*ename*/); + +/*----- File hashing ------------------------------------------------------*/ + +typedef struct fhashstate { + const gchash *gch; + unsigned f; + struct fhent *ents; +} fhashstate; + +#define FHF_BINARY 0x100u +#define FHF_PROGRESS 0x200u +#define FHF_JUNK 0x400u + +#define FHF_MASK 0xff00u + +/* --- @gethash@ --- * + * + * Arguments: @const char *name@ = pointer to name string + * + * Returns: Pointer to appropriate hash class. + * + * Use: Chooses a hash function by name. + */ + +extern const gchash *gethash(const char */*name*/); + +/* --- @describefile@ --- * + * + * Arguments: @const struct stat *st@ = pointer to file state + * + * Returns: A snappy one-word description of the file. + */ + +extern const char *describefile(const struct stat */*st*/); + +/* --- @fhash_init@ ---* + * + * Arguments: @fhashstate *fh@ = pointer to fhash state to initialize + * @const gchash *gch@ = hash class to set + * @unsigned f@ initial flags to set + * + * Returns: --- + * + * Use: Initializes an @fhashstate@ structure. + */ + +extern void fhash_init(fhashstate */*fh*/, + const gchash */*gch*/, unsigned /*f*/); + +/* --- @fhash_free@ --- * + * + * Arguments: @fhashstate *fh@ = pointer to fhash state to free + * + * Returns: --- + * + * Use: Frees an fhash state. + */ + +extern void fhash_free(fhashstate */*fh*/); + +/* --- @fhash@ --- * + * + * Arguments: @fhashstate *fh@ = pointer to fhash state + * @const char *file@ = file name to be hashed (null for stdin) + * @void *buf@ = pointer to hash output buffer + * + * Returns: Zero if it worked, nonzero on error. + * + * Use: Hashes a file. + */ + +extern int fhash(fhashstate */*fh*/, const char */*file*/, void */*buf*/); + +/* --- @fhash_junk@ --- * + * + * Arguments: @fhashstate *fh@ = pointer to fhash state + * @void (*func)(const char *, const struct stat *, void *)@ + * @void *p@ = pointer to pass to function + * + * Returns: Positive if any junk was found, negative on error, zero if + * everything was fine. + * + * Use: Reports junk files in any directories covered by the hash + * state. + */ + +extern int fhash_junk(fhashstate */*fh*/, + int (*/*func*/)(const char *, + const struct stat *, + void *), + void */*p*/); + +/* --- @hfparse@ --- * + * + * Arguments: @hfpctx *hfp@ = pointer to the context structure + * + * Returns: A code indicating what happened. + * + * Use: Parses a line from the input file. + */ + +enum { /* Meaning and members set */ + HF_FILE, /* File hash: @dline@ and @hbuf@ */ + HF_ENC, /* Encoding: @ee@ */ + HF_HASH, /* Hash function: @gch@ */ + HF_ESC, /* Name escape: @f@ */ + HF_EOF, /* End of file */ + HF_BAD /* Unrecognized line */ +}; + +typedef struct hfpctx { + unsigned f; /* Flags to read */ +#define HFF_ESCAPE 1u /* File names are escaped */ + FILE *fp; /* Input file to read */ + dstr *dline; /* Line contents, corrupted */ + const gchash *gch; /* Hash function to use */ + const encodeops *ee; /* Encoding to apply to hashes */ + dstr *dfile; /* File name for @HF_FILE@ lines */ + octet *hbuf; /* Output buffer for hash data */ +} hfpctx; + +extern int hfparse(hfpctx */*hfp*/); + +/*----- String I/O --------------------------------------------------------*/ + +#define GSF_RAW 4096u +#define GSF_FILE 0u +#define GSF_STRING 8192u + +#define GSF_MASK 61440u + +/* --- @getstring@ --- * + * + * Arguments: @void *in@ = input source + * @dstr *d@ = destination string + * @unsigned f@ = input flags + * + * Returns: Zero if OK, nonzero on end-of-file. + * + * Use: Reads a filename (or something similar) from a stream. + */ + +extern int getstring(void */*in*/, dstr */*d*/, unsigned /*f*/); + +/* --- @putstring@ --- * + * + * Arguments: @FILE *fp@ = stream to write on + * @const char *p@ = pointer to text + * @unsigned f@ = output flags + * + * Returns: --- + * + * Use: Emits a string to a stream. + */ + +extern void putstring(FILE */*fp*/, const char */*p*/, unsigned /*f*/); + +/*----- Lists of things ---------------------------------------------------*/ + /* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- * * * Produce list of things. Requires @i@ and @w@ variables in scope. @@ -365,47 +552,15 @@ struct listent { extern int displaylists(const struct listent */*listtab*/, char *const /*argv*/[]); -/*----- Subcommand dispatch -----------------------------------------------*/ - -typedef struct cmd { - const char *name; - int (*cmd)(int /*argc*/, char */*argv*/[]); - const char *usage; - const char *help; -} cmd; - -extern void version(FILE */*fp*/); -extern void help_global(FILE */*fp*/); - -/* --- @findcmd@ --- * - * - * Arguments: @const cmd *cmds@ = pointer to command table - * @const char *name@ = a command name - * - * Returns: Pointer to the command structure. - * - * Use: Looks up a command by name. If the command isn't found, an - * error is reported and the program is terminated. - */ - -const cmd *findcmd(const cmd */*cmds*/, const char */*name*/); - -/* --- @sc_help@ --- * - * - * Arguments: @const cmd *cmds@ = pointer to command table - * @FILE *fp@ = output file handle - * @char *const *argv@ = remaining arguments - * - * Returns: --- - * - * Use: Prints a help message, maybe with help about subcommands. - */ - -extern void sc_help(const cmd */*cmds*/, FILE */*fp*/, - char *const */*argv*/); - /*----- Progress indicators -----------------------------------------------*/ +typedef struct fprogress { + const char *bp; + off_t o, sz, olast; + time_t start, last; + char name[24]; +} fprogress; + /* --- @fprogress_init@ --- * * * Arguments: @fprogress *f@ = progress context to be initialized @@ -456,6 +611,45 @@ extern void fprogress_clear(fprogress */*f*/); extern void fprogress_done(fprogress */*f*/); +/*----- Subcommand dispatch -----------------------------------------------*/ + +typedef struct cmd { + const char *name; + int (*cmd)(int /*argc*/, char */*argv*/[]); + const char *usage; + const char *help; +} cmd; + +extern void version(FILE */*fp*/); +extern void help_global(FILE */*fp*/); + +/* --- @findcmd@ --- * + * + * Arguments: @const cmd *cmds@ = pointer to command table + * @const char *name@ = a command name + * + * Returns: Pointer to the command structure. + * + * Use: Looks up a command by name. If the command isn't found, an + * error is reported and the program is terminated. + */ + +const cmd *findcmd(const cmd */*cmds*/, const char */*name*/); + +/* --- @sc_help@ --- * + * + * Arguments: @const cmd *cmds@ = pointer to command table + * @FILE *fp@ = output file handle + * @char *const *argv@ = remaining arguments + * + * Returns: --- + * + * Use: Prints a help message, maybe with help about subcommands. + */ + +extern void sc_help(const cmd */*cmds*/, FILE */*fp*/, + char *const */*argv*/); + /*----- That's all, folks -------------------------------------------------*/ #ifdef __cplusplus