1259fa1be39c0a35ec374fe127ba49d1c5ed5eb18Utkarsh Sanghi//
2259fa1be39c0a35ec374fe127ba49d1c5ed5eb18Utkarsh Sanghi// Copyright (C) 2015 The Android Open Source Project
3259fa1be39c0a35ec374fe127ba49d1c5ed5eb18Utkarsh Sanghi//
4259fa1be39c0a35ec374fe127ba49d1c5ed5eb18Utkarsh Sanghi// Licensed under the Apache License, Version 2.0 (the "License");
5259fa1be39c0a35ec374fe127ba49d1c5ed5eb18Utkarsh Sanghi// you may not use this file except in compliance with the License.
6259fa1be39c0a35ec374fe127ba49d1c5ed5eb18Utkarsh Sanghi// You may obtain a copy of the License at
7259fa1be39c0a35ec374fe127ba49d1c5ed5eb18Utkarsh Sanghi//
8259fa1be39c0a35ec374fe127ba49d1c5ed5eb18Utkarsh Sanghi//      http://www.apache.org/licenses/LICENSE-2.0
9259fa1be39c0a35ec374fe127ba49d1c5ed5eb18Utkarsh Sanghi//
10259fa1be39c0a35ec374fe127ba49d1c5ed5eb18Utkarsh Sanghi// Unless required by applicable law or agreed to in writing, software
11259fa1be39c0a35ec374fe127ba49d1c5ed5eb18Utkarsh Sanghi// distributed under the License is distributed on an "AS IS" BASIS,
12259fa1be39c0a35ec374fe127ba49d1c5ed5eb18Utkarsh Sanghi// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13259fa1be39c0a35ec374fe127ba49d1c5ed5eb18Utkarsh Sanghi// See the License for the specific language governing permissions and
14259fa1be39c0a35ec374fe127ba49d1c5ed5eb18Utkarsh Sanghi// limitations under the License.
15259fa1be39c0a35ec374fe127ba49d1c5ed5eb18Utkarsh Sanghi//
1650e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi
1721637105867fb6b59a0864811f40d73aff944c19Utkarsh Sanghi#include "tpm_manager/server/openssl_crypto_util_impl.h"
1850e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi
1950e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi#include <base/logging.h>
2050e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi#include <base/stl_util.h>
2150e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi#include <openssl/rand.h>
2250e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi
2350e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghinamespace tpm_manager {
2450e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi
2521637105867fb6b59a0864811f40d73aff944c19Utkarsh Sanghibool OpensslCryptoUtilImpl::GetRandomBytes(size_t num_bytes,
2621637105867fb6b59a0864811f40d73aff944c19Utkarsh Sanghi                                           std::string* random_data) {
2750e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi  random_data->resize(num_bytes);
2850e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi  unsigned char* random_buffer =
2950e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi      reinterpret_cast<unsigned char*>(string_as_array(random_data));
3050e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi  if (RAND_bytes(random_buffer, num_bytes) != 1) {
3150e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi    LOG(ERROR) << "Error getting random bytes using Openssl.";
3250e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi    random_data->clear();
3350e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi    return false;
3450e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi  }
3550e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi  return true;
3650e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi}
3750e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi
3850e52ff6bcc478118a1cdec27903a5af5061d77bUtkarsh Sanghi}  // namespace tpm_manager
39