summaryrefslogtreecommitdiff
path: root/lib/libc/crypt/blowfish.c
diff options
context:
space:
mode:
authorNiels Provos <provos@cvs.openbsd.org>1998-08-10 18:41:00 +0000
committerNiels Provos <provos@cvs.openbsd.org>1998-08-10 18:41:00 +0000
commit66ec51ee42f8c61b0eb31938129a41dfee3c7bf7 (patch)
tree42227efe43ff3121dc695aaf2a13e35473f18601 /lib/libc/crypt/blowfish.c
parent7182e8c3cebdc3e69c012c12074dcc11f941924d (diff)
add ECB and CBC encryption for octet streams
Diffstat (limited to 'lib/libc/crypt/blowfish.c')
-rw-r--r--lib/libc/crypt/blowfish.c140
1 files changed, 139 insertions, 1 deletions
diff --git a/lib/libc/crypt/blowfish.c b/lib/libc/crypt/blowfish.c
index 5ffc634efa5..d9466556245 100644
--- a/lib/libc/crypt/blowfish.c
+++ b/lib/libc/crypt/blowfish.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: blowfish.c,v 1.8 1998/03/04 00:34:17 deraadt Exp $ */
+/* $OpenBSD: blowfish.c,v 1.9 1998/08/10 18:40:59 provos Exp $ */
/*
* Blowfish block cipher for OpenBSD
* Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
@@ -584,6 +584,144 @@ blf_dec(c, data, blocks)
d += 2;
}
}
+
+/* Repeating operations for little endian machines */
+
+#define BLF_BLK_ENC l = ntohl (*(u_int32_t *)data); \
+ r = ntohl (*(u_int32_t *)(data+4)); \
+ Blowfish_encipher(c, &l, &r); \
+ *(u_int32_t *)data = htonl (l); \
+ *(u_int32_t *)(data + 4) = htonl (r);
+
+#define BLF_BLK_DEC l = ntohl (*(u_int32_t *)data); \
+ r = ntohl (*(u_int32_t *)(data+4)); \
+ Blowfish_decipher(c, &l, &r); \
+ *(u_int32_t *)data = htonl (l); \
+ *(u_int32_t *)(data + 4) = htonl (r);
+
+
+#if __STDC__
+void
+blf_ecb_encrypt(blf_ctx *c, u_int8_t *data, u_int32_t len)
+#else
+void
+blf_ecb_encrypt(c, data, len)
+ blf_ctx *c;
+ u_int8_t *data;
+ u_int32_t len;
+#endif
+{
+#if BYTE_ORDER == LITTLE_ENDIAN
+ u_int32_t l, r;
+#endif
+ u_int32_t i;
+
+ for (i = 0; i < len; i += 8) {
+#if BYTE_ORDER == LITTLE_ENDIAN
+ BLF_BLK_ENC;
+#else
+ Blowfish_encipher(c, data, data + 4);
+#endif
+ data += 8;
+ }
+}
+
+#if __STDC__
+void
+blf_ecb_decrypt(blf_ctx *c, u_int8_t *data, u_int32_t len)
+#else
+void
+blf_ecb_decrypt(c, data, len)
+ blf_ctx *c;
+ u_int8_t *data;
+ u_int32_t len;
+#endif
+{
+#if BYTE_ORDER == LITTLE_ENDIAN
+ u_int32_t l, r;
+#endif
+ u_int32_t i;
+
+ for (i = 0; i < len; i += 8) {
+#if BYTE_ORDER == LITTLE_ENDIAN
+ BLF_BLK_DEC;
+#else
+ Blowfish_decipher(c, data, data + 4);
+#endif
+ 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
+{
+#if BYTE_ORDER == LITTLE_ENDIAN
+ u_int32_t l, r;
+#endif
+ u_int32_t i;
+
+ for (i = 0; i < len; i += 8) {
+ *(u_int32_t *)data ^= *(u_int32_t *)iv;
+ *(u_int32_t *)(data + 4) ^= *(u_int32_t *)(iv + 4);
+#if BYTE_ORDER == LITTLE_ENDIAN
+ BLF_BLK_ENC;
+#else
+ Blowfish_encipher(c, data, data + 4);
+#endif
+ 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
+{
+#if BYTE_ORDER == LITTLE_ENDIAN
+ u_int32_t l, r;
+#endif
+ u_int8_t *iv;
+ u_int32_t i;
+
+ iv = data + len - 16;
+ data = data + len - 8;
+ for (i = len - 8; i >= 8; i -= 8) {
+#if BYTE_ORDER == LITTLE_ENDIAN
+ BLF_BLK_DEC;
+#else
+ Blowfish_decipher(c, data, data + 4);
+#endif
+ *(u_int32_t *)data ^= *(u_int32_t *)iv;
+ *(u_int32_t *)(data + 4) ^= *(u_int32_t *)(iv + 4);
+ iv = data;
+ data -= 8;
+ }
+#if BYTE_ORDER == LITTLE_ENDIAN
+ BLF_BLK_DEC;
+#else
+ Blowfish_decipher(c, data, data + 4);
+#endif
+ *(u_int32_t *)data ^= *(u_int32_t *)iva;
+ *(u_int32_t *)(data + 4) ^= *(u_int32_t *)(iva + 4);
+}
+
#if 0
void
report(u_int32_t data[], u_int16_t len)