1 /* Copyright (C) 2014 2015 Jason Self <j@jxself.org>
3 This file is part of skeinsum.
5 skeinsum is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 skeinsum is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with skeinsum. If not, see <http://www.gnu.org/licenses/>.
32 #include "SHA3api_ref.h"
34 #define WARN(msg, ...) fprintf(stderr, "skein%dsum: " msg, hashbitlen, ##__VA_ARGS__)
36 #define TRYHELP_GOODBYE() do { printf("Try 'skein%dsum --help' for more information.\n", hashbitlen); exit(1); } while(0)
38 typedef long long unsigned LLU;
40 extern const int hashbitlen;
42 #define skeinVersion "1.3"
43 const size_t input_minbufsize = 32 * 1024;
44 const size_t input_maxbufsize = 32 * 1024 * 1024;
47 char invalidOption = 0;
56 static struct option const long_options[] =
58 { "binary", no_argument, NULL, 'b' },
59 { "check", no_argument, NULL, 'c' },
60 { "quiet", no_argument, NULL, QUIET_OPTION },
61 { "status", no_argument, NULL, STATUS_OPTION },
62 { "text", no_argument, NULL, 't' },
63 { "warn", no_argument, NULL, 'w' },
64 { "tag", no_argument, NULL, TAG_OPTION },
65 { "help", no_argument, NULL, 'h' },
66 { "version", no_argument, NULL, 'V' },
70 void hash2hexstr(unsigned char hash[], char str[])
73 for (i = 0; i < hashbitlen / 8; i++) {
74 sprintf(&str[i * 2], "%02X", hash[i]);
76 str[i * 2 + 1] = '\0';
79 int HashFile(const char file_name[], char MsgDigest[], char mode)
81 int is_stdin = (strcmp(file_name, "-") == 0);
83 /* Try to get file info */
85 st.st_size = 0; /* ..needed when reading from stdio */
86 if (!is_stdin && stat(file_name, &st) < 0) {
87 WARN("%s: cannot stat: %s\n", file_name, strerror(errno));
92 size_t fsize = st.st_size;
93 if (fsize != st.st_size) {
94 WARN("%s: SIZE WARNING: filesize %llu is too big for reading into memory!\n",
95 file_name, (long long unsigned)st.st_size);
99 FILE *fp_in = is_stdin ? stdin : fopen(file_name, (mode == 't' ? "r" : "rb") );
101 WARN("%s: cannot open: %s\n", file_name, strerror(errno));
105 /* Allocate buffer */
106 size_t bufsize = fsize + 1;
107 if (bufsize < input_minbufsize)
108 bufsize = input_minbufsize;
110 char *readbuf = malloc (bufsize);
111 if (!readbuf && bufsize > input_maxbufsize) { /* ..Try to get contents by smaller portions */
112 bufsize = input_maxbufsize;
113 readbuf = malloc (bufsize);
117 WARN("%s: MEM FAILED: error %s\n", file_name, strerror(errno));
124 size_t readpos = 0, total_readed = 0;
127 size_t maxread = bufsize - readpos;
128 size_t readed = fread(readbuf + readpos, 1, maxread, fp_in);
129 if (readed > 0 && readed <= maxread)
130 total_readed += readed;
131 if (readed != maxread)
133 if (getenv("SKEIN_DEBUG"))
134 printf("DEBUG: bufsize=%llu (0x%llx), readpos=%llu (0x%llx), maxread=%llu (0x%llx), total=%llu (0x%llx)\n",
135 (LLU)bufsize, (LLU)bufsize, (LLU)readpos, (LLU)readpos,
136 (LLU)maxread, (LLU)maxread, (LLU)total_readed, (LLU)total_readed);
138 if (bufsize * 2 > bufsize) /* ..check overflow */
139 newbuf = realloc(readbuf, bufsize * 2);
141 if (total_readed < st.st_size) {
142 WARN("%s: MEM WARNING: %llu bytes only readed from %llu\n",
143 file_name, (LLU)total_readed, (LLU)st.st_size);
145 WARN("%s: MEM WARNING: %llu bytes only readed.\n",
146 file_name, (LLU)total_readed);
158 if (!is_stdin && total_readed < st.st_size && total_readed < bufsize) {
159 WARN("%s: READ WARNING: filesize=%llu, readed=%llu, error %d, %s\n",
160 file_name, (LLU)st.st_size, (LLU)total_readed, errno, strerror(errno));
163 if (getenv("SKEIN_DUMP")) {
164 printf("DEBUG: file=%s, bufsize=%llu (0x%llx), total_readed=%llu, buf:\n",
165 file_name, (LLU)bufsize, (LLU)bufsize, (LLU)total_readed);
166 fwrite(readbuf, 1, total_readed, stdout);
169 unsigned char output[hashbitlen/4];
170 Hash(hashbitlen, (unsigned char*) readbuf, total_readed, output);
174 hash2hexstr(output, MsgDigest);
179 int PrintFileHash(const char filename[], int tag, char mode)
181 char MsgDigest[hashbitlen/2];
182 if (HashFile(filename, MsgDigest, mode) < 0)
185 printf("skein%d_v%s (%s) = %s\n", hashbitlen, skeinVersion, filename, MsgDigest);
187 printf("%s %s%s\n", MsgDigest, (mode == 'b' ? "*" : ""), filename);
192 /* Return: -1 = Error, 0 = Mismatch, 1 = Match */
193 int HashMatch(const char StoredDigest[], const char *filename, int quiet)
196 if (filename[0] == '*') {
201 char MsgDigest[hashbitlen/2];
202 if (HashFile(filename, MsgDigest, mode) < 0)
205 if (strcmp(MsgDigest, StoredDigest)) {
206 printf("%s: FAILED\n", filename);
211 printf("%s: OK\n", filename);
215 int isProper(const char MsgDigest[])
217 int len = strlen(MsgDigest);
218 if (len != (hashbitlen / 4))
221 for (index = 0; index < len; index++)
223 char c = MsgDigest[index];
224 if (c >= '0' && c <= '9') continue;
225 if (c >= 'A' && c <= 'F') continue;
233 int decomposeHashLine(char hash[], char MsgDigest_tmp[], char file_tmp[])
240 while(((c = hash[i])!=' ')&&((c = hash[i])!='_'))
247 sprintf(alg,"skein%d",hashbitlen);
255 strcpy(MsgDigest_tmp,tmp);
257 while((c = hash[i])!= '\n')
259 file_tmp[j] = hash[i];
265 else if((hash[i]=='_')&&(hash[i+1]=='v'))
270 while((c = hash[i])!=' ')
272 version[j] = hash[i];
277 float vers = 0, skeinVers = 0;
278 sscanf(version,"%f",&vers);
279 sscanf(skeinVersion,"%f",&skeinVers);
283 while((c = hash[i])!=')')
285 file_tmp[j] = hash[i];
293 while((c = hash[i])!='\n')
295 MsgDigest_tmp[j] = hash[i];
299 MsgDigest_tmp[j] = 0;
302 {//version newer than mine
305 else if (skeinVers > vers)
306 {//going to use older version than mine
318 /* Return: -1 = some errors/mismatches, 1 = all ok */
319 int VerifyHashesFromFile(FILE *fp, int status, int warn, int quiet)
321 char hash[PATH_MAX + hashbitlen/4 + 4];
322 char MsgDigest_tmp[hashbitlen/2];
323 int NoMatch = 0, NotProper = 0, Computed = 0;
326 while (fgets(hash, sizeof(hash)-1, fp))
328 char file_tmp[PATH_MAX];
331 int hashVersion = decomposeHashLine(hash,MsgDigest_tmp,file_tmp);
332 if (hashVersion == -1)
334 fprintf(stderr, "skein%d: %s is using newer version of skein%d algorithm\n",hashbitlen,file_tmp,hashbitlen);
335 fprintf(stderr, "You should update your algorithm\n");
338 else if (hashVersion == 0)
340 fprintf(stderr, "skein%d: %s is using an older version of skein%d algorithm\n",hashbitlen,file_tmp,hashbitlen);
341 fprintf(stderr, "You should use the older algorithm\n");
344 else if (!isProper(MsgDigest_tmp))
346 if(status != 1 && warn == 1)
347 WARN("%s: %d: improperly formatted skein%d checksum line\n",file_tmp,line,hashbitlen);
351 else if (HashMatch(MsgDigest_tmp, file_tmp, quiet) <= 0)
358 WARN("WARNING: %d of %d computed checksums did NOT match\n",
363 WARN("WARNING: %d line is improperly formatted\n", NotProper);
365 return (NotProper || NoMatch) ? -1 : 1;
368 void print_version(void)
370 printf("skein%dsum 1.0\n", hashbitlen);
371 printf("License GPLv3+: GNU GPL version 3 or later\n");
372 printf("<http://gnu.org/licenses/gpl.html>\n");
373 printf("This is free software: you are free to change and redistribute it.\n");
374 printf("There is NO WARRANTY, to the extent permitted by law.\n");
378 void print_usage(void)
380 printf("Usage: skein%dsum [OPTION]... [FILE]...\n",hashbitlen);
381 printf("Print or check skein (%d-bit) checksums.\n",hashbitlen);
382 printf("With no FILE, or when FILE is -, read standard input.\n");
384 printf("-b, --binary read in binary mode\n");
385 printf("-c, --check read skein sums from the FILEs and check them\n");
386 printf("--tag create a BSD-style checksum\n");
387 printf("-t, --text read in text mode (default)\n");
389 printf("The following three options are useful only when verifying checksums:\n");
390 printf("--quiet don't print OK for each successfully verified file\n");
391 printf("--status don't output anything, status code shows success\n");
392 printf("-w, --warn warn about improperly formatted checksum lines\n");
394 printf("--strict with --check, exit non-zero for any invalid input\n");
395 printf("--help display this help and exit\n");
396 printf("--version output version information and exit\n");
398 printf("The sums are computed as described in version 1.3 of the Skein\n");
399 printf("specification. When checking, the input should be a former output of\n");
400 printf("this program. The default mode is to print a line with checksum, a\n");
401 printf("character indicating input mode ('*' for binary, space for text), and\n");
402 printf("name for each FILE.\n");
406 int is_goodfile(const char filename[])
408 if (!strcmp(filename, "-"))
413 if (stat(filename, &s) < 0) {
414 WARN("%s: no such file or directory\n", filename);
418 if (S_ISREG (s.st_mode)) return 1;
419 if (S_ISCHR (s.st_mode)) return 1;
420 if (S_ISBLK (s.st_mode)) return 1;
421 if (S_ISLNK (s.st_mode)) return 1;
422 if (S_ISFIFO(s.st_mode)) return 1;
424 if (S_ISDIR (s.st_mode)) {
425 WARN("%s: is a directory\n", filename);
429 WARN("%s: WARNING: unknown filetype 0x%Xu\n", filename, s.st_mode);
430 return 1; /* try it as good */
434 int main(int argc, char** argv)
436 int first_file = argc;
447 /*****************************************************************************************
448 ************************************* GETTING DATA ***********************************
449 *****************************************************************************************/
450 while ((opt = getopt_long (argc, argv, "hVbctw", long_options, NULL)) != -1)
453 case 0 : hashString = 1; break;
454 case 'b' : binary = 1; break;
455 case 't' : binary = 0; break;
456 case 'c' : check = 1; break;
457 case 'w' : warn = 1; break;
458 case QUIET_OPTION : quiet = 1; break;
459 case STATUS_OPTION : status = 1; break;
460 case TAG_OPTION : tag = 1; break;
462 case 'h' : print_usage(); /* ..never returns */
463 case 'V' : print_version(); /* ..never returns */
465 default: TRYHELP_GOODBYE();
471 /*****************************************************************************************
472 ************************************* PROCESSING DATA ***********************************
473 *****************************************************************************************/
475 if (check < 0) /* READ FILES, GENERATE CHECKSUMS AND PRINT TO STDOUT.. */
477 if (quiet == 1 || warn == 1 || status == 1)
480 WARN("the --quiet option is meaningful only when verifying checksums\n");
482 WARN("the --status option is meaningful only when verifying checksums\n");
484 WARN("the --warn option is meaningful only when verifying checksums\n");
488 char mode = binary > 0 ? 'b' : 't';
491 for (file_index = first_file; file_index < argc; file_index++)
493 const char *filename = argv[file_index];
494 if (!is_goodfile(filename)) {
499 if (binary > 0 && hashString == 1) {
500 WARN("%s: No such file or directory\n", filename);
504 if (PrintFileHash(filename, tag, mode) < 0)
508 if (first_file >= argc) /* no files in cmdline? hash stdin.. */
509 if (PrintFileHash("-", tag, mode) < 0)
512 return (errorFound ? 1 : 0);
515 if (check == 1) /* READ LISTFILES, GENERATE HASHES, COMPARE WITH STORED, PRINT RESULT */
517 if (tag == 1 || binary >= 0)
520 WARN("the --tag option is meaningless when verifying checksums\n");
522 WARN("the --text and --binary options are meaningless when verifying checksums\n");
527 for (file_index = first_file; file_index < argc; file_index++)
529 const char *filename = argv[file_index];
530 if (!is_goodfile(filename)) {
535 FILE *fp = (strcmp(filename, "-") == 0) ? stdin : fopen(filename, "r");
538 WARN("%s: %s\n", filename, strerror(errno));
541 if (VerifyHashesFromFile(fp, status, warn, quiet) < 0)
547 if (first_file >= argc) /* no files in cmdline? verify stdin.. */
548 if (VerifyHashesFromFile(stdin, status, warn, quiet) < 0)
551 return (errorFound ? 1 : 0);