706091e4c7359c60046db0c46f9f5ab0f4daae03
[skeinsum.git] / skein_cli.c
1 /* Copyright (C) 2014 2015 Jason Self <j@jxself.org>
2
3 This file is part of skeinsum.
4
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.
9
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.
14
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/>.
17 */
18
19 #include "config.h"
20
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <dirent.h>
26 #include <getopt.h>
27 #include <malloc.h>
28 #include <math.h>
29 #include <glob.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32 #include "SHA3api_ref.h"
33
34 #define WARN(msg, ...) fprintf(stderr, "skein%dsum: " msg, hashbitlen, ##__VA_ARGS__)
35
36 #define TRYHELP_GOODBYE() do { printf("Try 'skein%dsum --help' for more information.\n", hashbitlen); exit(1); } while(0)
37
38 typedef long long unsigned LLU;
39
40 extern const int hashbitlen;
41
42 #define skeinVersion "1.3"
43
44 const size_t input_minbufsize = 32 * 1024;
45 const size_t input_maxbufsize = 32 * 1024 * 1024;
46
47
48 enum
49 {
50   QUIET_OPTION = 11,
51   STATUS_OPTION,
52   TAG_OPTION
53 };
54
55 static struct option const long_options[] =
56 {
57   { "binary",  no_argument, NULL, 'b' },
58   { "check",   no_argument, NULL, 'c' },
59   { "quiet",   no_argument, NULL, QUIET_OPTION },
60   { "status",  no_argument, NULL, STATUS_OPTION },
61   { "text",    no_argument, NULL, 't' },
62   { "warn",    no_argument, NULL, 'w' },
63   { "tag",     no_argument, NULL, TAG_OPTION },
64   { "help",    no_argument, NULL, 'h' },
65   { "version", no_argument, NULL, 'V' },
66   { NULL, 0, NULL, 0 }
67 };
68
69 void hash2hexstr(unsigned char hash[], char str[])
70 {
71         int i;
72         for (i = 0; i < hashbitlen / 8; i++) {
73                 sprintf(&str[i * 2], "%02X", hash[i]);
74         }
75         str[i * 2 + 1] = '\0';
76 }
77
78 int HashFile(const char file_name[], char MsgDigest[], char mode)
79 {
80         int is_stdin = (strcmp(file_name, "-") == 0);
81
82         /* Try to get file info */
83         struct stat st;
84         st.st_size = 0;  /* ..needed when reading from stdio */
85         if (!is_stdin && stat(file_name, &st) < 0) {
86                 WARN("%s: cannot stat: %s\n", file_name, strerror(errno));
87                 return -1;
88         }
89
90         /* Get filesize */
91         size_t fsize = st.st_size;
92         if (fsize != st.st_size) {
93                 WARN("%s: SIZE WARNING: filesize %llu is too big for reading into memory!\n",
94                         file_name, (long long unsigned)st.st_size);
95         }
96
97         /* Open file */
98         FILE *fp_in = is_stdin ? stdin : fopen(file_name, (mode == 't' ? "r" : "rb") );
99         if (!fp_in) {
100                 WARN("%s: cannot open: %s\n", file_name, strerror(errno));
101                 return -1;
102         }
103
104         /* Allocate buffer */
105         size_t bufsize = fsize + 1;
106         if (bufsize < input_minbufsize)
107                 bufsize = input_minbufsize;
108
109         char *readbuf = malloc (bufsize);
110         if (!readbuf && bufsize > input_maxbufsize) {  /* ..Try to get contents by smaller portions */
111                 bufsize = input_maxbufsize;
112                 readbuf = malloc (bufsize);
113         }
114
115         if (!readbuf) {
116                 WARN("%s: MEM FAILED: error %s\n", file_name, strerror(errno));
117                 if (!is_stdin)
118                         fclose(fp_in);
119                 return -1;
120         }
121
122         /* Read contents */
123         size_t readpos = 0, total_readed = 0;
124
125         while (1) {
126                 size_t maxread = bufsize - readpos;
127                 size_t readed = fread(readbuf + readpos, 1, maxread, fp_in);
128                 if (readed > 0 && readed <= maxread)
129                         total_readed += readed;
130                 if (readed != maxread)
131                         break;
132                 if (getenv("SKEIN_DEBUG"))
133                         printf("DEBUG: bufsize=%llu (0x%llx), readpos=%llu (0x%llx), maxread=%llu (0x%llx), total=%llu (0x%llx)\n",
134                                 (LLU)bufsize, (LLU)bufsize, (LLU)readpos, (LLU)readpos,
135                                 (LLU)maxread, (LLU)maxread, (LLU)total_readed, (LLU)total_readed);
136                 char *newbuf = NULL;
137                 if (bufsize * 2 > bufsize)  /* ..check overflow */
138                         newbuf = realloc(readbuf, bufsize * 2);
139                 if (!newbuf) {
140                         if (total_readed < st.st_size) {
141                                 WARN("%s: MEM WARNING: %llu bytes only readed from %llu\n",
142                                         file_name, (LLU)total_readed, (LLU)st.st_size);
143                         } else {
144                                 WARN("%s: MEM WARNING: %llu bytes only readed.\n",
145                                         file_name, (LLU)total_readed);
146                         }
147                         break;
148                 }
149                 readbuf = newbuf;
150                 readpos += readed;
151                 bufsize *= 2;
152         }
153
154         if (!is_stdin)
155                 fclose(fp_in);
156
157         if (!is_stdin && total_readed < st.st_size && total_readed < bufsize) {
158                 WARN("%s: READ WARNING: filesize=%llu, readed=%llu, error %d, %s\n",
159                         file_name, (LLU)st.st_size, (LLU)total_readed, errno, strerror(errno));
160         }
161
162         if (getenv("SKEIN_DUMP")) {
163                 printf("DEBUG: file=%s, bufsize=%llu (0x%llx), total_readed=%llu, buf:\n",
164                         file_name, (LLU)bufsize, (LLU)bufsize, (LLU)total_readed);
165                 fwrite(readbuf, 1, total_readed, stdout);
166         }
167
168         unsigned char output[hashbitlen/4];
169         Hash(hashbitlen, (unsigned char*) readbuf, total_readed, output);
170
171         free(readbuf);
172
173         hash2hexstr(output, MsgDigest);
174
175         return 1;
176 }
177
178 int PrintFileHash(const char filename[], int tag, char mode)
179 {
180         char MsgDigest[hashbitlen/2];
181         if (HashFile(filename, MsgDigest, mode) < 0)
182                 return -1;
183         if (tag == 1) {
184                 printf("skein%d_v%s (%s) = %s\n", hashbitlen, skeinVersion, filename, MsgDigest);
185         } else {
186                 printf("%s %s%s\n", MsgDigest, (mode == 'b' ? "*" : ""), filename);
187         }
188         return 1;
189 }
190
191 /* Return: -1 = Error, 0 = Mismatch, 1 = Match */
192 int HashMatch(const char StoredDigest[], const char *filename, int quiet)
193 {
194         char mode = 't';
195         if (filename[0] == '*') {
196                 filename++;
197                 mode = 'b';
198         }
199         
200         char MsgDigest[hashbitlen/2];
201         if (HashFile(filename, MsgDigest, mode) < 0)
202                 return -1;
203
204         if (strcmp(MsgDigest, StoredDigest)) {
205                 printf("%s: FAILED\n", filename);
206                 return 0;
207         }
208
209         if (quiet > 0)
210                 printf("%s: OK\n", filename);
211         return 1;
212 }
213
214 int isProper(const char MsgDigest[])
215 {
216         int len = strlen(MsgDigest);
217         if (len != (hashbitlen / 4))
218                 return 0;
219         int index = 0;
220         for (index = 0; index < len; index++)
221         {
222                 char c = MsgDigest[index];
223                 if (c >= '0' && c <= '9') continue;
224                 if (c >= 'A' && c <= 'F') continue;
225                 return 0;
226         }
227
228         return 1;
229 }
230
231
232 int decomposeHashLine(char hash[], char MsgDigest_tmp[], char file_tmp[])
233 {
234         char c = 0;
235         int i = 0 , j =0;
236         int isTagFile = 0;
237         char alg[20];
238         char tmp[1000];
239         while(((c = hash[i])!=' ')&&((c = hash[i])!='_'))
240         {
241                 tmp[i] = hash[i];
242                 i++;
243         }
244         tmp[i] = 0;
245
246         sprintf(alg,"skein%d",hashbitlen);
247         if(!strcmp(alg,tmp))
248         {
249                 isTagFile = 1;
250         }
251
252         if(isTagFile == 0)
253         {
254                 strcpy(MsgDigest_tmp,tmp);
255                 i++;
256                 while((c = hash[i])!= '\n')
257                 {
258                         file_tmp[j] = hash[i];
259                         i++;
260                         j++;
261                 }
262                 file_tmp[j] = 0;
263         }
264         else if((hash[i]=='_')&&(hash[i+1]=='v'))
265         {
266                 i = i + 2;
267                 j = 0;
268                 char version[5];
269                 while((c = hash[i])!=' ')
270                 {
271                         version[j] = hash[i];
272                         i++;
273                         j++;
274                 }
275                 version[i] = 0;
276                 float vers = 0, skeinVers = 0;
277                 sscanf(version,"%f",&vers);
278                 sscanf(skeinVersion,"%f",&skeinVers);
279
280                 j = 0;
281                 i = i + 2;
282                 while((c = hash[i])!=')')
283                 {
284                         file_tmp[j] = hash[i];
285                         i++;
286                         j++;
287                 }
288                 file_tmp[j] = 0;
289
290                 i = i + 4;
291                 j = 0;
292                 while((c = hash[i])!='\n')
293                 {
294                         MsgDigest_tmp[j] = hash[i];
295                         i++;
296                         j++;
297                 }
298                 MsgDigest_tmp[j] = 0;
299
300                 if(skeinVers < vers)
301                 {//version newer than mine
302                         return (-1);
303                 }
304                 else if (skeinVers > vers)
305                 {//going to use older version than mine
306                         return (0) ;
307                 }
308                 else
309                 { //versions match
310                         return (1);
311                 }
312         }
313         return 1;
314
315 }
316
317 /* Return: -1 = some errors/mismatches, 1 = all ok */
318 int VerifyHashesFromFile(FILE *fp, int status, int warn, int quiet)
319 {
320         char hash[PATH_MAX + hashbitlen/4 + 4];
321         char MsgDigest_tmp[hashbitlen/2];
322         int NoMatch = 0, NotProper = 0, Computed = 0;
323         int line = 0;
324
325         while (fgets(hash, sizeof(hash)-1, fp))
326         {
327                 char file_tmp[PATH_MAX];
328                 line ++;
329                 Computed++;
330                 int hashVersion = decomposeHashLine(hash,MsgDigest_tmp,file_tmp);
331                 if (hashVersion == -1)
332                 {
333                         WARN("%s is using newer version of skein%d algorithm\n"
334                              "You should update your algorithm\n",
335                              file_tmp, hashbitlen);
336                         continue;
337                 }
338                 else if (hashVersion == 0)
339                 {
340                         WARN("%s is using an older version of skein%d algorithm\n"
341                              "You should use the older algorithm\n",
342                              file_tmp, hashbitlen);
343                         continue;
344                 }
345                 else if (!isProper(MsgDigest_tmp))
346                 {
347                         if(status != 1 && warn == 1)
348                                 WARN("%s: %d: improperly formatted skein%d checksum line\n",file_tmp,line,hashbitlen);
349                         NotProper ++;
350                         NoMatch ++;
351                 }
352                 else if (HashMatch(MsgDigest_tmp, file_tmp, quiet) <= 0)
353                 {
354                         NoMatch++;
355                 }
356         }
357         if(NoMatch)
358         {
359                 WARN("WARNING: %d of %d computed checksums did NOT match\n",
360                         NoMatch, Computed);
361         }
362         if(NotProper)
363         {
364                 WARN("WARNING: %d line is improperly formatted\n", NotProper);
365         }
366         return (NotProper || NoMatch) ? -1 : 1;
367 }
368
369 void print_version(void)
370 {
371         printf("skein%dsum 1.0\n", hashbitlen);
372         printf("License GPLv3+: GNU GPL version 3 or later\n");
373         printf("<http://gnu.org/licenses/gpl.html>\n");
374         printf("This is free software: you are free to change and redistribute it.\n");
375         printf("There is NO WARRANTY, to the extent permitted by law.\n");
376         exit(1);
377 }
378
379 void print_usage(void)
380 {
381         printf("Usage: skein%dsum [OPTION]... [FILE]...\n",hashbitlen);
382         printf("Print or check skein (%d-bit) checksums.\n",hashbitlen);
383         printf("With no FILE, or when FILE is -, read standard input.\n");
384         printf("\n");
385         printf("-b, --binary         read in binary mode\n");
386         printf("-c, --check          read skein sums from the FILEs and check them\n");
387         printf("--tag            create a BSD-style checksum\n");
388         printf("-t, --text           read in text mode (default)\n");
389         printf("\n");
390         printf("The following three options are useful only when verifying checksums:\n");
391         printf("--quiet          don't print OK for each successfully verified file\n");
392         printf("--status         don't output anything, status code shows success\n");
393         printf("-w, --warn           warn about improperly formatted checksum lines\n");
394         printf("\n");
395         printf("--strict         with --check, exit non-zero for any invalid input\n");
396         printf("--help     display this help and exit\n");
397         printf("--version  output version information and exit\n");
398         printf("\n");
399         printf("The sums are computed as described in version 1.3 of the Skein\n");
400         printf("specification. When checking, the input should be a former output of\n");
401         printf("this program. The default mode is to print a line with checksum, a\n");
402         printf("character indicating input mode ('*' for binary, space for text), and\n");
403         printf("name for each FILE.\n");
404         exit(1);
405 }
406
407 int is_goodfile(const char filename[])
408 {
409         if (!strcmp(filename, "-"))
410                 return 1;
411
412         struct stat s;
413
414         if (stat(filename, &s) < 0) {
415                 WARN("%s: no such file or directory\n", filename);
416                 return 0;
417         }
418
419         if (S_ISREG (s.st_mode)) return 1;
420         if (S_ISCHR (s.st_mode)) return 1;
421         if (S_ISBLK (s.st_mode)) return 1;
422         if (S_ISLNK (s.st_mode)) return 1;
423         if (S_ISFIFO(s.st_mode)) return 1;
424
425         if (S_ISDIR (s.st_mode)) {
426                 WARN("%s: is a directory\n", filename);
427                 return 0;
428         }
429
430         WARN("%s: WARNING: unknown filetype 0x%Xu\n", filename, s.st_mode);
431         return 1;  /* try it as good */
432 }
433
434
435 int main(int argc, char** argv)
436 {
437         int first_file = argc;
438         int binary = -1,
439                  check = -1,
440                  quiet = -1,
441                  warn = -1,
442                  status = -1,
443                  tag = -1,
444                  hashString = -1;
445
446         int errorFound = 0;
447         int opt = 0;
448 /*****************************************************************************************
449  ************************************* GETTING DATA ***********************************
450  *****************************************************************************************/
451         while ((opt = getopt_long (argc, argv, "hVbctw", long_options, NULL)) != -1)
452         {
453                 switch (opt) {
454                         case 0              : hashString = 1;  break;
455                         case 'b'            : binary     = 1;  break;
456                         case 't'            : binary     = 0;  break;
457                         case 'c'            : check      = 1;  break;
458                         case 'w'            : warn       = 1;  break;
459                         case QUIET_OPTION   : quiet      = 1;  break;
460                         case STATUS_OPTION  : status     = 1;  break;
461                         case TAG_OPTION     : tag        = 1;  break;
462                         
463                         case 'h'            : print_usage();   /* ..never returns */
464                         case 'V'            : print_version(); /* ..never returns */
465
466                         default: TRYHELP_GOODBYE();
467                 }
468         }
469
470         first_file = optind;
471
472 /*****************************************************************************************
473  ************************************* PROCESSING DATA ***********************************
474  *****************************************************************************************/
475
476         if (check < 0)   /* READ FILES, GENERATE CHECKSUMS AND PRINT TO STDOUT.. */
477         {
478                 if (quiet == 1 || warn == 1 || status == 1)
479                 {
480                         if(quiet == 1)
481                                 WARN("the --quiet option is meaningful only when verifying checksums\n");
482                         if(status ==1)
483                                 WARN("the --status option is meaningful only when verifying checksums\n");
484                         if(warn == 1)
485                                 WARN("the --warn option is meaningful only when verifying checksums\n");
486                         TRYHELP_GOODBYE();
487                 }
488
489                 char mode = binary > 0 ? 'b' : 't';
490
491                 int file_index;
492                 for (file_index = first_file; file_index < argc; file_index++)
493                 {
494                         const char *filename = argv[file_index];
495                         if (!is_goodfile(filename)) {
496                                 errorFound++;
497                                 continue;
498                         }
499
500                         if (binary > 0 && hashString == 1) {
501                                 WARN("%s: No such file or directory\n", filename);
502                                 continue;
503                         }
504
505                         if (PrintFileHash(filename, tag, mode) < 0)
506                                 errorFound++;
507                 }
508
509                 if (first_file >= argc)  /* no files in cmdline? hash stdin.. */
510                         if (PrintFileHash("-", tag, mode) < 0)
511                                 errorFound++;
512
513                 return (errorFound ? 1 : 0);
514         }
515
516         if (check == 1)   /* READ LISTFILES, GENERATE HASHES, COMPARE WITH STORED, PRINT RESULT */
517         {
518                 if (tag == 1 || binary >= 0)
519                 {
520                         if (tag == 1)
521                                 WARN("the --tag option is meaningless when verifying checksums\n");
522                         if (binary >= 0)
523                                 WARN("the --text and --binary options are meaningless when verifying checksums\n");
524                         TRYHELP_GOODBYE();
525                 }
526
527                 int file_index;
528                 for (file_index = first_file; file_index < argc; file_index++)
529                 {
530                         const char *filename = argv[file_index];
531                         if (!is_goodfile(filename)) {
532                                 errorFound++;
533                                 continue;
534                         }
535
536                         FILE *fp = (strcmp(filename, "-") == 0) ? stdin : fopen(filename, "r");
537                         if (!fp) {
538                                 errorFound++;
539                                 WARN("%s: %s\n", filename, strerror(errno));
540                                 continue;
541                         }
542                         if (VerifyHashesFromFile(fp, status, warn, quiet) < 0)
543                                 errorFound++;
544                         if (fp != stdin)
545                                 fclose(fp);
546                 }
547
548                 if (first_file >= argc)  /* no files in cmdline? verify stdin.. */
549                         if (VerifyHashesFromFile(stdin, status, warn, quiet) < 0)
550                                 errorFound++;
551
552                 return (errorFound ? 1 : 0);
553         }
554
555         return 1;
556 }