2
0

hmac.c 5.1 KB

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