| 6 |
| 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | #include <string.h> |
| 10 |
| 11 | #include "base/sys_byteorder.h" |
| 12 |
| 13 | namespace base { |
| 14 |
| 15 | // Implementation of SHA-1. Only handles data in byte-sized blocks, |
| 16 | // which simplifies the code a fair bit. |
| 17 |
| 18 | // Identifier names follow notation in FIPS PUB 180-3, where you'll |
| 19 | // also find a description of the algorithm: |
| 20 | // http://csrc.nist.gov/publications/fips/fips180-3/fips180-3_final.pdf |
| 21 |
| 22 | // Usage example: |
| 23 | // |
| 24 | // SecureHashAlgorithm sha; |
| 25 | // while(there is data to hash) |
| 26 | // sha.Update(moredata, size of data); |
| 27 | // sha.Final(); |
| 28 | // memcpy(somewhere, sha.Digest(), 20); |
| 29 | // |
| 30 | // to reuse the instance of sha, call sha.Init(); |
| 31 |
| 32 | class SecureHashAlgorithm { |
| 33 | public: |
| 34 | SecureHashAlgorithm() { Init(); } |
| 35 |
| 36 | static const int kDigestSizeBytes; |
| 37 |
| 38 | void Init(); |
| 39 | void Update(const void* data, size_t nbytes); |
| 40 | void Final(); |
| 41 |
| 42 | // 20 bytes of message digest. |
| 43 | const unsigned char* Digest() const { |
| 44 | return reinterpret_cast<const unsigned char*>(H); |
| 45 | } |
| 46 |
| 47 | private: |
| 48 | void Pad(); |
| 49 | void Process(); |
| 50 |
| 51 | uint32_t A, B, C, D, E; |
| 52 |
| 53 | uint32_t H[5]; |
| 54 |
| 55 | union { |
| 56 | uint32_t W[80]; |
| 57 | uint8_t M[64]; |
| 58 | }; |
| 59 |
| 60 | uint32_t cursor; |
| 61 | uint64_t l; |
| 62 | }; |
| 63 |
| 64 | static inline uint32_t f(uint32_t t, uint32_t B, uint32_t C, uint32_t D) { |
| 65 | if (t < 20) |
| 66 | return (B & C) | ((~B) & D); |
| 67 | if (t < 40) |
| 68 | return B ^ C ^ D; |
| 69 | if (t < 60) |
| 70 | return (B & C) | (B & D) | (C & D); |
| 71 | return B ^ C ^ D; |
| 72 | } |
| 73 |
| 74 | static inline uint32_t S(uint32_t n, uint32_t X) { |
| 75 | return (X << n) | (X >> (32 - n)); |
| 76 | } |
| 77 |
| 78 | static inline uint32_t K(uint32_t t) { |
| 79 | if (t < 20) |
| 80 | return 0x5a827999; |
| 81 | if (t < 40) |
| 82 | return 0x6ed9eba1; |
| 83 | if (t < 60) |
| 84 | return 0x8f1bbcdc; |
| 85 | return 0xca62c1d6; |
| 86 | } |
| 87 |
| 88 | const int SecureHashAlgorithm::kDigestSizeBytes = 20; |
| 89 |
| 90 | void SecureHashAlgorithm::Init() { |
| 91 | A = 0; |
| 92 | B = 0; |
| 93 | C = 0; |
| 94 | D = 0; |
| 95 | E = 0; |
| 96 | cursor = 0; |
| 97 | l = 0; |
| 98 | H[0] = 0x67452301; |
| 99 | H[1] = 0xefcdab89; |
| 100 | H[2] = 0x98badcfe; |
| 101 | H[3] = 0x10325476; |
| 102 | H[4] = 0xc3d2e1f0; |
| 103 | } |
| 104 |
| 105 | void SecureHashAlgorithm::Final() { |
| 106 | Pad(); |
| 107 | Process(); |
| 108 |
| 109 | for (auto& t : H) |
| 110 | t = ByteSwap(t); |
| 111 | } |
| 112 |
| 113 | void SecureHashAlgorithm::Update(const void* data, size_t nbytes) { |
| 114 | const uint8_t* d = reinterpret_cast<const uint8_t*>(data); |
| 115 | while (nbytes--) { |
| 116 | M[cursor++] = *d++; |
| 117 | if (cursor >= 64) |
| 118 | Process(); |
| 119 | l += 8; |
| 120 | } |
| 121 | } |
| 122 |
| 123 | void SecureHashAlgorithm::Pad() { |
| 124 | M[cursor++] = 0x80; |
| 125 |
| 126 | if (cursor > 64 - 8) { |
| 127 | // pad out to next block |
| 128 | while (cursor < 64) |
| 129 | M[cursor++] = 0; |
| 130 |
| 131 | Process(); |
| 132 | } |
| 133 |
| 134 | while (cursor < 64 - 8) |
| 135 | M[cursor++] = 0; |
| 136 |
| 137 | M[cursor++] = (l >> 56) & 0xff; |
| 138 | M[cursor++] = (l >> 48) & 0xff; |
| 139 | M[cursor++] = (l >> 40) & 0xff; |
| 140 | M[cursor++] = (l >> 32) & 0xff; |
| 141 | M[cursor++] = (l >> 24) & 0xff; |
| 142 | M[cursor++] = (l >> 16) & 0xff; |
| 143 | M[cursor++] = (l >> 8) & 0xff; |
| 144 | M[cursor++] = l & 0xff; |
| 145 | } |
| 146 |
| 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] = ByteSwap(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 |
| 190 | SHA1Digest SHA1HashSpan(span<const uint8_t> data) { |
| 191 | SHA1Digest hash; |
| 192 | SHA1HashBytes(data.data(), data.size(), hash.data()); |
| 193 | return hash; |
| 194 | } |
| 195 |
| 196 | std::string SHA1HashString(const std::string& str) { |
| 197 | char hash[SecureHashAlgorithm::kDigestSizeBytes]; |
| 198 | SHA1HashBytes(reinterpret_cast<const unsigned char*>(str.c_str()), |
| 199 | str.length(), reinterpret_cast<unsigned char*>(hash)); |
| 200 | return std::string(hash, SecureHashAlgorithm::kDigestSizeBytes); |
| 201 | } |
| 202 |
| 203 | void SHA1HashBytes(const unsigned char* data, size_t len, unsigned char* hash) { |
| 204 | SecureHashAlgorithm sha; |
| 205 | sha.Update(data, len); |
| 206 | sha.Final(); |
| 207 |
| 208 | memcpy(hash, sha.Digest(), SecureHashAlgorithm::kDigestSizeBytes); |
| 209 | } |
| 210 |
| 211 | } // namespace base |
| 212 |