• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "src/crypto/fipsmodule/rand/internal.h"
16 
17 extern "C" {
18 // OPENSSL_URANDOM is defined automatically based on platform flags.
19 // See crypto/fipsmodule/rand/internal.h
20 #ifdef OPENSSL_URANDOM
21 // When OPENSSL_URANDOM is defined, boringssl assumes linux and
22 // reads from "dev/urandom" for generating randoms bytes.
23 // We mock the required file io functions to accomodate it for now.
24 // TODO(zyecheng): Ask BoringSSL team if there are ways to disable
25 // OPENSSL_URANDOM, potentially by adding a OPENSSL_PIGWEED flag in
26 // crypto/fipsmodule/rand/internal.h. If not, we need to keep these
27 // mockings.
28 
29 #define URANDOM_FILE_FD 123
open(const char * file,int,...)30 int open(const char* file, int, ...) {
31   if (strcmp(file, "/dev/urandom") == 0) {
32     return URANDOM_FILE_FD;
33   }
34   return -1;
35 }
36 
read(int fd,void *,size_t len)37 ssize_t read(int fd, void*, size_t len) {
38   if (fd == URANDOM_FILE_FD) {
39     // TODO(zyecheng): Add code to generate random bytes.
40   }
41   return static_cast<ssize_t>(len);
42 }
43 
44 #else
45 // When OPENSSL_URANDOM is not defined, BoringSSL expects an implementation of
46 // the following function for generating random bytes.
47 void CRYPTO_sysrand(uint8_t*, size_t) {
48   // TODO(zyecheng): Add code to generate random bytes.
49 }
50 #endif
51 }
52