hmac.c 4.0 KB

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