/* ======================================================================== Routine Description: HMAC using MD5 hash function Arguments: key Secret key key_len The length of the key in bytes message Message context message_len The length of message in bytes macLen Request the length of message authentication code Return Value: mac Message authentication code Note: None ======================================================================== */ void RT_HMAC_MD5( IN const u8 Key[], IN UINT KeyLen, IN const u8 Message[], IN UINT MessageLen, OUT u8 MAC[], IN UINT MACLen) { MD5_CTX_STRUC md5_ctx1; MD5_CTX_STRUC md5_ctx2; u8 K0[MD5_BLOCK_SIZE]; u8 Digest[MD5_DIGEST_SIZE]; UINT index; memset(&md5_ctx1, 0, sizeof(MD5_CTX_STRUC)); memset(&md5_ctx2, 0, sizeof(MD5_CTX_STRUC)); /* * If the length of K = B(Block size): K0 = K. * If the length of K > B: hash K to obtain an L byte string, * then append (B-L) zeros to create a B-byte string K0 (i.e., K0 = H(K) || 00...00). * If the length of K < B: append zeros to the end of K to create a B-byte string K0 */ memset(K0, 0, MD5_BLOCK_SIZE); if (KeyLen <= MD5_BLOCK_SIZE) { memmove(K0, Key, KeyLen); } else { RT_MD5(Key, KeyLen, K0); } /* Exclusive-Or K0 with ipad */ /* ipad: Inner pad; the byte x・36・ repeated B times. */ for (index = 0; index < MD5_BLOCK_SIZE; index++) K0[index] ^= 0x36; /* End of for */ RT_MD5_Init(&md5_ctx1); /* H(K0^ipad) */ RT_MD5_Append(&md5_ctx1, K0, sizeof(K0)); /* H((K0^ipad)||text) */ RT_MD5_Append(&md5_ctx1, Message, MessageLen); RT_MD5_End(&md5_ctx1, Digest); /* Exclusive-Or K0 with opad and remove ipad */ /* opad: Outer pad; the byte x・5c・ repeated B times. */ for (index = 0; index < MD5_BLOCK_SIZE; index++) K0[index] ^= 0x36^0x5c; /* End of for */ RT_MD5_Init(&md5_ctx2); /* H(K0^opad) */ RT_MD5_Append(&md5_ctx2, K0, sizeof(K0)); /* H( (K0^opad) || H((K0^ipad)||text) ) */ RT_MD5_Append(&md5_ctx2, Digest, MD5_DIGEST_SIZE); RT_MD5_End(&md5_ctx2, Digest); if (MACLen > MD5_DIGEST_SIZE) memmove(MAC, Digest, MD5_DIGEST_SIZE); else memmove(MAC, Digest, MACLen); } /* End of RT_HMAC_SHA256 */
/* ======================================================================== Routine Description: MD5 algorithm Arguments: message Message context messageLen The length of message in bytes Return Value: digestMessage Digest message Note: None ======================================================================== */ VOID RT_MD5 ( IN const UINT8 Message[], IN UINT MessageLen, OUT UINT8 DigestMessage[]) { MD5_CTX_STRUC md5_ctx; NdisZeroMemory(&md5_ctx, sizeof(MD5_CTX_STRUC)); RT_MD5_Init(&md5_ctx); RT_MD5_Append(&md5_ctx, Message, MessageLen); RT_MD5_End(&md5_ctx, DigestMessage); } /* End of RT_MD5 */