2
0

hmac.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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->ctxtsize + hashparams->resultlen;
  55. ctxt = malloc(i);
  56. if(!ctxt)
  57. return ctxt;
  58. ctxt->hash = hashparams;
  59. ctxt->hashctxt1 = (void *) (ctxt + 1);
  60. ctxt->hashctxt2 = (void *) ((char *) ctxt->hashctxt1 + hashparams->ctxtsize);
  61. /* If the key is too long, replace it by its hash digest. */
  62. if(keylen > hashparams->maxkeylen) {
  63. hashparams->hinit(ctxt->hashctxt1);
  64. hashparams->hupdate(ctxt->hashctxt1, key, keylen);
  65. hkey = (unsigned char *) ctxt->hashctxt2 + hashparams->ctxtsize;
  66. hashparams->hfinal(hkey, ctxt->hashctxt1);
  67. key = hkey;
  68. keylen = hashparams->resultlen;
  69. }
  70. /* Prime the two hash contexts with the modified key. */
  71. hashparams->hinit(ctxt->hashctxt1);
  72. hashparams->hinit(ctxt->hashctxt2);
  73. for(i = 0; i < keylen; i++) {
  74. b = (unsigned char)(*key ^ hmac_ipad);
  75. hashparams->hupdate(ctxt->hashctxt1, &b, 1);
  76. b = (unsigned char)(*key++ ^ hmac_opad);
  77. hashparams->hupdate(ctxt->hashctxt2, &b, 1);
  78. }
  79. for(; i < hashparams->maxkeylen; i++) {
  80. hashparams->hupdate(ctxt->hashctxt1, &hmac_ipad, 1);
  81. hashparams->hupdate(ctxt->hashctxt2, &hmac_opad, 1);
  82. }
  83. /* Done, return pointer to HMAC context. */
  84. return ctxt;
  85. }
  86. int Curl_HMAC_update(struct HMAC_context *ctxt,
  87. const unsigned char *ptr,
  88. unsigned int len)
  89. {
  90. /* Update first hash calculation. */
  91. ctxt->hash->hupdate(ctxt->hashctxt1, ptr, len);
  92. return 0;
  93. }
  94. int Curl_HMAC_final(struct HMAC_context *ctxt, unsigned char *output)
  95. {
  96. const struct HMAC_params *hashparams = ctxt->hash;
  97. /* Do not get output if called with a null parameter: only release
  98. storage. */
  99. if(!output)
  100. output = (unsigned char *) ctxt->hashctxt2 + ctxt->hash->ctxtsize;
  101. hashparams->hfinal(output, ctxt->hashctxt1);
  102. hashparams->hupdate(ctxt->hashctxt2, output, hashparams->resultlen);
  103. hashparams->hfinal(output, ctxt->hashctxt2);
  104. free(ctxt);
  105. return 0;
  106. }
  107. /*
  108. * Curl_hmacit()
  109. *
  110. * This is used to generate a HMAC hash, for the specified input data, given
  111. * the specified hash function and key.
  112. *
  113. * Parameters:
  114. *
  115. * hashparams [in] - The hash function (Curl_HMAC_MD5).
  116. * key [in] - The key to use.
  117. * keylen [in] - The length of the key.
  118. * buf [in] - The data to encrypt.
  119. * buflen [in] - The length of the data.
  120. * output [in/out] - The output buffer.
  121. *
  122. * Returns CURLE_OK on success.
  123. */
  124. CURLcode Curl_hmacit(const struct HMAC_params *hashparams,
  125. const unsigned char *key, const size_t keylen,
  126. const unsigned char *buf, const size_t buflen,
  127. unsigned char *output)
  128. {
  129. struct HMAC_context *ctxt =
  130. Curl_HMAC_init(hashparams, key, curlx_uztoui(keylen));
  131. if(!ctxt)
  132. return CURLE_OUT_OF_MEMORY;
  133. /* Update the digest with the given challenge */
  134. Curl_HMAC_update(ctxt, buf, curlx_uztoui(buflen));
  135. /* Finalise the digest */
  136. Curl_HMAC_final(ctxt, output);
  137. return CURLE_OK;
  138. }
  139. #endif /* Using NTLM (without SSPI) or AWS */