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 | 136 | #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 | 68 | { |
51 | 68 | size_t i; |
52 | 68 | struct HMAC_context *ctxt; |
53 | 68 | const uint8_t *bkey; |
54 | 68 | uint8_t *hkey; |
55 | 68 | uint8_t b; |
56 | 68 | CURLcode hok; |
57 | 68 | bool hashctxt1_initialized; |
58 | 68 | bool hashctxt2_initialized; |
59 | | |
60 | | /* Create HMAC context. */ |
61 | 68 | i = HMAC_CTXT_OFFSET + (2 * hashparams->ctxtsize) + hashparams->digest_len; |
62 | 68 | ctxt = curlx_malloc(i); |
63 | 68 | if(!ctxt) |
64 | 0 | return ctxt; |
65 | | |
66 | 68 | hashctxt1_initialized = FALSE; |
67 | 68 | hashctxt2_initialized = FALSE; |
68 | | |
69 | 68 | ctxt->hash = hashparams; |
70 | 68 | ctxt->hashctxt1 = (void *)((char *)ctxt + HMAC_CTXT_OFFSET); |
71 | 68 | ctxt->hashctxt2 = (void *)((char *)ctxt->hashctxt1 + hashparams->ctxtsize); |
72 | | |
73 | | /* If the key is too long, replace it by its hash digest. */ |
74 | 68 | if(key_len > hashparams->key_len_max) { |
75 | 11 | if(hashparams->hinit(ctxt->hashctxt1)) |
76 | 0 | goto fail; |
77 | 11 | hok = hashparams->hupdate(ctxt->hashctxt1, key, key_len); |
78 | 11 | hkey = (uint8_t *)ctxt->hashctxt2 + hashparams->ctxtsize; |
79 | 11 | hok |= hashparams->hfinal(hkey, ctxt->hashctxt1); |
80 | 11 | if(hok) |
81 | 0 | goto fail; |
82 | 11 | key = hkey; |
83 | 11 | key_len = hashparams->digest_len; |
84 | 11 | } |
85 | | |
86 | | /* Prime the two hash contexts with the modified key. */ |
87 | 68 | if(hashparams->hinit(ctxt->hashctxt1)) |
88 | 0 | goto fail; |
89 | 68 | hashctxt1_initialized = TRUE; |
90 | 68 | if(hashparams->hinit(ctxt->hashctxt2)) |
91 | 0 | goto fail; |
92 | 68 | hashctxt2_initialized = TRUE; |
93 | | |
94 | 68 | bkey = key; |
95 | 709 | for(i = 0; i < key_len; i++) { |
96 | 641 | b = (uint8_t)(*bkey ^ hmac_ipad); |
97 | 641 | if(hashparams->hupdate(ctxt->hashctxt1, &b, 1)) |
98 | 0 | goto fail; |
99 | 641 | b = (uint8_t)(*bkey++ ^ hmac_opad); |
100 | 641 | if(hashparams->hupdate(ctxt->hashctxt2, &b, 1)) |
101 | 0 | goto fail; |
102 | 641 | } |
103 | | |
104 | 3.77k | for(; i < hashparams->key_len_max; i++) { |
105 | 3.71k | if(hashparams->hupdate(ctxt->hashctxt1, &hmac_ipad, 1)) |
106 | 0 | goto fail; |
107 | 3.71k | if(hashparams->hupdate(ctxt->hashctxt2, &hmac_opad, 1)) |
108 | 0 | goto fail; |
109 | 3.71k | } |
110 | | |
111 | | /* Done, return pointer to HMAC context. */ |
112 | 68 | 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 | 68 | } |
124 | | |
125 | | CURLcode Curl_HMAC_update(struct HMAC_context *ctxt, |
126 | | const void *input, size_t input_len) |
127 | 13 | { |
128 | | /* Update first hash calculation. */ |
129 | 13 | return ctxt->hash->hupdate(ctxt->hashctxt1, input, input_len); |
130 | 13 | } |
131 | | |
132 | | CURLcode Curl_HMAC_final(struct HMAC_context *ctxt, uint8_t *mac) |
133 | 68 | { |
134 | 68 | const struct HMAC_params *hashparams = ctxt->hash; |
135 | 68 | CURLcode hok; |
136 | | |
137 | | /* Do not get output if called with a NULL parameter: only release |
138 | | storage. */ |
139 | 68 | if(!mac) |
140 | 0 | mac = (uint8_t *)ctxt->hashctxt2 + ctxt->hash->ctxtsize; |
141 | | |
142 | 68 | hok = hashparams->hfinal(mac, ctxt->hashctxt1); |
143 | 68 | if(!hok) |
144 | 68 | hok |= hashparams->hupdate(ctxt->hashctxt2, mac, hashparams->digest_len); |
145 | 68 | hok |= hashparams->hfinal(mac, ctxt->hashctxt2); |
146 | | |
147 | 68 | curlx_free(ctxt); |
148 | 68 | return hok ? CURLE_BAD_FUNCTION_ARGUMENT : hok; |
149 | 68 | } |
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 | 0 | { |
171 | 0 | struct HMAC_context *ctxt; |
172 | 0 | CURLcode result; |
173 | |
|
174 | 0 | if(!hashparams->hinit) |
175 | 0 | return CURLE_NOT_BUILT_IN; |
176 | | |
177 | 0 | ctxt = Curl_HMAC_init(hashparams, key, key_len); |
178 | 0 | if(!ctxt) |
179 | 0 | return CURLE_OUT_OF_MEMORY; |
180 | | |
181 | | /* Update the digest with the given challenge */ |
182 | 0 | result = Curl_HMAC_update(ctxt, input, input_len); |
183 | 0 | if(result) { |
184 | 0 | (void)Curl_HMAC_final(ctxt, mac); |
185 | 0 | return result; |
186 | 0 | } |
187 | | |
188 | 0 | return Curl_HMAC_final(ctxt, mac); /* Finalise the digest */ |
189 | 0 | } |
190 | | |
191 | | #endif /* AWS, httpsig or Digest and TLS */ |