aboutsummaryrefslogtreecommitdiff
path: root/third_party/boringssl/crypto_sysrand.cc
blob: 34fe1bb02cb9515c3022c671e615c788cfd3a5e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2021 The Pigweed Authors
//
// 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
//
//     https://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.

#include "src/crypto/fipsmodule/rand/internal.h"

extern "C" {
// OPENSSL_URANDOM is defined automatically based on platform flags.
// See crypto/fipsmodule/rand/internal.h
#ifdef OPENSSL_URANDOM
// When OPENSSL_URANDOM is defined, boringssl assumes linux and
// reads from "dev/urandom" for generating randoms bytes.
// We mock the required file io functions to accomodate it for now.
// TODO(zyecheng): Ask BoringSSL team if there are ways to disable
// OPENSSL_URANDOM, potentially by adding a OPENSSL_PIGWEED flag in
// crypto/fipsmodule/rand/internal.h. If not, we need to keep these
// mockings.

#define URANDOM_FILE_FD 123
int open(const char* file, int, ...) {
  if (strcmp(file, "/dev/urandom") == 0) {
    return URANDOM_FILE_FD;
  }
  return -1;
}

ssize_t read(int fd, void*, size_t len) {
  if (fd == URANDOM_FILE_FD) {
    // TODO(zyecheng): Add code to generate random bytes.
  }
  return static_cast<ssize_t>(len);
}

#else
// When OPENSSL_URANDOM is not defined, BoringSSL expects an implementation of
// the following function for generating random bytes.
void CRYPTO_sysrand(uint8_t*, size_t) {
  // TODO(zyecheng): Add code to generate random bytes.
}
#endif
}