hmac.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #if (defined(USE_CURL_NTLM_CORE) && !defined(USE_WINDOWS_SSPI)) \
  28. || !defined(CURL_DISABLE_AWS) || !defined(CURL_DISABLE_DIGEST_AUTH)
  29. #include <curl/curl.h>
  30. #include "curl_hmac.h"
  31. #include "curl_memory.h"
  32. #include "warnless.h"
  33. /* The last #include file should be: */
  34. #include "memdebug.h"
  35. /*
  36. * Generic HMAC algorithm.
  37. *
  38. * This module computes HMAC digests based on any hash function. Parameters
  39. * and computing procedures are setup dynamically at HMAC computation context
  40. * initialization.
  41. */
  42. static const unsigned char hmac_ipad = 0x36;
  43. static const unsigned char hmac_opad = 0x5C;
  44. struct HMAC_context *
  45. Curl_HMAC_init(const struct HMAC_params *hashparams,
  46. const unsigned char *key,
  47. unsigned int keylen)
  48. {
  49. size_t i;
  50. struct HMAC_context *ctxt;
  51. unsigned char *hkey;
  52. unsigned char b;
  53. /* Create HMAC context. */
  54. i = sizeof(*ctxt) + 2 * hashparams->hmac_ctxtsize +
  55. hashparams->hmac_resultlen;
  56. ctxt = malloc(i);
  57. if(!ctxt)
  58. return ctxt;
  59. ctxt->hmac_hash = hashparams;
  60. ctxt->hmac_hashctxt1 = (void *) (ctxt + 1);
  61. ctxt->hmac_hashctxt2 = (void *) ((char *) ctxt->hmac_hashctxt1 +
  62. hashparams->hmac_ctxtsize);
  63. /* If the key is too long, replace it by its hash digest. */
  64. if(keylen > hashparams->hmac_maxkeylen) {
  65. (*hashparams->hmac_hinit)(ctxt->hmac_hashctxt1);
  66. (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt1, key, keylen);
  67. hkey = (unsigned char *) ctxt->hmac_hashctxt2 + hashparams->hmac_ctxtsize;
  68. (*hashparams->hmac_hfinal)(hkey, ctxt->hmac_hashctxt1);
  69. key = hkey;
  70. keylen = hashparams->hmac_resultlen;
  71. }
  72. /* Prime the two hash contexts with the modified key. */
  73. (*hashparams->hmac_hinit)(ctxt->hmac_hashctxt1);
  74. (*hashparams->hmac_hinit)(ctxt->hmac_hashctxt2);
  75. for(i = 0; i < keylen; i++) {
  76. b = (unsigned char)(*key ^ hmac_ipad);
  77. (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt1, &b, 1);
  78. b = (unsigned char)(*key++ ^ hmac_opad);
  79. (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt2, &b, 1);
  80. }
  81. for(; i < hashparams->hmac_maxkeylen; i++) {
  82. (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt1, &hmac_ipad, 1);
  83. (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt2, &hmac_opad, 1);
  84. }
  85. /* Done, return pointer to HMAC context. */
  86. return ctxt;
  87. }
  88. int Curl_HMAC_update(struct HMAC_context *ctxt,
  89. const unsigned char *data,
  90. unsigned int len)
  91. {
  92. /* Update first hash calculation. */
  93. (*ctxt->hmac_hash->hmac_hupdate)(ctxt->hmac_hashctxt1, data, len);
  94. return 0;
  95. }
  96. int Curl_HMAC_final(struct HMAC_context *ctxt, unsigned char *result)
  97. {
  98. const struct HMAC_params *hashparams = ctxt->hmac_hash;
  99. /* Do not get result if called with a null parameter: only release
  100. storage. */
  101. if(!result)
  102. result = (unsigned char *) ctxt->hmac_hashctxt2 +
  103. ctxt->hmac_hash->hmac_ctxtsize;
  104. (*hashparams->hmac_hfinal)(result, ctxt->hmac_hashctxt1);
  105. (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt2,
  106. result, hashparams->hmac_resultlen);
  107. (*hashparams->hmac_hfinal)(result, ctxt->hmac_hashctxt2);
  108. free((char *) ctxt);
  109. return 0;
  110. }
  111. /*
  112. * Curl_hmacit()
  113. *
  114. * This is used to generate a HMAC hash, for the specified input data, given
  115. * the specified hash function and key.
  116. *
  117. * Parameters:
  118. *
  119. * hashparams [in] - The hash function (Curl_HMAC_MD5).
  120. * key [in] - The key to use.
  121. * keylen [in] - The length of the key.
  122. * data [in] - The data to encrypt.
  123. * datalen [in] - The length of the data.
  124. * output [in/out] - The output buffer.
  125. *
  126. * Returns CURLE_OK on success.
  127. */
  128. CURLcode Curl_hmacit(const struct HMAC_params *hashparams,
  129. const unsigned char *key, const size_t keylen,
  130. const unsigned char *data, const size_t datalen,
  131. unsigned char *output)
  132. {
  133. struct HMAC_context *ctxt =
  134. Curl_HMAC_init(hashparams, key, curlx_uztoui(keylen));
  135. if(!ctxt)
  136. return CURLE_OUT_OF_MEMORY;
  137. /* Update the digest with the given challenge */
  138. Curl_HMAC_update(ctxt, data, curlx_uztoui(datalen));
  139. /* Finalise the digest */
  140. Curl_HMAC_final(ctxt, output);
  141. return CURLE_OK;
  142. }
  143. #endif /* Using NTLM (without SSPI) or AWS */