summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorume <ume@FreeBSD.org>2003-10-14 13:37:37 +0000
committerume <ume@FreeBSD.org>2003-10-14 13:37:37 +0000
commit3d011c684cbc09c3e7bfac3def70572bace7f8ea (patch)
tree93a38a5c67061d7794afbedc6f0158130e711268
parent136a7a6d45af023279db579bea0b60e51f9d2b8f (diff)
downloadFreeBSD-src-3d011c684cbc09c3e7bfac3def70572bace7f8ea.zip
FreeBSD-src-3d011c684cbc09c3e7bfac3def70572bace7f8ea.tar.gz
Fix alignment problem on 64 bit arch.
I only tested if it doesn't break anything on i368. Since I have no 64 bit machine, I cannot test it, actually. Reported by: jmallett
-rw-r--r--sys/crypto/rijndael/rijndael-api-fst.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/sys/crypto/rijndael/rijndael-api-fst.c b/sys/crypto/rijndael/rijndael-api-fst.c
index ecccc22..5479763 100644
--- a/sys/crypto/rijndael/rijndael-api-fst.c
+++ b/sys/crypto/rijndael/rijndael-api-fst.c
@@ -103,7 +103,11 @@ int rijndael_cipherInit(cipherInstance *cipher, BYTE mode, char *IV) {
int rijndael_blockEncrypt(cipherInstance *cipher, keyInstance *key,
BYTE *input, int inputLen, BYTE *outBuffer) {
int i, k, t, numBlocks;
+#if 1 /*STRICT_ALIGN*/
+ u_int8_t block[16], iv[16];
+#else
u_int8_t block[16], *iv;
+#endif
if (cipher == NULL ||
key == NULL ||
@@ -126,21 +130,41 @@ int rijndael_blockEncrypt(cipherInstance *cipher, keyInstance *key,
break;
case MODE_CBC:
+#if 1 /*STRICT_ALIGN*/
+ memcpy(iv, cipher->IV, 16);
+#else
iv = cipher->IV;
+#endif
for (i = numBlocks; i > 0; i--) {
+#if 1 /*STRICT_ALIGN*/
+ memcpy(block, input, 16);
+ ((u_int32_t*)block)[0] ^= ((u_int32_t*)iv)[0];
+ ((u_int32_t*)block)[1] ^= ((u_int32_t*)iv)[1];
+ ((u_int32_t*)block)[2] ^= ((u_int32_t*)iv)[2];
+ ((u_int32_t*)block)[3] ^= ((u_int32_t*)iv)[3];
+#else
((u_int32_t*)block)[0] = ((u_int32_t*)input)[0] ^ ((u_int32_t*)iv)[0];
((u_int32_t*)block)[1] = ((u_int32_t*)input)[1] ^ ((u_int32_t*)iv)[1];
((u_int32_t*)block)[2] = ((u_int32_t*)input)[2] ^ ((u_int32_t*)iv)[2];
((u_int32_t*)block)[3] = ((u_int32_t*)input)[3] ^ ((u_int32_t*)iv)[3];
+#endif
rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
+#if 1 /*STRICT_ALIGN*/
+ memcpy(iv, outBuffer, 16);
+#else
iv = outBuffer;
+#endif
input += 16;
outBuffer += 16;
}
break;
case MODE_CFB1:
+#if 1 /*STRICT_ALIGN*/
+ memcpy(iv, cipher->IV, 16);
+#else
iv = cipher->IV;
+#endif
for (i = numBlocks; i > 0; i--) {
memcpy(outBuffer, input, 16);
for (k = 0; k < 128; k++) {
@@ -154,6 +178,9 @@ int rijndael_blockEncrypt(cipherInstance *cipher, keyInstance *key,
outBuffer += 16;
input += 16;
}
+#if 1 /*STRICT_ALIGN*/
+ memcpy(cipher->IV, iv, 16);
+#endif
break;
default:
@@ -237,7 +264,11 @@ int rijndael_padEncrypt(cipherInstance *cipher, keyInstance *key,
int rijndael_blockDecrypt(cipherInstance *cipher, keyInstance *key,
BYTE *input, int inputLen, BYTE *outBuffer) {
int i, k, t, numBlocks;
+#if 1 /*STRICT_ALIGN*/
+ u_int8_t block[16], iv[16];
+#else
u_int8_t block[16], *iv;
+#endif
if (cipher == NULL ||
key == NULL ||
@@ -260,22 +291,37 @@ int rijndael_blockDecrypt(cipherInstance *cipher, keyInstance *key,
break;
case MODE_CBC:
+#if 1 /*STRICT_ALIGN*/
+ memcpy(iv, cipher->IV, 16);
+#else
iv = cipher->IV;
+#endif
for (i = numBlocks; i > 0; i--) {
rijndaelDecrypt(key->rk, key->Nr, input, block);
((u_int32_t*)block)[0] ^= ((u_int32_t*)iv)[0];
((u_int32_t*)block)[1] ^= ((u_int32_t*)iv)[1];
((u_int32_t*)block)[2] ^= ((u_int32_t*)iv)[2];
((u_int32_t*)block)[3] ^= ((u_int32_t*)iv)[3];
+#if 1 /*STRICT_ALIGN*/
+ memcpy(iv, input, 16);
+#else
memcpy(cipher->IV, input, 16);
+#endif
memcpy(outBuffer, block, 16);
input += 16;
outBuffer += 16;
}
+#if 1 /*STRICT_ALIGN*/
+ memcpy(cipher->IV, iv, 16);
+#endif
break;
case MODE_CFB1:
+#if 1 /*STRICT_ALIGN*/
+ memcpy(iv, cipher->IV, 16);
+#else
iv = cipher->IV;
+#endif
for (i = numBlocks; i > 0; i--) {
memcpy(outBuffer, input, 16);
for (k = 0; k < 128; k++) {
@@ -289,6 +335,9 @@ int rijndael_blockDecrypt(cipherInstance *cipher, keyInstance *key,
outBuffer += 16;
input += 16;
}
+#if 1 /*STRICT_ALIGN*/
+ memcpy(cipher->IV, iv, 16);
+#endif
break;
default:
OpenPOWER on IntegriCloud