embedded IPsec source code documentation


esp_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 
00048 #include <string.h>
00049 
00050 #include "ipsec/util.h"
00051 #include "ipsec/debug.h"
00052 #include "testing/structural/structural_test.h"
00053 
00054 #include "ipsec/sa.h"
00055 #include "ipsec/esp.h"
00056 
00057 
00058 unsigned char enc_esp_packet1[484] =
00059 {
00060         /* IP header */
00061     0x45, 0x00, 0x01, 0xE4, 
00062         0x56, 0xDC, 0x00, 0x00, 
00063         0x40, 0x32, 0x9E, 0x90, 
00064         0xC0, 0xA8, 0x01, 0x03,
00065     0xC0, 0xA8, 0x01, 0x28, 
00066         /* ESP header */
00067         0x00, 0x00, 0x10, 0x06, 
00068         0x00, 0x00, 0x00, 0x02, 
00069         /* IV */
00070         0xD4, 0xDB, 0xAB, 0x9A,
00071     0x9A, 0xDB, 0xD1, 0x94, 
00072         /* encrypted payload */
00073         0x53, 0x83, 0xFB, 0x32, 0x7F, 0x7E, 0x9A, 0xE9, 0x01, 0xB3, 0x7F, 0x80,
00074     0x06, 0xCF, 0x39, 0x26, 0x6F, 0x5D, 0x25, 0x5B, 0x1E, 0x26, 0x28, 0xC4, 0xB3, 0x78, 0x04, 0xAC,
00075     0x9F, 0x0B, 0xAE, 0x9D, 0x57, 0xAC, 0x4E, 0x82, 0x0E, 0xB4, 0xBA, 0x6C, 0x27, 0x38, 0x0A, 0x71,
00076     0x9B, 0xE1, 0xF7, 0xFD, 0x14, 0xCC, 0x14, 0x0D, 0x71, 0xE5, 0xB8, 0xF7, 0x7C, 0xCB, 0xE0, 0x0B,
00077     0x04, 0x78, 0xB1, 0x7F, 0x2B, 0xEA, 0x85, 0x41, 0x95, 0x22, 0x18, 0xEE, 0x06, 0xFF, 0x79, 0x13,
00078     0x26, 0xD3, 0x08, 0x10, 0x75, 0x04, 0x53, 0x6C, 0x04, 0xFD, 0x91, 0xF3, 0x31, 0x93, 0xB6, 0x14,
00079     0xE3, 0x53, 0xF6, 0xCE, 0x6E, 0xC3, 0x74, 0xFE, 0xCE, 0x62, 0x01, 0xBD, 0x8D, 0x11, 0x26, 0x39,
00080     0x0A, 0x74, 0x51, 0xCA, 0x56, 0x42, 0x51, 0xD5, 0x7B, 0x8E, 0xA6, 0x47, 0xDC, 0x7F, 0x74, 0x09,
00081     0x90, 0x8F, 0x9A, 0xF2, 0x0C, 0x74, 0x81, 0x64, 0x10, 0x0D, 0x64, 0xCE, 0x54, 0xB5, 0x78, 0x81,
00082     0xA5, 0x90, 0x36, 0xEB, 0x82, 0xC9, 0xE7, 0x5C, 0xBD, 0x3F, 0x3B, 0x29, 0xD3, 0x2F, 0x22, 0x6E,
00083     0x6F, 0x24, 0xDD, 0xFB, 0xDD, 0x98, 0x00, 0x1D, 0x4B, 0x82, 0xCE, 0x12, 0xD1, 0x33, 0x29, 0x22,
00084     0x4F, 0x92, 0xF9, 0x94, 0xEB, 0x12, 0x57, 0x2F, 0xFA, 0x30, 0xC7, 0xF1, 0x9D, 0x72, 0xCA, 0x8D,
00085     0x25, 0xEB, 0x2C, 0x3D, 0xE3, 0xFB, 0xCC, 0xBC, 0x54, 0x11, 0x53, 0x25, 0xE2, 0xCE, 0x78, 0xF9,
00086     0xDC, 0x6F, 0xE8, 0xE2, 0xFB, 0x41, 0x3C, 0xED, 0x6F, 0x9A, 0xB1, 0xF3, 0x72, 0x78, 0x02, 0xCC,
00087     0x91, 0x3D, 0x20, 0x7F, 0xBE, 0xE9, 0x53, 0xD4, 0xFB, 0xFF, 0x6E, 0x42, 0xB1, 0xF6, 0x5E, 0x4D,
00088     0x14, 0x03, 0xB4, 0xFB, 0x2F, 0x53, 0x7A, 0xBC, 0x9E, 0xA7, 0xCE, 0x8E, 0xCA, 0x08, 0xC4, 0x7B,
00089     0x19, 0xA1, 0x2B, 0x7F, 0x6A, 0x79, 0x77, 0xCD, 0x77, 0xA4, 0xBA, 0x8C, 0xB8, 0xAC, 0x9C, 0x35,
00090     0x9C, 0x30, 0xB1, 0x29, 0x78, 0xAA, 0x4A, 0xFF, 0xB5, 0xAB, 0x5B, 0x03, 0x09, 0x43, 0xC9, 0x0F,
00091     0xFC, 0x2D, 0x50, 0x65, 0x62, 0xC2, 0xA4, 0xB2, 0xC5, 0x07, 0x4D, 0x41, 0x43, 0xEE, 0x9A, 0x23,
00092     0xA3, 0x02, 0xA5, 0x9A, 0x6E, 0xBD, 0x9E, 0x8C, 0x9F, 0xA2, 0x70, 0x52, 0x61, 0x33, 0x9A, 0x21,
00093     0x26, 0xA6, 0xBE, 0xF6, 0x82, 0x55, 0x0D, 0xB7, 0x03, 0x40, 0x56, 0x18, 0xAA, 0xAB, 0x09, 0xC1,
00094     0x3B, 0x1F, 0xC0, 0x6D, 0x62, 0x0C, 0x7D, 0xA3, 0x59, 0x96, 0x99, 0x94, 0xC3, 0x06, 0x98, 0x6B,
00095     0x64, 0x82, 0xEB, 0xCC, 0x9B, 0x3E, 0x56, 0xC3, 0xA7, 0xEB, 0x9C, 0xF7, 0x07, 0x6C, 0x07, 0x45,
00096     0x8E, 0xBC, 0x85, 0x41, 0x48, 0x69, 0x78, 0x23, 0xAB, 0xBC, 0xE7, 0x1F, 0x7B, 0xD2, 0x07, 0x81,
00097     0xDA, 0x9D, 0x1B, 0x5F, 0x0A, 0x4A, 0x37, 0x88, 0xF8, 0x1C, 0x4F, 0xB4, 0x84, 0xDC, 0x14, 0xD4,
00098     0xD3, 0x3B, 0x29, 0x2B, 0xA9, 0x42, 0x77, 0x2C, 0x91, 0x48, 0x05, 0xF5, 0x21, 0x54, 0xDD, 0xD3,
00099     0xC8, 0xB5, 0xCE, 0xB7, 0x10, 0x7E, 0x63, 0x5A, 0xF3, 0x9E, 0x2C, 0xC7, 0xBC, 0xC8, 0xC9, 0xC2,
00100     0x2D, 0xF0, 0x62, 0x5E, 0xB8, 0xA3, 0x84, 0x8A, 0x3D, 0x5D, 0xB5, 0xF0, 0x14, 0xB4, 0x77, 0xC3,
00101     0x67, 0xD0, 0x5E, 0x77,
00102 } ;
00103 
00104 unsigned char dec_esp_packet1[448] =
00105 {
00106         /* inner IP header (20 bytes) */
00107         0x45, 0x00, 0x01, 0xB9, 
00108         0x59, 0x53, 0x40, 0x00, 
00109         0x40, 0x06, 0x5C, 0x70,
00110     0xC0, 0xA8, 0x01, 0x03, 
00111         0xC0, 0xA8, 0x01, 0x28, 
00112         /* TCP header (32 bytes) */
00113         0x00, 0x50, 0x80, 0x0A, 0x0F, 0x25, 0xC3, 0x55,
00114     0x47, 0x67, 0xC8, 0xD2, 0x80, 0x18, 0x7E, 0xA0, 
00115         0xC4, 0x5A, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0A,
00116     0x00, 0x69, 0x9A, 0x06, 0x00, 0x07, 0x90, 0x0F, 
00117         /* HTTP (389 bytes) */
00118         0x48, 0x54, 0x54, 0x50, 0x2F, 0x31, 0x2E, 0x30,
00119     0x20, 0x32, 0x30, 0x30, 0x20, 0x4F, 0x4B, 0x0D, 0x0A, 0x43, 0x6F, 0x6E, 0x74, 0x65, 0x6E, 0x74,
00120     0x2D, 0x54, 0x79, 0x70, 0x65, 0x3A, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2F, 0x68, 0x74, 0x6D, 0x6C,
00121     0x0D, 0x0A, 0x0D, 0x0A, 0x3C, 0x68, 0x74, 0x6D, 0x6C, 0x3E, 0x0D, 0x0A, 0x3C, 0x68, 0x65, 0x61,
00122     0x64, 0x3E, 0x0D, 0x0A, 0x3C, 0x2F, 0x68, 0x65, 0x61, 0x64, 0x3E, 0x0D, 0x0A, 0x3C, 0x62, 0x6F,
00123     0x64, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74, 0x3D, 0x22, 0x23, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
00124     0x22, 0x20, 0x62, 0x67, 0x63, 0x6F, 0x6C, 0x6F, 0x72, 0x3D, 0x22, 0x23, 0x46, 0x46, 0x46, 0x46,
00125     0x39, 0x39, 0x22, 0x20, 0x6C, 0x69, 0x6E, 0x6B, 0x3D, 0x22, 0x23, 0x30, 0x30, 0x30, 0x30, 0x45,
00126     0x45, 0x22, 0x20, 0x76, 0x6C, 0x69, 0x6E, 0x6B, 0x3D, 0x22, 0x23, 0x35, 0x35, 0x31, 0x41, 0x38,
00127     0x42, 0x22, 0x20, 0x61, 0x6C, 0x69, 0x6E, 0x6B, 0x3D, 0x22, 0x23, 0x46, 0x46, 0x30, 0x30, 0x30,
00128     0x30, 0x22, 0x3E, 0x0D, 0x0A, 0x26, 0x6E, 0x62, 0x73, 0x70, 0x3B, 0x0D, 0x0A, 0x3C, 0x63, 0x65,
00129     0x6E, 0x74, 0x65, 0x72, 0x3E, 0x3C, 0x74, 0x61, 0x62, 0x6C, 0x65, 0x20, 0x42, 0x4F, 0x52, 0x44,
00130     0x45, 0x52, 0x3D, 0x30, 0x20, 0x43, 0x4F, 0x4C, 0x53, 0x3D, 0x31, 0x20, 0x57, 0x49, 0x44, 0x54,
00131     0x48, 0x3D, 0x22, 0x39, 0x30, 0x25, 0x22, 0x20, 0x48, 0x45, 0x49, 0x47, 0x48, 0x54, 0x3D, 0x22,
00132     0x38, 0x35, 0x25, 0x22, 0x20, 0x3E, 0x0D, 0x0A, 0x3C, 0x74, 0x72, 0x20, 0x41, 0x4C, 0x49, 0x47,
00133     0x4E, 0x3D, 0x4C, 0x45, 0x46, 0x54, 0x20, 0x56, 0x41, 0x4C, 0x49, 0x47, 0x4E, 0x3D, 0x54, 0x4F,
00134     0x50, 0x3E, 0x0D, 0x0A, 0x3C, 0x74, 0x64, 0x3E, 0x3C, 0x62, 0x3E, 0x3C, 0x66, 0x6F, 0x6E, 0x74,
00135     0x20, 0x66, 0x61, 0x63, 0x65, 0x3D, 0x22, 0x56, 0x65, 0x72, 0x64, 0x61, 0x6E, 0x61, 0x22, 0x3E,
00136     0x3C, 0x66, 0x6F, 0x6E, 0x74, 0x20, 0x73, 0x69, 0x7A, 0x65, 0x3D, 0x2B, 0x32, 0x3E, 0x45, 0x6D,
00137     0x62, 0x65, 0x64, 0x64, 0x65, 0x64, 0x20, 0x57, 0x65, 0x62, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65,
00138     0x72, 0x20, 0x54, 0x65, 0x73, 0x74, 0x3C, 0x2F, 0x66, 0x6F, 0x6E, 0x74, 0x3E, 0x3C, 0x2F, 0x66,
00139     0x6F, 0x6E, 0x74, 0x3E, 0x3C, 0x2F, 0x62, 0x3E, 0x3C, 0x2F, 0x74, 0x64, 0x3E, 0x0D, 0x0A, 0x3C,
00140     0x2F, 0x74, 0x72, 0x3E, 0x0D, 0x0A, 0x3C, 0x2F, 0x74, 0x61, 0x62, 0x6C, 0x65, 0x3E, 0x0D, 0x0A,
00141     0x3C, 0x2F, 0x63, 0x65, 0x6E, 0x74, 0x65, 0x72, 0x3E, 0x0D, 0x0A, 0x3C, 0x2F, 0x62, 0x6F, 0x64,
00142     0x79, 0x3E, 0x0D, 0x0A, 0x3C, 0x2F, 0x68, 0x74, 0x6D, 0x6C, 0x3E, 0x0D, 0x0A, 
00143         /* padding (5 bytes) */
00144         0x01, 0x02, 0x03, 0x04, 0x05, 
00145         /* padding length (1 byte) */
00146         0x05, 
00147         /* next protocol (1 byte) */
00148         0x04
00149 } ;
00150 
00151 unsigned char enc_esp_packet2[100] =
00152 {
00153         /* IP header */
00154     0x45, 0x00, 0x00, 0x64, 
00155         0x79, 0x30, 0x00, 0x00, 
00156         0x40, 0x32, 0x7D, 0xBC, 
00157         0xC0, 0xA8, 0x01, 0x28,
00158     0xC0, 0xA8, 0x01, 0x03, 
00159         /* ESP header */
00160         0x00, 0x00, 0x10, 0x06, 
00161         0x00, 0x00, 0x00, 0x01, 
00162         /* IV */
00163         0x87, 0xC5, 0xBA, 0x8C,
00164     0x6F, 0x31, 0xEE, 0x1E, 
00165         /* encrypted payload */
00166         0xFB, 0xCE, 0x75, 0xD2, 0xD4, 0x16, 0x25, 0x8A, 0x92, 0xB8, 0x63, 0x09,
00167     0xBB, 0x88, 0x7E, 0x10, 0x50, 0x2B, 0x23, 0x2F, 0xF6, 0x51, 0xF2, 0x2B, 0x82, 0xAA, 0xE5, 0xD3,
00168     0x5D, 0x4C, 0xFB, 0xCF, 0x87, 0xFB, 0xAD, 0x9F, 0x28, 0x1D, 0xFF, 0x90, 0xDD, 0x43, 0x6B, 0x04,
00169     0xBC, 0xFB, 0xD4, 0x47, 0x36, 0xBC, 0x24, 0x20, 0x71, 0x00, 0x44, 0x07, 0xC2, 0x49, 0xEB, 0x48,
00170     0x26, 0xA4, 0xA7, 0x87,
00171 } ;
00172 
00173 unsigned char dec_esp_packet2[64] =
00174 {
00175         /* inner IP header (20 bytes) */
00176     0x45, 0x00, 0x00, 0x3C, 
00177         0x58, 0xDF, 0x40, 0x00, 
00178         0x40, 0x06, 0x5E, 0x61, 
00179         0xC0, 0xA8, 0x01, 0x28,
00180     0xC0, 0xA8, 0x01, 0x03, 
00181         /* TCP header */        
00182         0x80, 0x0A, 0x00, 0x50, 0x47, 0x67, 0xC8, 0xD1, 0x00, 0x00, 0x00, 0x00,
00183     0xA0, 0x02, 0x7E, 0xB8, 0xEB, 0x81, 0x00, 0x00, 0x02, 0x04, 0x3F, 0x5C, 0x04, 0x02, 0x08, 0x0A,
00184     0x00, 0x07, 0x90, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x00, 
00185         /* padding */
00186         0x01, 0x02,
00187         /* padd len */
00188         0x02, 
00189         /* next protocol*/
00190         0x04,
00191 } ;
00192 
00193 unsigned char esp_packet_tmp [500] ;
00194 
00195 sad_entry packet1_sa = {        SAD_ENTRY(      192,168,1,40, 255,255,255,255, 
00196                                                         0x001006, 
00197                                                         IPSEC_PROTO_ESP, IPSEC_TUNNEL, 
00198                                                         IPSEC_3DES, 
00199                                                         0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 
00200                                                         0,  
00201                                                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)} ;
00202 
00203 sad_entry packet2_sa = {        SAD_ENTRY(      192,168,1,3, 255,255,255,255, 
00204                                                         0x001006, 
00205                                                         IPSEC_PROTO_ESP, IPSEC_TUNNEL, 
00206                                                         IPSEC_3DES, 
00207                                                         0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 
00208                                                         0,  
00209                                                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)} ;
00210 
00215 int test_esp_decapsulate(void)
00216 {
00217         int             local_error_count = 0 ;
00218         int                     offset, len ;
00219         sad_entry       *sa ;
00220 
00221         /* test decryption of packet 1 */
00222         memcpy(esp_packet_tmp, enc_esp_packet1, 484) ;
00223         sa = &packet1_sa ;
00224 
00225         ipsec_esp_decapsulate((ipsec_ip_header*)esp_packet_tmp, &offset, &len, sa) ;
00226         
00227         if(offset != 36)
00228         {
00229                 local_error_count++ ;
00230                 IPSEC_LOG_TST("test_esp_decapsulate", "FAILURE", ("offset was not calculated properly")) ;
00231         }
00232 
00233         if(len != 441)
00234         {
00235                 local_error_count++ ;
00236                 IPSEC_LOG_TST("test_esp_decapsulate", "FAILURE", ("length was not calculated properly")) ;
00237         }
00238 
00239         if(memcmp(((char*)esp_packet_tmp)+offset, dec_esp_packet1, len) != 0)
00240         {
00241                 local_error_count++ ;
00242                 IPSEC_LOG_TST("test_esp_decapsulate", "FAILURE", ("packet was not decrypted properly")) ;
00243         }
00244 
00245         /* test decryption of packet2 */
00246 
00247         memset(esp_packet_tmp, 0, 500) ;
00248         memcpy(esp_packet_tmp, enc_esp_packet2, 100) ;
00249         sa = &packet2_sa ;
00250 
00251         ipsec_esp_decapsulate((ipsec_ip_header*)esp_packet_tmp, &offset, &len, sa) ;
00252         
00253         if(offset != 36)
00254         {
00255                 local_error_count++ ;
00256                 IPSEC_LOG_TST("test_esp_decapsulate", "FAILURE", ("offset was not calculated properly")) ;
00257         }
00258 
00259         if(len != 60)
00260         {
00261                 local_error_count++ ;
00262                 IPSEC_LOG_TST("test_esp_decapsulate", "FAILURE", ("length was not calculated properly")) ;
00263         }
00264 
00265         if(memcmp(((char*)esp_packet_tmp)+offset, dec_esp_packet2, len) != 0)
00266         {
00267                 local_error_count++ ;
00268                 IPSEC_LOG_TST("test_esp_decapsulate", "FAILURE", ("packet was not decrypted properly")) ;
00269         }
00270 
00271         return local_error_count ;
00272 }
00273 
00278 int test_esp_encapsulate(void)
00279 {
00280         int             local_error_count = 0 ;
00281         int                     offset, len ;
00282         int                     cmp_len ;
00283         char            *cmp_buffer_result ;
00284         char            *cmp_buffer_expected ;
00285         sad_entry       *sa ;
00286 
00287         memset(esp_packet_tmp, 0, 500) ;
00288         memcpy(&esp_packet_tmp[40], dec_esp_packet1, 441) ;
00289         sa = &packet1_sa ;
00290 
00291         ipsec_esp_encapsulate((ipsec_ip_header*)&esp_packet_tmp[40], &offset, &len, sa, ipsec_inet_addr("192.168.1.3"), ipsec_inet_addr("192.168.1.40")) ;
00292         
00293         if(offset != -36)
00294         {
00295                 local_error_count++ ;
00296                 IPSEC_LOG_TST("test_esp_encapsulate", "FAILURE", ("offset was not calculated properly")) ;
00297         }
00298 
00299         if(len != 484)
00300         {
00301                 local_error_count++ ;
00302                 IPSEC_LOG_TST("test_esp_encapsulate", "FAILURE", ("length was not calculated properly")) ;
00303         }
00304 
00305         cmp_len = len - 20 ;
00306         cmp_buffer_result = &esp_packet_tmp[40+offset+20] ;
00307         cmp_buffer_expected = &enc_esp_packet1[20] ;
00308 
00309         if(memcmp(cmp_buffer_result, cmp_buffer_expected , cmp_len) != 0)
00310         {
00311                 local_error_count++ ;
00312                 IPSEC_DUMP_BUFFER("RESULT", cmp_buffer_result, 0, cmp_len) ;
00313                 IPSEC_DUMP_BUFFER("EXP   ", cmp_buffer_expected, 0, cmp_len) ;
00314                 IPSEC_LOG_TST("test_esp_encapsulate", "FAILURE", ("packet was not encrypted properly")) ;
00315         }
00316 
00317         memset(esp_packet_tmp, 0, 500) ;
00318         memcpy(esp_packet_tmp, dec_esp_packet2, 60) ;
00319         sa = &packet1_sa ;
00320 
00321         ipsec_esp_encapsulate((ipsec_ip_header*)esp_packet_tmp, &offset, &len, sa, ipsec_inet_addr("192.168.1.40"), ipsec_inet_addr("102.168.1.3")) ;
00322         
00323         if(offset != -36)
00324         {
00325                 local_error_count++ ;
00326                 IPSEC_LOG_TST("test_esp_encapsulate", "FAILURE", ("offset was not calculated properly")) ;
00327         }
00328 
00329         if(len != 100)
00330         {
00331                 local_error_count++ ;
00332                 IPSEC_LOG_TST("test_esp_encapsulate", "FAILURE", ("length was not calculated properly")) ;
00333         }
00334 
00335         cmp_len = len - 20 ;
00336         cmp_buffer_result = &esp_packet_tmp[40+offset+20] ;
00337         cmp_buffer_expected = &enc_esp_packet1[20] ;
00338 
00339         if(memcmp(cmp_buffer_result, cmp_buffer_result, cmp_len) != 0)
00340         {
00341                 local_error_count++ ;
00342                 IPSEC_DUMP_BUFFER("RESULT", cmp_buffer_result, 0, cmp_len) ;
00343                 IPSEC_DUMP_BUFFER("EXP   ", cmp_buffer_expected, 0, cmp_len) ;
00344                 IPSEC_LOG_TST("test_esp_encapsulate", "FAILURE", ("packet was not encrypted properly")) ;
00345         }
00346 
00347         return local_error_count ;
00348 }
00349 
00350 
00355 void esp_test(test_result *global_results)
00356 {
00357         test_result     sub_results     = {
00358                                                  12,            
00359                                                   2,                    
00360                                                   0, 
00361                                                   0,                    
00362                                         };
00363 
00364         int retcode;
00365 
00366         retcode = test_esp_decapsulate() ;
00367         IPSEC_TESTING_EVALUATE(retcode, sub_results, "test_esp_decapsulate", (" "));
00368 
00369         retcode = test_esp_encapsulate() ;
00370         IPSEC_TESTING_EVALUATE(retcode, sub_results, "test_esp_encapsulate", (" "));
00371 
00372         global_results->tests += sub_results.tests;
00373         global_results->functions += sub_results.functions;
00374         global_results->errors += sub_results.errors;
00375         global_results->notimplemented += sub_results.notimplemented;
00376 }
00377 
00378 

Copyright 2003 by Christian Scheurer and Niklaus Schild