tests/Makefile.m4: Distribute the converted AES test-vector files.
[u/mdw/catacomb] / cc-hash.c
CommitLineData
18b3351a
MW
1/* -*-c-*-
2 *
3 * Common functions for hashing utilities
4 *
5 * (c) 2011 Straylight/Edgeware
6 */
7
8/*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28/*----- Header files ------------------------------------------------------*/
29
30#define _FILE_OFFSET_BITS 64
31
32#include "config.h"
33
34#include <ctype.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38
39#include <mLib/dstr.h>
f377eee1 40#include <mLib/str.h>
18b3351a
MW
41
42#include <mLib/hex.h>
43#include <mLib/base32.h>
44#include <mLib/base64.h>
45
46#include "ghash.h"
47#include "cc.h"
48
49/*----- Encoding and decoding ---------------------------------------------*/
50
51/* --- Hex encoding --- */
52
53static void puthex(const octet *buf, size_t sz, FILE *fp)
54{
55 while (sz) {
56 fprintf(fp, "%02x", *buf++);
57 sz--;
58 }
59}
60
61static size_t gethex(const char *p, octet *q, size_t sz, char **pp)
62{
63 size_t i = 0;
64 while (sz > 0 &&
65 isxdigit((unsigned char)p[0]) &&
66 isxdigit((unsigned char)p[1])) {
67 char buf[3];
68 buf[0] = p[0];
69 buf[1] = p[1];
70 buf[2] = 0;
71 *q++ = strtoul(buf, 0, 16);
72 sz--;
73 p += 2;
74 i++;
75 }
76 if (pp)
77 *pp = (char *)p;
78 return (i);
79}
80
81/* --- Base64 encoding --- */
82
83static void putbase64(const octet *buf, size_t sz, FILE *fp)
84{
85 base64_ctx b;
86 dstr d = DSTR_INIT;
87
88 base64_init(&b);
89 b.indent = "";
90 b.maxline = 0;
91 base64_encode(&b, buf, sz, &d);
92 base64_encode(&b, 0, 0, &d);
93 dstr_write(&d, fp);
94 dstr_destroy(&d);
95}
96
97static size_t getbase64(const char *p, octet *q, size_t sz, char **pp)
98{
99 base64_ctx b;
100 dstr d = DSTR_INIT;
101 size_t n = strlen(p);
102
103 base64_init(&b);
104 base64_decode(&b, p, n, &d);
105 if (pp) *pp = (/*unconst*/ char *)p + n;
106 base64_decode(&b, 0, 0, &d);
107 assert(d.len <= sz);
108 memcpy(q, d.buf, sz);
109 n = d.len;
110 dstr_destroy(&d);
111 return (n);
112}
113
114/* --- Base32 encoding --- */
115
116static void putbase32(const octet *buf, size_t sz, FILE *fp)
117{
118 base32_ctx b;
119 dstr d = DSTR_INIT;
120
121 base32_init(&b);
122 b.indent = "";
123 b.maxline = 0;
124 base32_encode(&b, buf, sz, &d);
125 base32_encode(&b, 0, 0, &d);
126 dstr_write(&d, fp);
127 dstr_destroy(&d);
128}
129
130static size_t getbase32(const char *p, octet *q, size_t sz, char **pp)
131{
132 base32_ctx b;
133 dstr d = DSTR_INIT;
134 size_t n = strlen(p);
135
136 base32_init(&b);
137 base32_decode(&b, p, n, &d);
138 if (pp) *pp = (/*unconst*/ char *)p + n;
139 base32_decode(&b, 0, 0, &d);
140 assert(d.len <= sz);
141 memcpy(q, d.buf, sz);
142 n = d.len;
143 dstr_destroy(&d);
144 return (n);
145}
146
147/* --- Table --- */
148
149const encodeops encodingtab[] = {
150#define TAB(tag, name) { #name, put##name, get##name },
151 ENCODINGS(TAB)
152#undef TAB
153 { 0, 0, 0 }
154};
155
156const encodeops *getencoding(const char *ename)
157{
158 const encodeops *e;
159
160 for (e = encodingtab; e->name; e++) {
161 if (strcmp(ename, e->name) == 0)
162 return (e);
163 }
164 return (0);
165}
166
167/*----- File hashing ------------------------------------------------------*/
168
f377eee1
MW
169/* --- @gethash@ --- *
170 *
171 * Arguments: @const char *name@ = pointer to name string
172 *
173 * Returns: Pointer to appropriate hash class.
174 *
175 * Use: Chooses a hash function by name.
176 */
177
178const gchash *gethash(const char *name)
179{
180 const gchash *const *g, *gg = 0;
181 size_t sz = strlen(name);
182 for (g = ghashtab; *g; g++) {
183 if (strncmp(name, (*g)->name, sz) == 0) {
184 if ((*g)->name[sz] == 0) {
185 gg = *g;
186 break;
187 } else if (gg)
188 return (0);
189 else
190 gg = *g;
191 }
192 }
193 return (gg);
194}
195
18b3351a
MW
196/* --- @fhash@ --- *
197 *
198 * Arguments: @const gchash *gch@ = pointer to hash function to use
199 * @unsigned f@ = flags to set
200 * @const char *file@ = file name to be hashed (null for stdin)
201 * @void *buf@ = pointer to hash output buffer
202 *
203 * Returns: Zero if it worked, nonzero on error.
204 *
205 * Use: Hashes a file.
206 */
207
208int fhash(const gchash *gch, unsigned f, const char *file, void *buf)
209{
210 FILE *fp;
211 char fbuf[1024 * 128];
212 size_t sz;
213 ghash *h;
214 int rc = 0;
215 fprogress ff;
216
217 if (!file || strcmp(file, "-") == 0)
218 fp = stdin;
219 else if ((fp = fopen(file, f & FHF_BINARY ? "rb" : "r")) == 0)
220 return (-1);
221
222 if (f & FHF_PROGRESS) {
223 if (fprogress_init(&ff, file, fp)) return (-1);
224 }
225
226 h = GH_INIT(gch);
227 while ((sz = fread(fbuf, 1, sizeof(fbuf), fp)) > 0) {
228 GH_HASH(h, fbuf, sz);
229 if (f & FHF_PROGRESS) fprogress_update(&ff, sz);
230 }
231 if (ferror(fp)) rc = -1;
232 if (fp != stdin) fclose(fp);
233 if (f & FHF_PROGRESS) fprogress_done(&ff);
234 GH_DONE(h, buf);
235 GH_DESTROY(h);
236 return (rc);
237}
238
f377eee1
MW
239/* --- @hfparse@ --- *
240 *
241 * Arguments: @hfpctx *hfp@ = pointer to the context structure
242 *
243 * Returns: A code indicating what happened.
244 *
245 * Use: Parses a line from the input file.
246 */
247
248int hfparse(hfpctx *hfp)
249{
250 char *p, *q;
251 const gchash *gch;
252 const encodeops *ee;
253 dstr *d = hfp->dline;
254 size_t hsz;
255
256 /* --- Fetch the input line and get ready to parse --- */
257
258 DRESET(d);
259 if (dstr_putline(d, hfp->fp) == EOF) return (HF_EOF);
260 p = d->buf;
261
262 /* --- Parse magic comments --- */
263
264 if (*p == '#') {
265 p++;
266 if ((q = str_getword(&p)) == 0) return (HF_BAD);
267 if (strcmp(q, "hash") == 0) {
268 if ((q = str_getword(&p)) == 0) return (HF_BAD);
269 if ((gch = gethash(q)) == 0) return (HF_BAD);
270 hfp->gch = gch;
271 return (HF_HASH);
272 } else if (strcmp(q, "encoding") == 0) {
273 if ((q = str_getword(&p)) == 0) return (HF_BAD);
274 if ((ee = getencoding(q)) == 0) return (HF_BAD);
275 hfp->ee = ee;
276 return (HF_ENC);
277 } else if (strcmp(q, "escape") == 0) {
278 hfp->f |= HFF_ESCAPE;
279 return (HF_ESC);
280 }
281 return (HF_BAD);
282 }
283
284 /* --- Otherwise it's a file line --- */
285
286 q = p;
287 while (*p && *p != ' ') p++;
288 if (!*p) return (HF_BAD);
289 *p++ = 0;
290 hsz = hfp->gch->hashsz;
291 if (hfp->ee->get(q, hfp->hbuf, hsz, 0) < hsz) return (HF_BAD);
292 switch (*p) {
293 case '*': hfp->f |= FHF_BINARY; break;
294 case ' ': hfp->f &= ~FHF_BINARY; break;
295 default: return (HF_BAD);
296 }
297 p++;
298
299 DRESET(hfp->dfile);
300 if (hfp->f & HFF_ESCAPE)
301 getstring(&p, hfp->dfile, GSF_STRING);
302 else {
303 dstr_putm(hfp->dfile, p, d->len - (p - d->buf));
304 dstr_putz(hfp->dfile);
305 }
306
307 return (HF_FILE);
308}
309
18b3351a
MW
310/*----- String I/O --------------------------------------------------------*/
311
312/* --- @getstring@ --- *
313 *
314 * Arguments: @void *in@ = input source
315 * @dstr *d@ = destination string
316 * @unsigned f@ = input flags
317 *
318 * Returns: Zero if OK, nonzero on end-of-file.
319 *
320 * Use: Reads a filename (or something similar) from a stream.
321 */
322
323static int nextch_file(void *in)
324 { FILE *fp = in; return (getc(fp)); }
325
326static int nextch_string(void *in)
327 { const unsigned char **p = in; return (*(*p)++); }
328
329int getstring(void *in, dstr *d, unsigned f)
330{
331 int ch;
332 int eofch = (f & GSF_STRING) ? 0 : EOF;
333 int (*nextch)(void *) = (f & GSF_STRING) ? nextch_string : nextch_file;
334 int q = 0;
335
336 /* --- Raw: just read exactly what's written up to a null byte --- */
337
338 if (f & GSF_RAW) {
339 if ((ch = nextch(in)) == eofch)
340 return (EOF);
341 for (;;) {
342 if (!ch)
343 break;
344 DPUTC(d, ch);
345 if ((ch = nextch(in)) == eofch)
346 break;
347 }
348 DPUTZ(d);
349 return (0);
350 }
351
352 /* --- Skip as far as whitespace --- *
353 *
354 * Also skip past comments.
355 */
356
357again:
358 ch = nextch(in);
359 while (isspace(ch))
360 ch = nextch(in);
361 if (ch == '#') {
362 do ch = nextch(in); while (ch != '\n' && ch != eofch);
363 goto again;
364 }
365 if (ch == eofch)
366 return (EOF);
367
368 /* --- If the character is a quote then read a quoted string --- */
369
370 switch (ch) {
371 case '`':
372 ch = '\'';
373 case '\'':
374 case '\"':
375 q = ch;
376 ch = nextch(in);
377 break;
378 }
379
380 /* --- Now read all sorts of interesting things --- */
381
382 for (;;) {
383
384 /* --- Handle an escaped thing --- */
385
386 if (ch == '\\') {
387 ch = nextch(in);
388 if (ch == eofch)
389 break;
390 switch (ch) {
391 case 'a': ch = '\a'; break;
392 case 'b': ch = '\b'; break;
393 case 'f': ch = '\f'; break;
394 case 'n': ch = '\n'; break;
395 case 'r': ch = '\r'; break;
396 case 't': ch = '\t'; break;
397 case 'v': ch = '\v'; break;
398 }
399 DPUTC(d, ch);
400 ch = nextch(in);
401 continue;
402 }
403
404 /* --- If it's a quote or some other end marker then stop --- */
405
406 if (ch == q)
407 break;
408 if (!q && isspace(ch))
409 break;
410
411 /* --- Otherwise contribute and continue --- */
412
413 DPUTC(d, ch);
414 if ((ch = nextch(in)) == eofch)
415 break;
416 }
417
418 /* --- Done --- */
419
420 DPUTZ(d);
421 return (0);
422}
423
424/* --- @putstring@ --- *
425 *
426 * Arguments: @FILE *fp@ = stream to write on
427 * @const char *p@ = pointer to text
428 * @unsigned f@ = output flags
429 *
430 * Returns: ---
431 *
432 * Use: Emits a string to a stream.
433 */
434
435void putstring(FILE *fp, const char *p, unsigned f)
436{
437 size_t sz = strlen(p);
438 unsigned qq;
439 const char *q;
440
441 /* --- Just write the string null terminated if raw --- */
442
443 if (f & GSF_RAW) {
444 fwrite(p, 1, sz + 1, fp);
445 return;
446 }
447
448 /* --- Check for any dodgy characters --- */
449
450 qq = 0;
451 for (q = p; *q; q++) {
452 if (isspace((unsigned char)*q)) {
453 qq = '\"';
454 break;
455 }
456 }
457
458 if (qq)
459 putc(qq, fp);
460
461 /* --- Emit the string --- */
462
463 for (q = p; *q; q++) {
464 switch (*q) {
465 case '\a': fputc('\\', fp); fputc('a', fp); break;
466 case '\b': fputc('\\', fp); fputc('b', fp); break;
467 case '\f': fputc('\\', fp); fputc('f', fp); break;
468 case '\n': fputc('\\', fp); fputc('n', fp); break;
469 case '\r': fputc('\\', fp); fputc('r', fp); break;
470 case '\t': fputc('\\', fp); fputc('t', fp); break;
471 case '\v': fputc('\\', fp); fputc('v', fp); break;
472 case '`': fputc('\\', fp); fputc('`', fp); break;
473 case '\'': fputc('\\', fp); fputc('\'', fp); break;
474 case '\"': fputc('\\', fp); fputc('\"', fp); break;
475 default:
476 putc(*q, fp);
477 break;
478 }
479 }
480
481 /* --- Done --- */
482
483 if (qq)
484 putc(qq, fp);
485}
486
487/*----- That's all, folks -------------------------------------------------*/