summaryrefslogtreecommitdiffstats
path: root/src/include/ipxe/hmac.h
blob: cf9d086777fdecac2c6bfab53d054a97ad7f27dc (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef _IPXE_HMAC_H
#define _IPXE_HMAC_H

/** @file
 *
 * Keyed-Hashing for Message Authentication
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <ipxe/crypto.h>

/** HMAC context type */
#define hmac_context_t( digest ) struct {				\
		/** Digest context */					\
		uint8_t ctx[ digest->ctxsize ];				\
		/** HMAC input/output padding */			\
		uint8_t pad[ digest->blocksize ];			\
	} __attribute__ (( packed ))

/**
 * Calculate HMAC context size
 *
 * @v digest		Digest algorithm to use
 * @ret len		HMAC context size
 */
static inline __attribute__ (( always_inline )) size_t
hmac_ctxsize ( struct digest_algorithm *digest ) {
	hmac_context_t ( digest ) *hctx;

	return sizeof ( *hctx );
}

/**
 * Update HMAC
 *
 * @v digest		Digest algorithm to use
 * @v ctx		HMAC context
 * @v data		Data
 * @v len		Length of data
 */
static inline void hmac_update ( struct digest_algorithm *digest, void *ctx,
				 const void *data, size_t len ) {
	hmac_context_t ( digest ) *hctx = ctx;

	digest_update ( digest, hctx->ctx, data, len );
}

extern void hmac_init ( struct digest_algorithm *digest, void *ctx,
			const void *key, size_t key_len );
extern void hmac_final ( struct digest_algorithm *digest, void *ctx,
			 void *hmac );

#endif /* _IPXE_HMAC_H */