cc-hash.c: New file containing hash-related code from hashsum and dsig.
[u/mdw/catacomb] / cc.h
diff --git a/cc.h b/cc.h
index 4124b82..04ff17f 100644 (file)
--- a/cc.h
+++ b/cc.h
@@ -7,7 +7,7 @@
  * (c) 2004 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Catacomb.
  *
  * it under the terms of the GNU Library General Public License as
  * published by the Free Software Foundation; either version 2 of the
  * License, or (at your option) any later version.
- * 
+ *
  * Catacomb is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU Library General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU Library General Public
  * License along with Catacomb; if not, write to the Free
  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
 /*----- Header files ------------------------------------------------------*/
 
+#if _FILE_OFFSET_BITS != 64
+#  error "Must set _FILE_OFFSET_BITS to 64."
+#endif
+
 #include <stdio.h>
 #include <string.h>
+#include <time.h>
 
 #include <mLib/dstr.h>
 
@@ -46,7 +51,7 @@
 #include "ghash.h"
 #include "gmac.h"
 
-/*----- Data structures ---------------------------------------------------*/
+/*----- Cryptographic object tables ---------------------------------------*/
 
 /* --- Key encapsulation --- */
 
@@ -76,58 +81,6 @@ struct kemtab {
 
 extern const struct kemtab kemtab[];
 
-/* --- Signing --- */
-
-typedef struct sig {
-  const struct sigops *ops;
-  key_packdef *kp;
-  void *kd;
-  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;
-  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
@@ -168,6 +121,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
@@ -192,6 +173,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
@@ -258,29 +264,124 @@ extern void freeenc(enc */*e*/);
 
 #define CMD_ENCODE {                                                   \
   "encode", cmd_encode,                                                        \
-    "encode [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]",                        \
+    "encode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]",           \
     "\
 Options:\n\
 \n\
 -f, --format=FORMAT    Encode to FORMAT.\n\
 -b, --boundary=LABEL   PEM boundary is LABEL.\n\
 -o, --output=FILE      Write output to FILE.\n\
+-p, --progress         Show progress on large files.\n\
 " }
 
 #define CMD_DECODE {                                                   \
   "decode", cmd_decode,                                                        \
-    "decode [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]",                        \
+    "decode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]",           \
     "\
 Options:\n\
 \n\
 -f, --format=FORMAT    Decode from FORMAT.\n\
 -b, --boundary=LABEL   PEM boundary is LABEL.\n\
 -o, --output=FILE      Write output to FILE.\n\
+-p, --progress         Show progress on large files.\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 ------------------------------------------------------*/
+
+/* --- @fhash@ --- *
+ *
+ * Arguments:  @const gchash *gch@ = pointer to hash function to use
+ *             @unsigned f@ = flags to set
+ *             @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.
+ */
+
+#define FHF_BINARY 256u
+#define FHF_PROGRESS 512u
+
+#define FHF_MASK 3840
+
+extern int fhash(const gchash */*gch*/, unsigned /*f*/,
+                const char */*file*/, void */*buf*/);
+
+/*----- 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.
@@ -348,6 +449,65 @@ struct listent {
 extern int displaylists(const struct listent */*listtab*/,
                        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
+ *             @const char *name@ = file name string to show
+ *             @FILE *fp@ = file we're reading from
+ *
+ * Returns:    Zero on success, nonzero if the file's state is now broken.
+ *
+ * Use:                Initializes a progress context.  Nothing is actually
+ *             displayed yet.
+ */
+
+extern int fprogress_init(fprogress */*f*/,
+                         const char */*name*/, FILE */*fp*/);
+
+/* --- @fprogress_update@ --- *
+ *
+ * Arguments:  @fprogress *f@ = progress context
+ *             @size_t n@ = how much progress has been made
+ *
+ * Returns:    ---
+ *
+ * Use:                Maybe updates the display to show that some progress has been
+ *             made.
+ */
+
+extern void fprogress_update(fprogress */*f*/, size_t /*n*/);
+
+/* --- @fprogress_clear@ --- *
+ *
+ * Arguments:  @fprogress *f@ = progress context
+ *
+ * Returns:    ---
+ *
+ * Use:                Clears the progress display from the screen.
+ */
+
+extern void fprogress_clear(fprogress */*f*/);
+
+/* --- @fprogress_done@ --- *
+ *
+ * Arguments:  @fprogress *f@ = progress context
+ *
+ * Returns:    ---
+ *
+ * Use:                Clear up the progress context and removes any display.
+ */
+
+extern void fprogress_done(fprogress */*f*/);
+
 /*----- Subcommand dispatch -----------------------------------------------*/
 
 typedef struct cmd {