| 1 |
/* |
| 2 |
* rand.go - Reader used to generate secure random data for fscrypt. |
| 3 |
* |
| 4 |
* Copyright 2017 Google Inc. |
| 5 |
* Author: Joe Richey ([email protected]) |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 8 |
* use this file except in compliance with the License. You may obtain a copy of |
| 9 |
* the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, software |
| 14 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 |
* License for the specific language governing permissions and limitations under |
| 17 |
* the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package crypto |
| 21 |
|
| 22 |
import ( |
| 23 |
"io" |
| 24 |
|
| 25 |
"github.com/pkg/errors" |
| 26 |
"golang.org/x/sys/unix" |
| 27 |
) |
| 28 |
|
| 29 |
// NewRandomBuffer uses the Linux Getrandom() syscall to create random bytes. If |
| 30 |
// the operating system has insufficient randomness, the buffer creation will |
| 31 |
// fail. This is an improvement over Go's built-in crypto/rand which will still |
| 32 |
// return bytes if the system has insufficiency entropy. |
| 33 |
// |
| 34 |
// See: https://github.com/golang/go/issues/19274 |
| 35 |
// |
| 36 |
// While this syscall was only introduced in Kernel v3.17, it predates the |
| 37 |
// introduction of filesystem encryption, so it introduces no additional |
| 38 |
// compatibility issues. |
| 39 |
func NewRandomBuffer(length int) ([]byte, error) { |
| 40 |
buffer := make([]byte, length) |
| 41 |
if _, err := io.ReadFull(randReader{}, buffer); err != nil { |
| 42 |
return nil, err |
| 43 |
} |
| 44 |
return buffer, nil |
| 45 |
} |
| 46 |
|
| 47 |
// NewRandomKey creates a random key of the specified length. This function uses |
| 48 |
// the same random number generation process as NewRandomBuffer. |
| 49 |
func NewRandomKey(length int) (*Key, error) { |
| 50 |
return NewFixedLengthKeyFromReader(randReader{}, length) |
| 51 |
} |
| 52 |
|
| 53 |
// NewRandomPassphrase creates a random passphrase of the specified length |
| 54 |
// containing random alphabetic characters. |
| 55 |
func NewRandomPassphrase(length int) (*Key, error) { |
| 56 |
chars := []byte("abcdefghijklmnopqrstuvwxyz") |
| 57 |
passphrase, err := NewBlankKey(length) |
| 58 |
if err != nil { |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
for i := 0; i < length; { |
| 62 |
// Get some random bytes. |
| 63 |
raw, err := NewRandomKey((length - i) * 2) |
| 64 |
if err != nil { |
| 65 |
return nil, err |
| 66 |
} |
| 67 |
// Translate the random bytes into random characters. |
| 68 |
for _, b := range raw.data { |
| 69 |
if int(b) >= 256-(256%len(chars)) { |
| 70 |
// Avoid bias towards the first characters in the list. |
| 71 |
continue |
| 72 |
} |
| 73 |
c := chars[int(b)%len(chars)] |
| 74 |
passphrase.data[i] = c |
| 75 |
i++ |
| 76 |
if i == length { |
| 77 |
break |
| 78 |
} |
| 79 |
} |
| 80 |
raw.Wipe() |
| 81 |
} |
| 82 |
return passphrase, nil |
| 83 |
} |
| 84 |
|
| 85 |
// randReader just calls into Getrandom, so no internal data is needed. |
| 86 |
type randReader struct{} |
| 87 |
|
| 88 |
func (r randReader) Read(buffer []byte) (int, error) { |
| 89 |
n, err := unix.Getrandom(buffer, unix.GRND_NONBLOCK) |
| 90 |
switch err { |
| 91 |
case nil: |
| 92 |
return n, nil |
| 93 |
case unix.EAGAIN: |
| 94 |
err = errors.New("insufficient entropy in pool") |
| 95 |
case unix.ENOSYS: |
| 96 |
err = errors.New("kernel must be v3.17 or later") |
| 97 |
} |
| 98 |
return 0, errors.Wrap(err, "getrandom() failed") |
| 99 |
} |