Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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/hmac.h" | |
| 12 #include "crypto/random.h" | |
| 13 | |
| 14 namespace rappor { | |
| 15 | |
| 16 ByteVector* ByteVectorAnd(ByteVector* lhs, const ByteVector& rhs) { | |
| 17 DCHECK_EQ(lhs->size(), rhs.size()); | |
| 18 for (size_t i = 0, len = rhs.size(); i < len; i++) { | |
| 19 (*lhs)[i] = (*lhs)[i] & rhs[i]; | |
| 20 } | |
| 21 return lhs; | |
| 22 } | |
| 23 | |
| 24 ByteVector* ByteVectorOr(ByteVector* lhs, const ByteVector& rhs) { | |
| 25 DCHECK_EQ(lhs->size(), rhs.size()); | |
| 26 for (size_t i = 0, len = rhs.size(); i < len; i++) { | |
| 27 (*lhs)[i] = (*lhs)[i] | rhs[i]; | |
| 28 } | |
| 29 return lhs; | |
| 30 } | |
| 31 | |
| 32 ByteVector* ByteVectorMerge(ByteVector* lhs, | |
| 33 const ByteVector& rhs, | |
| 34 const ByteVector& mask) { | |
| 35 DCHECK_EQ(lhs->size(), rhs.size()); | |
| 36 for (size_t i = 0, len = rhs.size(); i < len; i++) { | |
| 37 (*lhs)[i] = ((*lhs)[i] & ~mask[i]) | (rhs[i] & mask[i]); | |
| 38 } | |
| 39 return lhs; | |
| 40 } | |
| 41 | |
| 42 ByteVectorGenerator::ByteVectorGenerator(size_t byte_count) | |
| 43 : byte_count_(byte_count) {} | |
| 44 | |
| 45 uint8_t ByteVectorGenerator::RandByte() { | |
| 46 uint8_t random_bits; | |
| 47 crypto::RandBytes(&random_bits, sizeof(uint8_t)); | |
| 48 return random_bits; | |
| 49 } | |
| 50 | |
| 51 ByteVector ByteVectorGenerator::GetRandomByteVector() { | |
| 52 ByteVector bytes(byte_count_); | |
| 53 for (size_t i = 0; i < byte_count_; i++) { | |
| 54 bytes[i] = RandByte(); | |
| 55 } | |
| 56 return bytes; | |
| 57 } | |
| 58 | |
| 59 ByteVector ByteVectorGenerator::GetWeightedRandomByteVector( | |
| 60 Probability probability) { | |
| 61 ByteVector bytes = GetRandomByteVector(); | |
| 62 switch (probability) { | |
| 63 case PROBABILITY_87_5: | |
| 64 return *ByteVectorOr(ByteVectorOr(&bytes, GetRandomByteVector()), | |
| 65 GetRandomByteVector()); | |
| 66 case PROBABILITY_75: | |
| 67 return *ByteVectorOr(&bytes, GetRandomByteVector()); | |
| 68 case PROBABILITY_50: | |
| 69 return bytes; | |
| 70 case PROBABILITY_25: | |
| 71 return *ByteVectorAnd(&bytes, GetRandomByteVector()); | |
| 72 case PROBABILITY_12_5: | |
| 73 return *ByteVectorAnd(ByteVectorAnd(&bytes, GetRandomByteVector()), | |
| 74 GetRandomByteVector()); | |
| 75 default: | |
| 76 // Invalid probability for coin flips | |
| 77 abort(); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 HmacByteVectorGenerator::HmacByteVectorGenerator(size_t byte_count, | |
| 82 const std::string& secret) | |
| 83 : ByteVectorGenerator(byte_count), hmac_(crypto::HMAC::SHA256) { | |
| 84 // A completely arbitrary value for the initial value of an | |
| 85 // HmacByteVectorGenerator's state. The initial digits of pi. | |
| 86 const uint64_t kHmacInitialState = 3141592653589793; | |
| 87 | |
| 88 if (!hmac_.Init(secret)) { | |
| 89 abort(); | |
|
Alexei Svitkine (slow)
2014/01/09 19:23:09
Don't use abort() in Chrome code. You probably wan
Steven Holte
2014/01/09 22:03:01
Done.
| |
| 90 }; | |
| 91 assert(hmac_.DigestLength() > sizeof(uint64_t)); | |
| 92 hmac_state_ = kHmacInitialState; | |
| 93 } | |
| 94 | |
| 95 uint8_t HmacByteVectorGenerator::RandByte() { | |
| 96 uint8_t random_bits; | |
| 97 std::string state = base::Uint64ToString(hmac_state_); | |
| 98 if (!hmac_.Sign(state, &random_bits, sizeof(uint8_t))) { | |
| 99 abort(); | |
| 100 } | |
| 101 ++hmac_state_; | |
| 102 return random_bits; | |
| 103 } | |
| 104 | |
| 105 } // namespace rappor | |
| OLD | NEW |