Coverage Report

Created: 2026-01-25 06:10

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