xref: /aosp_15_r20/external/perfetto/src/base/http/sha1.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "perfetto/ext/base/http/sha1.h"
17 
18 #include <stddef.h>
19 #include <string.h>
20 
21 // From chrome_elf/sha1/sha1.cc.
22 
23 namespace perfetto {
24 namespace base {
25 
26 namespace {
27 
BSwap32(uint32_t x)28 inline uint32_t BSwap32(uint32_t x) {
29 #if defined(__GNUC__)
30   return __builtin_bswap32(x);
31 #elif defined(_MSC_VER)
32   return _byteswap_ulong(x);
33 #else
34   return (((x & 0xff000000u) >> 24) | ((x & 0x00ff0000u) >> 8) |
35           ((x & 0x0000ff00u) << 8) | ((x & 0x000000ffu) << 24));
36 #endif
37 }
38 
39 // Usage example:
40 //
41 // SecureHashAlgorithm sha;
42 // while(there is data to hash)
43 //   sha.Update(moredata, size of data);
44 // sha.Final();
45 // memcpy(somewhere, sha.Digest(), 20);
46 //
47 // to reuse the instance of sha, call sha.Init();
48 class SecureHashAlgorithm {
49  public:
SecureHashAlgorithm()50   SecureHashAlgorithm() { Init(); }
51 
52   void Init();
53   void Update(const void* data, size_t nbytes);
54   void Final();
55 
56   // 20 bytes of message digest.
Digest() const57   const unsigned char* Digest() const {
58     return reinterpret_cast<const unsigned char*>(H);
59   }
60 
61  private:
62   void Pad();
63   void Process();
64 
65   uint32_t A, B, C, D, E;
66 
67   uint32_t H[5];
68 
69   union {
70     uint32_t W[80];
71     uint8_t M[64];
72   };
73 
74   uint32_t cursor;
75   uint64_t l;
76 };
77 
78 //------------------------------------------------------------------------------
79 // Private functions
80 //------------------------------------------------------------------------------
81 
82 // Identifier names follow notation in FIPS PUB 180-3, where you'll
83 // also find a description of the algorithm:
84 // http://csrc.nist.gov/publications/fips/fips180-3/fips180-3_final.pdf
85 
f(uint32_t t,uint32_t B,uint32_t C,uint32_t D)86 inline uint32_t f(uint32_t t, uint32_t B, uint32_t C, uint32_t D) {
87   if (t < 20) {
88     return (B & C) | ((~B) & D);
89   } else if (t < 40) {
90     return B ^ C ^ D;
91   } else if (t < 60) {
92     return (B & C) | (B & D) | (C & D);
93   } else {
94     return B ^ C ^ D;
95   }
96 }
97 
S(uint32_t n,uint32_t X)98 inline uint32_t S(uint32_t n, uint32_t X) {
99   return (X << n) | (X >> (32 - n));
100 }
101 
K(uint32_t t)102 inline uint32_t K(uint32_t t) {
103   if (t < 20) {
104     return 0x5a827999;
105   } else if (t < 40) {
106     return 0x6ed9eba1;
107   } else if (t < 60) {
108     return 0x8f1bbcdc;
109   } else {
110     return 0xca62c1d6;
111   }
112 }
113 
Init()114 void SecureHashAlgorithm::Init() {
115   A = 0;
116   B = 0;
117   C = 0;
118   D = 0;
119   E = 0;
120   cursor = 0;
121   l = 0;
122   H[0] = 0x67452301;
123   H[1] = 0xefcdab89;
124   H[2] = 0x98badcfe;
125   H[3] = 0x10325476;
126   H[4] = 0xc3d2e1f0;
127 }
128 
Update(const void * data,size_t nbytes)129 void SecureHashAlgorithm::Update(const void* data, size_t nbytes) {
130   const uint8_t* d = reinterpret_cast<const uint8_t*>(data);
131   while (nbytes--) {
132     M[cursor++] = *d++;
133     if (cursor >= 64)
134       Process();
135     l += 8;
136   }
137 }
138 
Final()139 void SecureHashAlgorithm::Final() {
140   Pad();
141   Process();
142 
143   for (size_t t = 0; t < 5; ++t)
144     H[t] = BSwap32(H[t]);
145 }
146 
Process()147 void SecureHashAlgorithm::Process() {
148   uint32_t t;
149 
150   // Each a...e corresponds to a section in the FIPS 180-3 algorithm.
151 
152   // a.
153   //
154   // W and M are in a union, so no need to memcpy.
155   // memcpy(W, M, sizeof(M));
156   for (t = 0; t < 16; ++t)
157     W[t] = BSwap32(W[t]);
158 
159   // b.
160   for (t = 16; t < 80; ++t)
161     W[t] = S(1, W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]);
162 
163   // c.
164   A = H[0];
165   B = H[1];
166   C = H[2];
167   D = H[3];
168   E = H[4];
169 
170   // d.
171   for (t = 0; t < 80; ++t) {
172     uint32_t TEMP = S(5, A) + f(t, B, C, D) + E + W[t] + K(t);
173     E = D;
174     D = C;
175     C = S(30, B);
176     B = A;
177     A = TEMP;
178   }
179 
180   // e.
181   H[0] += A;
182   H[1] += B;
183   H[2] += C;
184   H[3] += D;
185   H[4] += E;
186 
187   cursor = 0;
188 }
189 
Pad()190 void SecureHashAlgorithm::Pad() {
191   M[cursor++] = 0x80;
192 
193   if (cursor > 64 - 8) {
194     // pad out to next block
195     while (cursor < 64)
196       M[cursor++] = 0;
197 
198     Process();
199   }
200 
201   while (cursor < 64 - 8)
202     M[cursor++] = 0;
203 
204   M[cursor++] = (l >> 56) & 0xff;
205   M[cursor++] = (l >> 48) & 0xff;
206   M[cursor++] = (l >> 40) & 0xff;
207   M[cursor++] = (l >> 32) & 0xff;
208   M[cursor++] = (l >> 24) & 0xff;
209   M[cursor++] = (l >> 16) & 0xff;
210   M[cursor++] = (l >> 8) & 0xff;
211   M[cursor++] = l & 0xff;
212 }
213 
214 // Computes the SHA-1 hash of the |len| bytes in |data| and puts the hash
215 // in |hash|. |hash| must be kSHA1Length bytes long.
SHA1HashBytes(const unsigned char * data,size_t len,unsigned char * hash)216 void SHA1HashBytes(const unsigned char* data, size_t len, unsigned char* hash) {
217   SecureHashAlgorithm sha;
218   sha.Update(data, len);
219   sha.Final();
220 
221   ::memcpy(hash, sha.Digest(), kSHA1Length);
222 }
223 
224 }  // namespace
225 
226 //------------------------------------------------------------------------------
227 // Public functions
228 //------------------------------------------------------------------------------
SHA1Hash(const void * data,size_t size)229 SHA1Digest SHA1Hash(const void* data, size_t size) {
230   SHA1Digest digest;
231   SHA1HashBytes(static_cast<const unsigned char*>(data), size,
232                 reinterpret_cast<unsigned char*>(&digest[0]));
233   return digest;
234 }
235 
SHA1Hash(const std::string & str)236 SHA1Digest SHA1Hash(const std::string& str) {
237   return SHA1Hash(str.data(), str.size());
238 }
239 
240 }  // namespace base
241 }  // namespace perfetto
242