Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.4 KiB

  1. /*++
  2. Copyright (C) 1997 Cisco Systems, Inc. All Rights Reserved.
  3. Module Name:
  4. hmac_md5.c
  5. Abstract:
  6. This module contains routines to perform HMAC using MD5
  7. as defined in RFC-2104.
  8. Author:
  9. Derrell Piper (v-dpiper)
  10. Facility:
  11. ISAKMP/Oakley
  12. Revision History:
  13. --*/
  14. //#include <iascore.h>
  15. #include <windows.h>
  16. #include <hmacmd5.h>
  17. #ifdef WIN32
  18. #define bcopy(b1, b2, len) memcpy((b2), (b1), (len))
  19. #define bzero(b,len) memset((b), 0, (len))
  20. #define bcmp(b1, b2, len) memcmp((b1), (b2), (len))
  21. #endif /* WIN32 */
  22. /*
  23. * hmac_md5 = MD5(key ^ opad, MD5(key ^ ipad, text))
  24. * where ipad is 64 0x36's, and
  25. * opad is 64 0x5c's
  26. *
  27. * Also contains native MD5 wrappers which allow for consistent calling
  28. */
  29. VOID
  30. HmacMD5Init (
  31. PHmacContext pContext,
  32. PBYTE pkey,
  33. ULONG keylen
  34. )
  35. {
  36. BYTE ipad[64];
  37. BYTE keydigest[MD5_LEN];
  38. int i;
  39. if((pkey == NULL) && (keylen != 0))
  40. return;
  41. if(keylen > 64)
  42. {
  43. MD5_CTX tempctx;
  44. MD5Init(&tempctx);
  45. MD5Update(&tempctx, pkey, keylen);
  46. MD5Final(&tempctx);
  47. CopyMemory( keydigest, tempctx.digest, MD5_LEN );
  48. pkey = keydigest;
  49. keylen = MD5_LEN;
  50. }
  51. bzero(ipad, 64);
  52. bzero(pContext->opad, 64);
  53. bcopy(pkey, ipad, keylen);
  54. bcopy(pkey, pContext->opad, keylen);
  55. for(i=0; i<64; i++)
  56. {
  57. ipad[i] ^= 0x36;
  58. pContext->opad[i] ^= 0x5c;
  59. }
  60. MD5Init(&pContext->md5);
  61. MD5Update(&pContext->md5, ipad, 64);
  62. return;
  63. } // end of HmacMD5Init method
  64. VOID
  65. HmacMD5Update (
  66. PHmacContext pContext,
  67. PBYTE ptext,
  68. ULONG textlen
  69. )
  70. {
  71. if((ptext == NULL) || (textlen == 0))
  72. return;
  73. MD5Update(&pContext->md5, ptext, textlen);
  74. } // end of HMacMD5Update method
  75. VOID
  76. HmacMD5Final (
  77. BYTE pdigest[MD5_LEN],
  78. PHmacContext pContext
  79. )
  80. {
  81. BYTE tempDigest[MD5_LEN];
  82. if ((NULL == pdigest) || (NULL == pContext))
  83. return;
  84. MD5Final(&pContext->md5);
  85. CopyMemory( tempDigest, pContext->md5.digest, MD5_LEN );
  86. MD5Init(&pContext->md5);
  87. MD5Update(&pContext->md5, pContext->opad, 64);
  88. MD5Update(&pContext->md5, tempDigest, MD5_LEN);
  89. MD5Final(&pContext->md5);
  90. CopyMemory( pdigest, pContext->md5.digest, MD5_LEN );
  91. return;
  92. }