| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id$ |
| 4 | * |
| 5 | * Catcrypt common stuff |
| 6 | * |
| 7 | * (c) 2004 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 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. |
| 18 | * |
| 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. |
| 23 | * |
| 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 | |
| 39 | #include <stdio.h> |
| 40 | #include <string.h> |
| 41 | |
| 42 | #include <mLib/dstr.h> |
| 43 | |
| 44 | #include "key.h" |
| 45 | #include "gcipher.h" |
| 46 | #include "ghash.h" |
| 47 | #include "gmac.h" |
| 48 | |
| 49 | /*----- Data structures ---------------------------------------------------*/ |
| 50 | |
| 51 | /* --- Key encapsulation --- */ |
| 52 | |
| 53 | typedef struct kem { |
| 54 | const struct kemops *ops; |
| 55 | key_packdef *kp; |
| 56 | void *kd; |
| 57 | const gchash *h; |
| 58 | const gccipher *c, *cx; |
| 59 | const gcmac *m; |
| 60 | } kem; |
| 61 | |
| 62 | typedef struct kemops { |
| 63 | const key_fetchdef *kf; /* Key fetching structure */ |
| 64 | size_t kdsz; /* Size of the key-data structure */ |
| 65 | kem *(*init)(key */*k*/, void */*kd*/); |
| 66 | int (*doit)(kem */*k*/, dstr */*d*/, ghash */*h*/); |
| 67 | const char *(*check)(kem */*k*/); |
| 68 | void (*destroy)(kem */*k*/); |
| 69 | } kemops; |
| 70 | |
| 71 | struct kemtab { |
| 72 | const char *name; |
| 73 | const kemops *encops; |
| 74 | const kemops *decops; |
| 75 | }; |
| 76 | |
| 77 | extern const struct kemtab kemtab[]; |
| 78 | |
| 79 | /* --- Signing --- */ |
| 80 | |
| 81 | typedef struct sig { |
| 82 | const struct sigops *ops; |
| 83 | key_packdef *kp; |
| 84 | void *kd; |
| 85 | const gchash *ch; |
| 86 | ghash *h; |
| 87 | } sig; |
| 88 | |
| 89 | typedef struct sigops { |
| 90 | const key_fetchdef *kf; /* Key fetching structure */ |
| 91 | size_t kdsz; /* Size of the key-data structure */ |
| 92 | sig *(*init)(key */*k*/, void */*kd*/, const gchash */*hc*/); |
| 93 | int (*doit)(sig */*s*/, dstr */*d*/); |
| 94 | const char *(*check)(sig */*s*/); |
| 95 | void (*destroy)(sig */*s*/); |
| 96 | } sigops; |
| 97 | |
| 98 | struct sigtab { |
| 99 | const char *name; |
| 100 | const sigops *signops; |
| 101 | const sigops *verifyops; |
| 102 | const gchash *ch; |
| 103 | }; |
| 104 | |
| 105 | extern const struct sigtab sigtab[]; |
| 106 | |
| 107 | /* --- Data encoding --- */ |
| 108 | |
| 109 | typedef struct enc { |
| 110 | const struct encops *ops; |
| 111 | FILE *fp; |
| 112 | } enc; |
| 113 | |
| 114 | typedef struct encops { |
| 115 | const char *name; |
| 116 | const char *rmode, *wmode; |
| 117 | enc *(*initenc)(FILE */*fp*/, const char */*msg*/); |
| 118 | enc *(*initdec)(FILE */*fp*/, |
| 119 | int (*/*func*/)(const char *, void *), void */*p*/); |
| 120 | int (*read)(enc */*e*/, void */*p*/, size_t /*sz*/); |
| 121 | int (*write)(enc */*e*/, const void */*p*/, size_t /*sz*/); |
| 122 | int (*encdone)(enc */*e*/); |
| 123 | int (*decdone)(enc */*e*/); |
| 124 | void (*destroy)(enc */*e*/); |
| 125 | } encops; |
| 126 | |
| 127 | extern const encops enctab[]; |
| 128 | |
| 129 | /*----- Functions provided ------------------------------------------------*/ |
| 130 | |
| 131 | /* --- @getkem@ --- * |
| 132 | * |
| 133 | * Arguments: @key *k@ = the key to load |
| 134 | * @const char *app@ = application name |
| 135 | * @int wantpriv@ = nonzero if we want to decrypt |
| 136 | * |
| 137 | * Returns: A key-encapsulating thing. |
| 138 | * |
| 139 | * Use: Loads a key. |
| 140 | */ |
| 141 | |
| 142 | extern kem *getkem(key */*k*/, const char */*app*/, int /*wantpriv*/); |
| 143 | |
| 144 | /* --- @setupkem@ --- * |
| 145 | * |
| 146 | * Arguments: @kem *k@ = key-encapsulation thing |
| 147 | * @dstr *d@ = key-encapsulation data |
| 148 | * @gcipher **cx@ = key-expansion function (for IVs) |
| 149 | * @gcipher **c@ = where to put initialized encryption scheme |
| 150 | * @gmac **m@ = where to put initialized MAC |
| 151 | * |
| 152 | * Returns: Zero for success, nonzero on faliure. |
| 153 | * |
| 154 | * Use: Initializes all the various symmetric things from a KEM. |
| 155 | */ |
| 156 | |
| 157 | extern int setupkem(kem */*k*/, dstr */*d*/, |
| 158 | gcipher **/*cx*/, gcipher **/*c*/, gmac **/*m*/); |
| 159 | |
| 160 | /* --- @freekem@ --- * |
| 161 | * |
| 162 | * Arguments: @kem *k@ = key-encapsulation thing |
| 163 | * |
| 164 | * Returns: --- |
| 165 | * |
| 166 | * Use: Frees up a key-encapsulation thing. |
| 167 | */ |
| 168 | |
| 169 | extern void freekem(kem */*k*/); |
| 170 | |
| 171 | /* --- @getsig@ --- * |
| 172 | * |
| 173 | * Arguments: @key *k@ = the key to load |
| 174 | * @const char *app@ = application name |
| 175 | * @int wantpriv@ = nonzero if we want to sign |
| 176 | * |
| 177 | * Returns: A signature-making thing. |
| 178 | * |
| 179 | * Use: Loads a key and starts hashing. |
| 180 | */ |
| 181 | |
| 182 | extern sig *getsig(key */*k*/, const char */*app*/, int /*wantpriv*/); |
| 183 | |
| 184 | /* --- @freesig@ --- * |
| 185 | * |
| 186 | * Arguments: @sig *s@ = signature-making thing |
| 187 | * |
| 188 | * Returns: --- |
| 189 | * |
| 190 | * Use: Frees up a signature-making thing |
| 191 | */ |
| 192 | |
| 193 | extern void freesig(sig */*s*/); |
| 194 | |
| 195 | /* --- @getenc@ --- * |
| 196 | * |
| 197 | * Arguments: @const char *enc@ = name of wanted encoding |
| 198 | * |
| 199 | * Returns: Pointer to encoder operations. |
| 200 | * |
| 201 | * Use: Finds a named encoder or decoder. |
| 202 | */ |
| 203 | |
| 204 | extern const encops *getenc(const char */*enc*/); |
| 205 | |
| 206 | /* --- @checkbdry@ --- * |
| 207 | * |
| 208 | * Arguments: @const char *b@ = boundary string found |
| 209 | * @void *p@ = boundary string wanted |
| 210 | * |
| 211 | * Returns: Nonzero if the boundary string is the one we wanted. |
| 212 | * |
| 213 | * Use: Pass as @func@ to @initdec@ if you just want a simple life. |
| 214 | */ |
| 215 | |
| 216 | extern int checkbdry(const char */*b*/, void */*p*/); |
| 217 | |
| 218 | /* --- @initenc@ --- * |
| 219 | * |
| 220 | * Arguments: @const encops *eo@ = operations (from @getenc@) |
| 221 | * @FILE *fp@ = file handle to attach |
| 222 | * @const char *msg@ = banner message |
| 223 | * |
| 224 | * Returns: The encoder object. |
| 225 | * |
| 226 | * Use: Initializes an encoder. |
| 227 | */ |
| 228 | |
| 229 | extern enc *initenc(const encops */*eo*/, FILE */*fp*/, const char */*msg*/); |
| 230 | |
| 231 | /* --- @initdec@ --- * |
| 232 | * |
| 233 | * Arguments: @const encops *eo@ = operations (from @getenc@) |
| 234 | * @FILE *fp@ = file handle to attach |
| 235 | * @int (*func)(const char *, void *)@ = banner check function |
| 236 | * @void *p@ = argument for @func@ |
| 237 | * |
| 238 | * Returns: The encoder object. |
| 239 | * |
| 240 | * Use: Initializes an encoder. |
| 241 | */ |
| 242 | |
| 243 | extern enc *initdec(const encops */*eo*/, FILE */*fp*/, |
| 244 | int (*/*func*/)(const char *, void *), void */*p*/); |
| 245 | |
| 246 | /* --- @freeenc@ --- * |
| 247 | * |
| 248 | * Arguments: @enc *e@ = encoder object |
| 249 | * |
| 250 | * Returns: --- |
| 251 | * |
| 252 | * Use: Frees an encoder object. |
| 253 | */ |
| 254 | |
| 255 | extern void freeenc(enc */*e*/); |
| 256 | |
| 257 | /* --- @cmd_encode@, @cmd_decode@ --- */ |
| 258 | |
| 259 | #define CMD_ENCODE { \ |
| 260 | "encode", cmd_encode, \ |
| 261 | "encode [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \ |
| 262 | "\ |
| 263 | Options:\n\ |
| 264 | \n\ |
| 265 | -f, --format=FORMAT Encode to FORMAT.\n\ |
| 266 | -b, --boundary=LABEL PEM boundary is LABEL.\n\ |
| 267 | -o, --output=FILE Write output to FILE.\n\ |
| 268 | " } |
| 269 | |
| 270 | #define CMD_DECODE { \ |
| 271 | "decode", cmd_decode, \ |
| 272 | "decode [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \ |
| 273 | "\ |
| 274 | Options:\n\ |
| 275 | \n\ |
| 276 | -f, --format=FORMAT Decode from FORMAT.\n\ |
| 277 | -b, --boundary=LABEL PEM boundary is LABEL.\n\ |
| 278 | -o, --output=FILE Write output to FILE.\n\ |
| 279 | " } |
| 280 | |
| 281 | extern int cmd_encode(int /*argc*/, char */*argv*/[]); |
| 282 | extern int cmd_decode(int /*argc*/, char */*argv*/[]); |
| 283 | |
| 284 | /* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- * |
| 285 | * |
| 286 | * Produce list of things. Requires @i@ and @w@ variables in scope. |
| 287 | * END-TEST and NAME-EXPR are in terms of @i@. |
| 288 | */ |
| 289 | |
| 290 | #define LIST(what, fp, end, name) do { \ |
| 291 | fputs(what ":\n ", fp); \ |
| 292 | w = 2; \ |
| 293 | for (i = 0; end; i++) { \ |
| 294 | if (w == 2) \ |
| 295 | w += strlen(name); \ |
| 296 | else { \ |
| 297 | if (strlen(name) + w > 76) { \ |
| 298 | fputs("\n ", fp); \ |
| 299 | w = 2 + strlen(name); \ |
| 300 | } else { \ |
| 301 | fputc(' ', fp); \ |
| 302 | w += strlen(name) + 1; \ |
| 303 | } \ |
| 304 | } \ |
| 305 | fputs(name, fp); \ |
| 306 | } \ |
| 307 | fputc('\n', fp); \ |
| 308 | } while (0) |
| 309 | |
| 310 | #define STDLISTS(LI) \ |
| 311 | LI("Hash functions", hash, \ |
| 312 | ghashtab[i], ghashtab[i]->name) \ |
| 313 | LI("Encryption schemes", enc, \ |
| 314 | gciphertab[i], gciphertab[i]->name) \ |
| 315 | LI("Message authentication schemes", mac, \ |
| 316 | gmactab[i], gmactab[i]->name) \ |
| 317 | LI("Elliptic curves", ec, \ |
| 318 | ectab[i].name, ectab[i].name) \ |
| 319 | LI("Diffie-Hellman groups", dh, \ |
| 320 | ptab[i].name, ptab[i].name) |
| 321 | |
| 322 | #define LIDECL(text, tag, test, name) \ |
| 323 | static void show_##tag(void); |
| 324 | |
| 325 | #define LIDEF(text, tag, test, name) \ |
| 326 | static void show_##tag(void) \ |
| 327 | { \ |
| 328 | unsigned i, w; \ |
| 329 | LIST(text, stdout, test, name); \ |
| 330 | } |
| 331 | |
| 332 | #define LIENT(text, tag, test, name) \ |
| 333 | { #tag, show_##tag }, |
| 334 | |
| 335 | struct listent { |
| 336 | const char *name; |
| 337 | void (*list)(void); |
| 338 | }; |
| 339 | |
| 340 | #define MAKELISTTAB(listtab, LISTS) \ |
| 341 | LISTS(LIDECL) \ |
| 342 | static const struct listent listtab[] = { \ |
| 343 | LISTS(LIENT) \ |
| 344 | { 0, 0 } \ |
| 345 | }; \ |
| 346 | LISTS(LIDEF) |
| 347 | |
| 348 | extern int displaylists(const struct listent */*listtab*/, |
| 349 | char *const /*argv*/[]); |
| 350 | |
| 351 | /*----- Subcommand dispatch -----------------------------------------------*/ |
| 352 | |
| 353 | typedef struct cmd { |
| 354 | const char *name; |
| 355 | int (*cmd)(int /*argc*/, char */*argv*/[]); |
| 356 | const char *usage; |
| 357 | const char *help; |
| 358 | } cmd; |
| 359 | |
| 360 | extern void version(FILE */*fp*/); |
| 361 | extern void help_global(FILE */*fp*/); |
| 362 | |
| 363 | /* --- @findcmd@ --- * |
| 364 | * |
| 365 | * Arguments: @const cmd *cmds@ = pointer to command table |
| 366 | * @const char *name@ = a command name |
| 367 | * |
| 368 | * Returns: Pointer to the command structure. |
| 369 | * |
| 370 | * Use: Looks up a command by name. If the command isn't found, an |
| 371 | * error is reported and the program is terminated. |
| 372 | */ |
| 373 | |
| 374 | const cmd *findcmd(const cmd */*cmds*/, const char */*name*/); |
| 375 | |
| 376 | /* --- @sc_help@ --- * |
| 377 | * |
| 378 | * Arguments: @const cmd *cmds@ = pointer to command table |
| 379 | * @FILE *fp@ = output file handle |
| 380 | * @char *const *argv@ = remaining arguments |
| 381 | * |
| 382 | * Returns: --- |
| 383 | * |
| 384 | * Use: Prints a help message, maybe with help about subcommands. |
| 385 | */ |
| 386 | |
| 387 | extern void sc_help(const cmd */*cmds*/, FILE */*fp*/, |
| 388 | char *const */*argv*/); |
| 389 | |
| 390 | /*----- That's all, folks -------------------------------------------------*/ |
| 391 | |
| 392 | #ifdef __cplusplus |
| 393 | } |
| 394 | #endif |
| 395 | |
| 396 | #endif |