Coverage Report

Created: 2026-12-01 06:27

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
#include "curlx/winapi.h"
37
38
#ifndef USE_SSL
39
/* ---- possibly non-cryptographic version following ---- */
40
static CURLcode weak_random(struct Curl_easy *data,
41
                            unsigned char *entropy,
42
                            size_t length) /* always 4, size of int */
43
{
44
  unsigned int r;
45
  DEBUGASSERT(length == sizeof(int));
46
47
  /* Trying cryptographically secure functions first */
48
#ifdef _WIN32
49
  (void)data;
50
  {
51
    CURLcode result = curlx_win32_random(entropy, length);
52
    if(result != CURLE_NOT_BUILT_IN)
53
      return result;
54
  }
55
#endif
56
57
#ifdef HAVE_ARC4RANDOM
58
  (void)data;
59
  r = (unsigned int)arc4random();
60
  memcpy(entropy, &r, length);
61
#else
62
  infof(data, "WARNING: using weak random seed");
63
  {
64
    static unsigned int randseed;
65
    static bool seeded = FALSE;
66
    unsigned int rnd;
67
    if(!seeded) {
68
      struct curltime now;
69
      curlx_pnow(&now);
70
      randseed += (unsigned int)now.tv_usec + (unsigned int)now.tv_sec;
71
      randseed = randseed * 1103515245 + 12345;
72
      randseed = randseed * 1103515245 + 12345;
73
      randseed = randseed * 1103515245 + 12345;
74
      seeded = TRUE;
75
    }
76
77
    /* Return an unsigned 32-bit pseudo-random number. */
78
    r = randseed = randseed * 1103515245 + 12345;
79
    rnd = (r << 16) | ((r >> 16) & 0xFFFF);
80
    memcpy(entropy, &rnd, length);
81
  }
82
#endif
83
  return CURLE_OK;
84
}
85
#endif
86
87
static CURLcode randit(struct Curl_easy *data, unsigned int *rnd,
88
                       bool env_override)
89
285k
{
90
285k
#ifdef DEBUGBUILD
91
285k
  if(env_override) {
92
285k
    char *force_entropy = getenv("CURL_ENTROPY");
93
285k
    if(force_entropy) {
94
0
      static unsigned int randseed;
95
0
      static bool seeded = FALSE;
96
97
0
      if(!seeded) {
98
0
        unsigned int seed = 0;
99
0
        size_t elen = strlen(force_entropy);
100
0
        size_t clen = sizeof(seed);
101
0
        size_t min = elen < clen ? elen : clen;
102
0
        memcpy((char *)&seed, force_entropy, min);
103
0
        randseed = ntohl(seed);
104
0
        seeded = TRUE;
105
0
      }
106
0
      else
107
0
        randseed++;
108
0
      *rnd = randseed;
109
0
      return CURLE_OK;
110
0
    }
111
285k
  }
112
#else
113
  (void)env_override;
114
#endif
115
116
  /* data may be NULL! */
117
285k
#ifdef USE_SSL
118
285k
  return Curl_ssl_random(data, (unsigned char *)rnd, sizeof(*rnd));
119
#else
120
  return weak_random(data, (unsigned char *)rnd, sizeof(*rnd));
121
#endif
122
285k
}
123
124
/*
125
 * Curl_rand() stores 'num' number of random unsigned characters in the buffer
126
 * 'rnd' points to.
127
 *
128
 * If libcurl is built without TLS support or arc4random, this function will
129
 * use "weak" random.
130
 *
131
 * When built *with* TLS support, it will return error if it cannot provide
132
 * strong random values.
133
 *
134
 * NOTE: 'data' may be passed in as NULL when coming from external API without
135
 * easy handle!
136
 *
137
 */
138
139
CURLcode Curl_rand_bytes(struct Curl_easy *data,
140
#ifdef DEBUGBUILD
141
                         bool env_override,
142
#endif
143
                         unsigned char *rnd, size_t num)
144
15.3k
{
145
15.3k
  CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
146
#ifndef DEBUGBUILD
147
  const bool env_override = FALSE;
148
#endif
149
150
15.3k
  DEBUGASSERT(num);
151
152
15.3k
#ifdef USE_SSL
153
  /* get all bytes in one call unless they are forced for testing */
154
15.3k
  if(!env_override || !getenv("CURL_ENTROPY"))
155
15.3k
    return Curl_ssl_random(data, rnd, num);
156
0
#endif
157
158
0
  while(num) {
159
0
    unsigned int r;
160
0
    size_t left = num < sizeof(unsigned int) ? num : sizeof(unsigned int);
161
162
0
    result = randit(data, &r, env_override);
163
0
    if(result)
164
0
      return result;
165
166
0
    while(left) {
167
0
      *rnd++ = (unsigned char)(r & 0xFF);
168
0
      r >>= 8;
169
0
      --num;
170
0
      --left;
171
0
    }
172
0
  }
173
174
0
  return result;
175
0
}
176
177
/*
178
 * Curl_rand_hex() fills the 'rnd' buffer with a given 'num' size with random
179
 * hexadecimal digits PLUS a null-terminator byte. It must be an odd number
180
 * size.
181
 */
182
183
CURLcode Curl_rand_hex(struct Curl_easy *data, unsigned char *rnd, size_t num)
184
0
{
185
0
  CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
186
0
  unsigned char buffer[128];
187
0
  DEBUGASSERT(num > 1);
188
189
0
  if((num / 2 >= sizeof(buffer)) || !(num & 1)) {
190
    /* make sure it fits in the local buffer and that it is an odd number! */
191
0
    DEBUGF(infof(data, "invalid buffer size with Curl_rand_hex"));
192
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
193
0
  }
194
195
0
  num--; /* save one for null-termination */
196
197
0
  result = Curl_rand(data, buffer, num / 2);
198
0
  if(result)
199
0
    return result;
200
201
0
  Curl_hexencode(buffer, num / 2, rnd, num + 1);
202
0
  return result;
203
0
}
204
205
/*
206
 * Curl_rand_alnum() fills the 'rnd' buffer with a given 'num' size with random
207
 * alphanumerical chars PLUS a null-terminator byte.
208
 */
209
210
static const char alnum[] =
211
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
212
213
CURLcode Curl_rand_alnum(struct Curl_easy *data, unsigned char *rnd,
214
                         size_t num)
215
12.9k
{
216
12.9k
  CURLcode result = CURLE_OK;
217
12.9k
  const unsigned int alnumspace = CURL_CSTRLEN(alnum);
218
12.9k
  unsigned int r;
219
12.9k
  DEBUGASSERT(num > 1);
220
221
12.9k
  num--; /* save one for null-termination */
222
223
298k
  while(num) {
224
285k
    do {
225
285k
      result = randit(data, &r, TRUE);
226
285k
      if(result)
227
0
        return result;
228
285k
    } while(r >= (UINT_MAX - UINT_MAX % alnumspace));
229
230
285k
    *rnd++ = (unsigned char)alnum[r % alnumspace];
231
285k
    num--;
232
285k
  }
233
12.9k
  *rnd = 0;
234
235
12.9k
  return result;
236
12.9k
}