2006-09-17 13:00:18 +00:00
|
|
|
/**********************************************************************
|
|
|
|
* gostsum.c *
|
|
|
|
* Copyright (c) 2005-2006 Cryptocom LTD *
|
|
|
|
* This file is distributed under the same license as OpenSSL *
|
|
|
|
* *
|
|
|
|
* Almost drop-in replacement for md5sum and sha1sum *
|
|
|
|
* which computes GOST R 34.11-94 hashsum instead *
|
2006-09-21 13:04:43 +00:00
|
|
|
* *
|
|
|
|
**********************************************************************/
|
2006-09-17 13:00:18 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "gosthash.h"
|
|
|
|
#define BUF_SIZE 262144
|
|
|
|
int hash_file(gost_hash_ctx *ctx,char *filename,char *sum,int mode);
|
|
|
|
int hash_stream(gost_hash_ctx *ctx,int fd, char *sum);
|
|
|
|
int get_line(FILE *f,char *hash,char *filename);
|
2006-09-21 13:04:43 +00:00
|
|
|
void help()
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
fprintf(stderr,"gostsum [-bvt] [-c [file]]| [files]\n"
|
2006-09-21 13:04:43 +00:00
|
|
|
"\t-c check message digests (default is generate)\n"
|
|
|
|
"\t-v verbose, print file names when checking\n"
|
|
|
|
"\t-b read files in binary mode\n"
|
|
|
|
"\t-t use test GOST paramset (default is CryptoPro paramset)\n"
|
|
|
|
"The input for -c should be the list of message digests and file names\n"
|
|
|
|
"that is printed on stdout by this program when it generates digests.\n");
|
|
|
|
exit(3);
|
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
|
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2006-09-21 13:04:43 +00:00
|
|
|
int main(int argc,char **argv)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
int c,i;
|
|
|
|
int verbose=0;
|
|
|
|
int errors=0;
|
|
|
|
int open_mode = O_RDONLY;
|
|
|
|
gost_subst_block *b= &GostR3411_94_CryptoProParamSet;
|
|
|
|
FILE *check_file = NULL;
|
|
|
|
gost_hash_ctx ctx;
|
|
|
|
|
2006-09-21 13:04:43 +00:00
|
|
|
while( (c=getopt(argc,argv,"bc::tv"))!=-1)
|
2006-09-17 13:00:18 +00:00
|
|
|
{
|
2006-09-21 13:04:43 +00:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'v': verbose=1; break;
|
|
|
|
case 't': b= &GostR3411_94_TestParamSet; break;
|
|
|
|
case 'b': open_mode |= O_BINARY; break;
|
|
|
|
case 'c':
|
|
|
|
if (optarg)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
check_file = fopen(optarg,"r");
|
2006-09-21 13:04:43 +00:00
|
|
|
if (!check_file)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
perror(optarg);
|
|
|
|
exit(2);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
}
|
2006-09-21 13:04:43 +00:00
|
|
|
else
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
check_file= stdin;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2006-09-17 13:00:18 +00:00
|
|
|
fprintf(stderr,"invalid option %c",optopt);
|
|
|
|
help();
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
}
|
|
|
|
init_gost_hash_ctx(&ctx,b);
|
2006-09-21 13:04:43 +00:00
|
|
|
if (check_file)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
char inhash[65],calcsum[65],filename[PATH_MAX];
|
|
|
|
int failcount=0,count=0;;
|
2006-09-21 13:04:43 +00:00
|
|
|
if (check_file==stdin && optind<argc)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
check_file=fopen(argv[optind],"r");
|
2006-09-21 13:04:43 +00:00
|
|
|
if (!check_file)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
perror(argv[optind]);
|
|
|
|
exit(2);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
}
|
2006-09-21 13:04:43 +00:00
|
|
|
while (get_line(check_file,inhash,filename))
|
2006-09-17 13:00:18 +00:00
|
|
|
{
|
2006-09-21 13:04:43 +00:00
|
|
|
if (!hash_file(&ctx,filename,calcsum,open_mode))
|
|
|
|
{
|
|
|
|
exit (2);
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
if (!strncmp(calcsum,inhash,65))
|
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
fprintf(stderr,"%s\tOK\n",filename);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
}
|
2006-09-21 13:04:43 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
fprintf(stderr,"%s\tFAILED\n",filename);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
fprintf(stderr,"%s: GOST hash sum check failed for '%s'\n",
|
2006-09-21 13:04:43 +00:00
|
|
|
argv[0],filename);
|
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
failcount++;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
}
|
2006-09-21 13:04:43 +00:00
|
|
|
if (verbose && failcount)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
fprintf(stderr,"%s: %d of %d file(f) failed GOST hash sum check\n",
|
2006-09-21 13:04:43 +00:00
|
|
|
argv[0],failcount,count);
|
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
exit (failcount?1:0);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
if (optind==argc)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
char sum[65];
|
2006-09-21 13:04:43 +00:00
|
|
|
if (!hash_stream(&ctx,fileno(stdin),sum))
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
perror("stdin");
|
|
|
|
exit(1);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
printf("%s -\n",sum);
|
|
|
|
exit(0);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
for (i=optind;i<argc;i++)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
char sum[65];
|
2006-09-21 13:04:43 +00:00
|
|
|
if (!hash_file(&ctx,argv[i],sum,open_mode))
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
errors++;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
printf("%s %s\n",sum,argv[i]);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
exit(errors?1:0);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
|
2006-09-21 13:04:43 +00:00
|
|
|
int hash_file(gost_hash_ctx *ctx,char *filename,char *sum,int mode)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
int fd;
|
2006-09-21 13:04:43 +00:00
|
|
|
if ((fd=open(filename,mode))<0)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
perror(filename);
|
|
|
|
return 0;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
if (!hash_stream(ctx,fd,sum))
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
perror(filename);
|
|
|
|
return 0;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
close(fd);
|
|
|
|
return 1;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
|
2006-09-21 13:04:43 +00:00
|
|
|
int hash_stream(gost_hash_ctx *ctx,int fd, char *sum)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
unsigned char buffer[BUF_SIZE];
|
|
|
|
ssize_t bytes;
|
|
|
|
int i;
|
|
|
|
start_hash(ctx);
|
2006-09-21 13:04:43 +00:00
|
|
|
while ((bytes=read(fd,buffer,BUF_SIZE))>0)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
hash_block(ctx,buffer,bytes);
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
if (bytes<0)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
return 0;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
finish_hash(ctx,buffer);
|
2006-09-21 13:04:43 +00:00
|
|
|
for (i=0;i<32;i++)
|
|
|
|
{
|
|
|
|
sprintf(sum+2*i,"%02x",buffer[31-i]);
|
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
return 1;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
|
2006-09-21 13:04:43 +00:00
|
|
|
int get_line(FILE *f,char *hash,char *filename)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
int i;
|
|
|
|
if (fread(hash,1,64,f)<64) return 0;
|
|
|
|
hash[64]=0;
|
2006-09-21 13:04:43 +00:00
|
|
|
for (i=0;i<64;i++)
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
if (hash[i]<'0' || (hash[i]>'9' && hash[i]<'A') || (hash[i]>'F'
|
2006-09-21 13:04:43 +00:00
|
|
|
&& hash[i]<'a')||hash[i]>'f')
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
fprintf(stderr,"Not a hash value '%s'\n",hash);
|
|
|
|
return 0;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fgetc(f)!=' ')
|
|
|
|
{
|
2006-09-17 13:00:18 +00:00
|
|
|
fprintf(stderr,"Malformed input line\n");
|
|
|
|
return 0;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|
2006-09-17 13:00:18 +00:00
|
|
|
i=strlen(fgets(filename,PATH_MAX,f));
|
|
|
|
while (filename[--i]=='\n'||filename[i]=='\r') filename[i]=0;
|
|
|
|
return 1;
|
2006-09-21 13:04:43 +00:00
|
|
|
}
|