Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include <sys/types.h> |
michael@0 | 6 | #include <unistd.h> |
michael@0 | 7 | #include <fcntl.h> |
michael@0 | 8 | #include <syslog.h> |
michael@0 | 9 | #include <inttypes.h> |
michael@0 | 10 | |
michael@0 | 11 | /** |
michael@0 | 12 | * platGenerateCryptoRand |
michael@0 | 13 | * @brief Generates a Random Number |
michael@0 | 14 | * |
michael@0 | 15 | * Generate crypto graphically random number for a desired length. |
michael@0 | 16 | * The function uses "secd" 's provided API. The random bytes are |
michael@0 | 17 | * generated by "secd" which runs as another process. The function |
michael@0 | 18 | * will be much slower than the cpr_rand(). This function should be |
michael@0 | 19 | * used when good random number is needed such as random number that |
michael@0 | 20 | * to be used for SRTP key for an example. |
michael@0 | 21 | * |
michael@0 | 22 | * @param[in] buf - pointer to the buffer to store the result of random |
michael@0 | 23 | * bytes requested. |
michael@0 | 24 | * @param[in] len - pointer to the length of the desired random bytes. |
michael@0 | 25 | * When calling the function, the integer's value |
michael@0 | 26 | * should be set to the desired number of random |
michael@0 | 27 | * bytes ('buf' should be of at least this size). |
michael@0 | 28 | * upon success, its value will be set to the |
michael@0 | 29 | * actual number of random bytes being returned. |
michael@0 | 30 | * (realistically, there is a maximum number of |
michael@0 | 31 | * random bytes that can be returned at a time. |
michael@0 | 32 | * if the caller request more than that, the |
michael@0 | 33 | * 'len' will indicate how many bytes are actually being |
michael@0 | 34 | * returned) on failure, its value will be set to 0. |
michael@0 | 35 | * |
michael@0 | 36 | * @return |
michael@0 | 37 | * 1 - success. |
michael@0 | 38 | * 0 - fail. |
michael@0 | 39 | * |
michael@0 | 40 | * @note This function MUST BE REWRITTEN BY THE VENDORS |
michael@0 | 41 | * @note The intent of this function is to generate a cryptographically strong |
michael@0 | 42 | * random number. Vendors can map this to HandyIron or OpenSSL random number |
michael@0 | 43 | * generation functions. |
michael@0 | 44 | */ |
michael@0 | 45 | int |
michael@0 | 46 | platGenerateCryptoRand(uint8_t *buf, int *len) |
michael@0 | 47 | { |
michael@0 | 48 | int fd; |
michael@0 | 49 | int rc = 0; |
michael@0 | 50 | ssize_t s; |
michael@0 | 51 | |
michael@0 | 52 | if ((fd = open("/dev/urandom", O_RDONLY)) == -1) { |
michael@0 | 53 | syslog(LOG_ERR, "Failed to open prng driver"); |
michael@0 | 54 | return 0; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | /* |
michael@0 | 58 | * Try to read the given amount of bytes from the PRNG device. We do not |
michael@0 | 59 | * handle short reads but just return the number of bytes read from the |
michael@0 | 60 | * device. The caller has to manage this. |
michael@0 | 61 | * E.g. gsmsdp_generate_key() in core/gsm/gsm_sdp_crypto.c |
michael@0 | 62 | */ |
michael@0 | 63 | s = read(fd, buf, (size_t) *len); |
michael@0 | 64 | |
michael@0 | 65 | if (s > 0) { |
michael@0 | 66 | *len = s; |
michael@0 | 67 | rc = 1; /* Success */ |
michael@0 | 68 | } else { |
michael@0 | 69 | *len = 0; |
michael@0 | 70 | rc = 0; /* Failure */ |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | (void) close(fd); |
michael@0 | 74 | return rc; |
michael@0 | 75 | } |
michael@0 | 76 |