github.com/flowerwrong/netstack@v0.0.0-20191009141956-e5848263af28/rand/rand_linux.go (about)

     1  // Copyright 2018 The gVisor 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  //     http://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  // Package rand implements a cryptographically secure pseudorandom number
    16  // generator.
    17  package rand
    18  
    19  import (
    20  	"crypto/rand"
    21  	"io"
    22  	"sync"
    23  
    24  	"golang.org/x/sys/unix"
    25  )
    26  
    27  // reader implements an io.Reader that returns pseudorandom bytes.
    28  type reader struct {
    29  	once         sync.Once
    30  	useGetrandom bool
    31  }
    32  
    33  // Read implements io.Reader.Read.
    34  func (r *reader) Read(p []byte) (int, error) {
    35  	r.once.Do(func() {
    36  		_, err := unix.Getrandom(p, 0)
    37  		if err != unix.ENOSYS {
    38  			r.useGetrandom = true
    39  		}
    40  	})
    41  
    42  	if r.useGetrandom {
    43  		return unix.Getrandom(p, 0)
    44  	}
    45  	return rand.Read(p)
    46  }
    47  
    48  // Reader is the default reader.
    49  var Reader io.Reader = &reader{}
    50  
    51  // Read reads from the default reader.
    52  func Read(b []byte) (int, error) {
    53  	return io.ReadFull(Reader, b)
    54  }
    55  
    56  // Init can be called to make sure /dev/urandom is pre-opened on kernels that
    57  // do not support getrandom(2).
    58  func Init() error {
    59  	p := make([]byte, 1)
    60  	_, err := Read(p)
    61  	return err
    62  }