| 48 |
#define DEATTACK_DETECTED 1 |
#define DEATTACK_DETECTED 1 |
| 49 |
|
|
| 50 |
/* |
/* |
| 51 |
* $Id: crypt.c,v 1.14 2007-10-17 04:03:41 maya Exp $ Cryptographic attack |
* $Id: crypt.c,v 1.15 2007-10-17 15:58:15 maya Exp $ Cryptographic attack |
| 52 |
* detector for ssh - source code (C)1998 CORE-SDI, Buenos Aires Argentina |
* detector for ssh - source code (C)1998 CORE-SDI, Buenos Aires Argentina |
| 53 |
* Ariel Futoransky(futo@core-sdi.com) <http://www.core-sdi.com> |
* Ariel Futoransky(futo@core-sdi.com) <http://www.core-sdi.com> |
| 54 |
*/ |
*/ |
| 241 |
} |
} |
| 242 |
|
|
| 243 |
if (EVP_Cipher(&pvar->evpcip[MODE_OUT], newbuf, buf, bytes) == 0) { |
if (EVP_Cipher(&pvar->evpcip[MODE_OUT], newbuf, buf, bytes) == 0) { |
|
// TODO: failure |
|
| 244 |
UTIL_get_lang_msg("MSG_AES128_ENCRYPT_ERROR2", pvar, |
UTIL_get_lang_msg("MSG_AES128_ENCRYPT_ERROR2", pvar, |
| 245 |
"AES128 encrypt error(1): bytes %d (%d)"); |
"AES128 encrypt error(2)"); |
| 246 |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
| 247 |
|
goto error; |
| 248 |
|
|
| 249 |
} else { |
} else { |
| 250 |
//memcpy(key, pvar->ssh2_keys[MODE_OUT].enc.key, AES128_KEYLEN); |
//memcpy(key, pvar->ssh2_keys[MODE_OUT].enc.key, AES128_KEYLEN); |
| 287 |
} |
} |
| 288 |
|
|
| 289 |
if (EVP_Cipher(&pvar->evpcip[MODE_IN], newbuf, buf, bytes) == 0) { |
if (EVP_Cipher(&pvar->evpcip[MODE_IN], newbuf, buf, bytes) == 0) { |
|
// TODO: |
|
| 290 |
UTIL_get_lang_msg("MSG_AES128_DECRYPT_ERROR2", pvar, |
UTIL_get_lang_msg("MSG_AES128_DECRYPT_ERROR2", pvar, |
| 291 |
"AES128 decrypt error(2)"); |
"AES128 decrypt error(2)"); |
| 292 |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
notify_fatal_error(pvar, pvar->ts->UIMsg); |
| 293 |
|
goto error; |
| 294 |
|
|
| 295 |
} else { |
} else { |
| 296 |
#if 0 |
//memcpy(key, pvar->ssh2_keys[MODE_IN].enc.key, AES128_KEYLEN); |
|
memcpy(key, pvar->ssh2_keys[MODE_IN].enc.key, AES128_KEYLEN); |
|
| 297 |
// IVはDES関数内で更新されるため、ローカルにコピーしてから使う。 |
// IVはDES関数内で更新されるため、ローカルにコピーしてから使う。 |
| 298 |
memcpy(iv, pvar->ssh2_keys[MODE_IN].enc.iv, AES128_IVLEN); |
//memcpy(iv, pvar->ssh2_keys[MODE_IN].enc.iv, AES128_IVLEN); |
| 299 |
|
|
| 300 |
{ |
//debug_print(70, key, AES128_KEYLEN); |
| 301 |
static int no = 70; |
//debug_print(71, iv, AES128_IVLEN); |
| 302 |
debug_print(no, buf, bytes); |
//debug_print(72, buf, bytes); |
| 303 |
//debug_print(no, key, AES128_KEYLEN); |
//debug_print(73, newbuf, bytes); |
|
//debug_print(10*no, iv, AES128_IVLEN); |
|
|
debug_print(30*no, newbuf, bytes); |
|
|
no++; |
|
|
} |
|
|
#endif |
|
| 304 |
|
|
| 305 |
memcpy(buf, newbuf, bytes); |
memcpy(buf, newbuf, bytes); |
| 306 |
} |
} |
| 307 |
|
|
| 308 |
error:; |
error: |
| 309 |
free(newbuf); |
free(newbuf); |
| 310 |
} |
} |
| 311 |
|
|
| 315 |
static void c3DES_CBC_encrypt(PTInstVar pvar, unsigned char FAR * buf, |
static void c3DES_CBC_encrypt(PTInstVar pvar, unsigned char FAR * buf, |
| 316 |
int bytes) |
int bytes) |
| 317 |
{ |
{ |
| 318 |
unsigned char key[24], iv[8]; |
// unsigned char key[24], iv[8]; |
| 319 |
unsigned char *newbuf = malloc(bytes); |
unsigned char *newbuf = malloc(bytes); |
| 320 |
|
|
| 321 |
if (newbuf == NULL) |
if (newbuf == NULL) |
| 322 |
return; |
return; |
| 323 |
|
|
|
#if 1 |
|
|
|
|
| 324 |
if (EVP_Cipher(&pvar->evpcip[MODE_OUT], newbuf, buf, bytes) == 0) { |
if (EVP_Cipher(&pvar->evpcip[MODE_OUT], newbuf, buf, bytes) == 0) { |
| 325 |
// TODO: failure |
UTIL_get_lang_msg("MSG_3DESCBC_ENCRYPT_ERROR", pvar, |
| 326 |
|
"3DES-CBC encrypt error"); |
| 327 |
|
notify_fatal_error(pvar, pvar->ts->UIMsg); |
| 328 |
|
goto error; |
| 329 |
|
|
| 330 |
} else { |
} else { |
| 331 |
memcpy(key, pvar->ssh2_keys[MODE_OUT].enc.key, 24); |
//memcpy(key, pvar->ssh2_keys[MODE_OUT].enc.key, 24); |
| 332 |
// IVはDES関数内で更新されるため、ローカルにコピーしてから使う。 |
// IVはDES関数内で更新されるため、ローカルにコピーしてから使う。 |
| 333 |
memcpy(iv, pvar->ssh2_keys[MODE_OUT].enc.iv, 8); |
//memcpy(iv, pvar->ssh2_keys[MODE_OUT].enc.iv, 8); |
| 334 |
|
|
| 335 |
//debug_print(50, key, 24); |
//debug_print(50, key, 24); |
| 336 |
//debug_print(51, iv, 8); |
//debug_print(51, iv, 8); |
| 339 |
|
|
| 340 |
memcpy(buf, newbuf, bytes); |
memcpy(buf, newbuf, bytes); |
| 341 |
} |
} |
|
free(newbuf); |
|
|
#else |
|
|
|
|
|
memcpy(key, pvar->ssh2_keys[MODE_OUT].enc.key, 24); |
|
|
// IVはDES関数内で更新されるため、ローカルにコピーしてから使う。 |
|
|
memcpy(iv, pvar->ssh2_keys[MODE_OUT].enc.iv, 8); |
|
|
|
|
|
//debug_print(50, key, 24); |
|
|
//debug_print(51, iv, 8); |
|
|
//debug_print(52, buf, bytes); |
|
|
|
|
|
#if 0 |
|
|
DES_ede3_cbc_encrypt( |
|
|
buf, newbuf, bytes, |
|
|
(DES_key_schedule *)&key[0], |
|
|
(DES_key_schedule *)&key[8], |
|
|
(DES_key_schedule *)&key[16], |
|
|
(DES_cblock *)iv, |
|
|
DES_ENCRYPT); |
|
|
#else |
|
|
DES_ncbc_encrypt(buf, newbuf, bytes, (DES_key_schedule *)&key[0], (DES_cblock *)iv, DES_ENCRYPT); |
|
|
DES_ncbc_encrypt(buf, newbuf, bytes, (DES_key_schedule *)&key[8], (DES_cblock *)iv, DES_DECRYPT); |
|
|
DES_ncbc_encrypt(buf, newbuf, bytes, (DES_key_schedule *)&key[16], (DES_cblock *)iv, DES_ENCRYPT); |
|
| 342 |
|
|
| 343 |
#endif |
error: |
|
|
|
|
//debug_print(53, newbuf, bytes); |
|
|
|
|
|
memcpy(buf, newbuf, bytes); |
|
| 344 |
free(newbuf); |
free(newbuf); |
|
|
|
|
#endif |
|
| 345 |
} |
} |
| 346 |
|
|
| 347 |
static void c3DES_CBC_decrypt(PTInstVar pvar, unsigned char FAR * buf, |
static void c3DES_CBC_decrypt(PTInstVar pvar, unsigned char FAR * buf, |
| 348 |
int bytes) |
int bytes) |
| 349 |
{ |
{ |
| 350 |
unsigned char key[24], iv[8]; |
// unsigned char key[24], iv[8]; |
| 351 |
unsigned char *newbuf = malloc(bytes); |
unsigned char *newbuf = malloc(bytes); |
| 352 |
|
|
| 353 |
if (newbuf == NULL) |
if (newbuf == NULL) |
| 354 |
return; |
return; |
| 355 |
|
|
|
#if 1 |
|
| 356 |
if (EVP_Cipher(&pvar->evpcip[MODE_IN], newbuf, buf, bytes) == 0) { |
if (EVP_Cipher(&pvar->evpcip[MODE_IN], newbuf, buf, bytes) == 0) { |
| 357 |
// TODO: |
UTIL_get_lang_msg("MSG_3DESCBC_DECRYPT_ERROR", pvar, |
| 358 |
|
"3DES-CBC decrypt error"); |
| 359 |
|
notify_fatal_error(pvar, pvar->ts->UIMsg); |
| 360 |
|
goto error; |
| 361 |
|
|
| 362 |
} else { |
} else { |
| 363 |
memcpy(key, pvar->ssh2_keys[MODE_IN].enc.key, 24); |
//memcpy(key, pvar->ssh2_keys[MODE_IN].enc.key, 24); |
| 364 |
// IVはDES関数内で更新されるため、ローカルにコピーしてから使う。 |
// IVはDES関数内で更新されるため、ローカルにコピーしてから使う。 |
| 365 |
memcpy(iv, pvar->ssh2_keys[MODE_IN].enc.iv, 8); |
//memcpy(iv, pvar->ssh2_keys[MODE_IN].enc.iv, 8); |
| 366 |
|
|
| 367 |
//debug_print(70, key, 24); |
//debug_print(70, key, 24); |
| 368 |
//debug_print(71, iv, 8); |
//debug_print(71, iv, 8); |
| 371 |
|
|
| 372 |
memcpy(buf, newbuf, bytes); |
memcpy(buf, newbuf, bytes); |
| 373 |
} |
} |
|
free(newbuf); |
|
|
|
|
|
#else |
|
|
unsigned char *key, iv[8]; |
|
|
unsigned char *newbuf = malloc(bytes); |
|
|
if (newbuf == NULL) |
|
|
return; |
|
|
|
|
|
key = pvar->ssh2_keys[MODE_IN].enc.key; |
|
|
// IVはDES関数内で更新されるため、ローカルにコピーしてから使う。 |
|
|
memcpy(iv, pvar->ssh2_keys[MODE_IN].enc.iv, 8); |
|
|
|
|
|
//debug_print(60, key, 24); |
|
|
//debug_print(61, iv, 8); |
|
|
//debug_print(62, buf, bytes); |
|
|
|
|
|
DES_ede3_cbc_encrypt( |
|
|
buf, newbuf, bytes, |
|
|
(DES_key_schedule *)&key[0], |
|
|
(DES_key_schedule *)&key[8], |
|
|
(DES_key_schedule *)&key[16], |
|
|
(DES_cblock *)iv, |
|
|
DES_DECRYPT); |
|
|
|
|
|
//debug_print(63, newbuf, bytes); |
|
|
|
|
|
memcpy(buf, newbuf, bytes); |
|
| 374 |
|
|
| 375 |
|
error: |
| 376 |
free(newbuf); |
free(newbuf); |
|
#endif |
|
| 377 |
} |
} |
| 378 |
|
|
| 379 |
|
|
| 1003 |
const u_char *key, u_int keylen, |
const u_char *key, u_int keylen, |
| 1004 |
const u_char *iv, u_int ivlen, |
const u_char *iv, u_int ivlen, |
| 1005 |
int encrypt, |
int encrypt, |
| 1006 |
const EVP_CIPHER *(*func)(void)) |
const EVP_CIPHER *(*func)(void), |
| 1007 |
|
PTInstVar pvar) |
| 1008 |
{ |
{ |
| 1009 |
EVP_CIPHER *type; |
EVP_CIPHER *type; |
| 1010 |
int klen; |
int klen; |
| 1011 |
|
char tmp[80]; |
| 1012 |
|
|
| 1013 |
type = (EVP_CIPHER *)func(); |
type = (EVP_CIPHER *)func(); |
| 1014 |
|
|
| 1015 |
EVP_CIPHER_CTX_init(evp); |
EVP_CIPHER_CTX_init(evp); |
| 1016 |
if (EVP_CipherInit(evp, type, NULL, (u_char *)iv, (encrypt == CIPHER_ENCRYPT)) == 0) { |
if (EVP_CipherInit(evp, type, NULL, (u_char *)iv, (encrypt == CIPHER_ENCRYPT)) == 0) { |
| 1017 |
// TODO: |
UTIL_get_lang_msg("MSG_CIPHER_INIT_ERROR", pvar, |
| 1018 |
|
"Cipher initialize error(%d)"); |
| 1019 |
|
_snprintf_s(tmp, sizeof(tmp), _TRUNCATE, pvar->ts->UIMsg, 1); |
| 1020 |
|
notify_fatal_error(pvar, tmp); |
| 1021 |
|
return; |
| 1022 |
} |
} |
| 1023 |
|
|
| 1024 |
klen = EVP_CIPHER_CTX_key_length(evp); |
klen = EVP_CIPHER_CTX_key_length(evp); |
| 1025 |
if (klen > 0 && keylen != klen) { |
if (klen > 0 && keylen != klen) { |
| 1026 |
if (EVP_CIPHER_CTX_set_key_length(evp, keylen) == 0) { |
if (EVP_CIPHER_CTX_set_key_length(evp, keylen) == 0) { |
| 1027 |
// TODO: |
UTIL_get_lang_msg("MSG_CIPHER_INIT_ERROR", pvar, |
| 1028 |
|
"Cipher initialize error(%d)"); |
| 1029 |
|
_snprintf_s(tmp, sizeof(tmp), _TRUNCATE, pvar->ts->UIMsg, 2); |
| 1030 |
|
notify_fatal_error(pvar, tmp); |
| 1031 |
|
return; |
| 1032 |
} |
} |
| 1033 |
} |
} |
| 1034 |
if (EVP_CipherInit(evp, NULL, (u_char *)key, NULL, -1) == 0) { |
if (EVP_CipherInit(evp, NULL, (u_char *)key, NULL, -1) == 0) { |
| 1035 |
// TODO: |
UTIL_get_lang_msg("MSG_CIPHER_INIT_ERROR", pvar, |
| 1036 |
|
"Cipher initialize error(%d)"); |
| 1037 |
|
_snprintf_s(tmp, sizeof(tmp), _TRUNCATE, pvar->ts->UIMsg, 3); |
| 1038 |
|
notify_fatal_error(pvar, tmp); |
| 1039 |
|
return; |
| 1040 |
} |
} |
| 1041 |
} |
} |
| 1042 |
|
|
| 1059 |
enc->key, get_cipher_key_len(pvar->crypt_state.sender_cipher), |
enc->key, get_cipher_key_len(pvar->crypt_state.sender_cipher), |
| 1060 |
enc->iv, get_cipher_block_size(pvar->crypt_state.sender_cipher), |
enc->iv, get_cipher_block_size(pvar->crypt_state.sender_cipher), |
| 1061 |
CIPHER_ENCRYPT, |
CIPHER_ENCRYPT, |
| 1062 |
get_cipher_EVP_CIPHER(pvar->crypt_state.sender_cipher)); |
get_cipher_EVP_CIPHER(pvar->crypt_state.sender_cipher), |
| 1063 |
|
pvar); |
| 1064 |
|
|
| 1065 |
//debug_print(10, enc->key, get_cipher_key_len(pvar->crypt_state.sender_cipher)); |
//debug_print(10, enc->key, get_cipher_key_len(pvar->crypt_state.sender_cipher)); |
| 1066 |
//debug_print(11, enc->iv, get_cipher_block_size(pvar->crypt_state.sender_cipher)); |
//debug_print(11, enc->iv, get_cipher_block_size(pvar->crypt_state.sender_cipher)); |
| 1081 |
enc->key, get_cipher_key_len(pvar->crypt_state.sender_cipher), |
enc->key, get_cipher_key_len(pvar->crypt_state.sender_cipher), |
| 1082 |
enc->iv, get_cipher_block_size(pvar->crypt_state.sender_cipher), |
enc->iv, get_cipher_block_size(pvar->crypt_state.sender_cipher), |
| 1083 |
CIPHER_ENCRYPT, |
CIPHER_ENCRYPT, |
| 1084 |
get_cipher_EVP_CIPHER(pvar->crypt_state.sender_cipher)); |
get_cipher_EVP_CIPHER(pvar->crypt_state.sender_cipher), |
| 1085 |
|
pvar); |
| 1086 |
|
|
| 1087 |
//debug_print(10, enc->key, get_cipher_key_len(pvar->crypt_state.sender_cipher)); |
//debug_print(10, enc->key, get_cipher_key_len(pvar->crypt_state.sender_cipher)); |
| 1088 |
//debug_print(11, enc->iv, get_cipher_block_size(pvar->crypt_state.sender_cipher)); |
//debug_print(11, enc->iv, get_cipher_block_size(pvar->crypt_state.sender_cipher)); |
| 1136 |
enc->key, get_cipher_key_len(pvar->crypt_state.sender_cipher), |
enc->key, get_cipher_key_len(pvar->crypt_state.sender_cipher), |
| 1137 |
enc->iv, get_cipher_block_size(pvar->crypt_state.sender_cipher), |
enc->iv, get_cipher_block_size(pvar->crypt_state.sender_cipher), |
| 1138 |
CIPHER_DECRYPT, |
CIPHER_DECRYPT, |
| 1139 |
get_cipher_EVP_CIPHER(pvar->crypt_state.sender_cipher)); |
get_cipher_EVP_CIPHER(pvar->crypt_state.sender_cipher), |
| 1140 |
|
pvar); |
| 1141 |
|
|
| 1142 |
//debug_print(12, enc->key, 24); |
//debug_print(12, enc->key, 24); |
| 1143 |
//debug_print(13, enc->iv, 24); |
//debug_print(13, enc->iv, 24); |
| 1158 |
enc->key, get_cipher_key_len(pvar->crypt_state.sender_cipher), |
enc->key, get_cipher_key_len(pvar->crypt_state.sender_cipher), |
| 1159 |
enc->iv, get_cipher_block_size(pvar->crypt_state.sender_cipher), |
enc->iv, get_cipher_block_size(pvar->crypt_state.sender_cipher), |
| 1160 |
CIPHER_DECRYPT, |
CIPHER_DECRYPT, |
| 1161 |
get_cipher_EVP_CIPHER(pvar->crypt_state.sender_cipher)); |
get_cipher_EVP_CIPHER(pvar->crypt_state.sender_cipher), |
| 1162 |
|
pvar); |
| 1163 |
|
|
| 1164 |
//debug_print(12, enc->key, 24); |
//debug_print(12, enc->key, 24); |
| 1165 |
//debug_print(13, enc->iv, 24); |
//debug_print(13, enc->iv, 24); |