David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 1 | /* test_sha256.c - TinyCrypt implementation of some SHA-256 tests */ |
| 2 | |
| 3 | /* |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 4 | * Copyright (C) 2017 by Intel Corporation, All Rights Reserved. |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are met: |
| 8 | * |
| 9 | * - Redistributions of source code must retain the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * - Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * - Neither the name of Intel Corporation nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | * POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | DESCRIPTION |
| 35 | This module tests the following SHA256 routines: |
| 36 | |
| 37 | Scenarios tested include: |
| 38 | - NIST SHA256 test vectors |
| 39 | */ |
| 40 | |
| 41 | #include <tinycrypt/sha256.h> |
| 42 | #include <tinycrypt/constants.h> |
| 43 | #include <test_utils.h> |
| 44 | |
| 45 | #include <stdio.h> |
| 46 | #include <stdlib.h> |
| 47 | #include <string.h> |
| 48 | #include <stdint.h> |
| 49 | |
| 50 | /* |
| 51 | * NIST SHA256 test vector 1. |
| 52 | */ |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 53 | unsigned int test_1(void) |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 54 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 55 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 56 | |
| 57 | TC_PRINT("SHA256 test #1:\n"); |
| 58 | const uint8_t expected[32] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 59 | 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, |
| 60 | 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, |
| 61 | 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 62 | }; |
| 63 | const char *m = "abc"; |
| 64 | uint8_t digest[32]; |
| 65 | struct tc_sha256_state_struct s; |
| 66 | |
| 67 | (void)tc_sha256_init(&s); |
| 68 | tc_sha256_update(&s, (const uint8_t *) m, strlen(m)); |
| 69 | (void)tc_sha256_final(digest, &s); |
| 70 | result = check_result(1, expected, sizeof(expected), |
| 71 | digest, sizeof(digest)); |
| 72 | TC_END_RESULT(result); |
| 73 | return result; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * NIST SHA256 test vector 2. |
| 78 | */ |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 79 | unsigned int test_2(void) |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 80 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 81 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 82 | TC_PRINT("SHA256 test #2:\n"); |
| 83 | const uint8_t expected[32] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 84 | 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93, |
| 85 | 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, |
| 86 | 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 87 | }; |
| 88 | const char *m = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; |
| 89 | uint8_t digest[32]; |
| 90 | struct tc_sha256_state_struct s; |
| 91 | |
| 92 | (void)tc_sha256_init(&s); |
| 93 | tc_sha256_update(&s, (const uint8_t *) m, strlen(m)); |
| 94 | (void) tc_sha256_final(digest, &s); |
| 95 | |
| 96 | result = check_result(2, expected, sizeof(expected), |
| 97 | digest, sizeof(digest)); |
| 98 | TC_END_RESULT(result); |
| 99 | return result; |
| 100 | } |
| 101 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 102 | unsigned int test_3(void) |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 103 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 104 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 105 | TC_PRINT("SHA256 test #3:\n"); |
| 106 | const uint8_t expected[32] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 107 | 0x68, 0x32, 0x57, 0x20, 0xaa, 0xbd, 0x7c, 0x82, 0xf3, 0x0f, 0x55, 0x4b, |
| 108 | 0x31, 0x3d, 0x05, 0x70, 0xc9, 0x5a, 0xcc, 0xbb, 0x7d, 0xc4, 0xb5, 0xaa, |
| 109 | 0xe1, 0x12, 0x04, 0xc0, 0x8f, 0xfe, 0x73, 0x2b |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 110 | }; |
| 111 | const uint8_t m[1] = { 0xbd }; |
| 112 | uint8_t digest[32]; |
| 113 | struct tc_sha256_state_struct s; |
| 114 | |
| 115 | (void)tc_sha256_init(&s); |
| 116 | tc_sha256_update(&s, m, sizeof(m)); |
| 117 | (void)tc_sha256_final(digest, &s); |
| 118 | |
| 119 | result = check_result(3, expected, sizeof(expected), |
| 120 | digest, sizeof(digest)); |
| 121 | TC_END_RESULT(result); |
| 122 | return result; |
| 123 | } |
| 124 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 125 | unsigned int test_4(void) |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 126 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 127 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 128 | TC_PRINT("SHA256 test #4:\n"); |
| 129 | const uint8_t expected[32] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 130 | 0x7a, 0xbc, 0x22, 0xc0, 0xae, 0x5a, 0xf2, 0x6c, 0xe9, 0x3d, 0xbb, 0x94, |
| 131 | 0x43, 0x3a, 0x0e, 0x0b, 0x2e, 0x11, 0x9d, 0x01, 0x4f, 0x8e, 0x7f, 0x65, |
| 132 | 0xbd, 0x56, 0xc6, 0x1c, 0xcc, 0xcd, 0x95, 0x04 |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 133 | }; |
| 134 | const uint8_t m[4] = { 0xc9, 0x8c, 0x8e, 0x55 }; |
| 135 | uint8_t digest[32]; |
| 136 | struct tc_sha256_state_struct s; |
| 137 | |
| 138 | (void)tc_sha256_init(&s); |
| 139 | tc_sha256_update(&s, m, sizeof(m)); |
| 140 | (void)tc_sha256_final(digest, &s); |
| 141 | |
| 142 | result = check_result(4, expected, sizeof(expected), |
| 143 | digest, sizeof(digest)); |
| 144 | TC_END_RESULT(result); |
| 145 | return result; |
| 146 | } |
| 147 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 148 | unsigned int test_5(void) |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 149 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 150 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 151 | TC_PRINT("SHA256 test #5:\n"); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 152 | |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 153 | const uint8_t expected[32] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 154 | 0x02, 0x77, 0x94, 0x66, 0xcd, 0xec, 0x16, 0x38, 0x11, 0xd0, 0x78, 0x81, |
| 155 | 0x5c, 0x63, 0x3f, 0x21, 0x90, 0x14, 0x13, 0x08, 0x14, 0x49, 0x00, 0x2f, |
| 156 | 0x24, 0xaa, 0x3e, 0x80, 0xf0, 0xb8, 0x8e, 0xf7 |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 157 | }; |
| 158 | uint8_t m[55]; |
| 159 | uint8_t digest[32]; |
| 160 | struct tc_sha256_state_struct s; |
| 161 | |
| 162 | (void)memset(m, 0x00, sizeof(m)); |
| 163 | |
| 164 | (void)tc_sha256_init(&s); |
| 165 | tc_sha256_update(&s, m, sizeof(m)); |
| 166 | (void)tc_sha256_final(digest, &s); |
| 167 | |
| 168 | result = check_result(5, expected, sizeof(expected), |
| 169 | digest, sizeof(digest)); |
| 170 | TC_END_RESULT(result); |
| 171 | return result; |
| 172 | } |
| 173 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 174 | unsigned int test_6(void) |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 175 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 176 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 177 | TC_PRINT("SHA256 test #6:\n"); |
| 178 | const uint8_t expected[32] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 179 | 0xd4, 0x81, 0x7a, 0xa5, 0x49, 0x76, 0x28, 0xe7, 0xc7, 0x7e, 0x6b, 0x60, |
| 180 | 0x61, 0x07, 0x04, 0x2b, 0xbb, 0xa3, 0x13, 0x08, 0x88, 0xc5, 0xf4, 0x7a, |
| 181 | 0x37, 0x5e, 0x61, 0x79, 0xbe, 0x78, 0x9f, 0xbb |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 182 | }; |
| 183 | uint8_t m[56]; |
| 184 | uint8_t digest[32]; |
| 185 | struct tc_sha256_state_struct s; |
| 186 | |
| 187 | (void)memset(m, 0x00, sizeof(m)); |
| 188 | |
| 189 | (void)tc_sha256_init(&s); |
| 190 | tc_sha256_update(&s, m, sizeof(m)); |
| 191 | (void)tc_sha256_final(digest, &s); |
| 192 | |
| 193 | result = check_result(6, expected, sizeof(expected), |
| 194 | digest, sizeof(digest)); |
| 195 | TC_END_RESULT(result); |
| 196 | return result; |
| 197 | } |
| 198 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 199 | unsigned int test_7(void) |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 200 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 201 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 202 | TC_PRINT("SHA256 test #7:\n"); |
| 203 | const uint8_t expected[32] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 204 | 0x65, 0xa1, 0x6c, 0xb7, 0x86, 0x13, 0x35, 0xd5, 0xac, 0xe3, 0xc6, 0x07, |
| 205 | 0x18, 0xb5, 0x05, 0x2e, 0x44, 0x66, 0x07, 0x26, 0xda, 0x4c, 0xd1, 0x3b, |
| 206 | 0xb7, 0x45, 0x38, 0x1b, 0x23, 0x5a, 0x17, 0x85 |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 207 | }; |
| 208 | uint8_t m[57]; |
| 209 | uint8_t digest[32]; |
| 210 | struct tc_sha256_state_struct s; |
| 211 | |
| 212 | (void)memset(m, 0x00, sizeof(m)); |
| 213 | |
| 214 | (void)tc_sha256_init(&s); |
| 215 | tc_sha256_update(&s, m, sizeof(m)); |
| 216 | (void)tc_sha256_final(digest, &s); |
| 217 | |
| 218 | result = check_result(7, expected, sizeof(expected), |
| 219 | digest, sizeof(digest)); |
| 220 | TC_END_RESULT(result); |
| 221 | return result; |
| 222 | } |
| 223 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 224 | unsigned int test_8(void) |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 225 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 226 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 227 | |
| 228 | TC_PRINT("SHA256 test #8:\n"); |
| 229 | const uint8_t expected[32] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 230 | 0xf5, 0xa5, 0xfd, 0x42, 0xd1, 0x6a, 0x20, 0x30, 0x27, 0x98, 0xef, 0x6e, |
| 231 | 0xd3, 0x09, 0x97, 0x9b, 0x43, 0x00, 0x3d, 0x23, 0x20, 0xd9, 0xf0, 0xe8, |
| 232 | 0xea, 0x98, 0x31, 0xa9, 0x27, 0x59, 0xfb, 0x4b |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 233 | }; |
| 234 | uint8_t m[64]; |
| 235 | uint8_t digest[32]; |
| 236 | struct tc_sha256_state_struct s; |
| 237 | |
| 238 | (void)memset(m, 0x00, sizeof(m)); |
| 239 | |
| 240 | (void)tc_sha256_init(&s); |
| 241 | tc_sha256_update(&s, m, sizeof(m)); |
| 242 | (void)tc_sha256_final(digest, &s); |
| 243 | |
| 244 | result = check_result(8, expected, sizeof(expected), |
| 245 | digest, sizeof(digest)); |
| 246 | TC_END_RESULT(result); |
| 247 | return result; |
| 248 | } |
| 249 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 250 | unsigned int test_9(void) |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 251 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 252 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 253 | TC_PRINT("SHA256 test #9:\n"); |
| 254 | const uint8_t expected[32] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 255 | 0x54, 0x1b, 0x3e, 0x9d, 0xaa, 0x09, 0xb2, 0x0b, 0xf8, 0x5f, 0xa2, 0x73, |
| 256 | 0xe5, 0xcb, 0xd3, 0xe8, 0x01, 0x85, 0xaa, 0x4e, 0xc2, 0x98, 0xe7, 0x65, |
| 257 | 0xdb, 0x87, 0x74, 0x2b, 0x70, 0x13, 0x8a, 0x53 |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 258 | }; |
| 259 | uint8_t m[1000]; |
| 260 | uint8_t digest[32]; |
| 261 | struct tc_sha256_state_struct s; |
| 262 | |
| 263 | (void)memset(m, 0x00, sizeof(m)); |
| 264 | |
| 265 | (void)tc_sha256_init(&s); |
| 266 | tc_sha256_update(&s, m, sizeof(m)); |
| 267 | (void)tc_sha256_final(digest, &s); |
| 268 | |
| 269 | result = check_result(9, expected, sizeof(expected), |
| 270 | digest, sizeof(digest)); |
| 271 | TC_END_RESULT(result); |
| 272 | return result; |
| 273 | } |
| 274 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 275 | unsigned int test_10(void) |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 276 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 277 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 278 | TC_PRINT("SHA256 test #10:\n"); |
| 279 | const uint8_t expected[32] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 280 | 0xc2, 0xe6, 0x86, 0x82, 0x34, 0x89, 0xce, 0xd2, 0x01, 0x7f, 0x60, 0x59, |
| 281 | 0xb8, 0xb2, 0x39, 0x31, 0x8b, 0x63, 0x64, 0xf6, 0xdc, 0xd8, 0x35, 0xd0, |
| 282 | 0xa5, 0x19, 0x10, 0x5a, 0x1e, 0xad, 0xd6, 0xe4 |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 283 | }; |
| 284 | uint8_t m[1000]; |
| 285 | uint8_t digest[32]; |
| 286 | struct tc_sha256_state_struct s; |
| 287 | |
| 288 | (void)memset(m, 0x41, sizeof(m)); |
| 289 | |
| 290 | (void)tc_sha256_init(&s); |
| 291 | tc_sha256_update(&s, m, sizeof(m)); |
| 292 | (void)tc_sha256_final(digest, &s); |
| 293 | |
| 294 | result = check_result(10, expected, sizeof(expected), |
| 295 | digest, sizeof(digest)); |
| 296 | TC_END_RESULT(result); |
| 297 | return result; |
| 298 | } |
| 299 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 300 | unsigned int test_11(void) |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 301 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 302 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 303 | TC_PRINT("SHA256 test #11:\n"); |
| 304 | const uint8_t expected[32] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 305 | 0xf4, 0xd6, 0x2d, 0xde, 0xc0, 0xf3, 0xdd, 0x90, 0xea, 0x13, 0x80, 0xfa, |
| 306 | 0x16, 0xa5, 0xff, 0x8d, 0xc4, 0xc5, 0x4b, 0x21, 0x74, 0x06, 0x50, 0xf2, |
| 307 | 0x4a, 0xfc, 0x41, 0x20, 0x90, 0x35, 0x52, 0xb0 |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 308 | }; |
| 309 | uint8_t m[1005]; |
| 310 | uint8_t digest[32]; |
| 311 | struct tc_sha256_state_struct s; |
| 312 | |
| 313 | (void)memset(m, 0x55, sizeof(m)); |
| 314 | |
| 315 | (void)tc_sha256_init(&s); |
| 316 | tc_sha256_update(&s, m, sizeof(m)); |
| 317 | (void)tc_sha256_final(digest, &s); |
| 318 | |
| 319 | result = check_result(11, expected, sizeof(expected), |
| 320 | digest, sizeof(digest)); |
| 321 | TC_END_RESULT(result); |
| 322 | return result; |
| 323 | } |
| 324 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 325 | unsigned int test_12(void) |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 326 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 327 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 328 | TC_PRINT("SHA256 test #12:\n"); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 329 | |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 330 | const uint8_t expected[32] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 331 | 0xd2, 0x97, 0x51, 0xf2, 0x64, 0x9b, 0x32, 0xff, 0x57, 0x2b, 0x5e, 0x0a, |
| 332 | 0x9f, 0x54, 0x1e, 0xa6, 0x60, 0xa5, 0x0f, 0x94, 0xff, 0x0b, 0xee, 0xdf, |
| 333 | 0xb0, 0xb6, 0x92, 0xb9, 0x24, 0xcc, 0x80, 0x25 |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 334 | }; |
| 335 | uint8_t m[1000]; |
| 336 | uint8_t digest[32]; |
| 337 | struct tc_sha256_state_struct s; |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 338 | unsigned int i; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 339 | |
| 340 | (void)memset(m, 0x00, sizeof(m)); |
| 341 | |
| 342 | (void)tc_sha256_init(&s); |
| 343 | for (i = 0; i < 1000; ++i) { |
| 344 | tc_sha256_update(&s, m, sizeof(m)); |
| 345 | } |
| 346 | (void)tc_sha256_final(digest, &s); |
| 347 | |
| 348 | result = check_result(12, expected, sizeof(expected), |
| 349 | digest, sizeof(digest)); |
| 350 | TC_END_RESULT(result); |
| 351 | return result; |
| 352 | } |
| 353 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 354 | unsigned int test_13(void) |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 355 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 356 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 357 | TC_PRINT("SHA256 test #13:\n"); |
| 358 | const uint8_t expected[32] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 359 | 0x15, 0xa1, 0x86, 0x8c, 0x12, 0xcc, 0x53, 0x95, 0x1e, 0x18, 0x23, 0x44, |
| 360 | 0x27, 0x74, 0x47, 0xcd, 0x09, 0x79, 0x53, 0x6b, 0xad, 0xcc, 0x51, 0x2a, |
| 361 | 0xd2, 0x4c, 0x67, 0xe9, 0xb2, 0xd4, 0xf3, 0xdd |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 362 | }; |
| 363 | uint8_t m[32768]; |
| 364 | uint8_t digest[32]; |
| 365 | struct tc_sha256_state_struct s; |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 366 | unsigned int i; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 367 | |
| 368 | (void)memset(m, 0x5a, sizeof(m)); |
| 369 | |
| 370 | (void)tc_sha256_init(&s); |
| 371 | for (i = 0; i < 16384; ++i) { |
| 372 | tc_sha256_update(&s, m, sizeof(m)); |
| 373 | } |
| 374 | (void)tc_sha256_final(digest, &s); |
| 375 | |
| 376 | result = check_result(13, expected, sizeof(expected), |
| 377 | digest, sizeof(digest)); |
| 378 | TC_END_RESULT(result); |
| 379 | return result; |
| 380 | } |
| 381 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 382 | unsigned int test_14(void) |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 383 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 384 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 385 | TC_PRINT("SHA256 test #14:\n"); |
| 386 | const uint8_t expected[32] = { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 387 | 0x46, 0x1c, 0x19, 0xa9, 0x3b, 0xd4, 0x34, 0x4f, 0x92, 0x15, 0xf5, 0xec, |
| 388 | 0x64, 0x35, 0x70, 0x90, 0x34, 0x2b, 0xc6, 0x6b, 0x15, 0xa1, 0x48, 0x31, |
| 389 | 0x7d, 0x27, 0x6e, 0x31, 0xcb, 0xc2, 0x0b, 0x53 |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 390 | }; |
| 391 | uint8_t m[32768]; |
| 392 | uint8_t digest[32]; |
| 393 | struct tc_sha256_state_struct s; |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 394 | unsigned int i; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 395 | |
| 396 | (void)memset(m, 0x00, sizeof(m)); |
| 397 | |
| 398 | (void) tc_sha256_init(&s); |
| 399 | for (i = 0; i < 33280; ++i) { |
| 400 | tc_sha256_update(&s, m, sizeof(m)); |
| 401 | } |
| 402 | (void) tc_sha256_final(digest, &s); |
| 403 | |
| 404 | result = check_result(14, expected, sizeof(expected), |
| 405 | digest, sizeof(digest)); |
| 406 | TC_END_RESULT(result); |
| 407 | return result; |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * Main task to test AES |
| 412 | */ |
| 413 | |
| 414 | int main(void) |
| 415 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 416 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 417 | TC_START("Performing SHA256 tests (NIST tests vectors):"); |
| 418 | |
| 419 | result = test_1(); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 420 | if (result == TC_FAIL) { |
| 421 | /* terminate test */ |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 422 | TC_ERROR("SHA256 test #1 failed.\n"); |
| 423 | goto exitTest; |
| 424 | } |
| 425 | result = test_2(); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 426 | if (result == TC_FAIL) { |
| 427 | /* terminate test */ |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 428 | TC_ERROR("SHA256 test #2 failed.\n"); |
| 429 | goto exitTest; |
| 430 | } |
| 431 | result = test_3(); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 432 | if (result == TC_FAIL) { |
| 433 | /* terminate test */ |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 434 | TC_ERROR("SHA256 test #3 failed.\n"); |
| 435 | goto exitTest; |
| 436 | } |
| 437 | result = test_4(); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 438 | if (result == TC_FAIL) { |
| 439 | /* terminate test */ |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 440 | TC_ERROR("SHA256 test #4 failed.\n"); |
| 441 | goto exitTest; |
| 442 | } |
| 443 | result = test_5(); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 444 | if (result == TC_FAIL) { |
| 445 | /* terminate test */ |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 446 | TC_ERROR("SHA256 test #5 failed.\n"); |
| 447 | goto exitTest; |
| 448 | } |
| 449 | result = test_6(); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 450 | if (result == TC_FAIL) { |
| 451 | /* terminate test */ |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 452 | TC_ERROR("SHA256 test #6 failed.\n"); |
| 453 | goto exitTest; |
| 454 | } |
| 455 | result = test_7(); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 456 | if (result == TC_FAIL) { |
| 457 | /* terminate test */ |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 458 | TC_ERROR("SHA256 test #7 failed.\n"); |
| 459 | goto exitTest; |
| 460 | } |
| 461 | result = test_8(); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 462 | if (result == TC_FAIL) { |
| 463 | /* terminate test */ |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 464 | TC_ERROR("SHA256 test #8 failed.\n"); |
| 465 | goto exitTest; |
| 466 | } |
| 467 | result = test_9(); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 468 | if (result == TC_FAIL) { |
| 469 | /* terminate test */ |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 470 | TC_ERROR("SHA256 test #9 failed.\n"); |
| 471 | goto exitTest; |
| 472 | } |
| 473 | result = test_10(); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 474 | if (result == TC_FAIL) { |
| 475 | /* terminate test */ |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 476 | TC_ERROR("SHA256 test #10 failed.\n"); |
| 477 | goto exitTest; |
| 478 | } |
| 479 | result = test_11(); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 480 | if (result == TC_FAIL) { |
| 481 | /* terminate test */ |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 482 | TC_ERROR("SHA256 test #11 failed.\n"); |
| 483 | goto exitTest; |
| 484 | } |
| 485 | result = test_12(); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 486 | if (result == TC_FAIL) { |
| 487 | /* terminate test */ |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 488 | TC_ERROR("SHA256 test #12 failed.\n"); |
| 489 | goto exitTest; |
| 490 | } |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 491 | /* memory and computation intensive test cases: */ |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 492 | result = test_13(); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 493 | if (result == TC_FAIL) { |
| 494 | /* terminate test */ |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 495 | TC_ERROR("SHA256 test #13 failed.\n"); |
| 496 | goto exitTest; |
| 497 | } |
| 498 | result = test_14(); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 499 | if (result == TC_FAIL) { |
| 500 | /* terminate test */ |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 501 | TC_ERROR("SHA256 test #14 failed.\n"); |
| 502 | goto exitTest; |
| 503 | } |
| 504 | |
| 505 | TC_PRINT("All SHA256 tests succeeded!\n"); |
| 506 | |
| 507 | exitTest: |
| 508 | TC_END_RESULT(result); |
| 509 | TC_END_REPORT(result); |
| 510 | } |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 511 | |