hmac.c 4.0 KB

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