2013-10-07 16:03:26 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2017-01-02 01:00:33 +00:00
|
|
|
#include <fcntl.h>
|
2014-11-13 18:54:28 +00:00
|
|
|
#include <stdint.h>
|
2013-07-07 14:29:45 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-03-23 11:18:38 +00:00
|
|
|
#include <string.h>
|
2017-01-02 01:00:33 +00:00
|
|
|
#include <unistd.h>
|
2014-11-13 18:54:28 +00:00
|
|
|
|
2013-07-07 14:29:45 +00:00
|
|
|
#include "../crypt.h"
|
2014-11-13 18:54:28 +00:00
|
|
|
#include "../text.h"
|
|
|
|
#include "../util.h"
|
2013-07-07 14:29:45 +00:00
|
|
|
|
2014-03-23 11:18:38 +00:00
|
|
|
static int
|
2014-04-04 10:09:46 +00:00
|
|
|
hexdec(int c)
|
|
|
|
{
|
2014-11-13 18:54:28 +00:00
|
|
|
if (c >= '0' && c <= '9')
|
2014-03-23 11:18:38 +00:00
|
|
|
return c - '0';
|
2014-11-13 18:54:28 +00:00
|
|
|
else if (c >= 'A' && c <= 'F')
|
2014-03-23 11:18:38 +00:00
|
|
|
return c - 'A' + 10;
|
2014-11-13 18:54:28 +00:00
|
|
|
else if (c >= 'a' && c <= 'f')
|
2014-03-23 11:18:38 +00:00
|
|
|
return c - 'a' + 10;
|
|
|
|
return -1; /* unknown character */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-04-04 10:09:46 +00:00
|
|
|
mdcheckline(const char *s, uint8_t *md, size_t sz)
|
|
|
|
{
|
2014-03-23 11:18:38 +00:00
|
|
|
size_t i;
|
|
|
|
int b1, b2;
|
|
|
|
|
2014-11-13 18:54:28 +00:00
|
|
|
for (i = 0; i < sz; i++) {
|
|
|
|
if (!*s || (b1 = hexdec(*s++)) < 0)
|
2014-03-23 11:18:38 +00:00
|
|
|
return -1; /* invalid format */
|
2014-11-13 18:54:28 +00:00
|
|
|
if (!*s || (b2 = hexdec(*s++)) < 0)
|
2014-03-23 11:18:38 +00:00
|
|
|
return -1; /* invalid format */
|
2014-11-13 18:54:28 +00:00
|
|
|
if ((uint8_t)((b1 << 4) | b2) != md[i])
|
2014-03-23 11:18:38 +00:00
|
|
|
return 0; /* value mismatch */
|
|
|
|
}
|
|
|
|
return (i == sz) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2015-03-01 21:51:52 +00:00
|
|
|
static void
|
|
|
|
mdchecklist(FILE *listfp, struct crypt_ops *ops, uint8_t *md, size_t sz,
|
2015-03-27 16:03:44 +00:00
|
|
|
int *formatsucks, int *noread, int *nonmatch)
|
|
|
|
{
|
2017-01-02 01:00:33 +00:00
|
|
|
int fd;
|
2014-03-23 11:18:38 +00:00
|
|
|
size_t bufsiz = 0;
|
2015-03-01 21:51:52 +00:00
|
|
|
int r;
|
|
|
|
char *line = NULL, *file, *p;
|
2014-03-23 11:18:38 +00:00
|
|
|
|
2015-03-27 13:49:48 +00:00
|
|
|
while (getline(&line, &bufsiz, listfp) > 0) {
|
2014-11-13 18:54:28 +00:00
|
|
|
if (!(file = strstr(line, " "))) {
|
2015-03-01 21:51:52 +00:00
|
|
|
(*formatsucks)++;
|
2014-03-23 11:18:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-11-13 18:54:28 +00:00
|
|
|
if ((file - line) / 2 != sz) {
|
2015-03-01 21:51:52 +00:00
|
|
|
(*formatsucks)++; /* checksum length mismatch */
|
2014-03-23 11:18:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*file = '\0';
|
|
|
|
file += 2;
|
2014-11-13 18:54:28 +00:00
|
|
|
for (p = file; *p && *p != '\n' && *p != '\r'; p++); /* strip newline */
|
2014-03-23 11:18:38 +00:00
|
|
|
*p = '\0';
|
2017-01-02 01:00:33 +00:00
|
|
|
if ((fd = open(file, O_RDONLY)) < 0) {
|
|
|
|
weprintf("open %s:", file);
|
2015-03-01 21:51:52 +00:00
|
|
|
(*noread)++;
|
2014-03-23 11:18:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-01-02 01:00:33 +00:00
|
|
|
if (cryptsum(ops, fd, file, md)) {
|
2016-12-06 10:16:53 +00:00
|
|
|
(*noread)++;
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-23 11:18:38 +00:00
|
|
|
r = mdcheckline(line, md, sz);
|
2014-11-13 18:54:28 +00:00
|
|
|
if (r == 1) {
|
2014-03-23 11:18:38 +00:00
|
|
|
printf("%s: OK\n", file);
|
2014-11-13 18:54:28 +00:00
|
|
|
} else if (r == 0) {
|
2014-03-23 11:18:38 +00:00
|
|
|
printf("%s: FAILED\n", file);
|
2015-03-01 21:51:52 +00:00
|
|
|
(*nonmatch)++;
|
2014-03-23 11:18:38 +00:00
|
|
|
} else {
|
2015-03-01 21:51:52 +00:00
|
|
|
(*formatsucks)++;
|
2014-03-23 11:18:38 +00:00
|
|
|
}
|
2017-01-02 01:00:33 +00:00
|
|
|
close(fd);
|
2014-03-23 11:18:38 +00:00
|
|
|
}
|
2014-03-23 12:45:52 +00:00
|
|
|
free(line);
|
2015-03-01 21:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cryptcheck(int argc, char *argv[], struct crypt_ops *ops, uint8_t *md, size_t sz)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int formatsucks = 0, noread = 0, nonmatch = 0, ret = 0;
|
|
|
|
|
|
|
|
if (argc == 0) {
|
|
|
|
mdchecklist(stdin, ops, md, sz, &formatsucks, &noread, &nonmatch);
|
|
|
|
} else {
|
2015-03-02 13:19:26 +00:00
|
|
|
for (; *argv; argc--, argv++) {
|
2016-03-23 18:12:28 +00:00
|
|
|
if ((*argv)[0] == '-' && !(*argv)[1]) {
|
|
|
|
fp = stdin;
|
|
|
|
} else if (!(fp = fopen(*argv, "r"))) {
|
2015-03-01 21:51:52 +00:00
|
|
|
weprintf("fopen %s:", *argv);
|
|
|
|
ret = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mdchecklist(fp, ops, md, sz, &formatsucks, &noread, &nonmatch);
|
2016-03-23 18:12:28 +00:00
|
|
|
if (fp != stdin)
|
|
|
|
fclose(fp);
|
2015-03-01 21:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (formatsucks) {
|
2014-03-23 18:58:43 +00:00
|
|
|
weprintf("%d lines are improperly formatted\n", formatsucks);
|
2014-10-02 22:46:04 +00:00
|
|
|
ret = 1;
|
2014-03-23 11:18:38 +00:00
|
|
|
}
|
2015-03-01 21:51:52 +00:00
|
|
|
if (noread) {
|
2014-03-23 18:58:43 +00:00
|
|
|
weprintf("%d listed file could not be read\n", noread);
|
2014-10-02 22:46:04 +00:00
|
|
|
ret = 1;
|
2014-03-23 11:18:38 +00:00
|
|
|
}
|
2015-03-01 21:51:52 +00:00
|
|
|
if (nonmatch) {
|
2014-03-23 18:58:43 +00:00
|
|
|
weprintf("%d computed checksums did NOT match\n", nonmatch);
|
2014-10-02 22:46:04 +00:00
|
|
|
ret = 1;
|
2014-03-23 11:18:38 +00:00
|
|
|
}
|
2015-03-01 21:51:52 +00:00
|
|
|
|
2014-03-23 11:18:38 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-02 10:17:55 +00:00
|
|
|
int
|
2015-03-01 21:51:52 +00:00
|
|
|
cryptmain(int argc, char *argv[], struct crypt_ops *ops, uint8_t *md, size_t sz)
|
2013-09-02 10:17:55 +00:00
|
|
|
{
|
2017-01-02 01:00:33 +00:00
|
|
|
int fd;
|
2014-10-02 22:46:04 +00:00
|
|
|
int ret = 0;
|
2013-09-02 10:17:55 +00:00
|
|
|
|
|
|
|
if (argc == 0) {
|
2017-01-02 01:00:33 +00:00
|
|
|
if (cryptsum(ops, 0, "<stdin>", md))
|
2016-12-06 10:16:53 +00:00
|
|
|
ret = 1;
|
|
|
|
else
|
|
|
|
mdprint(md, "<stdin>", sz);
|
2013-09-02 10:17:55 +00:00
|
|
|
} else {
|
2015-03-02 13:19:26 +00:00
|
|
|
for (; *argv; argc--, argv++) {
|
2015-05-15 11:28:39 +00:00
|
|
|
if ((*argv)[0] == '-' && !(*argv)[1]) {
|
|
|
|
*argv = "<stdin>";
|
2017-01-02 01:00:33 +00:00
|
|
|
fd = 0;
|
|
|
|
} else if ((fd = open(*argv, O_RDONLY)) < 0) {
|
|
|
|
weprintf("open %s:", *argv);
|
2014-10-02 22:46:04 +00:00
|
|
|
ret = 1;
|
2014-03-23 11:23:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-01-02 01:00:33 +00:00
|
|
|
if (cryptsum(ops, fd, *argv, md))
|
2014-10-02 22:46:04 +00:00
|
|
|
ret = 1;
|
2016-12-06 10:16:53 +00:00
|
|
|
else
|
2014-03-23 11:30:34 +00:00
|
|
|
mdprint(md, *argv, sz);
|
2017-01-02 01:00:33 +00:00
|
|
|
if (fd != 0)
|
|
|
|
close(fd);
|
2013-09-02 10:17:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-15 11:28:39 +00:00
|
|
|
|
2014-03-23 11:30:34 +00:00
|
|
|
return ret;
|
2013-09-02 10:17:55 +00:00
|
|
|
}
|
|
|
|
|
2013-07-07 14:29:45 +00:00
|
|
|
int
|
2017-01-02 01:00:33 +00:00
|
|
|
cryptsum(struct crypt_ops *ops, int fd, const char *f, uint8_t *md)
|
2013-07-07 14:29:45 +00:00
|
|
|
{
|
2013-11-22 14:14:08 +00:00
|
|
|
uint8_t buf[BUFSIZ];
|
2017-01-02 01:00:33 +00:00
|
|
|
ssize_t n;
|
2013-07-07 14:29:45 +00:00
|
|
|
|
|
|
|
ops->init(ops->s);
|
2017-01-02 01:00:33 +00:00
|
|
|
while ((n = read(fd, buf, sizeof(buf))) > 0)
|
2013-07-07 14:29:45 +00:00
|
|
|
ops->update(ops->s, buf, n);
|
2017-01-02 01:00:33 +00:00
|
|
|
if (n < 0) {
|
2014-11-17 16:22:01 +00:00
|
|
|
weprintf("%s: read error:", f);
|
2013-07-07 14:29:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ops->sum(ops->s, md);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mdprint(const uint8_t *md, const char *f, size_t len)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
printf("%02x", md[i]);
|
|
|
|
printf(" %s\n", f);
|
|
|
|
}
|