fips_rand.c revision 296465
1/* ====================================================================
2 * Copyright (c) 2007 The OpenSSL Project.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in
13 *    the documentation and/or other materials provided with the
14 *    distribution.
15 *
16 * 3. All advertising materials mentioning features or use of this
17 *    software must display the following acknowledgment:
18 *    "This product includes software developed by the OpenSSL Project
19 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20 *
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 *    endorse or promote products derived from this software without
23 *    prior written permission. For written permission, please contact
24 *    openssl-core@openssl.org.
25 *
26 * 5. Products derived from this software may not be called "OpenSSL"
27 *    nor may "OpenSSL" appear in their names without prior written
28 *    permission of the OpenSSL Project.
29 *
30 * 6. Redistributions of any form whatsoever must retain the following
31 *    acknowledgment:
32 *    "This product includes software developed by the OpenSSL Project
33 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 *
48 */
49
50/*
51 * This is a FIPS approved AES PRNG based on ANSI X9.31 A.2.4.
52 */
53
54#include "e_os.h"
55
56/*
57 * If we don't define _XOPEN_SOURCE_EXTENDED, struct timeval won't be defined
58 * and gettimeofday() won't be declared with strict compilers like DEC C in
59 * ANSI C mode.
60 */
61#ifndef _XOPEN_SOURCE_EXTENDED
62# define _XOPEN_SOURCE_EXTENDED 1
63#endif
64
65#include <openssl/rand.h>
66#include <openssl/aes.h>
67#include <openssl/err.h>
68#include <openssl/fips_rand.h>
69#ifndef OPENSSL_SYS_WIN32
70# include <sys/time.h>
71#endif
72#include <assert.h>
73#ifndef OPENSSL_SYS_WIN32
74# ifdef OPENSSL_UNISTD
75#  include OPENSSL_UNISTD
76# else
77#  include <unistd.h>
78# endif
79#endif
80#include <string.h>
81#include <openssl/fips.h>
82#include "fips_locl.h"
83
84#ifdef OPENSSL_FIPS
85
86void *OPENSSL_stderr(void);
87
88# define AES_BLOCK_LENGTH        16
89
90/* AES FIPS PRNG implementation */
91
92typedef struct {
93    int seeded;
94    int keyed;
95    int test_mode;
96    int second;
97    int error;
98    unsigned long counter;
99    AES_KEY ks;
100    int vpos;
101    /* Temporary storage for key if it equals seed length */
102    unsigned char tmp_key[AES_BLOCK_LENGTH];
103    unsigned char V[AES_BLOCK_LENGTH];
104    unsigned char DT[AES_BLOCK_LENGTH];
105    unsigned char last[AES_BLOCK_LENGTH];
106} FIPS_PRNG_CTX;
107
108static FIPS_PRNG_CTX sctx;
109
110static int fips_prng_fail = 0;
111
112void FIPS_rng_stick(void)
113{
114    fips_prng_fail = 1;
115}
116
117static void fips_rand_prng_reset(FIPS_PRNG_CTX * ctx)
118{
119    ctx->seeded = 0;
120    ctx->keyed = 0;
121    ctx->test_mode = 0;
122    ctx->counter = 0;
123    ctx->second = 0;
124    ctx->error = 0;
125    ctx->vpos = 0;
126    OPENSSL_cleanse(ctx->V, AES_BLOCK_LENGTH);
127    OPENSSL_cleanse(&ctx->ks, sizeof(AES_KEY));
128}
129
130static int fips_set_prng_key(FIPS_PRNG_CTX * ctx,
131                             const unsigned char *key,
132                             FIPS_RAND_SIZE_T keylen)
133{
134    FIPS_selftest_check();
135    if (keylen != 16 && keylen != 24 && keylen != 32) {
136        /* error: invalid key size */
137        return 0;
138    }
139    AES_set_encrypt_key(key, keylen << 3, &ctx->ks);
140    if (keylen == 16) {
141        memcpy(ctx->tmp_key, key, 16);
142        ctx->keyed = 2;
143    } else
144        ctx->keyed = 1;
145    ctx->seeded = 0;
146    ctx->second = 0;
147    return 1;
148}
149
150static int fips_set_prng_seed(FIPS_PRNG_CTX * ctx,
151                              const unsigned char *seed,
152                              FIPS_RAND_SIZE_T seedlen)
153{
154    int i;
155    if (!ctx->keyed)
156        return 0;
157    /* In test mode seed is just supplied data */
158    if (ctx->test_mode) {
159        if (seedlen != AES_BLOCK_LENGTH)
160            return 0;
161        memcpy(ctx->V, seed, AES_BLOCK_LENGTH);
162        ctx->seeded = 1;
163        return 1;
164    }
165    /* Outside test mode XOR supplied data with existing seed */
166    for (i = 0; i < seedlen; i++) {
167        ctx->V[ctx->vpos++] ^= seed[i];
168        if (ctx->vpos == AES_BLOCK_LENGTH) {
169            ctx->vpos = 0;
170            /*
171             * Special case if first seed and key length equals block size
172             * check key and seed do not match.
173             */
174            if (ctx->keyed == 2) {
175                if (!memcmp(ctx->tmp_key, ctx->V, 16)) {
176                    RANDerr(RAND_F_FIPS_SET_PRNG_SEED,
177                            RAND_R_PRNG_SEED_MUST_NOT_MATCH_KEY);
178                    return 0;
179                }
180                OPENSSL_cleanse(ctx->tmp_key, 16);
181                ctx->keyed = 1;
182            }
183            ctx->seeded = 1;
184        }
185    }
186    return 1;
187}
188
189static int fips_set_test_mode(FIPS_PRNG_CTX * ctx)
190{
191    if (ctx->keyed) {
192        RANDerr(RAND_F_FIPS_SET_TEST_MODE, RAND_R_PRNG_KEYED);
193        return 0;
194    }
195    ctx->test_mode = 1;
196    return 1;
197}
198
199int FIPS_rand_test_mode(void)
200{
201    return fips_set_test_mode(&sctx);
202}
203
204int FIPS_rand_set_dt(unsigned char *dt)
205{
206    if (!sctx.test_mode) {
207        RANDerr(RAND_F_FIPS_RAND_SET_DT, RAND_R_NOT_IN_TEST_MODE);
208        return 0;
209    }
210    memcpy(sctx.DT, dt, AES_BLOCK_LENGTH);
211    return 1;
212}
213
214static void fips_get_dt(FIPS_PRNG_CTX * ctx)
215{
216# ifdef OPENSSL_SYS_WIN32
217    FILETIME ft;
218# else
219    struct timeval tv;
220# endif
221    unsigned char *buf = ctx->DT;
222
223# ifndef GETPID_IS_MEANINGLESS
224    unsigned long pid;
225# endif
226
227# ifdef OPENSSL_SYS_WIN32
228    GetSystemTimeAsFileTime(&ft);
229    buf[0] = (unsigned char)(ft.dwHighDateTime & 0xff);
230    buf[1] = (unsigned char)((ft.dwHighDateTime >> 8) & 0xff);
231    buf[2] = (unsigned char)((ft.dwHighDateTime >> 16) & 0xff);
232    buf[3] = (unsigned char)((ft.dwHighDateTime >> 24) & 0xff);
233    buf[4] = (unsigned char)(ft.dwLowDateTime & 0xff);
234    buf[5] = (unsigned char)((ft.dwLowDateTime >> 8) & 0xff);
235    buf[6] = (unsigned char)((ft.dwLowDateTime >> 16) & 0xff);
236    buf[7] = (unsigned char)((ft.dwLowDateTime >> 24) & 0xff);
237# else
238    gettimeofday(&tv, NULL);
239    buf[0] = (unsigned char)(tv.tv_sec & 0xff);
240    buf[1] = (unsigned char)((tv.tv_sec >> 8) & 0xff);
241    buf[2] = (unsigned char)((tv.tv_sec >> 16) & 0xff);
242    buf[3] = (unsigned char)((tv.tv_sec >> 24) & 0xff);
243    buf[4] = (unsigned char)(tv.tv_usec & 0xff);
244    buf[5] = (unsigned char)((tv.tv_usec >> 8) & 0xff);
245    buf[6] = (unsigned char)((tv.tv_usec >> 16) & 0xff);
246    buf[7] = (unsigned char)((tv.tv_usec >> 24) & 0xff);
247# endif
248    buf[8] = (unsigned char)(ctx->counter & 0xff);
249    buf[9] = (unsigned char)((ctx->counter >> 8) & 0xff);
250    buf[10] = (unsigned char)((ctx->counter >> 16) & 0xff);
251    buf[11] = (unsigned char)((ctx->counter >> 24) & 0xff);
252
253    ctx->counter++;
254
255# ifndef GETPID_IS_MEANINGLESS
256    pid = (unsigned long)getpid();
257    buf[12] = (unsigned char)(pid & 0xff);
258    buf[13] = (unsigned char)((pid >> 8) & 0xff);
259    buf[14] = (unsigned char)((pid >> 16) & 0xff);
260    buf[15] = (unsigned char)((pid >> 24) & 0xff);
261# endif
262}
263
264static int fips_rand(FIPS_PRNG_CTX * ctx,
265                     unsigned char *out, FIPS_RAND_SIZE_T outlen)
266{
267    unsigned char R[AES_BLOCK_LENGTH], I[AES_BLOCK_LENGTH];
268    unsigned char tmp[AES_BLOCK_LENGTH];
269    int i;
270    if (ctx->error) {
271        RANDerr(RAND_F_FIPS_RAND, RAND_R_PRNG_ERROR);
272        return 0;
273    }
274    if (!ctx->keyed) {
275        RANDerr(RAND_F_FIPS_RAND, RAND_R_NO_KEY_SET);
276        return 0;
277    }
278    if (!ctx->seeded) {
279        RANDerr(RAND_F_FIPS_RAND, RAND_R_PRNG_NOT_SEEDED);
280        return 0;
281    }
282    for (;;) {
283        if (!ctx->test_mode)
284            fips_get_dt(ctx);
285        AES_encrypt(ctx->DT, I, &ctx->ks);
286        for (i = 0; i < AES_BLOCK_LENGTH; i++)
287            tmp[i] = I[i] ^ ctx->V[i];
288        AES_encrypt(tmp, R, &ctx->ks);
289        for (i = 0; i < AES_BLOCK_LENGTH; i++)
290            tmp[i] = R[i] ^ I[i];
291        AES_encrypt(tmp, ctx->V, &ctx->ks);
292        /* Continuous PRNG test */
293        if (ctx->second) {
294            if (fips_prng_fail)
295                memcpy(ctx->last, R, AES_BLOCK_LENGTH);
296            if (!memcmp(R, ctx->last, AES_BLOCK_LENGTH)) {
297                RANDerr(RAND_F_FIPS_RAND, RAND_R_PRNG_STUCK);
298                ctx->error = 1;
299                fips_set_selftest_fail();
300                return 0;
301            }
302        }
303        memcpy(ctx->last, R, AES_BLOCK_LENGTH);
304        if (!ctx->second) {
305            ctx->second = 1;
306            if (!ctx->test_mode)
307                continue;
308        }
309
310        if (outlen <= AES_BLOCK_LENGTH) {
311            memcpy(out, R, outlen);
312            break;
313        }
314
315        memcpy(out, R, AES_BLOCK_LENGTH);
316        out += AES_BLOCK_LENGTH;
317        outlen -= AES_BLOCK_LENGTH;
318    }
319    return 1;
320}
321
322int FIPS_rand_set_key(const unsigned char *key, FIPS_RAND_SIZE_T keylen)
323{
324    int ret;
325    CRYPTO_w_lock(CRYPTO_LOCK_RAND);
326    ret = fips_set_prng_key(&sctx, key, keylen);
327    CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
328    return ret;
329}
330
331int FIPS_rand_seed(const void *seed, FIPS_RAND_SIZE_T seedlen)
332{
333    int ret;
334    CRYPTO_w_lock(CRYPTO_LOCK_RAND);
335    ret = fips_set_prng_seed(&sctx, seed, seedlen);
336    CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
337    return ret;
338}
339
340int FIPS_rand_bytes(unsigned char *out, FIPS_RAND_SIZE_T count)
341{
342    int ret;
343    CRYPTO_w_lock(CRYPTO_LOCK_RAND);
344    ret = fips_rand(&sctx, out, count);
345    CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
346    return ret;
347}
348
349int FIPS_rand_status(void)
350{
351    int ret;
352    CRYPTO_r_lock(CRYPTO_LOCK_RAND);
353    ret = sctx.seeded;
354    CRYPTO_r_unlock(CRYPTO_LOCK_RAND);
355    return ret;
356}
357
358void FIPS_rand_reset(void)
359{
360    CRYPTO_w_lock(CRYPTO_LOCK_RAND);
361    fips_rand_prng_reset(&sctx);
362    CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
363}
364
365static void fips_do_rand_seed(const void *seed, FIPS_RAND_SIZE_T seedlen)
366{
367    FIPS_rand_seed(seed, seedlen);
368}
369
370static void fips_do_rand_add(const void *seed, FIPS_RAND_SIZE_T seedlen,
371                             double add_entropy)
372{
373    FIPS_rand_seed(seed, seedlen);
374}
375
376static const RAND_METHOD rand_fips_meth = {
377    fips_do_rand_seed,
378    FIPS_rand_bytes,
379    FIPS_rand_reset,
380    fips_do_rand_add,
381    FIPS_rand_bytes,
382    FIPS_rand_status
383};
384
385const RAND_METHOD *FIPS_rand_method(void)
386{
387    return &rand_fips_meth;
388}
389
390#endif
391