OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [crypto/] [rand/] [rand_unix.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2010 The Go Authors.  All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
// +build darwin freebsd linux netbsd openbsd
6
 
7
// Unix cryptographically secure pseudorandom number
8
// generator.
9
 
10
package rand
11
 
12
import (
13
        "bufio"
14
        "crypto/aes"
15
        "io"
16
        "os"
17
        "sync"
18
        "time"
19
)
20
 
21
// Easy implementation: read from /dev/urandom.
22
// This is sufficient on Linux, OS X, and FreeBSD.
23
 
24
func init() { Reader = &devReader{name: "/dev/urandom"} }
25
 
26
// A devReader satisfies reads by reading the file named name.
27
type devReader struct {
28
        name string
29
        f    io.Reader
30
        mu   sync.Mutex
31
}
32
 
33
func (r *devReader) Read(b []byte) (n int, err error) {
34
        r.mu.Lock()
35
        defer r.mu.Unlock()
36
        if r.f == nil {
37
                f, err := os.Open(r.name)
38
                if f == nil {
39
                        return 0, err
40
                }
41
                r.f = bufio.NewReader(f)
42
        }
43
        return r.f.Read(b)
44
}
45
 
46
// Alternate pseudo-random implementation for use on
47
// systems without a reliable /dev/urandom.  So far we
48
// haven't needed it.
49
 
50
// newReader returns a new pseudorandom generator that
51
// seeds itself by reading from entropy.  If entropy == nil,
52
// the generator seeds itself by reading from the system's
53
// random number generator, typically /dev/random.
54
// The Read method on the returned reader always returns
55
// the full amount asked for, or else it returns an error.
56
//
57
// The generator uses the X9.31 algorithm with AES-128,
58
// reseeding after every 1 MB of generated data.
59
func newReader(entropy io.Reader) io.Reader {
60
        if entropy == nil {
61
                entropy = &devReader{name: "/dev/random"}
62
        }
63
        return &reader{entropy: entropy}
64
}
65
 
66
type reader struct {
67
        mu                   sync.Mutex
68
        budget               int // number of bytes that can be generated
69
        cipher               *aes.Cipher
70
        entropy              io.Reader
71
        time, seed, dst, key [aes.BlockSize]byte
72
}
73
 
74
func (r *reader) Read(b []byte) (n int, err error) {
75
        r.mu.Lock()
76
        defer r.mu.Unlock()
77
        n = len(b)
78
 
79
        for len(b) > 0 {
80
                if r.budget == 0 {
81
                        _, err := io.ReadFull(r.entropy, r.seed[0:])
82
                        if err != nil {
83
                                return n - len(b), err
84
                        }
85
                        _, err = io.ReadFull(r.entropy, r.key[0:])
86
                        if err != nil {
87
                                return n - len(b), err
88
                        }
89
                        r.cipher, err = aes.NewCipher(r.key[0:])
90
                        if err != nil {
91
                                return n - len(b), err
92
                        }
93
                        r.budget = 1 << 20 // reseed after generating 1MB
94
                }
95
                r.budget -= aes.BlockSize
96
 
97
                // ANSI X9.31 (== X9.17) algorithm, but using AES in place of 3DES.
98
                //
99
                // single block:
100
                // t = encrypt(time)
101
                // dst = encrypt(t^seed)
102
                // seed = encrypt(t^dst)
103
                ns := time.Now().UnixNano()
104
                r.time[0] = byte(ns >> 56)
105
                r.time[1] = byte(ns >> 48)
106
                r.time[2] = byte(ns >> 40)
107
                r.time[3] = byte(ns >> 32)
108
                r.time[4] = byte(ns >> 24)
109
                r.time[5] = byte(ns >> 16)
110
                r.time[6] = byte(ns >> 8)
111
                r.time[7] = byte(ns)
112
                r.cipher.Encrypt(r.time[0:], r.time[0:])
113
                for i := 0; i < aes.BlockSize; i++ {
114
                        r.dst[i] = r.time[i] ^ r.seed[i]
115
                }
116
                r.cipher.Encrypt(r.dst[0:], r.dst[0:])
117
                for i := 0; i < aes.BlockSize; i++ {
118
                        r.seed[i] = r.time[i] ^ r.dst[i]
119
                }
120
                r.cipher.Encrypt(r.seed[0:], r.seed[0:])
121
 
122
                m := copy(b, r.dst[0:])
123
                b = b[m:]
124
        }
125
 
126
        return n, nil
127
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.