<html>
<head>
<title>embedded IPsec - IPsec library</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="doxygen.css" type="text/css">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<table width="98%" border="0" align="center">
  <tr>
    <td>
      <h2 align="left"><img src="logo_small.gif" alt="embedded IPsec"> source 
        code documentation </h2>
<hr>

<!-- Generated by Doxygen 1.3.4 -->
<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
<h1>ah_test.c</h1><a href="ah__test_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">/*</span>
00002 <span class="comment"> * embedded IPsec</span>
00003 <span class="comment"> * Copyright (c) 2003 Niklaus Schild and Christian Scheurer, HTI Biel/Bienne</span>
00004 <span class="comment"> * All rights reserved.</span>
00005 <span class="comment"> *</span>
00006 <span class="comment"> * Redistribution and use in source and binary forms, with or without modification,</span>
00007 <span class="comment"> * are permitted provided that the following conditions are met:</span>
00008 <span class="comment"> *</span>
00009 <span class="comment"> * 1. Redistributions of source code must retain the above copyright notice,</span>
00010 <span class="comment"> *    this list of conditions and the following disclaimer.</span>
00011 <span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright notice,</span>
00012 <span class="comment"> *    this list of conditions and the following disclaimer in the documentation</span>
00013 <span class="comment"> *    and/or other materials provided with the distribution.</span>
00014 <span class="comment"> * 3. The name of the author may not be used to endorse or promote products</span>
00015 <span class="comment"> *    derived from this software without specific prior written permission.</span>
00016 <span class="comment"> *</span>
00017 <span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED</span>
00018 <span class="comment"> * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF</span>
00019 <span class="comment"> * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT</span>
00020 <span class="comment"> * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,</span>
00021 <span class="comment"> * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT</span>
00022 <span class="comment"> * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span>
00023 <span class="comment"> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN</span>
00024 <span class="comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING</span>
00025 <span class="comment"> * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY</span>
00026 <span class="comment"> * OF SUCH DAMAGE.</span>
00027 <span class="comment"> *</span>
00028 <span class="comment"> */</span>
00029 
00051 <span class="preprocessor">#include &lt;string.h&gt;</span>
00052 
00053 <span class="preprocessor">#include "<a class="code" href="util_8h.html">ipsec/util.h</a>"</span>
00054 <span class="preprocessor">#include "<a class="code" href="ah_8h.html">ipsec/ah.h</a>"</span>
00055 <span class="preprocessor">#include "<a class="code" href="sa_8h.html">ipsec/sa.h</a>"</span>
00056 <span class="preprocessor">#include "<a class="code" href="debug_8h.html">ipsec/debug.h</a>"</span>
00057 <span class="preprocessor">#include "<a class="code" href="structural__test_8h.html">testing/structural/structural_test.h</a>"</span>
00058 
00059 <span class="preprocessor">#include "<a class="code" href="ah__test-sample__ah__packet_8h.html">testing/structural/ah_test-sample_ah_packet.h</a>"</span>
00060 
<a name="l00065"></a><a class="code" href="ah__test_8c.html#a0">00065</a> <span class="keywordtype">int</span> <a class="code" href="ah__test_8c.html#a0">ah_test_ipsec_ah_check</a>(<span class="keywordtype">void</span>) 
00066 {
00067         <a class="code" href="structsa__entry__struct.html">sad_entry</a> <a class="code" href="esp__test_8c.html#a5">packet1_sa</a> =  {       <a class="code" href="sa_8h.html#a16">SAD_ENTRY</a>(      192,168,1,40, 255,255,255,255, 
00068                                                                 0x1010, 
00069                                                                 <a class="code" href="types_8h.html#a40a37">IPSEC_PROTO_AH</a>, <a class="code" href="sa_8h.html#a7">IPSEC_TUNNEL</a>, 
00070                                                                 <a class="code" href="sa_8h.html#a10">IPSEC_3DES</a>, 
00071                                                                 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 
00072                                                                 <a class="code" href="sa_8h.html#a12">IPSEC_HMAC_MD5</a>,  
00073                                                                 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0, 0, 0, 0)
00074                                                         };
00075         <span class="keywordtype">int</span> local_error_count   = 0;
00076         <span class="keywordtype">int</span> payload_size                = 0;
00077         <span class="keywordtype">int</span> payload_offset              = 0;
00078         <span class="keywordtype">int</span> ret_val;
00079 
00080         <span class="comment">// feed valid AH packet</span>
00081         ret_val = <a class="code" href="ah_8h.html#a4">ipsec_ah_check</a>((<a class="code" href="structipsec__ip__hdr__struct.html">ipsec_ip_header</a> *)&amp;<a class="code" href="ah__test-sample__ah__packet_8h.html#a0">ah_test_sample_ah_outer_packet</a>, (<span class="keywordtype">int</span> *)&amp;payload_offset, (<span class="keywordtype">int</span> *)&amp;payload_size, (<a class="code" href="structsa__entry__struct.html">sad_entry</a> *)&amp;<a class="code" href="esp__test_8c.html#a5">packet1_sa</a>);
00082         <span class="keywordflow">if</span>(ret_val != <a class="code" href="types_8h.html#a38a12">IPSEC_STATUS_SUCCESS</a>) {
00083                 local_error_count++;
00084                 <a class="code" href="debug_8h.html#a10">IPSEC_LOG_TST</a>(<span class="stringliteral">"ah_test_ipsec_ah_check"</span>, <span class="stringliteral">"FAILURE"</span>, (<span class="stringliteral">"ipsec_ah_check(ah_test_sample_ah_packet) failed"</span>)) ;
00085         }
00086 
00087         <span class="comment">// feed invalid packet (offset + 1)</span>
00088         ret_val = <a class="code" href="ah_8h.html#a4">ipsec_ah_check</a>(((<a class="code" href="structipsec__ip__hdr__struct.html">ipsec_ip_header</a> *)&amp;<a class="code" href="ah__test-sample__ah__packet_8h.html#a0">ah_test_sample_ah_outer_packet</a>[1]), (<span class="keywordtype">int</span> *)&amp;payload_offset, (<span class="keywordtype">int</span> *)&amp;payload_size, (<a class="code" href="structsa__entry__struct.html">sad_entry</a> *)&amp;<a class="code" href="esp__test_8c.html#a5">packet1_sa</a>);
00089         <span class="keywordflow">if</span>(ret_val == <a class="code" href="types_8h.html#a38a12">IPSEC_STATUS_SUCCESS</a>) {
00090                 local_error_count++;
00091                 <a class="code" href="debug_8h.html#a10">IPSEC_LOG_TST</a>(<span class="stringliteral">"ah_test_ipsec_ah_check"</span>, <span class="stringliteral">"FAILURE"</span>, (<span class="stringliteral">"ipsec_ah_check(invalid_packet) was not rejected"</span>)) ;
00092         }
00093 
00094         <span class="keywordflow">return</span> local_error_count;
00095 }
00096 
<a name="l00101"></a><a class="code" href="ah__test_8c.html#a1">00101</a> <span class="keywordtype">int</span> <a class="code" href="ah__test_8c.html#a1">ah_test_ipsec_ah_encapsulate</a>(<span class="keywordtype">void</span>) 
00102 {
00103         <a class="code" href="structsa__entry__struct.html">sad_entry</a> <a class="code" href="esp__test_8c.html#a5">packet1_sa</a> =  {       <a class="code" href="sa_8h.html#a16">SAD_ENTRY</a>(      192,168,1,5, 255,255,255,255, 
00104                                                                 0x1016, 
00105                                                                 <a class="code" href="types_8h.html#a40a37">IPSEC_PROTO_AH</a>, <a class="code" href="sa_8h.html#a7">IPSEC_TUNNEL</a>, 
00106                                                                 <a class="code" href="sa_8h.html#a10">IPSEC_3DES</a>, 
00107                                                                 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 
00108                                                                 <a class="code" href="sa_8h.html#a12">IPSEC_HMAC_MD5</a>,  
00109                                                                 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0, 0, 0, 0)
00110                                                         };
00111 
00112         <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> encapsulated_ah_packet[104] =
00113         {
00114             0x45, 0x00, 0x00, 0x68, 0xE8, 0x03, 0x00, 0x00, 0x40, 0x33, 0x0F, 0x07, 0xC0, 0xA8, 0x01, 0x03, 
00115             0xC0, 0xA8, 0x01, 0x05, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x10, 0x16, 0x00, 0x00, 0x00, 0x01, 
00116 
00117                 <span class="comment">// ICV</span>
00118                 0x6F, 0x1D, 0x8E, 0x94, 0x4F, 0x70, 0x23, 0xE8, 0x53, 0xB1, 0x51, 0xBF,
00119                 
00120                 0x45, 0x00, 0x00, 0x3C, 
00121             0xE7, 0x7A, 0x40, 0x00, 0x40, 0x06, 0xCF, 0xC5, 0xC0, 0xA8, 0x01, 0x28, 0xC0, 0xA8, 0x01, 0x03, 
00122             0x80, 0x1A, 0x00, 0x50, 0x84, 0xB9, 0xC5, 0x66, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x02, 0x7E, 0xB8, 
00123             0x1F, 0x75, 0x00, 0x00, 0x02, 0x04, 0x3F, 0x5C, 0x04, 0x02, 0x08, 0x0A, 0x00, 0x0F, 0x22, 0x1C, 
00124             0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x00, 
00125         } ;
00126 
00127         <span class="keywordtype">int</span> local_error_count   = 0;
00128         <span class="keywordtype">int</span> payload_size                = 0;
00129         <span class="keywordtype">int</span> payload_offset              = 0;
00130         <a class="code" href="types_8h.html#a4">__u32</a> src;
00131         <a class="code" href="types_8h.html#a4">__u32</a> dst;
00132         <span class="keywordtype">int</span> ret_val = 0;
00133         <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> buffer[<span class="keyword">sizeof</span> (<a class="code" href="ah__test-sample__ah__packet_8h.html#a1">ah_test_sample_ah_inner_packet</a>) + 100];
00134 
00135         local_error_count = 0;
00136 
00137         src = 0x0301A8C0;
00138         dst = 0x0501A8C0;
00139 
00140         <span class="comment">/* copy packet in a buffer where space for the new headers is left */</span>
00141         memcpy(buffer + 100, <a class="code" href="ah__test-sample__ah__packet_8h.html#a1">ah_test_sample_ah_inner_packet</a>, <span class="keyword">sizeof</span>(<a class="code" href="ah__test-sample__ah__packet_8h.html#a1">ah_test_sample_ah_inner_packet</a>));
00142 
00143         ret_val = <a class="code" href="ah_8h.html#a5">ipsec_ah_encapsulate</a>((<a class="code" href="structipsec__ip__hdr__struct.html">ipsec_ip_header</a> *)(buffer + 100), 
00144                                               (<span class="keywordtype">int</span> *)&amp;payload_offset, (<span class="keywordtype">int</span> *)&amp;payload_size, 
00145                                                                                   (<a class="code" href="structsa__entry__struct.html">sad_entry</a> *)&amp;<a class="code" href="esp__test_8c.html#a5">packet1_sa</a>,
00146                                                                                   src, dst
00147                                                                                  );
00148         <span class="keywordflow">if</span>(ret_val != 0) {
00149                 local_error_count++;
00150                 <a class="code" href="debug_8h.html#a10">IPSEC_LOG_TST</a>(<span class="stringliteral">"ah_test_ipsec_ah_encapsulate"</span>, <span class="stringliteral">"FAILURE"</span>, (<span class="stringliteral">"ipsec_ah_encapsulate() failed (rev_val indicates no SUCCESS)"</span>)) ;
00151         } 
00152 
00153         <span class="keywordflow">if</span>(payload_offset != -44)
00154         {
00155                 local_error_count++ ;
00156                 <a class="code" href="debug_8h.html#a10">IPSEC_LOG_TST</a>(<span class="stringliteral">"ah_test_ipsec_ah_encapsulate"</span>, <span class="stringliteral">"FAILURE"</span>, (<span class="stringliteral">"offset was not calculated properly"</span>)) ;
00157         }
00158 
00159         <span class="keywordflow">if</span>(payload_size != 104)
00160         {
00161                 local_error_count++ ;
00162                 <a class="code" href="debug_8h.html#a10">IPSEC_LOG_TST</a>(<span class="stringliteral">"ah_test_ipsec_ah_encapsulate"</span>, <span class="stringliteral">"FAILURE"</span>, (<span class="stringliteral">"length was not calculated properly"</span>)) ;
00163         }
00164 
00165         payload_offset = -44;
00166         payload_size = 104;
00167 
00168         <span class="keywordflow">if</span>(memcmp(((<span class="keywordtype">char</span>*)(buffer + 100)) + payload_offset, encapsulated_ah_packet, payload_size) != 0)
00169         {
00170                 local_error_count++ ;
00171                 <a class="code" href="debug_8h.html#a10">IPSEC_LOG_TST</a>(<span class="stringliteral">"ah_test_ipsec_ah_encapsulate"</span>, <span class="stringliteral">"FAILURE"</span>, (<span class="stringliteral">"packet was not properly encapsulated"</span>));
00172         }
00173 
00174         <span class="keywordflow">return</span> local_error_count;
00175 }
00176 
<a name="l00181"></a><a class="code" href="ah__test_8c.html#a2">00181</a> <span class="keywordtype">void</span> <a class="code" href="ah__test_8c.html#a2">ah_test</a>(<a class="code" href="structtest__result__struct.html">test_result</a> *global_results)
00182 {
00183         <a class="code" href="structtest__result__struct.html">test_result</a>     sub_results     = {
00184                                                   6,                    
00185                                                   2,                    
00186                                                   0,                    
00187                                                   0,                    
00188                                         };
00189 
00190         <span class="keywordtype">int</span> retcode;
00191 
00192         retcode = <a class="code" href="ah__test_8c.html#a0">ah_test_ipsec_ah_check</a>();
00193         <a class="code" href="structural__test_8h.html#a0">IPSEC_TESTING_EVALUATE</a>(retcode, sub_results, <span class="stringliteral">"ah_test_ipsec_ah_check()"</span>, (<span class="stringliteral">""</span>));
00194 
00195         retcode = <a class="code" href="ah__test_8c.html#a1">ah_test_ipsec_ah_encapsulate</a>();
00196         <a class="code" href="structural__test_8h.html#a0">IPSEC_TESTING_EVALUATE</a>(retcode, sub_results, <span class="stringliteral">"ah_test_ipsec_ah_encapsulate()"</span>, (<span class="stringliteral">""</span>));
00197 
00198         global_results-&gt;<a class="code" href="structtest__result__struct.html#o0">tests</a> += sub_results.<a class="code" href="structtest__result__struct.html#o0">tests</a>;
00199         global_results-&gt;<a class="code" href="structtest__result__struct.html#o1">functions</a> += sub_results.<a class="code" href="structtest__result__struct.html#o1">functions</a>;
00200         global_results-&gt;<a class="code" href="structtest__result__struct.html#o2">errors</a> += sub_results.<a class="code" href="structtest__result__struct.html#o2">errors</a>;
00201         global_results-&gt;<a class="code" href="structtest__result__struct.html#o3">notimplemented</a> += sub_results.<a class="code" href="structtest__result__struct.html#o3">notimplemented</a>;
00202 }
00203 
00204 
00205 
</pre></div>	</td>
  </tr>
</table>
<hr>
<div align="center">Copyright 2003 by Christian Scheurer and Niklaus Schild</div>
</html>