hmac.c 5.0 KB

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