Coverage Report

Created: 2026-12-01 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/hmac.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
 * RFC2104 Keyed-Hashing for Message Authentication
24
 *
25
 ***************************************************************************/
26
#include "curl_setup.h"
27
28
#if !defined(CURL_DISABLE_AWS) || !defined(CURL_DISABLE_HTTPSIG) || \
29
  !defined(CURL_DISABLE_DIGEST_AUTH) ||                             \
30
  defined(USE_LIBSSH2) || defined(USE_SSL)
31
32
#include "curl_hmac.h"
33
34
/*
35
 * Generic HMAC algorithm.
36
 *
37
 *   This module computes HMAC digests based on any hash function. Parameters
38
 * and computing procedures are setup dynamically at HMAC computation context
39
 * initialization.
40
 */
41
42
static const uint8_t hmac_ipad = 0x36;
43
static const uint8_t hmac_opad = 0x5C;
44
45
/* Some hash contexts need 16-byte alignment, e.g. wolfSSL on 64-bit CPUs */
46
19.8k
#define HMAC_CTXT_OFFSET ((sizeof(struct HMAC_context) + 15) & ~(size_t)15)
47
48
struct HMAC_context *Curl_HMAC_init(const struct HMAC_params *hashparams,
49
                                    const void *key, size_t key_len)
50
9.92k
{
51
9.92k
  size_t i;
52
9.92k
  struct HMAC_context *ctxt;
53
9.92k
  const uint8_t *bkey;
54
9.92k
  uint8_t *hkey;
55
9.92k
  uint8_t b;
56
9.92k
  CURLcode hok;
57
9.92k
  bool hashctxt1_initialized;
58
9.92k
  bool hashctxt2_initialized;
59
60
  /* Create HMAC context. */
61
9.92k
  i = HMAC_CTXT_OFFSET + (2 * hashparams->ctxtsize) + hashparams->digest_len;
62
9.92k
  ctxt = curlx_malloc(i);
63
9.92k
  if(!ctxt)
64
19
    return ctxt;
65
66
9.90k
  hashctxt1_initialized = FALSE;
67
9.90k
  hashctxt2_initialized = FALSE;
68
69
9.90k
  ctxt->hash = hashparams;
70
9.90k
  ctxt->hashctxt1 = (void *)((char *)ctxt + HMAC_CTXT_OFFSET);
71
9.90k
  ctxt->hashctxt2 = (void *)((char *)ctxt->hashctxt1 + hashparams->ctxtsize);
72
73
  /* If the key is too long, replace it by its hash digest. */
74
9.90k
  if(key_len > hashparams->key_len_max) {
75
33
    if(hashparams->hinit(ctxt->hashctxt1))
76
0
      goto fail;
77
33
    hok = hashparams->hupdate(ctxt->hashctxt1, key, key_len);
78
33
    hkey = (uint8_t *)ctxt->hashctxt2 + hashparams->ctxtsize;
79
33
    hok |= hashparams->hfinal(hkey, ctxt->hashctxt1);
80
33
    if(hok)
81
0
      goto fail;
82
33
    key = hkey;
83
33
    key_len = hashparams->digest_len;
84
33
  }
85
86
  /* Prime the two hash contexts with the modified key. */
87
9.90k
  if(hashparams->hinit(ctxt->hashctxt1))
88
0
    goto fail;
89
9.90k
  hashctxt1_initialized = TRUE;
90
9.90k
  if(hashparams->hinit(ctxt->hashctxt2))
91
0
    goto fail;
92
9.90k
  hashctxt2_initialized = TRUE;
93
94
9.90k
  bkey = key;
95
271k
  for(i = 0; i < key_len; i++) {
96
261k
    b = (uint8_t)(*bkey ^ hmac_ipad);
97
261k
    if(hashparams->hupdate(ctxt->hashctxt1, &b, 1))
98
0
      goto fail;
99
261k
    b = (uint8_t)(*bkey++ ^ hmac_opad);
100
261k
    if(hashparams->hupdate(ctxt->hashctxt2, &b, 1))
101
0
      goto fail;
102
261k
  }
103
104
382k
  for(; i < hashparams->key_len_max; i++) {
105
373k
    if(hashparams->hupdate(ctxt->hashctxt1, &hmac_ipad, 1))
106
0
      goto fail;
107
373k
    if(hashparams->hupdate(ctxt->hashctxt2, &hmac_opad, 1))
108
0
      goto fail;
109
373k
  }
110
111
  /* Done, return pointer to HMAC context. */
112
9.90k
  return ctxt;
113
114
0
fail:
115
0
  if(hashctxt1_initialized)
116
0
    hashparams->hfinal((uint8_t *)ctxt->hashctxt2 + hashparams->ctxtsize,
117
0
                       ctxt->hashctxt1);
118
0
  if(hashctxt2_initialized)
119
0
    hashparams->hfinal((uint8_t *)ctxt->hashctxt2 + hashparams->ctxtsize,
120
0
                       ctxt->hashctxt2);
121
0
  curlx_free(ctxt);
122
0
  return NULL;
123
9.90k
}
124
125
CURLcode Curl_HMAC_update(struct HMAC_context *ctxt,
126
                          const void *input, size_t input_len)
127
9.90k
{
128
  /* Update first hash calculation. */
129
9.90k
  return ctxt->hash->hupdate(ctxt->hashctxt1, input, input_len);
130
9.90k
}
131
132
CURLcode Curl_HMAC_final(struct HMAC_context *ctxt, uint8_t *mac)
133
9.90k
{
134
9.90k
  const struct HMAC_params *hashparams = ctxt->hash;
135
9.90k
  CURLcode hok;
136
137
  /* Do not get output if called with a NULL parameter: only release
138
     storage. */
139
9.90k
  if(!mac)
140
0
    mac = (uint8_t *)ctxt->hashctxt2 + ctxt->hash->ctxtsize;
141
142
9.90k
  hok = hashparams->hfinal(mac, ctxt->hashctxt1);
143
9.90k
  if(!hok)
144
9.90k
    hok |= hashparams->hupdate(ctxt->hashctxt2, mac, hashparams->digest_len);
145
9.90k
  hok |= hashparams->hfinal(mac, ctxt->hashctxt2);
146
147
9.90k
  curlx_free(ctxt);
148
9.90k
  return hok ? CURLE_BAD_FUNCTION_ARGUMENT : hok;
149
9.90k
}
150
151
/*
152
 * This is used to generate a HMAC hash, for the specified input data, given
153
 * the specified hash function and key.
154
 *
155
 * Parameters:
156
 *
157
 * hashparams [in]  - The hash function (Curl_HMAC_SHA256).
158
 * key        [in]  - The key to use.
159
 * key_len    [in]  - The length of the key.
160
 * input      [in]  - The data to encrypt.
161
 * input_len  [in]  - The length of the data.
162
 * mac        [out] - The output buffer.
163
 *
164
 * Returns CURLE_OK on success.
165
 */
166
CURLcode Curl_hmacit(const struct HMAC_params *hashparams,
167
                     const void *key, const size_t key_len,
168
                     const void *input, size_t input_len,
169
                     uint8_t *mac)
170
9.92k
{
171
9.92k
  struct HMAC_context *ctxt;
172
9.92k
  CURLcode result;
173
174
9.92k
  if(!hashparams->hinit)
175
0
    return CURLE_NOT_BUILT_IN;
176
177
9.92k
  ctxt = Curl_HMAC_init(hashparams, key, key_len);
178
9.92k
  if(!ctxt)
179
19
    return CURLE_OUT_OF_MEMORY;
180
181
  /* Update the digest with the given challenge */
182
9.90k
  result = Curl_HMAC_update(ctxt, input, input_len);
183
9.90k
  if(result) {
184
0
    (void)Curl_HMAC_final(ctxt, mac);
185
0
    return result;
186
0
  }
187
188
9.90k
  return Curl_HMAC_final(ctxt, mac);  /* Finalise the digest */
189
9.90k
}
190
191
#endif /* AWS, httpsig or Digest and TLS */