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