Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3872665d authored by Marie Janssen's avatar Marie Janssen
Browse files

osi: add osi_rand()

Provide a replacement for rand() which has a better random source.

Change-Id: I38a8c74d86b89ec160b43b0f648f53b463be89bb
parent d5a752a8
Loading
Loading
Loading
Loading
+4 −6
Original line number Diff line number Diff line
@@ -383,16 +383,14 @@ static void btif_fetch_local_bdaddr(bt_bdaddr_t *local_addr)
    if (!valid_bda)
    {
        bdstr_t bdstr;
        /* Seed the random number generator */
        srand((unsigned int) (time(0)));

        /* No autogen BDA. Generate one now. */
        local_addr->address[0] = 0x22;
        local_addr->address[1] = 0x22;
        local_addr->address[2] = (uint8_t) ((rand() >> 8) & 0xFF);
        local_addr->address[3] = (uint8_t) ((rand() >> 8) & 0xFF);
        local_addr->address[4] = (uint8_t) ((rand() >> 8) & 0xFF);
        local_addr->address[5] = (uint8_t) ((rand() >> 8) & 0xFF);
        local_addr->address[2] = (uint8_t) osi_rand();
        local_addr->address[3] = (uint8_t) osi_rand();
        local_addr->address[4] = (uint8_t) osi_rand();
        local_addr->address[5] = (uint8_t) osi_rand();

        /* Convert to ascii, and store as a persistent property */
        bdaddr_to_string(local_addr, bdstr, sizeof(bdstr));
+2 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ btosiCommonSrc := \
    ./src/list.c \
    ./src/metrics.cpp \
    ./src/mutex.c \
    ./src/osi.c \
    ./src/properties.c \
    ./src/reactor.c \
    ./src/ringbuffer.c \
@@ -68,6 +69,7 @@ btosiCommonTestSrc := \
    ./test/hash_map_utils_test.cpp \
    ./test/list_test.cpp \
    ./test/properties_test.cpp \
    ./test/rand_test.cpp \
    ./test/reactor_test.cpp \
    ./test/ringbuffer_test.cpp \
    ./test/semaphore_test.cpp \
+2 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ static_library("osi") {
    "src/list.c",
    "src/metrics_linux.cpp",
    "src/mutex.c",
    "src/osi.c",
    "src/properties.c",
    "src/reactor.c",
    "src/ringbuffer.c",
@@ -72,6 +73,7 @@ executable("net_test_osi") {
    "test/hash_map_utils_test.cpp",
    "test/list_test.cpp",
    "test/properties_test.cpp",
    "test/rand_test.cpp",
    "test/reactor_test.cpp",
    "test/ringbuffer_test.cpp",
    "test/thread_test.cpp",
+24 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright (C) 2016 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#pragma once

#include <stdbool.h>
@@ -35,3 +53,9 @@

#define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
#define INT_TO_PTR(i) ((void *) ((intptr_t) (i)))

// Obtain a random number between 0 and INT_MAX inclusive.
// Taken from a system random source such as /dev/random.
// No guarantees of distribution are made.
// Effort is made for this to come from a real random source.
int osi_rand(void);

system/osi/src/osi.c

0 → 100644
+53 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright (C) 2016 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#define LOG_TAG "bt_osi_rand"

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "osi/include/log.h"
#include "osi/include/osi.h"

#define RANDOM_PATH "/dev/urandom"

int osi_rand(void) {
  int rand;
  int rand_fd = open(RANDOM_PATH, O_RDONLY);

  if (rand_fd == INVALID_FD) {
    LOG_ERROR(LOG_TAG, "%s can't open rand fd %s: %s ", __func__, RANDOM_PATH,
              strerror(errno));
    assert(0);
  }

  ssize_t read_bytes = read(rand_fd, &rand, sizeof(rand));
  close(rand_fd);

  assert(read_bytes == sizeof(rand));

  if (rand < 0)
    rand = -rand;

  return rand;
}
Loading