1 //
2 // Copyright (C) 2015 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "tpm_manager/server/openssl_crypto_util_impl.h"
18 
19 #include <base/logging.h>
20 #include <base/stl_util.h>
21 #include <openssl/rand.h>
22 
23 namespace tpm_manager {
24 
GetRandomBytes(size_t num_bytes,std::string * random_data)25 bool OpensslCryptoUtilImpl::GetRandomBytes(size_t num_bytes,
26                                            std::string* random_data) {
27   random_data->resize(num_bytes);
28   unsigned char* random_buffer =
29       reinterpret_cast<unsigned char*>(string_as_array(random_data));
30   if (RAND_bytes(random_buffer, num_bytes) != 1) {
31     LOG(ERROR) << "Error getting random bytes using Openssl.";
32     random_data->clear();
33     return false;
34   }
35   return true;
36 }
37 
38 }  // namespace tpm_manager
39