1d3ea8342fc10b6fdd51f7a262261fa5e16c3f2e
[platform/upstream/curl.git] / lib / hmac.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2010, 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 http://curl.haxx.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  * RFC2104 Keyed-Hashing for Message Authentication
22  *
23  ***************************************************************************/
24
25 #ifndef CURL_DISABLE_CRYPTO_AUTH
26
27 #include "setup.h"
28 #include "curl_hmac.h"
29
30 /*
31  * Generic HMAC algorithm.
32  *
33  *   This module computes HMAC digests based on any hash function. Parameters
34  * and computing procedures are set-up dynamically at HMAC computation
35  * context initialisation.
36  */
37
38 static const unsigned char hmac_ipad = 0x36;
39 static const unsigned char hmac_opad = 0x5C;
40
41
42
43 HMAC_context *
44 Curl_HMAC_init(const HMAC_params * hashparams,
45                const unsigned char * key,
46                unsigned int keylen)
47 {
48   unsigned int i;
49   HMAC_context * ctxt;
50   unsigned char * hkey;
51   unsigned char b;
52
53   /* Create HMAC context. */
54   i = sizeof *ctxt + 2 * hashparams->hmac_ctxtsize + hashparams->hmac_resultlen;
55   ctxt = (HMAC_context *) malloc(i);
56
57   if(!ctxt)
58     return ctxt;
59
60   ctxt->hmac_hash = hashparams;
61   ctxt->hmac_hashctxt1 = (void *) (ctxt + 1);
62   ctxt->hmac_hashctxt2 = (void *) ((char *) ctxt->hmac_hashctxt1 +
63       hashparams->hmac_ctxtsize);
64
65   /* If the key is too long, replace it by its hash digest. */
66   if(keylen > hashparams->hmac_maxkeylen) {
67     (*hashparams->hmac_hinit)(ctxt->hmac_hashctxt1);
68     (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt1, key, keylen);
69     hkey = (unsigned char *) ctxt->hmac_hashctxt2 + hashparams->hmac_ctxtsize;
70     (*hashparams->hmac_hfinal)(hkey, ctxt->hmac_hashctxt1);
71     key = hkey;
72     keylen = hashparams->hmac_resultlen;
73   }
74
75   /* Prime the two hash contexts with the modified key. */
76   (*hashparams->hmac_hinit)(ctxt->hmac_hashctxt1);
77   (*hashparams->hmac_hinit)(ctxt->hmac_hashctxt2);
78
79   for (i = 0; i < keylen; i++) {
80     b = *key ^ hmac_ipad;
81     (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt1, &b, 1);
82     b = *key++ ^ hmac_opad;
83     (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt2, &b, 1);
84   }
85
86   for (; i < hashparams->hmac_maxkeylen; i++) {
87     (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt1, &hmac_ipad, 1);
88     (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt2, &hmac_opad, 1);
89   }
90
91   /* Done, return pointer to HMAC context. */
92   return ctxt;
93 }
94
95 int Curl_HMAC_update(HMAC_context * ctxt,
96                      const unsigned char * data,
97                      unsigned int len)
98 {
99   /* Update first hash calculation. */
100   (*ctxt->hmac_hash->hmac_hupdate)(ctxt->hmac_hashctxt1, data, len);
101   return 0;
102 }
103
104
105 int Curl_HMAC_final(HMAC_context * ctxt, unsigned char * result)
106 {
107   const HMAC_params * hashparams = ctxt->hmac_hash;
108
109   /* Do not get result if called with a null parameter: only release storage. */
110
111   if(!result)
112     result = (unsigned char *) ctxt->hmac_hashctxt2 +
113      ctxt->hmac_hash->hmac_ctxtsize;
114
115   (*hashparams->hmac_hfinal)(result, ctxt->hmac_hashctxt1);
116   (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt2,
117    result, hashparams->hmac_resultlen);
118   (*hashparams->hmac_hfinal)(result, ctxt->hmac_hashctxt2);
119   free((char *) ctxt);
120   return 0;
121 }
122
123 #endif