summaryrefslogtreecommitdiff
path: root/chromium/net/quic/crypto/quic_random.cc
blob: c96f01a9139ea1508a7f2d6c91c49ba809175b10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/quic/crypto/quic_random.h"

#include "base/logging.h"
#include "base/memory/singleton.h"
#include "crypto/random.h"

namespace net {

namespace {

class DefaultRandom : public QuicRandom {
 public:
  static DefaultRandom* GetInstance();

  // QuicRandom implementation
  virtual void RandBytes(void* data, size_t len) OVERRIDE;
  virtual uint64 RandUint64() OVERRIDE;
  virtual bool RandBool() OVERRIDE;
  virtual void Reseed(const void* additional_entropy,
                      size_t entropy_len) OVERRIDE;

 private:
  DefaultRandom();
  virtual ~DefaultRandom() {}

  friend struct DefaultSingletonTraits<DefaultRandom>;
  DISALLOW_COPY_AND_ASSIGN(DefaultRandom);
};

DefaultRandom* DefaultRandom::GetInstance() {
  return Singleton<DefaultRandom>::get();
}

void DefaultRandom::RandBytes(void* data, size_t len) {
  crypto::RandBytes(data, len);
}

uint64 DefaultRandom::RandUint64() {
  uint64 value;
  RandBytes(&value, sizeof(value));
  return value;
}

bool DefaultRandom::RandBool() {
  char value;
  RandBytes(&value, sizeof(value));
  return (value & 1) == 1;
}

void DefaultRandom::Reseed(const void* additional_entropy, size_t entropy_len) {
  // No such function exists in crypto/random.h.
}

DefaultRandom::DefaultRandom() {
}

}  // namespace

// static
QuicRandom* QuicRandom::GetInstance() { return DefaultRandom::GetInstance(); }

}  // namespace net