2 * Copyright 2004-2017 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/sha.h>
11 #include <openssl/evp.h>
13 #include "test_main.h"
16 static const unsigned char app_c1[SHA512_DIGEST_LENGTH] = {
17 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba,
18 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
19 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2,
20 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
21 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8,
22 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
23 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e,
24 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f
27 static const unsigned char app_c2[SHA512_DIGEST_LENGTH] = {
28 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda,
29 0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f,
30 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1,
31 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18,
32 0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4,
33 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a,
34 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54,
35 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09
38 static const unsigned char app_c3[SHA512_DIGEST_LENGTH] = {
39 0xe7, 0x18, 0x48, 0x3d, 0x0c, 0xe7, 0x69, 0x64,
40 0x4e, 0x2e, 0x42, 0xc7, 0xbc, 0x15, 0xb4, 0x63,
41 0x8e, 0x1f, 0x98, 0xb1, 0x3b, 0x20, 0x44, 0x28,
42 0x56, 0x32, 0xa8, 0x03, 0xaf, 0xa9, 0x73, 0xeb,
43 0xde, 0x0f, 0xf2, 0x44, 0x87, 0x7e, 0xa6, 0x0a,
44 0x4c, 0xb0, 0x43, 0x2c, 0xe5, 0x77, 0xc3, 0x1b,
45 0xeb, 0x00, 0x9c, 0x5c, 0x2c, 0x49, 0xaa, 0x2e,
46 0x4e, 0xad, 0xb2, 0x17, 0xad, 0x8c, 0xc0, 0x9b
49 static const unsigned char app_d1[SHA384_DIGEST_LENGTH] = {
50 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b,
51 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,
52 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63,
53 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,
54 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23,
55 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7
58 static const unsigned char app_d2[SHA384_DIGEST_LENGTH] = {
59 0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8,
60 0x3d, 0x19, 0x2f, 0xc7, 0x82, 0xcd, 0x1b, 0x47,
61 0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2,
62 0x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12,
63 0xfc, 0xc7, 0xc7, 0x1a, 0x55, 0x7e, 0x2d, 0xb9,
64 0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39
67 static const unsigned char app_d3[SHA384_DIGEST_LENGTH] = {
68 0x9d, 0x0e, 0x18, 0x09, 0x71, 0x64, 0x74, 0xcb,
69 0x08, 0x6e, 0x83, 0x4e, 0x31, 0x0a, 0x4a, 0x1c,
70 0xed, 0x14, 0x9e, 0x9c, 0x00, 0xf2, 0x48, 0x52,
71 0x79, 0x72, 0xce, 0xc5, 0x70, 0x4c, 0x2a, 0x5b,
72 0x07, 0xb8, 0xb3, 0xdc, 0x38, 0xec, 0xc4, 0xeb,
73 0xae, 0x97, 0xdd, 0xd8, 0x7f, 0x3d, 0x89, 0x85
76 static int test_sha512_short(void)
78 unsigned char md[SHA512_DIGEST_LENGTH];
80 if (!TEST_true(EVP_Digest("abc", 3, md, NULL, EVP_sha512(), NULL)))
82 return TEST_mem_eq(md, sizeof(md), app_c1, sizeof(app_c1));
85 static int test_sha512_long(void)
87 unsigned char md[SHA512_DIGEST_LENGTH];
89 if (!TEST_true(EVP_Digest("abcdefgh" "bcdefghi" "cdefghij" "defghijk"
90 "efghijkl" "fghijklm" "ghijklmn" "hijklmno"
91 "ijklmnop" "jklmnopq" "klmnopqr" "lmnopqrs"
92 "mnopqrst" "nopqrstu", 112, md, NULL,
95 return TEST_mem_eq(md, sizeof(md), app_c2, sizeof(app_c2));
98 static int test_sha512_multi(void)
100 unsigned char md[SHA512_DIGEST_LENGTH];
101 int i, testresult = 0;
103 static const char *updstr = "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
104 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
105 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
106 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
107 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
108 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
109 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
110 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
111 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa";
113 evp = EVP_MD_CTX_new();
116 if (!TEST_true(EVP_DigestInit_ex(evp, EVP_sha512(), NULL)))
118 for (i = 0; i < 1000000; i += 288)
119 if (!TEST_true(EVP_DigestUpdate(evp, updstr,
120 (1000000 - i) < 288 ? 1000000 - i
123 if (!TEST_true(EVP_DigestFinal_ex(evp, md, NULL))
124 || !TEST_mem_eq(md, sizeof(md), app_c3, sizeof(app_c3)))
129 EVP_MD_CTX_free(evp);
133 static int test_sha384_short(void)
135 unsigned char md[SHA384_DIGEST_LENGTH];
137 if (!TEST_true(EVP_Digest("abc", 3, md, NULL, EVP_sha384(), NULL)))
139 return TEST_mem_eq(md, sizeof(md), app_d1, sizeof(app_d1));
142 static int test_sha384_long(void)
144 unsigned char md[SHA384_DIGEST_LENGTH];
146 if (!TEST_true(EVP_Digest("abcdefgh" "bcdefghi" "cdefghij" "defghijk"
147 "efghijkl" "fghijklm" "ghijklmn" "hijklmno"
148 "ijklmnop" "jklmnopq" "klmnopqr" "lmnopqrs"
149 "mnopqrst" "nopqrstu", 112, md, NULL,
150 EVP_sha384(), NULL)))
152 return TEST_mem_eq(md, sizeof(md), app_d2, sizeof(app_d2));
155 static int test_sha384_multi(void)
157 unsigned char md[SHA384_DIGEST_LENGTH];
158 int i, testresult = 0;
160 static const char *updstr = "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
161 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa";
163 evp = EVP_MD_CTX_new();
166 if (!TEST_true(EVP_DigestInit_ex(evp, EVP_sha384(), NULL)))
168 for (i = 0; i < 1000000; i += 64)
169 if (!TEST_true(EVP_DigestUpdate(evp, updstr,
170 (1000000 - i) < 64 ? 1000000 - i : 64)))
172 if (!TEST_true(EVP_DigestFinal_ex(evp, md, NULL))
173 || !TEST_mem_eq(md, sizeof(md), app_d3, sizeof(app_d3)))
178 EVP_MD_CTX_free(evp);
182 void register_tests(void)
184 ADD_TEST(test_sha512_short);
185 ADD_TEST(test_sha512_long);
186 ADD_TEST(test_sha512_multi);
187 ADD_TEST(test_sha384_short);
188 ADD_TEST(test_sha384_long);
189 ADD_TEST(test_sha384_multi);