FreeRDP
Loading...
Searching...
No Matches
hmac_md5.c
1#include <assert.h>
2#include <string.h>
3
4#include "hmac_md5.h"
5#include "md5.h"
6
7void hmac_md5_init(WINPR_HMAC_MD5_CTX* ctx, const unsigned char* key, size_t key_len)
8{
9 const WINPR_HMAC_MD5_CTX empty = { 0 };
10 unsigned char k_ipad[KEY_IOPAD_SIZE] = { 0 };
11 unsigned char k_opad[KEY_IOPAD_SIZE] = { 0 };
12
13 assert(ctx);
14 *ctx = empty;
15
16 if (key_len <= KEY_IOPAD_SIZE)
17 {
18 memcpy(k_ipad, key, key_len);
19 memcpy(k_opad, key, key_len);
20 }
21 else
22 {
23 WINPR_MD5_CTX lctx = { 0 };
24
25 winpr_MD5_Init(&lctx);
26 winpr_MD5_Update(&lctx, key, key_len);
27 winpr_MD5_Final(k_ipad, &lctx);
28 memcpy(k_opad, k_ipad, KEY_IOPAD_SIZE);
29 }
30 for (size_t i = 0; i < KEY_IOPAD_SIZE; i++)
31 {
32 k_ipad[i] ^= 0x36;
33 k_opad[i] ^= 0x5c;
34 }
35
36 winpr_MD5_Init(&ctx->icontext);
37 winpr_MD5_Update(&ctx->icontext, k_ipad, KEY_IOPAD_SIZE);
38
39 winpr_MD5_Init(&ctx->ocontext);
40 winpr_MD5_Update(&ctx->ocontext, k_opad, KEY_IOPAD_SIZE);
41}
42
43void hmac_md5_update(WINPR_HMAC_MD5_CTX* ctx, const unsigned char* text, size_t text_len)
44{
45 assert(ctx);
46 winpr_MD5_Update(&ctx->icontext, text, text_len);
47}
48
49void hmac_md5_finalize(WINPR_HMAC_MD5_CTX* ctx, unsigned char* hmac)
50{
51 assert(ctx);
52 winpr_MD5_Final(hmac, &ctx->icontext);
53
54 winpr_MD5_Update(&ctx->ocontext, hmac, 16);
55 winpr_MD5_Final(hmac, &ctx->ocontext);
56}