8 HMAC_CTX_init(HMAC_CTX
*ctx
)
10 memset(ctx
, 0, sizeof(*ctx
));
14 HMAC_CTX_cleanup(HMAC_CTX
*ctx
)
17 memset(ctx
->buf
, 0, ctx
->key_length
);
22 memset(ctx
->ipad
, 0, ctx
->key_length
);
27 memset(ctx
->ipad
, 0, ctx
->key_length
);
32 EVP_MD_CTX_destroy(ctx
->ctx
);
38 HMAC_size(const HMAC_CTX
*ctx
)
40 return EVP_MD_size(ctx
->md
);
44 HMAC_Init_ex(HMAC_CTX
*ctx
,
56 memset(ctx
->buf
, 0, ctx
->key_length
);
59 ctx
->key_length
= EVP_MD_size(ctx
->md
);
60 ctx
->buf
= malloc(ctx
->key_length
);
66 if (keylen
> EVP_MD_block_size(ctx
->md
)) {
67 EVP_Digest(key
, keylen
, ctx
->buf
, NULL
, ctx
->md
, engine
);
69 keylen
= EVP_MD_size(ctx
->md
);
73 memset(ctx
->opad
, 0, ctx
->key_length
);
77 memset(ctx
->ipad
, 0, ctx
->key_length
);
81 ctx
->opad
= malloc(EVP_MD_block_size(ctx
->md
));
82 ctx
->ipad
= malloc(EVP_MD_block_size(ctx
->md
));
83 memset(ctx
->ipad
, 0x36, EVP_MD_block_size(ctx
->md
));
84 memset(ctx
->opad
, 0x5c, EVP_MD_block_size(ctx
->md
));
86 for (i
= 0, p
= ctx
->ipad
; i
< keylen
; i
++)
87 p
[i
] ^= ((const unsigned char *)key
)[i
];
88 for (i
= 0, p
= ctx
->opad
; i
< keylen
; i
++)
89 p
[i
] ^= ((const unsigned char *)key
)[i
];
91 ctx
->ctx
= EVP_MD_CTX_create();
93 EVP_DigestInit_ex(ctx
->ctx
, ctx
->md
, ctx
->engine
);
94 EVP_DigestUpdate(ctx
->ctx
, ctx
->ipad
, EVP_MD_block_size(ctx
->md
));
98 HMAC_Update(HMAC_CTX
*ctx
, const void *data
, size_t len
)
100 EVP_DigestUpdate(ctx
->ctx
, data
, len
);
104 HMAC_Final(HMAC_CTX
*ctx
, void *md
, unsigned int *len
)
106 EVP_DigestFinal_ex(ctx
->ctx
, ctx
->buf
, NULL
);
108 EVP_DigestInit_ex(ctx
->ctx
, ctx
->md
, ctx
->engine
);
109 EVP_DigestUpdate(ctx
->ctx
, ctx
->opad
, EVP_MD_block_size(ctx
->md
));
110 EVP_DigestUpdate(ctx
->ctx
, ctx
->buf
, ctx
->key_length
);
111 EVP_DigestFinal_ex(ctx
->ctx
, md
, len
);
115 HMAC(const EVP_MD
*md
,
116 const void *key
, size_t key_size
,
117 const void *data
, size_t data_size
,
118 void *hash
, unsigned int *hash_len
)
123 HMAC_Init_ex(&ctx
, key
, key_size
, md
, NULL
);
124 HMAC_Update(&ctx
, data
, data_size
);
125 HMAC_Final(&ctx
, hash
, hash_len
);
126 HMAC_CTX_cleanup(&ctx
);