hmac.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  * RFC2104 Keyed-Hashing for Message Authentication
00022  *
00023  ***************************************************************************/
00024 
00025 #include "curl_setup.h"
00026 
00027 #ifndef CURL_DISABLE_CRYPTO_AUTH
00028 
00029 #include <curl/curl.h>
00030 
00031 #include "curl_hmac.h"
00032 #include "curl_memory.h"
00033 
00034 /* The last #include file should be: */
00035 #include "memdebug.h"
00036 
00037 /*
00038  * Generic HMAC algorithm.
00039  *
00040  *   This module computes HMAC digests based on any hash function. Parameters
00041  * and computing procedures are set-up dynamically at HMAC computation
00042  * context initialisation.
00043  */
00044 
00045 static const unsigned char hmac_ipad = 0x36;
00046 static const unsigned char hmac_opad = 0x5C;
00047 
00048 
00049 
00050 HMAC_context *
00051 Curl_HMAC_init(const HMAC_params * hashparams,
00052                const unsigned char *key,
00053                unsigned int keylen)
00054 {
00055   size_t i;
00056   HMAC_context *ctxt;
00057   unsigned char *hkey;
00058   unsigned char b;
00059 
00060   /* Create HMAC context. */
00061   i = sizeof *ctxt + 2 * hashparams->hmac_ctxtsize +
00062     hashparams->hmac_resultlen;
00063   ctxt = malloc(i);
00064 
00065   if(!ctxt)
00066     return ctxt;
00067 
00068   ctxt->hmac_hash = hashparams;
00069   ctxt->hmac_hashctxt1 = (void *) (ctxt + 1);
00070   ctxt->hmac_hashctxt2 = (void *) ((char *) ctxt->hmac_hashctxt1 +
00071       hashparams->hmac_ctxtsize);
00072 
00073   /* If the key is too long, replace it by its hash digest. */
00074   if(keylen > hashparams->hmac_maxkeylen) {
00075     (*hashparams->hmac_hinit)(ctxt->hmac_hashctxt1);
00076     (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt1, key, keylen);
00077     hkey = (unsigned char *) ctxt->hmac_hashctxt2 + hashparams->hmac_ctxtsize;
00078     (*hashparams->hmac_hfinal)(hkey, ctxt->hmac_hashctxt1);
00079     key = hkey;
00080     keylen = hashparams->hmac_resultlen;
00081   }
00082 
00083   /* Prime the two hash contexts with the modified key. */
00084   (*hashparams->hmac_hinit)(ctxt->hmac_hashctxt1);
00085   (*hashparams->hmac_hinit)(ctxt->hmac_hashctxt2);
00086 
00087   for(i = 0; i < keylen; i++) {
00088     b = (unsigned char)(*key ^ hmac_ipad);
00089     (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt1, &b, 1);
00090     b = (unsigned char)(*key++ ^ hmac_opad);
00091     (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt2, &b, 1);
00092   }
00093 
00094   for(; i < hashparams->hmac_maxkeylen; i++) {
00095     (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt1, &hmac_ipad, 1);
00096     (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt2, &hmac_opad, 1);
00097   }
00098 
00099   /* Done, return pointer to HMAC context. */
00100   return ctxt;
00101 }
00102 
00103 int Curl_HMAC_update(HMAC_context * ctxt,
00104                      const unsigned char *data,
00105                      unsigned int len)
00106 {
00107   /* Update first hash calculation. */
00108   (*ctxt->hmac_hash->hmac_hupdate)(ctxt->hmac_hashctxt1, data, len);
00109   return 0;
00110 }
00111 
00112 
00113 int Curl_HMAC_final(HMAC_context *ctxt, unsigned char *result)
00114 {
00115   const HMAC_params * hashparams = ctxt->hmac_hash;
00116 
00117   /* Do not get result if called with a null parameter: only release
00118      storage. */
00119 
00120   if(!result)
00121     result = (unsigned char *) ctxt->hmac_hashctxt2 +
00122      ctxt->hmac_hash->hmac_ctxtsize;
00123 
00124   (*hashparams->hmac_hfinal)(result, ctxt->hmac_hashctxt1);
00125   (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt2,
00126    result, hashparams->hmac_resultlen);
00127   (*hashparams->hmac_hfinal)(result, ctxt->hmac_hashctxt2);
00128   free((char *) ctxt);
00129   return 0;
00130 }
00131 
00132 #endif /* CURL_DISABLE_CRYPTO_AUTH */


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:04