hmac_md5.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*****************************************************************************
  2. * __________________ _________ _____ _____ .__ ._.
  3. * \______ \______ \ / _____/ / \ / _ \ |__| ____ | |
  4. * | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | |
  5. * | | \| ` \/ / Y \ / | | \ ___/ \|
  6. * |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __
  7. * \/ \/ \/ \/ )/ \/ \/ \/
  8. *
  9. * This file is part of liBDSM. Copyright © 2014-2015 VideoLabs SAS
  10. *
  11. * Author: Julien 'Lta' BALLET <contact@lta.io>
  12. *
  13. * liBDSM is released under LGPLv2.1 (or later) and is also available
  14. * under a commercial license.
  15. *****************************************************************************
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU Lesser General Public License as published by
  18. * the Free Software Foundation; either version 2.1 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Lesser General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Lesser General Public License
  27. * along with this program; if not, write to the Free Software Foundation,
  28. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  29. *****************************************************************************/
  30. #include <assert.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "hmac_md5.h"
  34. unsigned char *HMAC_MD5(const void *key, size_t key_len, const void *msg,
  35. size_t msg_len, void *hmac)
  36. {
  37. static uint8_t hmac_static[16];
  38. uint8_t key_pad[64], o_key_pad[64], i_key_pad[64], kcat[80];
  39. void *cat, *out;
  40. MD5_CTX ctx;
  41. assert(key != NULL && msg != NULL);
  42. // This is Microsoft variation of HMAC_MD5 for NTLMv2
  43. // It seems they truncate over-sized keys instead of rehashing
  44. if (key_len > 64)
  45. key_len = 64;
  46. memcpy(key_pad, key, key_len);
  47. memset(key_pad + key_len, 0, 64 - key_len);
  48. // Compute the o/i XORed padded keys
  49. for (unsigned i = 0; i < 64; i++)
  50. {
  51. o_key_pad[i] = 0x5c ^ key_pad[i];
  52. i_key_pad[i] = 0x36 ^ key_pad[i];
  53. }
  54. // Concatenate inner padded key with message
  55. cat = malloc(msg_len + 64);
  56. if (!cat)
  57. return NULL;
  58. memcpy(cat, i_key_pad, 64);
  59. memcpy(cat + 64, msg, msg_len);
  60. // MD5 of the result
  61. MD5_Init(&ctx);
  62. MD5_Update(&ctx, cat, msg_len + 64);
  63. MD5_Final(key_pad, &ctx);
  64. free(cat);
  65. memcpy(kcat, o_key_pad, 64);
  66. memcpy(kcat + 64, key_pad, 16);
  67. if (hmac != NULL)
  68. out = hmac;
  69. else
  70. out = hmac_static;
  71. MD5_Init(&ctx);
  72. MD5_Update(&ctx, kcat, 80);
  73. MD5_Final(out, &ctx);
  74. return (out);
  75. }