1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * RFC2104 Keyed-Hashing for Message Authentication
23 ***************************************************************************/
27 #ifndef CURL_DISABLE_CRYPTO_AUTH
29 #include "curl_hmac.h"
31 #define _MPRINTF_REPLACE /* use our functions only */
32 #include <curl/mprintf.h>
34 #include "curl_memory.h"
35 /* The last #include file should be: */
39 * Generic HMAC algorithm.
41 * This module computes HMAC digests based on any hash function. Parameters
42 * and computing procedures are set-up dynamically at HMAC computation
43 * context initialisation.
46 static const unsigned char hmac_ipad = 0x36;
47 static const unsigned char hmac_opad = 0x5C;
52 Curl_HMAC_init(const HMAC_params * hashparams,
53 const unsigned char * key,
61 /* Create HMAC context. */
62 i = sizeof *ctxt + 2 * hashparams->hmac_ctxtsize + hashparams->hmac_resultlen;
68 ctxt->hmac_hash = hashparams;
69 ctxt->hmac_hashctxt1 = (void *) (ctxt + 1);
70 ctxt->hmac_hashctxt2 = (void *) ((char *) ctxt->hmac_hashctxt1 +
71 hashparams->hmac_ctxtsize);
73 /* If the key is too long, replace it by its hash digest. */
74 if(keylen > hashparams->hmac_maxkeylen) {
75 (*hashparams->hmac_hinit)(ctxt->hmac_hashctxt1);
76 (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt1, key, keylen);
77 hkey = (unsigned char *) ctxt->hmac_hashctxt2 + hashparams->hmac_ctxtsize;
78 (*hashparams->hmac_hfinal)(hkey, ctxt->hmac_hashctxt1);
80 keylen = hashparams->hmac_resultlen;
83 /* Prime the two hash contexts with the modified key. */
84 (*hashparams->hmac_hinit)(ctxt->hmac_hashctxt1);
85 (*hashparams->hmac_hinit)(ctxt->hmac_hashctxt2);
87 for (i = 0; i < keylen; i++) {
88 b = (unsigned char)(*key ^ hmac_ipad);
89 (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt1, &b, 1);
90 b = (unsigned char)(*key++ ^ hmac_opad);
91 (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt2, &b, 1);
94 for (; i < hashparams->hmac_maxkeylen; i++) {
95 (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt1, &hmac_ipad, 1);
96 (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt2, &hmac_opad, 1);
99 /* Done, return pointer to HMAC context. */
103 int Curl_HMAC_update(HMAC_context * ctxt,
104 const unsigned char * data,
107 /* Update first hash calculation. */
108 (*ctxt->hmac_hash->hmac_hupdate)(ctxt->hmac_hashctxt1, data, len);
113 int Curl_HMAC_final(HMAC_context * ctxt, unsigned char * result)
115 const HMAC_params * hashparams = ctxt->hmac_hash;
117 /* Do not get result if called with a null parameter: only release storage. */
120 result = (unsigned char *) ctxt->hmac_hashctxt2 +
121 ctxt->hmac_hash->hmac_ctxtsize;
123 (*hashparams->hmac_hfinal)(result, ctxt->hmac_hashctxt1);
124 (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt2,
125 result, hashparams->hmac_resultlen);
126 (*hashparams->hmac_hfinal)(result, ctxt->hmac_hashctxt2);
131 #endif /* CURL_DISABLE_CRYPTO_AUTH */