• R/O
  • SSH
  • HTTPS

ttssh2: コミット


コミットメタ情報

リビジョン7133 (tree)
日時2018-06-14 19:57:13
作者doda

ログメッセージ

encrypt/decrypt 用のバッファを毎回 malloc/free するのではなく使いまわすようにした。

多少は軽く(高速に)なるのを期待したが、あまり(ほとんど)効果は出なかった。

変更サマリ

差分

--- trunk/ttssh2/ttxssh/crypt.c (revision 7132)
+++ trunk/ttssh2/ttxssh/crypt.c (revision 7133)
@@ -72,6 +72,8 @@
7272
7373 #define CMP(a,b) memcmp(a, b, SSH_BLOCKSIZE)
7474
75+static unsigned char *encbuff = NULL;
76+static unsigned int encbufflen = 0;
7577
7678 static char *get_cipher_name(int cipher);
7779
@@ -200,7 +202,7 @@
200202
201203 BOOL CRYPT_encrypt_aead(PTInstVar pvar, unsigned char *data, unsigned int bytes, unsigned int aadlen, unsigned int authlen)
202204 {
203- unsigned char *newbuf = NULL;
205+ unsigned char *newbuff = NULL;
204206 unsigned int block_size = pvar->ssh2_keys[MODE_OUT].enc.block_size;
205207 unsigned char lastiv[1];
206208 char tmp[80];
@@ -218,8 +220,12 @@
218220 return FALSE;
219221 }
220222
221- if ((newbuf = malloc(bytes)) == NULL)
222- goto err;
223+ if (bytes > encbufflen) {
224+ if ((newbuff = realloc(encbuff, bytes)) == NULL)
225+ goto err;
226+ encbuff = newbuff;
227+ encbufflen = bytes;
228+ }
223229
224230 if (!EVP_CIPHER_CTX_ctrl(evp, EVP_CTRL_GCM_IV_GEN, 1, lastiv))
225231 goto err;
@@ -227,10 +233,10 @@
227233 if (aadlen && !EVP_Cipher(evp, NULL, data, aadlen) < 0)
228234 goto err;
229235
230- if (EVP_Cipher(evp, newbuf, data+aadlen, bytes) < 0)
236+ if (EVP_Cipher(evp, encbuff, data+aadlen, bytes) < 0)
231237 goto err;
232238
233- memcpy(data+aadlen, newbuf, bytes);
239+ memcpy(data+aadlen, encbuff, bytes);
234240
235241 if (EVP_Cipher(evp, NULL, NULL, 0) < 0)
236242 goto err;
@@ -238,13 +244,9 @@
238244 if (!EVP_CIPHER_CTX_ctrl(evp, EVP_CTRL_GCM_GET_TAG, authlen, data+aadlen+bytes))
239245 goto err;
240246
241- free(newbuf);
242-
243247 return TRUE;
244248
245249 err:
246- free(newbuf);
247-
248250 UTIL_get_lang_msg("MSG_ENCRYPT_ERROR2", pvar, "%s encrypt error(2)");
249251 _snprintf_s(tmp, sizeof(tmp), _TRUNCATE, pvar->ts->UIMsg,
250252 get_cipher_name(pvar->crypt_state.sender_cipher));
@@ -254,7 +256,7 @@
254256
255257 BOOL CRYPT_decrypt_aead(PTInstVar pvar, unsigned char *data, unsigned int bytes, unsigned int aadlen, unsigned int authlen)
256258 {
257- unsigned char *newbuf = NULL;
259+ unsigned char *newbuff = NULL;
258260 unsigned int block_size = pvar->ssh2_keys[MODE_IN].enc.block_size;
259261 unsigned char lastiv[1];
260262 char tmp[80];
@@ -272,8 +274,12 @@
272274 return FALSE;
273275 }
274276
275- if ((newbuf = malloc(bytes)) == NULL)
276- goto err;
277+ if (bytes > encbufflen) {
278+ if ((newbuff = realloc(encbuff, bytes)) == NULL)
279+ goto err;
280+ encbuff = newbuff;
281+ encbufflen = bytes;
282+ }
277283
278284 if (!EVP_CIPHER_CTX_ctrl(evp, EVP_CTRL_GCM_IV_GEN, 1, lastiv))
279285 goto err;
@@ -284,11 +290,10 @@
284290 if (aadlen && !EVP_Cipher(evp, NULL, data, aadlen) < 0)
285291 goto err;
286292
287- if (EVP_Cipher(evp, newbuf, data+aadlen, bytes) < 0)
293+ if (EVP_Cipher(evp, encbuff, data+aadlen, bytes) < 0)
288294 goto err;
289295
290- memcpy(data+aadlen, newbuf, bytes);
291- free(newbuf);
296+ memcpy(data+aadlen, encbuff, bytes);
292297
293298 if (EVP_Cipher(evp, NULL, NULL, 0) < 0)
294299 return FALSE;
@@ -296,8 +301,6 @@
296301 return TRUE;
297302
298303 err:
299- free(newbuf);
300-
301304 UTIL_get_lang_msg("MSG_DECRYPT_ERROR2", pvar, "%s decrypt error(2)");
302305 _snprintf_s(tmp, sizeof(tmp), _TRUNCATE, pvar->ts->UIMsg,
303306 get_cipher_name(pvar->crypt_state.receiver_cipher));
@@ -311,7 +314,7 @@
311314
312315 static void crypt_SSH2_encrypt(PTInstVar pvar, unsigned char *buf, int bytes)
313316 {
314- unsigned char *newbuf;
317+ unsigned char *newbuff;
315318 int block_size = pvar->ssh2_keys[MODE_OUT].enc.block_size;
316319 char tmp[80];
317320
@@ -328,24 +331,26 @@
328331 return;
329332 }
330333
331- if ((newbuf = malloc(bytes)) == NULL)
332- return;
334+ if (bytes > encbufflen) {
335+ if ((newbuff = realloc(encbuff, bytes)) == NULL)
336+ return;
337+ encbuff = newbuff;
338+ encbufflen = bytes;
339+ }
333340
334- if (EVP_Cipher(&pvar->evpcip[MODE_OUT], newbuf, buf, bytes) == 0) {
341+ if (EVP_Cipher(&pvar->evpcip[MODE_OUT], encbuff, buf, bytes) == 0) {
335342 UTIL_get_lang_msg("MSG_ENCRYPT_ERROR2", pvar, "%s encrypt error(2)");
336343 _snprintf_s(tmp, sizeof(tmp), _TRUNCATE, pvar->ts->UIMsg,
337344 get_cipher_name(pvar->crypt_state.sender_cipher));
338345 notify_fatal_error(pvar, tmp, TRUE);
339346 } else {
340- memcpy(buf, newbuf, bytes);
347+ memcpy(buf, encbuff, bytes);
341348 }
342-
343- free(newbuf);
344349 }
345350
346351 static void crypt_SSH2_decrypt(PTInstVar pvar, unsigned char *buf, int bytes)
347352 {
348- unsigned char *newbuf;
353+ unsigned char *newbuff;
349354 int block_size = pvar->ssh2_keys[MODE_IN].enc.block_size;
350355 char tmp[80];
351356
@@ -362,19 +367,21 @@
362367 return;
363368 }
364369
365- if ((newbuf = malloc(bytes)) == NULL)
366- return;
370+ if (bytes > encbufflen) {
371+ if ((newbuff = malloc(bytes)) == NULL)
372+ return;
373+ encbuff = newbuff;
374+ encbufflen = bytes;
375+ }
367376
368- if (EVP_Cipher(&pvar->evpcip[MODE_IN], newbuf, buf, bytes) == 0) {
377+ if (EVP_Cipher(&pvar->evpcip[MODE_IN], encbuff, buf, bytes) == 0) {
369378 UTIL_get_lang_msg("MSG_DECRYPT_ERROR2", pvar, "%s decrypt error(2)");
370379 _snprintf_s(tmp, sizeof(tmp), _TRUNCATE, pvar->ts->UIMsg,
371380 get_cipher_name(pvar->crypt_state.receiver_cipher));
372381 notify_fatal_error(pvar, tmp, TRUE);
373382 } else {
374- memcpy(buf, newbuf, bytes);
383+ memcpy(buf, encbuff, bytes);
375384 }
376-
377- free(newbuf);
378385 }
379386
380387 static void c3DES_encrypt(PTInstVar pvar, unsigned char *buf, int bytes)
@@ -1307,6 +1314,10 @@
13071314
13081315 void CRYPT_end(PTInstVar pvar)
13091316 {
1317+ free(encbuff);
1318+ encbuff = NULL;
1319+ encbufflen = 0;
1320+
13101321 destroy_public_key(&pvar->crypt_state.host_key);
13111322 destroy_public_key(&pvar->crypt_state.server_key);
13121323
旧リポジトリブラウザで表示