1
0

md5.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. md5.c
  5. Abstract:
  6. This module implements the MD5 hash routine.
  7. Author:
  8. Evan Green 14-Jan-2015
  9. Environment:
  10. Kernel
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include "cryptop.h"
  16. //
  17. // --------------------------------------------------------------------- Macros
  18. //
  19. //
  20. // Define F, G, H, and I, the basic MD5 functions.
  21. //
  22. #define MD5_F(_ValueX, _ValueY, _ValueZ) \
  23. (((_ValueX) & (_ValueY)) | ((~_ValueX) & (_ValueZ)))
  24. #define MD5_G(_ValueX, _ValueY, _ValueZ) \
  25. (((_ValueX) & (_ValueZ)) | ((_ValueY) & (~_ValueZ)))
  26. #define MD5_H(_ValueX, _ValueY, _ValueZ) \
  27. ((_ValueX) ^ (_ValueY) ^ (_ValueZ))
  28. #define MD5_I(_ValueX, _ValueY, _ValueZ) \
  29. ((_ValueY) ^ ((_ValueX) | (~_ValueZ)))
  30. #define MD5_ROTATE_LEFT(_Value, _Count) \
  31. (((_Value) << (_Count)) | ((_Value) >> (32 - (_Count))))
  32. //
  33. // Define FF, GG, HH, and II transformations for rounds 1 through 4.
  34. //
  35. #define MD5_FF(_ValueA, _ValueB, _ValueC, _ValueD, _ValueX, _Shift, _ValueAc) \
  36. (_ValueA) += MD5_F((_ValueB), (_ValueC), (_ValueD)) + (_ValueX) + \
  37. (ULONG)(_ValueAc); \
  38. (_ValueA) = MD5_ROTATE_LEFT((_ValueA), (_Shift)); \
  39. (_ValueA) += (_ValueB);
  40. #define MD5_GG(_ValueA, _ValueB, _ValueC, _ValueD, _ValueX, _Shift, _ValueAc) \
  41. (_ValueA) += MD5_G((_ValueB), (_ValueC), (_ValueD)) + (_ValueX) + \
  42. (ULONG)(_ValueAc); \
  43. (_ValueA) = MD5_ROTATE_LEFT((_ValueA), (_Shift)); \
  44. (_ValueA) += (_ValueB);
  45. #define MD5_HH(_ValueA, _ValueB, _ValueC, _ValueD, _ValueX, _Shift, _ValueAc) \
  46. (_ValueA) += MD5_H((_ValueB), (_ValueC), (_ValueD)) + (_ValueX) + \
  47. (ULONG)(_ValueAc); \
  48. (_ValueA) = MD5_ROTATE_LEFT((_ValueA), (_Shift)); \
  49. (_ValueA) += (_ValueB);
  50. #define MD5_II(_ValueA, _ValueB, _ValueC, _ValueD, _ValueX, _Shift, _ValueAc) \
  51. (_ValueA) += MD5_I((_ValueB), (_ValueC), (_ValueD)) + (_ValueX) + \
  52. (ULONG)(_ValueAc); \
  53. (_ValueA) = MD5_ROTATE_LEFT((_ValueA), (_Shift)); \
  54. (_ValueA) += (_ValueB);
  55. //
  56. // ---------------------------------------------------------------- Definitions
  57. //
  58. //
  59. // Define constants for the MD5 transform routine.
  60. //
  61. #define MD5_S11 7
  62. #define MD5_S12 12
  63. #define MD5_S13 17
  64. #define MD5_S14 22
  65. #define MD5_S21 5
  66. #define MD5_S22 9
  67. #define MD5_S23 14
  68. #define MD5_S24 20
  69. #define MD5_S31 4
  70. #define MD5_S32 11
  71. #define MD5_S33 16
  72. #define MD5_S34 23
  73. #define MD5_S41 6
  74. #define MD5_S42 10
  75. #define MD5_S43 15
  76. #define MD5_S44 21
  77. //
  78. // ------------------------------------------------------ Data Type Definitions
  79. //
  80. //
  81. // ----------------------------------------------- Internal Function Prototypes
  82. //
  83. VOID
  84. CypMd5ProcessMessage (
  85. PMD5_CONTEXT Context,
  86. UCHAR Block[MD5_BLOCK_SIZE]
  87. );
  88. VOID
  89. CypMd5PadMessage (
  90. PMD5_CONTEXT Context
  91. );
  92. //
  93. // -------------------------------------------------------------------- Globals
  94. //
  95. //
  96. // ------------------------------------------------------------------ Functions
  97. //
  98. CRYPTO_API
  99. VOID
  100. CyMd5Initialize (
  101. PMD5_CONTEXT Context
  102. )
  103. /*++
  104. Routine Description:
  105. This routine initializes a MD5 context structure, preparing it to accept
  106. and hash data.
  107. Arguments:
  108. Context - Supplies a pointer to the context to initialize.
  109. Return Value:
  110. None.
  111. --*/
  112. {
  113. Context->Length = 0;
  114. Context->State[0] = 0x67452301;
  115. Context->State[1] = 0xEFCDAB89;
  116. Context->State[2] = 0x98BADCFE;
  117. Context->State[3] = 0x10325476;
  118. return;
  119. }
  120. CRYPTO_API
  121. VOID
  122. CyMd5AddContent (
  123. PMD5_CONTEXT Context,
  124. PVOID Message,
  125. UINTN Length
  126. )
  127. /*++
  128. Routine Description:
  129. This routine adds data to a MD5 digest.
  130. Arguments:
  131. Context - Supplies a pointer to the initialized MD5 context.
  132. Message - Supplies a pointer to the buffer containing the bytes.
  133. Length - Supplies the length of the message buffer, in bytes.
  134. Return Value:
  135. None.
  136. --*/
  137. {
  138. PUCHAR Bytes;
  139. UINTN CurrentBlockBytes;
  140. UINTN Index;
  141. UINTN PartialSize;
  142. Bytes = Message;
  143. //
  144. // Figure out how many bytes are already in the block.
  145. //
  146. CurrentBlockBytes = (Context->Length / BITS_PER_BYTE) % MD5_BLOCK_SIZE;
  147. //
  148. // Update the length (in bits).
  149. //
  150. Context->Length += Length * BITS_PER_BYTE;
  151. PartialSize = MD5_BLOCK_SIZE - CurrentBlockBytes;
  152. if (Length >= PartialSize) {
  153. RtlCopyMemory(&(Context->MessageBlock[CurrentBlockBytes]),
  154. Bytes,
  155. PartialSize);
  156. CypMd5ProcessMessage(Context, Context->MessageBlock);
  157. //
  158. // Transform the other complete messages without copying.
  159. //
  160. for (Index = PartialSize;
  161. Index + MD5_BLOCK_SIZE - 1 < Length;
  162. Index += MD5_BLOCK_SIZE) {
  163. CypMd5ProcessMessage(Context, &(Bytes[Index]));
  164. }
  165. CurrentBlockBytes = 0;
  166. } else {
  167. Index = 0;
  168. }
  169. //
  170. // Copy the partial block to the working.
  171. //
  172. ASSERT(Length - Index + CurrentBlockBytes <= MD5_BLOCK_SIZE);
  173. if (Length - Index != 0) {
  174. RtlCopyMemory(&(Context->MessageBlock[CurrentBlockBytes]),
  175. &(Bytes[Index]),
  176. Length - Index);
  177. }
  178. return;
  179. }
  180. CRYPTO_API
  181. VOID
  182. CyMd5GetHash (
  183. PMD5_CONTEXT Context,
  184. UCHAR Hash[MD5_HASH_SIZE]
  185. )
  186. /*++
  187. Routine Description:
  188. This routine computes and returns the final MD5 hash value for the messages
  189. that have been previously entered.
  190. Arguments:
  191. Context - Supplies a pointer to the initialized MD5 context.
  192. Hash - Supplies a pointer where the final hash value will be returned. This
  193. buffer must be MD5_HASH_SIZE length in bytes.
  194. Return Value:
  195. None.
  196. --*/
  197. {
  198. CypMd5PadMessage(Context);
  199. RtlCopyMemory(Hash, Context->State, MD5_HASH_SIZE);
  200. return;
  201. }
  202. //
  203. // --------------------------------------------------------- Internal Functions
  204. //
  205. VOID
  206. CypMd5ProcessMessage (
  207. PMD5_CONTEXT Context,
  208. UCHAR Block[MD5_BLOCK_SIZE]
  209. )
  210. /*++
  211. Routine Description:
  212. This routine processes the 512 bits in the current message block and adds
  213. them to the digest.
  214. Arguments:
  215. Context - Supplies a pointer to the initialized MD5 context with a full
  216. message block.
  217. Block - Supplies a pointer to the block to digest.
  218. Return Value:
  219. None.
  220. --*/
  221. {
  222. ULONG ValueA;
  223. ULONG ValueB;
  224. ULONG ValueC;
  225. ULONG ValueD;
  226. ULONG Working[MD5_HASH_SIZE];
  227. ASSERT(sizeof(Working) == MD5_BLOCK_SIZE);
  228. ValueA = Context->State[0];
  229. ValueB = Context->State[1];
  230. ValueC = Context->State[2];
  231. ValueD = Context->State[3];
  232. RtlCopyMemory(Working, Block, MD5_BLOCK_SIZE);
  233. //
  234. // Perform round 1.
  235. //
  236. MD5_FF(ValueA, ValueB, ValueC, ValueD, Working[0], MD5_S11, 0xD76AA478);
  237. MD5_FF(ValueD, ValueA, ValueB, ValueC, Working[1], MD5_S12, 0xE8C7B756);
  238. MD5_FF(ValueC, ValueD, ValueA, ValueB, Working[2], MD5_S13, 0x242070DB);
  239. MD5_FF(ValueB, ValueC, ValueD, ValueA, Working[3], MD5_S14, 0xC1BDCEEE);
  240. MD5_FF(ValueA, ValueB, ValueC, ValueD, Working[4], MD5_S11, 0xF57C0FAF);
  241. MD5_FF(ValueD, ValueA, ValueB, ValueC, Working[5], MD5_S12, 0x4787C62A);
  242. MD5_FF(ValueC, ValueD, ValueA, ValueB, Working[6], MD5_S13, 0xA8304613);
  243. MD5_FF(ValueB, ValueC, ValueD, ValueA, Working[7], MD5_S14, 0xFD469501);
  244. MD5_FF(ValueA, ValueB, ValueC, ValueD, Working[8], MD5_S11, 0x698098D8);
  245. MD5_FF(ValueD, ValueA, ValueB, ValueC, Working[9], MD5_S12, 0x8B44F7AF);
  246. MD5_FF(ValueC, ValueD, ValueA, ValueB, Working[10], MD5_S13, 0xFFFF5BB1);
  247. MD5_FF(ValueB, ValueC, ValueD, ValueA, Working[11], MD5_S14, 0x895CD7BE);
  248. MD5_FF(ValueA, ValueB, ValueC, ValueD, Working[12], MD5_S11, 0x6B901122);
  249. MD5_FF(ValueD, ValueA, ValueB, ValueC, Working[13], MD5_S12, 0xFD987193);
  250. MD5_FF(ValueC, ValueD, ValueA, ValueB, Working[14], MD5_S13, 0xA679438E);
  251. MD5_FF(ValueB, ValueC, ValueD, ValueA, Working[15], MD5_S14, 0x49B40821);
  252. //
  253. // Perform round 2.
  254. //
  255. MD5_GG(ValueA, ValueB, ValueC, ValueD, Working[1], MD5_S21, 0xF61E2562);
  256. MD5_GG(ValueD, ValueA, ValueB, ValueC, Working[6], MD5_S22, 0xC040B340);
  257. MD5_GG(ValueC, ValueD, ValueA, ValueB, Working[11], MD5_S23, 0x265E5A51);
  258. MD5_GG(ValueB, ValueC, ValueD, ValueA, Working[0], MD5_S24, 0xE9B6C7AA);
  259. MD5_GG(ValueA, ValueB, ValueC, ValueD, Working[5], MD5_S21, 0xD62F105D);
  260. MD5_GG(ValueD, ValueA, ValueB, ValueC, Working[10], MD5_S22, 0x02441453);
  261. MD5_GG(ValueC, ValueD, ValueA, ValueB, Working[15], MD5_S23, 0xD8A1E681);
  262. MD5_GG(ValueB, ValueC, ValueD, ValueA, Working[4], MD5_S24, 0xE7D3FBC8);
  263. MD5_GG(ValueA, ValueB, ValueC, ValueD, Working[9], MD5_S21, 0x21E1CDE6);
  264. MD5_GG(ValueD, ValueA, ValueB, ValueC, Working[14], MD5_S22, 0xC33707D6);
  265. MD5_GG(ValueC, ValueD, ValueA, ValueB, Working[3], MD5_S23, 0xF4D50D87);
  266. MD5_GG(ValueB, ValueC, ValueD, ValueA, Working[8], MD5_S24, 0x455A14ED);
  267. MD5_GG(ValueA, ValueB, ValueC, ValueD, Working[13], MD5_S21, 0xA9E3E905);
  268. MD5_GG(ValueD, ValueA, ValueB, ValueC, Working[2], MD5_S22, 0xFCEFA3F8);
  269. MD5_GG(ValueC, ValueD, ValueA, ValueB, Working[7], MD5_S23, 0x676F02D9);
  270. MD5_GG(ValueB, ValueC, ValueD, ValueA, Working[12], MD5_S24, 0x8D2A4C8A);
  271. //
  272. // Perform round 3.
  273. //
  274. MD5_HH(ValueA, ValueB, ValueC, ValueD, Working[5], MD5_S31, 0xFFFA3942);
  275. MD5_HH(ValueD, ValueA, ValueB, ValueC, Working[8], MD5_S32, 0x8771F681);
  276. MD5_HH(ValueC, ValueD, ValueA, ValueB, Working[11], MD5_S33, 0x6D9D6122);
  277. MD5_HH(ValueB, ValueC, ValueD, ValueA, Working[14], MD5_S34, 0xFDE5380C);
  278. MD5_HH(ValueA, ValueB, ValueC, ValueD, Working[1], MD5_S31, 0xA4BEEA44);
  279. MD5_HH(ValueD, ValueA, ValueB, ValueC, Working[4], MD5_S32, 0x4BDECFA9);
  280. MD5_HH(ValueC, ValueD, ValueA, ValueB, Working[7], MD5_S33, 0xF6BB4B60);
  281. MD5_HH(ValueB, ValueC, ValueD, ValueA, Working[10], MD5_S34, 0xBEBFBC70);
  282. MD5_HH(ValueA, ValueB, ValueC, ValueD, Working[13], MD5_S31, 0x289B7EC6);
  283. MD5_HH(ValueD, ValueA, ValueB, ValueC, Working[0], MD5_S32, 0xEAA127FA);
  284. MD5_HH(ValueC, ValueD, ValueA, ValueB, Working[3], MD5_S33, 0xD4EF3085);
  285. MD5_HH(ValueB, ValueC, ValueD, ValueA, Working[6], MD5_S34, 0x04881D05);
  286. MD5_HH(ValueA, ValueB, ValueC, ValueD, Working[9], MD5_S31, 0xD9D4D039);
  287. MD5_HH(ValueD, ValueA, ValueB, ValueC, Working[12], MD5_S32, 0xE6DB99E5);
  288. MD5_HH(ValueC, ValueD, ValueA, ValueB, Working[15], MD5_S33, 0x1FA27CF8);
  289. MD5_HH(ValueB, ValueC, ValueD, ValueA, Working[2], MD5_S34, 0xC4AC5665);
  290. //
  291. // Perform round 4.
  292. //
  293. MD5_II(ValueA, ValueB, ValueC, ValueD, Working[0], MD5_S41, 0xF4292244);
  294. MD5_II(ValueD, ValueA, ValueB, ValueC, Working[7], MD5_S42, 0x432AFF97);
  295. MD5_II(ValueC, ValueD, ValueA, ValueB, Working[14], MD5_S43, 0xAB9423A7);
  296. MD5_II(ValueB, ValueC, ValueD, ValueA, Working[5], MD5_S44, 0xFC93A039);
  297. MD5_II(ValueA, ValueB, ValueC, ValueD, Working[12], MD5_S41, 0x655B59C3);
  298. MD5_II(ValueD, ValueA, ValueB, ValueC, Working[3], MD5_S42, 0x8F0CCC92);
  299. MD5_II(ValueC, ValueD, ValueA, ValueB, Working[10], MD5_S43, 0xFFEFF47D);
  300. MD5_II(ValueB, ValueC, ValueD, ValueA, Working[1], MD5_S44, 0x85845DD1);
  301. MD5_II(ValueA, ValueB, ValueC, ValueD, Working[8], MD5_S41, 0x6FA87E4F);
  302. MD5_II(ValueD, ValueA, ValueB, ValueC, Working[15], MD5_S42, 0xFE2CE6E0);
  303. MD5_II(ValueC, ValueD, ValueA, ValueB, Working[6], MD5_S43, 0xA3014314);
  304. MD5_II(ValueB, ValueC, ValueD, ValueA, Working[13], MD5_S44, 0x4E0811A1);
  305. MD5_II(ValueA, ValueB, ValueC, ValueD, Working[4], MD5_S41, 0xF7537E82);
  306. MD5_II(ValueD, ValueA, ValueB, ValueC, Working[11], MD5_S42, 0xBD3AF235);
  307. MD5_II(ValueC, ValueD, ValueA, ValueB, Working[2], MD5_S43, 0x2AD7D2BB);
  308. MD5_II(ValueB, ValueC, ValueD, ValueA, Working[9], MD5_S44, 0xEB86D391);
  309. Context->State[0] += ValueA;
  310. Context->State[1] += ValueB;
  311. Context->State[2] += ValueC;
  312. Context->State[3] += ValueD;
  313. return;
  314. }
  315. VOID
  316. CypMd5PadMessage (
  317. PMD5_CONTEXT Context
  318. )
  319. /*++
  320. Routine Description:
  321. This routine pads the message out to an even multiple of 512 bits.
  322. The standard specifies that the first padding bit must be a 1, and then the
  323. last 64 bits represent the length of the original message.
  324. Arguments:
  325. Context - Supplies a pointer to the initialized MD5 context.
  326. Return Value:
  327. None.
  328. --*/
  329. {
  330. UINTN Index;
  331. //
  332. // Check to see if the current message block is too small to hold the
  333. // initial padding bits and length. If so, process the block, then continue
  334. // padding onto a second block.
  335. //
  336. Index = (Context->Length / BITS_PER_BYTE) % MD5_BLOCK_SIZE;
  337. if (Index < 56) {
  338. Context->MessageBlock[Index] = 0x80;
  339. Index += 1;
  340. while (Index < 56) {
  341. Context->MessageBlock[Index] = 0;
  342. Index += 1;
  343. }
  344. } else {
  345. Context->MessageBlock[Index] = 0x80;
  346. Index += 1;
  347. while (Index < sizeof(Context->MessageBlock)) {
  348. Context->MessageBlock[Index] = 0;
  349. Index += 1;
  350. }
  351. CypMd5ProcessMessage(Context, Context->MessageBlock);
  352. RtlZeroMemory(Context->MessageBlock, 56);
  353. }
  354. //
  355. // Store the message length in the last 8 octets.
  356. //
  357. Context->MessageBlock[56] = (UCHAR)(Context->Length);
  358. Context->MessageBlock[57] = (UCHAR)(Context->Length >> 8);
  359. Context->MessageBlock[58] = (UCHAR)(Context->Length >> 16);
  360. Context->MessageBlock[59] = (UCHAR)(Context->Length >> 24);
  361. Context->MessageBlock[60] = (UCHAR)(Context->Length >> 32);
  362. Context->MessageBlock[61] = (UCHAR)(Context->Length >> (32 + 8));
  363. Context->MessageBlock[62] = (UCHAR)(Context->Length >> (32 + 16));
  364. Context->MessageBlock[63] = (UCHAR)(Context->Length >> (32 + 24));
  365. CypMd5ProcessMessage(Context, Context->MessageBlock);
  366. return;
  367. }