summaryrefslogtreecommitdiff
path: root/source4/lib/crypto/sha1test.c
blob: c3b4506decf1cdfec305fbd9860efc853dcf5f58 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 This file contains the reference implementation of SHA-1
 from http://www.ietf.org/rfc/rfc3174.txt
*/
/*
 *  sha1test.c
 *
 *  Description:
 *      This file will exercise the SHA-1 code performing the three
 *      tests documented in FIPS PUB 180-1 plus one which calls
 *      SHA1Input with an exact multiple of 512 bits, plus a few
 *      error test checks.
 *
 *  Portability Issues:
 *      None.
 *
 */

#include "includes.h"

#include "lib/crypto/crypto.h"

struct torture_context;

/*
 *  Define patterns for testing
 */
#define TEST1   "abc"
#define TEST2a  "abcdbcdecdefdefgefghfghighijhi"
#define TEST2b  "jkijkljklmklmnlmnomnopnopq"
#define TEST2   TEST2a TEST2b
#define TEST3   "a"
#define TEST4a  "01234567012345670123456701234567"
#define TEST4b  "01234567012345670123456701234567"
    /* an exact multiple of 512 bits */
#define TEST4   TEST4a TEST4b
static const char *testarray[4] =
{
    TEST1,
    TEST2,
    TEST3,
    TEST4
};
static int repeatcount[4] = { 1, 1, 1000000, 10 };
static const char *resultarray[4] =
{
    "A9 99 3E 36 47 06 81 6A BA 3E 25 71 78 50 C2 6C 9C D0 D8 9D ",
    "84 98 3E 44 1C 3B D2 6E BA AE 4A A1 F9 51 29 E5 E5 46 70 F1 ",
    "34 AA 97 3C D4 C4 DA A4 F6 1E EB 2B DB AD 27 31 65 34 01 6F ",
    "DE A3 56 A2 CD DD 90 C7 A7 EC ED C5 EB B5 63 93 4F 46 04 52 "
};

BOOL torture_local_crypto_sha1(struct torture_context *torture) 
{
    struct SHA1Context sha;
    int i, j, err;
    uint8_t Message_Digest[20];
    BOOL ret = True;
    char tmp[60 + 10];

    /*
     *  Perform SHA-1 tests
     */
    for(j = 0; j < 4; ++j)
    {
    	ZERO_STRUCT(tmp);
        printf( "\nTest %d: %d, '%s'\n",
                j+1,
                repeatcount[j],
                testarray[j]);

        err = SHA1Init(&sha);
        if (err)
        {
            fprintf(stderr, "SHA1Init Error %d.\n", err );
	    ret = False;
            break;    /* out of for j loop */
        }

        for(i = 0; i < repeatcount[j]; ++i)
        {
            err = SHA1Update(&sha,
                  (const unsigned char *) testarray[j],
                  strlen(testarray[j]));
            if (err)
            {
                fprintf(stderr, "SHA1Update Error %d.\n", err );
		ret = False;
                break;    /* out of for i loop */
            }
        }

        err = SHA1Final(Message_Digest, &sha);
        if (err)
        {
            fprintf(stderr,
            "SHA1Result Error %d, could not compute message digest.\n",
            err );
	    ret = False;
        }
        else
        {
            printf("\t");
            for(i = 0; i < 20 ; ++i)
            {
	    	snprintf(tmp+(i*3), sizeof(tmp) - (i*3),"%02X ", Message_Digest[i]);
                printf("%02X ", Message_Digest[i]);
            }
            printf("\n");
        }
        printf("Should match:\n");
        printf("\t%s\n", resultarray[j]);
	if (strcmp(resultarray[j], tmp) != 0) {
	    ret = False;	
	}
    }

    /* Test some error returns */
    err = SHA1Update(&sha,(const unsigned char *) testarray[1], 1);
    if (err != shaStateError) ret = False;
    printf ("\nError %d. Should be %d.\n", err, shaStateError );
    err = SHA1Init(0);
    if (err != shaNull) ret = False;
    printf ("\nError %d. Should be %d.\n", err, shaNull );
    return ret;
}