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: 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. - using NSIS 2.46: install on Windows 7 works, but no "Edit with Vim" menu.
Use register_shell_extension()? (George Reilly, 2010 May 26) Use register_shell_extension()? (George Reilly, 2010 May 26)
Ron's version: http://dev.ronware.org/p/vim/finfo?name=gvim.nsi Ron's version: http://dev.ronware.org/p/vim/finfo?name=gvim.nsi
@@ -1094,11 +1096,6 @@ Vim 7.3:
Verify recovery works. Verify recovery works.
- Update for crypt code to use salt. (Mohsin May 30) - Update for crypt code to use salt. (Mohsin May 30)
Make the strengthen_key value configurable and store it in the header. 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: Patches to include:
- Include conceal patch? - Include conceal patch?
http://vince.negri.googlepages.com/ http://vince.negri.googlepages.com/

View File

@@ -317,17 +317,17 @@ static UINT32_T sbi[4][256] = {
#define F1(i) \ #define F1(i) \
xl ^= pax[i]; \ xl ^= pax[i]; \
xr ^= ((sbx[0][xl>>24] + \ xr ^= ((sbx[0][xl >> 24] + \
sbx[1][(xl&0xFF0000)>>16]) ^ \ sbx[1][(xl & 0xFF0000) >> 16]) ^ \
sbx[2][(xl&0xFF00)>>8]) + \ sbx[2][(xl & 0xFF00) >> 8]) + \
sbx[3][xl&0xFF]; sbx[3][xl & 0xFF];
#define F2(i) \ #define F2(i) \
xr ^= pax[i]; \ xr ^= pax[i]; \
xl ^= ((sbx[0][xr>>24] + \ xl ^= ((sbx[0][xr >> 24] + \
sbx[1][(xr&0xFF0000)>>16]) ^ \ sbx[1][(xr & 0xFF0000) >> 16]) ^ \
sbx[2][(xr&0xFF00)>>8]) + \ sbx[2][(xr & 0xFF00) >> 8]) + \
sbx[3][xr&0xFF]; sbx[3][xr & 0xFF];
static void static void
@@ -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(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) F1(8) F2(9) F1(10) F2(11) F1(12) F2(13) F1(14) F2(15)
xl ^= pax[16]; xr ^= pax[17]; xl ^= pax[16];
temp = xl; xl = xr; xr = temp; xr ^= pax[17];
*p_xl = xl; *p_xr = xr; temp = xl;
xl = xr;
xr = temp;
*p_xl = xl;
*p_xr = xr;
} }
#if 0 /* not used */ #if 0 /* not used */
@@ -373,7 +377,8 @@ bf_d_block(p_xl, p_xr)
bf_e_cblock(block) bf_e_cblock(block)
char_u *block; char_u *block;
{ {
block8 bk; block8 bk;
memcpy(bk.uc, block, 8); memcpy(bk.uc, block, 8);
htonl2(bk.ul[0]); htonl2(bk.ul[0]);
htonl2(bk.ul[1]); htonl2(bk.ul[1]);
@@ -552,26 +557,75 @@ bf_ofb_init(iv, iv_len)
} }
} }
void #define BF_OFB_UPDATE(c) { \
bf_ofb_update(c) ofb_buffer[update_offset] ^= (char_u)c; \
int c; if (++update_offset == BF_OFB_LEN) \
{ update_offset = 0; \
ofb_buffer[update_offset++] ^= (char_u)c;
if (update_offset == BF_OFB_LEN)
update_offset = 0;
} }
int #define BF_RANBYTE(t) { \
bf_ranbyte() if ((randbyte_offset & BF_BLOCK_MASK) == 0) \
{ bf_e_cblock(&ofb_buffer[randbyte_offset]); \
int b; 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]); * Encrypt "from[len]" into "to[len]".
b = ofb_buffer[randbyte_offset]; * "from" and "to" can be equal to encrypt in place.
if (++randbyte_offset == BF_OFB_LEN) */
randbyte_offset = 0; void
return b; 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]); ackNum = atoi(&cmd[4]);
vim_snprintf(buf, sizeof(buf), vim_snprintf(buf, sizeof(buf),
NOCATGETS("ack %d\n"), ackNum); NOCATGETS("ack %d\n"), ackNum);
write(sd, buf, strlen(buf)); (void)write(sd, buf, strlen(buf));
} else if (strncmp(cmd, } else if (strncmp(cmd,
NOCATGETS("addMarkType "), 12) == 0) { NOCATGETS("addMarkType "), 12) == 0) {
int idx; int idx;
@@ -280,7 +280,7 @@ messageFromEserve(XtPointer clientData UNUSED,
vim_snprintf(buf, sizeof(buf), vim_snprintf(buf, sizeof(buf),
NOCATGETS("markLine %s %d %d\n"), NOCATGETS("markLine %s %d %d\n"),
file, markid, line); file, markid, line);
write(sd, buf, strlen(buf)); (void)write(sd, buf, strlen(buf));
} else if (cmd[1] == 'o' && cmd[4] == 'L' && } else if (cmd[1] == 'o' && cmd[4] == 'L' &&
strncmp(cmd, NOCATGETS("gotoLine "), 9) == 0) { strncmp(cmd, NOCATGETS("gotoLine "), 9) == 0) {
char *file; char *file;
@@ -729,10 +729,10 @@ void workshop_connect(XtAppContext context)
workshop_get_editor_name(), workshop_get_editor_name(),
PROTOCOL_VERSION, PROTOCOL_VERSION,
workshop_get_editor_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")); vim_snprintf(buf, sizeof(buf), NOCATGETS("ack 1\n"));
write(sd, buf, strlen(buf)); (void)write(sd, buf, strlen(buf));
} }
void workshop_disconnect() void workshop_disconnect()
@@ -1059,7 +1059,7 @@ void workshop_file_closed(char *filename)
char buffer[2*MAXPATHLEN]; char buffer[2*MAXPATHLEN];
vim_snprintf(buffer, sizeof(buffer), vim_snprintf(buffer, sizeof(buffer),
NOCATGETS("deletedFile %s\n"), filename); NOCATGETS("deletedFile %s\n"), filename);
write(sd, buffer, strlen(buffer)); (void)write(sd, buffer, strlen(buffer));
} }
#endif #endif
@@ -1068,7 +1068,7 @@ void workshop_file_closed_lineno(char *filename, int lineno)
char buffer[2*MAXPATHLEN]; char buffer[2*MAXPATHLEN];
vim_snprintf(buffer, sizeof(buffer), vim_snprintf(buffer, sizeof(buffer),
NOCATGETS("deletedFile %s %d\n"), filename, lineno); 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) void workshop_file_opened(char *filename, int readOnly)
@@ -1076,7 +1076,7 @@ void workshop_file_opened(char *filename, int readOnly)
char buffer[2*MAXPATHLEN]; char buffer[2*MAXPATHLEN];
vim_snprintf(buffer, sizeof(buffer), vim_snprintf(buffer, sizeof(buffer),
NOCATGETS("loadedFile %s %d\n"), filename, readOnly); 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]; char buffer[2*MAXPATHLEN];
vim_snprintf(buffer, sizeof(buffer), vim_snprintf(buffer, sizeof(buffer),
NOCATGETS("savedFile %s\n"), filename); 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 /* Let editor report any moved marks that the eserve client
* should deal with (for example, moving location-based breakpoints) */ * should deal with (for example, moving location-based breakpoints) */
@@ -1098,7 +1098,7 @@ void workshop_file_modified(char *filename)
char buffer[2*MAXPATHLEN]; char buffer[2*MAXPATHLEN];
vim_snprintf(buffer, sizeof(buffer), vim_snprintf(buffer, sizeof(buffer),
NOCATGETS("modifiedFile %s\n"), filename); 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) 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]; char buffer[2*MAXPATHLEN];
vim_snprintf(buffer, sizeof(buffer), vim_snprintf(buffer, sizeof(buffer),
NOCATGETS("moveMark %s %d %d\n"), filename, markId, newLineno); NOCATGETS("moveMark %s %d %d\n"), filename, markId, newLineno);
write(sd, buffer, strlen(buffer)); (void)write(sd, buffer, strlen(buffer));
} }
#endif #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), vim_snprintf(buffer, sizeof(buffer),
NOCATGETS("frameAt %d %d %d %d\n"), NOCATGETS("frameAt %d %d %d %d\n"),
new_x, new_y, new_w, new_h); 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, selEndLine, selEndCol,
selLength, selLength,
selection); selection);
write(sd, buf, strlen(buf)); (void)write(sd, buf, strlen(buf));
if (*selection) { if (*selection) {
free(selection); free(selection);
} }
@@ -1190,7 +1190,7 @@ void workshop_perform_verb(char *verb, void *clientData)
#if defined(NOHANDS_SUPPORT_FUNCTIONS) || defined(FEAT_BEVAL) #if defined(NOHANDS_SUPPORT_FUNCTIONS) || defined(FEAT_BEVAL)
void workshop_send_message(char *buf) void workshop_send_message(char *buf)
{ {
write(sd, buf, strlen(buf)); (void)write(sd, buf, strlen(buf));
} }
#endif #endif

View File

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

View File

@@ -1,7 +1,8 @@
/* blowfish.c */ /* blowfish.c */
void bf_key_init __ARGS((char_u *password)); void bf_key_init __ARGS((char_u *password));
void bf_ofb_init __ARGS((char_u *iv, int iv_len)); void bf_ofb_init __ARGS((char_u *iv, int iv_len));
void bf_ofb_update __ARGS((int c)); void bf_crypt_encode __ARGS((char_u *from, size_t len, char_u *to));
int bf_ranbyte __ARGS((void)); 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)); int blowfish_self_test __ARGS((void));
/* vim: set ft=c : */ /* vim: set ft=c : */

View File

@@ -825,7 +825,7 @@ serialize_header(fp, buf, hash)
header = prepare_crypt_write(buf, &header_len); header = prepare_crypt_write(buf, &header_len);
if (header == NULL) if (header == NULL)
return FAIL; 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); vim_free(header);
if (len != 1) if (len != 1)
return FAIL; return FAIL;

View File

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