aboutsummaryrefslogtreecommitdiffstats
path: root/hmac_digest.c
blob: 464710107951d3241c10281d62f3609e1e270cdb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* See LICENSE file for copyright and license details. */
#include "common.h"


/**
 * Feed data into the HMAC algorithm and
 * get the result
 * 
 * The state of the algorithm will be reset and
 * `libsha1_hmac_update` and `libsha1_hmac_update`
 * can be called again
 * 
 * @param  state   The state of the algorithm
 * @param  data    Data to feed into the algorithm
 * @param  n       The number of bytes to feed into the algorithm
 * @param  output  The output buffer for the hash, it will be as
 *                 large as for the underlaying hash algorithm
 */
void
libsha1_hmac_digest(struct libsha1_hmac_state *restrict state, const void *data, size_t n, void *output)
{
	if (!state->inited) {
		libsha1_init(&state->sha1_state, state->sha1_state.algorithm);
		libsha1_update(&state->sha1_state, state->ipad, sizeof(state->sha1_state.chunk) * 8);
	}

	libsha1_digest(&state->sha1_state, data, n, output);
	libsha1_init(&state->sha1_state, state->sha1_state.algorithm);

	libsha1_update(&state->sha1_state, state->opad, sizeof(state->sha1_state.chunk) * 8);
	libsha1_digest(&state->sha1_state, output, state->outsize, output);
	state->inited = 0;
}