Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: components/rappor/byte_vector_utils.h

Issue 49753002: RAPPOR implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: NIST test vector Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 <vector>
9
10 #include "base/basictypes.h"
11 #include "components/rappor/rappor_parameters.h"
12 #include "crypto/hmac.h"
13
14 namespace rappor {
15
16 // A vector of 8-bit integers used to store a set of binary bits.
17 typedef std::vector<uint8_t> ByteVector;
18
19 // Computes a bitwise OR of byte vectors and stores the result in rhs.
20 // Returns rhs for chaining.
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 independent random
41 // variables which are true with the given |probability|.
42 ByteVector GetWeightedRandomByteVector(Probability probability);
43
44 protected:
45 // Size of vectors to be generated.
46 size_t byte_count() const { return byte_count_; }
47
48 // Generates a random vector of bytes from a uniform distribution.
49 virtual ByteVector GetRandomByteVector();
50
51 private:
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 a
58 // deterministically random bits. This class only implements a single request
59 // from HMAC_DRBG and streams up to 2^19 bits from that request.
60 // Ref: http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf
61 // We're using our own PRNG instead of crypto::RandBytes because we need to
62 // generate a repeatable sequence of bits from the same seed. Conservatively,
63 // we're choosing to use HMAC_DRBG here, as it is one of the best studied
64 // and standardized ways of generating deterministic, unpredictable sequences
65 // based on a secret seed.
66 class HmacByteVectorGenerator : public ByteVectorGenerator {
67 public:
68 // Constructor takes the size of the vector to generate, along with a
69 // |entropy_input| and |personalization_string| to seed the pseudo-random
70 // number generator. The string parameters are treated as byte arrays.
71 HmacByteVectorGenerator(size_t byte_count,
72 const std::string& entropy_input,
73 const std::string& personalization_string);
74
75 ~HmacByteVectorGenerator();
76
77 // Generates a random string suitable for passing to the constructor as
78 // |entropy_input|.
79 static std::string GenerateEntropyInput();
80
81 // Key size required for 128-bit security strength (including nonce).
82 static const size_t kEntropyInputSize;
83
84 protected:
85 // Generate byte vector generator that streams from the next request instead
86 // of the current one. For testing against NIST test vectors only.
87 explicit HmacByteVectorGenerator(const HmacByteVectorGenerator& prev_request);
88
89 // ByteVector implementation:
90 virtual ByteVector GetRandomByteVector() OVERRIDE;
91
92 private:
93 // HMAC initalized with the value of "Key" HMAC_DRBG_Initialize.
94 crypto::HMAC hmac_;
95
96 // The "V" value from HMAC_DRBG.
97 ByteVector value_;
98
99 // Total number of bytes streamed from the HMAC_DRBG Generate Process.
100 size_t generated_bytes_;
101
102 DISALLOW_ASSIGN(HmacByteVectorGenerator);
103 };
104
105 } // namespace rappor
106
107 #endif // COMPONENTS_RAPPOR_BYTE_VECTOR_UTILS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698