summaryrefslogtreecommitdiff
path: root/server/openssl_crypto_util.cc
blob: 0edaf0540d8431583ebc9f813f72e85e11469faf (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
// Copyright 2015 The Chromium OS 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 "tpm_manager/server/openssl_crypto_util.h"

#include <base/logging.h>
#include <base/stl_util.h>
#include <openssl/rand.h>

namespace tpm_manager {

bool OpensslCryptoUtil::GetRandomBytes(size_t num_bytes,
                                       std::string* random_data) {
  random_data->resize(num_bytes);
  unsigned char* random_buffer =
      reinterpret_cast<unsigned char*>(string_as_array(random_data));
  if (RAND_bytes(random_buffer, num_bytes) != 1) {
    LOG(ERROR) << "Error getting random bytes using Openssl.";
    random_data->clear();
    return false;
  }
  return true;
}

}  // namespace tpm_manager