hmac.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #ifndef CURL_DISABLE_CRYPTO_AUTH
  28. #include <curl/curl.h>
  29. #include "curl_hmac.h"
  30. #include "curl_memory.h"
  31. #include "warnless.h"
  32. /* The last #include file should be: */
  33. #include "memdebug.h"
  34. /*
  35. * Generic HMAC algorithm.
  36. *
  37. * This module computes HMAC digests based on any hash function. Parameters
  38. * and computing procedures are set-up dynamically at HMAC computation context
  39. * initialization.
  40. */
  41. static const unsigned char hmac_ipad = 0x36;
  42. static const unsigned char hmac_opad = 0x5C;
  43. struct HMAC_context *
  44. Curl_HMAC_init(const struct HMAC_params *hashparams,
  45. const unsigned char *key,
  46. unsigned int keylen)
  47. {
  48. size_t i;
  49. struct HMAC_context *ctxt;
  50. unsigned char *hkey;
  51. unsigned char b;
  52. /* Create HMAC context. */
  53. i = sizeof(*ctxt) + 2 * hashparams->hmac_ctxtsize +
  54. hashparams->hmac_resultlen;
  55. ctxt = malloc(i);
  56. if(!ctxt)
  57. return ctxt;
  58. ctxt->hmac_hash = hashparams;
  59. ctxt->hmac_hashctxt1 = (void *) (ctxt + 1);
  60. ctxt->hmac_hashctxt2 = (void *) ((char *) ctxt->hmac_hashctxt1 +
  61. hashparams->hmac_ctxtsize);
  62. /* If the key is too long, replace it by its hash digest. */
  63. if(keylen > hashparams->hmac_maxkeylen) {
  64. (*hashparams->hmac_hinit)(ctxt->hmac_hashctxt1);
  65. (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt1, key, keylen);
  66. hkey = (unsigned char *) ctxt->hmac_hashctxt2 + hashparams->hmac_ctxtsize;
  67. (*hashparams->hmac_hfinal)(hkey, ctxt->hmac_hashctxt1);
  68. key = hkey;
  69. keylen = hashparams->hmac_resultlen;
  70. }
  71. /* Prime the two hash contexts with the modified key. */
  72. (*hashparams->hmac_hinit)(ctxt->hmac_hashctxt1);
  73. (*hashparams->hmac_hinit)(ctxt->hmac_hashctxt2);
  74. for(i = 0; i < keylen; i++) {
  75. b = (unsigned char)(*key ^ hmac_ipad);
  76. (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt1, &b, 1);
  77. b = (unsigned char)(*key++ ^ hmac_opad);
  78. (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt2, &b, 1);
  79. }
  80. for(; i < hashparams->hmac_maxkeylen; i++) {
  81. (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt1, &hmac_ipad, 1);
  82. (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt2, &hmac_opad, 1);
  83. }
  84. /* Done, return pointer to HMAC context. */
  85. return ctxt;
  86. }
  87. int Curl_HMAC_update(struct HMAC_context *ctxt,
  88. const unsigned char *data,
  89. unsigned int len)
  90. {
  91. /* Update first hash calculation. */
  92. (*ctxt->hmac_hash->hmac_hupdate)(ctxt->hmac_hashctxt1, data, len);
  93. return 0;
  94. }
  95. int Curl_HMAC_final(struct HMAC_context *ctxt, unsigned char *result)
  96. {
  97. const struct HMAC_params *hashparams = ctxt->hmac_hash;
  98. /* Do not get result if called with a null parameter: only release
  99. storage. */
  100. if(!result)
  101. result = (unsigned char *) ctxt->hmac_hashctxt2 +
  102. ctxt->hmac_hash->hmac_ctxtsize;
  103. (*hashparams->hmac_hfinal)(result, ctxt->hmac_hashctxt1);
  104. (*hashparams->hmac_hupdate)(ctxt->hmac_hashctxt2,
  105. result, hashparams->hmac_resultlen);
  106. (*hashparams->hmac_hfinal)(result, ctxt->hmac_hashctxt2);
  107. free((char *) 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. * data [in] - The data to encrypt.
  122. * datalen [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 *data, const size_t datalen,
  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, data, curlx_uztoui(datalen));
  138. /* Finalise the digest */
  139. Curl_HMAC_final(ctxt, output);
  140. return CURLE_OK;
  141. }
  142. #endif /* CURL_DISABLE_CRYPTO_AUTH */