Make sure we return 0 if test passed.
[openssl.git] / crypto / sha / sha256t.c
1 /* crypto/sha/sha256t.c */
2 /* ====================================================================
3  * Copyright (c) 2004 The OpenSSL Project.  All rights reserved.
4  * ====================================================================
5  */
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9
10 #include <openssl/sha.h>
11
12 unsigned char app_b1[SHA256_DIGEST_LENGTH] = {
13         0xba,0x78,0x16,0xbf,0x8f,0x01,0xcf,0xea,
14         0x41,0x41,0x40,0xde,0x5d,0xae,0x22,0x23,
15         0xb0,0x03,0x61,0xa3,0x96,0x17,0x7a,0x9c,
16         0xb4,0x10,0xff,0x61,0xf2,0x00,0x15,0xad };
17
18 unsigned char app_b2[SHA256_DIGEST_LENGTH] = {
19         0x24,0x8d,0x6a,0x61,0xd2,0x06,0x38,0xb8,
20         0xe5,0xc0,0x26,0x93,0x0c,0x3e,0x60,0x39,
21         0xa3,0x3c,0xe4,0x59,0x64,0xff,0x21,0x67,
22         0xf6,0xec,0xed,0xd4,0x19,0xdb,0x06,0xc1 };
23
24 unsigned char app_b3[SHA256_DIGEST_LENGTH] = {
25         0xcd,0xc7,0x6e,0x5c,0x99,0x14,0xfb,0x92,
26         0x81,0xa1,0xc7,0xe2,0x84,0xd7,0x3e,0x67,
27         0xf1,0x80,0x9a,0x48,0xa4,0x97,0x20,0x0e,
28         0x04,0x6d,0x39,0xcc,0xc7,0x11,0x2c,0xd0 };
29
30 unsigned char addenum_1[SHA224_DIGEST_LENGTH] = {
31         0x23,0x09,0x7d,0x22,0x34,0x05,0xd8,0x22,
32         0x86,0x42,0xa4,0x77,0xbd,0xa2,0x55,0xb3,
33         0x2a,0xad,0xbc,0xe4,0xbd,0xa0,0xb3,0xf7,
34         0xe3,0x6c,0x9d,0xa7 };
35
36 unsigned char addenum_2[SHA224_DIGEST_LENGTH] = {
37         0x75,0x38,0x8b,0x16,0x51,0x27,0x76,0xcc,
38         0x5d,0xba,0x5d,0xa1,0xfd,0x89,0x01,0x50,
39         0xb0,0xc6,0x45,0x5c,0xb4,0xf5,0x8b,0x19,
40         0x52,0x52,0x25,0x25 };
41
42 unsigned char addenum_3[SHA224_DIGEST_LENGTH] = {
43         0x20,0x79,0x46,0x55,0x98,0x0c,0x91,0xd8,
44         0xbb,0xb4,0xc1,0xea,0x97,0x61,0x8a,0x4b,
45         0xf0,0x3f,0x42,0x58,0x19,0x48,0xb2,0xee,
46         0x4e,0xe7,0xad,0x67 };
47
48 int main ()
49 { unsigned char md[SHA256_DIGEST_LENGTH];
50   int           i;
51   SHA256_CTX    ctx;
52
53     fprintf(stdout,"Testing SHA-256 ");
54
55     SHA256("abc",3,md);
56     if (memcmp(md,app_b1,sizeof(app_b1)))
57     {   fflush(stdout);
58         fprintf(stderr,"\nTEST 1 of 3 failed.\n");
59         return 1;
60     }
61     else
62         fprintf(stdout,"."); fflush(stdout);
63
64     SHA256("abcdbcde""cdefdefg""efghfghi""ghijhijk"
65            "ijkljklm""klmnlmno""mnopnopq",56,md);
66     if (memcmp(md,app_b2,sizeof(app_b2)))
67     {   fflush(stdout);
68         fprintf(stderr,"\nTEST 2 of 3 failed.\n");
69         return 1;
70     }
71     else
72         fprintf(stdout,"."); fflush(stdout);
73
74     SHA256_Init(&ctx);
75     for (i=0;i<1000000;i+=64)
76         SHA256_Update(&ctx, "aaaaaaaa""aaaaaaaa""aaaaaaaa""aaaaaaaa"
77                             "aaaaaaaa""aaaaaaaa""aaaaaaaa""aaaaaaaa",
78                             (1000000-i)<64?1000000-i:64);
79     SHA256_Final(md,&ctx);
80
81     if (memcmp(md,app_b3,sizeof(app_b3)))
82     {   fflush(stdout);
83         fprintf(stderr,"\nTEST 3 of 3 failed.\n");
84         return 1;
85     }
86     else
87         fprintf(stdout,"."); fflush(stdout);
88
89     fprintf(stdout," passed.\n"); fflush(stdout);
90
91     fprintf(stdout,"Testing SHA-224 ");
92
93     SHA224("abc",3,md);
94     if (memcmp(md,addenum_1,sizeof(addenum_1)))
95     {   fflush(stdout);
96         fprintf(stderr,"\nTEST 1 of 3 failed.\n");
97         return 1;
98     }
99     else
100         fprintf(stdout,"."); fflush(stdout);
101
102     SHA224("abcdbcde""cdefdefg""efghfghi""ghijhijk"
103            "ijkljklm""klmnlmno""mnopnopq",56,md);
104     if (memcmp(md,addenum_2,sizeof(addenum_2)))
105     {   fflush(stdout);
106         fprintf(stderr,"\nTEST 2 of 3 failed.\n");
107         return 1;
108     }
109     else
110         fprintf(stdout,"."); fflush(stdout);
111
112     SHA224_Init(&ctx);
113     for (i=0;i<1000000;i+=64)
114         SHA256_Update(&ctx, "aaaaaaaa""aaaaaaaa""aaaaaaaa""aaaaaaaa"
115                             "aaaaaaaa""aaaaaaaa""aaaaaaaa""aaaaaaaa",
116                             (1000000-i)<64?1000000-i:64);
117     SHA256_Final(md,&ctx);
118
119     if (memcmp(md,addenum_3,sizeof(addenum_3)))
120     {   fflush(stdout);
121         fprintf(stderr,"\nTEST 3 of 3 failed.\n");
122         return 1;
123     }
124     else
125         fprintf(stdout,"."); fflush(stdout);
126
127     fprintf(stdout," passed.\n"); fflush(stdout);
128
129   return 0;
130 }