Coverage Report

Created: 2026-03-11 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/rand.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#ifdef HAVE_ARPA_INET_H
27
#include <arpa/inet.h>
28
#endif
29
30
#include "urldata.h"
31
#include "vtls/vtls.h"
32
#include "curl_trc.h"
33
#include "rand.h"
34
#include "escape.h"
35
36
#ifdef _WIN32
37
38
#ifndef CURL_WINDOWS_UWP
39
#  include <bcrypt.h>
40
#  ifdef _MSC_VER
41
#    pragma comment(lib, "bcrypt.lib")
42
#  endif
43
#  ifndef STATUS_SUCCESS
44
#  define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
45
#  endif
46
#endif
47
48
CURLcode Curl_win32_random(unsigned char *entropy, size_t length)
49
{
50
  memset(entropy, 0, length);
51
52
#ifndef CURL_WINDOWS_UWP
53
  if(BCryptGenRandom(NULL, entropy, (ULONG)length,
54
                     BCRYPT_USE_SYSTEM_PREFERRED_RNG) != STATUS_SUCCESS)
55
    return CURLE_FAILED_INIT;
56
57
  return CURLE_OK;
58
#else
59
  return CURLE_NOT_BUILT_IN;
60
#endif
61
}
62
#endif
63
64
#ifndef USE_SSL
65
/* ---- possibly non-cryptographic version following ---- */
66
static CURLcode weak_random(struct Curl_easy *data,
67
                            unsigned char *entropy,
68
                            size_t length) /* always 4, size of int */
69
{
70
  unsigned int r;
71
  DEBUGASSERT(length == sizeof(int));
72
73
  /* Trying cryptographically secure functions first */
74
#ifdef _WIN32
75
  (void)data;
76
  {
77
    CURLcode result = Curl_win32_random(entropy, length);
78
    if(result != CURLE_NOT_BUILT_IN)
79
      return result;
80
  }
81
#endif
82
83
#ifdef HAVE_ARC4RANDOM
84
  (void)data;
85
  r = (unsigned int)arc4random();
86
  memcpy(entropy, &r, length);
87
#else
88
  infof(data, "WARNING: using weak random seed");
89
  {
90
    static unsigned int randseed;
91
    static bool seeded = FALSE;
92
    unsigned int rnd;
93
    if(!seeded) {
94
      struct curltime now;
95
      curlx_pnow(&now);
96
      randseed += (unsigned int)now.tv_usec + (unsigned int)now.tv_sec;
97
      randseed = randseed * 1103515245 + 12345;
98
      randseed = randseed * 1103515245 + 12345;
99
      randseed = randseed * 1103515245 + 12345;
100
      seeded = TRUE;
101
    }
102
103
    /* Return an unsigned 32-bit pseudo-random number. */
104
    r = randseed = randseed * 1103515245 + 12345;
105
    rnd = (r << 16) | ((r >> 16) & 0xFFFF);
106
    memcpy(entropy, &rnd, length);
107
  }
108
#endif
109
  return CURLE_OK;
110
}
111
#endif
112
113
static CURLcode randit(struct Curl_easy *data, unsigned int *rnd,
114
                       bool env_override)
115
5.56k
{
116
5.56k
#ifdef DEBUGBUILD
117
5.56k
  if(env_override) {
118
5.56k
    char *force_entropy = getenv("CURL_ENTROPY");
119
5.56k
    if(force_entropy) {
120
0
      static unsigned int randseed;
121
0
      static bool seeded = FALSE;
122
123
0
      if(!seeded) {
124
0
        unsigned int seed = 0;
125
0
        size_t elen = strlen(force_entropy);
126
0
        size_t clen = sizeof(seed);
127
0
        size_t min = elen < clen ? elen : clen;
128
0
        memcpy((char *)&seed, force_entropy, min);
129
0
        randseed = ntohl(seed);
130
0
        seeded = TRUE;
131
0
      }
132
0
      else
133
0
        randseed++;
134
0
      *rnd = randseed;
135
0
      return CURLE_OK;
136
0
    }
137
5.56k
  }
138
#else
139
  (void)env_override;
140
#endif
141
142
  /* data may be NULL! */
143
5.56k
#ifdef USE_SSL
144
5.56k
  return Curl_ssl_random(data, (unsigned char *)rnd, sizeof(*rnd));
145
#else
146
  return weak_random(data, (unsigned char *)rnd, sizeof(*rnd));
147
#endif
148
5.56k
}
149
150
/*
151
 * Curl_rand() stores 'num' number of random unsigned characters in the buffer
152
 * 'rnd' points to.
153
 *
154
 * If libcurl is built without TLS support or arc4random, this function will
155
 * use "weak" random.
156
 *
157
 * When built *with* TLS support, it will return error if it cannot provide
158
 * strong random values.
159
 *
160
 * NOTE: 'data' may be passed in as NULL when coming from external API without
161
 * easy handle!
162
 *
163
 */
164
165
CURLcode Curl_rand_bytes(struct Curl_easy *data,
166
#ifdef DEBUGBUILD
167
                         bool env_override,
168
#endif
169
                         unsigned char *rnd, size_t num)
170
0
{
171
0
  CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
172
#ifndef DEBUGBUILD
173
  const bool env_override = FALSE;
174
#endif
175
176
0
  DEBUGASSERT(num);
177
178
0
  while(num) {
179
0
    unsigned int r;
180
0
    size_t left = num < sizeof(unsigned int) ? num : sizeof(unsigned int);
181
182
0
    result = randit(data, &r, env_override);
183
0
    if(result)
184
0
      return result;
185
186
0
    while(left) {
187
0
      *rnd++ = (unsigned char)(r & 0xFF);
188
0
      r >>= 8;
189
0
      --num;
190
0
      --left;
191
0
    }
192
0
  }
193
194
0
  return result;
195
0
}
196
197
/*
198
 * Curl_rand_hex() fills the 'rnd' buffer with a given 'num' size with random
199
 * hexadecimal digits PLUS a null-terminating byte. It must be an odd number
200
 * size.
201
 */
202
203
CURLcode Curl_rand_hex(struct Curl_easy *data, unsigned char *rnd, size_t num)
204
0
{
205
0
  CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
206
0
  unsigned char buffer[128];
207
0
  DEBUGASSERT(num > 1);
208
209
0
  if((num / 2 >= sizeof(buffer)) || !(num & 1)) {
210
    /* make sure it fits in the local buffer and that it is an odd number! */
211
0
    DEBUGF(infof(data, "invalid buffer size with Curl_rand_hex"));
212
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
213
0
  }
214
215
0
  num--; /* save one for null-termination */
216
217
0
  result = Curl_rand(data, buffer, num / 2);
218
0
  if(result)
219
0
    return result;
220
221
0
  Curl_hexencode(buffer, num / 2, rnd, num + 1);
222
0
  return result;
223
0
}
224
225
/*
226
 * Curl_rand_alnum() fills the 'rnd' buffer with a given 'num' size with random
227
 * alphanumerical chars PLUS a null-terminating byte.
228
 */
229
230
static const char alnum[] =
231
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
232
233
CURLcode Curl_rand_alnum(struct Curl_easy *data, unsigned char *rnd,
234
                         size_t num)
235
253
{
236
253
  CURLcode result = CURLE_OK;
237
253
  const unsigned int alnumspace = sizeof(alnum) - 1;
238
253
  unsigned int r;
239
253
  DEBUGASSERT(num > 1);
240
241
253
  num--; /* save one for null-termination */
242
243
5.81k
  while(num) {
244
5.56k
    do {
245
5.56k
      result = randit(data, &r, TRUE);
246
5.56k
      if(result)
247
0
        return result;
248
5.56k
    } while(r >= (UINT_MAX - UINT_MAX % alnumspace));
249
250
5.56k
    *rnd++ = (unsigned char)alnum[r % alnumspace];
251
5.56k
    num--;
252
5.56k
  }
253
253
  *rnd = 0;
254
255
253
  return result;
256
253
}