00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00050 #include <string.h>
00051
00052 #include "ipsec/util.h"
00053 #include "ipsec/sha1.h"
00054 #include "ipsec/debug.h"
00055 #include "testing/structural/structural_test.h"
00056
00057
00062 int sha1_test_SHA1_Init(void)
00063 {
00064 int local_error_count = 0;
00065 int i;
00066 SHA_CTX context;
00067
00068 context.h0 = 0x12345678L;
00069 context.h1 = 0x12345678L;
00070 context.h2 = 0x12345678L;
00071 context.h3 = 0x12345678L;
00072 context.h1 = 0x12345678L;
00073 context.Nl = 0x12345678L;
00074 context.Nh = 0x12345678L;
00075 memset(&context.data, 'X', SHA_LBLOCK*sizeof(SHA_LONG));
00076 context.num = 0x1234;
00077
00078 SHA1_Init(&context);
00079
00080 if(context.h0 != 0x67452301UL) local_error_count++;
00081 if(context.h1 != 0xefcdab89UL) local_error_count++;
00082 if(context.h2 != 0x98badcfeUL) local_error_count++;
00083 if(context.h3 != 0x10325476UL) local_error_count++;
00084 if(context.h4 != 0xc3d2e1f0UL) local_error_count++;
00085 if(context.Nl != 0x00000000UL) local_error_count++;
00086 if(context.Nh != 0x00000000UL) local_error_count++;
00087 if(context.num != 0x0000) local_error_count++;
00088 for(i = 0; i < SHA_LBLOCK; i++) {
00089 if(context.data[i] != 0x58585858UL) {
00090 local_error_count++;
00091 break;
00092 }
00093 }
00094
00095 if(local_error_count != 0) {
00096 printf("sha1_test_SHA1_init(): error - SHA1_Init(&context) did made %d errors initializing a SHA_CTX data structure\n", local_error_count);
00097 }
00098
00099 return local_error_count;
00100 }
00101
00102
00103
00108 int sha1_test_SHA1_Update(void)
00109 {
00110 unsigned char inputdata_1[65] =
00111 {
00112 0x7C, 0x53, 0x50, 0x53, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
00113 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
00114 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
00115 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
00116 0x00,
00117 };
00118 unsigned char inputdata_2[28] =
00119 {
00120 0x77, 0x68, 0x61, 0x74, 0x20, 0x64, 0x6F, 0x20, 0x79, 0x61, 0x20, 0x77, 0x61, 0x6E, 0x74, 0x20,
00121 0x66, 0x6F, 0x72, 0x20, 0x6E, 0x6F, 0x74, 0x68, 0x69, 0x6E, 0x67, 0x3F,
00122 };
00123 unsigned char context_before_update_1[94] =
00124 {
00125 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
00126 0xF0, 0xE1, 0xD2, 0xC3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x30, 0x5A, 0x8B,
00127 0x80, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0xC1, 0xCD, 0xC6, 0x21, 0x5A, 0xCD, 0x4A,
00128 0xDC, 0xC1, 0x9D, 0xB7, 0xB4, 0x8E, 0x06, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00129 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00130 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00131 };
00132 unsigned char context_after_update_1[94] =
00133 {
00134 0xA7, 0x8E, 0x4D, 0x62, 0x03, 0x18, 0x2B, 0x3F, 0xE4, 0xB9, 0x54, 0x95, 0xF5, 0x65, 0xC0, 0x28,
00135 0x8E, 0xB9, 0xFC, 0xA9, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x30, 0x5A, 0x8B,
00136 0x80, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0xC1, 0xCD, 0xC6, 0x21, 0x5A, 0xCD, 0x4A,
00137 0xDC, 0xC1, 0x9D, 0xB7, 0xB4, 0x8E, 0x06, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00138 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00139 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00140 };
00141 unsigned char context_after_update_2[94] =
00142 {
00143 0xA7, 0x8E, 0x4D, 0x62, 0x03, 0x18, 0x2B, 0x3F, 0xE4, 0xB9, 0x54, 0x95, 0xF5, 0x65, 0xC0, 0x28,
00144 0x8E, 0xB9, 0xFC, 0xA9, 0xE0, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0x61, 0x68, 0x77,
00145 0x20, 0x6F, 0x64, 0x20, 0x77, 0x20, 0x61, 0x79, 0x20, 0x74, 0x6E, 0x61, 0x20, 0x72, 0x6F, 0x66,
00146 0x68, 0x74, 0x6F, 0x6E, 0x3F, 0x67, 0x6E, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00147 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00148 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x00,
00149 };
00150 int local_error_count = 0;
00151 SHA_CTX context;
00152
00153 memcpy(&context, context_before_update_1, sizeof(context_before_update_1));
00154
00155 SHA1_Update(&context, inputdata_1, sizeof(inputdata_1) - 1);
00156
00157 if(memcmp(&context, context_after_update_1, sizeof(context_after_update_1) - 1) != 0)
00158 {
00159 local_error_count++;
00160 IPSEC_LOG_TST("sha1_test_SHA1_Update", "FAILURE", ("SHA1_Update() failed")) ;
00161 printf(" INPUT:\n") ;
00162 IPSEC_DUMP_BUFFER(" ", inputdata_1, 0, sizeof(inputdata_1) - 1);
00163 printf(" OUTPUT:\n") ;
00164 IPSEC_DUMP_BUFFER(" ", (char*)&context, 0, sizeof(context));
00165 printf(" EXPECTED OUTPUT:\n") ;
00166 IPSEC_DUMP_BUFFER(" ", context_after_update_1, 0, sizeof(context_after_update_1));
00167 }
00168
00169 memcpy(&context, context_after_update_1, sizeof(context_after_update_1));
00170
00171 SHA1_Update(&context, inputdata_2, sizeof(inputdata_2));
00172
00173 if(memcmp(&context, context_after_update_2, sizeof(context_after_update_2)) != 0)
00174 {
00175 local_error_count++;
00176 IPSEC_LOG_TST("sha1_test_SHA1_Update", "FAILURE", ("SHA1_Update() failed")) ;
00177 printf(" INPUT:\n") ;
00178 IPSEC_DUMP_BUFFER(" ", inputdata_2, 0, sizeof(inputdata_2));
00179 printf(" OUTPUT:\n") ;
00180 IPSEC_DUMP_BUFFER(" ", (char*)&context, 0, sizeof(context));
00181 printf(" EXPECTED OUTPUT:\n") ;
00182 IPSEC_DUMP_BUFFER(" ", context_after_update_2, 0, sizeof(context_after_update_2));
00183 }
00184
00185 return local_error_count;
00186 }
00187
00188
00189
00190
00195 int sha1_test_SHA1_Final(void)
00196 {
00197 unsigned char context_before_final[94] =
00198 {
00199 0xA7, 0x8E, 0x4D, 0x62, 0x03, 0x18, 0x2B, 0x3F, 0xE4, 0xB9, 0x54, 0x95, 0xF5, 0x65, 0xC0, 0x28,
00200 0x8E, 0xB9, 0xFC, 0xA9, 0xE0, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0x61, 0x68, 0x77,
00201 0x20, 0x6F, 0x64, 0x20, 0x77, 0x20, 0x61, 0x79, 0x20, 0x74, 0x6E, 0x61, 0x20, 0x72, 0x6F, 0x66,
00202 0x68, 0x74, 0x6F, 0x6E, 0x3F, 0x67, 0x6E, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00203 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00204 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x00,
00205 };
00206 unsigned char context_after_final[94] =
00207 {
00208 0x44, 0x12, 0x85, 0x98, 0x57, 0x1C, 0xF9, 0x3A, 0xDC, 0xE2, 0x8C, 0xA3, 0x96, 0x87, 0xC3, 0x8E,
00209 0xC4, 0xDE, 0x08, 0x6C, 0xE0, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0x61, 0x68, 0x77,
00210 0x20, 0x6F, 0x64, 0x20, 0x77, 0x20, 0x61, 0x79, 0x20, 0x74, 0x6E, 0x61, 0x20, 0x72, 0x6F, 0x66,
00211 0x68, 0x74, 0x6F, 0x6E, 0x3F, 0x67, 0x6E, 0x69, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
00212 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00213 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x02, 0x00, 0x00, 0x00, 0x00,
00214 };
00215 unsigned char digest_before_final[20] =
00216 {
00217
00218 0x00, 0x00, 0xAC, 0x90, 0x44, 0x67, 0xC0, 0x29, 0x27, 0xFB, 0x60, 0x92, 0xC0, 0x97, 0xFE, 0xAC,
00219 0x1F, 0x30, 0x5A, 0x8B,
00220 };
00221 unsigned char digest_after_final[20] =
00222 {
00223 0x98, 0x85, 0x12, 0x44, 0x3A, 0xF9, 0x1C, 0x57, 0xA3, 0x8C, 0xE2, 0xDC, 0x8E, 0xC3, 0x87, 0x96,
00224 0x6C, 0x08, 0xDE, 0xC4,
00225 };
00226 int local_error_count = 0;
00227 SHA_CTX context;
00228 unsigned char digest[20];
00229
00230 memcpy(&context, context_before_final, sizeof(context_before_final));
00231 memcpy(&digest, digest_before_final, sizeof(digest_before_final));
00232
00233 SHA1_Final(digest, &context);
00234
00235 if(memcmp(&context, context_after_final, sizeof(context_after_final)) != 0)
00236 {
00237 local_error_count++;
00238 IPSEC_LOG_TST("sha1_test_SHA1_Final", "FAILURE", ("context does not match")) ;
00239 printf(" INPUT:\n") ;
00240 IPSEC_DUMP_BUFFER(" ", context_before_final, 0, sizeof(context_before_final));
00241 printf(" OUTPUT:\n") ;
00242 IPSEC_DUMP_BUFFER(" ", (char*)&context, 0, sizeof(context));
00243 printf(" EXPECTED OUTPUT:\n") ;
00244 IPSEC_DUMP_BUFFER(" ", context_after_final, 0, sizeof(context_after_final));
00245 }
00246
00247 if(memcmp(&digest, digest_after_final, sizeof(digest_after_final)) != 0)
00248 {
00249 local_error_count++;
00250 IPSEC_LOG_TST("sha1_test_SHA1_Final", "FAILURE", ("digest does not match")) ;
00251 printf(" INPUT:\n") ;
00252 IPSEC_DUMP_BUFFER(" ", digest_before_final, 0, sizeof(digest_before_final));
00253 printf(" OUTPUT:\n") ;
00254 IPSEC_DUMP_BUFFER(" ", (char*)&digest, 0, sizeof(digest));
00255 printf(" EXPECTED OUTPUT:\n") ;
00256 IPSEC_DUMP_BUFFER(" ", digest_after_final, 0, sizeof(digest_after_final));
00257 }
00258
00259 return local_error_count;
00260 }
00261
00262
00263
00268 void sha1_test(test_result *global_results)
00269 {
00270 test_result sub_results = {
00271 13,
00272 3,
00273 0,
00274 0,
00275 };
00276 int retcode;
00277
00278 retcode = sha1_test_SHA1_Init();
00279 IPSEC_TESTING_EVALUATE(retcode, sub_results, "sha1_test_SHA1_Init()", ("ported from openssl.org"));
00280
00281 retcode = sha1_test_SHA1_Update();
00282 IPSEC_TESTING_EVALUATE(retcode, sub_results, "sha1_test_SHA1_Update()", ("ported from openssl.org"));
00283
00284 retcode = sha1_test_SHA1_Final();
00285 IPSEC_TESTING_EVALUATE(retcode, sub_results, "sha1_test_SHA1_Final()", ("ported from openssl.org"));
00286
00287 global_results->tests += sub_results.tests;
00288 global_results->functions += sub_results.functions;
00289 global_results->errors += sub_results.errors;
00290 global_results->notimplemented += sub_results.notimplemented;
00291 }
00292
00293