summaryrefslogtreecommitdiffstats
path: root/lib/cookies/alg_sha.c
blob: 34e79c38520fc478043274a2731797989703c8d8 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*  Copyright (C) CZ.NIC, z.s.p.o. <knot-resolver@labs.nic.cz>
 *  SPDX-License-Identifier: GPL-3.0-or-later
 */

#include <nettle/hmac.h>
#include <stdint.h>
#include <stdlib.h>

#include <libknot/errcode.h>
#include <libknot/rrtype/opt-cookie.h>

#include "lib/cookies/alg_sha.h"
#include "lib/utils.h"

/**
 * @brief Update hash value.
 *
 * @param ctx HMAC-SHA256 context to be updated.
 * @param sa  Socket address.
 */
static inline void update_hash(struct hmac_sha256_ctx *ctx,
                               const struct sockaddr *sa)
{
	if (kr_fails_assert(ctx && sa))
		return;

	int addr_len = kr_inaddr_len(sa);
	const uint8_t *addr = (uint8_t *)kr_inaddr(sa);

	if (addr && addr_len > 0) {
		hmac_sha256_update(ctx, addr_len, addr);
	}
}

/**
 * @brief Compute client cookie using HMAC-SHA256-64.
 * @note At least one of the arguments must be non-null.
 * @param input  input parameters
 * @param cc_out buffer for computed client cookie
 * @param cc_len buffer size
 * @return Non-zero size of written data on success, 0 in case of a failure.
 */
static uint16_t cc_gen_hmac_sha256_64(const struct knot_cc_input *input,
                                      uint8_t *cc_out, uint16_t cc_len)
{
	if (!knot_cc_input_is_valid(input) ||
	    !cc_out || cc_len < KNOT_OPT_COOKIE_CLNT) {
		return 0;
	}

	struct hmac_sha256_ctx ctx;
	hmac_sha256_set_key(&ctx, input->secret_len, input->secret_data);

	if (input->clnt_sockaddr) {
		update_hash(&ctx, input->clnt_sockaddr);
	}

	if (input->srvr_sockaddr) {
		update_hash(&ctx, input->srvr_sockaddr);
	}

	/* KNOT_OPT_COOKIE_CLNT <= SHA256_DIGEST_SIZE */

	hmac_sha256_digest(&ctx, KNOT_OPT_COOKIE_CLNT, cc_out);

	return KNOT_OPT_COOKIE_CLNT;
}

#define SRVR_HMAC_SHA256_64_HASH_SIZE 8

/**
 * @brief Compute server cookie hash using HMAC-SHA256-64).
 * @note Server cookie = nonce | time | HMAC-SHA256-64( server secret, client cookie | nonce| time | client IP )
 * @param input    data to compute cookie from
 * @param hash_out hash output buffer
 * @param hash_len buffer size
 * @return Non-zero size of written data on success, 0 in case of a failure.
 */
static uint16_t sc_gen_hmac_sha256_64(const struct knot_sc_input *input,
                                      uint8_t *hash_out, uint16_t hash_len)
{
	if (!knot_sc_input_is_valid(input) ||
	    !hash_out || hash_len < SRVR_HMAC_SHA256_64_HASH_SIZE) {
		return 0;
	}

	struct hmac_sha256_ctx ctx;
	hmac_sha256_set_key(&ctx, input->srvr_data->secret_len,
	                    input->srvr_data->secret_data);

	hmac_sha256_update(&ctx, input->cc_len, input->cc);

	if (input->nonce && input->nonce_len) {
		hmac_sha256_update(&ctx, input->nonce_len, input->nonce);
	}

	if (input->srvr_data->clnt_sockaddr) {
		update_hash(&ctx, input->srvr_data->clnt_sockaddr);
	}

	/* SRVR_HMAC_SHA256_64_HASH_SIZE < SHA256_DIGEST_SIZE */

	hmac_sha256_digest(&ctx, SRVR_HMAC_SHA256_64_HASH_SIZE, hash_out);

	return SRVR_HMAC_SHA256_64_HASH_SIZE;
}

const struct knot_cc_alg knot_cc_alg_hmac_sha256_64 = { KNOT_OPT_COOKIE_CLNT, cc_gen_hmac_sha256_64 };

const struct knot_sc_alg knot_sc_alg_hmac_sha256_64 = { SRVR_HMAC_SHA256_64_HASH_SIZE, sc_gen_hmac_sha256_64 };