The changes also built and passed certification on a 32-bit SuSE 8.2 system with gcc 3.3. Optimized builds of unmodified vs. modified code on this system showed no differences in benchmark runs.
I put a FIXME comment in config.h that someone who understands what 'word' and 'dword' mean in cryptopp should look at. I also fixed some code in serpent.cpp which was probably accidentally working in 32 bit.
-K
diff -Naur cryptopp-org-src/config.h cryptopp-mod-src/config.h
--- cryptopp-org-src/config.h 2003-03-19 19:24:11.000000000 -0600
+++ cryptopp-mod-src/config.h 2003-09-19 13:20:15.000000000 -0500
@@ -100,10 +100,12 @@
NAMESPACE_BEGIN(CryptoPP)
typedef unsigned short word16;
-#if defined(__alpha) && !defined(_MSC_VER)
+#if (defined(__alpha) || defined(__x86_64__)) && !defined(_MSC_VER)
typedef unsigned int word32;
+ typedef int sword32;
#else
typedef unsigned long word32;
+ typedef long sword32;
#endif
#if defined(__GNUC__) || defined(__MWERKS__)
@@ -117,16 +119,21 @@
#endif
// defined this if your CPU is not 64-bit
-#if defined(WORD64_AVAILABLE) && !defined(__alpha)
+#if defined(WORD64_AVAILABLE) && !(defined(__alpha) || defined(__x86_64__))
# define SLOW_WORD64
#endif
// word should have the same size as your CPU registers
// dword should be twice as big as word
-#if (defined(__GNUC__) && !defined(__alpha)) || defined(__MWERKS__)
+#if (defined(__GNUC__) && !(defined(__alpha) || defined(__x86_64__))) ||
defined(__MWERKS__)
typedef unsigned long word;
typedef unsigned long long dword;
+#elif defined(__GNUC__) && defined(__x86_64__)
+ // FIXME -- Wordsize for x86_64 should be 64 bit, but then dword
+ // cannot be 2*sizeof(word). Is this correct?
+ typedef word32 word;
+ typedef word64 dword;
#elif defined(_MSC_VER) || defined(__BCPLUSPLUS__)
typedef unsigned __int32 word;
typedef unsigned __int64 dword;
diff -Naur cryptopp-org-src/misc.cpp cryptopp-mod-src/misc.cpp
--- cryptopp-org-src/misc.cpp 2002-10-04 12:31:51.000000000 -0500
+++ cryptopp-mod-src/misc.cpp 2003-09-19 09:17:06.000000000 -0500
@@ -16,7 +16,7 @@
void xorbuf(byte *buf, const byte *mask, unsigned int count)
{
- if (((unsigned int)buf | (unsigned int)mask | count) % WORD_SIZE == 0)
+ if (IsAligned<word>(buf) && IsAligned<word>(mask) && count%WORD_SIZE == 0)
XorWords((word *)buf, (const word *)mask, count/WORD_SIZE);
else
{
@@ -27,7 +27,7 @@
void xorbuf(byte *output, const byte *input, const byte *mask, unsigned int count)
{
- if (((unsigned int)output | (unsigned int)input | (unsigned int)mask | count)
% WORD_SIZE == 0)
+ if (IsAligned<word>(output) && IsAligned<word>(input) && IsAligned<word>(mask)
&& count%WORD_SIZE == 0)
XorWords((word *)output, (const word *)input, (const word *)mask,
count/WORD_SIZE);
else
{
diff -Naur cryptopp-org-src/misc.h cryptopp-mod-src/misc.h
--- cryptopp-org-src/misc.h 2002-10-04 12:31:52.000000000 -0500
+++ cryptopp-mod-src/misc.h 2003-09-19 08:14:38.000000000 -0500
@@ -7,6 +7,7 @@
#include <string.h> // CodeWarrior doesn't have memory.h
#include <algorithm>
#include <string>
+#include <stddef.h>
#ifdef INTEL_INTRINSICS
#include <stdlib.h>
@@ -142,7 +143,7 @@
inline bool IsAlignedOn(const void *p, unsigned int alignment)
{
- return IsPowerOf2(alignment) ? ModPowerOf2((unsigned int)p, alignment) == 0 :
(unsigned int)p % alignment == 0;
+ return IsPowerOf2(alignment) ? ModPowerOf2(reinterpret_cast<ptrdiff_t>(p),
alignment) == 0 : reinterpret_cast<ptrdiff_t>(p) % alignment == 0;
}
template <class T>
diff -Naur cryptopp-org-src/serpent.cpp cryptopp-mod-src/serpent.cpp
--- cryptopp-org-src/serpent.cpp 2002-10-04 12:31:57.000000000 -0500
+++ cryptopp-mod-src/serpent.cpp 2003-09-19 13:47:59.000000000 -0500
@@ -428,15 +428,16 @@
word32 *k = m_key;
GetUserKey(LITTLE_ENDIAN_ORDER, k, 8, userKey, keylen);
- word32 i,a,b,c,d,e;
+ sword32 si;
+ word32 a,b,c,d,e;
if (keylen < 32)
k[keylen/4] |= word32(1) << ((keylen%4)*8);
k += 8;
word32 t = k[-1];
- for (i = 0; i < 132; ++i)
- k[i] = t = rotlFixed(k[i-8] ^ k[i-5] ^ k[i-3] ^ t ^ 0x9e3779b9 ^ i,
11);
+ for (si = 0; si < 132; ++si)
+ k[si] = t = rotlFixed(k[si-8] ^ k[si-5] ^ k[si-3] ^ t ^ 0x9e3779b9 ^
si, 11);
k -= 20;
#define LK(r, a, b, c, d, e) {\
@@ -451,7 +452,7 @@
k[(8-r)*4 + 6] = c; \
k[(8-r)*4 + 7] = d;} \
- for (i=0; i<4; i++)
+ for (si=0; si<4; ++si)
{
afterS2(LK); afterS2(S3); afterS3(SK);
afterS1(LK); afterS1(S2); afterS2(SK);
