|
| 1 | +#ifndef HMAC_CPP_SECRET_HPP_INCLUDED |
| 2 | +#define HMAC_CPP_SECRET_HPP_INCLUDED |
| 3 | + |
| 4 | +#include <vector> |
| 5 | +#include <array> |
| 6 | +#include <string> |
| 7 | +#include <cstdint> |
| 8 | +#include <stdexcept> |
| 9 | +#include <algorithm> |
| 10 | +#include <functional> |
| 11 | +#include <cstring> |
| 12 | + |
| 13 | +#include "hmac_cpp/hmac.hpp" |
| 14 | +#include "hmac_cpp/hmac_utils.hpp" |
| 15 | +#include "hmac_cpp/secure_buffer.hpp" |
| 16 | +#include "hmac_cpp/memlock.hpp" |
| 17 | + |
| 18 | +namespace hmac_cpp { |
| 19 | + |
| 20 | +class secret_string { |
| 21 | +public: |
| 22 | + secret_string() : nonce_(), locked_(false) {} |
| 23 | + |
| 24 | + explicit secret_string(const std::string& s) : nonce_(), locked_(false) { set(s); } |
| 25 | + explicit secret_string(const uint8_t* p, size_t n) : nonce_(), locked_(false) { set(p, n); } |
| 26 | + |
| 27 | + secret_string(secret_string&& other) noexcept { move_from(other); } |
| 28 | + secret_string& operator=(secret_string&& other) noexcept { |
| 29 | + if (this != &other) { clear(); move_from(other); } |
| 30 | + return *this; |
| 31 | + } |
| 32 | + |
| 33 | + secret_string(const secret_string&) = delete; |
| 34 | + secret_string& operator=(const secret_string&) = delete; |
| 35 | + |
| 36 | + ~secret_string() { clear(); } |
| 37 | + |
| 38 | + void clear() noexcept { |
| 39 | + if (!ct_.empty()) { |
| 40 | + secure_zero(ct_.data(), ct_.size()); |
| 41 | + if (locked_) { |
| 42 | + unlock_pages(ct_.data(), ct_.size()); |
| 43 | + locked_ = false; |
| 44 | + } |
| 45 | + ct_.clear(); |
| 46 | + ct_.shrink_to_fit(); |
| 47 | + } |
| 48 | + secure_zero(nonce_.data(), nonce_.size()); |
| 49 | + } |
| 50 | + |
| 51 | + bool empty() const noexcept { return ct_.empty(); } |
| 52 | + size_t size() const noexcept { return ct_.size(); } |
| 53 | + |
| 54 | + void set(const std::string& s) { set(reinterpret_cast<const uint8_t*>(s.data()), s.size()); } |
| 55 | + |
| 56 | + void set(const uint8_t* p, size_t n) { |
| 57 | + if (n > 0 && p == NULL) throw std::invalid_argument("secret_string::set: null data with non-zero length"); |
| 58 | + clear(); |
| 59 | + |
| 60 | + std::vector<uint8_t> rnd = hmac_cpp::random_bytes(12); |
| 61 | + std::copy(rnd.begin(), rnd.end(), nonce_.begin()); |
| 62 | + |
| 63 | + ct_.assign(p, p + n); |
| 64 | + |
| 65 | + if (!ct_.empty()) { |
| 66 | + locked_ = lock_pages(ct_.data(), ct_.size()); |
| 67 | + } |
| 68 | + |
| 69 | + xor_keystream_inplace(ct_.data(), ct_.size(), nonce_.data()); |
| 70 | + } |
| 71 | + |
| 72 | + bool with_plaintext(const std::function<void(const uint8_t*, size_t)>& fn) const { |
| 73 | + std::vector<uint8_t> subkey = hmac_cpp::get_hmac(process_key().data(), process_key().size(), |
| 74 | + nonce_.data(), nonce_.size(), |
| 75 | + hmac_cpp::TypeHash::SHA256); |
| 76 | + std::vector<uint8_t> tmp(ct_); |
| 77 | + PageLockGuard g1(tmp.data(), tmp.size()); |
| 78 | + PageLockGuard g2(subkey.data(), subkey.size()); |
| 79 | + xor_keystream_inplace_with_key(tmp.data(), tmp.size(), nonce_.data(), subkey.data(), subkey.size()); |
| 80 | + fn(tmp.data(), tmp.size()); |
| 81 | + secure_zero(tmp.data(), tmp.size()); |
| 82 | + secure_zero(subkey.data(), subkey.size()); |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + std::string reveal_copy() const { |
| 87 | + std::string out; |
| 88 | + out.resize(ct_.size()); |
| 89 | + with_plaintext([&](const uint8_t* p, size_t n) { |
| 90 | + if (n) std::memcpy(&out[0], p, n); |
| 91 | + }); |
| 92 | + return out; |
| 93 | + } |
| 94 | + |
| 95 | +private: |
| 96 | + static std::array<uint8_t,32>& process_key() { |
| 97 | + static std::array<uint8_t,32> k = []{ |
| 98 | + std::array<uint8_t,32> tmp{}; |
| 99 | + std::vector<uint8_t> rnd = hmac_cpp::random_bytes(32); |
| 100 | + std::copy(rnd.begin(), rnd.end(), tmp.begin()); |
| 101 | + (void)lock_pages(tmp.data(), tmp.size()); |
| 102 | + return tmp; |
| 103 | + }(); |
| 104 | + return k; |
| 105 | + } |
| 106 | + |
| 107 | + static void be32(uint8_t out[4], uint32_t v) { |
| 108 | + out[0] = static_cast<uint8_t>((v >> 24) & 0xFF); |
| 109 | + out[1] = static_cast<uint8_t>((v >> 16) & 0xFF); |
| 110 | + out[2] = static_cast<uint8_t>((v >> 8) & 0xFF); |
| 111 | + out[3] = static_cast<uint8_t>( v & 0xFF); |
| 112 | + } |
| 113 | + |
| 114 | + static void xor_keystream_inplace_with_key(uint8_t* buf, size_t len, |
| 115 | + const uint8_t* nonce12, |
| 116 | + const uint8_t* subkey, size_t sublen) { |
| 117 | + if (!buf && len) return; |
| 118 | + if (!nonce12) return; |
| 119 | + if (!subkey || sublen != 32) return; |
| 120 | + |
| 121 | + uint8_t msg[16]; |
| 122 | + std::memcpy(msg, nonce12, 12); |
| 123 | + |
| 124 | + size_t pos = 0; |
| 125 | + uint32_t ctr = 0; |
| 126 | + while (pos < len) { |
| 127 | + be32(msg + 12, ctr++); |
| 128 | + std::vector<uint8_t> block = hmac_cpp::get_hmac(subkey, 32, msg, sizeof(msg), |
| 129 | + hmac_cpp::TypeHash::SHA256); |
| 130 | + const size_t take = (len - pos < block.size()) ? (len - pos) : block.size(); |
| 131 | + for (size_t i = 0; i < take; ++i) buf[pos + i] ^= block[i]; |
| 132 | + pos += take; |
| 133 | + secure_zero(block.data(), block.size()); |
| 134 | + } |
| 135 | + secure_zero(msg, sizeof(msg)); |
| 136 | + } |
| 137 | + |
| 138 | + void xor_keystream_inplace(uint8_t* buf, size_t len, const uint8_t* nonce12) const { |
| 139 | + std::vector<uint8_t> subkey = hmac_cpp::get_hmac(process_key().data(), process_key().size(), |
| 140 | + nonce12, 12, hmac_cpp::TypeHash::SHA256); |
| 141 | + xor_keystream_inplace_with_key(buf, len, nonce12, subkey.data(), subkey.size()); |
| 142 | + secure_zero(subkey.data(), subkey.size()); |
| 143 | + } |
| 144 | + |
| 145 | + void move_from(secret_string& other) noexcept { |
| 146 | + ct_ = std::move(other.ct_); |
| 147 | + nonce_ = other.nonce_; |
| 148 | + locked_ = other.locked_; |
| 149 | + other.locked_ = false; |
| 150 | + secure_zero(other.nonce_.data(), other.nonce_.size()); |
| 151 | + } |
| 152 | + |
| 153 | +private: |
| 154 | + std::vector<uint8_t> ct_; |
| 155 | + std::array<uint8_t,12> nonce_; |
| 156 | + bool locked_; |
| 157 | +}; |
| 158 | + |
| 159 | +} // namespace hmac_cpp |
| 160 | + |
| 161 | +#endif // HMAC_CPP_SECRET_HPP_INCLUDED |
0 commit comments