blob: 90262d96242d24175dfad09e88702ffb80aaa8d3 [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 Polyakovc8422612004-05-13 13:48:33 +000016
Ben Lauriedf2ee0e2015-09-05 13:32:58 +010017static const unsigned char app_b1[SHA256_DIGEST_LENGTH] = {
Matt Caswell0f113f32015-01-22 03:40:55 +000018 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
19 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
20 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
21 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
22};
Andy Polyakovc8422612004-05-13 13:48:33 +000023
Ben Lauriedf2ee0e2015-09-05 13:32:58 +010024static const unsigned char app_b2[SHA256_DIGEST_LENGTH] = {
Matt Caswell0f113f32015-01-22 03:40:55 +000025 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
26 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
27 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
28 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1
29};
Andy Polyakovc8422612004-05-13 13:48:33 +000030
Ben Lauriedf2ee0e2015-09-05 13:32:58 +010031static const unsigned char app_b3[SHA256_DIGEST_LENGTH] = {
Matt Caswell0f113f32015-01-22 03:40:55 +000032 0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92,
33 0x81, 0xa1, 0xc7, 0xe2, 0x84, 0xd7, 0x3e, 0x67,
34 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e,
35 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0
36};
Andy Polyakovc8422612004-05-13 13:48:33 +000037
Ben Lauriedf2ee0e2015-09-05 13:32:58 +010038static const unsigned char addenum_1[SHA224_DIGEST_LENGTH] = {
Matt Caswell0f113f32015-01-22 03:40:55 +000039 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, 0x22,
40 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2, 0x55, 0xb3,
41 0x2a, 0xad, 0xbc, 0xe4, 0xbd, 0xa0, 0xb3, 0xf7,
42 0xe3, 0x6c, 0x9d, 0xa7
43};
Andy Polyakovda8348e2004-05-27 19:46:07 +000044
Ben Lauriedf2ee0e2015-09-05 13:32:58 +010045static const unsigned char addenum_2[SHA224_DIGEST_LENGTH] = {
Matt Caswell0f113f32015-01-22 03:40:55 +000046 0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76, 0xcc,
47 0x5d, 0xba, 0x5d, 0xa1, 0xfd, 0x89, 0x01, 0x50,
48 0xb0, 0xc6, 0x45, 0x5c, 0xb4, 0xf5, 0x8b, 0x19,
49 0x52, 0x52, 0x25, 0x25
50};
Andy Polyakovda8348e2004-05-27 19:46:07 +000051
Ben Lauriedf2ee0e2015-09-05 13:32:58 +010052static const unsigned char addenum_3[SHA224_DIGEST_LENGTH] = {
Matt Caswell0f113f32015-01-22 03:40:55 +000053 0x20, 0x79, 0x46, 0x55, 0x98, 0x0c, 0x91, 0xd8,
54 0xbb, 0xb4, 0xc1, 0xea, 0x97, 0x61, 0x8a, 0x4b,
55 0xf0, 0x3f, 0x42, 0x58, 0x19, 0x48, 0xb2, 0xee,
56 0x4e, 0xe7, 0xad, 0x67
57};
Andy Polyakovda8348e2004-05-27 19:46:07 +000058
Matt Caswell0f113f32015-01-22 03:40:55 +000059int main(int argc, char **argv)
60{
61 unsigned char md[SHA256_DIGEST_LENGTH];
62 int i;
Richard Levitte6e59a892015-11-27 14:02:12 +010063 EVP_MD_CTX *evp;
Andy Polyakovc8422612004-05-13 13:48:33 +000064
Matt Caswell0f113f32015-01-22 03:40:55 +000065 fprintf(stdout, "Testing SHA-256 ");
Andy Polyakovc8422612004-05-13 13:48:33 +000066
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +010067 if (!EVP_Digest("abc", 3, md, NULL, EVP_sha256(), NULL))
68 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +000069 if (memcmp(md, app_b1, sizeof(app_b1))) {
70 fflush(stdout);
71 fprintf(stderr, "\nTEST 1 of 3 failed.\n");
72 return 1;
73 } else
74 fprintf(stdout, ".");
75 fflush(stdout);
Andy Polyakovc8422612004-05-13 13:48:33 +000076
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +010077 if (!EVP_Digest("abcdbcde" "cdefdefg" "efghfghi" "ghijhijk"
78 "ijkljklm" "klmnlmno" "mnopnopq", 56, md,
79 NULL, EVP_sha256(), NULL))
80 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +000081 if (memcmp(md, app_b2, sizeof(app_b2))) {
82 fflush(stdout);
83 fprintf(stderr, "\nTEST 2 of 3 failed.\n");
84 return 1;
85 } else
86 fprintf(stdout, ".");
87 fflush(stdout);
Andy Polyakovc8422612004-05-13 13:48:33 +000088
Richard Levittebfb06412015-12-02 00:49:35 +010089 evp = EVP_MD_CTX_new();
Richard Levitte6e59a892015-11-27 14:02:12 +010090 if (evp == NULL) {
91 fflush(stdout);
92 fprintf(stderr, "\nTEST 3 of 3 failed. (malloc failure)\n");
93 return 1;
94 }
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +010095 if (!EVP_DigestInit_ex(evp, EVP_sha256(), NULL))
96 goto err;
97 for (i = 0; i < 1000000; i += 288) {
98 if (!EVP_DigestUpdate(evp, "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
99 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
100 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
101 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
102 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
103 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
104 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
105 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
106 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa",
107 (1000000 - i) < 288 ? 1000000 - i : 288))
108 goto err;
109 }
110 if (!EVP_DigestFinal_ex(evp, md, NULL))
111 goto err;
Andy Polyakovc8422612004-05-13 13:48:33 +0000112
Matt Caswell0f113f32015-01-22 03:40:55 +0000113 if (memcmp(md, app_b3, sizeof(app_b3))) {
114 fflush(stdout);
115 fprintf(stderr, "\nTEST 3 of 3 failed.\n");
116 return 1;
117 } else
118 fprintf(stdout, ".");
119 fflush(stdout);
Andy Polyakovc8422612004-05-13 13:48:33 +0000120
Matt Caswell0f113f32015-01-22 03:40:55 +0000121 fprintf(stdout, " passed.\n");
122 fflush(stdout);
Andy Polyakovda8348e2004-05-27 19:46:07 +0000123
Matt Caswell0f113f32015-01-22 03:40:55 +0000124 fprintf(stdout, "Testing SHA-224 ");
Andy Polyakovda8348e2004-05-27 19:46:07 +0000125
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +0100126 if (!EVP_Digest("abc", 3, md, NULL, EVP_sha224(), NULL))
127 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +0000128 if (memcmp(md, addenum_1, sizeof(addenum_1))) {
129 fflush(stdout);
130 fprintf(stderr, "\nTEST 1 of 3 failed.\n");
131 return 1;
132 } else
133 fprintf(stdout, ".");
134 fflush(stdout);
Andy Polyakovda8348e2004-05-27 19:46:07 +0000135
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +0100136 if (!EVP_Digest("abcdbcde" "cdefdefg" "efghfghi" "ghijhijk"
137 "ijkljklm" "klmnlmno" "mnopnopq", 56, md,
138 NULL, EVP_sha224(), NULL))
139 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +0000140 if (memcmp(md, addenum_2, sizeof(addenum_2))) {
141 fflush(stdout);
142 fprintf(stderr, "\nTEST 2 of 3 failed.\n");
143 return 1;
144 } else
145 fprintf(stdout, ".");
146 fflush(stdout);
Andy Polyakovda8348e2004-05-27 19:46:07 +0000147
Richard Levittebfb06412015-12-02 00:49:35 +0100148 EVP_MD_CTX_reset(evp);
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +0100149 if (!EVP_DigestInit_ex(evp, EVP_sha224(), NULL))
150 goto err;
151 for (i = 0; i < 1000000; i += 64) {
152 if (!EVP_DigestUpdate(evp, "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa"
153 "aaaaaaaa" "aaaaaaaa" "aaaaaaaa" "aaaaaaaa",
154 (1000000 - i) < 64 ? 1000000 - i : 64))
155 goto err;
156 }
157 if (!EVP_DigestFinal_ex(evp, md, NULL))
158 goto err;
Richard Levittebfb06412015-12-02 00:49:35 +0100159 EVP_MD_CTX_free(evp);
Andy Polyakovda8348e2004-05-27 19:46:07 +0000160
Matt Caswell0f113f32015-01-22 03:40:55 +0000161 if (memcmp(md, addenum_3, sizeof(addenum_3))) {
162 fflush(stdout);
163 fprintf(stderr, "\nTEST 3 of 3 failed.\n");
164 return 1;
165 } else
166 fprintf(stdout, ".");
167 fflush(stdout);
Andy Polyakovda8348e2004-05-27 19:46:07 +0000168
Matt Caswell0f113f32015-01-22 03:40:55 +0000169 fprintf(stdout, " passed.\n");
170 fflush(stdout);
Andy Polyakov674ee8b2004-05-28 21:42:40 +0000171
Matt Caswell0f113f32015-01-22 03:40:55 +0000172 return 0;
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +0100173
174 err:
175 fprintf(stderr, "Fatal EVP error!\n");
176 return 1;
Andy Polyakovc8422612004-05-13 13:48:33 +0000177}