1
0
forked from aniani/vim

Optimize the blowfish crypt/decrypt code a bit more.

This commit is contained in:
Bram Moolenaar
2010-06-02 20:32:23 +02:00
parent 04c9bafa71
commit bbd6afe03e
7 changed files with 118 additions and 70 deletions

View File

@@ -1082,6 +1082,8 @@ restored. (Luc St-Louis)
Vim 7.3:
- undofile: keep markers where the file was written/read, so that it's easy to
go back to a saved version of the file ":earlier 1file"?
- 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
@@ -1094,11 +1096,6 @@ 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 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

@@ -339,9 +339,13 @@ bf_e_block(p_xl, p_xr)
F1(0) F2(1) F1(2) F2(3) F1(4) F2(5) F1(6) F2(7)
F1(8) F2(9) F1(10) F2(11) F1(12) F2(13) F1(14) F2(15)
xl ^= pax[16]; xr ^= pax[17];
temp = xl; xl = xr; xr = temp;
*p_xl = xl; *p_xr = xr;
xl ^= pax[16];
xr ^= pax[17];
temp = xl;
xl = xr;
xr = temp;
*p_xl = xl;
*p_xr = xr;
}
#if 0 /* not used */
@@ -374,6 +378,7 @@ bf_e_cblock(block)
char_u *block;
{
block8 bk;
memcpy(bk.uc, block, 8);
htonl2(bk.ul[0]);
htonl2(bk.ul[1]);
@@ -552,26 +557,75 @@ bf_ofb_init(iv, iv_len)
}
}
void
bf_ofb_update(c)
int c;
{
ofb_buffer[update_offset++] ^= (char_u)c;
if (update_offset == BF_OFB_LEN)
update_offset = 0;
#define BF_OFB_UPDATE(c) { \
ofb_buffer[update_offset] ^= (char_u)c; \
if (++update_offset == BF_OFB_LEN) \
update_offset = 0; \
}
int
bf_ranbyte()
{
int b;
#define BF_RANBYTE(t) { \
if ((randbyte_offset & BF_BLOCK_MASK) == 0) \
bf_e_cblock(&ofb_buffer[randbyte_offset]); \
t = ofb_buffer[randbyte_offset]; \
if (++randbyte_offset == BF_OFB_LEN) \
randbyte_offset = 0; \
}
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;
return b;
/*
* Encrypt "from[len]" into "to[len]".
* "from" and "to" can be equal to encrypt in place.
*/
void
bf_crypt_encode(from, len, to)
char_u *from;
size_t len;
char_u *to;
{
size_t i;
int ztemp, t;
for (i = 0; i < len; ++i)
{
ztemp = from[i];
BF_RANBYTE(t);
BF_OFB_UPDATE(ztemp);
to[i] = t ^ ztemp;
}
}
/*
* Decrypt "ptr[len]" in place.
*/
void
bf_crypt_decode(ptr, len)
char_u *ptr;
long len;
{
char_u *p;
int t;
for (p = ptr; p < ptr + len; ++p)
{
BF_RANBYTE(t);
*p ^= t;
BF_OFB_UPDATE(*p);
}
}
/*
* Initialize the encryption keys and the random header according to
* the given password.
*/
void
bf_crypt_init_keys(passwd)
char_u *passwd; /* password string with which to modify keys */
{
char_u *p;
for (p = passwd; *p != NUL; ++p)
{
BF_OFB_UPDATE(*p);
}
}
/*

View File

@@ -183,7 +183,7 @@ messageFromEserve(XtPointer clientData UNUSED,
ackNum = atoi(&cmd[4]);
vim_snprintf(buf, sizeof(buf),
NOCATGETS("ack %d\n"), ackNum);
write(sd, buf, strlen(buf));
(void)write(sd, buf, strlen(buf));
} else if (strncmp(cmd,
NOCATGETS("addMarkType "), 12) == 0) {
int idx;
@@ -280,7 +280,7 @@ messageFromEserve(XtPointer clientData UNUSED,
vim_snprintf(buf, sizeof(buf),
NOCATGETS("markLine %s %d %d\n"),
file, markid, line);
write(sd, buf, strlen(buf));
(void)write(sd, buf, strlen(buf));
} else if (cmd[1] == 'o' && cmd[4] == 'L' &&
strncmp(cmd, NOCATGETS("gotoLine "), 9) == 0) {
char *file;
@@ -729,10 +729,10 @@ void workshop_connect(XtAppContext context)
workshop_get_editor_name(),
PROTOCOL_VERSION,
workshop_get_editor_version());
write(sd, buf, strlen(buf));
(void)write(sd, buf, strlen(buf));
vim_snprintf(buf, sizeof(buf), NOCATGETS("ack 1\n"));
write(sd, buf, strlen(buf));
(void)write(sd, buf, strlen(buf));
}
void workshop_disconnect()
@@ -1059,7 +1059,7 @@ void workshop_file_closed(char *filename)
char buffer[2*MAXPATHLEN];
vim_snprintf(buffer, sizeof(buffer),
NOCATGETS("deletedFile %s\n"), filename);
write(sd, buffer, strlen(buffer));
(void)write(sd, buffer, strlen(buffer));
}
#endif
@@ -1068,7 +1068,7 @@ void workshop_file_closed_lineno(char *filename, int lineno)
char buffer[2*MAXPATHLEN];
vim_snprintf(buffer, sizeof(buffer),
NOCATGETS("deletedFile %s %d\n"), filename, lineno);
write(sd, buffer, strlen(buffer));
(void)write(sd, buffer, strlen(buffer));
}
void workshop_file_opened(char *filename, int readOnly)
@@ -1076,7 +1076,7 @@ void workshop_file_opened(char *filename, int readOnly)
char buffer[2*MAXPATHLEN];
vim_snprintf(buffer, sizeof(buffer),
NOCATGETS("loadedFile %s %d\n"), filename, readOnly);
write(sd, buffer, strlen(buffer));
(void)write(sd, buffer, strlen(buffer));
}
@@ -1085,7 +1085,7 @@ void workshop_file_saved(char *filename)
char buffer[2*MAXPATHLEN];
vim_snprintf(buffer, sizeof(buffer),
NOCATGETS("savedFile %s\n"), filename);
write(sd, buffer, strlen(buffer));
(void)write(sd, buffer, strlen(buffer));
/* Let editor report any moved marks that the eserve client
* should deal with (for example, moving location-based breakpoints) */
@@ -1098,7 +1098,7 @@ void workshop_file_modified(char *filename)
char buffer[2*MAXPATHLEN];
vim_snprintf(buffer, sizeof(buffer),
NOCATGETS("modifiedFile %s\n"), filename);
write(sd, buffer, strlen(buffer));
(void)write(sd, buffer, strlen(buffer));
}
void workshop_move_mark(char *filename, int markId, int newLineno)
@@ -1106,7 +1106,7 @@ void workshop_move_mark(char *filename, int markId, int newLineno)
char buffer[2*MAXPATHLEN];
vim_snprintf(buffer, sizeof(buffer),
NOCATGETS("moveMark %s %d %d\n"), filename, markId, newLineno);
write(sd, buffer, strlen(buffer));
(void)write(sd, buffer, strlen(buffer));
}
#endif
@@ -1119,7 +1119,7 @@ void workshop_frame_moved(int new_x, int new_y, int new_w, int new_h)
vim_snprintf(buffer, sizeof(buffer),
NOCATGETS("frameAt %d %d %d %d\n"),
new_x, new_y, new_w, new_h);
write(sd, buffer, strlen(buffer));
(void)write(sd, buffer, strlen(buffer));
}
}
@@ -1179,7 +1179,7 @@ void workshop_perform_verb(char *verb, void *clientData)
selEndLine, selEndCol,
selLength,
selection);
write(sd, buf, strlen(buf));
(void)write(sd, buf, strlen(buf));
if (*selection) {
free(selection);
}
@@ -1190,7 +1190,7 @@ void workshop_perform_verb(char *verb, void *clientData)
#if defined(NOHANDS_SUPPORT_FUNCTIONS) || defined(FEAT_BEVAL)
void workshop_send_message(char *buf)
{
write(sd, buf, strlen(buf));
(void)write(sd, buf, strlen(buf));
}
#endif

View File

@@ -3768,13 +3768,7 @@ crypt_encode(from, len, to)
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;
}
bf_crypt_encode(from, len, to);
}
/*
@@ -3797,8 +3791,7 @@ crypt_decode(ptr, len)
UPDATE_KEYS_ZIP(*p ^= temp);
}
else
for (p = ptr; p < ptr + len; ++p)
bf_ofb_update(*p ^= bf_ranbyte());
bf_crypt_decode(ptr, len);
}
/*
@@ -3812,18 +3805,21 @@ crypt_init_keys(passwd)
{
if (passwd != NULL && *passwd != NUL)
{
if (use_crypt_method == 0)
{
char_u *p;
make_crc_tab();
keys[0] = 305419896L;
keys[1] = 591751049L;
keys[2] = 878082192L;
if (use_crypt_method == 0)
while (*passwd != '\0')
for (p = passwd; *p!= NUL; ++p)
{
UPDATE_KEYS_ZIP((int)*passwd++);
UPDATE_KEYS_ZIP((int)*p);
}
}
else
while (*passwd != '\0')
bf_ofb_update((int)*passwd++);
bf_crypt_init_keys(passwd);
}
}
@@ -6320,9 +6316,9 @@ put_time(fd, the_time)
else
{
#if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4
c = wtime >> (i * 8);
c = (int)(wtime >> (i * 8));
#else
c = (long_u)wtime >> (i * 8);
c = (int)((long_u)wtime >> (i * 8));
#endif
putc(c, fd);
}

View File

@@ -1,7 +1,8 @@
/* blowfish.c */
void bf_key_init __ARGS((char_u *password));
void bf_ofb_init __ARGS((char_u *iv, int iv_len));
void bf_ofb_update __ARGS((int c));
int bf_ranbyte __ARGS((void));
void bf_crypt_encode __ARGS((char_u *from, size_t len, char_u *to));
void bf_crypt_decode __ARGS((char_u *ptr, long len));
void bf_crypt_init_keys __ARGS((char_u *passwd));
int blowfish_self_test __ARGS((void));
/* vim: set ft=c : */

View File

@@ -825,7 +825,7 @@ serialize_header(fp, buf, hash)
header = prepare_crypt_write(buf, &header_len);
if (header == NULL)
return FAIL;
len = fwrite(header, (size_t)header_len, (size_t)1, fp);
len = (int)fwrite(header, (size_t)header_len, (size_t)1, fp);
vim_free(header);
if (len != 1)
return FAIL;

View File

@@ -1826,7 +1826,7 @@ findYourself(
else if (*argv0 == '.' || strchr(argv0, '/'))
{
runpath = (char *) malloc(MAXPATHLEN);
getcwd(runpath, MAXPATHLEN);
(void)getcwd(runpath, MAXPATHLEN);
strcat(runpath, "/");
strcat(runpath, argv0);
}