1
0
forked from aniani/vim

Made crypt/decrypt faster.

This commit is contained in:
Bram Moolenaar
2010-06-01 23:37:39 +02:00
parent 8cd213c09a
commit 04c9bafa71
6 changed files with 89 additions and 69 deletions

View 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/

View 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;
}
/*

View 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. */

View File

@@ -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

View 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()
{
ush temp;
if (use_crypt_method > 0)
return bf_ranbyte();
temp = (ush)keys[2] | 2;
return (int)(((unsigned)(temp * (temp ^ 1)) >> 8) & 0xff);
#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
* 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
update_keys(c)
int c; /* byte of plain text */
crypt_encode(from, len, to)
char_u *from;
size_t len;
char_u *to;
{
if (use_crypt_method > 0)
bf_ofb_update(c);
size_t i;
int ztemp, t;
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
{
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 (i = 0; i < len; ++i)
{
ztemp = from[i];
t = bf_ranbyte();
bf_ofb_update(ztemp);
to[i] = t ^ ztemp;
}
}
/*
* Decrypt "ptr[len]" in place.
*/
void
crypt_decode(ptr, len)
char_u *ptr;
long len;
{
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
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++);
}
}

View 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));