diff options
author | Bram Moolenaar <Bram@vim.org> | 2010-06-01 23:37:39 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2010-06-01 23:37:39 +0200 |
commit | 04c9bafa7136564e3059d493dffa84a4c9b6dfb7 (patch) | |
tree | 6f000d5eca2d4494d97cb638308c7c95d471f3de | |
parent | 8cd213c09a3598834888d81deb45ff17e6654a86 (diff) |
Made crypt/decrypt faster.
-rw-r--r-- | runtime/doc/todo.txt | 6 | ||||
-rw-r--r-- | src/blowfish.c | 13 | ||||
-rw-r--r-- | src/fileio.c | 24 | ||||
-rw-r--r-- | src/macros.h | 15 | ||||
-rw-r--r-- | src/misc2.c | 92 | ||||
-rw-r--r-- | src/proto/misc2.pro | 4 |
6 files changed, 87 insertions, 67 deletions
diff --git a/runtime/doc/todo.txt b/runtime/doc/todo.txt index cad2e98621..41326a9bf1 100644 --- a/runtime/doc/todo.txt +++ b/runtime/doc/todo.txt @@ -1085,7 +1085,6 @@ Vim 7.3: - using NSIS 2.46: install on Windows 7 works, but no "Edit with Vim" menu. Use register_shell_extension()? (George Reilly, 2010 May 26) Ron's version: http://dev.ronware.org/p/vim/finfo?name=gvim.nsi -- Undo code: use union to store long in place of pointers? - Also crypt the swap file, each block separately. Change mf_write() and mf_read(). How to get b_p_key to these functions? Generate seed for each block, store in pointer block. Block 1 is not @@ -1095,8 +1094,11 @@ Vim 7.3: Verify recovery works. - Update for crypt code to use salt. (Mohsin May 30) Make the strengthen_key value configurable and store it in the header. -- Do profiling on sha256 code to find obvious bottlenecks. - Do profiling on crypt code to find obvious bottlenecks. + bf_ranbyte() and bf_ofb_init() are called for each byte, can they be done + inline somehow? + -> Add a function in blowfish.c to process an array, called once from + crypt_decode() and crypt_encode(). Patches to include: - Include conceal patch? http://vince.negri.googlepages.com/ diff --git a/src/blowfish.c b/src/blowfish.c index e449da38fc..f88cbfdeac 100644 --- a/src/blowfish.c +++ b/src/blowfish.c @@ -18,6 +18,7 @@ #define ARRAY_LENGTH(A) (sizeof(A)/sizeof(A[0])) #define BF_BLOCK 8 +#define BF_BLOCK_MASK 7 #define BF_OFB_LEN (8*(BF_BLOCK)) typedef union { @@ -563,14 +564,14 @@ bf_ofb_update(c) int bf_ranbyte() { - int current_byte = randbyte_offset++; - int current_block = (current_byte / BF_BLOCK) * BF_BLOCK; + int b; - if (randbyte_offset == BF_OFB_LEN) + if ((randbyte_offset & BF_BLOCK_MASK) == 0) + bf_e_cblock(&ofb_buffer[randbyte_offset]); + b = ofb_buffer[randbyte_offset]; + if (++randbyte_offset == BF_OFB_LEN) randbyte_offset = 0; - if ((current_byte % BF_BLOCK) == 0) - bf_e_cblock(&ofb_buffer[current_block]); - return ofb_buffer[current_byte]; + return b; } /* diff --git a/src/fileio.c b/src/fileio.c index 4a9a7f10e1..889e4ff449 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -1426,8 +1426,7 @@ retry: * Decrypt the read bytes. */ if (cryptkey != NULL && size > 0) - for (p = ptr; p < ptr + size; ++p) - ZDECODE(*p); + crypt_decode(ptr, size); #endif } skip_read = FALSE; @@ -3004,7 +3003,6 @@ fwrite_crypt(buf, ptr, len, fp) { char_u *copy; char_u small_buf[100]; - int ztemp, t; size_t i; if (*buf->b_p_key == NUL) @@ -3017,11 +3015,7 @@ fwrite_crypt(buf, ptr, len, fp) if (copy == NULL) return 0; } - for (i = 0; i < len; ++i) - { - ztemp = ptr[i]; - copy[i] = ZENCODE(ztemp, t); - } + crypt_encode(ptr, len, copy); i = fwrite(copy, len, (size_t)1, fp); if (copy != small_buf) vim_free(copy); @@ -3039,12 +3033,10 @@ read_string_decrypt(buf, fd, len) int len; { char_u *ptr; - char_u *p; ptr = read_string(fd, len); if (ptr != NULL || *buf->b_p_key != NUL) - for (p = ptr; p < ptr + len; ++p) - ZDECODE(*p); + crypt_decode(ptr, len); return ptr; } @@ -5678,15 +5670,7 @@ buf_write_bytes(ip) #ifdef FEAT_CRYPT if (flags & FIO_ENCRYPTED) /* encrypt the data */ - { - int ztemp, t, i; - - for (i = 0; i < len; i++) - { - ztemp = buf[i]; - buf[i] = ZENCODE(ztemp, t); - } - } + crypt_encode(buf, len, buf); #endif /* Repeat the write(), it may be interrupted by a signal. */ diff --git a/src/macros.h b/src/macros.h index 51e4dd423e..7fc5df0ce4 100644 --- a/src/macros.h +++ b/src/macros.h @@ -227,21 +227,6 @@ # endif #endif -/* - * Encryption macros. Mohsin Ahmed, mosh@sasi.com 98-09-24 - * Based on zip/crypt sources. - */ - -#ifdef FEAT_CRYPT - -/* encode byte c, using temp t. Warning: c must not have side effects. */ -# define ZENCODE(c, t) (t = decrypt_byte(), update_keys(c), t^(c)) - -/* decode byte c in place */ -# define ZDECODE(c) update_keys(c ^= decrypt_byte()) - -#endif - #ifdef STARTUPTIME # define TIME_MSG(s) { if (time_fd != NULL) time_msg(s, NULL); } #else diff --git a/src/misc2.c b/src/misc2.c index 88b12aa0d3..7a0a0512e0 100644 --- a/src/misc2.c +++ b/src/misc2.c @@ -3724,39 +3724,81 @@ make_crc_tab() #define CRC32(c, b) (crc_32_tab[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8)) - static ulg keys[3]; /* keys defining the pseudo-random sequence */ /* - * Return the next byte in the pseudo-random sequence + * Return the next byte in the pseudo-random sequence. */ - int -decrypt_byte() +#define DECRYPT_BYTE_ZIP(t) { \ + ush temp; \ + \ + temp = (ush)keys[2] | 2; \ + t = (int)(((unsigned)(temp * (temp ^ 1)) >> 8) & 0xff); \ +} + +/* + * Update the encryption keys with the next byte of plain text. + */ +#define UPDATE_KEYS_ZIP(c) { \ + keys[0] = CRC32(keys[0], (c)); \ + keys[1] += keys[0] & 0xff; \ + keys[1] = keys[1] * 134775813L + 1; \ + keys[2] = CRC32(keys[2], (int)(keys[1] >> 24)); \ +} + +/* + * Encrypt "from[len]" into "to[len]". + * "from" and "to" can be equal to encrypt in place. + */ + void +crypt_encode(from, len, to) + char_u *from; + size_t len; + char_u *to; { - ush temp; + size_t i; + int ztemp, t; - if (use_crypt_method > 0) - return bf_ranbyte(); - temp = (ush)keys[2] | 2; - return (int)(((unsigned)(temp * (temp ^ 1)) >> 8) & 0xff); + if (use_crypt_method == 0) + for (i = 0; i < len; ++i) + { + ztemp = from[i]; + DECRYPT_BYTE_ZIP(t); + UPDATE_KEYS_ZIP(ztemp); + to[i] = t ^ ztemp; + } + else + for (i = 0; i < len; ++i) + { + ztemp = from[i]; + t = bf_ranbyte(); + bf_ofb_update(ztemp); + to[i] = t ^ ztemp; + } } /* - * Update the encryption keys with the next byte of plain text + * Decrypt "ptr[len]" in place. */ void -update_keys(c) - int c; /* byte of plain text */ +crypt_decode(ptr, len) + char_u *ptr; + long len; { - if (use_crypt_method > 0) - bf_ofb_update(c); + char_u *p; + + if (use_crypt_method == 0) + for (p = ptr; p < ptr + len; ++p) + { + ush temp; + + temp = (ush)keys[2] | 2; + temp = (int)(((unsigned)(temp * (temp ^ 1)) >> 8) & 0xff); + UPDATE_KEYS_ZIP(*p ^= temp); + } else - { - keys[0] = CRC32(keys[0], c); - keys[1] += keys[0] & 0xff; - keys[1] = keys[1] * 134775813L + 1; - keys[2] = CRC32(keys[2], (int)(keys[1] >> 24)); - } + for (p = ptr; p < ptr + len; ++p) + bf_ofb_update(*p ^= bf_ranbyte()); } /* @@ -3774,8 +3816,14 @@ crypt_init_keys(passwd) keys[0] = 305419896L; keys[1] = 591751049L; keys[2] = 878082192L; - while (*passwd != '\0') - update_keys((int)*passwd++); + if (use_crypt_method == 0) + while (*passwd != '\0') + { + UPDATE_KEYS_ZIP((int)*passwd++); + } + else + while (*passwd != '\0') + bf_ofb_update((int)*passwd++); } } diff --git a/src/proto/misc2.pro b/src/proto/misc2.pro index a688147429..6b292bb81f 100644 --- a/src/proto/misc2.pro +++ b/src/proto/misc2.pro @@ -80,8 +80,8 @@ int illegal_slash __ARGS((char *name)); char_u *parse_shape_opt __ARGS((int what)); int get_shape_idx __ARGS((int mouse)); void update_mouseshape __ARGS((int shape_idx)); -int decrypt_byte __ARGS((void)); -void update_keys __ARGS((int c)); +void crypt_encode __ARGS((char_u *from, size_t len, char_u *to)); +void crypt_decode __ARGS((char_u *ptr, long len)); void crypt_init_keys __ARGS((char_u *passwd)); void free_crypt_key __ARGS((char_u *key)); char_u *get_crypt_key __ARGS((int store, int twice)); |