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/des.h"
00054 #include "ipsec/debug.h"
00055 #include "testing/structural/structural_test.h"
00056
00057
00058
00063 int des_test_DES_set_key_checked(void)
00064 {
00065 const unsigned char correct_key[] = {0x01,0x23,0x45,0x67,0x01,0x23,0x45,0x67};
00066 const unsigned char parityerror_key[]= {0x01,0x23,0x45,0x67,0x03,0x23,0x45,0x67};
00067 const unsigned char weak_key[] = {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E};
00068 const unsigned char semiweak_key[] = {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1};
00069
00070 int local_error_count = 0;
00071 int ret_val;
00072 DES_key_schedule ks;
00073
00074 ret_val = DES_set_key_checked(&correct_key,&ks);
00075 if(ret_val != 0) {
00076 local_error_count++;
00077 printf("des_test_DES_set_key_checked(): error - DES_set_key_checked(&correct_key,&ks) rejected correct key - ret_val = %d\n", ret_val);
00078 }
00079
00080 ret_val = DES_set_key_checked(&parityerror_key,&ks);
00081 if(ret_val != -1) {
00082 local_error_count++;
00083 printf("des_test_DES_set_key_checked(): error - DES_set_key_checked(&parityerror_key,&ks) accepted key with parity error - ret_val = %d\n", ret_val);
00084 }
00085
00086 ret_val = DES_set_key_checked(&weak_key,&ks);
00087 if(ret_val != -2) {
00088 local_error_count++;
00089 printf("des_test_DES_set_key_checked(): error - DES_set_key_checked(&weak_key,&ks) did not reject weak key - ret_val = %d\n", ret_val);
00090 }
00091
00092 ret_val = DES_set_key_checked(&semiweak_key,&ks);
00093 if(ret_val != -2) {
00094 local_error_count++;
00095 printf("des_test_DES_set_key_checked(): error - DES_set_key_checked(&semiweak_key,&ks) did not reject semi-weak key - ret_val = %d\n", ret_val);
00096 }
00097
00098 return local_error_count;
00099 }
00100
00101
00106 int des_test_des_ede3_cbc_encrypt(void)
00107 {
00108 const unsigned char esp_encrypted_payload[] = {
00109
00110 0x53, 0x83, 0xFB, 0x32, 0x7F, 0x7E, 0x9A, 0xE9, 0x01, 0xB3, 0x7F, 0x80, 0x06, 0xCF, 0x39, 0x26,
00111 0x6F, 0x5D, 0x25, 0x5B, 0x1E, 0x26, 0x28, 0xC4, 0xB3, 0x78, 0x04, 0xAC, 0x9F, 0x0B, 0xAE, 0x9D,
00112 0x57, 0xAC, 0x4E, 0x82, 0x0E, 0xB4, 0xBA, 0x6C, 0x27, 0x38, 0x0A, 0x71, 0x9B, 0xE1, 0xF7, 0xFD,
00113 0x14, 0xCC, 0x14, 0x0D, 0x71, 0xE5, 0xB8, 0xF7, 0x7C, 0xCB, 0xE0, 0x0B, 0x04, 0x78, 0xB1, 0x7F,
00114 0x2B, 0xEA, 0x85, 0x41, 0x95, 0x22, 0x18, 0xEE, 0x06, 0xFF, 0x79, 0x13, 0x26, 0xD3, 0x08, 0x10,
00115 0x75, 0x04, 0x53, 0x6C, 0x04, 0xFD, 0x91, 0xF3, 0x31, 0x93, 0xB6, 0x14, 0xE3, 0x53, 0xF6, 0xCE,
00116 0x6E, 0xC3, 0x74, 0xFE, 0xCE, 0x62, 0x01, 0xBD, 0x8D, 0x11, 0x26, 0x39, 0x0A, 0x74, 0x51, 0xCA,
00117 0x56, 0x42, 0x51, 0xD5, 0x7B, 0x8E, 0xA6, 0x47, 0xDC, 0x7F, 0x74, 0x09, 0x90, 0x8F, 0x9A, 0xF2,
00118 0x0C, 0x74, 0x81, 0x64, 0x10, 0x0D, 0x64, 0xCE, 0x54, 0xB5, 0x78, 0x81, 0xA5, 0x90, 0x36, 0xEB,
00119 0x82, 0xC9, 0xE7, 0x5C, 0xBD, 0x3F, 0x3B, 0x29, 0xD3, 0x2F, 0x22, 0x6E, 0x6F, 0x24, 0xDD, 0xFB,
00120 0xDD, 0x98, 0x00, 0x1D, 0x4B, 0x82, 0xCE, 0x12, 0x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C3, 0x06, 0x98, 0x6B, 0x64, 0x82, 0xEB, 0xCC,
00132 0x9B, 0x3E, 0x56, 0xC3, 0xA7, 0xEB, 0x9C, 0xF7, 0x07, 0x6C, 0x07, 0x45, 0x8E, 0xBC, 0x85, 0x41,
00133 0x48, 0x69, 0x78, 0x23, 0xAB, 0xBC, 0xE7, 0x1F, 0x7B, 0xD2, 0x07, 0x81, 0xDA, 0x9D, 0x1B, 0x5F,
00134 0x0A, 0x4A, 0x37, 0x88, 0xF8, 0x1C, 0x4F, 0xB4, 0x84, 0xDC, 0x14, 0xD4, 0xD3, 0x3B, 0x29, 0x2B,
00135 0xA9, 0x42, 0x77, 0x2C, 0x91, 0x48, 0x05, 0xF5, 0x21, 0x54, 0xDD, 0xD3, 0xC8, 0xB5, 0xCE, 0xB7,
00136 0x10, 0x7E, 0x63, 0x5A, 0xF3, 0x9E, 0x2C, 0xC7, 0xBC, 0xC8, 0xC9, 0xC2, 0x2D, 0xF0, 0x62, 0x5E,
00137 0xB8, 0xA3, 0x84, 0x8A, 0x3D, 0x5D, 0xB5, 0xF0, 0x14, 0xB4, 0x77, 0xC3, 0x67, 0xD0, 0x5E, 0x77,
00138 };
00139
00140 const unsigned char esp_orig_payload[] = {
00141
00142 0x45, 0x00, 0x01, 0xB9,
00143 0xDF, 0x12, 0x40, 0x00,
00144 0x40, 0x06, 0xD6, 0xB0,
00145 0xC0, 0xA8, 0x01, 0x03,
00146 0xC0, 0xA8, 0x01, 0x28,
00147
00148 0x00, 0x50, 0x80, 0x1A,
00149 0x4B, 0x42, 0xCB, 0x6B,
00150 0x84, 0xB9, 0xC5, 0x67,
00151 0x80, 0x18, 0x7E, 0xA0,
00152 0x39, 0xA7, 0x00, 0x00,
00153 0x01, 0x01, 0x08, 0x0A,
00154 0x00, 0x6B, 0x14, 0x79,
00155 0x00, 0x0F, 0x22, 0x1C,
00156
00157 0x48, 0x54, 0x54, 0x50, 0x2F, 0x31, 0x2E, 0x30, 0x20, 0x32, 0x30, 0x30,
00158 0x20, 0x4F, 0x4B, 0x0D, 0x0A, 0x43, 0x6F, 0x6E, 0x74, 0x65, 0x6E, 0x74, 0x2D, 0x54, 0x79, 0x70,
00159 0x65, 0x3A, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2F, 0x68, 0x74, 0x6D, 0x6C, 0x0D, 0x0A, 0x0D, 0x0A,
00160 0x3C, 0x68, 0x74, 0x6D, 0x6C, 0x3E, 0x0D, 0x0A, 0x3C, 0x68, 0x65, 0x61, 0x64, 0x3E, 0x0D, 0x0A,
00161 0x3C, 0x2F, 0x68, 0x65, 0x61, 0x64, 0x3E, 0x0D, 0x0A, 0x3C, 0x62, 0x6F, 0x64, 0x79, 0x20, 0x74,
00162 0x65, 0x78, 0x74, 0x3D, 0x22, 0x23, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x22, 0x20, 0x62, 0x67,
00163 0x63, 0x6F, 0x6C, 0x6F, 0x72, 0x3D, 0x22, 0x23, 0x46, 0x46, 0x46, 0x46, 0x39, 0x39, 0x22, 0x20,
00164 0x6C, 0x69, 0x6E, 0x6B, 0x3D, 0x22, 0x23, 0x30, 0x30, 0x30, 0x30, 0x45, 0x45, 0x22, 0x20, 0x76,
00165 0x6C, 0x69, 0x6E, 0x6B, 0x3D, 0x22, 0x23, 0x35, 0x35, 0x31, 0x41, 0x38, 0x42, 0x22, 0x20, 0x61,
00166 0x6C, 0x69, 0x6E, 0x6B, 0x3D, 0x22, 0x23, 0x46, 0x46, 0x30, 0x30, 0x30, 0x30, 0x22, 0x3E, 0x0D,
00167 0x0A, 0x26, 0x6E, 0x62, 0x73, 0x70, 0x3B, 0x0D, 0x0A, 0x3C, 0x63, 0x65, 0x6E, 0x74, 0x65, 0x72,
00168 0x3E, 0x3C, 0x74, 0x61, 0x62, 0x6C, 0x65, 0x20, 0x42, 0x4F, 0x52, 0x44, 0x45, 0x52, 0x3D, 0x30,
00169 0x20, 0x43, 0x4F, 0x4C, 0x53, 0x3D, 0x31, 0x20, 0x57, 0x49, 0x44, 0x54, 0x48, 0x3D, 0x22, 0x39,
00170 0x30, 0x25, 0x22, 0x20, 0x48, 0x45, 0x49, 0x47, 0x48, 0x54, 0x3D, 0x22, 0x38, 0x35, 0x25, 0x22,
00171 0x20, 0x3E, 0x0D, 0x0A, 0x3C, 0x74, 0x72, 0x20, 0x41, 0x4C, 0x49, 0x47, 0x4E, 0x3D, 0x4C, 0x45,
00172 0x46, 0x54, 0x20, 0x56, 0x41, 0x4C, 0x49, 0x47, 0x4E, 0x3D, 0x54, 0x4F, 0x50, 0x3E, 0x0D, 0x0A,
00173 0x3C, 0x74, 0x64, 0x3E, 0x3C, 0x62, 0x3E, 0x3C, 0x66, 0x6F, 0x6E, 0x74, 0x20, 0x66, 0x61, 0x63,
00174 0x65, 0x3D, 0x22, 0x56, 0x65, 0x72, 0x64, 0x61, 0x6E, 0x61, 0x22, 0x3E, 0x3C, 0x66, 0x6F, 0x6E,
00175 0x74, 0x20, 0x73, 0x69, 0x7A, 0x65, 0x3D, 0x2B, 0x32, 0x3E, 0x45, 0x6D, 0x62, 0x65, 0x64, 0x64,
00176 0x65, 0x64, 0x20, 0x57, 0x65, 0x62, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x54, 0x65,
00177 0x73, 0x74, 0x3C, 0x2F, 0x66, 0x6F, 0x6E, 0x74, 0x3E, 0x3C, 0x2F, 0x66, 0x6F, 0x6E, 0x74, 0x3E,
00178 0x3C, 0x2F, 0x62, 0x3E, 0x3C, 0x2F, 0x74, 0x64, 0x3E, 0x0D, 0x0A, 0x3C, 0x2F, 0x74, 0x72, 0x3E,
00179 0x0D, 0x0A, 0x3C, 0x2F, 0x74, 0x61, 0x62, 0x6C, 0x65, 0x3E, 0x0D, 0x0A, 0x3C, 0x2F, 0x63, 0x65,
00180 0x6E, 0x74, 0x65, 0x72, 0x3E, 0x0D, 0x0A, 0x3C, 0x2F, 0x62, 0x6F, 0x64, 0x79, 0x3E, 0x0D, 0x0A,
00181 0x3C, 0x2F, 0x68, 0x74, 0x6D, 0x6C, 0x3E, 0x0D, 0x0A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x05, 0x04,
00182 };
00183 unsigned char esp_decrypted_payload[sizeof(esp_encrypted_payload)];
00184 const unsigned char _3des_key[8*3] = { 0x01,0x23,0x45,0x67,0x01,0x23,0x45,0x67,0x01,0x23,0x45,0x67,0x01,0x23,0x45,0x67,0x01,0x23,0x45,0x67,0x01,0x23,0x45,0x67 };
00185 unsigned char iv_esp_payload[8] = { 0xD4,0xDB,0xAB,0x9A,0x9A,0xDB,0xD1,0x94 };
00186 int local_error_count = 0;
00187 int ret_val;
00188
00189
00190 cipher_3des_cbc((unsigned char *)&esp_encrypted_payload,
00191 sizeof(esp_encrypted_payload),
00192 (unsigned char *)&_3des_key,
00193 (unsigned char *)&iv_esp_payload,
00194 DES_DECRYPT,
00195 esp_decrypted_payload);
00196
00197 ret_val = memcmp((unsigned char *)esp_orig_payload[52], (unsigned char *)esp_decrypted_payload[52], sizeof(esp_encrypted_payload) - 52);
00198 if (ret_val != 0)
00199 {
00200 local_error_count++;
00201 printf("des_test_des_ede3_cbc_encrypt(): error - des_ede3_cbc_encrypt() could not properly decrypt 3DES encrypted ESP payload - ret_val = %d\n", ret_val);
00202 IPSEC_DUMP_BUFFER(" input : ", esp_encrypted_payload, 0, sizeof(esp_encrypted_payload));
00203 printf("\n");
00204 IPSEC_DUMP_BUFFER(" output : ", esp_decrypted_payload, 0, sizeof(esp_decrypted_payload));
00205 printf("\n");
00206 IPSEC_DUMP_BUFFER(" expected: ", esp_orig_payload, 0, sizeof(esp_orig_payload));
00207 printf("\n\n");
00208 }
00209
00210 return local_error_count;
00211 }
00212
00213
00214
00219 void des_test(test_result *global_results)
00220 {
00221 test_result sub_results = {
00222 5,
00223 2,
00224 0,
00225 0,
00226 };
00227
00228 int retcode;
00229
00230 retcode = des_test_DES_set_key_checked();
00231 IPSEC_TESTING_EVALUATE(retcode, sub_results, "des_test_DES_set_key_checked()", ("ported from openssl.org"));
00232
00233 retcode = des_test_des_ede3_cbc_encrypt();
00234 IPSEC_TESTING_EVALUATE(retcode, sub_results, "des_test_des_ede3_cbc_encrypt()", ("ported from openssl.org"));
00235
00236 global_results->tests += sub_results.tests;
00237 global_results->functions += sub_results.functions;
00238 global_results->errors += sub_results.errors;
00239 global_results->notimplemented += sub_results.notimplemented;
00240 }
00241
00242
00243