1 // Copyright (C) 2024 Google LLC
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 "icing/util/sha256.h"
16
17 #include <array>
18 #include <cstdint>
19 #include <cstring>
20
21 namespace icing {
22 namespace lib {
23
24 // Constants for SHA-256 algorithm
25 constexpr uint32_t k[64] = {
26 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
27 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
28 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
29 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
30 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
31 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
32 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
33 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
34 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
35 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
36 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
37
38 constexpr uint8_t kPaddingFirstByte = 0x80;
39 constexpr uint8_t kNullChar = '\0';
40
41 // Function to perform a right rotation on a 32-bit value
RightRotate(uint32_t value,unsigned int count)42 uint32_t RightRotate(uint32_t value, unsigned int count) {
43 return value >> count | value << (32 - count);
44 }
45
Sha256()46 Sha256::Sha256() {
47 state_[0] = 0x6a09e667;
48 state_[1] = 0xbb67ae85;
49 state_[2] = 0x3c6ef372;
50 state_[3] = 0xa54ff53a;
51 state_[4] = 0x510e527f;
52 state_[5] = 0x9b05688c;
53 state_[6] = 0x1f83d9ab;
54 state_[7] = 0x5be0cd19;
55 count_ = 0;
56 memset(buffer_.data(), 0, sizeof(buffer_));
57 }
58
Transform()59 void Sha256::Transform() {
60 uint32_t w[64];
61 int t = 0;
62 // Process the first 16 words of the message block
63 for (; t < 16; ++t) {
64 uint32_t tmp = static_cast<uint32_t>(buffer_[t * 4]) << 24;
65 tmp |= static_cast<uint32_t>(buffer_[t * 4 + 1]) << 16;
66 tmp |= static_cast<uint32_t>(buffer_[t * 4 + 2]) << 8;
67 tmp |= static_cast<uint32_t>(buffer_[t * 4 + 3]);
68 w[t] = tmp;
69 }
70
71 // Extend the first 16 words into the remaining 48 words of the message
72 // schedule
73 for (; t < 64; t++) {
74 // Calculate the next word in the message schedule based on the previous
75 // words
76 uint32_t s0 = RightRotate(w[t - 15], 7) ^ RightRotate(w[t - 15], 18) ^
77 (w[t - 15] >> 3);
78 uint32_t s1 = RightRotate(w[t - 2], 17) ^ RightRotate(w[t - 2], 19) ^
79 (w[t - 2] >> 10);
80 w[t] = w[t - 16] + s0 + w[t - 7] + s1;
81 }
82
83 uint32_t a = state_[0];
84 uint32_t b = state_[1];
85 uint32_t c = state_[2];
86 uint32_t d = state_[3];
87 uint32_t e = state_[4];
88 uint32_t f = state_[5];
89 uint32_t g = state_[6];
90 uint32_t h = state_[7];
91
92 for (int i = 0; i < 64; i++) {
93 uint32_t sigma0 =
94 RightRotate(a, 2) ^ RightRotate(a, 13) ^ RightRotate(a, 22);
95 uint32_t majority = (a & b) ^ (a & c) ^ (b & c);
96 uint32_t temp2 = sigma0 + majority;
97 uint32_t sigma1 =
98 RightRotate(e, 6) ^ RightRotate(e, 11) ^ RightRotate(e, 25);
99 uint32_t choice = (e & f) ^ ((~e) & g);
100 uint32_t temp1 = h + sigma1 + choice + k[i] + w[i];
101
102 h = g;
103 g = f;
104 f = e;
105 e = d + temp1;
106 d = c;
107 c = b;
108 b = a;
109 a = temp1 + temp2;
110 }
111
112 state_[0] += a;
113 state_[1] += b;
114 state_[2] += c;
115 state_[3] += d;
116 state_[4] += e;
117 state_[5] += f;
118 state_[6] += g;
119 state_[7] += h;
120 }
121
Update(const uint8_t * data,size_t length)122 void Sha256::Update(const uint8_t* data, size_t length) {
123 int i = static_cast<int>(count_ & 0b111111);
124 count_ += length;
125 while (length--) {
126 buffer_[i] = *data;
127 ++data;
128 ++i;
129 if (i == 64) {
130 Transform();
131 i = 0;
132 }
133 }
134 }
135
Finalize()136 std::array<uint8_t, 32> Sha256::Finalize() && {
137 uint64_t bits_count = count_ << 3;
138
139 // SHA-256 padding: the message is padded with a '1' bit, then with '0' bits
140 // until the message length modulo 512 is 448 bits (56 bytes modulo 64).
141 Update(&kPaddingFirstByte, 1);
142
143 // Pad with '0' bits until the message length modulo 64 is 56 bytes, leaving
144 // 8 bytes for the length of the original message.
145 while (count_ % 64 != 56) {
146 Update(&kNullChar, 1);
147 }
148
149 // Append the length of the original message (in bits) to the end of the
150 // padded message in big-endian order.
151 for (int i = 0; i < 8; ++i) {
152 uint8_t tmp = static_cast<uint8_t>(bits_count >> 56);
153 bits_count <<= 8;
154 Update(&tmp, 1);
155 }
156
157 // Convert the state array to a 32-byte sha256 hash array in big-endian order.
158 std::array<uint8_t, 32> hash;
159 for (int i = 0, j = 0; i < 8; i++) {
160 uint32_t tmp = state_[i];
161 hash[j++] = static_cast<uint8_t>(tmp >> 24);
162 hash[j++] = static_cast<uint8_t>(tmp >> 16);
163 hash[j++] = static_cast<uint8_t>(tmp >> 8);
164 hash[j++] = static_cast<uint8_t>(tmp);
165 }
166
167 return hash;
168 }
169
170 } // namespace lib
171 } // namespace icing
172