1 // Copyright 2017 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of 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,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <limits.h>
16 
17 #include <openssl/rand.h>
18 
19 #include "../bcm_support.h"
20 #include "../fipsmodule/bcm_interface.h"
21 
22 
RAND_bytes(uint8_t * buf,size_t len)23 int RAND_bytes(uint8_t *buf, size_t len) {
24   BCM_rand_bytes(buf, len);
25   return 1;
26 }
27 
RAND_pseudo_bytes(uint8_t * buf,size_t len)28 int RAND_pseudo_bytes(uint8_t *buf, size_t len) { return RAND_bytes(buf, len); }
29 
RAND_seed(const void * buf,int num)30 void RAND_seed(const void *buf, int num) {
31   // OpenSSH calls |RAND_seed| before jailing on the assumption that any needed
32   // file descriptors etc will be opened.
33   uint8_t unused;
34   RAND_bytes(&unused, sizeof(unused));
35 }
36 
RAND_load_file(const char * path,long num)37 int RAND_load_file(const char *path, long num) {
38   if (num < 0) {  // read the "whole file"
39     return 1;
40   } else if (num <= INT_MAX) {
41     return (int)num;
42   } else {
43     return INT_MAX;
44   }
45 }
46 
RAND_file_name(char * buf,size_t num)47 const char *RAND_file_name(char *buf, size_t num) { return NULL; }
48 
RAND_add(const void * buf,int num,double entropy)49 void RAND_add(const void *buf, int num, double entropy) {}
50 
RAND_egd(const char * path)51 int RAND_egd(const char *path) { return 255; }
52 
RAND_poll(void)53 int RAND_poll(void) { return 1; }
54 
RAND_status(void)55 int RAND_status(void) { return 1; }
56 
57 static const struct rand_meth_st kSSLeayMethod = {
58     RAND_seed, RAND_bytes,        RAND_cleanup,
59     RAND_add,  RAND_pseudo_bytes, RAND_status,
60 };
61 
RAND_SSLeay(void)62 RAND_METHOD *RAND_SSLeay(void) { return (RAND_METHOD *)&kSSLeayMethod; }
63 
RAND_OpenSSL(void)64 RAND_METHOD *RAND_OpenSSL(void) { return RAND_SSLeay(); }
65 
RAND_get_rand_method(void)66 const RAND_METHOD *RAND_get_rand_method(void) { return RAND_SSLeay(); }
67 
RAND_set_rand_method(const RAND_METHOD * method)68 int RAND_set_rand_method(const RAND_METHOD *method) { return 1; }
69 
RAND_cleanup(void)70 void RAND_cleanup(void) {}
71 
RAND_get_system_entropy_for_custom_prng(uint8_t * buf,size_t len)72 void RAND_get_system_entropy_for_custom_prng(uint8_t *buf, size_t len) {
73   if (len > 256) {
74     abort();
75   }
76   CRYPTO_sysrand_for_seed(buf, len);
77 }
78