media/webrtc/signaling/src/sipcc/plat/unix-common/random.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/signaling/src/sipcc/plat/unix-common/random.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,76 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include <sys/types.h>
     1.9 +#include <unistd.h>
    1.10 +#include <fcntl.h>
    1.11 +#include <syslog.h>
    1.12 +#include <inttypes.h>
    1.13 +
    1.14 +/**
    1.15 + * platGenerateCryptoRand
    1.16 + * @brief Generates a Random Number
    1.17 + *
    1.18 + * Generate crypto graphically random number for a desired length.
    1.19 + * The function uses "secd" 's provided API. The random bytes are
    1.20 + * generated by "secd" which runs as another process. The function
    1.21 + * will be much slower than the cpr_rand(). This function should be
    1.22 + * used when good random number is needed such as random number that
    1.23 + * to be used for SRTP key for an example.
    1.24 + *
    1.25 + * @param[in] buf  - pointer to the buffer to store the result of random
    1.26 + *                   bytes requested.
    1.27 + * @param[in] len  - pointer to the length of the desired random bytes.
    1.28 + *             When calling the function, the integer's value
    1.29 + *             should be set to the desired number of random
    1.30 + *             bytes ('buf' should be of at least this size).
    1.31 + *             upon success, its value will be set to the
    1.32 + *             actual number of random bytes being returned.
    1.33 + *             (realistically, there is a maximum number of
    1.34 + *             random bytes that can be returned at a time.
    1.35 + *             if the caller request more than that, the
    1.36 + *             'len' will indicate how many bytes are actually being
    1.37 + *             returned) on failure, its value will be set to 0.
    1.38 + *
    1.39 + * @return
    1.40 + *     1 - success.
    1.41 + *     0 - fail.
    1.42 + *
    1.43 + * @note This function MUST BE REWRITTEN BY THE VENDORS
    1.44 + * @note The intent of this function is to generate a cryptographically strong
    1.45 + * random number. Vendors can map this to HandyIron or OpenSSL random number
    1.46 + * generation functions.
    1.47 + */
    1.48 +int
    1.49 +platGenerateCryptoRand(uint8_t *buf, int *len)
    1.50 +{
    1.51 +    int fd;
    1.52 +    int rc = 0;
    1.53 +    ssize_t s;
    1.54 +
    1.55 +    if ((fd = open("/dev/urandom", O_RDONLY)) == -1) {
    1.56 +        syslog(LOG_ERR, "Failed to open prng driver");
    1.57 +        return 0;
    1.58 +    }
    1.59 +
    1.60 +    /*
    1.61 +     * Try to read the given amount of bytes from the PRNG device.  We do not
    1.62 +     * handle short reads but just return the number of bytes read from the
    1.63 +     * device.  The caller has to manage this.
    1.64 +     * E.g. gsmsdp_generate_key() in core/gsm/gsm_sdp_crypto.c
    1.65 +     */
    1.66 +    s = read(fd, buf, (size_t) *len);
    1.67 +
    1.68 +    if (s > 0) {
    1.69 +        *len = s;
    1.70 +        rc = 1; /* Success */
    1.71 +    } else {
    1.72 +        *len = 0;
    1.73 +        rc = 0; /* Failure */
    1.74 +    }
    1.75 +
    1.76 +    (void) close(fd);
    1.77 +    return rc;
    1.78 +}
    1.79 +

mercurial