Coverage Report

Created: 2026-12-01 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/sha256.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
#if !defined(CURL_DISABLE_AWS) || !defined(CURL_DISABLE_HTTPSIG) || \
27
  !defined(CURL_DISABLE_DIGEST_AUTH) ||                             \
28
  defined(USE_LIBSSH2) || defined(USE_SSL)
29
30
#include "curl_sha256.h"
31
#ifdef CURL_HAVE_SHA256
32
33
#ifdef USE_OPENSSL_SHA256
34
#include <openssl/evp.h>
35
36
typedef EVP_MD_CTX *my_sha256_ctx;
37
38
static CURLcode my_sha256_init(void *pctx)
39
525k
{
40
525k
  EVP_MD_CTX ** const ctx = pctx;
41
42
525k
  *ctx = EVP_MD_CTX_new();
43
525k
  if(!*ctx)
44
0
    return CURLE_OUT_OF_MEMORY;
45
46
525k
  if(!EVP_DigestInit_ex(*ctx, EVP_sha256(), NULL)) {
47
0
    EVP_MD_CTX_free(*ctx);
48
0
    *ctx = NULL;
49
0
    return CURLE_FAILED_INIT;
50
0
  }
51
525k
  return CURLE_OK;
52
525k
}
53
54
static CURLcode my_sha256_update(void *pctx,
55
                                 const void *input, size_t input_len)
56
23.0M
{
57
23.0M
  EVP_MD_CTX ** const ctx = pctx;
58
23.0M
  return EVP_DigestUpdate(*ctx, input, input_len) ?
59
23.0M
    CURLE_OK : CURLE_BAD_FUNCTION_ARGUMENT;
60
23.0M
}
61
62
static CURLcode my_sha256_final(uint8_t *digest, void *pctx)
63
525k
{
64
525k
  EVP_MD_CTX ** const ctx = pctx;
65
525k
  CURLcode result = EVP_DigestFinal_ex(*ctx, digest, NULL) ?
66
525k
    CURLE_OK : CURLE_BAD_FUNCTION_ARGUMENT;
67
525k
  EVP_MD_CTX_free(*ctx);
68
525k
  *ctx = NULL;
69
525k
  return result;
70
525k
}
71
72
#elif defined(USE_WOLFSSL_SHA256)
73
#include <wolfssl/wolfcrypt/sha256.h>
74
75
typedef struct wc_Sha256 my_sha256_ctx;
76
77
static CURLcode my_sha256_init(void *ctx)
78
{
79
  if(wc_InitSha256(ctx))
80
    return CURLE_FAILED_INIT;
81
  return CURLE_OK;
82
}
83
84
static CURLcode my_sha256_update(void *ctx,
85
                                 const void *input, size_t input_len)
86
{
87
  const byte *bytes = input;
88
  do {
89
    word32 ilen = (word32)CURLMIN(input_len, UINT32_MAX);
90
    if(wc_Sha256Update(ctx, bytes, ilen))
91
      return CURLE_BAD_FUNCTION_ARGUMENT;
92
    input_len -= ilen;
93
    bytes += ilen;
94
  } while(input_len);
95
  return CURLE_OK;
96
}
97
98
static CURLcode my_sha256_final(uint8_t *digest, void *ctx)
99
{
100
  if(wc_Sha256Final(ctx, digest))
101
    return CURLE_BAD_FUNCTION_ARGUMENT;
102
  return CURLE_OK;
103
}
104
105
#elif defined(USE_GNUTLS_SHA256)
106
#include <nettle/sha2.h>
107
#include <nettle/version.h>
108
109
typedef struct sha256_ctx my_sha256_ctx;
110
111
static CURLcode my_sha256_init(void *ctx)
112
{
113
  sha256_init(ctx);
114
  return CURLE_OK;
115
}
116
117
static CURLcode my_sha256_update(void *ctx,
118
                                 const void *input, size_t input_len)
119
{
120
  sha256_update(ctx, input_len, input);
121
  return CURLE_OK;
122
}
123
124
static CURLcode my_sha256_final(uint8_t *digest, void *ctx)
125
{
126
#if NETTLE_VERSION_MAJOR >= 4
127
  sha256_digest(ctx, digest);
128
#else
129
  sha256_digest(ctx, SHA256_DIGEST_SIZE, digest);
130
#endif
131
  return CURLE_OK;
132
}
133
134
#elif defined(USE_MBEDTLS_SHA256)
135
#include <psa/crypto.h>
136
137
typedef psa_hash_operation_t my_sha256_ctx;
138
139
static CURLcode my_sha256_init(void *pctx)
140
{
141
  psa_hash_operation_t *ctx = pctx;
142
  *ctx = psa_hash_operation_init();
143
  if(psa_hash_setup(ctx, PSA_ALG_SHA_256) != PSA_SUCCESS)
144
    return CURLE_FAILED_INIT;
145
  return CURLE_OK;
146
}
147
148
static CURLcode my_sha256_update(void *ctx,
149
                                 const void *input, size_t input_len)
150
{
151
  if(psa_hash_update(ctx, input, input_len) != PSA_SUCCESS)
152
    return CURLE_BAD_FUNCTION_ARGUMENT;
153
  return CURLE_OK;
154
}
155
156
static CURLcode my_sha256_final(uint8_t *digest, void *ctx)
157
115k
{
158
115k
  size_t actual_length;
159
115k
  if(psa_hash_finish(ctx, digest, CURL_SHA256_DIGEST_LEN,
160
115k
                     &actual_length) != PSA_SUCCESS)
161
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
162
115k
  return CURLE_OK;
163
115k
}
164
165
#elif defined(USE_APPLE_SHA256)
166
#include <CommonCrypto/CommonDigest.h>
167
168
typedef CC_SHA256_CTX my_sha256_ctx;
169
170
static CURLcode my_sha256_init(void *ctx)
171
{
172
  (void)CC_SHA256_Init(ctx);  /* always returns 1 */
173
  return CURLE_OK;
174
}
175
176
static CURLcode my_sha256_update(void *ctx,
177
                                 const void *input, size_t input_len)
178
{
179
  const uint8_t *bytes = input;
180
  do {
181
    CC_LONG ilen = (CC_LONG)CURLMIN(input_len, UINT32_MAX);
182
    CC_SHA256_Update(ctx, bytes, ilen);  /* always returns 1 */
183
    input_len -= ilen;
184
    bytes += ilen;
185
  } while(input_len);
186
  return CURLE_OK;
187
}
188
189
static CURLcode my_sha256_final(uint8_t *digest, void *ctx)
190
{
191
  (void)CC_SHA256_Final(digest, ctx);  /* always returns 1 */
192
  return CURLE_OK;
193
}
194
195
#elif defined(USE_WIN32_SHA256)
196
#include <bcrypt.h>
197
198
struct sha256_ctx {
199
  BCRYPT_ALG_HANDLE hAlg;
200
  BCRYPT_HASH_HANDLE hHash;
201
};
202
typedef struct sha256_ctx my_sha256_ctx;
203
204
static CURLcode my_sha256_init(void *pctx)
205
{
206
  struct sha256_ctx *ctx = pctx;
207
208
  ctx->hAlg = ctx->hHash = NULL;
209
  if(!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&ctx->hAlg,
210
                                                 BCRYPT_SHA256_ALGORITHM,
211
                                                 NULL, 0)))
212
    return CURLE_FAILED_INIT;
213
  if(!BCRYPT_SUCCESS(BCryptCreateHash(ctx->hAlg, &ctx->hHash,
214
                                      NULL, 0, NULL, 0, 0))) {
215
    BCryptCloseAlgorithmProvider(ctx->hAlg, 0);
216
    ctx->hAlg = NULL;
217
    return CURLE_FAILED_INIT;
218
  }
219
220
  return CURLE_OK;
221
}
222
223
static CURLcode my_sha256_update(void *pctx,
224
                                 const void *input, size_t input_len)
225
{
226
  struct sha256_ctx *ctx = pctx;
227
  const BYTE *bytes = input;
228
  do {
229
    ULONG ilen = (ULONG)CURLMIN(input_len, ULONG_MAX);
230
    if(!BCRYPT_SUCCESS(BCryptHashData(ctx->hHash, CURL_UNCONST(bytes),
231
                                      ilen, 0)))
232
      return CURLE_BAD_FUNCTION_ARGUMENT;
233
    input_len -= ilen;
234
    bytes += ilen;
235
  } while(input_len);
236
  return CURLE_OK;
237
}
238
239
static CURLcode my_sha256_final(uint8_t *digest, void *pctx)
240
{
241
  struct sha256_ctx *ctx = pctx;
242
  CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
243
  if(BCRYPT_SUCCESS(BCryptFinishHash(ctx->hHash,
244
                                     digest, CURL_SHA256_DIGEST_LEN, 0)))
245
    result = CURLE_OK;
246
  BCryptDestroyHash(ctx->hHash);
247
  ctx->hHash = NULL;
248
  if(ctx->hAlg) {
249
    BCryptCloseAlgorithmProvider(ctx->hAlg, 0);
250
    ctx->hAlg = NULL;
251
  }
252
  return result;
253
}
254
255
#endif /* CRYPTO LIBS */
256
257
const struct HMAC_params Curl_HMAC_SHA256 = {
258
  my_sha256_init,        /* Hash initialization function. */
259
  my_sha256_update,      /* Hash update function. */
260
  my_sha256_final,       /* Hash computation end function. */
261
  sizeof(my_sha256_ctx), /* Size of hash context structure. */
262
  64,                    /* Maximum key length. */
263
  CURL_SHA256_DIGEST_LEN /* Result size. */
264
};
265
266
/*
267
 * Generates a SHA256 hash for the given input data.
268
 *
269
 * Parameters:
270
 *
271
 * digest [out]   - The output buffer.
272
 * input [in]     - The input data.
273
 * input_len [in] - The input length.
274
 *
275
 * Returns CURLE_OK on success.
276
 * @unittest: 1601
277
 */
278
CURLcode Curl_sha256it(uint8_t *digest, const void *input, size_t input_len)
279
279k
{
280
279k
  my_sha256_ctx ctx;
281
279k
  CURLcode result = my_sha256_init(&ctx);
282
279k
  if(result)
283
0
    return result;
284
285
279k
  result = my_sha256_update(&ctx, input, input_len);
286
279k
  if(result) {
287
0
    (void)my_sha256_final(digest, &ctx);
288
0
    return result;
289
0
  }
290
291
279k
  return my_sha256_final(digest, &ctx);
292
279k
}
293
294
#else
295
296
const struct HMAC_params Curl_HMAC_SHA256 = {
297
  NULL,                  /* Hash initialization function. */
298
  NULL,                  /* Hash update function. */
299
  NULL,                  /* Hash computation end function. */
300
  0,                     /* Size of hash context structure. */
301
  64,                    /* Maximum key length. */
302
  CURL_SHA256_DIGEST_LEN /* Result size. */
303
};
304
305
CURLcode Curl_sha256it(uint8_t *digest, const void *input, size_t input_len)
306
{
307
  (void)digest;
308
  (void)input;
309
  (void)input_len;
310
  return CURLE_NOT_BUILT_IN;
311
}
312
313
#endif /* CURL_HAVE_SHA256 */
314
315
#endif /* AWS, httpsig, DIGEST, or libssh2 */