[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 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 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 13 | #include "base/macros.h" |
asvitkine | 0037e42 | 2016-07-18 22:37:51 | [diff] [blame] | 14 | #include "base/strings/string_piece.h" |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 15 | #include "components/rappor/public/rappor_parameters.h" |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 16 | #include "crypto/hmac.h" |
| 17 | |
| 18 | namespace rappor { |
| 19 | |
| 20 | // A vector of 8-bit integers used to store a set of binary bits. |
| 21 | typedef std::vector<uint8_t> ByteVector; |
| 22 | |
holte | d1fbbe8 | 2015-04-30 21:07:21 | [diff] [blame] | 23 | // Converts the lowest |size| bytes of |value| into a ByteVector. |
| 24 | void Uint64ToByteVector(uint64_t value, size_t size, ByteVector* output); |
| 25 | |
[email protected] | d2b20dd9 | 2014-05-28 01:21:55 | [diff] [blame] | 26 | // Computes a bitwise AND of byte vectors and stores the result in rhs. |
| 27 | // Returns rhs for chaining. |
| 28 | ByteVector* ByteVectorAnd(const ByteVector& lhs, ByteVector* rhs); |
| 29 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 30 | // Computes a bitwise OR of byte vectors and stores the result in rhs. |
| 31 | // Returns rhs for chaining. |
| 32 | ByteVector* ByteVectorOr(const ByteVector& lhs, ByteVector* rhs); |
| 33 | |
| 34 | // Merges the contents of lhs and rhs vectors according to a mask vector. |
| 35 | // The i-th bit of the result vector will be the i-th bit of either the lhs |
| 36 | // or rhs vector, based on the i-th bit of the mask vector. |
| 37 | // Equivalent to (lhs & ~mask) | (rhs & mask). Stores the result in rhs. |
| 38 | // Returns rhs for chaining. |
| 39 | ByteVector* ByteVectorMerge(const ByteVector& mask, |
| 40 | const ByteVector& lhs, |
| 41 | ByteVector* rhs); |
| 42 | |
| 43 | // Counts the number of bits set in the byte vector. |
| 44 | int CountBits(const ByteVector& vector); |
| 45 | |
| 46 | // A utility object for generating random binary data with different |
| 47 | // likelihood of bits being true, using entropy from crypto::RandBytes(). |
| 48 | class ByteVectorGenerator { |
| 49 | public: |
| 50 | explicit ByteVectorGenerator(size_t byte_count); |
| 51 | |
[email protected] | 1b30566 | 2014-08-22 21:39:09 | [diff] [blame] | 52 | virtual ~ByteVectorGenerator(); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 53 | |
| 54 | // Generates a random byte vector where the bits are independent random |
| 55 | // variables which are true with the given |probability|. |
| 56 | ByteVector GetWeightedRandomByteVector(Probability probability); |
| 57 | |
| 58 | protected: |
| 59 | // Size of vectors to be generated. |
| 60 | size_t byte_count() const { return byte_count_; } |
| 61 | |
| 62 | // Generates a random vector of bytes from a uniform distribution. |
| 63 | virtual ByteVector GetRandomByteVector(); |
| 64 | |
| 65 | private: |
| 66 | size_t byte_count_; |
| 67 | |
| 68 | DISALLOW_COPY_AND_ASSIGN(ByteVectorGenerator); |
| 69 | }; |
| 70 | |
sungmann.cho | 8b0ae1b | 2014-09-29 06:39:18 | [diff] [blame] | 71 | // A ByteVectorGenerator that uses a pseudo-random function to generate a |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 72 | // deterministically random bits. This class only implements a single request |
| 73 | // from HMAC_DRBG and streams up to 2^19 bits from that request. |
| 74 | // Ref: http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf |
| 75 | // We're using our own PRNG instead of crypto::RandBytes because we need to |
| 76 | // generate a repeatable sequence of bits from the same seed. Conservatively, |
| 77 | // we're choosing to use HMAC_DRBG here, as it is one of the best studied |
| 78 | // and standardized ways of generating deterministic, unpredictable sequences |
| 79 | // based on a secret seed. |
| 80 | class HmacByteVectorGenerator : public ByteVectorGenerator { |
| 81 | public: |
| 82 | // Constructor takes the size of the vector to generate, along with a |
| 83 | // |entropy_input| and |personalization_string| to seed the pseudo-random |
| 84 | // number generator. The string parameters are treated as byte arrays. |
| 85 | HmacByteVectorGenerator(size_t byte_count, |
| 86 | const std::string& entropy_input, |
asvitkine | 0037e42 | 2016-07-18 22:37:51 | [diff] [blame] | 87 | base::StringPiece personalization_string); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 88 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 89 | ~HmacByteVectorGenerator() override; |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 90 | |
| 91 | // Generates a random string suitable for passing to the constructor as |
| 92 | // |entropy_input|. |
| 93 | static std::string GenerateEntropyInput(); |
| 94 | |
| 95 | // Key size required for 128-bit security strength (including nonce). |
| 96 | static const size_t kEntropyInputSize; |
| 97 | |
| 98 | protected: |
| 99 | // Generate byte vector generator that streams from the next request instead |
| 100 | // of the current one. For testing against NIST test vectors only. |
| 101 | explicit HmacByteVectorGenerator(const HmacByteVectorGenerator& prev_request); |
| 102 | |
| 103 | // ByteVector implementation: |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 104 | ByteVector GetRandomByteVector() override; |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 105 | |
| 106 | private: |
| 107 | // HMAC initalized with the value of "Key" HMAC_DRBG_Initialize. |
| 108 | crypto::HMAC hmac_; |
| 109 | |
| 110 | // The "V" value from HMAC_DRBG. |
| 111 | ByteVector value_; |
| 112 | |
| 113 | // Total number of bytes streamed from the HMAC_DRBG Generate Process. |
| 114 | size_t generated_bytes_; |
| 115 | |
| 116 | DISALLOW_ASSIGN(HmacByteVectorGenerator); |
| 117 | }; |
| 118 | |
| 119 | } // namespace rappor |
| 120 | |
| 121 | #endif // COMPONENTS_RAPPOR_BYTE_VECTOR_UTILS_H_ |