Skip to content

Commit 9807283

Browse files
author
Peter Thorson
committed
Update sha1::calc length parameter to more sane type references XRPLF#358
also cleans up formatting and code style and adds documentation
1 parent 83a66f4 commit 9807283

File tree

2 files changed

+126
-116
lines changed

2 files changed

+126
-116
lines changed

changelog.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ HEAD
1515
conflicts with similarly named macros in some operating systems. If you are
1616
using the WebSocket++ provided 64 bit host/network byte order functions you
1717
will need to switch to the prefixed versions.
18-
- BREAKING UTILITY CHANGE: The signature of the `base64_encode` has changed from
18+
- BREAKING UTILITY CHANGE: The signature of `base64_encode` has changed from
1919
`websocketpp::base64_encode(unsigned char const *, unsigned int)` to
2020
`websocketpp::base64_encode(unsigned char const *, size_t)`.
21+
- BREAKING UTILITY CHANGE: The signature of `sha1::calc` has changed from
22+
`websocketpp::sha1::calc(void const *, int, unsigned char *)` to
23+
`websocketpp::sha1::calc(void const *, size_t, unsigned char *)`
2124
- Feature: Adds incomplete `minimal_server` and `minimal_client` configs that
2225
can be used to build custom configs without pulling in the dependencies of
2326
`core` or `core_client`. These configs will offer a stable base config to

websocketpp/sha1/sha1.hpp

+122-115
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*****
3-
sha1.hpp is a repackaging of the sha1.cpp and sha1.h files from the shallsha1
3+
sha1.hpp is a repackaging of the sha1.cpp and sha1.h files from the smallsha1
44
library (http://code.google.com/p/smallsha1/) into a single header suitable for
55
use as a header only library. This conversion was done by Peter Thorson
66
(webmaster@zaphoyd.com) in 2013. All modifications to the code are redistributed
@@ -39,142 +39,149 @@ under the same license as the original, which is listed below.
3939
namespace websocketpp {
4040
namespace sha1 {
4141

42-
namespace // local
42+
namespace { // local
43+
44+
// Rotate an integer value to left.
45+
inline unsigned int rol(unsigned int value, unsigned int steps) {
46+
return ((value << steps) | (value >> (32 - steps)));
47+
}
48+
49+
// Sets the first 16 integers in the buffert to zero.
50+
// Used for clearing the W buffert.
51+
inline void clearWBuffert(unsigned int * buffert)
52+
{
53+
for (int pos = 16; --pos >= 0;)
4354
{
44-
// Rotate an integer value to left.
45-
inline unsigned int rol(unsigned int value, unsigned int steps) {
46-
return ((value << steps) | (value >> (32 - steps)));
47-
}
55+
buffert[pos] = 0;
56+
}
57+
}
58+
59+
inline void innerHash(unsigned int * result, unsigned int * w)
60+
{
61+
unsigned int a = result[0];
62+
unsigned int b = result[1];
63+
unsigned int c = result[2];
64+
unsigned int d = result[3];
65+
unsigned int e = result[4];
66+
67+
int round = 0;
68+
69+
#define sha1macro(func,val) \
70+
{ \
71+
const unsigned int t = rol(a, 5) + (func) + e + val + w[round]; \
72+
e = d; \
73+
d = c; \
74+
c = rol(b, 30); \
75+
b = a; \
76+
a = t; \
77+
}
4878

49-
// Sets the first 16 integers in the buffert to zero.
50-
// Used for clearing the W buffert.
51-
inline void clearWBuffert(unsigned int * buffert)
52-
{
53-
for (int pos = 16; --pos >= 0;)
54-
{
55-
buffert[pos] = 0;
56-
}
57-
}
79+
while (round < 16)
80+
{
81+
sha1macro((b & c) | (~b & d), 0x5a827999)
82+
++round;
83+
}
84+
while (round < 20)
85+
{
86+
w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
87+
sha1macro((b & c) | (~b & d), 0x5a827999)
88+
++round;
89+
}
90+
while (round < 40)
91+
{
92+
w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
93+
sha1macro(b ^ c ^ d, 0x6ed9eba1)
94+
++round;
95+
}
96+
while (round < 60)
97+
{
98+
w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
99+
sha1macro((b & c) | (b & d) | (c & d), 0x8f1bbcdc)
100+
++round;
101+
}
102+
while (round < 80)
103+
{
104+
w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
105+
sha1macro(b ^ c ^ d, 0xca62c1d6)
106+
++round;
107+
}
58108

59-
inline void innerHash(unsigned int * result, unsigned int * w)
60-
{
61-
unsigned int a = result[0];
62-
unsigned int b = result[1];
63-
unsigned int c = result[2];
64-
unsigned int d = result[3];
65-
unsigned int e = result[4];
66-
67-
int round = 0;
68-
69-
#define sha1macro(func,val) \
70-
{ \
71-
const unsigned int t = rol(a, 5) + (func) + e + val + w[round]; \
72-
e = d; \
73-
d = c; \
74-
c = rol(b, 30); \
75-
b = a; \
76-
a = t; \
77-
}
109+
#undef sha1macro
78110

79-
while (round < 16)
80-
{
81-
sha1macro((b & c) | (~b & d), 0x5a827999)
82-
++round;
83-
}
84-
while (round < 20)
85-
{
86-
w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
87-
sha1macro((b & c) | (~b & d), 0x5a827999)
88-
++round;
89-
}
90-
while (round < 40)
91-
{
92-
w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
93-
sha1macro(b ^ c ^ d, 0x6ed9eba1)
94-
++round;
95-
}
96-
while (round < 60)
97-
{
98-
w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
99-
sha1macro((b & c) | (b & d) | (c & d), 0x8f1bbcdc)
100-
++round;
101-
}
102-
while (round < 80)
103-
{
104-
w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
105-
sha1macro(b ^ c ^ d, 0xca62c1d6)
106-
++round;
107-
}
111+
result[0] += a;
112+
result[1] += b;
113+
result[2] += c;
114+
result[3] += d;
115+
result[4] += e;
116+
}
108117

109-
#undef sha1macro
118+
} // namespace
110119

111-
result[0] += a;
112-
result[1] += b;
113-
result[2] += c;
114-
result[3] += d;
115-
result[4] += e;
116-
}
117-
} // namespace
118-
119-
/**
120-
@param src points to any kind of data to be hashed.
121-
@param bytelength the number of bytes to hash from the src pointer.
122-
@param hash should point to a buffer of at least 20 bytes of size for storing the sha1 result in.
123-
*/
124-
inline void calc(const void* src, const int bytelength, unsigned char* hash) {
125-
// Init the result array.
126-
unsigned int result[5] = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 };
127-
128-
// Cast the void src pointer to be the byte array we can work with.
129-
const unsigned char* sarray = (const unsigned char*) src;
130-
131-
// The reusable round buffer
132-
unsigned int w[80];
133-
134-
// Loop through all complete 64byte blocks.
135-
const int endOfFullBlocks = bytelength - 64;
136-
int endCurrentBlock;
137-
int currentBlock = 0;
138-
139-
while (currentBlock <= endOfFullBlocks)
140-
{
120+
/// Calculate a SHA1 hash
121+
/**
122+
* @param src points to any kind of data to be hashed.
123+
* @param bytelength the number of bytes to hash from the src pointer.
124+
* @param hash should point to a buffer of at least 20 bytes of size for storing
125+
* the sha1 result in.
126+
*/
127+
inline void calc(void const * src, size_t bytelength, unsigned char * hash) {
128+
// Init the result array.
129+
unsigned int result[5] = { 0x67452301, 0xefcdab89, 0x98badcfe,
130+
0x10325476, 0xc3d2e1f0 };
131+
132+
// Cast the void src pointer to be the byte array we can work with.
133+
unsigned char const * sarray = (unsigned char const *) src;
134+
135+
// The reusable round buffer
136+
unsigned int w[80];
137+
138+
// Loop through all complete 64byte blocks.
139+
140+
size_t endCurrentBlock;
141+
size_t currentBlock = 0;
142+
143+
if (bytelength >= 64) {
144+
size_t const endOfFullBlocks = bytelength - 64;
145+
146+
while (currentBlock <= endOfFullBlocks) {
141147
endCurrentBlock = currentBlock + 64;
142148

143149
// Init the round buffer with the 64 byte block data.
144150
for (int roundPos = 0; currentBlock < endCurrentBlock; currentBlock += 4)
145151
{
146-
// This line will swap endian on big endian and keep endian on little endian.
152+
// This line will swap endian on big endian and keep endian on
153+
// little endian.
147154
w[roundPos++] = (unsigned int) sarray[currentBlock + 3]
148155
| (((unsigned int) sarray[currentBlock + 2]) << 8)
149156
| (((unsigned int) sarray[currentBlock + 1]) << 16)
150157
| (((unsigned int) sarray[currentBlock]) << 24);
151158
}
152159
innerHash(result, w);
153160
}
161+
}
154162

155-
// Handle the last and not full 64 byte block if existing.
156-
endCurrentBlock = bytelength - currentBlock;
157-
clearWBuffert(w);
158-
int lastBlockBytes = 0;
159-
for (;lastBlockBytes < endCurrentBlock; ++lastBlockBytes)
160-
{
161-
w[lastBlockBytes >> 2] |= (unsigned int) sarray[lastBlockBytes + currentBlock] << ((3 - (lastBlockBytes & 3)) << 3);
162-
}
163-
w[lastBlockBytes >> 2] |= 0x80 << ((3 - (lastBlockBytes & 3)) << 3);
164-
if (endCurrentBlock >= 56)
165-
{
166-
innerHash(result, w);
167-
clearWBuffert(w);
168-
}
169-
w[15] = bytelength << 3;
163+
// Handle the last and not full 64 byte block if existing.
164+
endCurrentBlock = bytelength - currentBlock;
165+
clearWBuffert(w);
166+
int lastBlockBytes = 0;
167+
for (;lastBlockBytes < endCurrentBlock; ++lastBlockBytes) {
168+
w[lastBlockBytes >> 2] |= (unsigned int) sarray[lastBlockBytes + currentBlock] << ((3 - (lastBlockBytes & 3)) << 3);
169+
}
170+
171+
w[lastBlockBytes >> 2] |= 0x80 << ((3 - (lastBlockBytes & 3)) << 3);
172+
if (endCurrentBlock >= 56) {
170173
innerHash(result, w);
174+
clearWBuffert(w);
175+
}
176+
w[15] = bytelength << 3;
177+
innerHash(result, w);
171178

172-
// Store hash in result pointer, and make sure we get in in the correct order on both endian models.
173-
for (int hashByte = 20; --hashByte >= 0;)
174-
{
175-
hash[hashByte] = (result[hashByte >> 2] >> (((3 - hashByte) & 0x3) << 3)) & 0xff;
176-
}
179+
// Store hash in result pointer, and make sure we get in in the correct
180+
// order on both endian models.
181+
for (int hashByte = 20; --hashByte >= 0;) {
182+
hash[hashByte] = (result[hashByte >> 2] >> (((3 - hashByte) & 0x3) << 3)) & 0xff;
177183
}
184+
}
178185

179186
} // namespace sha1
180187
} // namespace websocketpp

0 commit comments

Comments
 (0)