md5_test.c ( File view )

  • By 残星浮梦 2016-01-13
  • View(s):0
  • Download(s):2
  • Point(s): 1
			/*
 * embedded IPsec
 * Copyright (c) 2003 Niklaus Schild and Christian Scheurer, HTI Biel/Bienne
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 */

/** @file md5_test.c
 *  @brief This module contains all the test functions used to test MD5
 *
 *  @author Niklaus Schild <n.schild@gmx.ch> <BR>
 *
 *  <B>OUTLINE:</B>
 * The purpose of this module is to provide structural tests for the MD5 functions.
 * The tests we did here are not how they should be. Because we took over the code 
 * from another project (OpenSSL) which is widely used and tested we assume that they 
 * work basically (yes I know assumptions are the worst mistakes of programmers).
 *
 *  <B>IMPLEMENTATION:</B>
 *  We call the four basic functions used to implement a HMAC and let them do their job.
 *  After the function is called we compare the result with the expected output.
 *
 *  Whenever a test fails we print out the INPUT, OUTPUT and the EXPECTED OUTPUT.
 *
 *  <B>NOTES:</B>
 *  This test do NOT fully prove the correctness of this code.
 *
 * This document is part of <EM>embedded IPsec<BR>
 * Copyright (c) 2003 Niklaus Schild and Christian Scheurer, HTI Biel/Bienne<BR>
 * All rights reserved.</EM><HR>
 */

#include <string.h>

#include "ipsec/util.h"
#include "ipsec/md5.h"
#include "ipsec/debug.h"
#include "testing/structural/structural_test.h"


/**
 * Testfunciton for MD5_Init
 * @return int number of tests failed in this function
 */
int md5_test_MD5_Init(void)
{

	MD5_CTX orig = {
 0x67452301L, 0xefcdab89L, 0x98badcfeL, 0x10325476L, 0, 0, {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
}, 0
} ;
	MD5_CTX input, test;

	memset(&input, 0, sizeof(MD5_CTX)) ;
	memcpy(&test, &input, sizeof(MD5_CTX)) ;

	MD5_Init(&test) ;

	if(memcmp(&test, &orig, sizeof(MD5_CTX)) != 0)
	{

		IPSEC_LOG_TST("md5_test_init", "FAILURE", ("MD5_Init() failed")) ;
		printf("     INPUT:\n") ;
		IPSEC_DUMP_BUFFER("          ", (char*)&input, 0, sizeof(MD5_CTX));
		printf("     OUTPUT:\n") ;
		IPSEC_DUMP_BUFFER("          ", (char*)&test, 0, sizeof(MD5_CTX));
		printf("     EXPECTED OUTPUT:\n") ;
		IPSEC_DUMP_BUFFER("          ", (char*)&orig, 0, sizeof(MD5_CTX));

		return 1;
	
}

	return 0 ;

}

/**
 * Testfunciton for MD5_Update
 * @return int number of tests failed in this function
 */
int md5_test_MD5_Update()
{

	MD5_CTX orig = {
 	0xCB6180E8L, 0x2FA83EA8L, 0x43278D6CL, 0xB9526934,
						0x000002E0, 0x0, 
						0x74616877, 0x206F6420, 0x77206179, 0x20746E61, 0x20726F66, 0x68746F6E, 0x3F676E69,      
						0, 0, 0, 0, 0, 0, 0, 0, 0,
						0x001C          
					
} ;
	MD5_CTX test, input ;

	unsigned char k_ipad[65];
    unsigned char k_opad[65];


	char text[] = "what do ya want for nothing?" ;
	int text_len = 28 ;

	char key[] = "Jefe" ;
	int key_len = 4 ;

	int i ;

	memset(k_ipad, '\0', sizeof(k_ipad));  
	memset(k_opad, '\0', sizeof(k_opad));  
	memcpy(k_ipad, key, key_len); 		   
	memcpy(k_opad, key, key_len); 

	for (i=0; i<64; i++) 
	{

		k_ipad[i] ^= 0x36;
		k_opad[i] ^= 0x5c;
	
}

	memset(&input, 0, sizeof(MD5_CTX)) ;

	MD5_Init(&input);      
    
   	memcpy(&test, &input, sizeof(MD5_CTX)) ; 

	MD5_Update(&test, k_ipad, 64);  
	MD5_Update(&test, text, text_len);

	if(memcmp(&test, &orig, sizeof(MD5_CTX)) != 0)
	{

		IPSEC_LOG_TST("md5_test_update", "FAILURE", ("MD5_Update() failed")) ;
		printf("     INPUT:\n") ;
		IPSEC_DUMP_BUFFER("          ", (char*)&input, 0, sizeof(MD5_CTX));
		printf("     OUTPUT:\n") ;
		IPSEC_DUMP_BUFFER("          ", (char*)&test, 0, sizeof(MD5_CTX));
		printf("  
...
...
(Please download the complete source code to view)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.97 kB
01.97 kB
ah.c13.66 kB2003-12-12|13:49
des.c37.21 kB2003-12-12|01:02
esp.c14.04 kB2003-12-12|13:49
ipsec.c10.18 kB2003-12-12|13:49
md5.c18.27 kB2013-06-24|17:22
sa.c34.38 kB2003-12-12|01:02
sha1.c21.95 kB2003-12-04|14:55
util.c13.83 kB2004-06-19|18:49
01.97 kB
01.97 kB
ah.h2.99 kB2003-12-12|01:02
debug.h8.08 kB2003-12-12|11:41
des.h2.93 kB2003-12-12|01:02
esp.h3.24 kB2003-12-12|01:02
ipsec.h2.98 kB2003-12-12|01:02
md5.h2.97 kB2003-12-12|01:02
sa.h9.98 kB2003-12-12|01:02
sha1.h2.88 kB2003-12-12|01:02
types.h6.17 kB2003-12-12|01:02
util.h3.81 kB2003-12-12|01:02
01.97 kB
dumpdev.h2.30 kB2003-12-12|01:02
ipsecdev.h3.03 kB2003-12-12|01:02
01.97 kB
01.97 kB
keil_1000_ah_md5.h2.51 kB2003-12-12|01:02
keil_1001_ah_sha1.h2.53 kB2003-12-12|01:02
keil_1002_esp_3des.h2.55 kB2003-12-12|01:02
keil_1003_esp_3des_md5.h2.68 kB2003-12-12|01:02
keil_1004_esp_3des_sha1.h2.71 kB2003-12-12|01:02
keil_bypass.h2.19 kB2003-12-12|01:02
keil_netconfig.h2.24 kB2003-12-12|01:02
phy_2000_ah_md5.h2.51 kB2003-12-12|01:02
phy_2001_ah_sha1.h2.53 kB2003-12-12|01:02
phy_2002_esp_3des.h2.55 kB2003-12-12|01:02
phy_2003_esp_3des_md5.h2.68 kB2003-12-12|01:02
phy_2004_esp_3des_sha1.h2.71 kB2003-12-12|01:02
phy_bypass.h2.20 kB2003-12-12|01:02
phy_netconfig.h2.25 kB2003-12-12|01:02
01.97 kB
01.97 kB
dumpdev-espdata.h4.51 kB2003-12-12|01:02
dumpdev-espdata.zip3.09 kB2003-11-24|19:45
dumpdev-httpgetdata.h28.08 kB2003-12-12|01:02
dumpdev-httpgetdata.zip32.70 kB2003-11-11|17:51
dumpdev-pingdata.h6.58 kB2003-12-12|01:02
dumpdev-pingdata.zip8.07 kB2003-11-11|17:51
01.97 kB
ah_test-sample_ah_packet.h3.46 kB2003-12-12|01:42
ah_test-sample_ah_packet.zip1.76 kB2003-11-18|19:12
structural_test.h3.16 kB2003-12-12|01:02
01.97 kB
dumpdev.c14.90 kB2003-12-12|01:02
ipsecdev.c18.58 kB2003-12-12|13:49
01.97 kB
01.97 kB
ah_test.c7.29 kB2003-12-12|01:42
des_test.c11.87 kB2003-12-12|01:02
esp_test.c14.96 kB2003-12-12|11:40
main.c5.35 kB2003-12-12|01:02
md5_test.c7.34 kB2003-12-12|01:02
sa_test.c32.48 kB2003-12-12|11:40
sha1_test.c11.65 kB2003-12-12|01:02
util_test.c10.19 kB2004-06-19|18:47
...
Sponsored links

md5_test.c (172.21 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D