|
| 1 | +/* |
| 2 | +The MIT License |
| 3 | +
|
| 4 | +Copyright (c) 2011 lyo.kato@gmail.com |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in |
| 14 | +all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +THE SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | +#pragma once |
| 26 | +#include <string> |
| 27 | +#include <cassert> |
| 28 | +#include <sstream> |
| 29 | +#include <iomanip> |
| 30 | +#include <cryptlite/base64.h> |
| 31 | +#include <boost/cstdint.hpp> |
| 32 | + |
| 33 | +namespace cryptlite { |
| 34 | + |
| 35 | +#define SHA2_SHFR(bits,word) ((word) >> (bits)) |
| 36 | +#define SHA2_ROTR(bits,word) (((word) >> (bits)) | ((word) << ((sizeof(word) << 3) - (bits)))) |
| 37 | +#define SHA2_CH(x, y, z) (((x) & (y)) ^ (~(x) & (z))) |
| 38 | +#define SHA2_MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) |
| 39 | +#define SHA512_F1(x) (SHA2_ROTR(28, x) ^ SHA2_ROTR(34, x) ^ SHA2_ROTR(39, x)) |
| 40 | +#define SHA512_F2(x) (SHA2_ROTR(14, x) ^ SHA2_ROTR(18, x) ^ SHA2_ROTR(41, x)) |
| 41 | +#define SHA512_F3(x) (SHA2_ROTR( 1, x) ^ SHA2_ROTR( 8, x) ^ SHA2_SHFR( 7, x)) |
| 42 | +#define SHA512_F4(x) (SHA2_ROTR(19, x) ^ SHA2_ROTR(61, x) ^ SHA2_SHFR( 6, x)) |
| 43 | +#define SHA2_UNPACK32(x, str) \ |
| 44 | +{ \ |
| 45 | + *((str) + 3) = static_cast<boost::uint8_t>((x) ); \ |
| 46 | + *((str) + 2) = static_cast<boost::uint8_t>((x) >> 8); \ |
| 47 | + *((str) + 1) = static_cast<boost::uint8_t>((x) >> 16); \ |
| 48 | + *((str) + 0) = static_cast<boost::uint8_t>((x) >> 24); \ |
| 49 | +} |
| 50 | +#define SHA2_UNPACK64(x, str) \ |
| 51 | +{ \ |
| 52 | + *((str) + 7) = static_cast<boost::uint8_t>((x) ); \ |
| 53 | + *((str) + 6) = static_cast<boost::uint8_t>((x) >> 8); \ |
| 54 | + *((str) + 5) = static_cast<boost::uint8_t>((x) >> 16); \ |
| 55 | + *((str) + 4) = static_cast<boost::uint8_t>((x) >> 24); \ |
| 56 | + *((str) + 3) = static_cast<boost::uint8_t>((x) >> 32); \ |
| 57 | + *((str) + 2) = static_cast<boost::uint8_t>((x) >> 40); \ |
| 58 | + *((str) + 1) = static_cast<boost::uint8_t>((x) >> 48); \ |
| 59 | + *((str) + 0) = static_cast<boost::uint8_t>((x) >> 56); \ |
| 60 | +} |
| 61 | +#define SHA2_PACK64(str, x) \ |
| 62 | +{ \ |
| 63 | + *(x) = static_cast<boost::uint64_t>(*((str) + 7) ) \ |
| 64 | + | static_cast<boost::uint64_t>(*((str) + 6) << 8) \ |
| 65 | + | static_cast<boost::uint64_t>(*((str) + 5) << 16) \ |
| 66 | + | static_cast<boost::uint64_t>(*((str) + 4) << 24) \ |
| 67 | + | static_cast<boost::uint64_t>(*((str) + 3) << 32) \ |
| 68 | + | static_cast<boost::uint64_t>(*((str) + 2) << 40) \ |
| 69 | + | static_cast<boost::uint64_t>(*((str) + 1) << 48) \ |
| 70 | + | static_cast<boost::uint64_t>(*((str) + 0) << 56); \ |
| 71 | +} |
| 72 | + |
| 73 | +class sha512 { |
| 74 | + |
| 75 | + public: |
| 76 | + |
| 77 | + static constexpr unsigned int BLOCK_SIZE = 128; |
| 78 | + static constexpr unsigned int HASH_SIZE = 64; |
| 79 | + static constexpr unsigned int HASH_SIZE_BITS = HASH_SIZE * 8; |
| 80 | + |
| 81 | + static void hash(const std::string& s, boost::uint8_t digest[HASH_SIZE]) |
| 82 | + { |
| 83 | + sha512 ctx; |
| 84 | + ctx.input(reinterpret_cast<const boost::uint8_t*>(s.c_str()), s.size()); |
| 85 | + ctx.result(digest); |
| 86 | + } |
| 87 | + |
| 88 | + static std::string hash_hex(const std::string& s) |
| 89 | + { |
| 90 | + int i; |
| 91 | + boost::uint8_t digest[HASH_SIZE]; |
| 92 | + std::ostringstream oss; |
| 93 | + oss << std::hex << std::setfill('0'); |
| 94 | + sha512 ctx; |
| 95 | + ctx.input(reinterpret_cast<const boost::uint8_t*>(s.c_str()), s.size()); |
| 96 | + ctx.result(digest); |
| 97 | + for (i = 0; i < HASH_SIZE; ++i) |
| 98 | + oss << std::setw(2) << (digest[i] & 0xff); |
| 99 | + oss << std::dec; |
| 100 | + return oss.str(); |
| 101 | + } |
| 102 | + |
| 103 | + static std::string hash_base64(const std::string& s) { |
| 104 | + boost::uint8_t digest[HASH_SIZE]; |
| 105 | + sha512 ctx; |
| 106 | + ctx.input(reinterpret_cast<const boost::uint8_t*>(s.c_str()), s.size()); |
| 107 | + ctx.result(digest); |
| 108 | + return base64::encode_from_array(digest, HASH_SIZE); |
| 109 | + } |
| 110 | + |
| 111 | + sha512() |
| 112 | + : computed_(false) |
| 113 | + , corrupted_(false) |
| 114 | + , len_(0) |
| 115 | + , tot_len_(0) |
| 116 | + { |
| 117 | + intermediate_hash_[0] = 0x6a09e667f3bcc908ULL; |
| 118 | + intermediate_hash_[1] = 0xbb67ae8584caa73bULL; |
| 119 | + intermediate_hash_[2] = 0x3c6ef372fe94f82bULL; |
| 120 | + intermediate_hash_[3] = 0xa54ff53a5f1d36f1ULL; |
| 121 | + intermediate_hash_[4] = 0x510e527fade682d1ULL; |
| 122 | + intermediate_hash_[5] = 0x9b05688c2b3e6c1fULL; |
| 123 | + intermediate_hash_[6] = 0x1f83d9abfb41bd6bULL; |
| 124 | + intermediate_hash_[7] = 0x5be0cd19137e2179ULL; |
| 125 | + } |
| 126 | + |
| 127 | + ~sha512() { } |
| 128 | + |
| 129 | + void reset() |
| 130 | + { |
| 131 | + computed_ = false; |
| 132 | + corrupted_ = false; |
| 133 | + len_ = 0; |
| 134 | + tot_len_ = 0; |
| 135 | + |
| 136 | + intermediate_hash_[0] = 0x6a09e667f3bcc908ULL; |
| 137 | + intermediate_hash_[1] = 0xbb67ae8584caa73bULL; |
| 138 | + intermediate_hash_[2] = 0x3c6ef372fe94f82bULL; |
| 139 | + intermediate_hash_[3] = 0xa54ff53a5f1d36f1ULL; |
| 140 | + intermediate_hash_[4] = 0x510e527fade682d1ULL; |
| 141 | + intermediate_hash_[5] = 0x9b05688c2b3e6c1fULL; |
| 142 | + intermediate_hash_[6] = 0x1f83d9abfb41bd6bULL; |
| 143 | + intermediate_hash_[7] = 0x5be0cd19137e2179ULL; |
| 144 | + } |
| 145 | + |
| 146 | + void input(const boost::uint8_t *message_array, unsigned int length) |
| 147 | + { |
| 148 | + assert(message_array); |
| 149 | + if (computed_ || corrupted_ || !length) |
| 150 | + return; |
| 151 | + |
| 152 | + unsigned int tmp_len = BLOCK_SIZE - len_; |
| 153 | + unsigned int rem_len = length < tmp_len ? length : tmp_len; |
| 154 | + memcpy(&message_block_[len_], message_array, rem_len); |
| 155 | + if (len_ + length < BLOCK_SIZE) { |
| 156 | + len_ += length; |
| 157 | + return; |
| 158 | + } |
| 159 | + unsigned int new_len = length - rem_len; |
| 160 | + unsigned int block_nb = new_len / BLOCK_SIZE; |
| 161 | + const boost::uint8_t *shifted_message = message_array + rem_len; |
| 162 | + transform(message_block_, 1); |
| 163 | + transform(shifted_message, block_nb); |
| 164 | + rem_len = new_len % BLOCK_SIZE; |
| 165 | + memcpy(message_block_, &shifted_message[block_nb << 7], rem_len); |
| 166 | + len_ = rem_len; |
| 167 | + tot_len_ += (block_nb + 1) << 7; |
| 168 | + } |
| 169 | + |
| 170 | + void result(boost::uint8_t digest[HASH_SIZE]) |
| 171 | + { |
| 172 | + assert(digest); |
| 173 | + if (corrupted_) |
| 174 | + return; |
| 175 | + if (!computed_) |
| 176 | + finalize(0x80); |
| 177 | + |
| 178 | + for (int i = 0; i < 8; i++) { |
| 179 | + SHA2_UNPACK64(intermediate_hash_[i], &digest[i << 3]); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | +private: |
| 184 | + boost::uint64_t intermediate_hash_[HASH_SIZE/8]; |
| 185 | + boost::uint32_t len_; |
| 186 | + boost::uint32_t tot_len_; |
| 187 | + boost::uint8_t message_block_[2 * BLOCK_SIZE]; |
| 188 | + bool computed_; |
| 189 | + bool corrupted_; |
| 190 | + |
| 191 | + void finalize(boost::uint8_t pad_byte) |
| 192 | + { |
| 193 | + unsigned int block_nb = 1 + ((BLOCK_SIZE - 17) < (len_ % BLOCK_SIZE)); |
| 194 | + unsigned int len_b = (tot_len_ + len_) << 3; |
| 195 | + unsigned int pm_len = block_nb << 7; |
| 196 | + memset(message_block_ + len_, 0, pm_len - len_); |
| 197 | + message_block_[len_] = pad_byte; |
| 198 | + SHA2_UNPACK32(len_b, message_block_ + pm_len - 4); |
| 199 | + transform(message_block_, block_nb); |
| 200 | + |
| 201 | + memset(message_block_, 0, sizeof(message_block_)); |
| 202 | + len_ = 0; |
| 203 | + tot_len_ = 0; |
| 204 | + computed_ = true; |
| 205 | + } |
| 206 | + |
| 207 | + void transform(const boost::uint8_t *message_array, unsigned int block_nb) |
| 208 | + { |
| 209 | + static const boost::uint64_t K[80] = { |
| 210 | + 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, |
| 211 | + 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, |
| 212 | + 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, |
| 213 | + 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, |
| 214 | + 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, |
| 215 | + 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, |
| 216 | + 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, |
| 217 | + 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, |
| 218 | + 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, |
| 219 | + 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, |
| 220 | + 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, |
| 221 | + 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, |
| 222 | + 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, |
| 223 | + 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, |
| 224 | + 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, |
| 225 | + 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, |
| 226 | + 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, |
| 227 | + 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, |
| 228 | + 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, |
| 229 | + 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, |
| 230 | + 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, |
| 231 | + 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, |
| 232 | + 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, |
| 233 | + 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, |
| 234 | + 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, |
| 235 | + 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, |
| 236 | + 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, |
| 237 | + 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, |
| 238 | + 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, |
| 239 | + 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, |
| 240 | + 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, |
| 241 | + 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, |
| 242 | + 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, |
| 243 | + 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, |
| 244 | + 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, |
| 245 | + 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, |
| 246 | + 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, |
| 247 | + 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, |
| 248 | + 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, |
| 249 | + 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL |
| 250 | + }; |
| 251 | + boost::uint64_t w[80]; |
| 252 | + boost::uint64_t wv[8]; |
| 253 | + boost::uint64_t t1, t2; |
| 254 | + int j; |
| 255 | + for (int i = 0; i < (int) block_nb; i++) { |
| 256 | + const boost::uint8_t *sub_block = message_array + (i << 7); |
| 257 | + for (j = 0; j < 16; j++) { |
| 258 | + SHA2_PACK64(&sub_block[j << 3], &w[j]); |
| 259 | + } |
| 260 | + for (j = 16; j < 80; j++) { |
| 261 | + w[j] = SHA512_F4(w[j - 2]) + w[j - 7] + SHA512_F3(w[j - 15]) + w[j - 16]; |
| 262 | + } |
| 263 | + for (j = 0; j < 8; j++) { |
| 264 | + wv[j] = intermediate_hash_[j]; |
| 265 | + } |
| 266 | + for (j = 0; j < 80; j++) { |
| 267 | + t1 = wv[7] + SHA512_F2(wv[4]) + SHA2_CH(wv[4], wv[5], wv[6]) |
| 268 | + + K[j] + w[j]; |
| 269 | + t2 = SHA512_F1(wv[0]) + SHA2_MAJ(wv[0], wv[1], wv[2]); |
| 270 | + wv[7] = wv[6]; |
| 271 | + wv[6] = wv[5]; |
| 272 | + wv[5] = wv[4]; |
| 273 | + wv[4] = wv[3] + t1; |
| 274 | + wv[3] = wv[2]; |
| 275 | + wv[2] = wv[1]; |
| 276 | + wv[1] = wv[0]; |
| 277 | + wv[0] = t1 + t2; |
| 278 | + } |
| 279 | + for (j = 0; j < 8; j++) { |
| 280 | + intermediate_hash_[j] += wv[j]; |
| 281 | + } |
| 282 | + } |
| 283 | + } |
| 284 | +}; // end of class |
| 285 | + |
| 286 | +} // end of namespace |
0 commit comments