1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
| // Copyright (c) Microsoft Corporation. All rights reserved.
| // Licensed under the MIT License.
|
| #include "core/platform/telemetry_sha256.h"
|
| #include <algorithm>
| #include <cstring>
|
| namespace onnxruntime::telemetry_internal {
|
| namespace {
|
| constexpr uint32_t kInitialState[8] = {
| 0x6a09e667u,
| 0xbb67ae85u,
| 0x3c6ef372u,
| 0xa54ff53au,
| 0x510e527fu,
| 0x9b05688cu,
| 0x1f83d9abu,
| 0x5be0cd19u,
| };
|
| constexpr uint32_t kRoundConstants[64] = {
| 0x428a2f98u,
| 0x71374491u,
| 0xb5c0fbcfu,
| 0xe9b5dba5u,
| 0x3956c25bu,
| 0x59f111f1u,
| 0x923f82a4u,
| 0xab1c5ed5u,
| 0xd807aa98u,
| 0x12835b01u,
| 0x243185beu,
| 0x550c7dc3u,
| 0x72be5d74u,
| 0x80deb1feu,
| 0x9bdc06a7u,
| 0xc19bf174u,
| 0xe49b69c1u,
| 0xefbe4786u,
| 0x0fc19dc6u,
| 0x240ca1ccu,
| 0x2de92c6fu,
| 0x4a7484aau,
| 0x5cb0a9dcu,
| 0x76f988dau,
| 0x983e5152u,
| 0xa831c66du,
| 0xb00327c8u,
| 0xbf597fc7u,
| 0xc6e00bf3u,
| 0xd5a79147u,
| 0x06ca6351u,
| 0x14292967u,
| 0x27b70a85u,
| 0x2e1b2138u,
| 0x4d2c6dfcu,
| 0x53380d13u,
| 0x650a7354u,
| 0x766a0abbu,
| 0x81c2c92eu,
| 0x92722c85u,
| 0xa2bfe8a1u,
| 0xa81a664bu,
| 0xc24b8b70u,
| 0xc76c51a3u,
| 0xd192e819u,
| 0xd6990624u,
| 0xf40e3585u,
| 0x106aa070u,
| 0x19a4c116u,
| 0x1e376c08u,
| 0x2748774cu,
| 0x34b0bcb5u,
| 0x391c0cb3u,
| 0x4ed8aa4au,
| 0x5b9cca4fu,
| 0x682e6ff3u,
| 0x748f82eeu,
| 0x78a5636fu,
| 0x84c87814u,
| 0x8cc70208u,
| 0x90befffau,
| 0xa4506cebu,
| 0xbef9a3f7u,
| 0xc67178f2u,
| };
|
| uint32_t RotateRight(uint32_t value, int bits) {
| return (value >> bits) | (value << (32 - bits));
| }
|
| uint32_t Choose(uint32_t x, uint32_t y, uint32_t z) {
| return (x & y) ^ (~x & z);
| }
|
| uint32_t Majority(uint32_t x, uint32_t y, uint32_t z) {
| return (x & y) ^ (x & z) ^ (y & z);
| }
|
| uint32_t BigSigma0(uint32_t value) {
| return RotateRight(value, 2) ^ RotateRight(value, 13) ^ RotateRight(value, 22);
| }
|
| uint32_t BigSigma1(uint32_t value) {
| return RotateRight(value, 6) ^ RotateRight(value, 11) ^ RotateRight(value, 25);
| }
|
| uint32_t SmallSigma0(uint32_t value) {
| return RotateRight(value, 7) ^ RotateRight(value, 18) ^ (value >> 3);
| }
|
| uint32_t SmallSigma1(uint32_t value) {
| return RotateRight(value, 17) ^ RotateRight(value, 19) ^ (value >> 10);
| }
|
| } // namespace
|
| Sha256::Sha256() {
| std::memcpy(state_, kInitialState, sizeof(state_));
| }
|
| void Sha256::Transform(const uint8_t block[64]) {
| uint32_t words[64];
| for (int i = 0; i < 16; ++i) {
| words[i] = (static_cast<uint32_t>(block[i * 4]) << 24) |
| (static_cast<uint32_t>(block[i * 4 + 1]) << 16) |
| (static_cast<uint32_t>(block[i * 4 + 2]) << 8) |
| static_cast<uint32_t>(block[i * 4 + 3]);
| }
| for (int i = 16; i < 64; ++i) {
| words[i] = SmallSigma1(words[i - 2]) + words[i - 7] + SmallSigma0(words[i - 15]) + words[i - 16];
| }
|
| uint32_t a = state_[0];
| uint32_t b = state_[1];
| uint32_t c = state_[2];
| uint32_t d = state_[3];
| uint32_t e = state_[4];
| uint32_t f = state_[5];
| uint32_t g = state_[6];
| uint32_t h = state_[7];
| for (int i = 0; i < 64; ++i) {
| const uint32_t first = h + BigSigma1(e) + Choose(e, f, g) + kRoundConstants[i] + words[i];
| const uint32_t second = BigSigma0(a) + Majority(a, b, c);
| h = g;
| g = f;
| f = e;
| e = d + first;
| d = c;
| c = b;
| b = a;
| a = first + second;
| }
|
| state_[0] += a;
| state_[1] += b;
| state_[2] += c;
| state_[3] += d;
| state_[4] += e;
| state_[5] += f;
| state_[6] += g;
| state_[7] += h;
| }
|
| void Sha256::Update(const void* data, size_t length) {
| const auto* bytes = static_cast<const uint8_t*>(data);
| bit_count_ += static_cast<uint64_t>(length) * 8;
| while (length > 0) {
| const size_t count = std::min<size_t>(64 - buffer_length_, length);
| std::memcpy(buffer_ + buffer_length_, bytes, count);
| buffer_length_ += count;
| bytes += count;
| length -= count;
| if (buffer_length_ == 64) {
| Transform(buffer_);
| buffer_length_ = 0;
| }
| }
| }
|
| void Sha256::Final(uint8_t output[kDigestSize]) {
| buffer_[buffer_length_++] = 0x80;
| if (buffer_length_ > 56) {
| std::memset(buffer_ + buffer_length_, 0, 64 - buffer_length_);
| Transform(buffer_);
| buffer_length_ = 0;
| }
| std::memset(buffer_ + buffer_length_, 0, 56 - buffer_length_);
| uint64_t bit_count = bit_count_;
| for (int i = 7; i >= 0; --i) {
| buffer_[56 + i] = static_cast<uint8_t>(bit_count & 0xff);
| bit_count >>= 8;
| }
| Transform(buffer_);
|
| for (int i = 0; i < 8; ++i) {
| output[i * 4] = static_cast<uint8_t>((state_[i] >> 24) & 0xff);
| output[i * 4 + 1] = static_cast<uint8_t>((state_[i] >> 16) & 0xff);
| output[i * 4 + 2] = static_cast<uint8_t>((state_[i] >> 8) & 0xff);
| output[i * 4 + 3] = static_cast<uint8_t>(state_[i] & 0xff);
| }
| }
|
| std::string Sha256::FinalHex() {
| static constexpr char kHex[] = "0123456789ABCDEF";
| uint8_t output[kDigestSize];
| Final(output);
|
| std::string result(kDigestSize * 2, '0');
| for (size_t i = 0; i < kDigestSize; ++i) {
| result[i * 2] = kHex[(output[i] >> 4) & 0x0f];
| result[i * 2 + 1] = kHex[output[i] & 0x0f];
| }
| return result;
| }
|
| std::string Sha256::HashStringHex(std::string_view value) {
| Sha256 hash;
| hash.Update(value.data(), value.size());
| return hash.FinalHex();
| }
|
| } // namespace onnxruntime::telemetry_internal
|
|