embedded IPsec source code documentation


md5_test.c

Go to the documentation of this file.
00001 /*
00002  * embedded IPsec
00003  * Copyright (c) 2003 Niklaus Schild and Christian Scheurer, HTI Biel/Bienne
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without modification,
00007  * are permitted provided that the following conditions are met:
00008  *
00009  * 1. Redistributions of source code must retain the above copyright notice,
00010  *    this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright notice,
00012  *    this list of conditions and the following disclaimer in the documentation
00013  *    and/or other materials provided with the distribution.
00014  * 3. The name of the author may not be used to endorse or promote products
00015  *    derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00018  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00020  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00021  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00022  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00025  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00026  * OF SUCH DAMAGE.
00027  *
00028  */
00029 
00055 #include <string.h>
00056 
00057 #include "ipsec/util.h"
00058 #include "ipsec/md5.h"
00059 #include "ipsec/debug.h"
00060 #include "testing/structural/structural_test.h"
00061 
00062 
00067 int md5_test_MD5_Init(void)
00068 {
00069         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} ;
00070         MD5_CTX input, test;
00071 
00072         memset(&input, 0, sizeof(MD5_CTX)) ;
00073         memcpy(&test, &input, sizeof(MD5_CTX)) ;
00074 
00075         MD5_Init(&test) ;
00076 
00077         if(memcmp(&test, &orig, sizeof(MD5_CTX)) != 0)
00078         {
00079                 IPSEC_LOG_TST("md5_test_init", "FAILURE", ("MD5_Init() failed")) ;
00080                 printf("     INPUT:\n") ;
00081                 IPSEC_DUMP_BUFFER("          ", (char*)&input, 0, sizeof(MD5_CTX));
00082                 printf("     OUTPUT:\n") ;
00083                 IPSEC_DUMP_BUFFER("          ", (char*)&test, 0, sizeof(MD5_CTX));
00084                 printf("     EXPECTED OUTPUT:\n") ;
00085                 IPSEC_DUMP_BUFFER("          ", (char*)&orig, 0, sizeof(MD5_CTX));
00086 
00087                 return 1;
00088         }
00089 
00090         return 0 ;
00091 }
00092 
00097 int md5_test_MD5_Update()
00098 {
00099         MD5_CTX orig = {        0xCB6180E8L, 0x2FA83EA8L, 0x43278D6CL, 0xB9526934,
00100                                                 0x000002E0, 0x0, 
00101                                                 0x74616877, 0x206F6420, 0x77206179, 0x20746E61, 0x20726F66, 0x68746F6E, 0x3F676E69,      
00102                                                 0, 0, 0, 0, 0, 0, 0, 0, 0,
00103                                                 0x001C          
00104                                         } ;
00105         MD5_CTX test, input ;
00106 
00107         unsigned char k_ipad[65];
00108     unsigned char k_opad[65];
00109 
00110 
00111         char text[] = "what do ya want for nothing?" ;
00112         int text_len = 28 ;
00113 
00114         char key[] = "Jefe" ;
00115         int key_len = 4 ;
00116 
00117         int i ;
00118 
00119         memset(k_ipad, '\0', sizeof(k_ipad));  
00120         memset(k_opad, '\0', sizeof(k_opad));  
00121         memcpy(k_ipad, key, key_len);              
00122         memcpy(k_opad, key, key_len); 
00123 
00124         for (i=0; i<64; i++) 
00125         {
00126                 k_ipad[i] ^= 0x36;
00127                 k_opad[i] ^= 0x5c;
00128         }
00129 
00130         memset(&input, 0, sizeof(MD5_CTX)) ;
00131 
00132         MD5_Init(&input);      
00133     
00134         memcpy(&test, &input, sizeof(MD5_CTX)) ; 
00135 
00136         MD5_Update(&test, k_ipad, 64);  
00137         MD5_Update(&test, text, text_len);
00138 
00139         if(memcmp(&test, &orig, sizeof(MD5_CTX)) != 0)
00140         {
00141                 IPSEC_LOG_TST("md5_test_update", "FAILURE", ("MD5_Update() failed")) ;
00142                 printf("     INPUT:\n") ;
00143                 IPSEC_DUMP_BUFFER("          ", (char*)&input, 0, sizeof(MD5_CTX));
00144                 printf("     OUTPUT:\n") ;
00145                 IPSEC_DUMP_BUFFER("          ", (char*)&test, 0, sizeof(MD5_CTX));
00146                 printf("     EXPECTED OUTPUT:\n") ;
00147                 IPSEC_DUMP_BUFFER("          ", (char*)&orig, 0, sizeof(MD5_CTX));
00148 
00149                 return 1;
00150         }
00151 
00152         return 0 ;
00153 }
00154 
00159 int md5_test_MD5_Final()
00160 {
00161         MD5_CTX input ;
00162 
00163         unsigned char k_ipad[65];   
00164     unsigned char k_opad[65];    
00165 
00166         char text[] = "what do ya want for nothing?" ;
00167         int text_len = 28 ;
00168 
00169         char key[] = "Jefe" ;
00170         int key_len = 4 ;
00171 
00172         #define DIGEST_SIZE (16)
00173         unsigned char orig_digest[] = { 0xC3, 0xDB, 0x14, 0xC0, 0x65, 0xF5, 0x52, 0x03, 0xB0, 0x33, 0xC8, 0x1A, 0x69, 0x7B, 0x97, 0xC5 } ;
00174         char test_digest[DIGEST_SIZE+1] ;
00175 
00176         int i ;
00177 
00178         memset(k_ipad, '\0', sizeof(k_ipad));  
00179         memset(k_opad, '\0', sizeof(k_opad));  
00180         memcpy(k_ipad, key, key_len);              
00181         memcpy(k_opad, key, key_len); 
00182 
00183         for (i=0; i<64; i++) 
00184         {
00185                 k_ipad[i] ^= 0x36;
00186                 k_opad[i] ^= 0x5c;
00187         }
00188 
00189         memset(&input, 0, sizeof(MD5_CTX)) ;
00190 
00191         MD5_Init(&input);      
00192     
00193         MD5_Update(&input, k_ipad, 64);  
00194         MD5_Update(&input, text, text_len);
00195 
00196         MD5_Final(test_digest, &input);
00197 
00198         if(memcmp(&test_digest, &orig_digest, DIGEST_SIZE) != 0)
00199         {
00200                 IPSEC_LOG_TST("md5_test_final", "FAILURE", ("MD5_Final() failed")) ;
00201                 printf("     INPUT:\n") ;
00202                 IPSEC_DUMP_BUFFER("          ", (char*)&input, 0, sizeof(MD5_CTX));
00203                 printf("     OUTPUT:\n") ;
00204                 IPSEC_DUMP_BUFFER("          ", (char*)&test_digest, 0, DIGEST_SIZE);
00205                 printf("     EXPECTED OUTPUT:\n") ;
00206                 IPSEC_DUMP_BUFFER("          ", (char*)&orig_digest, 0, DIGEST_SIZE);
00207 
00208                 return 1 ;
00209         }
00210 
00211         return 0 ;
00212 }
00213 
00218 void md5_test(test_result *global_results)
00219 {
00220         test_result     sub_results     = {
00221                                                   3,            
00222                                                   3,            
00223                                                   0,            
00224                                                   0,            
00225                                         };
00226 
00227         int retcode;
00228 
00229         retcode = md5_test_MD5_Init();
00230         IPSEC_TESTING_EVALUATE(retcode, sub_results, "md5_test_MD5_Init()", ("ported from openssl.org"));
00231 
00232         retcode = md5_test_MD5_Update();
00233         IPSEC_TESTING_EVALUATE(retcode, sub_results, "md5_test_MD5_Update()", ("ported from openssl.org"));
00234 
00235         retcode = md5_test_MD5_Final();
00236         IPSEC_TESTING_EVALUATE(retcode, sub_results, "md5_test_MD5_Final()", ("ported from openssl.org"));
00237 
00238 
00239         global_results->tests += sub_results.tests;
00240         global_results->functions += sub_results.functions;
00241         global_results->errors += sub_results.errors;
00242         global_results->notimplemented += sub_results.notimplemented;
00243 }
00244 
00245 
00246 

Copyright 2003 by Christian Scheurer and Niklaus Schild