This source file includes following definitions.
- ByteVectorAsStringPiece
- Concat
- HMAC_Rotate
- HMAC_Rehash
- HMAC_DRBG_Update
- ByteVectorOr
- ByteVectorMerge
- CountBits
- GetRandomByteVector
- GetWeightedRandomByteVector
- generated_bytes_
- generated_bytes_
- GenerateEntropyInput
- GetRandomByteVector
#include "components/rappor/byte_vector_utils.h"
#include <string>
#include "base/logging.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "crypto/random.h"
namespace rappor {
namespace {
base::StringPiece ByteVectorAsStringPiece(const ByteVector& lhs) {
return base::StringPiece(reinterpret_cast<const char *>(&lhs[0]), lhs.size());
}
std::string Concat(const ByteVector& value, char c, const std::string& data) {
return std::string(value.begin(), value.end()) + c + data;
}
bool HMAC_Rotate(const crypto::HMAC& hmac,
const std::string& data,
crypto::HMAC* result) {
ByteVector key(hmac.DigestLength());
if (!hmac.Sign(data, &key[0], key.size()))
return false;
return result->Init(ByteVectorAsStringPiece(key));
}
bool HMAC_Rehash(const crypto::HMAC& hmac, ByteVector* value) {
return hmac.Sign(ByteVectorAsStringPiece(*value),
&(*value)[0], value->size());
}
bool HMAC_DRBG_Update(const std::string& provided_data,
const crypto::HMAC& hmac1,
ByteVector* value,
crypto::HMAC* out_hmac) {
crypto::HMAC temp_hmac(crypto::HMAC::SHA256);
crypto::HMAC* hmac2 = provided_data.size() > 0 ? &temp_hmac : out_hmac;
if (!HMAC_Rotate(hmac1, Concat(*value, 0x00, provided_data), hmac2))
return false;
if (!HMAC_Rehash(*hmac2, value))
return false;
if (hmac2 == out_hmac)
return true;
if (!HMAC_Rotate(*hmac2, Concat(*value, 0x01, provided_data), out_hmac))
return false;
return HMAC_Rehash(*out_hmac, value);
}
}
ByteVector* ByteVectorOr(const ByteVector& lhs, ByteVector* rhs) {
DCHECK_EQ(lhs.size(), rhs->size());
for (size_t i = 0, len = lhs.size(); i < len; ++i) {
(*rhs)[i] = lhs[i] | (*rhs)[i];
}
return rhs;
}
ByteVector* ByteVectorMerge(const ByteVector& mask,
const ByteVector& lhs,
ByteVector* rhs) {
DCHECK_EQ(lhs.size(), rhs->size());
for (size_t i = 0, len = lhs.size(); i < len; ++i) {
(*rhs)[i] = (lhs[i] & ~mask[i]) | ((*rhs)[i] & mask[i]);
}
return rhs;
}
int CountBits(const ByteVector& vector) {
int bit_count = 0;
for (size_t i = 0; i < vector.size(); ++i) {
uint8_t byte = vector[i];
for (int j = 0; j < 8 ; ++j) {
if (byte & (1 << j))
bit_count++;
}
}
return bit_count;
}
ByteVectorGenerator::ByteVectorGenerator(size_t byte_count)
: byte_count_(byte_count) {}
ByteVectorGenerator::~ByteVectorGenerator() {}
ByteVector ByteVectorGenerator::GetRandomByteVector() {
ByteVector bytes(byte_count_);
crypto::RandBytes(&bytes[0], bytes.size());
return bytes;
}
ByteVector ByteVectorGenerator::GetWeightedRandomByteVector(
Probability probability) {
ByteVector bytes = GetRandomByteVector();
switch (probability) {
case PROBABILITY_75:
return *ByteVectorOr(GetRandomByteVector(), &bytes);
case PROBABILITY_50:
return bytes;
}
NOTREACHED();
return bytes;
}
HmacByteVectorGenerator::HmacByteVectorGenerator(
size_t byte_count,
const std::string& entropy_input,
const std::string& personalization_string)
: ByteVectorGenerator(byte_count),
hmac_(crypto::HMAC::SHA256),
value_(hmac_.DigestLength(), 0x01),
generated_bytes_(0) {
DCHECK_EQ(kEntropyInputSize, entropy_input.size());
std::string seed_material(entropy_input + personalization_string);
crypto::HMAC hmac1(crypto::HMAC::SHA256);
if (!hmac1.Init(std::string(hmac_.DigestLength(), 0x00)))
NOTREACHED();
if (!HMAC_DRBG_Update(seed_material, hmac1, &value_, &hmac_))
NOTREACHED();
}
HmacByteVectorGenerator::~HmacByteVectorGenerator() {}
HmacByteVectorGenerator::HmacByteVectorGenerator(
const HmacByteVectorGenerator& prev_request)
: ByteVectorGenerator(prev_request.byte_count()),
hmac_(crypto::HMAC::SHA256),
value_(prev_request.value_),
generated_bytes_(0) {
if (!HMAC_DRBG_Update("", prev_request.hmac_, &value_, &hmac_))
NOTREACHED();
}
const size_t HmacByteVectorGenerator::kEntropyInputSize = (256 / 8) * 3 / 2;
std::string HmacByteVectorGenerator::GenerateEntropyInput() {
return base::RandBytesAsString(kEntropyInputSize);
}
ByteVector HmacByteVectorGenerator::GetRandomByteVector() {
const size_t digest_length = hmac_.DigestLength();
DCHECK_EQ(value_.size(), digest_length);
ByteVector bytes(byte_count());
uint8_t* data = &bytes[0];
size_t bytes_to_go = byte_count();
while (bytes_to_go > 0) {
size_t requested_byte_in_digest = generated_bytes_ % digest_length;
if (requested_byte_in_digest == 0) {
if (!HMAC_Rehash(hmac_, &value_))
NOTREACHED();
}
size_t n = std::min(bytes_to_go,
digest_length - requested_byte_in_digest);
memcpy(data, &value_[requested_byte_in_digest], n);
data += n;
bytes_to_go -= n;
generated_bytes_ += n;
DCHECK_LT(generated_bytes_, 1U << 16);
}
return bytes;
}
}