OLD | NEW |
(Empty) | |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. |
| 4 */ |
| 5 |
| 6 /* FIPS 180-2 Tests for message digest functions. */ |
| 7 |
| 8 #include <stdio.h> |
| 9 #include <stdlib.h> |
| 10 #include <string.h> |
| 11 |
| 12 #include "sha.h" |
| 13 |
| 14 #include "sha_test_vectors.h" |
| 15 |
| 16 int SHA1_tests(void) { |
| 17 int i, success = 1; |
| 18 uint8_t sha1_digest[SHA1_DIGEST_SIZE]; |
| 19 uint8_t* test_inputs[3]; |
| 20 test_inputs[0] = (uint8_t *) oneblock_msg; |
| 21 test_inputs[1] = (uint8_t *) multiblock_msg1; |
| 22 test_inputs[2] = (uint8_t *) long_msg; |
| 23 |
| 24 for (i = 0; i < 3; i++) { |
| 25 SHA1(test_inputs[i], strlen((char *)test_inputs[i]), |
| 26 sha1_digest); |
| 27 if (!memcmp(sha1_digest, sha1_results[i], SHA1_DIGEST_SIZE)) { |
| 28 fprintf(stderr, "Test vector %d PASSED for SHA-1\n", i+1); |
| 29 } |
| 30 else { |
| 31 fprintf(stderr, "Test vector %d FAILED for SHA-1\n", i+1); |
| 32 success = 0; |
| 33 } |
| 34 } |
| 35 return success; |
| 36 } |
| 37 |
| 38 int SHA256_tests(void) { |
| 39 int i, success = 1; |
| 40 uint8_t sha256_digest[SHA256_DIGEST_SIZE]; |
| 41 uint8_t* test_inputs[3]; |
| 42 test_inputs[0] = (uint8_t *) oneblock_msg; |
| 43 test_inputs[1] = (uint8_t *) multiblock_msg1; |
| 44 test_inputs[2] = (uint8_t *) long_msg; |
| 45 |
| 46 for (i = 0; i < 3; i++) { |
| 47 SHA256(test_inputs[i], strlen((char *)test_inputs[i]), |
| 48 sha256_digest); |
| 49 if (!memcmp(sha256_digest, sha256_results[i], SHA256_DIGEST_SIZE)) { |
| 50 fprintf(stderr, "Test vector %d PASSED for SHA-256\n", i+1); |
| 51 } |
| 52 else { |
| 53 fprintf(stderr, "Test vector %d FAILED for SHA-256\n", i+1); |
| 54 success = 0; |
| 55 } |
| 56 } |
| 57 return success; |
| 58 } |
| 59 |
| 60 int SHA512_tests(void) { |
| 61 int i, success = 1; |
| 62 uint8_t sha512_digest[SHA512_DIGEST_SIZE]; |
| 63 uint8_t* test_inputs[3]; |
| 64 test_inputs[0] = (uint8_t *) oneblock_msg; |
| 65 test_inputs[1] = (uint8_t *) multiblock_msg2; |
| 66 test_inputs[2] = (uint8_t *) long_msg; |
| 67 |
| 68 for (i = 0; i < 3; i++) { |
| 69 SHA512(test_inputs[i], strlen((char *)test_inputs[i]), |
| 70 sha512_digest); |
| 71 if (!memcmp(sha512_digest, sha512_results[i], SHA512_DIGEST_SIZE)) { |
| 72 fprintf(stderr, "Test vector %d PASSED for SHA-512\n", i+1); |
| 73 } |
| 74 else { |
| 75 fprintf(stderr, "Test vector %d FAILED for SHA-512\n", i+1); |
| 76 success = 0; |
| 77 } |
| 78 } |
| 79 return success; |
| 80 } |
| 81 |
| 82 int main(int argc, char* argv[]) { |
| 83 int success = 1; |
| 84 /* Initialize long_msg with 'a' x 1,000,000 */ |
| 85 long_msg = (char *) malloc(1000001); |
| 86 memset(long_msg, 'a', 1000000); |
| 87 long_msg[1000000]=0; |
| 88 |
| 89 if (!SHA1_tests()) |
| 90 success = 0; |
| 91 if (!SHA256_tests()) |
| 92 success = 0; |
| 93 if (!SHA512_tests()) |
| 94 success = 0; |
| 95 |
| 96 free(long_msg); |
| 97 |
| 98 return !success; |
| 99 } |
OLD | NEW |