From 7eabcf8da01f9e15b41ffc2964d104a7b0efbef3 Mon Sep 17 00:00:00 2001 From: ishmal Date: Mon, 21 Apr 2008 19:21:57 +0000 Subject: [PATCH] Fix SHA1 64bit-ism. Clean up warnings. --- src/pedro/pedroutil.cpp | 178 ++++++++++++++++++++-------------------- src/pedro/pedroutil.h | 27 +++--- src/pedro/pedroxmpp.cpp | 84 +++++++++---------- src/pedro/pedroxmpp.h | 10 +-- 4 files changed, 151 insertions(+), 148 deletions(-) diff --git a/src/pedro/pedroutil.cpp b/src/pedro/pedroutil.cpp index 43ff9748d..e4433a6b5 100644 --- a/src/pedro/pedroutil.cpp +++ b/src/pedro/pedroutil.cpp @@ -72,7 +72,7 @@ namespace Pedro //################# -static char *base64encode = +static const char *base64encode = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -304,9 +304,6 @@ DOMString Base64Decoder::decodeToString(const DOMString &str) //######################################################################## //######################################################################## - - - void Sha1::hash(unsigned char *dataIn, int len, unsigned char *digest) { Sha1 sha1; @@ -314,7 +311,7 @@ void Sha1::hash(unsigned char *dataIn, int len, unsigned char *digest) sha1.finish(digest); } -static char *sha1hex = "0123456789abcdef"; +static const char *sha1hex = "0123456789abcdef"; DOMString Sha1::hashHex(unsigned char *dataIn, int len) { @@ -340,40 +337,53 @@ DOMString Sha1::hashHex(const DOMString &str) void Sha1::init() { - lenW = 0; - sizeHi = 0; - sizeLo = 0; + longNr = 0; + byteNr = 0; + nrBytesHi = 0; + nrBytesLo = 0; // Initialize H with the magic constants (see FIPS180 for constants) - H[0] = 0x67452301L; - H[1] = 0xefcdab89L; - H[2] = 0x98badcfeL; - H[3] = 0x10325476L; - H[4] = 0xc3d2e1f0L; + hashBuf[0] = 0x67452301L; + hashBuf[1] = 0xefcdab89L; + hashBuf[2] = 0x98badcfeL; + hashBuf[3] = 0x10325476L; + hashBuf[4] = 0xc3d2e1f0L; + + for (int i = 0; i < 4; i++) + inb[i] = 0; for (int i = 0; i < 80; i++) - W[i] = 0; + inBuf[i] = 0; } void Sha1::append(unsigned char ch) { - // Read the data into W and process blocks as they get full - W[lenW / 4] <<= 8; - W[lenW / 4] |= (unsigned long)ch; - if ((++lenW) % 64 == 0) + if (nrBytesLo == 0xffffffffL) + { + nrBytesHi++; + nrBytesLo = 0; + } + else + nrBytesLo++; + + inb[byteNr++] = (unsigned long)ch; + if (byteNr >= 4) + { + inBuf[longNr++] = inb[0] << 24 | inb[1] << 16 | + inb[2] << 8 | inb[3]; + byteNr = 0; + } + if (longNr >= 16) { - hashblock(); - lenW = 0; + transform(); + longNr = 0; } - sizeLo += 8; - sizeHi += (sizeLo < 8); } void Sha1::append(unsigned char *dataIn, int len) { - // Read the data into W and process blocks as they get full for (int i = 0; i < len; i++) append(dataIn[i]); } @@ -385,33 +395,39 @@ void Sha1::append(const DOMString &str) } -void Sha1::finish(unsigned char hashout[20]) +void Sha1::finish(unsigned char digest[20]) { - unsigned char pad0x80 = 0x80; - unsigned char pad0x00 = 0x00; - unsigned char padlen[8]; + //snapshot the bit count now before padding + unsigned long nrBitsLo = (nrBytesLo << 3) & 0xffffffff; + unsigned long nrBitsHi = (nrBytesHi << 3) | ((nrBytesLo >> 29) & 7); + + //Append terminal char + append(0x80); + + //pad until we have a 56 of 64 bytes, allowing for 8 bytes at the end + while (longNr != 14) + append(0); - // Pad with a binary 1 (e.g. 0x80), then zeroes, then length - padlen[0] = (unsigned char)((sizeHi >> 24) & 255); - padlen[1] = (unsigned char)((sizeHi >> 16) & 255); - padlen[2] = (unsigned char)((sizeHi >> 8) & 255); - padlen[3] = (unsigned char)((sizeHi >> 0) & 255); - padlen[4] = (unsigned char)((sizeLo >> 24) & 255); - padlen[5] = (unsigned char)((sizeLo >> 16) & 255); - padlen[6] = (unsigned char)((sizeLo >> 8) & 255); - padlen[7] = (unsigned char)((sizeLo >> 0) & 255); - append(&pad0x80, 1); + //##### Append length in bits + append((unsigned char)((nrBitsHi>>24) & 0xff)); + append((unsigned char)((nrBitsHi>>16) & 0xff)); + append((unsigned char)((nrBitsHi>> 8) & 0xff)); + append((unsigned char)((nrBitsHi ) & 0xff)); + append((unsigned char)((nrBitsLo>>24) & 0xff)); + append((unsigned char)((nrBitsLo>>16) & 0xff)); + append((unsigned char)((nrBitsLo>> 8) & 0xff)); + append((unsigned char)((nrBitsLo ) & 0xff)); - while (lenW != 56) - append(&pad0x00, 1); - append(padlen, 8); - // Output hash - for (int i = 0; i < 20; i++) + //copy out answer + int indx = 0; + for (int i=0 ; i<5 ; i++) { - hashout[i] = (unsigned char)(H[i / 4] >> 24); - H[i / 4] <<= 8; + digest[indx++] = (unsigned char)((hashBuf[i] >> 24) & 0xff); + digest[indx++] = (unsigned char)((hashBuf[i] >> 16) & 0xff); + digest[indx++] = (unsigned char)((hashBuf[i] >> 8) & 0xff); + digest[indx++] = (unsigned char)((hashBuf[i] ) & 0xff); } // Re-initialize the context (also zeroizes contents) @@ -419,10 +435,13 @@ void Sha1::finish(unsigned char hashout[20]) } -#define SHA_ROTL(X,n) ((((X) << (n)) | ((X) >> (32-(n)))) & 0xffffffffL) -void Sha1::hashblock() +#define SHA_ROTL(X,n) ((((X) << (n)) & 0xffffffff) | (((X) >> (32-(n))) & 0xffffffff)) + +void Sha1::transform() { + unsigned long *W = inBuf; + unsigned long *H = hashBuf; for (int t = 16; t <= 79; t++) W[t] = SHA_ROTL(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1); @@ -437,7 +456,7 @@ void Sha1::hashblock() for (int t = 0; t <= 19; t++) { - TEMP = (SHA_ROTL(A,5) + (((C^D)&B)^D) + + TEMP = (SHA_ROTL(A,5) + ((B&C)|((~B)&D)) + E + W[t] + 0x5a827999L) & 0xffffffffL; E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP; } @@ -449,7 +468,7 @@ void Sha1::hashblock() } for (int t = 40; t <= 59; t++) { - TEMP = (SHA_ROTL(A,5) + ((B&C)|(D&(B|C))) + + TEMP = (SHA_ROTL(A,5) + ((B&C)|(B&D)|(C&D)) + E + W[t] + 0x8f1bbcdcL) & 0xffffffffL; E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP; } @@ -460,18 +479,15 @@ void Sha1::hashblock() E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP; } - H[0] += A; - H[1] += B; - H[2] += C; - H[3] += D; - H[4] += E; + H[0] = (H[0] + A) & 0xffffffffL; + H[1] = (H[1] + B) & 0xffffffffL; + H[2] = (H[2] + C) & 0xffffffffL; + H[3] = (H[3] + D) & 0xffffffffL; + H[4] = (H[4] + E) & 0xffffffffL; } - - - //######################################################################## //######################################################################## //### M D 5 H A S H I N G @@ -481,7 +497,6 @@ void Sha1::hashblock() - void Md5::hash(unsigned char *dataIn, unsigned long len, unsigned char *digest) { Md5 md5; @@ -525,7 +540,7 @@ void Md5::init() -/* +/** * Update with one character */ void Md5::append(unsigned char ch) @@ -582,38 +597,25 @@ void Md5::append(const DOMString &str) void Md5::finish(unsigned char *digest) { //snapshot the bit count now before padding - unsigned long nrBitsLo = nrBytesLo << 3; + unsigned long nrBitsLo = (nrBytesLo << 3) & 0xffffffff; unsigned long nrBitsHi = (nrBytesHi << 3) | ((nrBytesLo >> 29) & 7); //Append terminal char append(0x80); - //pad until we have a 56 of 64 bits, allowing for 8 bytes at the end - while (true) - { - int remain = (int)(nrBytesLo & 63); - if (remain == 56) - break; + //pad until we have a 56 of 64 bytes, allowing for 8 bytes at the end + while (longNr != 14) append(0); - } //##### Append length in bits - int shift; - shift = 0; - for (int i=0 ; i<4 ; i++) - { - unsigned char ch = (unsigned char)((nrBitsLo>>shift) & 0xff); - append(ch); - shift += 8; - } - - shift = 0; - for (int i=0 ; i<4 ; i++) - { - unsigned char ch = (unsigned char)((nrBitsHi>>shift) & 0xff); - append(ch); - shift += 8; - } + append((unsigned char)((nrBitsLo ) & 0xff)); + append((unsigned char)((nrBitsLo>> 8) & 0xff)); + append((unsigned char)((nrBitsLo>>16) & 0xff)); + append((unsigned char)((nrBitsLo>>24) & 0xff)); + append((unsigned char)((nrBitsHi ) & 0xff)); + append((unsigned char)((nrBitsHi>> 8) & 0xff)); + append((unsigned char)((nrBitsHi>>16) & 0xff)); + append((unsigned char)((nrBitsHi>>24) & 0xff)); //copy out answer int indx = 0; @@ -659,14 +661,14 @@ DOMString Md5::finishHex() // ## This is the central step in the MD5 algorithm. #define MD5STEP(f, w, x, y, z, data, s) \ - ( w += (f(x, y, z) + data), M(w), w = w<>(32-s), w += x, M(w) ) + ( w += (f(x, y, z) + data), M(w), w = w<>(32-s), w += x, M(w) ) /* * The core of the MD5 algorithm, this alters an existing MD5 hash to * reflect the addition of 16 longwords of new data. MD5Update blocks * the data and converts bytes into longwords for this routine. - * @parm buf points to an array of 4 unsigned longs - * @parm in points to an array of 16 unsigned longs + * @parm buf points to an array of 4 unsigned 32bit (at least) integers + * @parm in points to an array of 16 unsigned 32bit (at least) integers */ void Md5::transform() { @@ -754,8 +756,6 @@ void Md5::transform() - - //######################################################################## //######################################################################## //### T H R E A D @@ -895,7 +895,7 @@ TcpSocket::TcpSocket(const std::string &hostnameArg, int port) #ifdef HAVE_SSL -static void cryptoLockCallback(int mode, int type, const char *file, int line) +static void cryptoLockCallback(int mode, int type, const char */*file*/, int /*line*/) { //printf("########### LOCK\n"); static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */ @@ -981,7 +981,7 @@ TcpSocket::TcpSocket(const TcpSocket &other) } -void TcpSocket::error(char *fmt, ...) +void TcpSocket::error(const char *fmt, ...) { static char buf[256]; lastError = "TcpSocket err: "; diff --git a/src/pedro/pedroutil.h b/src/pedro/pedroutil.h index 767c23b95..fde2b16f8 100644 --- a/src/pedro/pedroutil.h +++ b/src/pedro/pedroutil.h @@ -158,14 +158,14 @@ private: //######################################################################## /** - * + * This class performs a slow SHA1 hash on a stream of input data. */ class Sha1 { public: /** - * + * Constructor */ Sha1() { init(); } @@ -174,7 +174,7 @@ public: * */ virtual ~Sha1() - {} + { init(); } /** @@ -201,17 +201,17 @@ public: virtual void init(); /** - * + * Append a single character */ virtual void append(unsigned char ch); /** - * + * Append a data buffer */ virtual void append(unsigned char *dataIn, int len); /** - * + * Append a String */ virtual void append(const DOMString &str); @@ -224,12 +224,15 @@ public: private: - void hashblock(); + void transform(); - unsigned long H[5]; - unsigned long W[80]; - unsigned long sizeHi,sizeLo; - int lenW; + unsigned long hashBuf[5]; + unsigned long inBuf[80]; + unsigned long nrBytesHi; + unsigned long nrBytesLo; + int longNr; + int byteNr; + unsigned long inb[4]; }; @@ -469,7 +472,7 @@ public: virtual ~TcpSocket(); - void error(char *fmt, ...); + void error(const char *fmt, ...); DOMString &getLastError(); diff --git a/src/pedro/pedroxmpp.cpp b/src/pedro/pedroxmpp.cpp index 4880123e8..2b8719002 100644 --- a/src/pedro/pedroxmpp.cpp +++ b/src/pedro/pedroxmpp.cpp @@ -293,7 +293,7 @@ XmppEventTarget::~XmppEventTarget() /** * Print a printf()-like formatted error message */ -void XmppEventTarget::error(char *fmt, ...) +void XmppEventTarget::error(const char *fmt, ...) { va_list args; va_start(args,fmt); @@ -311,7 +311,7 @@ void XmppEventTarget::error(char *fmt, ...) /** * Print a printf()-like formatted trace message */ -void XmppEventTarget::status(char *fmt, ...) +void XmppEventTarget::status(const char *fmt, ...) { va_list args; va_start(args,fmt); @@ -672,7 +672,7 @@ bool XmppClient::pause(unsigned long millis) } -static int strIndex(const DOMString &str, char *key) +static int strIndex(const DOMString &str, const char *key) { unsigned int p = str.find(key); if (p == str.npos) @@ -964,7 +964,7 @@ bool XmppClient::processMessage(Element *root) if (strIndex(body, " locked") >= 0) { printf("LOCKED!! ;)\n"); - char *fmt = + const char *fmt = "" "" "" @@ -1289,7 +1289,7 @@ bool XmppClient::receiveAndProcessLoop() //######################################################################## -bool XmppClient::write(char *fmt, ...) +bool XmppClient::write(const char *fmt, ...) { bool rc = true; va_list args; @@ -1323,7 +1323,7 @@ bool XmppClient::inBandRegistrationNew() { Parser parser; - char *fmt = + const char *fmt = "" "" "\n\n"; @@ -1419,7 +1419,7 @@ bool XmppClient::inBandRegistrationChangePassword(const DOMString &newpassword) Parser parser; //# Let's try it form-style to allow the common old/new password thing - char *fmt = + const char *fmt = "" " " " " @@ -1456,10 +1456,10 @@ bool XmppClient::inBandRegistrationCancel() { Parser parser; - char *fmt = + const char *fmt = "" - "" - "\n\n"; + "" + "\n\n"; if (!write(fmt, msgId++)) return false; @@ -1480,10 +1480,10 @@ bool XmppClient::iqAuthenticate(const DOMString &streamId) { Parser parser; - char *fmt = - "" - "%s" - "\n"; + const char *fmt = + "" + "%s" + "\n"; if (!write(fmt, realm.c_str(), msgId++, username.c_str())) return false; @@ -1617,7 +1617,7 @@ static bool saslParse(const DOMString &s, bool XmppClient::saslMd5Authenticate() { Parser parser; - char *fmt = + const char *fmt = "\n"; if (!write("%s",fmt)) @@ -1818,7 +1818,7 @@ bool XmppClient::saslPlainAuthenticate() DOMString base64Auth = encoder.finish(); //printf("authbuf:%s\n", base64Auth.c_str()); - char *fmt = + const char *fmt = "%s\n"; if (!write(fmt, base64Auth.c_str())) @@ -1865,7 +1865,7 @@ bool XmppClient::saslAuthenticate(const DOMString &streamId) if (wantStartTls && !sock->getEnableSSL() && sock->getHaveSSL()) { delete elem; - char *fmt = + const char *fmt = "\n"; if (!write("%s",fmt)) return false; @@ -2022,7 +2022,7 @@ bool XmppClient::createSession() dispatchXmppEvent(event); } - char *fmt = + const char *fmt = "" "%s" @@ -2299,7 +2299,7 @@ bool XmppClient::rosterDelete(const DOMString &otherJid) { if (!checkConnect()) return false; - char *fmt = + const char *fmt = "" "" "%s" @@ -2391,7 +2391,7 @@ bool XmppClient::message(const DOMString &user, const DOMString &subj, if (xmlSubj.size() > 0) { - char *fmt = + const char *fmt = "" "%s%s\n"; if (!write(fmt, user.c_str(), jid.c_str(), @@ -2400,7 +2400,7 @@ bool XmppClient::message(const DOMString &user, const DOMString &subj, } else { - char *fmt = + const char *fmt = "" "%s\n"; if (!write(fmt, user.c_str(), jid.c_str(), xmlMsg.c_str())) @@ -2431,7 +2431,7 @@ bool XmppClient::presence(const DOMString &presence) DOMString xmlPres = toXml(presence); - char *fmt = + const char *fmt = "%s\n"; if (!write(fmt, xmlPres.c_str())) return false; @@ -2627,7 +2627,7 @@ std::vector XmppClient::groupChatGetUserList( */ bool XmppClient::groupChatJoin(const DOMString &groupJid, const DOMString &nick, - const DOMString &pass) + const DOMString &/*pass*/) { if (!checkConnect()) return false; @@ -2636,7 +2636,7 @@ bool XmppClient::groupChatJoin(const DOMString &groupJid, if (user.size()<1) user = username; - char *fmt = + const char *fmt = "" "\n"; if (!write(fmt, groupJid.c_str(), user.c_str())) @@ -2660,7 +2660,7 @@ bool XmppClient::groupChatLeave(const DOMString &groupJid, if (user.size()<1) user = username; - char *fmt = + const char *fmt = "" "\n"; if (!write(fmt, groupJid.c_str(), user.c_str())) @@ -2684,13 +2684,13 @@ bool XmppClient::groupChatMessage(const DOMString &groupJid, DOMString xmlMsg = toXml(msg); - char *fmt = + const char *fmt = "" "%s\n"; if (!write(fmt, jid.c_str(), groupJid.c_str(), xmlMsg.c_str())) return false; /* - char *fmt = + const char *fmt = "" "%s\n"; if (!write(fmt, groupJid.c_str(), xmlMsg.c_str())) @@ -2715,14 +2715,14 @@ bool XmppClient::groupChatPrivateMessage(const DOMString &groupJid, DOMString xmlMsg = toXml(msg); /* - char *fmt = + const char *fmt = "" "%s\n"; if (!write(fmt, jid.c_str(), groupJid.c_str(), toNick.c_str(), xmlMsg.c_str())) return false; */ - char *fmt = + const char *fmt = "" "%s\n"; if (!write(fmt, groupJid.c_str(), @@ -2750,7 +2750,7 @@ bool XmppClient::groupChatPresence(const DOMString &groupJid, DOMString xmlPresence = toXml(presence); - char *fmt = + const char *fmt = "" "\n"; if (!write(fmt, groupJid.c_str(), @@ -2895,7 +2895,7 @@ bool XmppClient::outputStreamOpen(const DOMString &destId, outs->setPeerId(destId); - char *fmt = + const char *fmt = "<%s type='set' to='%s' id='%s'>" "\n"; @@ -2960,7 +2960,7 @@ bool XmppClient::outputStreamWrite(const DOMString &streamId, DOMString b64data = encoder.finish(); - char *fmt = + const char *fmt = "" "" "%s" @@ -3005,7 +3005,7 @@ bool XmppClient::outputStreamClose(const DOMString &streamId) outs->setMessageId(messageId); outs->setState(STREAM_CLOSING); - char *fmt = + const char *fmt = "<%s type='set' to='%s' id='%s'>" "\n"; if (!write(fmt, @@ -3050,7 +3050,7 @@ bool XmppClient::outputStreamClose(const DOMString &streamId) */ bool XmppClient::inputStreamOpen(const DOMString &fromJid, const DOMString &streamId, - const DOMString &iqId) + const DOMString &/*iqId*/) { XmppStream *ins = new XmppStream(); @@ -3080,7 +3080,7 @@ bool XmppClient::inputStreamOpen(const DOMString &fromJid, ins->reset(); return false; } - char *fmt = + const char *fmt = "<%s type='result' to='%s' id='%s'/>\n"; if (!write(fmt, streamPacket.c_str(), fromJid.c_str(), ins->getMessageId().c_str())) @@ -3107,7 +3107,7 @@ bool XmppClient::inputStreamClose(const DOMString &streamId) if (ins->getState() == STREAM_CLOSING) { - char *fmt = + const char *fmt = "\n"; if (!write(fmt, ins->getPeerId().c_str(), ins->getMessageId().c_str())) @@ -3294,7 +3294,7 @@ bool XmppClient::fileSend(const DOMString &destJidArg, struct tm *timeVal = gmtime(&(finfo.st_mtime)); strftime(dtgBuf, 80, "%Y-%m-%dT%H:%M:%Sz", timeVal); - char *fmt = + const char *fmt = "<%s type='set' id='%s' to='%s'>" "" "" diff --git a/src/pedro/pedroxmpp.h b/src/pedro/pedroxmpp.h index 7f927897f..ee3234fd4 100644 --- a/src/pedro/pedroxmpp.h +++ b/src/pedro/pedroxmpp.h @@ -505,7 +505,7 @@ public: /** * Assignment */ - XmppEventListener(const XmppEventListener &other) + XmppEventListener(const XmppEventListener &/*other*/) {} @@ -520,7 +520,7 @@ public: * event handling. Use event.getType() to decide what to do * with the event. */ - virtual void processXmppEvent(const XmppEvent &event) + virtual void processXmppEvent(const XmppEvent &/*event*/) {} }; @@ -567,13 +567,13 @@ public: /** * Send an error message to all subscribers */ - void error(char *fmt, ...) G_GNUC_PRINTF(2,3); + void error(const char *fmt, ...) G_GNUC_PRINTF(2,3); /** * Send a status message to all subscribers */ - void status(char *fmt, ...) G_GNUC_PRINTF(2,3); + void status(const char *fmt, ...) G_GNUC_PRINTF(2,3); //########################### //# LISTENERS @@ -722,7 +722,7 @@ public: /** * */ - virtual bool write(char *fmt, ...) G_GNUC_PRINTF(2,3); + virtual bool write(const char *fmt, ...) G_GNUC_PRINTF(2,3); //####################### //# V A R I A B L E S -- 2.30.2