summaryrefslogtreecommitdiff
path: root/crypto/rand/rand_meth.c
blob: 276763057db268975c144d93f463e5b8bcd26f59 (plain)
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
/*
 * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <openssl/evp.h>
#include <openssl/rand.h>
#include "rand_local.h"

/* Implements the default OpenSSL RAND_add() method */
static int drbg_add(const void *buf, int num, double randomness)
{
    EVP_RAND_CTX *drbg = RAND_get0_primary(NULL);

    if (drbg == NULL || num <= 0)
        return 0;

    return EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num);
}

/* Implements the default OpenSSL RAND_seed() method */
static int drbg_seed(const void *buf, int num)
{
    return drbg_add(buf, num, num);
}

/* Implements the default OpenSSL RAND_status() method */
static int drbg_status(void)
{
    EVP_RAND_CTX *drbg = RAND_get0_primary(NULL);

    if (drbg == NULL)
        return 0;

    return  EVP_RAND_get_state(drbg) == EVP_RAND_STATE_READY ? 1 : 0;
}

/* Implements the default OpenSSL RAND_bytes() method */
static int drbg_bytes(unsigned char *out, int count)
{
    EVP_RAND_CTX *drbg = RAND_get0_public(NULL);

    if (drbg == NULL)
        return 0;

    return EVP_RAND_generate(drbg, out, count, 0, 0, NULL, 0);
}

RAND_METHOD ossl_rand_meth = {
    drbg_seed,
    drbg_bytes,
    NULL,
    drbg_add,
    drbg_bytes,
    drbg_status
};

RAND_METHOD *RAND_OpenSSL(void)
{
    return &ossl_rand_meth;
}