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.

195 lines
5.5 KiB

  1. // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil -*- (for GNU Emacs)
  2. //
  3. // Copyright (c) 1985-2000 Microsoft Corporation
  4. //
  5. // This file is part of the Microsoft Research IPv6 Network Protocol Stack.
  6. // You should have received a copy of the Microsoft End-User License Agreement
  7. // for this software along with this release; see the file "license.txt".
  8. // If not, please see http://www.research.microsoft.com/msripv6/license.htm,
  9. // or write to Microsoft Research, One Microsoft Way, Redmond, WA 98052-6399.
  10. //
  11. // Abstract:
  12. //
  13. // HMAC (Hashed? Message Authentication Code) wrapper for SHA-1 algorithm.
  14. // This code is based on the algorithm given in RFC 2104, which for HMAC-SHA1
  15. // is SHA-1(Key XOR outer pad, SHA-1(Key XOR inner pad, text)), where the
  16. // inner pad is 64 bytes of 0x36 and the outer pad is 64 bytes of 0x5c.
  17. //
  18. #include <string.h>
  19. #include "oscfg.h"
  20. #include <sha.h>
  21. //* HMAC_SHA1KeyPrep - preprocess raw keying data into directly usuable form.
  22. //
  23. // This routine is called to convert raw keying information into the most
  24. // convienient form for later processing. For SHA-1, we hash keys larger than
  25. // 64 bytes down to 64 bytes. We also perform the XOR operations between the
  26. // key and the inner and outer pads here since they don't change once we have
  27. // the key. Thus we return 128 bytes of data: 64 bytes of the (possibly
  28. // folded) key XOR'd with the inner pad, and 64 bytes of the key XOR'd with
  29. // the outer pad.
  30. //
  31. // REVIEW: Instead of "Temp", we could operate directly "*Key".
  32. //
  33. void
  34. HMAC_SHA1KeyPrep(
  35. uchar *RawKey, // Raw keying information.
  36. uint RawKeySize, // Size of above in bytes.
  37. uchar *Key) // Resulting 128 bytes of preprocessed key info.
  38. {
  39. uchar Temp[128];
  40. uint Loop;
  41. //
  42. // Load raw key into temp storage.
  43. // Constrain size of keying information to 64 bytes.
  44. //
  45. memset(Temp, 0, 64);
  46. if (RawKeySize > 64) {
  47. A_SHA_CTX Context;
  48. //
  49. // Use SHA-1 to hash key down to 20 bytes.
  50. //
  51. A_SHAInit(&Context);
  52. A_SHAUpdate(&Context, RawKey, RawKeySize);
  53. A_SHAFinal(&Context, Temp);
  54. } else
  55. memcpy(Temp, RawKey, RawKeySize);
  56. //
  57. // The first 64 bytes of "Temp" contain our (possibly hashed) key.
  58. // Make a copy of this in the second 64 bytes.
  59. //
  60. memcpy(&Temp[64], Temp, 64);
  61. //
  62. // XOR the first 64 bytes with the inner pad, and the second 64 bytes
  63. // with the outer pad.
  64. //
  65. for (Loop = 0; Loop < 64; Loop++) {
  66. Temp[Loop] ^= 0x36; // Inner Pad.
  67. Temp[Loop + 64] ^= 0x5c; // Outer Pad.
  68. }
  69. //
  70. // Return the result to location designated by our caller.
  71. //
  72. memcpy(Key, Temp, 128);
  73. //
  74. // Zero sensitive information.
  75. // REVIEW: Bother?
  76. //
  77. RtlSecureZeroMemory(Temp, 128);
  78. }
  79. //* HMAC_SHA1Init - prepare to process data.
  80. //
  81. void
  82. HMAC_SHA1Init(
  83. void *GenericContext, // HMAC-SHA1 context maintained across operations.
  84. uchar *Key) // Keying information.
  85. {
  86. A_SHA_CTX *Context = GenericContext;
  87. //
  88. // Start off the inner hash. I.e. "SHA-1(Key XOR inner pad, ...".
  89. //
  90. A_SHAInit(Context);
  91. A_SHAUpdate(Context, Key, 64);
  92. }
  93. //* HMAC_SHA1Op - Process a chunk of data.
  94. //
  95. void
  96. HMAC_SHA1Op(
  97. void *GenericContext, // HMAC-SHA1 context maintained across operations.
  98. uchar *Key, // Keying information.
  99. uchar *Data, // Data to process.
  100. uint Len) // Amount of above in bytes.
  101. {
  102. A_SHA_CTX *Context = GenericContext;
  103. UNREFERENCED_PARAMETER(Key);
  104. //
  105. // Continue the inner hash. I.e. "SHA-1(..., text)".
  106. //
  107. A_SHAUpdate(Context, Data, Len);
  108. }
  109. //* HMAC_SHA1Final - close off processing current data and return result.
  110. //
  111. // REVIEW: Instead of "Temp", we could operate directly "*Result".
  112. //
  113. void
  114. HMAC_SHA1Final(
  115. void *GenericContext, // HMAC-SHA1 context maintained across operations.
  116. uchar *Key, // Keying information.
  117. uchar *Result) // Where to put result of this process.
  118. {
  119. uchar Temp[20];
  120. A_SHA_CTX *Context = GenericContext;
  121. //
  122. // Finish the inner hash.
  123. //
  124. A_SHAFinal(Context, Temp);
  125. //
  126. // Perform the outer hash. I.e. SHA-1(Key XOR outer pad, ...).
  127. // SHA1Final returns the result directly to our caller.
  128. //
  129. A_SHAInit(Context);
  130. A_SHAUpdate(Context, &Key[64], 64);
  131. A_SHAUpdate(Context, Temp, 20);
  132. A_SHAFinal(Context, Result);
  133. //
  134. // Zero sensitive information.
  135. // REVIEW: Bother?
  136. //
  137. RtlSecureZeroMemory(Temp, 20);
  138. }
  139. void
  140. HMAC_SHA1_96Final(
  141. void *GenericContext, // HMAC-SHA1 context maintained across operations.
  142. uchar *Key, // Keying information.
  143. uchar *Result) // Where to put result of this process.
  144. {
  145. uchar Temp[20];
  146. A_SHA_CTX *Context = GenericContext;
  147. //
  148. // Finish the inner hash.
  149. //
  150. A_SHAFinal(Context, Temp);
  151. //
  152. // Perform the outer hash. I.e. SHA-1(Key XOR outer pad, ...).
  153. //
  154. A_SHAInit(Context);
  155. A_SHAUpdate(Context, &Key[64], 64);
  156. A_SHAUpdate(Context, Temp, 20);
  157. A_SHAFinal(Context, Temp);
  158. //
  159. // Truncate the SHA1 20 byte output to 12 bytes.
  160. // The first 12 bytes from the left are stored.
  161. //
  162. memcpy(Result, Temp, 12);
  163. //
  164. // Zero sensitive information.
  165. // REVIEW: Bother?
  166. //
  167. RtlSecureZeroMemory(Temp, 20);
  168. }