From e9363c1ca3bf52937f0f8c64dfbc91b7c0f303e5 Mon Sep 17 00:00:00 2001 From: pat Date: Tue, 13 Oct 2015 18:03:15 -0400 Subject: [PATCH] changed test makefiles --- Makefile | 5 +- tests/Makefile | 2 +- utils/Hash.cpp | 27 ++++ utils/Hash.h | 24 ++++ utils/sha1.cpp | 23 ++++ utils/sha1.h | 23 ++++ utils/sha256.cpp | 23 ++++ utils/sha256.h | 23 ++++ websocket/HTTPProtocolParser.cpp | 5 +- websocket/Makefile | 2 +- websocket/sha1.cpp | 209 ------------------------------- websocket/sha1.h | 53 -------- 12 files changed, 151 insertions(+), 268 deletions(-) delete mode 100644 websocket/sha1.cpp delete mode 100644 websocket/sha1.h diff --git a/Makefile b/Makefile index 4331f63..c1f0621 100644 --- a/Makefile +++ b/Makefile @@ -12,13 +12,15 @@ tls:TLS=tls clean-%: -cd $* && make clean -cd tests && make clean + -rm -rf obj clean: $(addprefix clean-,$(MODULES)) -rm -R $(OUTDIR) $(LIBDIR)/%.a:% cd $< && make $(TLS) - -mv $H; +} diff --git a/utils/Hash.h b/utils/Hash.h index c07520c..640d7c8 100644 --- a/utils/Hash.h +++ b/utils/Hash.h @@ -1,3 +1,26 @@ +/* +Copyright (c) 2015 Patrick Dumais + +http://www.dumaisnet.ca + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ #pragma once #include @@ -17,6 +40,7 @@ namespace Dumais public: Hash(unsigned int hashSize, const char* input, unsigned int inputSize); virtual ~Hash(); + unsigned char* getDigest(); std::string toHex(); }; } diff --git a/utils/sha1.cpp b/utils/sha1.cpp index fb8690e..e804934 100644 --- a/utils/sha1.cpp +++ b/utils/sha1.cpp @@ -1,3 +1,26 @@ +/* +Copyright (c) 2015 Patrick Dumais + +http://www.dumaisnet.ca + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ #include "sha1.h" #include #include diff --git a/utils/sha1.h b/utils/sha1.h index b02ae07..cf4131d 100644 --- a/utils/sha1.h +++ b/utils/sha1.h @@ -1,3 +1,26 @@ +/* +Copyright (c) 2015 Patrick Dumais + +http://www.dumaisnet.ca + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ #include "Hash.h" namespace Dumais diff --git a/utils/sha256.cpp b/utils/sha256.cpp index 9f9ab30..e4a38c7 100644 --- a/utils/sha256.cpp +++ b/utils/sha256.cpp @@ -1,3 +1,26 @@ +/* +Copyright (c) 2015 Patrick Dumais + +http://www.dumaisnet.ca + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ #include "sha256.h" #include #include diff --git a/utils/sha256.h b/utils/sha256.h index 2cba16d..cf5ac84 100644 --- a/utils/sha256.h +++ b/utils/sha256.h @@ -1,3 +1,26 @@ +/* +Copyright (c) 2015 Patrick Dumais + +http://www.dumaisnet.ca + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ #include "Hash.h" namespace Dumais diff --git a/websocket/HTTPProtocolParser.cpp b/websocket/HTTPProtocolParser.cpp index 2976ce6..8d482e6 100644 --- a/websocket/HTTPProtocolParser.cpp +++ b/websocket/HTTPProtocolParser.cpp @@ -23,7 +23,7 @@ SOFTWARE. */ #include "HTTPProtocolParser.h" #include "utils/Logging.h" -#include "sha1.h" +#include "utils/sha1.h" #include "base64.h" #include #include @@ -168,8 +168,7 @@ std::string HTTPProtocolParser::switchProtocol(const std::string& key, const std { std::string accept = key+MAGIC; - SHA1 sha; - sha.addBytes(accept.c_str(),accept.size()); + Dumais::Utils::SHA1 sha(accept.c_str(),accept.size()); accept = base64_encode(sha.getDigest(),20); std::stringstream ss; diff --git a/websocket/Makefile b/websocket/Makefile index 2a561f8..8d3124b 100644 --- a/websocket/Makefile +++ b/websocket/Makefile @@ -1,4 +1,4 @@ -SOURCES = WebSocketServer.cpp WebSocket.cpp sha1.cpp base64.cpp HTTPProtocolParser.cpp WSProtocolParser.cpp +SOURCES = WebSocketServer.cpp WebSocket.cpp base64.cpp HTTPProtocolParser.cpp WSProtocolParser.cpp OBJECTS=$(SOURCES:.cpp=.o) CFLAGS=-I ../ -g -std=c++11 .cpp.o: diff --git a/websocket/sha1.cpp b/websocket/sha1.cpp deleted file mode 100644 index 3676a43..0000000 --- a/websocket/sha1.cpp +++ /dev/null @@ -1,209 +0,0 @@ -/* sha1.cpp - -Copyright (c) 2005 Michael D. Leonhard - -http://tamale.net/ - -Copyright (c) 2005 Michael D. Leonhard - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -of the Software, and to permit persons to whom the Software is furnished to do -so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -#include -#include -#include -#include - -#include "sha1.h" - -// print out memory in hexadecimal -void SHA1::hexPrinter( unsigned char* c, int l ) -{ - assert( c ); - assert( l > 0 ); - while( l > 0 ) - { - printf( " %02x", *c ); - l--; - c++; - } -} - -// circular left bit rotation. MSB wraps around to LSB -Uint32 SHA1::lrot( Uint32 x, int bits ) -{ - return (x<>(32 - bits)); -}; - -// Save a 32-bit unsigned integer to memory, in big-endian order -void SHA1::storeBigEndianUint32( unsigned char* byte, Uint32 num ) -{ - assert( byte ); - byte[0] = (unsigned char)(num>>24); - byte[1] = (unsigned char)(num>>16); - byte[2] = (unsigned char)(num>>8); - byte[3] = (unsigned char)num; -} - - -// Constructor ******************************************************* -SHA1::SHA1() -{ - // make sure that the data type is the right size - assert( sizeof( Uint32 ) * 5 == 20 ); - - // initialize - H0 = 0x67452301; - H1 = 0xefcdab89; - H2 = 0x98badcfe; - H3 = 0x10325476; - H4 = 0xc3d2e1f0; - unprocessedBytes = 0; - size = 0; -} - -// Destructor ******************************************************** -SHA1::~SHA1() -{ - // erase data - H0 = H1 = H2 = H3 = H4 = 0; - for( int c = 0; c < 64; c++ ) bytes[c] = 0; - unprocessedBytes = size = 0; -} - -// process *********************************************************** -void SHA1::process() -{ - assert( unprocessedBytes == 64 ); - //printf( "process: " ); hexPrinter( bytes, 64 ); printf( "\n" ); - int t; - Uint32 a, b, c, d, e, K, f, W[80]; - // starting values - a = H0; - b = H1; - c = H2; - d = H3; - e = H4; - // copy and expand the message block - for( t = 0; t < 16; t++ ) W[t] = (bytes[t*4] << 24) - +(bytes[t*4 + 1] << 16) - +(bytes[t*4 + 2] << 8) - + bytes[t*4 + 3]; - for(; t< 80; t++ ) W[t] = lrot( W[t-3]^W[t-8]^W[t-14]^W[t-16], 1 ); - - /* main loop */ - Uint32 temp; - for( t = 0; t < 80; t++ ) - { - if( t < 20 ) { - K = 0x5a827999; - f = (b & c) | ((b ^ 0xFFFFFFFF) & d);//TODO: try using ~ - } else if( t < 40 ) { - K = 0x6ed9eba1; - f = b ^ c ^ d; - } else if( t < 60 ) { - K = 0x8f1bbcdc; - f = (b & c) | (b & d) | (c & d); - } else { - K = 0xca62c1d6; - f = b ^ c ^ d; - } - temp = lrot(a,5) + f + e + W[t] + K; - e = d; - d = c; - c = lrot(b,30); - b = a; - a = temp; - //printf( "t=%d %08x %08x %08x %08x %08x\n",t,a,b,c,d,e ); - } - /* add variables */ - H0 += a; - H1 += b; - H2 += c; - H3 += d; - H4 += e; - //printf( "Current: %08x %08x %08x %08x %08x\n",H0,H1,H2,H3,H4 ); - /* all bytes have been processed */ - unprocessedBytes = 0; -} - -// addBytes ********************************************************** -void SHA1::addBytes( const char* data, int num ) -{ - assert( data ); - assert( num > 0 ); - // add these bytes to the running total - size += num; - // repeat until all data is processed - while( num > 0 ) - { - // number of bytes required to complete block - int needed = 64 - unprocessedBytes; - assert( needed > 0 ); - // number of bytes to copy (use smaller of two) - int toCopy = (num < needed) ? num : needed; - // Copy the bytes - memcpy( bytes + unprocessedBytes, data, toCopy ); - // Bytes have been copied - num -= toCopy; - data += toCopy; - unprocessedBytes += toCopy; - - // there is a full block - if( unprocessedBytes == 64 ) process(); - } -} - -// digest ************************************************************ -unsigned char* SHA1::getDigest() -{ - // save the message size - Uint32 totalBitsL = size << 3; - Uint32 totalBitsH = size >> 29; - // add 0x80 to the message - addBytes( "\x80", 1 ); - - unsigned char footer[64] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - // block has no room for 8-byte filesize, so finish it - if( unprocessedBytes > 56 ) - addBytes( (char*)footer, 64 - unprocessedBytes); - assert( unprocessedBytes <= 56 ); - // how many zeros do we need - int neededZeros = 56 - unprocessedBytes; - // store file size (in bits) in big-endian format - storeBigEndianUint32( footer + neededZeros , totalBitsH ); - storeBigEndianUint32( footer + neededZeros + 4, totalBitsL ); - // finish the final block - addBytes( (char*)footer, neededZeros + 8 ); - // allocate memory for the digest bytes - unsigned char* digest = (unsigned char*)malloc( 20 ); - // copy the digest bytes - storeBigEndianUint32( digest, H0 ); - storeBigEndianUint32( digest + 4, H1 ); - storeBigEndianUint32( digest + 8, H2 ); - storeBigEndianUint32( digest + 12, H3 ); - storeBigEndianUint32( digest + 16, H4 ); - // return the digest - return digest; -} - diff --git a/websocket/sha1.h b/websocket/sha1.h deleted file mode 100644 index 834622a..0000000 --- a/websocket/sha1.h +++ /dev/null @@ -1,53 +0,0 @@ -/* sha1.h - -Copyright (c) 2005 Michael D. Leonhard - -http://tamale.net/ - -Copyright (c) 2005 Michael D. Leonhard - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -of the Software, and to permit persons to whom the Software is furnished to do -so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -#ifndef SHA1_HEADER -typedef unsigned int Uint32; - -class SHA1 -{ - private: - // fields - Uint32 H0, H1, H2, H3, H4; - unsigned char bytes[64]; - int unprocessedBytes; - Uint32 size; - void process(); - public: - SHA1(); - ~SHA1(); - void addBytes( const char* data, int num ); - unsigned char* getDigest(); - // utility methods - static Uint32 lrot( Uint32 x, int bits ); - static void storeBigEndianUint32( unsigned char* byte, Uint32 num ); - static void hexPrinter( unsigned char* c, int l ); -}; - -#define SHA1_HEADER -#endif -