alg_sha.c 3.69 KB
Newer Older
1
/*  Copyright (C) 2016-2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 3 4 5 6 7 8 9 10 11 12 13

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
14
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
15 16 17
 */

#include <assert.h>
18
#include <nettle/hmac.h>
19 20 21 22 23 24 25 26 27 28
#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"

/**
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 * @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)
{
	assert(ctx && sa);

	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);
	}
}

/**
48
 * @brief Compute client cookie using HMAC-SHA256-64.
49
 * @note At least one of the arguments must be non-null.
50 51 52 53
 * @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.
54
 */
55 56
static uint16_t cc_gen_hmac_sha256_64(const struct knot_cc_input *input,
                                      uint8_t *cc_out, uint16_t cc_len)
57
{
58 59
	if (!knot_cc_input_is_valid(input) ||
	    !cc_out || cc_len < KNOT_OPT_COOKIE_CLNT) {
60
		return 0;
61 62
	}

63 64
	struct hmac_sha256_ctx ctx;
	hmac_sha256_set_key(&ctx, input->secret_len, input->secret_data);
65 66

	if (input->clnt_sockaddr) {
67
		update_hash(&ctx, input->clnt_sockaddr);
68 69 70
	}

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

Karel Slaný's avatar
Karel Slaný committed
74
	/* KNOT_OPT_COOKIE_CLNT <= SHA256_DIGEST_SIZE */
75

Karel Slaný's avatar
Karel Slaný committed
76
	hmac_sha256_digest(&ctx, KNOT_OPT_COOKIE_CLNT, cc_out);
77

Karel Slaný's avatar
Karel Slaný committed
78
	return KNOT_OPT_COOKIE_CLNT;
79 80 81 82 83
}

#define SRVR_HMAC_SHA256_64_HASH_SIZE 8

/**
84
 * @brief Compute server cookie hash using HMAC-SHA256-64).
85 86
 * @note Server cookie = nonce | time | HMAC-SHA256-64( server secret, client cookie | nonce| time | client IP )
 * @param input    data to compute cookie from
87 88 89
 * @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.
90
 */
91 92
static uint16_t sc_gen_hmac_sha256_64(const struct knot_sc_input *input,
                                      uint8_t *hash_out, uint16_t hash_len)
93
{
94 95
	if (!knot_sc_input_is_valid(input) ||
	    !hash_out || hash_len < SRVR_HMAC_SHA256_64_HASH_SIZE) {
96
		return 0;
97 98
	}

99 100 101
	struct hmac_sha256_ctx ctx;
	hmac_sha256_set_key(&ctx, input->srvr_data->secret_len,
	                    input->srvr_data->secret_data);
102

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

	if (input->nonce && input->nonce_len) {
106
		hmac_sha256_update(&ctx, input->nonce_len, input->nonce);
107 108 109
	}

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

Karel Slaný's avatar
Karel Slaný committed
113
	/* SRVR_HMAC_SHA256_64_HASH_SIZE < SHA256_DIGEST_SIZE */
114

Karel Slaný's avatar
Karel Slaný committed
115
	hmac_sha256_digest(&ctx, SRVR_HMAC_SHA256_64_HASH_SIZE, hash_out);
116

Karel Slaný's avatar
Karel Slaný committed
117
	return SRVR_HMAC_SHA256_64_HASH_SIZE;
118 119 120 121 122
}

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 };