diff options
author | Niels Provos <provos@cvs.openbsd.org> | 2000-06-17 23:36:23 +0000 |
---|---|---|
committer | Niels Provos <provos@cvs.openbsd.org> | 2000-06-17 23:36:23 +0000 |
commit | c80a3f0602a591c636d046d07b57451942e79d11 (patch) | |
tree | a1e7ecba07c12f34d8404d13341f2fe09a9a5edb /sys/crypto | |
parent | 0d5811a2c2cc1f42ad20e4a56ca42094ee03b48e (diff) |
cbc mode
Diffstat (limited to 'sys/crypto')
-rw-r--r-- | sys/crypto/blf.c | 98 | ||||
-rw-r--r-- | sys/crypto/blf.h | 5 |
2 files changed, 101 insertions, 2 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]; +} diff --git a/sys/crypto/blf.h b/sys/crypto/blf.h index 90397f1fa3f..898c2ed5fab 100644 --- a/sys/crypto/blf.h +++ b/sys/crypto/blf.h @@ -1,4 +1,4 @@ -/* $OpenBSD: blf.h,v 1.1 2000/02/28 23:13:04 deraadt Exp $ */ +/* $OpenBSD: blf.h,v 1.2 2000/06/17 23:36:22 provos Exp $ */ /* * Blowfish - a fast block cipher designed by Bruce Schneier * @@ -75,4 +75,7 @@ u_int32_t Blowfish_stream2word __P((const u_int8_t *, u_int16_t , void blf_ecb_encrypt __P((blf_ctx *, u_int8_t *, u_int32_t)); void blf_ecb_decrypt __P((blf_ctx *, u_int8_t *, u_int32_t)); + +void blf_cbc_encrypt __P((blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t)); +void blf_cbc_decrypt __P((blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t)); #endif |