blob: 65cbb4627070dcda5c2851f6ca590bc6f82d38c5 [file] [log] [blame]
Gaurav Shah322536d2010-01-28 15:01:23 -08001/* 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
Gaurav Shah5411c7a2010-03-31 10:56:49 -070012#include "cryptolib.h"
Gaurav Shah322536d2010-01-28 15:01:23 -080013#include "sha_test_vectors.h"
14
15int SHA1_tests(void) {
16 int i, success = 1;
17 uint8_t sha1_digest[SHA1_DIGEST_SIZE];
18 uint8_t* test_inputs[3];
19 test_inputs[0] = (uint8_t *) oneblock_msg;
20 test_inputs[1] = (uint8_t *) multiblock_msg1;
21 test_inputs[2] = (uint8_t *) long_msg;
22
23 for (i = 0; i < 3; i++) {
24 SHA1(test_inputs[i], strlen((char *)test_inputs[i]),
25 sha1_digest);
26 if (!memcmp(sha1_digest, sha1_results[i], SHA1_DIGEST_SIZE)) {
27 fprintf(stderr, "Test vector %d PASSED for SHA-1\n", i+1);
28 }
29 else {
30 fprintf(stderr, "Test vector %d FAILED for SHA-1\n", i+1);
31 success = 0;
32 }
33 }
34 return success;
35}
36
37int SHA256_tests(void) {
38 int i, success = 1;
39 uint8_t sha256_digest[SHA256_DIGEST_SIZE];
40 uint8_t* test_inputs[3];
41 test_inputs[0] = (uint8_t *) oneblock_msg;
42 test_inputs[1] = (uint8_t *) multiblock_msg1;
43 test_inputs[2] = (uint8_t *) long_msg;
44
45 for (i = 0; i < 3; i++) {
46 SHA256(test_inputs[i], strlen((char *)test_inputs[i]),
47 sha256_digest);
48 if (!memcmp(sha256_digest, sha256_results[i], SHA256_DIGEST_SIZE)) {
49 fprintf(stderr, "Test vector %d PASSED for SHA-256\n", i+1);
50 }
51 else {
52 fprintf(stderr, "Test vector %d FAILED for SHA-256\n", i+1);
53 success = 0;
54 }
55 }
56 return success;
57}
58
59int SHA512_tests(void) {
60 int i, success = 1;
61 uint8_t sha512_digest[SHA512_DIGEST_SIZE];
62 uint8_t* test_inputs[3];
63 test_inputs[0] = (uint8_t *) oneblock_msg;
64 test_inputs[1] = (uint8_t *) multiblock_msg2;
65 test_inputs[2] = (uint8_t *) long_msg;
66
67 for (i = 0; i < 3; i++) {
68 SHA512(test_inputs[i], strlen((char *)test_inputs[i]),
69 sha512_digest);
70 if (!memcmp(sha512_digest, sha512_results[i], SHA512_DIGEST_SIZE)) {
71 fprintf(stderr, "Test vector %d PASSED for SHA-512\n", i+1);
72 }
73 else {
74 fprintf(stderr, "Test vector %d FAILED for SHA-512\n", i+1);
75 success = 0;
76 }
77 }
78 return success;
79}
80
vbendeb3ecaf772010-06-24 16:19:53 -070081/* disable MSVC warnings on unused arguments */
82__pragma(warning (disable: 4100))
83
Gaurav Shah322536d2010-01-28 15:01:23 -080084int main(int argc, char* argv[]) {
85 int success = 1;
86 /* Initialize long_msg with 'a' x 1,000,000 */
87 long_msg = (char *) malloc(1000001);
88 memset(long_msg, 'a', 1000000);
89 long_msg[1000000]=0;
90
91 if (!SHA1_tests())
92 success = 0;
93 if (!SHA256_tests())
94 success = 0;
95 if (!SHA512_tests())
96 success = 0;
97
98 free(long_msg);
99
100 return !success;
101}