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.

62 lines
1.4 KiB

  1. #include "stdafx.h"
  2. #ifdef UNIX
  3. #include "sha.cpp"
  4. #else
  5. #include <sha.h>
  6. #endif
  7. #define SHA_BLOCKSIZE 64
  8. void hmac_sha(unsigned char *k, int lk,
  9. unsigned char *d, int ld,
  10. unsigned char *out, int t)
  11. {
  12. A_SHA_CTX ictx, octx ;
  13. unsigned char isha[A_SHA_DIGEST_LEN], osha[A_SHA_DIGEST_LEN] ;
  14. unsigned char key[A_SHA_DIGEST_LEN] ;
  15. unsigned char buf[SHA_BLOCKSIZE] ;
  16. int i ;
  17. if (lk > SHA_BLOCKSIZE) {
  18. A_SHA_CTX tctx ;
  19. A_SHAInit(&tctx) ;
  20. A_SHAUpdate(&tctx, k, lk) ;
  21. A_SHAFinal(&tctx, key) ;
  22. k = key ;
  23. lk = A_SHA_DIGEST_LEN ;
  24. }
  25. /**** Inner Digest ****/
  26. A_SHAInit(&ictx) ;
  27. /* Pad the key for inner digest */
  28. for (i = 0 ; i < lk ; ++i) buf[i] = k[i] ^ 0x36 ;
  29. for (i = lk ; i < SHA_BLOCKSIZE ; ++i) buf[i] = 0x36 ;
  30. A_SHAUpdate(&ictx, buf, SHA_BLOCKSIZE) ;
  31. A_SHAUpdate(&ictx, d, ld) ;
  32. A_SHAFinal(&ictx, isha);
  33. /**** Outter Digest ****/
  34. A_SHAInit(&octx) ;
  35. /* Pad the key for outter digest */
  36. for (i = 0 ; i < lk ; ++i) buf[i] = k[i] ^ 0x5C ;
  37. for (i = lk ; i < SHA_BLOCKSIZE ; ++i) buf[i] = 0x5C ;
  38. A_SHAUpdate(&octx, buf, SHA_BLOCKSIZE) ;
  39. A_SHAUpdate(&octx, isha, A_SHA_DIGEST_LEN) ;
  40. A_SHAFinal(&octx, osha) ;
  41. /* truncate and print the results */
  42. memcpy(out, osha, (t > A_SHA_DIGEST_LEN) ? A_SHA_DIGEST_LEN : t);
  43. }