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.

378 lines
7.5 KiB

  1. /*++
  2. Copyright (c) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. ahxforms.c
  5. Abstract:
  6. This module contains the code to create various AH transforms
  7. Author:
  8. Sanjay Anand (SanjayAn) 2-January-1997
  9. ChunYe
  10. Environment:
  11. Kernel mode
  12. Revision History:
  13. --*/
  14. #include "precomp.h"
  15. #ifdef RUN_WPP
  16. #include "ahxforms.tmh"
  17. #endif
  18. #define MAX_LEN_PAD 65
  19. NTSTATUS
  20. ah_nullinit(
  21. IN PALGO_STATE pState,
  22. IN ULONG Index
  23. )
  24. /*++
  25. Routine Description:
  26. Init the MD5 context for keyed MD5
  27. Arguments:
  28. pState - state buffer which needs to be passed into the update/finish functions
  29. Return Value:
  30. STATUS_SUCCESS
  31. Others:
  32. STATUS_INSUFFICIENT_RESOURCES
  33. STATUS_UNSUCCESSFUL (error in algo.)
  34. --*/
  35. {
  36. return STATUS_SUCCESS;
  37. }
  38. NTSTATUS
  39. ah_nullupdate(
  40. IN PALGO_STATE pState,
  41. IN PUCHAR pData,
  42. IN ULONG Len
  43. )
  44. /*++
  45. Routine Description:
  46. Continue MD5 over the data passed in; as a side-effect, updates the bytes
  47. transformed count in the SA (for key-expiration)
  48. Arguments:
  49. pState - algo state buffer
  50. pData - data to be hashed
  51. Len - length of above data
  52. Return Value:
  53. STATUS_SUCCESS
  54. --*/
  55. {
  56. return STATUS_SUCCESS;
  57. }
  58. NTSTATUS
  59. ah_nullfinish(
  60. IN PALGO_STATE pState,
  61. OUT PUCHAR pHash,
  62. IN ULONG Index
  63. )
  64. /*++
  65. Routine Description:
  66. Finish the MD5 calculation
  67. Arguments:
  68. pState - algo state buffer
  69. pHash - pointer to final hash data
  70. Return Value:
  71. STATUS_SUCCESS
  72. --*/
  73. {
  74. RtlCopyMemory(pHash, "0123456789012345", MD5DIGESTLEN);
  75. return STATUS_SUCCESS;
  76. }
  77. /*++
  78. The ah_hmac* family:
  79. Generates the actual hash using HMAC-MD5 or HMAC-SHA according to RFC 2104
  80. which works as under:
  81. We define two fixed and different strings ipad and opad as follows
  82. (the 'i' and 'o' are mnemonics for inner and outer):
  83. ipad = the byte 0x36 repeated B times
  84. opad = the byte 0x5C repeated B times.
  85. To compute HMAC over the data `text' we perform
  86. H(K XOR opad, H(K XOR ipad, text))
  87. --*/
  88. NTSTATUS
  89. ah_hmacmd5init(
  90. IN PALGO_STATE pState,
  91. IN ULONG Index
  92. )
  93. /*++
  94. Routine Description:
  95. Init the MD5 context for HMAC.
  96. Arguments:
  97. pState - state buffer which needs to be passed into the update/finish functions
  98. Return Value:
  99. STATUS_SUCCESS
  100. Others:
  101. STATUS_INSUFFICIENT_RESOURCES
  102. STATUS_UNSUCCESSFUL (error in algo.)
  103. --*/
  104. {
  105. PSA_TABLE_ENTRY pSA = pState->as_sa;
  106. PUCHAR key = pSA->INT_KEY(Index);
  107. ULONG key_len = pSA->INT_KEYLEN(Index);
  108. UCHAR k_ipad[MAX_LEN_PAD]; /* inner padding - key XORd with ipad */
  109. UCHAR tk[MD5DIGESTLEN];
  110. ULONG i;
  111. IPSEC_HMAC_MD5_INIT(&(pState->as_md5ctx),
  112. key,
  113. key_len);
  114. IPSEC_DEBUG(LL_A,DBF_AHEX, ("MD5init: %lx-%lx-%lx-%lx-%lx-%lx-%lx-%lx",
  115. *(ULONG *)&(pState->as_md5ctx).in[0],
  116. *(ULONG *)&(pState->as_md5ctx).in[4],
  117. *(ULONG *)&(pState->as_md5ctx).in[8],
  118. *(ULONG *)&(pState->as_md5ctx).in[12],
  119. *(ULONG *)&(pState->as_md5ctx).in[16],
  120. *(ULONG *)&(pState->as_md5ctx).in[20],
  121. *(ULONG *)&(pState->as_md5ctx).in[24],
  122. *(ULONG *)&(pState->as_md5ctx).in[28]));
  123. return STATUS_SUCCESS;
  124. }
  125. NTSTATUS
  126. ah_hmacmd5update(
  127. IN PALGO_STATE pState,
  128. IN PUCHAR pData,
  129. IN ULONG Len
  130. )
  131. /*++
  132. Routine Description:
  133. Continue MD5 over the data passed in; as a side-effect, updates the bytes
  134. transformed count in the SA (for key-expiration)
  135. Arguments:
  136. pState - algo state buffer
  137. pData - data to be hashed
  138. Len - length of above data
  139. Return Value:
  140. STATUS_SUCCESS
  141. --*/
  142. {
  143. PSA_TABLE_ENTRY pSA = pState->as_sa;
  144. IPSEC_HMAC_MD5_UPDATE(&(pState->as_md5ctx), pData, Len);
  145. IPSEC_DEBUG(LL_A,DBF_AHEX, ("MD5update: %lx-%lx-%lx-%lx-%lx-%lx-%lx-%lx",
  146. *(ULONG *)&(pState->as_md5ctx).in[0],
  147. *(ULONG *)&(pState->as_md5ctx).in[4],
  148. *(ULONG *)&(pState->as_md5ctx).in[8],
  149. *(ULONG *)&(pState->as_md5ctx).in[12],
  150. *(ULONG *)&(pState->as_md5ctx).in[16],
  151. *(ULONG *)&(pState->as_md5ctx).in[20],
  152. *(ULONG *)&(pState->as_md5ctx).in[24],
  153. *(ULONG *)&(pState->as_md5ctx).in[28]));
  154. return STATUS_SUCCESS;
  155. }
  156. NTSTATUS
  157. ah_hmacmd5finish(
  158. IN PALGO_STATE pState,
  159. OUT PUCHAR pHash,
  160. IN ULONG Index
  161. )
  162. /*++
  163. Routine Description:
  164. Finish the MD5 calculation
  165. Arguments:
  166. pState - algo state buffer
  167. pHash - pointer to final hash data
  168. Return Value:
  169. STATUS_SUCCESS
  170. --*/
  171. {
  172. UCHAR k_opad[MAX_LEN_PAD]; /* outer padding - key XORd with opad */
  173. UCHAR tk[MD5DIGESTLEN];
  174. PSA_TABLE_ENTRY pSA = pState->as_sa;
  175. PUCHAR key = pSA->INT_KEY(Index);
  176. ULONG key_len = pSA->INT_KEYLEN(Index);
  177. ULONG i;
  178. IPSEC_HMAC_MD5_FINAL(&(pState->as_md5ctx),key,key_len,pHash);
  179. return STATUS_SUCCESS;
  180. }
  181. NTSTATUS
  182. ah_hmacshainit(
  183. IN PALGO_STATE pState,
  184. IN ULONG Index
  185. )
  186. /*++
  187. Routine Description:
  188. Init the SHA context for HMAC.
  189. Arguments:
  190. pState - state buffer which needs to be passed into the update/finish functions
  191. Return Value:
  192. STATUS_SUCCESS
  193. Others:
  194. STATUS_INSUFFICIENT_RESOURCES
  195. STATUS_UNSUCCESSFUL (error in algo.)
  196. --*/
  197. {
  198. PSA_TABLE_ENTRY pSA = pState->as_sa;
  199. PUCHAR key = pSA->INT_KEY(Index);
  200. ULONG key_len = pSA->INT_KEYLEN(Index);
  201. UCHAR k_ipad[MAX_LEN_PAD]; /* inner padding - key XORd with ipad */
  202. UCHAR tk[A_SHA_DIGEST_LEN];
  203. ULONG i;
  204. IPSEC_HMAC_SHA_INIT(&(pState->as_shactx),key,key_len);
  205. return STATUS_SUCCESS;
  206. }
  207. NTSTATUS
  208. ah_hmacshaupdate(
  209. IN PALGO_STATE pState,
  210. IN PUCHAR pData,
  211. IN ULONG Len
  212. )
  213. /*++
  214. Routine Description:
  215. Continue A_SHA_ over the data passed in; as a side-effect, updates the bytes
  216. transformed count in the SA (for key-expiration)
  217. Arguments:
  218. pState - algo state buffer
  219. pData - data to be hashed
  220. Len - length of above data
  221. Return Value:
  222. STATUS_SUCCESS
  223. --*/
  224. {
  225. PSA_TABLE_ENTRY pSA = pState->as_sa;
  226. IPSEC_HMAC_SHA_UPDATE(&(pState->as_shactx), pData, Len);
  227. return STATUS_SUCCESS;
  228. }
  229. NTSTATUS
  230. ah_hmacshafinish(
  231. IN PALGO_STATE pState,
  232. OUT PUCHAR pHash,
  233. IN ULONG Index
  234. )
  235. /*++
  236. Routine Description:
  237. Finish the A_SHA_ calculation
  238. Arguments:
  239. pState - algo state buffer
  240. pHash - pointer to final hash data
  241. Return Value:
  242. STATUS_SUCCESS
  243. --*/
  244. {
  245. UCHAR k_opad[MAX_LEN_PAD]; /* outer padding - key XORd with opad */
  246. UCHAR tk[A_SHA_DIGEST_LEN];
  247. PSA_TABLE_ENTRY pSA = pState->as_sa;
  248. PUCHAR key = pSA->INT_KEY(Index);
  249. ULONG key_len = pSA->INT_KEYLEN(Index);
  250. ULONG i;
  251. IPSEC_HMAC_SHA_FINAL(&(pState->as_shactx),key,key_len, pHash);
  252. return STATUS_SUCCESS;
  253. }