test_sha256.c (20626B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2019-2023 Evgeny Grin (Karlson2k) 4 5 This test tool is free software; you can redistribute it and/or 6 modify it under the terms of the GNU General Public License as 7 published by the Free Software Foundation; either version 2, or 8 (at your option) any later version. 9 10 This test tool is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20 /** 21 * @file microhttpd/test_sha256.h 22 * @brief Unit tests for SHA-256 functions 23 * @author Karlson2k (Evgeny Grin) 24 */ 25 26 #include "mhd_options.h" 27 #include "mhd_sha256_wrap.h" 28 #include "test_helpers.h" 29 #include <stdio.h> 30 #include <stdlib.h> 31 32 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this 33 test into a marked, classifiable test error (TESTING.md, P5). */ 34 #include "mhd_panic_tripwire.h" 35 36 #if defined(MHD_SHA256_TLSLIB) && defined(MHD_HTTPS_REQUIRE_GCRYPT) 37 #define NEED_GCRYP_INIT 1 38 #include <gcrypt.h> 39 #endif /* MHD_SHA256_TLSLIB && MHD_HTTPS_REQUIRE_GCRYPT */ 40 41 static int verbose = 0; /* verbose level (0-1)*/ 42 43 44 struct str_with_len 45 { 46 const char *const str; 47 const size_t len; 48 }; 49 50 #define D_STR_W_LEN(s) {(s), (sizeof((s)) / sizeof(char)) - 1} 51 52 struct data_unit1 53 { 54 const struct str_with_len str_l; 55 const uint8_t digest[SHA256_DIGEST_SIZE]; 56 }; 57 58 static const struct data_unit1 data_units1[] = { 59 {D_STR_W_LEN ("abc"), 60 {0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 61 0x5d, 0xae, 0x22, 0x23, 62 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 63 0xf2, 0x00, 0x15, 0xad}}, 64 {D_STR_W_LEN ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"), 65 {0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93, 66 0x0c, 0x3e, 0x60, 0x39, 67 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 0xf6, 0xec, 0xed, 0xd4, 68 0x19, 0xdb, 0x06, 0xc1}}, 69 {D_STR_W_LEN (""), 70 {0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 71 0x99, 0x6f, 0xb9, 0x24, 72 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 73 0x78, 0x52, 0xb8, 0x55}}, 74 {D_STR_W_LEN ("1234567890!@~%&$@#{}[]\\/!?`."), 75 {0x2f, 0xad, 0x7a, 0xff, 0x7d, 0xfe, 0xcd, 0x78, 0xe4, 0xa6, 0xf3, 0x85, 76 0x97, 0x9d, 0xdc, 0x39, 77 0x55, 0x24, 0x35, 0x4a, 0x00, 0x6f, 0x42, 0x72, 0x41, 0xc1, 0x52, 0xa7, 78 0x01, 0x0b, 0x2c, 0x41}}, 79 {D_STR_W_LEN ("Simple string."), 80 {0x01, 0x73, 0x17, 0xc4, 0x0a, 0x9a, 0x0e, 0x81, 0xb3, 0xa4, 0xb1, 0x8e, 81 0xe9, 0xd6, 0xc2, 0xdf, 82 0xfa, 0x7d, 0x53, 0x4e, 0xa1, 0xda, 0xb2, 0x5a, 0x75, 0xbb, 0x2c, 0x30, 83 0x2f, 0x5f, 0x7a, 0xf4}}, 84 {D_STR_W_LEN ("abcdefghijklmnopqrstuvwxyz"), 85 {0x71, 0xc4, 0x80, 0xdf, 0x93, 0xd6, 0xae, 0x2f, 0x1e, 0xfa, 0xd1, 0x44, 86 0x7c, 0x66, 0xc9, 0x52, 87 0x5e, 0x31, 0x62, 0x18, 0xcf, 0x51, 0xfc, 0x8d, 0x9e, 0xd8, 0x32, 0xf2, 88 0xda, 0xf1, 0x8b, 0x73}}, 89 {D_STR_W_LEN ("zyxwvutsrqponMLKJIHGFEDCBA"), 90 {0xce, 0x7d, 0xde, 0xb6, 0x1f, 0x7c, 0x1d, 0x83, 0x7c, 0x60, 0xd8, 0x36, 91 0x73, 0x82, 0xac, 0x92, 92 0xca, 0x37, 0xfd, 0x72, 0x8b, 0x0c, 0xd1, 0x6c, 0x55, 0xd5, 0x88, 0x98, 93 0x24, 0xfa, 0x16, 0xf2}}, 94 {D_STR_W_LEN ("abcdefghijklmnopqrstuvwxyzzyxwvutsrqponMLKJIHGFEDCBA" \ 95 "abcdefghijklmnopqrstuvwxyzzyxwvutsrqponMLKJIHGFEDCBA"), 96 {0x27, 0xd1, 0xe8, 0xbc, 0x6a, 0x79, 0x16, 0x83, 0x61, 0x73, 0xa9, 0xa8, 97 0x9b, 0xaf, 0xaf, 0xcf, 98 0x47, 0x4d, 0x09, 0xef, 0x6d, 0x50, 0x35, 0x12, 0x25, 0x72, 0xd8, 0x68, 99 0xdc, 0x1f, 0xd2, 0xf4}}, 100 {D_STR_W_LEN ("/long/long/long/long/long/long/long/long/long/long/long" \ 101 "/long/long/long/long/long/long/long/long/long/long/long" \ 102 "/long/long/long/long/long/long/long/long/long/long/long" \ 103 "/long/long/long/long/long/long/long/long/long/long/long" \ 104 "/long/long/long/long/long/long/long/long/long/long/long" \ 105 "/long/long/long/long/long/long/long/long/long/long/long" \ 106 "/long/long/long/long/path?with%20some=parameters"), 107 {0x73, 0x85, 0xc5, 0xb9, 0x8f, 0xaf, 0x7d, 0x5e, 0xad, 0xd8, 0x0b, 0x8e, 108 0x12, 0xdb, 0x28, 0x60, 0xc7, 0xc7, 0x55, 0x05, 0x2f, 0x7c, 0x6f, 0xfa, 109 0xd1, 0xe3, 0xe1, 0x7b, 0x04, 0xd4, 0xb0, 0x21}} 110 }; 111 112 static const size_t units1_num = sizeof(data_units1) / sizeof(data_units1[0]); 113 114 struct bin_with_len 115 { 116 const uint8_t bin[512]; 117 const size_t len; 118 }; 119 120 struct data_unit2 121 { 122 const struct bin_with_len bin_l; 123 const uint8_t digest[SHA256_DIGEST_SIZE]; 124 }; 125 126 /* Size must be less than 512 bytes! */ 127 static const struct data_unit2 data_units2[] = { 128 { { {97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 129 112, 113, 114, 115, 116, 130 117, 118, 119, 120, 121, 122}, 26}, /* a..z ASCII sequence */ 131 {0x71, 0xc4, 0x80, 0xdf, 0x93, 0xd6, 0xae, 0x2f, 0x1e, 0xfa, 0xd1, 0x44, 132 0x7c, 0x66, 0xc9, 0x52, 133 0x5e, 0x31, 0x62, 0x18, 0xcf, 0x51, 0xfc, 0x8d, 0x9e, 0xd8, 0x32, 0xf2, 134 0xda, 0xf1, 0x8b, 0x73}}, 135 { { {65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 136 65, 65, 65, 65, 65, 65, 137 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 138 65, 65, 65, 65, 65, 65, 139 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 140 65, 65, 65, 65, 65, 65}, 72 },/* 'A' x 72 times */ 141 {0x6a, 0x6d, 0x69, 0x1a, 0xc9, 0xba, 0x70, 0x95, 0x50, 0x46, 0x75, 0x7c, 142 0xd6, 0x85, 0xb6, 0x25, 143 0x77, 0x73, 0xff, 0x3a, 0xd9, 0x3f, 0x43, 0xd4, 0xd4, 0x81, 0x2c, 0x5b, 144 0x10, 0x6f, 0x4b, 0x5b}}, 145 { { {19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 146 37, 38, 39, 40, 41, 42, 147 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 148 61, 62, 63, 64, 65, 66, 67, 149 68, 69, 70, 71, 72, 73}, 55}, /* 19..73 sequence */ 150 {0x06, 0xe4, 0xb3, 0x9e, 0xf1, 0xfb, 0x6c, 0xcf, 0xd7, 0x3f, 0x50, 0x9e, 151 0xf4, 0x16, 0x17, 0xd4, 152 0x63, 0x7c, 0x39, 0x1e, 0xa8, 0x0f, 0xa9, 0x88, 0x03, 0x44, 0x98, 0x0e, 153 0x95, 0x81, 0xf0, 0x2a}}, 154 { { {7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 155 26, 27, 28, 29, 30, 31, 156 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 157 50, 51, 52, 53, 54, 55, 56, 158 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69}, 63}, /* 7..69 sequence */ 159 {0x4a, 0xd3, 0xc6, 0x87, 0x1f, 0xd1, 0xc5, 0xe2, 0x3e, 0x52, 0xdc, 0x22, 160 0xd1, 0x10, 0xd2, 0x05, 161 0x15, 0x23, 0xcd, 0x15, 0xac, 0x24, 0x88, 0x26, 0x02, 0x00, 0x70, 0x78, 162 0x9f, 0x17, 0xf8, 0xd9}}, 163 { { {38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 164 56, 57, 58, 59, 60, 61, 165 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 166 80, 81, 82, 83, 84, 85, 86, 167 87, 88, 89, 90, 91, 92}, 55}, /* 38..92 sequence */ 168 {0xe6, 0x03, 0x0f, 0xc9, 0x0d, 0xca, 0x0c, 0x26, 0x41, 0xcf, 0x43, 0x27, 169 0xec, 0xd6, 0x28, 0x2a, 170 0x98, 0x24, 0x55, 0xd3, 0x5a, 0xed, 0x8b, 0x32, 0x19, 0x78, 0xeb, 0x83, 171 0x1d, 0x19, 0x92, 0x79}}, 172 { { {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 173 21, 22, 23, 24, 25, 26, 27, 174 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 175 46, 47, 48, 49, 50, 51, 52, 176 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 177 71, 72}, 178 72},/* 1..72 sequence */ 179 {0x87, 0xa2, 0xfa, 0x2e, 0xec, 0x53, 0x05, 0x3c, 0xb1, 0xee, 0x07, 0xd7, 180 0x59, 0x70, 0xf6, 0x50, 181 0xcd, 0x9d, 0xc5, 0x8b, 0xdc, 0xb8, 0x65, 0x30, 0x4f, 0x70, 0x82, 0x9e, 182 0xbd, 0xe2, 0x7d, 0xac}}, 183 { { {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 184 21, 22, 23, 24, 25, 26, 185 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 186 45, 46, 47, 48, 49, 50, 51, 187 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 188 70, 71, 72, 73, 74, 75, 76, 189 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 190 95, 96, 97, 98, 99, 100, 191 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 192 115, 116, 117, 118, 119, 120, 193 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 194 135, 136, 137, 138, 139, 140, 195 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 196 155, 156, 157, 158, 159, 160, 197 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 198 175, 176, 177, 178, 179, 180, 199 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 200 195, 196, 197, 198, 199, 200, 201 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 202 215, 216, 217, 218, 219, 220, 203 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 204 235, 236, 237, 238, 239, 240, 205 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 206 255}, 256}, /* 0..255 sequence */ 207 {0x40, 0xaf, 0xf2, 0xe9, 0xd2, 0xd8, 0x92, 0x2e, 0x47, 0xaf, 0xd4, 0x64, 208 0x8e, 0x69, 0x67, 0x49, 209 0x71, 0x58, 0x78, 0x5f, 0xbd, 0x1d, 0xa8, 0x70, 0xe7, 0x11, 0x02, 0x66, 210 0xbf, 0x94, 0x48, 0x80}}, 211 { { {199, 198, 197, 196, 195, 194, 193, 192, 191, 190, 189, 188, 187, 186, 212 185, 184, 183, 182, 181, 180, 213 179, 178, 177, 176, 175, 174, 173, 172, 171, 170, 169, 168, 167, 166, 214 165, 164, 163, 162, 161, 160, 215 159, 158, 157, 156, 155, 154, 153, 152, 151, 150, 149, 148, 147, 146, 216 145, 144, 143, 142, 141, 140, 217 139}, 61}, /* 199..139 sequence */ 218 {0x85, 0xf8, 0xa2, 0x83, 0xd6, 0x3c, 0x76, 0x8e, 0xea, 0x8f, 0x1c, 0x57, 219 0x2d, 0x85, 0xb6, 0xff, 220 0xd8, 0x33, 0x57, 0x62, 0x1d, 0x37, 0xae, 0x0e, 0xfc, 0x22, 0xd3, 0xd5, 221 0x8f, 0x53, 0x21, 0xb7}}, 222 { { {255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 223 241, 240, 239, 238, 237, 236, 224 235, 234, 233, 232, 231, 230, 229, 228, 227, 226, 225, 224, 223, 222, 225 221, 220, 219, 218, 217, 216, 226 215, 214, 213, 212, 211, 210, 209, 208, 207, 206, 205, 204, 203, 202, 227 201, 200, 199, 198, 197, 196, 228 195, 194, 193, 192, 191, 190, 189, 188, 187, 186, 185, 184, 183, 182, 229 181, 180, 179, 178, 177, 176, 230 175, 174, 173, 172, 171, 170, 169, 168, 167, 166, 165, 164, 163, 162, 231 161, 160, 159, 158, 157, 156, 232 155, 154, 153, 152, 151, 150, 149, 148, 147, 146, 145, 144, 143, 142, 233 141, 140, 139, 138, 137, 136, 234 135, 134, 133, 132, 131, 130, 129, 128, 127, 126, 125, 124, 123, 122, 235 121, 120, 119, 118, 117, 116, 236 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 237 101, 100, 99, 98, 97, 96, 95, 238 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 239 76, 75, 74, 73, 72, 71, 70, 240 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 241 51, 50, 49, 48, 47, 46, 45, 242 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 243 26, 25, 24, 23, 22, 21, 20, 244 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, 255}, /* 255..1 sequence */ 245 {0x61, 0x86, 0x96, 0xab, 0x3e, 0xaa, 0x0e, 0x64, 0xb2, 0xf7, 0x2d, 0x75, 246 0x47, 0x5a, 0x14, 0x97, 247 0xa3, 0x3d, 0x59, 0xa4, 0x08, 0xd9, 0x9e, 0x73, 0xf2, 0x78, 0x00, 0x5b, 248 0x4b, 0x55, 0xca, 0x43}}, 249 { { {41, 35, 190, 132, 225, 108, 214, 174, 82, 144, 73, 241, 241, 187, 233, 250 235, 179, 166, 219, 60, 135, 251 12, 62, 153, 36, 94, 13, 28, 6, 183, 71, 222, 179, 18, 77, 200, 67, 187, 252 139, 166, 31, 3, 90, 125, 9, 253 56, 37, 31, 93, 212, 203, 252, 150, 245, 69, 59, 19, 13, 137, 10, 28, 254 219, 174, 50, 32, 154, 80, 238, 255 64, 120, 54, 253, 18, 73, 50, 246, 158, 125, 73, 220, 173, 79, 20, 242, 256 68, 64, 102, 208, 107, 196, 257 48, 183, 50, 59, 161, 34, 246, 34, 145, 157, 225, 139, 31, 218, 176, 202, 258 153, 2, 185, 114, 157, 73, 259 44, 128, 126, 197, 153, 213, 233, 128, 178, 234, 201, 204, 83, 191, 103, 260 214, 191, 20, 214, 126, 45, 261 220, 142, 102, 131, 239, 87, 73, 97, 255, 105, 143, 97, 205, 209, 30, 262 157, 156, 22, 114, 114, 230, 263 29, 240, 132, 79, 74, 119, 2, 215, 232, 57, 44, 83, 203, 201, 18, 30, 51, 264 116, 158, 12, 244, 213, 265 212, 159, 212, 164, 89, 126, 53, 207, 50, 34, 244, 204, 207, 211, 144, 266 45, 72, 211, 143, 117, 230, 267 217, 29, 42, 229, 192, 247, 43, 120, 129, 135, 68, 14, 95, 80, 0, 212, 268 97, 141, 190, 123, 5, 21, 7, 269 59, 51, 130, 31, 24, 112, 146, 218, 100, 84, 206, 177, 133, 62, 105, 21, 270 248, 70, 106, 4, 150, 115, 271 14, 217, 22, 47, 103, 104, 212, 247, 74, 74, 208, 87, 104}, 255}, /* pseudo-random data */ 272 {0x08, 0x7f, 0x86, 0xac, 0xe2, 0x2e, 0x28, 0x56, 0x74, 0x53, 0x4f, 0xc0, 273 0xfb, 0xb8, 0x79, 0x57, 274 0xc5, 0xc8, 0xd1, 0xb7, 0x47, 0xb7, 0xd9, 0xea, 0x97, 0xa8, 0x67, 0xe9, 275 0x26, 0x93, 0xee, 0xa3}} 276 }; 277 278 static const size_t units2_num = sizeof(data_units2) / sizeof(data_units2[0]); 279 280 281 /* 282 * Helper functions 283 */ 284 285 /** 286 * Print bin as hex 287 * 288 * @param bin binary data 289 * @param len number of bytes in bin 290 * @param hex pointer to len*2+1 bytes buffer 291 */ 292 static void 293 bin2hex (const uint8_t *bin, 294 size_t len, 295 char *hex) 296 { 297 while (len-- > 0) 298 { 299 unsigned int b1, b2; 300 b1 = (*bin >> 4) & 0xf; 301 *hex++ = (char) ((b1 > 9) ? (b1 + 'A' - 10) : (b1 + '0')); 302 b2 = *bin++ & 0xf; 303 *hex++ = (char) ((b2 > 9) ? (b2 + 'A' - 10) : (b2 + '0')); 304 } 305 *hex = 0; 306 } 307 308 309 static int 310 check_result (const char *test_name, 311 unsigned int check_num, 312 const uint8_t calculated[SHA256_DIGEST_SIZE], 313 const uint8_t expected[SHA256_DIGEST_SIZE]) 314 { 315 int failed = memcmp (calculated, expected, SHA256_DIGEST_SIZE); 316 check_num++; /* Print 1-based numbers */ 317 if (failed) 318 { 319 char calc_str[SHA256_DIGEST_STRING_SIZE]; 320 char expc_str[SHA256_DIGEST_STRING_SIZE]; 321 bin2hex (calculated, SHA256_DIGEST_SIZE, calc_str); 322 bin2hex (expected, SHA256_DIGEST_SIZE, expc_str); 323 fprintf (stderr, 324 "FAILED: %s check %u: calculated digest %s, expected digest %s.\n", 325 test_name, check_num, calc_str, expc_str); 326 fflush (stderr); 327 } 328 else if (verbose) 329 { 330 char calc_str[SHA256_DIGEST_STRING_SIZE]; 331 bin2hex (calculated, SHA256_DIGEST_SIZE, calc_str); 332 printf ("PASSED: %s check %u: calculated digest %s " 333 "matches expected digest.\n", 334 test_name, check_num, calc_str); 335 fflush (stdout); 336 } 337 return failed ? 1 : 0; 338 } 339 340 341 /* 342 * Tests 343 */ 344 345 /* Calculated SHA-256 as one pass for whole data */ 346 static int 347 test1_str (void) 348 { 349 int num_failed = 0; 350 unsigned int i; 351 struct Sha256CtxWr ctx; 352 353 MHD_SHA256_init_one_time (&ctx); 354 for (i = 0; i < units1_num; i++) 355 { 356 uint8_t digest[SHA256_DIGEST_SIZE]; 357 358 MHD_SHA256_update (&ctx, (const uint8_t *) data_units1[i].str_l.str, 359 data_units1[i].str_l.len); 360 MHD_SHA256_finish_reset (&ctx, digest); 361 #ifdef MHD_SHA256_HAS_EXT_ERROR 362 if (0 != ctx.ext_error) 363 { 364 fprintf (stderr, "External hashing error: %d.\n", ctx.ext_error); 365 exit (99); 366 } 367 #endif /* MHD_SHA256_HAS_EXT_ERROR */ 368 num_failed += check_result (MHD_FUNC_, i, digest, 369 data_units1[i].digest); 370 } 371 MHD_SHA256_deinit (&ctx); 372 return num_failed; 373 } 374 375 376 static int 377 test1_bin (void) 378 { 379 int num_failed = 0; 380 unsigned int i; 381 struct Sha256CtxWr ctx; 382 383 MHD_SHA256_init_one_time (&ctx); 384 for (i = 0; i < units2_num; i++) 385 { 386 uint8_t digest[SHA256_DIGEST_SIZE]; 387 388 MHD_SHA256_update (&ctx, data_units2[i].bin_l.bin, 389 data_units2[i].bin_l.len); 390 MHD_SHA256_finish_reset (&ctx, digest); 391 #ifdef MHD_SHA256_HAS_EXT_ERROR 392 if (0 != ctx.ext_error) 393 { 394 fprintf (stderr, "External hashing error: %d.\n", ctx.ext_error); 395 exit (99); 396 } 397 #endif /* MHD_SHA256_HAS_EXT_ERROR */ 398 num_failed += check_result (MHD_FUNC_, i, digest, 399 data_units2[i].digest); 400 } 401 MHD_SHA256_deinit (&ctx); 402 return num_failed; 403 } 404 405 406 /* Calculated SHA-256 as two iterations for whole data */ 407 static int 408 test2_str (void) 409 { 410 int num_failed = 0; 411 unsigned int i; 412 struct Sha256CtxWr ctx; 413 414 MHD_SHA256_init_one_time (&ctx); 415 for (i = 0; i < units1_num; i++) 416 { 417 uint8_t digest[SHA256_DIGEST_SIZE]; 418 size_t part_s = data_units1[i].str_l.len / 4; 419 420 MHD_SHA256_update (&ctx, (const uint8_t *) "", 0); 421 MHD_SHA256_update (&ctx, (const uint8_t *) data_units1[i].str_l.str, 422 part_s); 423 MHD_SHA256_update (&ctx, (const uint8_t *) "", 0); 424 MHD_SHA256_update (&ctx, (const uint8_t *) data_units1[i].str_l.str 425 + part_s, 426 data_units1[i].str_l.len - part_s); 427 MHD_SHA256_update (&ctx, (const uint8_t *) "", 0); 428 MHD_SHA256_finish_reset (&ctx, digest); 429 #ifdef MHD_SHA256_HAS_EXT_ERROR 430 if (0 != ctx.ext_error) 431 { 432 fprintf (stderr, "External hashing error: %d.\n", ctx.ext_error); 433 exit (99); 434 } 435 #endif /* MHD_SHA256_HAS_EXT_ERROR */ 436 num_failed += check_result (MHD_FUNC_, i, digest, 437 data_units1[i].digest); 438 } 439 MHD_SHA256_deinit (&ctx); 440 return num_failed; 441 } 442 443 444 static int 445 test2_bin (void) 446 { 447 int num_failed = 0; 448 unsigned int i; 449 struct Sha256CtxWr ctx; 450 451 MHD_SHA256_init_one_time (&ctx); 452 for (i = 0; i < units2_num; i++) 453 { 454 uint8_t digest[SHA256_DIGEST_SIZE]; 455 size_t part_s = data_units2[i].bin_l.len * 2 / 3; 456 457 MHD_SHA256_update (&ctx, data_units2[i].bin_l.bin, part_s); 458 MHD_SHA256_update (&ctx, (const uint8_t *) "", 0); 459 MHD_SHA256_update (&ctx, data_units2[i].bin_l.bin + part_s, 460 data_units2[i].bin_l.len - part_s); 461 MHD_SHA256_finish_reset (&ctx, digest); 462 #ifdef MHD_SHA256_HAS_EXT_ERROR 463 if (0 != ctx.ext_error) 464 { 465 fprintf (stderr, "External hashing error: %d.\n", ctx.ext_error); 466 exit (99); 467 } 468 #endif /* MHD_SHA256_HAS_EXT_ERROR */ 469 num_failed += check_result (MHD_FUNC_, i, digest, 470 data_units2[i].digest); 471 } 472 MHD_SHA256_deinit (&ctx); 473 return num_failed; 474 } 475 476 477 /* Use data set number 7 as it has the longest sequence */ 478 #define DATA_POS 6 479 #define MAX_OFFSET 31 480 481 static int 482 test_unaligned (void) 483 { 484 int num_failed = 0; 485 unsigned int offset; 486 uint8_t *buf; 487 uint8_t *digest_buf; 488 struct Sha256CtxWr ctx; 489 490 const struct data_unit2 *const tdata = data_units2 + DATA_POS; 491 492 MHD_SHA256_init_one_time (&ctx); 493 buf = malloc (tdata->bin_l.len + MAX_OFFSET); 494 digest_buf = malloc (SHA256_DIGEST_SIZE + MAX_OFFSET); 495 if ((NULL == buf) || (NULL == digest_buf)) 496 exit (99); 497 498 for (offset = MAX_OFFSET; offset >= 1; --offset) 499 { 500 uint8_t *unaligned_digest; 501 uint8_t *unaligned_buf; 502 503 unaligned_buf = buf + offset; 504 memcpy (unaligned_buf, tdata->bin_l.bin, tdata->bin_l.len); 505 unaligned_digest = digest_buf + MAX_OFFSET - offset; 506 memset (unaligned_digest, 0, SHA256_DIGEST_SIZE); 507 508 MHD_SHA256_update (&ctx, unaligned_buf, tdata->bin_l.len); 509 MHD_SHA256_finish_reset (&ctx, unaligned_digest); 510 #ifdef MHD_SHA256_HAS_EXT_ERROR 511 if (0 != ctx.ext_error) 512 { 513 fprintf (stderr, "External hashing error: %d.\n", ctx.ext_error); 514 exit (99); 515 } 516 #endif /* MHD_SHA256_HAS_EXT_ERROR */ 517 num_failed += check_result (MHD_FUNC_, MAX_OFFSET - offset, 518 unaligned_digest, tdata->digest); 519 } 520 free (digest_buf); 521 free (buf); 522 MHD_SHA256_deinit (&ctx); 523 return num_failed; 524 } 525 526 527 int 528 main (int argc, char *argv[]) 529 { 530 int num_failed = 0; 531 (void) has_in_name; /* Mute compiler warning. */ 532 if (has_param (argc, argv, "-v") || has_param (argc, argv, "--verbose")) 533 verbose = 1; 534 535 #ifdef NEED_GCRYP_INIT 536 gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0); 537 #ifdef GCRYCTL_INITIALIZATION_FINISHED 538 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); 539 #endif /* GCRYCTL_INITIALIZATION_FINISHED */ 540 #endif /* NEED_GCRYP_INIT */ 541 542 num_failed += test1_str (); 543 num_failed += test1_bin (); 544 545 num_failed += test2_str (); 546 num_failed += test2_bin (); 547 548 num_failed += test_unaligned (); 549 550 return num_failed ? 1 : 0; 551 }