| 1 | /********************************************************************* |
| 2 | * Filename: md2_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 MD2 |
| 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 <string.h> |
| 17 | #include <memory.h> |
| 18 | #include "md2.h" |
| 19 | |
| 20 | /*********************** FUNCTION DEFINITIONS ***********************/ |
| 21 | int md2_test() |
| 22 | { |
| 23 | BYTE text1[] = {"abc"}; |
| 24 | BYTE text2[] = {"abcdefghijklmnopqrstuvwxyz"}; |
| 25 | BYTE text3_1[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"}; |
| 26 | BYTE text3_2[] = {"fghijklmnopqrstuvwxyz0123456789"}; |
| 27 | BYTE hash1[MD2_BLOCK_SIZE] = {0xda,0x85,0x3b,0x0d,0x3f,0x88,0xd9,0x9b,0x30,0x28,0x3a,0x69,0xe6,0xde,0xd6,0xbb}; |
| 28 | BYTE hash2[MD2_BLOCK_SIZE] = {0x4e,0x8d,0xdf,0xf3,0x65,0x02,0x92,0xab,0x5a,0x41,0x08,0xc3,0xaa,0x47,0x94,0x0b}; |
| 29 | BYTE hash3[MD2_BLOCK_SIZE] = {0xda,0x33,0xde,0xf2,0xa4,0x2d,0xf1,0x39,0x75,0x35,0x28,0x46,0xc3,0x03,0x38,0xcd}; |
| 30 | BYTE buf[16]; |
| 31 | MD2_CTX ctx; |
| 32 | int pass = 1; |
| 33 | |
| 34 | md2_init(&ctx); |
| 35 | md2_update(&ctx, text1, strlen(text1)); |
| 36 | md2_final(&ctx, buf); |
| 37 | pass = pass && !memcmp(hash1, buf, MD2_BLOCK_SIZE); |
| 38 | |
| 39 | // Note that the MD2 object can be re-used. |
| 40 | md2_init(&ctx); |
| 41 | md2_update(&ctx, text2, strlen(text2)); |
| 42 | md2_final(&ctx, buf); |
| 43 | pass = pass && !memcmp(hash2, buf, MD2_BLOCK_SIZE); |
| 44 | |
| 45 | // Note that the data is added in two chunks. |
| 46 | md2_init(&ctx); |
| 47 | md2_update(&ctx, text3_1, strlen(text3_1)); |
| 48 | md2_update(&ctx, text3_2, strlen(text3_2)); |
| 49 | md2_final(&ctx, buf); |
| 50 | pass = pass && !memcmp(hash3, buf, MD2_BLOCK_SIZE); |
| 51 | |
| 52 | return(pass); |
| 53 | } |
| 54 | |
| 55 | int main() |
| 56 | { |
| 57 | printf("MD2 tests: %s\n", md2_test() ? "SUCCEEDED" : "FAILED"); |
| 58 | } |