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