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