summaryrefslogtreecommitdiffstats
path: root/src/lib/crypto/crc32/crctest.c
blob: aed2391fd5a992ca8ec2a799ca743be676c55dda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * $Source$
 * $Author$
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <krb5/copyright.h>.
 *
 * CRC test driver program.
 */

#if !defined(lint) && !defined(SABER)
static char rcsid_crctest_c[] =
"$Id$";
#endif	/* !lint & !SABER */

#include <krb5/copyright.h>
#include <krb5/krb5.h>
#include <krb5/crc-32.h>
#include <stdio.h>

void
main()
{
    unsigned char ckout[4];
    krb5_checksum outck;

    char input[16], expected_crc[16];
    unsigned char inbytes[4], outbytes[4];
    int in_length;
    unsigned int expect;

    int bad = 0;

    outck.contents = ckout;

    while (scanf("%s %s", input, expected_crc) == 2) {
	in_length = strlen(input);
	if (in_length % 2) {
	    fprintf(stderr, "bad input '%s', not hex data\n", input);
	    exit(1);
	}
	in_length = in_length / 2;
	if (strlen(expected_crc) != 8) {
	    fprintf(stderr, "bad expectation '%s', not 8 chars\n",
		    expected_crc);
	    exit(1);
	}
	if (sscanf(expected_crc, "%lx",  &expect) != 1) {
	    fprintf(stderr, "bad expectation '%s', not 4bytes hex\n",
		    expected_crc);
	    exit(1);
	}
	outbytes[0] = expect & 0xff;
	outbytes[1] = (expect >> 8) & 0xff;
	outbytes[2] = (expect >> 16) & 0xff;
	outbytes[3] = (expect >> 24) & 0xff;

	if (sscanf(input, "%lx",  &expect) != 1) {
	    fprintf(stderr, "bad expectation '%s', not hex\n",
		    expected_crc);
	    exit(1);
	}
	inbytes[0] = expect & 0xff;
	inbytes[1] = (expect >> 8) & 0xff;
	inbytes[2] = (expect >> 16) & 0xff;
	inbytes[3] = (expect >> 24) & 0xff;

	(*crc32_cksumtable_entry.sum_func)((krb5_pointer)inbytes,
					   in_length, 0, 0, &outck);
	if (memcmp(outbytes, ckout, 4)) {
	    printf("mismatch: input '%s', output '%02x%02x%02x%02x', \
expected '%s'\n",
		   input, ckout[3], ckout[2], ckout[1], ckout[0],
		   expected_crc);
	    bad = 1;
	}	
    }
    if (bad) 
	printf("crctest: failed to pass the test\n");
    else
	printf("crctest: test is passed successfully\n");

    exit(bad);
}