[Ttssh2-commit] [6984] ssh の暗号方式関連のコード整理

Back to archive index

scmno****@osdn***** scmno****@osdn*****
2017年 11月 26日 (日) 00:26:39 JST


Revision: 6984
          http://sourceforge.jp/projects/ttssh2/scm/svn/commits/6984
Author:   doda
Date:     2017-11-26 00:26:39 +0900 (Sun, 26 Nov 2017)
Log Message:
-----------
ssh の暗号方式関連のコード整理

・暗号関連のパラメータ取得で、暗号方式が見つからなかった時(通常は無い)に
  未初期化の変数の値を返していたのを修正。
・条件分岐の簡略化
・不用な条件分岐/代入を削除

Modified Paths:
--------------
    trunk/ttssh2/ttxssh/ssh.c
    trunk/ttssh2/ttxssh/ssh.h

-------------- next part --------------
Modified: trunk/ttssh2/ttxssh/ssh.c
===================================================================
--- trunk/ttssh2/ttxssh/ssh.c	2017-11-25 15:26:35 UTC (rev 6983)
+++ trunk/ttssh2/ttxssh/ssh.c	2017-11-25 15:26:39 UTC (rev 6984)
@@ -4053,46 +4053,46 @@
 int get_cipher_block_size(SSHCipher cipher)
 {
 	ssh2_cipher_t *ptr = ssh2_ciphers;
-	int val = 8;
 
 	while (ptr->name != NULL) {
 		if (cipher == ptr->cipher) {
-			val = ptr->block_size;
-			break;
+			return ptr->block_size;
 		}
 		ptr++;
 	}
-	return (val);
+
+	// not found.
+	return 8;
 }
 
 int get_cipher_key_len(SSHCipher cipher)
 {
 	ssh2_cipher_t *ptr = ssh2_ciphers;
-	int val = 0;
 
 	while (ptr->name != NULL) {
 		if (cipher == ptr->cipher) {
-			val = ptr->key_len;
-			break;
+			return ptr->key_len;
 		}
 		ptr++;
 	}
-	return (val);
+
+	// not found.
+	return 0;
 }
 
 int get_cipher_discard_len(SSHCipher cipher)
 {
 	ssh2_cipher_t *ptr = ssh2_ciphers;
-	int val = 0;
 
 	while (ptr->name != NULL) {
 		if (cipher == ptr->cipher) {
-			val = ptr->discard_len;
-			break;
+			return ptr->discard_len;
 		}
 		ptr++;
 	}
-	return (val);
+
+	// not found.
+	return 0;
 }
 
 // \x88Í\x86\x83A\x83\x8B\x83S\x83\x8A\x83Y\x83\x80\x96\xBC\x82\xA9\x82猟\x8D\x{142DC2}\xE9\x81B
@@ -4099,7 +4099,6 @@
 SSHCipher get_cipher_by_name(char *name)
 {
 	ssh2_cipher_t *ptr = ssh2_ciphers;
-	SSHCipher ret = SSH_CIPHER_NONE;
 
 	if (name == NULL)
 		goto error;
@@ -4106,181 +4105,179 @@
 
 	while (ptr->name != NULL) {
 		if (strcmp(ptr->name, name) == 0) {
-			ret = ptr->cipher;
-			break;
+			return ptr->cipher;
 		}
 		ptr++;
 	}
+
+	// not found.
 error:
-	return (ret);
+	return SSH_CIPHER_NONE;
 }
 
 static char * get_cipher_string(SSHCipher cipher)
 {
 	ssh2_cipher_t *ptr = ssh2_ciphers;
-	char *p = "unknown";
 
 	while (ptr->name != NULL) {
 		if (cipher == ptr->cipher) {
-			p = ptr->name;
-			break;
+			return ptr->name;
 		}
 		ptr++;
 	}
-	return p;
+
+	// not found.
+	return "unknown";
 }
 
 const EVP_CIPHER* get_cipher_EVP_CIPHER(SSHCipher cipher)
 {
 	ssh2_cipher_t *ptr = ssh2_ciphers;
-	const EVP_CIPHER *type;
 
-	type = EVP_enc_null();
-
 	while (ptr->name != NULL) {
 		if (cipher == ptr->cipher) {
-			type = ptr->func();
-			break;
+			return ptr->func();
 		}
 		ptr++;
 	}
-	return type;
+
+	// not found.
+	return EVP_enc_null();
 }
 
 char* get_kex_algorithm_name(kex_algorithm kextype)
 {
 	ssh2_kex_algorithm_t *ptr = ssh2_kex_algorithms;
-	char *p = "unknown";
 
 	while (ptr->name != NULL) {
 		if (kextype == ptr->kextype) {
-			p = ptr->name;
-			break;
+			return ptr->name;
 		}
 		ptr++;
 	}
-	return p;
+
+	// not found.
+	return "unknown";
 }
 
 const EVP_MD* get_kex_algorithm_EVP_MD(kex_algorithm kextype)
 {
 	ssh2_kex_algorithm_t *ptr = ssh2_kex_algorithms;
-	const EVP_MD *evp_md;
 
 	while (ptr->name != NULL) {
 		if (kextype == ptr->kextype) {
-			evp_md = ptr->evp_md();
-			break;
+			return ptr->evp_md();
 		}
 		ptr++;
 	}
-	return evp_md;
+
+	// not found.
+	return EVP_md_null();
 }
 
 char* get_ssh2_mac_name(hmac_type type)
 {
 	ssh2_mac_t *ptr = ssh2_macs;
-	char *p = "unknown";
 
 	while (ptr->name != NULL) {
 		if (type == ptr->type) {
-			p = ptr->name;
-			break;
+			return ptr->name;
 		}
 		ptr++;
 	}
-	return p;
+
+	// not found.
+	return "unknown";
 }
 
 const EVP_MD* get_ssh2_mac_EVP_MD(hmac_type type)
 {
 	ssh2_mac_t *ptr = ssh2_macs;
-	const EVP_MD *evp_md;
 
 	while (ptr->name != NULL) {
 		if (type == ptr->type) {
-			evp_md = ptr->evp_md();
-			break;
+			return ptr->evp_md();
 		}
 		ptr++;
 	}
-	return evp_md;
+
+	// not found.
+	return EVP_md_null();
 }
 
 int get_ssh2_mac_truncatebits(hmac_type type)
 {
 	ssh2_mac_t *ptr = ssh2_macs;
-	int bits;
 
 	while (ptr->name != NULL) {
 		if (type == ptr->type) {
-			bits = ptr->truncatebits;
-			break;
+			return ptr->truncatebits;
 		}
 		ptr++;
 	}
-	return bits;
+
+	// not found.
+	return 0;
 }
 
 int get_ssh2_mac_etm(hmac_type type)
 {
 	ssh2_mac_t *ptr = ssh2_macs;
-	int etm;
 
 	while (ptr->name != NULL) {
 		if (type == ptr->type) {
-			etm = ptr->etm;
-			break;
+			return ptr->etm;
 		}
 		ptr++;
 	}
-	return etm;
+
+	// not found
+	return 0;
 }
 
 char* get_ssh2_comp_name(compression_type type)
 {
 	ssh2_comp_t *ptr = ssh2_comps;
-	char *p = "unknown";
 
 	while (ptr->name != NULL) {
 		if (type == ptr->type) {
-			p = ptr->name;
-			break;
+			return ptr->name;
 		}
 		ptr++;
 	}
-	return p;
+
+	// not found.
+	return "unknown";
 }
 
 char* get_ssh_keytype_name(ssh_keytype type)
 {
 	ssh2_host_key_t *ptr = ssh2_host_key;
-	char *p = "ssh-unknown";
 
 	while (ptr->name != NULL) {
 		if (type == ptr->type) {
-			// ssh2_host_key[]\x82̓O\x83\x8D\x81[\x83o\x83\x8B\x95ϐ\x94\x82Ȃ̂ŁA\x82\xBB\x82̂܂ܕԂ\xE8\x92l\x82ɂł\xAB\x82\xE9\x81B
-			p = ptr->name;
-			break;
+			return ptr->name;
 		}
 		ptr++;
 	}
-	return p;
+
+	// not found.
+	return "ssh-unknown";
 }
 
 char* get_digest_algorithm_name(digest_algorithm id)
 {
 	ssh_digest_t *ptr = ssh_digests;
-	char *p = "unknown";
 
 	while (ptr->name != NULL) {
 		if (id == ptr->id) {
-			p = ptr->name;
-			break;
+			return ptr->name;
 		}
 		ptr++;
 	}
-	return p;
+
+	// not found.
+	return "unknown";
 }
 
 static void do_write_buffer_file(void *buf, int len, char *file, int lineno)
@@ -4299,7 +4296,6 @@
 	fclose(fp);
 }
 
-
 void SSH2_packet_start(buffer_t *msg, unsigned char type)
 {
 	unsigned char buf[9];
@@ -4311,7 +4307,6 @@
 	buffer_append(msg, buf, len);
 }
 
-
 // the caller is normalize_cipher_order()
 void SSH2_update_cipher_myproposal(PTInstVar pvar)
 {
@@ -4748,38 +4743,35 @@
 // \x88Í\x86\x83A\x83\x8B\x83S\x83\x8A\x83Y\x83\x80\x82̃L\x81[\x83T\x83C\x83Y\x81A\x83u\x83\x8D\x83b\x83N\x83T\x83C\x83Y\x81AMAC\x83T\x83C\x83Y\x82̂\xA4\x82\xBF\x8Dő\xE5\x92l(we_need)\x82\xF0\x8C\x88\x92肷\x82\xE9\x81B
 static void choose_SSH2_key_maxlength(PTInstVar pvar)
 {
-	int mode, need, val, ctos;
+	int mode, val;
+	unsigned int need = 0;
 	const EVP_MD *md;
+	SSHCipher cipher;
+	hmac_type mac;
 
 	for (mode = 0; mode < MODE_MAX; mode++) {
-		if (mode == MODE_OUT)
-			ctos = 1;
-		else
-			ctos = 0;
-
-		if (ctos == 1) {
-			val = pvar->ctos_hmac;
-		} else {
-			val = pvar->stoc_hmac;
+		if (mode == MODE_OUT) {
+			mac = pvar->ctos_hmac;
+			cipher = pvar->ctos_cipher;
 		}
+		else {
+			mac = pvar->stoc_hmac;
+			cipher = pvar->stoc_cipher;
+		}
 
 		// current_keys[]\x82ɐݒ肵\x82Ă\xA8\x82\xA2\x82āA\x82\xA0\x82Ƃ\xC5 pvar->ssh2_keys[] \x82փR\x83s\x81[\x82\xB7\x82\xE9\x81B
-		md = get_ssh2_mac_EVP_MD(val);
+		md = get_ssh2_mac_EVP_MD(mac);
 		current_keys[mode].mac.md = md;
 		current_keys[mode].mac.key_len = current_keys[mode].mac.mac_len = EVP_MD_size(md);
-		if (get_ssh2_mac_truncatebits(val) != 0) {
-			current_keys[mode].mac.mac_len = get_ssh2_mac_truncatebits(val) / 8;
+		val = get_ssh2_mac_truncatebits(mac);
+		if (val != 0) {
+			current_keys[mode].mac.mac_len = val / 8;
 		}
-		current_keys[mode].mac.etm = get_ssh2_mac_etm(val);
+		current_keys[mode].mac.etm = get_ssh2_mac_etm(mac);
 
 		// \x83L\x81[\x83T\x83C\x83Y\x82ƃu\x83\x8D\x83b\x83N\x83T\x83C\x83Y\x82\xE0\x82\xB1\x82\xB1\x82Őݒ肵\x82Ă\xA8\x82\xAD (2004.11.7 yutaka)
-		if (ctos == 1) {
-			current_keys[mode].enc.key_len = get_cipher_key_len(pvar->ctos_cipher);
-			current_keys[mode].enc.block_size = get_cipher_block_size(pvar->ctos_cipher);
-		} else {
-			current_keys[mode].enc.key_len = get_cipher_key_len(pvar->stoc_cipher);
-			current_keys[mode].enc.block_size = get_cipher_block_size(pvar->stoc_cipher);
-		}
+		current_keys[mode].enc.key_len = get_cipher_key_len(cipher);
+		current_keys[mode].enc.block_size = get_cipher_block_size(cipher);
 		current_keys[mode].mac.enabled = 0;
 		current_keys[mode].comp.enabled = 0; // (2005.7.9 yutaka)
 
@@ -4787,27 +4779,13 @@
 		pvar->ssh2_keys[mode].mac.enabled = 0;
 		pvar->ssh2_keys[mode].comp.enabled = 0; // (2005.7.9 yutaka)
 	}
-	need = 0;
+
 	for (mode = 0; mode < MODE_MAX; mode++) {
-		if (mode == MODE_OUT)
-			ctos = 1;
-		else
-			ctos = 0;
-
-		val = current_keys[mode].enc.key_len;
-		if (need < val)
-			need = val;
-
-		val = current_keys[mode].enc.block_size;
-		if (need < val)
-			need = val;
-
-		val = current_keys[mode].mac.key_len;
-		if (need < val)
-			need = val;
+		need = max(need, current_keys[mode].enc.key_len);
+		need = max(need, current_keys[mode].enc.block_size);
+		need = max(need, current_keys[mode].mac.key_len);
 	}
 	pvar->we_need = need;
-
 }
 
 

Modified: trunk/ttssh2/ttxssh/ssh.h
===================================================================
--- trunk/ttssh2/ttxssh/ssh.h	2017-11-25 15:26:35 UTC (rev 6983)
+++ trunk/ttssh2/ttxssh/ssh.h	2017-11-25 15:26:39 UTC (rev 6984)
@@ -535,13 +535,13 @@
 };
 
 struct Mac {
-	char            *name; 
-	int             enabled; 
+	char            *name;
+	int             enabled;
 	const EVP_MD    *md;
-	int             mac_len; 
+	unsigned int    mac_len;
 	u_char          *key;
-	int             key_len;
-	int		etm;
+	unsigned int    key_len;
+	int             etm;
 };
 
 struct Comp {



Ttssh2-commit メーリングリストの案内
Back to archive index