summaryrefslogtreecommitdiff
path: root/src/blowfish.c
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2010-06-02 20:32:23 +0200
committerBram Moolenaar <Bram@vim.org>2010-06-02 20:32:23 +0200
commitbbd6afe03e138886f70989f31be110726ca077d8 (patch)
tree893d4513e6ff7b75a4f3bdeddc9867ada427a0a9 /src/blowfish.c
parent04c9bafa7136564e3059d493dffa84a4c9b6dfb7 (diff)
downloadvim-git-bbd6afe03e138886f70989f31be110726ca077d8.tar.gz
Optimize the blowfish crypt/decrypt code a bit more.
Diffstat (limited to 'src/blowfish.c')
-rw-r--r--src/blowfish.c108
1 files changed, 81 insertions, 27 deletions
diff --git a/src/blowfish.c b/src/blowfish.c
index f88cbfdea..1ab8eca8d 100644
--- a/src/blowfish.c
+++ b/src/blowfish.c
@@ -317,17 +317,17 @@ static UINT32_T sbi[4][256] = {
#define F1(i) \
xl ^= pax[i]; \
- xr ^= ((sbx[0][xl>>24] + \
- sbx[1][(xl&0xFF0000)>>16]) ^ \
- sbx[2][(xl&0xFF00)>>8]) + \
- sbx[3][xl&0xFF];
+ xr ^= ((sbx[0][xl >> 24] + \
+ sbx[1][(xl & 0xFF0000) >> 16]) ^ \
+ sbx[2][(xl & 0xFF00) >> 8]) + \
+ sbx[3][xl & 0xFF];
#define F2(i) \
xr ^= pax[i]; \
- xl ^= ((sbx[0][xr>>24] + \
- sbx[1][(xr&0xFF0000)>>16]) ^ \
- sbx[2][(xr&0xFF00)>>8]) + \
- sbx[3][xr&0xFF];
+ xl ^= ((sbx[0][xr >> 24] + \
+ sbx[1][(xr & 0xFF0000) >> 16]) ^ \
+ sbx[2][(xr & 0xFF00) >> 8]) + \
+ sbx[3][xr & 0xFF];
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(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 */
@@ -373,7 +377,8 @@ bf_d_block(p_xl, p_xr)
bf_e_cblock(block)
char_u *block;
{
- block8 bk;
+ 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)
}
}
+#define BF_OFB_UPDATE(c) { \
+ ofb_buffer[update_offset] ^= (char_u)c; \
+ if (++update_offset == BF_OFB_LEN) \
+ update_offset = 0; \
+}
+
+#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; \
+}
+
+/*
+ * Encrypt "from[len]" into "to[len]".
+ * "from" and "to" can be equal to encrypt in place.
+ */
void
-bf_ofb_update(c)
- int c;
+bf_crypt_encode(from, len, to)
+ char_u *from;
+ size_t len;
+ char_u *to;
{
- ofb_buffer[update_offset++] ^= (char_u)c;
- if (update_offset == BF_OFB_LEN)
- update_offset = 0;
+ 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;
+ }
}
- int
-bf_ranbyte()
+/*
+ * 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 */
{
- int b;
-
- 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;
+ char_u *p;
+
+ for (p = passwd; *p != NUL; ++p)
+ {
+ BF_OFB_UPDATE(*p);
+ }
}
/*