summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_random.c
blob: 8290488c8258ea84848b6452704425ad70cf9bd5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */


#include <nxt_main.h>


/*
 * The pseudorandom generator based on OpenBSD arc4random.  Although it is
 * usually stated that arc4random uses RC4 pseudorandom generation algorithm
 * they are actually different in nxt_random_add().
 */


#define NXT_RANDOM_KEY_SIZE  128


nxt_inline void nxt_random_start_schedule(nxt_random_t *r);
static void nxt_random_stir(nxt_random_t *r);
static void nxt_random_add(nxt_random_t *r, const u_char *key, uint32_t len);
nxt_inline uint8_t nxt_random_byte(nxt_random_t *r);


void
nxt_random_init(nxt_random_t *r)
{
    nxt_random_start_schedule(r);

    nxt_random_stir(r);
}


nxt_inline void
nxt_random_start_schedule(nxt_random_t *r)
{
    nxt_uint_t  i;

    r->i = 0;
    r->j = 0;

    for (i = 0; i < 256; i++) {
        r->s[i] = i;
    }
}


static void
nxt_random_stir(nxt_random_t *r)
{
    int             fd;
    ssize_t         n;
    struct timeval  tv;
    union {
        uint32_t    value[4];
        u_char      bytes[NXT_RANDOM_KEY_SIZE];
    } key;

#if (NXT_HAVE_GETRANDOM)

    n = getrandom(&key, NXT_RANDOM_KEY_SIZE, 0);

#elif (NXT_HAVE_LINUX_SYS_GETRANDOM)

    /* Linux 3.17 SYS_getrandom. */

    n = syscall(SYS_getrandom, &key, NXT_RANDOM_KEY_SIZE, 0);

#elif (NXT_HAVE_GETENTROPY || NXT_HAVE_GETENTROPY_SYS_RANDOM)

    n = 0;

    if (getentropy(&key, NXT_RANDOM_KEY_SIZE) == 0) {
        n = NXT_RANDOM_KEY_SIZE;
    }

#else

    n = 0;

#endif

    if (n != NXT_RANDOM_KEY_SIZE) {
        fd = open("/dev/urandom", O_RDONLY);

        if (fd >= 0) {
            n = read(fd, &key, NXT_RANDOM_KEY_SIZE);
            (void) close(fd);
        }
    }

    if (n != NXT_RANDOM_KEY_SIZE) {
        (void) gettimeofday(&tv, NULL);

        /* XOR with stack garbage. */

        key.value[0] ^= tv.tv_usec;
        key.value[1] ^= tv.tv_sec;
        key.value[2] ^= nxt_pid;
        key.value[3] ^= (uintptr_t) nxt_thread_tid(nxt_thread());
    }

    nxt_random_add(r, key.bytes, NXT_RANDOM_KEY_SIZE);

    /* Drop the first 3072 bytes. */
    for (n = 3072; n != 0; n--) {
        (void) nxt_random_byte(r);
    }

    /* Stir again after 1,600,000 bytes. */
    r->count = 400000;
}


static void
nxt_random_add(nxt_random_t *r, const u_char *key, uint32_t len)
{
    uint8_t   val;
    uint32_t  n;

    for (n = 0; n < 256; n++) {
        val = r->s[r->i];
        r->j += val + key[n % len];

        r->s[r->i] = r->s[r->j];
        r->s[r->j] = val;

        r->i++;
    }

    /* This index is not decremented in RC4 algorithm. */
    r->i--;

    r->j = r->i;
}


uint32_t
nxt_random(nxt_random_t *r)
{
    uint32_t  val;

    r->count--;

    if (r->count <= 0) {
        nxt_random_stir(r);
    }

    val  = (uint32_t) nxt_random_byte(r) << 24;
    val |= (uint32_t) nxt_random_byte(r) << 16;
    val |= (uint32_t) nxt_random_byte(r) << 8;
    val |= (uint32_t) nxt_random_byte(r);

    return val;
}


nxt_inline uint8_t
nxt_random_byte(nxt_random_t *r)
{
    uint8_t  si, sj;

    r->i++;
    si = r->s[r->i];
    r->j += si;

    sj = r->s[r->j];
    r->s[r->i] = sj;
    r->s[r->j] = si;

    si += sj;

    return r->s[si];
}


#if (NXT_TESTS)

nxt_int_t
nxt_random_test(nxt_thread_t *thr)
{
    nxt_uint_t    n;
    nxt_random_t  r;

    nxt_random_start_schedule(&r);

    r.count = 400000;

    nxt_random_add(&r, (u_char *) "arc4random", nxt_length("arc4random"));

    /*
     * Test arc4random() numbers.
     * RC4 pseudorandom numbers would be 0x4642AFC3 and 0xBAF0FFF0.
     */

    if (nxt_random(&r) == 0xD6270B27) {

        for (n = 100000; n != 0; n--) {
            (void) nxt_random(&r);
        }

        if (nxt_random(&r) == 0x6FCAE186) {
            nxt_log_error(NXT_LOG_NOTICE, thr->log, "arc4random test passed");

            return NXT_OK;
        }
    }

    nxt_log_error(NXT_LOG_NOTICE, thr->log, "arc4random test failed");

    return NXT_ERROR;
}

#endif