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