Commit 468a1db7 authored by Karel Slaný's avatar Karel Slaný

Removed in/out buffer size parameters from cookie API.

parent 6d1572bd
......@@ -49,22 +49,23 @@ static inline void update_hash(Fnv64_t *hash_val, const struct sockaddr *sa)
*
* \note At least one input address must be provided.
*
* \param[in] input Input parameters.
* \param[in] cc_out Buffer for computed client cookie.
* \param[in,out] cc_len Size of buffer/written data.
* \param input Input parameters.
* \param cc_out Buffer for computed client cookie.
* \param cc_len Buffer size.
*
* \return KNOT_EOK on success, error code else.
* \retval non-zero size of written data on successful return
* \retval 0 on error
*/
static int cc_gen_fnv64(const struct knot_cc_input *input,
uint8_t *cc_out, uint16_t *cc_len)
static uint16_t cc_gen_fnv64(const struct knot_cc_input *input,
uint8_t *cc_out, uint16_t cc_len)
{
if (!input || !cc_out || !cc_len || *cc_len < KNOT_OPT_COOKIE_CLNT) {
return KNOT_EINVAL;
if (!input || !cc_out || cc_len < KNOT_OPT_COOKIE_CLNT) {
return 0;
}
if ((!input->clnt_sockaddr && !input->srvr_sockaddr) ||
!(input->secret_data && input->secret_len)) {
return KNOT_EINVAL;
return 0;
}
Fnv64_t hash_val = FNV1A_64_INIT;
......@@ -82,10 +83,10 @@ static int cc_gen_fnv64(const struct knot_cc_input *input,
assert(KNOT_OPT_COOKIE_CLNT == sizeof(hash_val));
*cc_len = KNOT_OPT_COOKIE_CLNT;
memcpy(cc_out, &hash_val, *cc_len);
cc_len = sizeof(hash_val);
memcpy(cc_out, &hash_val, cc_len);
return KNOT_EOK;
return cc_len;
}
#define SRVR_FNV64_HASH_SIZE 8
......@@ -97,23 +98,23 @@ static int cc_gen_fnv64(const struct knot_cc_input *input,
*
* \note This function computes only the hash value.
*
* \param[in] input Data to compute cookie from.
* \param[in] hash_out Buffer to write the resulting hash data into.
* \param[in,out] hash_len On input set to hash buffer size. On successful
* return contains size of written hash.
* \param input Data to compute cookie from.
* \param hash_out Buffer to write the resulting hash data into.
* \param hash_len Buffer size.
*
* \return KNOT_EOK or error code.
* \retval non-zero size of written data on successful return
* \retval 0 on error
*/
static int sc_gen_fnv64(const struct knot_sc_input *input,
uint8_t *hash_out, uint16_t *hash_len)
static uint16_t sc_gen_fnv64(const struct knot_sc_input *input,
uint8_t *hash_out, uint16_t hash_len)
{
if (!input || !hash_out || !hash_len || (*hash_len < SRVR_FNV64_HASH_SIZE)) {
return KNOT_EINVAL;
if (!input || !hash_out || hash_len < SRVR_FNV64_HASH_SIZE) {
return 0;
}
if (!input->cc || !input->cc_len || !input->srvr_data ||
!input->srvr_data->secret_data || !input->srvr_data->secret_len) {
return KNOT_EINVAL;
return 0;
}
Fnv64_t hash_val = FNV1A_64_INIT;
......@@ -131,11 +132,12 @@ static int sc_gen_fnv64(const struct knot_sc_input *input,
hash_val = fnv_64a_buf((void *)input->srvr_data->secret_data,
input->srvr_data->secret_len, hash_val);
*hash_len = sizeof(hash_val);
memcpy(hash_out, &hash_val, *hash_len);
assert(SRVR_FNV64_HASH_SIZE == *hash_len);
assert(SRVR_FNV64_HASH_SIZE == sizeof(hash_val));
return KNOT_EOK;
hash_len = sizeof(hash_val);
memcpy(hash_out, &hash_val, hash_len);
return hash_len;
}
_public_
......
......@@ -26,7 +26,7 @@ int knot_cc_check(const uint8_t *cc, uint16_t cc_len,
const struct knot_cc_input *input,
const struct knot_cc_alg *cc_alg)
{
if (!cc || !cc_len || !input ||
if (!cc || cc_len == 0 || !input ||
!cc_alg || !cc_alg->cc_size || !cc_alg->gen_func) {
return KNOT_EINVAL;
}
......@@ -38,16 +38,12 @@ int knot_cc_check(const uint8_t *cc, uint16_t cc_len,
uint8_t generated_cc[KNOT_OPT_COOKIE_CLNT] = { 0 };
uint16_t generated_cc_len = KNOT_OPT_COOKIE_CLNT;
int ret = cc_alg->gen_func(input, generated_cc, &generated_cc_len);
if (ret != KNOT_EOK) {
return ret;
}
generated_cc_len = cc_alg->gen_func(input, generated_cc, generated_cc_len);
if (generated_cc_len != cc_len) {
return KNOT_EINVAL;
}
ret = memcmp(cc, generated_cc, generated_cc_len);
int ret = memcmp(cc, generated_cc, generated_cc_len);
if (ret != 0) {
return KNOT_EINVAL;
}
......
......@@ -32,16 +32,15 @@ struct knot_cc_input {
/*!
* \brief Client cookie generator function type.
*
* \param[in] input Data which to generate the cookie from.
* \param[in] cc_out Buffer to write the resulting client cookie data into.
* \param[in,out] cc_len On input set to cookie buffer size. On successful
* return contains size of client cookie.
* \param input Data which to generate the cookie from.
* \param cc_out Buffer to write the resulting client cookie data into.
* \param cc_len Cookie buffer size.
*
* \retval KNOT_EOK
* \retval KNOT_EINVAL
* \retval non-zero size of written data on successful return
* \retval 0 on error
*/
typedef int (knot_cc_gen_t)(const struct knot_cc_input *input,
uint8_t *cc_out, uint16_t *cc_len);
typedef uint16_t (knot_cc_gen_t)(const struct knot_cc_input *input,
uint8_t *cc_out, uint16_t cc_len);
/*!
* \brief Holds description of the client cookie algorithm.
......
......@@ -92,9 +92,9 @@ int knot_sc_check(uint16_t nonce_len, const struct knot_dns_cookies *cookies,
};
/* Generate a new hash. */
ret = sc_alg->hash_func(&sc_input, generated_hash, &generated_hash_len);
if (ret != KNOT_EOK) {
return ret;
generated_hash_len = sc_alg->hash_func(&sc_input, generated_hash, generated_hash_len);
if (generated_hash_len == 0) {
return KNOT_EINVAL;
}
/* Compare hashes. */
......
......@@ -90,16 +90,15 @@ int knot_sc_parse(uint16_t nonce_len, const uint8_t *sc, uint16_t sc_len,
* data prefixed before the actual hash value. Nonce data must be written
* by an external function into the server cookie.
*
* \param[in] input Data which to generate the cookie from.
* \param[in] hash_out Buffer to write the resulting hash data into.
* \param[in,out] hash_len On input set to hash buffer size. On successful
* return contains size of written hash.
* \param input Data which to generate the cookie from.
* \param hash_out Buffer to write the resulting hash data into.
* \param hash_len Hash buffer size.
*
* \retval KNOT_EOK
* \retval KNOT_EINVAL
* \retval non-zero size of written data on successful return
* \retval 0 on error
*/
typedef int (knot_sc_hash_t)(const struct knot_sc_input *input,
uint8_t *hash_out, uint16_t *hash_len);
typedef uint16_t (knot_sc_hash_t)(const struct knot_sc_input *input,
uint8_t *hash_out, uint16_t hash_len);
/*!
* \brief Holds description of the server cookie algorithm.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment