Coverage Report

Created: 2026-06-01 07:00

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(USE_CURL_NTLM_CORE) && !defined(USE_WINDOWS_SSPI)) ||    \
29
  !defined(CURL_DISABLE_AWS) || !defined(CURL_DISABLE_DIGEST_AUTH) || \
30
  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 unsigned char hmac_ipad = 0x36;
43
static const unsigned char hmac_opad = 0x5C;
44
45
struct HMAC_context *Curl_HMAC_init(const struct HMAC_params *hashparams,
46
                                    const unsigned char *key,
47
                                    unsigned int keylen)
48
64.3k
{
49
64.3k
  size_t i;
50
64.3k
  struct HMAC_context *ctxt;
51
64.3k
  unsigned char *hkey;
52
64.3k
  unsigned char b;
53
54
  /* Create HMAC context. */
55
64.3k
  i = sizeof(*ctxt) + (2 * hashparams->ctxtsize) + hashparams->resultlen;
56
64.3k
  ctxt = curlx_malloc(i);
57
58
64.3k
  if(!ctxt)
59
0
    return ctxt;
60
61
64.3k
  ctxt->hash = hashparams;
62
64.3k
  ctxt->hashctxt1 = (void *)(ctxt + 1);
63
64.3k
  ctxt->hashctxt2 = (void *)((char *)ctxt->hashctxt1 + hashparams->ctxtsize);
64
65
  /* If the key is too long, replace it by its hash digest. */
66
64.3k
  if(keylen > hashparams->maxkeylen) {
67
287
    if(hashparams->hinit(ctxt->hashctxt1))
68
0
      goto fail;
69
287
    hashparams->hupdate(ctxt->hashctxt1, key, keylen);
70
287
    hkey = (unsigned char *)ctxt->hashctxt2 + hashparams->ctxtsize;
71
287
    hashparams->hfinal(hkey, ctxt->hashctxt1);
72
287
    key = hkey;
73
287
    keylen = hashparams->resultlen;
74
287
  }
75
76
  /* Prime the two hash contexts with the modified key. */
77
64.3k
  if(hashparams->hinit(ctxt->hashctxt1) ||
78
64.3k
     hashparams->hinit(ctxt->hashctxt2))
79
0
    goto fail;
80
81
1.79M
  for(i = 0; i < keylen; i++) {
82
1.73M
    b = (unsigned char)(*key ^ hmac_ipad);
83
1.73M
    hashparams->hupdate(ctxt->hashctxt1, &b, 1);
84
1.73M
    b = (unsigned char)(*key++ ^ hmac_opad);
85
1.73M
    hashparams->hupdate(ctxt->hashctxt2, &b, 1);
86
1.73M
  }
87
88
2.44M
  for(; i < hashparams->maxkeylen; i++) {
89
2.37M
    hashparams->hupdate(ctxt->hashctxt1, &hmac_ipad, 1);
90
2.37M
    hashparams->hupdate(ctxt->hashctxt2, &hmac_opad, 1);
91
2.37M
  }
92
93
  /* Done, return pointer to HMAC context. */
94
64.3k
  return ctxt;
95
96
0
fail:
97
0
  curlx_free(ctxt);
98
0
  return NULL;
99
64.3k
}
100
101
void Curl_HMAC_update(struct HMAC_context *ctxt,
102
                      const unsigned char *data,
103
                      unsigned int len)
104
64.2k
{
105
  /* Update first hash calculation. */
106
64.2k
  ctxt->hash->hupdate(ctxt->hashctxt1, data, len);
107
64.2k
}
108
109
int Curl_HMAC_final(struct HMAC_context *ctxt, unsigned char *output)
110
64.3k
{
111
64.3k
  const struct HMAC_params *hashparams = ctxt->hash;
112
113
  /* Do not get output if called with a null parameter: only release
114
     storage. */
115
116
64.3k
  if(!output)
117
0
    output = (unsigned char *)ctxt->hashctxt2 + ctxt->hash->ctxtsize;
118
119
64.3k
  hashparams->hfinal(output, ctxt->hashctxt1);
120
64.3k
  hashparams->hupdate(ctxt->hashctxt2, output, hashparams->resultlen);
121
64.3k
  hashparams->hfinal(output, ctxt->hashctxt2);
122
64.3k
  curlx_free(ctxt);
123
64.3k
  return 0;
124
64.3k
}
125
126
/*
127
 * Curl_hmacit()
128
 *
129
 * This is used to generate a HMAC hash, for the specified input data, given
130
 * the specified hash function and key.
131
 *
132
 * Parameters:
133
 *
134
 * hashparams [in]     - The hash function (Curl_HMAC_MD5).
135
 * key        [in]     - The key to use.
136
 * keylen     [in]     - The length of the key.
137
 * buf        [in]     - The data to encrypt.
138
 * buflen     [in]     - The length of the data.
139
 * output     [in/out] - The output buffer.
140
 *
141
 * Returns CURLE_OK on success.
142
 */
143
CURLcode Curl_hmacit(const struct HMAC_params *hashparams,
144
                     const unsigned char *key, const size_t keylen,
145
                     const unsigned char *data, size_t datalen,
146
                     unsigned char *output)
147
64.2k
{
148
64.2k
  struct HMAC_context *ctxt;
149
64.2k
  if(keylen > UINT_MAX) /* unlikely to ever happen */
150
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
151
64.2k
  ctxt = Curl_HMAC_init(hashparams, key, curlx_uztoui(keylen));
152
153
64.2k
  if(!ctxt)
154
0
    return CURLE_OUT_OF_MEMORY;
155
156
  /* Update the digest with the given challenge */
157
64.2k
  do {
158
64.2k
    unsigned int ilen = (unsigned int)CURLMIN(datalen, UINT_MAX);
159
64.2k
    Curl_HMAC_update(ctxt, data, ilen);
160
64.2k
    datalen -= ilen;
161
64.2k
    data += ilen;
162
64.2k
  } while(datalen);
163
164
  /* Finalise the digest */
165
64.2k
  Curl_HMAC_final(ctxt, output);
166
167
64.2k
  return CURLE_OK;
168
64.2k
}
169
170
#endif /* Using NTLM (without SSPI) or AWS */