diff options
author | Heikki Korpela <heko@cvs.openbsd.org> | 2001-08-09 14:49:01 +0000 |
---|---|---|
committer | Heikki Korpela <heko@cvs.openbsd.org> | 2001-08-09 14:49:01 +0000 |
commit | 88138eff8b1c98ed2b573a36253497d711c601ba (patch) | |
tree | 7357d4c14bedbc7d345e522b7cc26d30a12caa67 /sys | |
parent | 7f74da706b38e41c79d0271a08679d1a73b03668 (diff) |
Force alignment of blocks so that we don't crash on strict alignment
archs. This is a stopgap until we get a rijndael implementation
that doesn't assume 4 byte alignment.
ok deraadt@
Diffstat (limited to 'sys')
-rw-r--r-- | sys/crypto/rijndael.c | 64 |
1 files changed, 49 insertions, 15 deletions
diff --git a/sys/crypto/rijndael.c b/sys/crypto/rijndael.c index a2b543bd9e0..e68403f78e7 100644 --- a/sys/crypto/rijndael.c +++ b/sys/crypto/rijndael.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rijndael.c,v 1.7 2001/07/31 16:39:54 stevesk Exp $ */ +/* $OpenBSD: rijndael.c,v 1.8 2001/08/09 14:49:00 heko Exp $ */ /* This is an independent implementation of the encryption algorithm: */ /* */ @@ -352,11 +352,20 @@ rijndael_encrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk) u4byte k_len = ctx->k_len; u4byte *e_key = ctx->e_key; u4byte b0[4], b1[4], *kp; - - b0[0] = io_swap(in_blk[0]) ^ e_key[0]; - b0[1] = io_swap(in_blk[1]) ^ e_key[1]; - b0[2] = io_swap(in_blk[2]) ^ e_key[2]; - b0[3] = io_swap(in_blk[3]) ^ e_key[3]; + u4byte tbuf[4]; + + if ((u_long)in_blk & 3) { + bcopy(in_blk, tbuf, sizeof(tbuf)); + b0[0] = io_swap(tbuf[0]) ^ e_key[0]; + b0[1] = io_swap(tbuf[1]) ^ e_key[1]; + b0[2] = io_swap(tbuf[2]) ^ e_key[2]; + b0[3] = io_swap(tbuf[3]) ^ e_key[3]; + } else { + b0[0] = io_swap(in_blk[0]) ^ e_key[0]; + b0[1] = io_swap(in_blk[1]) ^ e_key[1]; + b0[2] = io_swap(in_blk[2]) ^ e_key[2]; + b0[3] = io_swap(in_blk[3]) ^ e_key[3]; + } kp = e_key + 4; @@ -374,8 +383,16 @@ rijndael_encrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk) f_nround(b1, b0, kp); f_nround(b0, b1, kp); f_nround(b1, b0, kp); f_lround(b0, b1, kp); - out_blk[0] = io_swap(b0[0]); out_blk[1] = io_swap(b0[1]); - out_blk[2] = io_swap(b0[2]); out_blk[3] = io_swap(b0[3]); + if ((u_long)out_blk & 3) { + tbuf[0] = io_swap(b0[0]); + tbuf[1] = io_swap(b0[1]); + tbuf[2] = io_swap(b0[2]); + tbuf[3] = io_swap(b0[3]); + bcopy(tbuf, out_blk, sizeof(tbuf)); + } else { + out_blk[0] = io_swap(b0[0]); out_blk[1] = io_swap(b0[1]); + out_blk[2] = io_swap(b0[2]); out_blk[3] = io_swap(b0[3]); + } } /* decrypt a block of text */ @@ -400,11 +417,20 @@ rijndael_decrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk) u4byte k_len = ctx->k_len; u4byte *e_key = ctx->e_key; u4byte *d_key = ctx->d_key; - - b0[0] = io_swap(in_blk[0]) ^ e_key[4 * k_len + 24]; - b0[1] = io_swap(in_blk[1]) ^ e_key[4 * k_len + 25]; - b0[2] = io_swap(in_blk[2]) ^ e_key[4 * k_len + 26]; - b0[3] = io_swap(in_blk[3]) ^ e_key[4 * k_len + 27]; + u4byte tbuf[4]; + + if ((u_long)in_blk & 3) { + bcopy(in_blk, tbuf, sizeof(b0)); + b0[0] = io_swap(tbuf[0]) ^ e_key[4 * k_len + 24]; + b0[1] = io_swap(tbuf[1]) ^ e_key[4 * k_len + 25]; + b0[2] = io_swap(tbuf[2]) ^ e_key[4 * k_len + 26]; + b0[3] = io_swap(tbuf[3]) ^ e_key[4 * k_len + 27]; + } else { + b0[0] = io_swap(in_blk[0]) ^ e_key[4 * k_len + 24]; + b0[1] = io_swap(in_blk[1]) ^ e_key[4 * k_len + 25]; + b0[2] = io_swap(in_blk[2]) ^ e_key[4 * k_len + 26]; + b0[3] = io_swap(in_blk[3]) ^ e_key[4 * k_len + 27]; + } kp = d_key + 4 * (k_len + 5); @@ -422,6 +448,14 @@ rijndael_decrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk) i_nround(b1, b0, kp); i_nround(b0, b1, kp); i_nround(b1, b0, kp); i_lround(b0, b1, kp); - out_blk[0] = io_swap(b0[0]); out_blk[1] = io_swap(b0[1]); - out_blk[2] = io_swap(b0[2]); out_blk[3] = io_swap(b0[3]); + if ((u_long)out_blk & 3) { + tbuf[0] = io_swap(b0[0]); + tbuf[1] = io_swap(b0[1]); + tbuf[2] = io_swap(b0[2]); + tbuf[3] = io_swap(b0[3]); + bcopy(tbuf, out_blk, sizeof(tbuf)); + } else { + out_blk[0] = io_swap(b0[0]); out_blk[1] = io_swap(b0[1]); + out_blk[2] = io_swap(b0[2]); out_blk[3] = io_swap(b0[3]); + } } |