blob: ad85b9eacaee0e1636e552fd1ecd28dcf6b3d777 [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
Bill Richardson0c3ba242013-03-29 11:09:30 -07008#include <stdint.h>
Gaurav Shah322536d2010-01-28 15:01:23 -08009#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
Gaurav Shah5411c7a2010-03-31 10:56:49 -070013#include "cryptolib.h"
Gaurav Shah322536d2010-01-28 15:01:23 -080014#include "sha_test_vectors.h"
15
16int 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++) {
Kees Cook201fe0b2012-05-22 18:05:53 -070025 internal_SHA1(test_inputs[i], strlen((char *)test_inputs[i]),
Gaurav Shah322536d2010-01-28 15:01:23 -080026 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
38int 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++) {
Kees Cook201fe0b2012-05-22 18:05:53 -070047 internal_SHA256(test_inputs[i], strlen((char *)test_inputs[i]),
Gaurav Shah322536d2010-01-28 15:01:23 -080048 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
60int 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++) {
Kees Cook201fe0b2012-05-22 18:05:53 -070069 internal_SHA512(test_inputs[i], strlen((char *)test_inputs[i]),
Gaurav Shah322536d2010-01-28 15:01:23 -080070 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
Gaurav Shah322536d2010-01-28 15:01:23 -080082int 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}