ce188d4d |
1 | /********************************************************************* |
2 | * Filename: sha1_test.c |
3 | * Author: Brad Conte (brad AT bradconte.com) |
4 | * Copyright: |
5 | * Disclaimer: This code is presented "as is" without any guarantees. |
6 | * Details: Performs known-answer tests on the corresponding SHA1 |
7 | implementation. These tests do not encompass the full |
8 | range of available test vectors, however, if the tests |
9 | pass it is very, very likely that the code is correct |
10 | and was compiled properly. This code also serves as |
11 | example usage of the functions. |
12 | *********************************************************************/ |
13 | |
14 | /*************************** HEADER FILES ***************************/ |
15 | #include <stdio.h> |
16 | #include <memory.h> |
17 | #include <string.h> |
18 | #include "sha1.h" |
19 | |
20 | /*********************** FUNCTION DEFINITIONS ***********************/ |
21 | int sha1_test() |
22 | { |
23 | BYTE text1[] = {"abc"}; |
24 | BYTE text2[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"}; |
25 | BYTE text3[] = {"aaaaaaaaaa"}; |
26 | BYTE hash1[SHA1_BLOCK_SIZE] = {0xa9,0x99,0x3e,0x36,0x47,0x06,0x81,0x6a,0xba,0x3e,0x25,0x71,0x78,0x50,0xc2,0x6c,0x9c,0xd0,0xd8,0x9d}; |
27 | BYTE hash2[SHA1_BLOCK_SIZE] = {0x84,0x98,0x3e,0x44,0x1c,0x3b,0xd2,0x6e,0xba,0xae,0x4a,0xa1,0xf9,0x51,0x29,0xe5,0xe5,0x46,0x70,0xf1}; |
28 | BYTE hash3[SHA1_BLOCK_SIZE] = {0x34,0xaa,0x97,0x3c,0xd4,0xc4,0xda,0xa4,0xf6,0x1e,0xeb,0x2b,0xdb,0xad,0x27,0x31,0x65,0x34,0x01,0x6f}; |
29 | BYTE buf[SHA1_BLOCK_SIZE]; |
30 | int idx; |
31 | SHA1_CTX ctx; |
32 | int pass = 1; |
33 | |
34 | sha1_init(&ctx); |
35 | sha1_update(&ctx, text1, strlen(text1)); |
36 | sha1_final(&ctx, buf); |
37 | pass = pass && !memcmp(hash1, buf, SHA1_BLOCK_SIZE); |
38 | |
39 | sha1_init(&ctx); |
40 | sha1_update(&ctx, text2, strlen(text2)); |
41 | sha1_final(&ctx, buf); |
42 | pass = pass && !memcmp(hash2, buf, SHA1_BLOCK_SIZE); |
43 | |
44 | sha1_init(&ctx); |
45 | for (idx = 0; idx < 100000; ++idx) |
46 | sha1_update(&ctx, text3, strlen(text3)); |
47 | sha1_final(&ctx, buf); |
48 | pass = pass && !memcmp(hash3, buf, SHA1_BLOCK_SIZE); |
49 | |
50 | return(pass); |
51 | } |
52 | |
53 | int main() |
54 | { |
55 | printf("SHA1 tests: %s\n", sha1_test() ? "SUCCEEDED" : "FAILED"); |
56 | |
57 | return(0); |
58 | } |