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 | |
Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame^] | 5 | #include "update_engine/update_manager/real_random_provider.h" |
| 6 | |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <unistd.h> |
| 9 | |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 10 | #include <string> |
| 11 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 12 | #include <base/files/file_path.h> |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 13 | #include <base/files/scoped_file.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 14 | #include <base/strings/stringprintf.h> |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 15 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 16 | #include "update_engine/update_manager/variable.h" |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 17 | |
| 18 | using std::string; |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | // The device providing randomness. |
| 23 | const char* kRandomDevice = "/dev/urandom"; |
| 24 | |
| 25 | } // namespace |
| 26 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 27 | namespace chromeos_update_manager { |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 28 | |
Gilad Arnold | 12db701 | 2014-01-29 16:45:30 -0800 | [diff] [blame] | 29 | // A random seed variable. |
| 30 | class RandomSeedVariable : public Variable<uint64_t> { |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 31 | public: |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 32 | // RandomSeedVariable is initialized as kVariableModeConst to let the |
| 33 | // EvaluationContext cache the value between different evaluations of the same |
| 34 | // policy request. |
Gilad Arnold | 12db701 | 2014-01-29 16:45:30 -0800 | [diff] [blame] | 35 | RandomSeedVariable(const string& name, FILE* fp) |
Alex Deymo | 0e43369 | 2014-02-20 07:23:03 -0800 | [diff] [blame] | 36 | : Variable<uint64_t>(name, kVariableModeConst), fp_(fp) {} |
Gilad Arnold | 12db701 | 2014-01-29 16:45:30 -0800 | [diff] [blame] | 37 | virtual ~RandomSeedVariable() {} |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 38 | |
| 39 | protected: |
Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 40 | virtual const uint64_t* GetValue(base::TimeDelta /* timeout */, |
| 41 | string* errmsg) { |
| 42 | uint64_t result; |
| 43 | // Aliasing via char pointer abides by the C/C++ strict-aliasing rules. |
| 44 | char* const buf = reinterpret_cast<char*>(&result); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 45 | unsigned int buf_rd = 0; |
| 46 | |
Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 47 | while (buf_rd < sizeof(result)) { |
| 48 | 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] | 49 | if (rd == 0 || ferror(fp_.get())) { |
| 50 | // Either EOF on fp or read failed. |
Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 51 | if (errmsg) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 52 | *errmsg = base::StringPrintf( |
| 53 | "Error reading from the random device: %s", kRandomDevice); |
Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 54 | } |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 55 | return nullptr; |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 56 | } |
| 57 | buf_rd += rd; |
| 58 | } |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 59 | |
Gilad Arnold | 570ecb1 | 2014-01-29 10:52:29 -0800 | [diff] [blame] | 60 | return new uint64_t(result); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | private: |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 64 | base::ScopedFILE fp_; |
Gilad Arnold | a87340b | 2014-01-30 11:10:18 -0800 | [diff] [blame] | 65 | |
| 66 | DISALLOW_COPY_AND_ASSIGN(RandomSeedVariable); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
Alex Deymo | 42c30c3 | 2014-04-24 18:41:18 -0700 | [diff] [blame] | 69 | bool RealRandomProvider::Init(void) { |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 70 | FILE* fp = fopen(kRandomDevice, "r"); |
| 71 | if (!fp) |
| 72 | return false; |
David Zeuthen | 21716e2 | 2014-04-23 15:42:05 -0700 | [diff] [blame] | 73 | var_seed_.reset(new RandomSeedVariable("seed", fp)); |
Alex Deymo | ca0aaa6 | 2014-01-06 10:39:58 -0800 | [diff] [blame] | 74 | return true; |
| 75 | } |
| 76 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 77 | } // namespace chromeos_update_manager |