Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00035 #include "memdebug.h"
00036
00037
00038
00039
00040
00041
00042
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
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
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
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
00100 return ctxt;
00101 }
00102
00103 int Curl_HMAC_update(HMAC_context * ctxt,
00104 const unsigned char *data,
00105 unsigned int len)
00106 {
00107
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
00118
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