2
0

hmac.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, 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 + 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 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 */