Convert more tests to framework
[openssl.git] / test / sha256t.c
1 /*
2  * Copyright 2004-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/sha.h>
11 #include <openssl/evp.h>
12
13 #include "test_main.h"
14 #include "testutil.h"
15
16 static const unsigned char app_b1[SHA256_DIGEST_LENGTH] = {
17     0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
18     0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
19     0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
20     0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
21 };
22
23 static const unsigned char app_b2[SHA256_DIGEST_LENGTH] = {
24     0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
25     0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
26     0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
27     0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1
28 };
29
30 static const unsigned char app_b3[SHA256_DIGEST_LENGTH] = {
31     0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92,
32     0x81, 0xa1, 0xc7, 0xe2, 0x84, 0xd7, 0x3e, 0x67,
33     0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e,
34     0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0
35 };
36
37 static const unsigned char addenum_1[SHA224_DIGEST_LENGTH] = {
38     0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, 0x22,
39     0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2, 0x55, 0xb3,
40     0x2a, 0xad, 0xbc, 0xe4, 0xbd, 0xa0, 0xb3, 0xf7,
41     0xe3, 0x6c, 0x9d, 0xa7
42 };
43
44 static const unsigned char addenum_2[SHA224_DIGEST_LENGTH] = {
45     0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76, 0xcc,
46     0x5d, 0xba, 0x5d, 0xa1, 0xfd, 0x89, 0x01, 0x50,
47     0xb0, 0xc6, 0x45, 0x5c, 0xb4, 0xf5, 0x8b, 0x19,
48     0x52, 0x52, 0x25, 0x25
49 };
50
51 static const unsigned char addenum_3[SHA224_DIGEST_LENGTH] = {
52     0x20, 0x79, 0x46, 0x55, 0x98, 0x0c, 0x91, 0xd8,
53     0xbb, 0xb4, 0xc1, 0xea, 0x97, 0x61, 0x8a, 0x4b,
54     0xf0, 0x3f, 0x42, 0x58, 0x19, 0x48, 0xb2, 0xee,
55     0x4e, 0xe7, 0xad, 0x67
56 };
57
58 static int test_sha256_short(void)
59 {
60     unsigned char md[SHA256_DIGEST_LENGTH];
61
62     if (!TEST_true(EVP_Digest("abc", 3, md, NULL, EVP_sha256(), NULL)))
63         return 0;
64     return TEST_mem_eq(md, sizeof(md), app_b1, sizeof(app_b1));
65 }
66
67 static int test_sha256_long(void)
68 {
69     unsigned char md[SHA256_DIGEST_LENGTH];
70
71     if (!TEST_true(EVP_Digest("abcdbcde" "cdefdefg" "efghfghi" "ghijhijk"
72                               "ijkljklm" "klmnlmno" "mnopnopq", 56, md,
73                                NULL, EVP_sha256(), NULL)))
74         return 0;
75     return TEST_mem_eq(md, sizeof(md), app_b2, sizeof(app_b2));
76 }
77
78 static int test_sha256_multi(void)
79 {
80     unsigned char md[SHA256_DIGEST_LENGTH];
81     int i, testresult = 0;
82     EVP_MD_CTX *evp;
83     static const char *updstr = "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
84                                 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
85                                 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
86                                 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
87                                 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
88                                 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
89                                 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
90                                 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
91                                 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa";
92
93     evp = EVP_MD_CTX_new();
94     if (!TEST_ptr(evp))
95         return 0;
96     if (!TEST_true(EVP_DigestInit_ex(evp, EVP_sha256(), NULL)))
97         goto end;
98     for (i = 0; i < 1000000; i += 288)
99         if (!TEST_true(EVP_DigestUpdate(evp, updstr,
100                                         (1000000 - i) < 288 ? 1000000 - i
101                                                             : 288)))
102             goto end;
103     if (!TEST_true(EVP_DigestFinal_ex(evp, md, NULL))
104         || !TEST_mem_eq(md, sizeof(md), app_b3, sizeof(app_b3)))
105         goto end;
106
107     testresult = 1;
108  end:
109     EVP_MD_CTX_free(evp);
110     return testresult;
111 }
112
113 static int test_sha224_short(void)
114 {
115     unsigned char md[SHA224_DIGEST_LENGTH];
116
117     if (!TEST_true(EVP_Digest("abc", 3, md, NULL, EVP_sha224(), NULL)))
118         return 0;
119     return TEST_mem_eq(md, sizeof(md), addenum_1, sizeof(addenum_1));
120 }
121
122 static int test_sha224_long(void)
123 {
124     unsigned char md[SHA224_DIGEST_LENGTH];
125
126     if (!TEST_true(EVP_Digest("abcdbcde" "cdefdefg" "efghfghi" "ghijhijk"
127                               "ijkljklm" "klmnlmno" "mnopnopq", 56, md,
128                               NULL, EVP_sha224(), NULL)))
129         return 0;
130     return TEST_mem_eq(md, sizeof(md), addenum_2, sizeof(addenum_2));
131 }
132
133 static int test_sha224_multi(void)
134 {
135     unsigned char md[SHA224_DIGEST_LENGTH];
136     int i, testresult = 0;
137     EVP_MD_CTX *evp;
138     static const char *updstr = "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
139                                 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa";
140
141     evp = EVP_MD_CTX_new();
142     if (!TEST_ptr(evp))
143         return 0;
144     if (!TEST_true(EVP_DigestInit_ex(evp, EVP_sha224(), NULL)))
145         goto end;
146     for (i = 0; i < 1000000; i += 64)
147         if (!TEST_true(EVP_DigestUpdate(evp, updstr,
148                                         (1000000 - i) < 64 ? 1000000 - i : 64)))
149             goto end;
150     if (!TEST_true(EVP_DigestFinal_ex(evp, md, NULL))
151         || !TEST_mem_eq(md, sizeof(md), addenum_3, sizeof(addenum_3)))
152         goto end;
153
154     testresult = 1;
155  end:
156     EVP_MD_CTX_free(evp);
157     return testresult;
158 }
159
160 void register_tests(void)
161 {
162     ADD_TEST(test_sha256_short);
163     ADD_TEST(test_sha256_long);
164     ADD_TEST(test_sha256_multi);
165     ADD_TEST(test_sha224_short);
166     ADD_TEST(test_sha224_long);
167     ADD_TEST(test_sha224_multi);
168 }