#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/systm.h>
#include <crypto/blowfish/blowfish.h>
#include <crypto/blowfish/bf_locl.h>
#define panic(x) {printf(x); return;}
void BF_cbc_encrypt_m(m0, skip, length, key, iv, mode)
struct mbuf *m0;
int skip;
int length;
BF_KEY *key;
unsigned char *iv;
int mode;
{
u_int8_t inbuf[8], outbuf[8];
struct mbuf *m;
size_t off;
register BF_LONG tin0, tin1;
register BF_LONG tout0, tout1;
BF_LONG tin[2];
if (m0->m_pkthdr.len < skip) {
printf("mbuf length < skip\n");
return;
}
if (m0->m_pkthdr.len < length) {
printf("mbuf length < encrypt length\n");
return;
}
if (m0->m_pkthdr.len < skip + length) {
printf("mbuf length < skip + encrypt length\n");
return;
}
if (length % 8) {
printf("length is not multiple of 8\n");
return;
}
m = m0;
off = 0;
while (skip) {
if (!m)
panic("mbuf chain?\n");
if (m->m_len <= skip) {
skip -= m->m_len;
m = m->m_next;
off = 0;
} else {
off = skip;
skip = 0;
}
}
tin0 = tin1 = tout0 = tout1 = 0;
tin[0] = tin[1] = 0;
if (mode == BF_ENCRYPT) {
u_int8_t *in, *out;
n2l(iv, tout0);
n2l(iv, tout1);
while (0 < length) {
if (!m)
panic("mbuf chain?\n");
if (off + 8 <= m->m_len)
bcopy(mtod(m, u_int8_t *) + off, &inbuf[0], 8);
else {
struct mbuf *n;
size_t noff;
u_int8_t *p;
u_int8_t *in;
n = m;
noff = off;
p = mtod(n, u_int8_t *) + noff;
in = &inbuf[0];
while (in - &inbuf[0] < 8) {
if (!p)
panic("mbuf chain?\n");
*in++ = *p++;
noff++;
if (noff < n->m_len)
continue;
do {
n = n->m_next;
} while (n && ! n->m_len);
noff = 0;
if (n)
p = mtod(n, u_int8_t *) + noff;
else
p = NULL;
}
}
in = &inbuf[0];
out = &outbuf[0];
n2l(in, tin0);
n2l(in, tin1);
tin0 ^= tout0; tin[0] = tin0;
tin1 ^= tout1; tin[1] = tin1;
BF_encrypt(tin, key, BF_ENCRYPT);
tout0 = tin[0]; l2n(tout0, out);
tout1 = tin[1]; l2n(tout1, out);
if (off + 8 < m->m_len) {
bcopy(&outbuf[0], mtod(m, u_int8_t *) + off, 8);
off += 8;
} else if (off + 8 == m->m_len) {
bcopy(&outbuf[0], mtod(m, u_int8_t *) + off, 8);
do {
m = m->m_next;
} while (m && ! m->m_len);
off = 0;
} else {
struct mbuf *n;
size_t noff;
u_int8_t *p;
u_int8_t *out;
n = m;
noff = off;
p = mtod(n, u_int8_t *) + noff;
out = &outbuf[0];
while (out - &outbuf[0] < 8) {
if (!p)
panic("mbuf chain?");
*p++ = *out++;
noff++;
if (noff < n->m_len)
continue;
do {
n = n->m_next;
} while (n && ! n->m_len);
noff = 0;
if (n)
p = mtod(n, u_int8_t *) + noff;
else
p = NULL;
}
m = n;
off = noff;
}
length -= 8;
}
} else if (mode == BF_DECRYPT) {
register BF_LONG xor0, xor1;
u_int8_t *in, *out;
xor0 = xor1 = 0;
n2l(iv, xor0);
n2l(iv, xor1);
while (0 < length) {
if (!m)
panic("mbuf chain?\n");
if (off + 8 <= m->m_len)
bcopy(mtod(m, u_int8_t *) + off, &inbuf[0], 8);
else {
struct mbuf *n;
size_t noff;
u_int8_t *p;
u_int8_t *in;
n = m;
noff = off;
p = mtod(n, u_int8_t *) + noff;
in = &inbuf[0];
while (in - &inbuf[0] < 8) {
if (!p)
panic("mbuf chain?\n");
*in++ = *p++;
noff++;
if (noff < n->m_len)
continue;
do {
n = n->m_next;
} while (n && ! n->m_len);
noff = 0;
if (n)
p = mtod(n, u_int8_t *) + noff;
else
p = NULL;
}
}
in = &inbuf[0];
out = &outbuf[0];
n2l(in, tin0); tin[0] = tin0;
n2l(in, tin1); tin[1] = tin1;
BF_encrypt(tin, key, BF_DECRYPT);
tout0 = tin[0] ^ xor0;
tout1 = tin[1] ^ xor1;
l2n(tout0, out);
l2n(tout1, out);
xor0 = tin0;
xor1 = tin1;
if (off + 8 < m->m_len) {
bcopy(&outbuf[0], mtod(m, u_int8_t *) + off, 8);
off += 8;
} else if (off + 8 == m->m_len) {
bcopy(&outbuf[0], mtod(m, u_int8_t *) + off, 8);
do {
m = m->m_next;
} while (m && ! m->m_len);
off = 0;
} else {
struct mbuf *n;
size_t noff;
u_int8_t *p;
u_int8_t *out;
n = m;
noff = off;
p = mtod(n, u_int8_t *) + noff;
out = &outbuf[0];
while (out - &outbuf[0] < 8) {
if (!p)
panic("mbuf chain?\n");
*p++ = *out++;
noff++;
if (noff < n->m_len)
continue;
do {
n = n->m_next;
} while (n && ! n->m_len);
noff = 0;
if (n)
p = mtod(n, u_int8_t *) + noff;
else
p = NULL;
}
m = n;
off = noff;
}
length -= 8;
}
}
}