summaryrefslogtreecommitdiff
path: root/lib/crypto/c_src/rand.c
blob: 82bf2c74671560a8032be85f62908c38defa7202 (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
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
/*
 * %CopyrightBegin%
 *
 * Copyright Ericsson AB 2010-2020. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * %CopyrightEnd%
 */

#include "rand.h"
#include "bn.h"

ERL_NIF_TERM strong_rand_bytes_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{/* (Bytes) */
    unsigned bytes;
    unsigned char* data;
    ERL_NIF_TERM ret;

    ASSERT(argc == 1);

    if (!enif_get_uint(env, argv[0], &bytes))
        goto bad_arg;
    if (bytes > INT_MAX)
        goto bad_arg;

    if ((data = enif_make_new_binary(env, bytes, &ret)) == NULL)
        goto err;
    if (RAND_bytes(data, (int)bytes) != 1)
        goto err;

    ERL_VALGRIND_MAKE_MEM_DEFINED(data, bytes);
    return ret;

 bad_arg:
    return enif_make_badarg(env);

 err:
    return atom_false;
}

ERL_NIF_TERM strong_rand_range_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{/* (Range) */
    BIGNUM *bn_range = NULL, *bn_rand = NULL;
    ERL_NIF_TERM ret;

    ASSERT(argc == 1);

    if (!get_bn_from_bin(env, argv[0], &bn_range))
        goto bad_arg;

    if ((bn_rand = BN_new()) == NULL)
        goto err;
    if (!BN_rand_range(bn_rand, bn_range))
        goto err;

    if ((ret = bin_from_bn(env, bn_rand)) == atom_error)
        goto err;
    goto done;

 bad_arg:
    return enif_make_badarg(env);

 err:
    ret = atom_false;

 done:
    if (bn_rand)
        BN_free(bn_rand);
    if (bn_range)
        BN_free(bn_range);
    return ret;
}

ERL_NIF_TERM rand_uniform_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{/* (Lo,Hi) */
    BIGNUM *bn_from = NULL, *bn_to = NULL, *bn_rand = NULL;
    unsigned char* data;
    int dlen;
    ERL_NIF_TERM ret;

    ASSERT(argc == 2);

    if (!get_bn_from_mpint(env, argv[0], &bn_from))
        goto bad_arg;
    if (!get_bn_from_mpint(env, argv[1], &bn_rand))
        goto bad_arg;

    if ((bn_to = BN_new()) == NULL)
        goto err;

    if (!BN_sub(bn_to, bn_rand, bn_from))
        goto err;
    if (!BN_pseudo_rand_range(bn_rand, bn_to))
        goto err;
    if (!BN_add(bn_rand, bn_rand, bn_from))
        goto err;

    if ((dlen = BN_num_bytes(bn_rand)) < 0)
        goto err;
    if ((data = enif_make_new_binary(env, (size_t)dlen+4, &ret)) == NULL)
        goto err;

    put_uint32(data, (unsigned int)dlen);
    BN_bn2bin(bn_rand, data+4);
    ERL_VALGRIND_MAKE_MEM_DEFINED(data+4, dlen);
    goto done;

 bad_arg:
 err:
    ret = enif_make_badarg(env);

 done:
    if (bn_rand)
        BN_free(bn_rand);
    if (bn_from)
        BN_free(bn_from);
    if (bn_to)
        BN_free(bn_to);
    return ret;
}

ERL_NIF_TERM rand_seed_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{/* (Seed) */
    ErlNifBinary seed_bin;

    ASSERT(argc == 1);

    if (!enif_inspect_binary(env, argv[0], &seed_bin))
        goto bad_arg;
    if (seed_bin.size > INT_MAX)
        goto bad_arg;

    RAND_seed(seed_bin.data, (int)seed_bin.size);
    return atom_ok;

 bad_arg:
    return enif_make_badarg(env);
}