blob: 339b3da10caa8457fc7988c1331d72bbb2d2ed82 [file] [log] [blame]
[email protected]2a172e42014-02-21 04:06:101// 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
avif57136c12015-12-25 23:27:458#include <stddef.h>
9#include <stdint.h>
10
[email protected]2a172e42014-02-21 04:06:1011#include <vector>
12
[email protected]ccb49262014-03-26 04:10:1713#include "base/macros.h"
asvitkine0037e422016-07-18 22:37:5114#include "base/strings/string_piece.h"
nzolghadrd87a308d2016-12-07 15:45:5615#include "components/rappor/public/rappor_parameters.h"
[email protected]2a172e42014-02-21 04:06:1016#include "crypto/hmac.h"
17
18namespace rappor {
19
20// A vector of 8-bit integers used to store a set of binary bits.
21typedef std::vector<uint8_t> ByteVector;
22
holted1fbbe82015-04-30 21:07:2123// Converts the lowest |size| bytes of |value| into a ByteVector.
24void Uint64ToByteVector(uint64_t value, size_t size, ByteVector* output);
25
[email protected]d2b20dd92014-05-28 01:21:5526// Computes a bitwise AND of byte vectors and stores the result in rhs.
27// Returns rhs for chaining.
28ByteVector* ByteVectorAnd(const ByteVector& lhs, ByteVector* rhs);
29
[email protected]2a172e42014-02-21 04:06:1030// Computes a bitwise OR of byte vectors and stores the result in rhs.
31// Returns rhs for chaining.
32ByteVector* 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.
39ByteVector* ByteVectorMerge(const ByteVector& mask,
40 const ByteVector& lhs,
41 ByteVector* rhs);
42
43// Counts the number of bits set in the byte vector.
44int 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().
48class ByteVectorGenerator {
49 public:
50 explicit ByteVectorGenerator(size_t byte_count);
51
[email protected]1b305662014-08-22 21:39:0952 virtual ~ByteVectorGenerator();
[email protected]2a172e42014-02-21 04:06:1053
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.cho8b0ae1b2014-09-29 06:39:1871// A ByteVectorGenerator that uses a pseudo-random function to generate a
[email protected]2a172e42014-02-21 04:06:1072// 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.
80class 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,
asvitkine0037e422016-07-18 22:37:5187 base::StringPiece personalization_string);
[email protected]2a172e42014-02-21 04:06:1088
dcheng00ea022b2014-10-21 11:24:5689 ~HmacByteVectorGenerator() override;
[email protected]2a172e42014-02-21 04:06:1090
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:
dcheng00ea022b2014-10-21 11:24:56104 ByteVector GetRandomByteVector() override;
[email protected]2a172e42014-02-21 04:06:10105
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_