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 #ifndef COMPONENTS_RAPPOR_BYTE_VECTOR_UTILS_H_ | |
| 6 #define COMPONENTS_RAPPOR_BYTE_VECTOR_UTILS_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 #include <stdint.h> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "components/rappor/rappor_parameters.h" | |
| 13 #include "crypto/hmac.h" | |
| 14 | |
| 15 namespace rappor { | |
| 16 | |
| 17 // A vector of 8-bit integers used to store a set of binary bits. | |
| 18 typedef std::vector<uint8_t> ByteVector; | |
| 19 | |
| 20 // Computes a bitwise OR of byte vectors and stores the result in rhs. | |
| 21 ByteVector* ByteVectorOr(const ByteVector& lhs, ByteVector* rhs); | |
| 22 | |
| 23 // Merges the contents of lhs and rhs vectors according to a mask vector. | |
| 24 // Equivalent to (lhs & ~mask) | (rhs & mask). Stores the result in rhs. | |
| 25 ByteVector* ByteVectorMerge(const ByteVector& mask, | |
| 26 const ByteVector& lhs, | |
| 27 ByteVector* rhs); | |
| 28 | |
| 29 // Counts the number of bits set in the byte vector. | |
| 30 int CountBits(const ByteVector& vector); | |
| 31 | |
| 32 // A utility object for generating random binary data with different | |
| 33 // likelihood of bits being true, using entropy from crypto::RandBytes(). | |
| 34 class ByteVectorGenerator { | |
| 35 public: | |
| 36 explicit ByteVectorGenerator(size_t byte_count); | |
| 37 | |
| 38 ~ByteVectorGenerator(); | |
| 39 | |
| 40 // Generates a random byte vector where the bits are an independent random | |
| 41 // variables which are true with the given |probability|. | |
| 42 ByteVector GetWeightedRandomByteVector(Probability probability); | |
| 43 | |
| 44 protected: | |
| 45 // Generates a random byte from a uniform distribution. | |
| 46 virtual uint8_t RandByte(); | |
| 47 | |
| 48 private: | |
| 49 // Generates a random vector of bytes from a uniform distribution. | |
| 50 ByteVector GetRandomByteVector(); | |
| 51 | |
| 52 size_t byte_count_; | |
| 53 | |
| 54 DISALLOW_COPY_AND_ASSIGN(ByteVectorGenerator); | |
| 55 }; | |
| 56 | |
| 57 // A ByteVectorGenerator that uses a psuedo-random function to generate | |
| 58 // deterministicly random bits. The implementation is equivalent to the | |
|
wtc
2014/02/10 20:12:54
Nit: deterministicly => deterministically
Also, y
Steven Holte
2014/02/10 22:50:54
Done.
| |
| 59 // HMAC_DRBG algorithm from NIST, assuming that all of the bytes can | |
| 60 // be generated from one request. Therefore, this one instance of this class | |
| 61 // should not be used to generate more than 2^19 bits | |
| 62 // Ref: http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf | |
|
wtc
2014/02/10 20:12:54
IMPORTANT: sometimes we need to audit the Chromium
Steven Holte
2014/02/10 22:50:54
Added a longer comment explaining the rationale of
| |
| 63 class HmacByteVectorGenerator : public ByteVectorGenerator { | |
| 64 public: | |
| 65 // Constructor takes the size of the vector to generate, along with a | |
| 66 // secret value to seed the pseudo-random number generator. |secret| is | |
| 67 // treated as an array of bytes. | |
| 68 HmacByteVectorGenerator(size_t byte_count, const std::string& secret); | |
| 69 | |
| 70 ~HmacByteVectorGenerator(); | |
| 71 | |
| 72 protected: | |
| 73 virtual uint8_t RandByte() OVERRIDE; | |
| 74 | |
| 75 private: | |
| 76 crypto::HMAC hmac_; | |
| 77 ByteVector value_; | |
| 78 size_t requested_bytes_; | |
| 79 | |
| 80 DISALLOW_COPY_AND_ASSIGN(HmacByteVectorGenerator); | |
| 81 }; | |
| 82 | |
| 83 } // namespace rappor | |
| 84 | |
| 85 #endif // COMPONENTS_RAPPOR_BYTE_VECTOR_UTILS_H_ | |
| OLD | NEW |