| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS 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 | #include <stdio.h> | 
|  | 6 | #include <unistd.h> | 
|  | 7 |  | 
|  | 8 | #include <base/file_path.h> | 
|  | 9 | #include <base/file_util.h> | 
| Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 10 | #include <base/stringprintf.h> | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 11 |  | 
|  | 12 | #include "policy_manager/random_provider.h" | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 13 |  | 
|  | 14 | using std::string; | 
|  | 15 |  | 
|  | 16 | namespace { | 
|  | 17 |  | 
|  | 18 | // The device providing randomness. | 
|  | 19 | const char* kRandomDevice = "/dev/urandom"; | 
|  | 20 |  | 
|  | 21 | }  // namespace | 
|  | 22 |  | 
|  | 23 | namespace chromeos_policy_manager { | 
|  | 24 |  | 
| Gilad Arnold | 12db701 | 2014-01-29 16:45:30 -0800 | [diff] [blame^] | 25 | // A random seed variable. | 
|  | 26 | class RandomSeedVariable : public Variable<uint64_t> { | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 27 | public: | 
| Gilad Arnold | 12db701 | 2014-01-29 16:45:30 -0800 | [diff] [blame^] | 28 | RandomSeedVariable(const string& name, FILE* fp) | 
| Alex Deymo | 391ad9f | 2014-01-29 14:36:20 -0800 | [diff] [blame] | 29 | : Variable<uint64_t>(name), fp_(fp) {} | 
| Gilad Arnold | 12db701 | 2014-01-29 16:45:30 -0800 | [diff] [blame^] | 30 | virtual ~RandomSeedVariable() {} | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 31 |  | 
|  | 32 | protected: | 
| Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 33 | virtual const uint64_t* GetValue(base::TimeDelta /* timeout */, | 
|  | 34 | string* errmsg) { | 
|  | 35 | uint64_t result; | 
|  | 36 | // Aliasing via char pointer abides by the C/C++ strict-aliasing rules. | 
|  | 37 | char* const buf = reinterpret_cast<char*>(&result); | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 38 | unsigned int buf_rd = 0; | 
|  | 39 |  | 
| Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 40 | while (buf_rd < sizeof(result)) { | 
|  | 41 | int rd = fread(buf + buf_rd, 1, sizeof(result) - buf_rd, fp_.get()); | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 42 | if (rd == 0 || ferror(fp_.get())) { | 
|  | 43 | // Either EOF on fp or read failed. | 
| Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 44 | if (errmsg) { | 
|  | 45 | *errmsg = StringPrintf("Error reading from the random device: %s", | 
|  | 46 | kRandomDevice); | 
|  | 47 | } | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 48 | return NULL; | 
|  | 49 | } | 
|  | 50 | buf_rd += rd; | 
|  | 51 | } | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 52 |  | 
| Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 53 | return new uint64_t(result); | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 54 | } | 
|  | 55 |  | 
|  | 56 | private: | 
| Gilad Arnold | 12db701 | 2014-01-29 16:45:30 -0800 | [diff] [blame^] | 57 | DISALLOW_COPY_AND_ASSIGN(RandomSeedVariable); | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 58 |  | 
|  | 59 | file_util::ScopedFILE fp_; | 
|  | 60 | }; | 
|  | 61 |  | 
|  | 62 |  | 
|  | 63 | // RandomProvider implementation. | 
|  | 64 |  | 
| Gilad Arnold | b33e198 | 2014-01-27 14:46:27 -0800 | [diff] [blame] | 65 | bool RandomProvider::DoInit(void) { | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 66 | FILE* fp = fopen(kRandomDevice, "r"); | 
|  | 67 | if (!fp) | 
|  | 68 | return false; | 
| Gilad Arnold | 12db701 | 2014-01-29 16:45:30 -0800 | [diff] [blame^] | 69 | var_random_seed = new RandomSeedVariable("random_seed", fp); | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 70 | return true; | 
|  | 71 | } | 
|  | 72 |  | 
| Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 73 | }  // namespace chromeos_policy_manager |