]> granicus.if.org Git - vim/commitdiff
Made crypt/decrypt faster.
authorBram Moolenaar <Bram@vim.org>
Tue, 1 Jun 2010 21:37:39 +0000 (23:37 +0200)
committerBram Moolenaar <Bram@vim.org>
Tue, 1 Jun 2010 21:37:39 +0000 (23:37 +0200)
runtime/doc/todo.txt
src/blowfish.c
src/fileio.c
src/macros.h
src/misc2.c
src/proto/misc2.pro

index cad2e986219e4c882441a7104b7a9d9a3032679e..41326a9bf1123d650df116b27ed95a4d0a1c9977 100644 (file)
@@ -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/
index e449da38fcd0ce609f6ecc49b57797774952a9a3..f88cbfdeac6089a158fa1aee3f1a7232ffecd7fd 100644 (file)
@@ -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;
 }
 
 /*
index 4a9a7f10e1f92a0216835266f5f8d1d3ef83dad7..889e4ff4494414ad5e9ac065c67e61183ebd7555 100644 (file)
@@ -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. */
index 51e4dd423e9dcd889ddd70dfa0112de3df4b4b06..7fc5df0ce40c9b2bec3d36ccf30c3e61a0c3579a 100644 (file)
 # 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
index 88b12aa0d3facba93c3d07cb9d94e80914066262..7a0a0512e01ef79a1e1cfa3cd8371a238c07cfbc 100644 (file)
@@ -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++);
     }
 }
 
index a6881474297384b84897d5aeadbe2bda7f8bb64c..6b292bb81f644d9d5219aab2ac0162dc5bcab868 100644 (file)
@@ -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));