summaryrefslogtreecommitdiff
path: root/sys/crypto/blf.c
diff options
context:
space:
mode:
authorNiels Provos <provos@cvs.openbsd.org>2000-06-17 23:36:23 +0000
committerNiels Provos <provos@cvs.openbsd.org>2000-06-17 23:36:23 +0000
commitc80a3f0602a591c636d046d07b57451942e79d11 (patch)
treea1e7ecba07c12f34d8404d13341f2fe09a9a5edb /sys/crypto/blf.c
parent0d5811a2c2cc1f42ad20e4a56ca42094ee03b48e (diff)
cbc mode
Diffstat (limited to 'sys/crypto/blf.c')
-rw-r--r--sys/crypto/blf.c98
1 files changed, 97 insertions, 1 deletions
diff --git a/sys/crypto/blf.c b/sys/crypto/blf.c
index 4e7be1bc592..e321a46c4c1 100644
--- a/sys/crypto/blf.c
+++ b/sys/crypto/blf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: blf.c,v 1.2 2000/06/06 06:49:46 deraadt Exp $ */
+/* $OpenBSD: blf.c,v 1.3 2000/06/17 23:36:22 provos Exp $ */
/*
* Blowfish block cipher for OpenBSD
@@ -650,3 +650,99 @@ blf_ecb_decrypt(c, data, len)
data += 8;
}
}
+
+#if __STDC__
+void
+blf_cbc_encrypt(blf_ctx *c, u_int8_t *iv, u_int8_t *data, u_int32_t len)
+#else
+void
+blf_cbc_encrypt(c, iv, data, len)
+ blf_ctx *c;
+ u_int8_t *iv;
+ u_int8_t *data;
+ u_int32_t len;
+#endif
+{
+ u_int32_t l, r, d[2];
+ u_int32_t i, j;
+
+ for (i = 0; i < len; i += 8) {
+ for (j = 0; j < 8; j++)
+ data[j] ^= iv[j];
+ l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
+ r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
+ d[0] = l;
+ d[1] = r;
+ Blowfish_encipher(c, d);
+ l = d[0];
+ r = d[1];
+ data[0] = l >> 24 & 0xff;
+ data[1] = l >> 16 & 0xff;
+ data[2] = l >> 8 & 0xff;
+ data[3] = l & 0xff;
+ data[4] = r >> 24 & 0xff;
+ data[5] = r >> 16 & 0xff;
+ data[6] = r >> 8 & 0xff;
+ data[7] = r & 0xff;
+ iv = data;
+ data += 8;
+ }
+}
+
+#if __STDC__
+void
+blf_cbc_decrypt(blf_ctx *c, u_int8_t *iva, u_int8_t *data, u_int32_t len)
+#else
+void
+blf_cbc_decrypt(c, iva, data, len)
+ blf_ctx *c;
+ u_int8_t *iva;
+ u_int8_t *data;
+ u_int32_t len;
+#endif
+{
+ u_int32_t l, r, d[2];
+ u_int8_t *iv;
+ u_int32_t i, j;
+
+ iv = data + len - 16;
+ data = data + len - 8;
+ for (i = len - 8; i >= 8; i -= 8) {
+ l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
+ r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
+ d[0] = l;
+ d[1] = r;
+ Blowfish_decipher(c, d);
+ l = d[0];
+ r = d[1];
+ data[0] = l >> 24 & 0xff;
+ data[1] = l >> 16 & 0xff;
+ data[2] = l >> 8 & 0xff;
+ data[3] = l & 0xff;
+ data[4] = r >> 24 & 0xff;
+ data[5] = r >> 16 & 0xff;
+ data[6] = r >> 8 & 0xff;
+ data[7] = r & 0xff;
+ for (j = 0; j < 8; j++)
+ data[j] ^= iv[j];
+ iv -= 8;
+ data -= 8;
+ }
+ l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
+ r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
+ d[0] = l;
+ d[1] = r;
+ Blowfish_decipher(c, d);
+ l = d[0];
+ r = d[1];
+ data[0] = l >> 24 & 0xff;
+ data[1] = l >> 16 & 0xff;
+ data[2] = l >> 8 & 0xff;
+ data[3] = l & 0xff;
+ data[4] = r >> 24 & 0xff;
+ data[5] = r >> 16 & 0xff;
+ data[6] = r >> 8 & 0xff;
+ data[7] = r & 0xff;
+ for (j = 0; j < 8; j++)
+ data[j] ^= iva[j];
+}