hmac.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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 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 "setup.h"
  25. #ifndef CURL_DISABLE_CRYPTO_AUTH
  26. #include "curl_hmac.h"
  27. #define _MPRINTF_REPLACE /* use our functions only */
  28. #include <curl/mprintf.h>
  29. #include "curl_memory.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. HMAC_context *
  42. Curl_HMAC_init(const HMAC_params * hashparams,
  43. const unsigned char * key,
  44. unsigned int keylen)
  45. {
  46. size_t i;
  47. 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(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(HMAC_context * ctxt, unsigned char * result)
  94. {
  95. const 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. #endif /* CURL_DISABLE_CRYPTO_AUTH */