Coverage Report

Created: 2026-06-15 07:03

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