/src/libavif/build/_deps/fuzztest-src/common/sha1.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2024 The Centipede Authors. |
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 | | // https://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 "./common/sha1.h" |
16 | | |
17 | | #include <cstdint> |
18 | | #include <cstring> |
19 | | |
20 | | #include "absl/base/nullability.h" |
21 | | |
22 | | namespace centipede { |
23 | | namespace { |
24 | | |
25 | | // We vendor our own implementation of SHA1 (taken from public domain) to avoid |
26 | | // availability and linkage issues with implementations from OpenSSL/BoringSSL. |
27 | | |
28 | | #ifdef __BIG_ENDIAN__ |
29 | | #define SHA_BIG_ENDIAN |
30 | | #elif defined __LITTLE_ENDIAN__ |
31 | | /* override */ |
32 | | #elif defined __BYTE_ORDER |
33 | | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
34 | | #define SHA_BIG_ENDIAN |
35 | | #endif |
36 | | #else // ! defined __LITTLE_ENDIAN__ |
37 | | #include <endian.h> // machine/endian.h |
38 | | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
39 | | #define SHA_BIG_ENDIAN |
40 | | #endif |
41 | | #endif |
42 | | |
43 | | /* header */ |
44 | | |
45 | | #define HASH_LENGTH 20 |
46 | 0 | #define BLOCK_LENGTH 64 |
47 | | |
48 | | typedef struct sha1nfo { |
49 | | uint32_t buffer[BLOCK_LENGTH / 4]; |
50 | | uint32_t state[HASH_LENGTH / 4]; |
51 | | uint32_t byteCount; |
52 | | uint8_t bufferOffset; |
53 | | uint8_t keyBuffer[BLOCK_LENGTH]; |
54 | | uint8_t innerHash[HASH_LENGTH]; |
55 | | } sha1nfo; |
56 | | |
57 | | /* public API - prototypes - TODO: doxygen*/ |
58 | | |
59 | | /** |
60 | | */ |
61 | | void sha1_init(sha1nfo *s); |
62 | | /** |
63 | | */ |
64 | | void sha1_writebyte(sha1nfo *s, uint8_t data); |
65 | | /** |
66 | | */ |
67 | | void sha1_write(sha1nfo *s, const char *data, size_t len); |
68 | | /** |
69 | | */ |
70 | | uint8_t *sha1_result(sha1nfo *s); |
71 | | |
72 | | /* code */ |
73 | 0 | #define SHA1_K0 0x5a827999 |
74 | 0 | #define SHA1_K20 0x6ed9eba1 |
75 | 0 | #define SHA1_K40 0x8f1bbcdc |
76 | 0 | #define SHA1_K60 0xca62c1d6 |
77 | | |
78 | 0 | void sha1_init(sha1nfo *s) { |
79 | 0 | s->state[0] = 0x67452301; |
80 | 0 | s->state[1] = 0xefcdab89; |
81 | 0 | s->state[2] = 0x98badcfe; |
82 | 0 | s->state[3] = 0x10325476; |
83 | 0 | s->state[4] = 0xc3d2e1f0; |
84 | 0 | s->byteCount = 0; |
85 | 0 | s->bufferOffset = 0; |
86 | 0 | } |
87 | | |
88 | 0 | uint32_t sha1_rol32(uint32_t number, uint8_t bits) { |
89 | 0 | return ((number << bits) | (number >> (32 - bits))); |
90 | 0 | } |
91 | | |
92 | 0 | void sha1_hashBlock(sha1nfo *s) { |
93 | 0 | uint8_t i; |
94 | 0 | uint32_t a, b, c, d, e, t; |
95 | |
|
96 | 0 | a = s->state[0]; |
97 | 0 | b = s->state[1]; |
98 | 0 | c = s->state[2]; |
99 | 0 | d = s->state[3]; |
100 | 0 | e = s->state[4]; |
101 | 0 | for (i = 0; i < 80; i++) { |
102 | 0 | if (i >= 16) { |
103 | 0 | t = s->buffer[(i + 13) & 15] ^ s->buffer[(i + 8) & 15] ^ |
104 | 0 | s->buffer[(i + 2) & 15] ^ s->buffer[i & 15]; |
105 | 0 | s->buffer[i & 15] = sha1_rol32(t, 1); |
106 | 0 | } |
107 | 0 | if (i < 20) { |
108 | 0 | t = (d ^ (b & (c ^ d))) + SHA1_K0; |
109 | 0 | } else if (i < 40) { |
110 | 0 | t = (b ^ c ^ d) + SHA1_K20; |
111 | 0 | } else if (i < 60) { |
112 | 0 | t = ((b & c) | (d & (b | c))) + SHA1_K40; |
113 | 0 | } else { |
114 | 0 | t = (b ^ c ^ d) + SHA1_K60; |
115 | 0 | } |
116 | 0 | t += sha1_rol32(a, 5) + e + s->buffer[i & 15]; |
117 | 0 | e = d; |
118 | 0 | d = c; |
119 | 0 | c = sha1_rol32(b, 30); |
120 | 0 | b = a; |
121 | 0 | a = t; |
122 | 0 | } |
123 | 0 | s->state[0] += a; |
124 | 0 | s->state[1] += b; |
125 | 0 | s->state[2] += c; |
126 | 0 | s->state[3] += d; |
127 | 0 | s->state[4] += e; |
128 | 0 | } |
129 | | |
130 | 0 | void sha1_addUncounted(sha1nfo *s, uint8_t data) { |
131 | 0 | uint8_t *const b = (uint8_t *)s->buffer; |
132 | | #ifdef SHA_BIG_ENDIAN |
133 | | b[s->bufferOffset] = data; |
134 | | #else |
135 | 0 | b[s->bufferOffset ^ 3] = data; |
136 | 0 | #endif |
137 | 0 | s->bufferOffset++; |
138 | 0 | if (s->bufferOffset == BLOCK_LENGTH) { |
139 | 0 | sha1_hashBlock(s); |
140 | 0 | s->bufferOffset = 0; |
141 | 0 | } |
142 | 0 | } |
143 | | |
144 | 0 | void sha1_writebyte(sha1nfo *s, uint8_t data) { |
145 | 0 | ++s->byteCount; |
146 | 0 | sha1_addUncounted(s, data); |
147 | 0 | } |
148 | | |
149 | 0 | void sha1_write(sha1nfo *s, const char *data, size_t len) { |
150 | 0 | for (; len--;) sha1_writebyte(s, (uint8_t)*data++); |
151 | 0 | } |
152 | | |
153 | 0 | void sha1_pad(sha1nfo *s) { |
154 | | // Implement SHA-1 padding (fips180-2 ยง5.1.1) |
155 | | |
156 | | // Pad with 0x80 followed by 0x00 until the end of the block |
157 | 0 | sha1_addUncounted(s, 0x80); |
158 | 0 | while (s->bufferOffset != 56) sha1_addUncounted(s, 0x00); |
159 | | |
160 | | // Append length in the last 8 bytes |
161 | 0 | sha1_addUncounted(s, 0); // We're only using 32 bit lengths |
162 | 0 | sha1_addUncounted(s, 0); // But SHA-1 supports 64 bit lengths |
163 | 0 | sha1_addUncounted(s, 0); // So zero pad the top bits |
164 | 0 | sha1_addUncounted(s, s->byteCount >> 29); // Shifting to multiply by 8 |
165 | 0 | sha1_addUncounted( |
166 | 0 | s, s->byteCount >> 21); // as SHA-1 supports bitstreams as well as |
167 | 0 | sha1_addUncounted(s, s->byteCount >> 13); // byte. |
168 | 0 | sha1_addUncounted(s, s->byteCount >> 5); |
169 | 0 | sha1_addUncounted(s, s->byteCount << 3); |
170 | 0 | } |
171 | | |
172 | 0 | uint8_t *sha1_result(sha1nfo *s) { |
173 | | // Pad to complete the last block |
174 | 0 | sha1_pad(s); |
175 | |
|
176 | 0 | #ifndef SHA_BIG_ENDIAN |
177 | | // Swap byte order back |
178 | 0 | int i; |
179 | 0 | for (i = 0; i < 5; i++) { |
180 | 0 | s->state[i] = (((s->state[i]) << 24) & 0xff000000) | |
181 | 0 | (((s->state[i]) << 8) & 0x00ff0000) | |
182 | 0 | (((s->state[i]) >> 8) & 0x0000ff00) | |
183 | 0 | (((s->state[i]) >> 24) & 0x000000ff); |
184 | 0 | } |
185 | 0 | #endif |
186 | | |
187 | | // Return pointer to hash (20 characters) |
188 | 0 | return (uint8_t *)s->state; |
189 | 0 | } |
190 | | |
191 | | static_assert(kShaDigestLength == HASH_LENGTH); |
192 | | |
193 | | } // namespace |
194 | | |
195 | | void SHA1(absl::Nonnull<const uint8_t *> data, size_t len, |
196 | 0 | absl::Nonnull<uint8_t *> out) { |
197 | 0 | sha1nfo s; |
198 | 0 | sha1_init(&s); |
199 | 0 | sha1_write(&s, reinterpret_cast<const char *>(data), len); |
200 | 0 | memcpy(out, sha1_result(&s), kShaDigestLength); |
201 | 0 | } |
202 | | |
203 | | } // namespace centipede |