Coverage Report

Created: 2026-12-01 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/curl_sha512_256.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Evgeny Grin (Karlson2k), <k2k@narod.ru>.
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_DIGEST_AUTH) && !defined(CURL_DISABLE_SHA512_256)
27
28
#include "curl_sha512_256.h"
29
30
#ifdef CURL_HAVE_SHA512_256
31
32
#ifdef USE_OPENSSL_SHA512_256
33
34
/* Context type used for SHA-512/256 calculations */
35
typedef EVP_MD_CTX *Curl_sha512_256_ctx;
36
37
/* Initialize structure for SHA-512/256 calculation.
38
 *
39
 * @param pctx the calculation context
40
 * @return CURLE_OK if succeed, error code otherwise
41
 */
42
static CURLcode my_sha512_256_init(void *pctx)
43
0
{
44
0
  EVP_MD_CTX ** const ctx = pctx;
45
46
0
  *ctx = EVP_MD_CTX_new();
47
0
  if(!*ctx)
48
0
    return CURLE_OUT_OF_MEMORY;
49
50
0
  if(!EVP_DigestInit_ex(*ctx, EVP_sha512_256(), NULL)) {
51
0
    EVP_MD_CTX_free(*ctx);
52
0
    *ctx = NULL;
53
0
    return CURLE_FAILED_INIT;
54
0
  }
55
  /* Check whether the header and this file use the same numbers */
56
0
  DEBUGASSERT(EVP_MD_CTX_size(*ctx) == CURL_SHA512_256_DIGEST_LEN);
57
  /* Check whether the block size is correct */
58
0
  DEBUGASSERT(EVP_MD_CTX_block_size(*ctx) == 128);
59
0
  return CURLE_OK;
60
0
}
61
62
/* Process portion of bytes.
63
 *
64
 * @param pctx the calculation context
65
 * @param[in] input bytes to add to hash
66
 * @param[in] input_len number of bytes in @a input
67
 * @return CURLE_OK if succeed, error code otherwise
68
 */
69
static CURLcode my_sha512_256_update(void *pctx,
70
                                     const void *input, size_t input_len)
71
0
{
72
0
  EVP_MD_CTX ** const ctx = pctx;
73
0
  return EVP_DigestUpdate(*ctx, input, input_len) ?
74
0
    CURLE_OK : CURLE_BAD_FUNCTION_ARGUMENT;
75
0
}
76
77
/* Finalise SHA-512/256 calculation, return digest.
78
 *
79
 * @param[out] digest returned, must be #CURL_SHA512_256_DIGEST_LEN bytes
80
 * @param pctx the calculation context
81
 * @return CURLE_OK if succeed, error code otherwise
82
 */
83
static CURLcode my_sha512_256_final(uint8_t *digest, void *pctx)
84
0
{
85
0
  EVP_MD_CTX ** const ctx = pctx;
86
0
  CURLcode result;
87
88
/* Some NetBSD versions have a bug in SHA-512/256.
89
 * See https://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=58039
90
 * The problematic versions:
91
 * - NetBSD before 9.4
92
 * - NetBSD 9 all development versions (9.99.x)
93
 * - NetBSD 10 development versions (10.99.x) before 10.99.11
94
 * The bug was fixed in NetBSD 9.4 release, NetBSD 10.0 release,
95
 * NetBSD 10.99.11 development.
96
 * It is safe to apply the workaround even if the bug is not present, as
97
 * the workaround reduces performance slightly. */
98
#if defined(__NetBSD__) && \
99
  (__NetBSD_Version__ <   904000000 ||  \
100
   (__NetBSD_Version__ >=  999000000 &&  \
101
    __NetBSD_Version__ <  1000000000) || \
102
   (__NetBSD_Version__ >= 1099000000 &&  \
103
    __NetBSD_Version__ <  1099001100))
104
  /* Use a larger buffer to work around a bug in NetBSD:
105
     https://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=58039 */
106
  uint8_t tmp_digest[CURL_SHA512_256_DIGEST_LEN * 2];
107
  result = EVP_DigestFinal_ex(*ctx, tmp_digest, NULL) ?
108
    CURLE_OK : CURLE_BAD_FUNCTION_ARGUMENT;
109
  if(result == CURLE_OK)
110
    memcpy(digest, tmp_digest, CURL_SHA512_256_DIGEST_LEN);
111
  curlx_memzero(tmp_digest, sizeof(tmp_digest));
112
#else
113
0
  result = EVP_DigestFinal_ex(*ctx, digest, NULL) ?
114
0
    CURLE_OK : CURLE_BAD_FUNCTION_ARGUMENT;
115
0
#endif
116
117
0
  EVP_MD_CTX_free(*ctx);
118
0
  *ctx = NULL;
119
120
0
  return result;
121
0
}
122
123
#elif defined(USE_WOLFSSL_SHA512_256)
124
#include <wolfssl/wolfcrypt/sha512.h>
125
126
typedef struct wc_Sha512 Curl_sha512_256_ctx;
127
128
static CURLcode my_sha512_256_init(void *ctx)
129
{
130
  if(wc_InitSha512_256(ctx))
131
    return CURLE_FAILED_INIT;
132
  return CURLE_OK;
133
}
134
135
static CURLcode my_sha512_256_update(void *ctx,
136
                                     const void *input, size_t input_len)
137
{
138
  const byte *bytes = input;
139
  do {
140
    word32 ilen = (word32)CURLMIN(input_len, UINT_MAX);
141
    if(wc_Sha512_256Update(ctx, bytes, ilen))
142
      return CURLE_BAD_FUNCTION_ARGUMENT;
143
    input_len -= ilen;
144
    bytes += ilen;
145
  } while(input_len);
146
  return CURLE_OK;
147
}
148
149
static CURLcode my_sha512_256_final(uint8_t *digest, void *ctx)
150
{
151
  if(wc_Sha512_256Final(ctx, digest))
152
    return CURLE_BAD_FUNCTION_ARGUMENT;
153
  return CURLE_OK;
154
}
155
156
#elif defined(USE_GNUTLS_SHA512_256)
157
158
typedef struct sha512_256_ctx Curl_sha512_256_ctx;
159
160
static CURLcode my_sha512_256_init(void *ctx)
161
{
162
  sha512_256_init(ctx);
163
  return CURLE_OK;
164
}
165
166
static CURLcode my_sha512_256_update(void *ctx,
167
                                     const void *input, size_t input_len)
168
{
169
  DEBUGASSERT(input || (input_len == 0));
170
  sha512_256_update(ctx, input_len, input);
171
  return CURLE_OK;
172
}
173
174
static CURLcode my_sha512_256_final(uint8_t *digest, void *ctx)
175
{
176
#if NETTLE_VERSION_MAJOR >= 4
177
  sha512_256_digest(ctx, digest);
178
#else
179
  sha512_256_digest(ctx, CURL_SHA512_256_DIGEST_LEN, digest);
180
#endif
181
  return CURLE_OK;
182
}
183
184
#endif
185
186
/* Compute SHA-512/256 hash for the given data in one function call
187
 * @param[out] digest the pointer to put the hash
188
 * @param[in] input the pointer to the data to process
189
 * @param[in] input_len the size of the data pointed by @a input
190
 * @return always #CURLE_OK
191
 */
192
CURLcode Curl_sha512_256it(uint8_t *digest,
193
                           const void *input, size_t input_len)
194
0
{
195
0
  Curl_sha512_256_ctx ctx;
196
0
  CURLcode result = my_sha512_256_init(&ctx);
197
0
  if(result)
198
0
    return result;
199
200
0
  result = my_sha512_256_update(&ctx, input, input_len);
201
0
  if(result) {
202
0
    (void)my_sha512_256_final(digest, &ctx);
203
0
    return result;
204
0
  }
205
206
0
  return my_sha512_256_final(digest, &ctx);
207
0
}
208
209
#endif /* CURL_HAVE_SHA512_256 */
210
211
#endif /* !CURL_DISABLE_DIGEST_AUTH && !CURL_DISABLE_SHA512_256 */