1 /**********************************************************************
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * Almost drop-in replacement for md5sum and sha1sum *
7 * which computes GOST R 34.11-94 hashsum instead *
9 **********************************************************************/
15 # define PATH_MAX _MAX_PATH
18 typedef SSIZE_T ssize_t;
29 #define BUF_SIZE 262144
30 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode);
31 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum);
32 int get_line(FILE *f, char *hash, char *filename);
35 fprintf(stderr, "gostsum [-bvt] [-c [file]]| [files]\n"
36 "\t-c check message digests (default is generate)\n"
37 "\t-v verbose, print file names when checking\n"
38 "\t-b read files in binary mode\n"
39 "\t-t use test GOST paramset (default is CryptoPro paramset)\n"
40 "The input for -c should be the list of message digests and file names\n"
41 "that is printed on stdout by this program when it generates digests.\n");
49 int main(int argc, char **argv)
54 int open_mode = O_RDONLY;
55 gost_subst_block *b = &GostR3411_94_CryptoProParamSet;
56 FILE *check_file = NULL;
59 while ((c = getopt(argc, argv, "bc::tv")) != -1) {
65 b = &GostR3411_94_TestParamSet;
68 open_mode |= O_BINARY;
72 check_file = fopen(optarg, "r");
82 fprintf(stderr, "invalid option %c", optopt);
86 init_gost_hash_ctx(&ctx, b);
88 char inhash[65], calcsum[65], filename[PATH_MAX];
89 int failcount = 0, count = 0;
91 if (check_file == stdin && optind < argc) {
92 check_file = fopen(argv[optind], "r");
98 while (get_line(check_file, inhash, filename)) {
100 if (!hash_file(&ctx, filename, calcsum, open_mode)) {
104 if (strncmp(calcsum, inhash, 65) == 0) {
106 fprintf(stderr, "%s\tOK\n", filename);
110 fprintf(stderr, "%s\tFAILED\n", filename);
113 "%s: GOST hash sum check failed for '%s'\n",
121 "%s: WARNING %d of %d file(s) cannot be processed\n",
122 argv[0], errors, count);
125 if (verbose && failcount) {
127 "%s: %d of %d file(f) failed GOST hash sum check\n",
128 argv[0], failcount, count);
130 exit((failcount || errors) ? 1 : 0);
132 if (optind == argc) {
135 if (open_mode & O_BINARY) {
136 _setmode(fileno(stdin), O_BINARY);
139 if (!hash_stream(&ctx, fileno(stdin), sum)) {
143 printf("%s -\n", sum);
146 for (i = optind; i < argc; i++) {
148 if (!hash_file(&ctx, argv[i], sum, open_mode)) {
151 printf("%s %s\n", sum, argv[i]);
154 exit(errors ? 1 : 0);
157 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode)
160 if ((fd = open(filename, mode)) < 0) {
164 if (!hash_stream(ctx, fd, sum)) {
173 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum)
175 unsigned char buffer[BUF_SIZE];
179 while ((bytes = read(fd, buffer, BUF_SIZE)) > 0) {
180 hash_block(ctx, buffer, bytes);
185 finish_hash(ctx, buffer);
186 for (i = 0; i < 32; i++) {
187 sprintf(sum + 2 * i, "%02x", buffer[31 - i]);
192 int get_line(FILE *f, char *hash, char *filename)
195 if (fread(hash, 1, 64, f) < 64)
198 for (i = 0; i < 64; i++) {
199 if (hash[i] < '0' || (hash[i] > '9' && hash[i] < 'A')
200 || (hash[i] > 'F' && hash[i] < 'a') || hash[i] > 'f') {
201 fprintf(stderr, "Not a hash value '%s'\n", hash);
205 if (fgetc(f) != ' ') {
206 fprintf(stderr, "Malformed input line\n");
209 i = strlen(fgets(filename, PATH_MAX, f));
210 while (filename[--i] == '\n' || filename[i] == '\r')