blob: 18cdf397361041896215a29df73dbdd2b06327f9 [file] [log] [blame]
Rich Salz440e5d82016-05-17 14:20:24 -04001/*
2 * Copyright 2004-2016 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
Andy Polyakovc8422612004-05-13 13:48:33 +00008 */
Rich Salz440e5d82016-05-17 14:20:24 -04009
Andy Polyakovc8422612004-05-13 13:48:33 +000010#include <stdio.h>
11#include <string.h>
12#include <stdlib.h>
13
14#include <openssl/sha.h>
Andy Polyakove39c2542004-07-22 10:21:13 +000015#include <openssl/evp.h>
Andy Polyakov2b247cf2004-08-29 16:36:05 +000016#include <openssl/crypto.h>
Andy Polyakovc8422612004-05-13 13:48:33 +000017
Ben Lauriedf2ee0e2015-09-05 13:32:58 +010018static const unsigned char app_c1[SHA512_DIGEST_LENGTH] = {
Matt Caswell0f113f32015-01-22 03:40:55 +000019 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba,
20 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
21 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2,
22 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
23 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8,
24 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
25 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e,
26 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f
27};
Andy Polyakovc8422612004-05-13 13:48:33 +000028
Ben Lauriedf2ee0e2015-09-05 13:32:58 +010029static const unsigned char app_c2[SHA512_DIGEST_LENGTH] = {
Matt Caswell0f113f32015-01-22 03:40:55 +000030 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda,
31 0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f,
32 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1,
33 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18,
34 0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4,
35 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a,
36 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54,
37 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09
38};
Andy Polyakovc8422612004-05-13 13:48:33 +000039
Ben Lauriedf2ee0e2015-09-05 13:32:58 +010040static const unsigned char app_c3[SHA512_DIGEST_LENGTH] = {
Matt Caswell0f113f32015-01-22 03:40:55 +000041 0xe7, 0x18, 0x48, 0x3d, 0x0c, 0xe7, 0x69, 0x64,
42 0x4e, 0x2e, 0x42, 0xc7, 0xbc, 0x15, 0xb4, 0x63,
43 0x8e, 0x1f, 0x98, 0xb1, 0x3b, 0x20, 0x44, 0x28,
44 0x56, 0x32, 0xa8, 0x03, 0xaf, 0xa9, 0x73, 0xeb,
45 0xde, 0x0f, 0xf2, 0x44, 0x87, 0x7e, 0xa6, 0x0a,
46 0x4c, 0xb0, 0x43, 0x2c, 0xe5, 0x77, 0xc3, 0x1b,
47 0xeb, 0x00, 0x9c, 0x5c, 0x2c, 0x49, 0xaa, 0x2e,
48 0x4e, 0xad, 0xb2, 0x17, 0xad, 0x8c, 0xc0, 0x9b
49};
Andy Polyakovc8422612004-05-13 13:48:33 +000050
Ben Lauriedf2ee0e2015-09-05 13:32:58 +010051static const unsigned char app_d1[SHA384_DIGEST_LENGTH] = {
Matt Caswell0f113f32015-01-22 03:40:55 +000052 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b,
53 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,
54 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63,
55 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,
56 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23,
57 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7
58};
Andy Polyakovc8422612004-05-13 13:48:33 +000059
Ben Lauriedf2ee0e2015-09-05 13:32:58 +010060static const unsigned char app_d2[SHA384_DIGEST_LENGTH] = {
Matt Caswell0f113f32015-01-22 03:40:55 +000061 0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8,
62 0x3d, 0x19, 0x2f, 0xc7, 0x82, 0xcd, 0x1b, 0x47,
63 0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2,
64 0x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12,
65 0xfc, 0xc7, 0xc7, 0x1a, 0x55, 0x7e, 0x2d, 0xb9,
66 0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39
67};
Andy Polyakovc8422612004-05-13 13:48:33 +000068
Ben Lauriedf2ee0e2015-09-05 13:32:58 +010069static const unsigned char app_d3[SHA384_DIGEST_LENGTH] = {
Matt Caswell0f113f32015-01-22 03:40:55 +000070 0x9d, 0x0e, 0x18, 0x09, 0x71, 0x64, 0x74, 0xcb,
71 0x08, 0x6e, 0x83, 0x4e, 0x31, 0x0a, 0x4a, 0x1c,
72 0xed, 0x14, 0x9e, 0x9c, 0x00, 0xf2, 0x48, 0x52,
73 0x79, 0x72, 0xce, 0xc5, 0x70, 0x4c, 0x2a, 0x5b,
74 0x07, 0xb8, 0xb3, 0xdc, 0x38, 0xec, 0xc4, 0xeb,
75 0xae, 0x97, 0xdd, 0xd8, 0x7f, 0x3d, 0x89, 0x85
76};
Andy Polyakovc8422612004-05-13 13:48:33 +000077
Matt Caswell0f113f32015-01-22 03:40:55 +000078int main(int argc, char **argv)
79{
80 unsigned char md[SHA512_DIGEST_LENGTH];
81 int i;
Richard Levitte6e59a892015-11-27 14:02:12 +010082 EVP_MD_CTX *evp;
Andy Polyakovc8422612004-05-13 13:48:33 +000083
Matt Caswell0f113f32015-01-22 03:40:55 +000084 fprintf(stdout, "Testing SHA-512 ");
Andy Polyakovc8422612004-05-13 13:48:33 +000085
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +010086 if (!EVP_Digest("abc", 3, md, NULL, EVP_sha512(), NULL))
87 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +000088 if (memcmp(md, app_c1, sizeof(app_c1))) {
89 fflush(stdout);
90 fprintf(stderr, "\nTEST 1 of 3 failed.\n");
91 return 1;
92 } else
93 fprintf(stdout, ".");
94 fflush(stdout);
Andy Polyakovc8422612004-05-13 13:48:33 +000095
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +010096 if (!EVP_Digest("abcdefgh" "bcdefghi" "cdefghij" "defghijk"
97 "efghijkl" "fghijklm" "ghijklmn" "hijklmno"
98 "ijklmnop" "jklmnopq" "klmnopqr" "lmnopqrs"
99 "mnopqrst" "nopqrstu", 112, md, NULL, EVP_sha512(), NULL))
100 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +0000101 if (memcmp(md, app_c2, sizeof(app_c2))) {
102 fflush(stdout);
103 fprintf(stderr, "\nTEST 2 of 3 failed.\n");
104 return 1;
105 } else
106 fprintf(stdout, ".");
107 fflush(stdout);
Andy Polyakovc8422612004-05-13 13:48:33 +0000108
Richard Levittebfb06412015-12-02 00:49:35 +0100109 evp = EVP_MD_CTX_new();
Richard Levitte6e59a892015-11-27 14:02:12 +0100110 if (evp == NULL) {
111 fflush(stdout);
112 fprintf(stderr, "\nTEST 3 of 3 failed. (malloc failure)\n");
113 return 1;
114 }
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +0100115 if (!EVP_DigestInit_ex(evp, EVP_sha512(), NULL))
116 goto err;
117 for (i = 0; i < 1000000; i += 288) {
118 if (!EVP_DigestUpdate(evp, "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
119 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
120 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
121 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
122 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
123 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
124 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
125 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
126 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa",
127 (1000000 - i) < 288 ? 1000000 - i : 288))
128 goto err;
129 }
130 if (!EVP_DigestFinal_ex(evp, md, NULL))
131 goto err;
Richard Levittebfb06412015-12-02 00:49:35 +0100132 EVP_MD_CTX_reset(evp);
Andy Polyakovc8422612004-05-13 13:48:33 +0000133
Matt Caswell0f113f32015-01-22 03:40:55 +0000134 if (memcmp(md, app_c3, sizeof(app_c3))) {
135 fflush(stdout);
136 fprintf(stderr, "\nTEST 3 of 3 failed.\n");
137 return 1;
138 } else
139 fprintf(stdout, ".");
140 fflush(stdout);
Andy Polyakovc8422612004-05-13 13:48:33 +0000141
Matt Caswell0f113f32015-01-22 03:40:55 +0000142 fprintf(stdout, " passed.\n");
143 fflush(stdout);
Andy Polyakovc8422612004-05-13 13:48:33 +0000144
Matt Caswell0f113f32015-01-22 03:40:55 +0000145 fprintf(stdout, "Testing SHA-384 ");
Andy Polyakovc8422612004-05-13 13:48:33 +0000146
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +0100147 if (!EVP_Digest("abc", 3, md, NULL, EVP_sha384(), NULL))
148 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +0000149 if (memcmp(md, app_d1, sizeof(app_d1))) {
150 fflush(stdout);
151 fprintf(stderr, "\nTEST 1 of 3 failed.\n");
152 return 1;
153 } else
154 fprintf(stdout, ".");
155 fflush(stdout);
Andy Polyakovc8422612004-05-13 13:48:33 +0000156
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +0100157 if (!EVP_Digest("abcdefgh" "bcdefghi" "cdefghij" "defghijk"
158 "efghijkl" "fghijklm" "ghijklmn" "hijklmno"
159 "ijklmnop" "jklmnopq" "klmnopqr" "lmnopqrs"
160 "mnopqrst" "nopqrstu", 112, md, NULL, EVP_sha384(), NULL))
161 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +0000162 if (memcmp(md, app_d2, sizeof(app_d2))) {
163 fflush(stdout);
164 fprintf(stderr, "\nTEST 2 of 3 failed.\n");
165 return 1;
166 } else
167 fprintf(stdout, ".");
168 fflush(stdout);
Andy Polyakovc8422612004-05-13 13:48:33 +0000169
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +0100170 if (!EVP_DigestInit_ex(evp, EVP_sha384(), NULL))
171 goto err;
172 for (i = 0; i < 1000000; i += 64) {
173 if (!EVP_DigestUpdate(evp, "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
174 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa",
175 (1000000 - i) < 64 ? 1000000 - i : 64))
176 goto err;
177 }
178 if (!EVP_DigestFinal_ex(evp, md, NULL))
179 goto err;
Richard Levittebfb06412015-12-02 00:49:35 +0100180 EVP_MD_CTX_free(evp);
Andy Polyakovc8422612004-05-13 13:48:33 +0000181
Matt Caswell0f113f32015-01-22 03:40:55 +0000182 if (memcmp(md, app_d3, sizeof(app_d3))) {
183 fflush(stdout);
184 fprintf(stderr, "\nTEST 3 of 3 failed.\n");
185 return 1;
186 } else
187 fprintf(stdout, ".");
188 fflush(stdout);
Andy Polyakovc8422612004-05-13 13:48:33 +0000189
Matt Caswell0f113f32015-01-22 03:40:55 +0000190 fprintf(stdout, " passed.\n");
191 fflush(stdout);
Andy Polyakov674ee8b2004-05-28 21:42:40 +0000192
Matt Caswell0f113f32015-01-22 03:40:55 +0000193 return 0;
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +0100194
195 err:
196 fflush(stdout);
197 fprintf(stderr, "\nFatal EVP error!\n");
198 return 1;
Andy Polyakovc8422612004-05-13 13:48:33 +0000199}