Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/rappor/byte_vector_utils.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 #include "crypto/random.h" | |
| 12 | |
| 13 namespace rappor { | |
| 14 | |
| 15 ByteVector* ByteVectorOr(const ByteVector& lhs, ByteVector* rhs) { | |
| 16 DCHECK_EQ(lhs.size(), rhs->size()); | |
| 17 for (size_t i = 0, len = lhs.size(); i < len; ++i) { | |
| 18 (*rhs)[i] = lhs[i] | (*rhs)[i]; | |
| 19 } | |
| 20 return rhs; | |
| 21 } | |
| 22 | |
| 23 ByteVector* ByteVectorMerge(const ByteVector& mask, | |
| 24 const ByteVector& lhs, | |
| 25 ByteVector* rhs) { | |
| 26 DCHECK_EQ(lhs.size(), rhs->size()); | |
| 27 for (size_t i = 0, len = lhs.size(); i < len; ++i) { | |
| 28 (*rhs)[i] = (lhs[i] & ~mask[i]) | ((*rhs)[i] & mask[i]); | |
| 29 } | |
| 30 return rhs; | |
| 31 } | |
| 32 | |
| 33 ByteVectorGenerator::ByteVectorGenerator(size_t byte_count) | |
| 34 : byte_count_(byte_count) {} | |
| 35 | |
| 36 ByteVectorGenerator::~ByteVectorGenerator() {} | |
| 37 | |
| 38 uint8_t ByteVectorGenerator::RandByte() { | |
| 39 uint8_t random_bits; | |
| 40 crypto::RandBytes(&random_bits, sizeof(uint8_t)); | |
| 41 return random_bits; | |
| 42 } | |
| 43 | |
| 44 ByteVector ByteVectorGenerator::GetRandomByteVector() { | |
| 45 ByteVector bytes(byte_count_); | |
| 46 for (size_t i = 0; i < byte_count_; ++i) { | |
| 47 bytes[i] = RandByte(); | |
| 48 } | |
| 49 return bytes; | |
| 50 } | |
| 51 | |
| 52 ByteVector ByteVectorGenerator::GetWeightedRandomByteVector( | |
| 53 Probability probability) { | |
| 54 ByteVector bytes = GetRandomByteVector(); | |
| 55 switch (probability) { | |
| 56 case PROBABILITY_75: | |
| 57 return *ByteVectorOr(GetRandomByteVector(), &bytes); | |
| 58 case PROBABILITY_50: | |
| 59 return bytes; | |
| 60 } | |
| 61 NOTREACHED(); | |
| 62 return bytes; | |
| 63 } | |
| 64 | |
| 65 HmacByteVectorGenerator::HmacByteVectorGenerator(size_t byte_count, | |
| 66 const std::string& secret) | |
| 67 : ByteVectorGenerator(byte_count), | |
| 68 hmac_(crypto::HMAC::SHA256) { | |
| 69 // "Nothing up my sleeve" number for the initial value of an | |
| 70 // HmacByteVectorGenerator's state. The initial digits of pi. | |
| 71 const uint64_t kHmacInitialState = 3141592653589793; | |
| 72 | |
| 73 if (!hmac_.Init(secret)) | |
| 74 NOTREACHED(); | |
| 75 | |
| 76 DCHECK(hmac_.DigestLength() > sizeof(uint8_t)); | |
|
Alexei Svitkine (slow)
2014/01/21 18:14:04
DCHECK_GT
Steven Holte
2014/01/21 20:25:14
Done.
| |
| 77 hmac_state_ = kHmacInitialState; | |
| 78 } | |
| 79 | |
| 80 HmacByteVectorGenerator::~HmacByteVectorGenerator() {} | |
| 81 | |
| 82 uint8_t HmacByteVectorGenerator::RandByte() { | |
| 83 uint8_t random_bits; | |
| 84 std::string state = base::Uint64ToString(hmac_state_); | |
| 85 if (!hmac_.Sign(state, &random_bits, sizeof(uint8_t))) | |
| 86 NOTREACHED(); | |
| 87 | |
| 88 ++hmac_state_; | |
| 89 return random_bits; | |
| 90 } | |
| 91 | |
| 92 } // namespace rappor | |
| OLD | NEW |