1 // Copyright 2021 Code Intelligence GmbH
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "utils.h"
16
17 #include <cstdint>
18 #include <cstring>
19 #include <iomanip>
20 #include <sstream>
21 #include <string>
22
23 namespace {
24 // BEGIN: Obtained from https://github.com/x42/liboauth/blob/master/src/sha1.c:
25 /* This code is public-domain - it is based on libcrypt
26 * placed in the public domain by Wei Dai and other contributors.
27 */
28
29 #ifdef __BIG_ENDIAN__
30 #define SHA_BIG_ENDIAN
31 #elif defined __LITTLE_ENDIAN__
32 /* override */
33 #elif defined __BYTE_ORDER
34 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
35 #define SHA_BIG_ENDIAN
36 #endif
37 #else // ! defined __LITTLE_ENDIAN__
38 #include <endian.h> // machine/endian.h
39 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
40 #define SHA_BIG_ENDIAN
41 #endif
42 #endif
43
44 /* header */
45
46 #define HASH_LENGTH 20
47 #define BLOCK_LENGTH 64
48
49 typedef struct sha1nfo {
50 uint32_t buffer[BLOCK_LENGTH / 4];
51 uint32_t state[HASH_LENGTH / 4];
52 uint32_t byteCount;
53 uint8_t bufferOffset;
54 uint8_t keyBuffer[BLOCK_LENGTH];
55 uint8_t innerHash[HASH_LENGTH];
56 } sha1nfo;
57
58 /* public API - prototypes - TODO: doxygen*/
59
60 /**
61 */
62 void sha1_init(sha1nfo *s);
63 /**
64 */
65 void sha1_writebyte(sha1nfo *s, uint8_t data);
66 /**
67 */
68 void sha1_write(sha1nfo *s, const char *data, size_t len);
69 /**
70 */
71 uint8_t *sha1_result(sha1nfo *s);
72
73 /* code */
74 #define SHA1_K0 0x5a827999
75 #define SHA1_K20 0x6ed9eba1
76 #define SHA1_K40 0x8f1bbcdc
77 #define SHA1_K60 0xca62c1d6
78
sha1_init(sha1nfo * s)79 void sha1_init(sha1nfo *s) {
80 s->state[0] = 0x67452301;
81 s->state[1] = 0xefcdab89;
82 s->state[2] = 0x98badcfe;
83 s->state[3] = 0x10325476;
84 s->state[4] = 0xc3d2e1f0;
85 s->byteCount = 0;
86 s->bufferOffset = 0;
87 }
88
sha1_rol32(uint32_t number,uint8_t bits)89 uint32_t sha1_rol32(uint32_t number, uint8_t bits) {
90 return ((number << bits) | (number >> (32 - bits)));
91 }
92
sha1_hashBlock(sha1nfo * s)93 void sha1_hashBlock(sha1nfo *s) {
94 uint8_t i;
95 uint32_t a, b, c, d, e, t;
96
97 a = s->state[0];
98 b = s->state[1];
99 c = s->state[2];
100 d = s->state[3];
101 e = s->state[4];
102 for (i = 0; i < 80; i++) {
103 if (i >= 16) {
104 t = s->buffer[(i + 13) & 15] ^ s->buffer[(i + 8) & 15] ^
105 s->buffer[(i + 2) & 15] ^ s->buffer[i & 15];
106 s->buffer[i & 15] = sha1_rol32(t, 1);
107 }
108 if (i < 20) {
109 t = (d ^ (b & (c ^ d))) + SHA1_K0;
110 } else if (i < 40) {
111 t = (b ^ c ^ d) + SHA1_K20;
112 } else if (i < 60) {
113 t = ((b & c) | (d & (b | c))) + SHA1_K40;
114 } else {
115 t = (b ^ c ^ d) + SHA1_K60;
116 }
117 t += sha1_rol32(a, 5) + e + s->buffer[i & 15];
118 e = d;
119 d = c;
120 c = sha1_rol32(b, 30);
121 b = a;
122 a = t;
123 }
124 s->state[0] += a;
125 s->state[1] += b;
126 s->state[2] += c;
127 s->state[3] += d;
128 s->state[4] += e;
129 }
130
sha1_addUncounted(sha1nfo * s,uint8_t data)131 void sha1_addUncounted(sha1nfo *s, uint8_t data) {
132 uint8_t *const b = (uint8_t *)s->buffer;
133 #ifdef SHA_BIG_ENDIAN
134 b[s->bufferOffset] = data;
135 #else
136 b[s->bufferOffset ^ 3] = data;
137 #endif
138 s->bufferOffset++;
139 if (s->bufferOffset == BLOCK_LENGTH) {
140 sha1_hashBlock(s);
141 s->bufferOffset = 0;
142 }
143 }
144
sha1_writebyte(sha1nfo * s,uint8_t data)145 void sha1_writebyte(sha1nfo *s, uint8_t data) {
146 ++s->byteCount;
147 sha1_addUncounted(s, data);
148 }
149
sha1_write(sha1nfo * s,const char * data,size_t len)150 void sha1_write(sha1nfo *s, const char *data, size_t len) {
151 for (; len--;) sha1_writebyte(s, (uint8_t)*data++);
152 }
153
sha1_pad(sha1nfo * s)154 void sha1_pad(sha1nfo *s) {
155 // Implement SHA-1 padding (fips180-2 §5.1.1)
156
157 // Pad with 0x80 followed by 0x00 until the end of the block
158 sha1_addUncounted(s, 0x80);
159 while (s->bufferOffset != 56) sha1_addUncounted(s, 0x00);
160
161 // Append length in the last 8 bytes
162 sha1_addUncounted(s, 0); // We're only using 32 bit lengths
163 sha1_addUncounted(s, 0); // But SHA-1 supports 64 bit lengths
164 sha1_addUncounted(s, 0); // So zero pad the top bits
165 sha1_addUncounted(s, s->byteCount >> 29); // Shifting to multiply by 8
166 sha1_addUncounted(
167 s, s->byteCount >> 21); // as SHA-1 supports bitstreams as well as
168 sha1_addUncounted(s, s->byteCount >> 13); // byte.
169 sha1_addUncounted(s, s->byteCount >> 5);
170 sha1_addUncounted(s, s->byteCount << 3);
171 }
172
sha1_result(sha1nfo * s)173 uint8_t *sha1_result(sha1nfo *s) {
174 // Pad to complete the last block
175 sha1_pad(s);
176
177 #ifndef SHA_BIG_ENDIAN
178 // Swap byte order back
179 int i;
180 for (i = 0; i < 5; i++) {
181 s->state[i] = (((s->state[i]) << 24) & 0xff000000) |
182 (((s->state[i]) << 8) & 0x00ff0000) |
183 (((s->state[i]) >> 8) & 0x0000ff00) |
184 (((s->state[i]) >> 24) & 0x000000ff);
185 }
186 #endif
187
188 // Return pointer to hash (20 characters)
189 return (uint8_t *)s->state;
190 }
191 // END: Obtained from https://github.com/x42/liboauth/blob/master/src/sha1.c:
192 } // namespace
193
194 namespace jazzer {
Sha1Hash(const uint8_t * data,size_t size)195 std::string Sha1Hash(const uint8_t *data, size_t size) {
196 sha1nfo hasher;
197 sha1_init(&hasher);
198 sha1_write(&hasher, reinterpret_cast<const char *>(data), size);
199 const uint8_t *hash = sha1_result(&hasher);
200 std::ostringstream out;
201 for (size_t i = 0; i < HASH_LENGTH; ++i) {
202 // Cast required because uint8_t would print as a char.
203 out << std::hex << std::setfill('0') << std::setw(2)
204 << static_cast<uint32_t>(hash[i]);
205 }
206 return out.str();
207 }
208 } // namespace jazzer
209