31
31
#include "../tools.h"
33
33
/** Creates a new Alpha N association
35
35
* @param[in] id the id of the association (should be unique ranging from 0-255).
37
37
* @return the new association
39
39
struct association_relay *association_relay_new_alpha_n_ass(const unsigned int id) {
40
struct association_relay *ass = xmalloc(sizeof(struct association_relay));
40
struct association_relay *ass = xmalloc(sizeof(struct association_relay));
47
47
//! Frees a given ALpha N Association
49
* The association is freed if a not null pointer is passed.
50
* If the pointer is a null pointer no operation is performed.
52
* @note This function should be called by association_relay_free() only
49
* The association is freed if a not null pointer is passed.
50
* If the pointer is a null pointer no operation is performed.
52
* @note This function should be called by association_relay_free() only
53
53
* @param[in] ass the association to be freed
55
55
void association_relay_free_alpha_n_ass(struct association_relay *ass) {
57
print_error("can't free NULL association_relay");
61
assert(ass->mode == ALPHA_N);
57
print_error("can't free NULL association_relay");
61
assert(ass->mode == ALPHA_N);
66
66
//! Handles an incoming Alpha N packet
68
* Handles a given S1 packet. Hash chain checks are performed in
69
* association_relay_handle_s1(). This function handles only the
68
* Handles a given S1 packet. Hash chain checks are performed in
69
* association_relay_handle_s1(). This function handles only the
70
70
* special functions for Alpha n, i.e. buffering received HMACs.
72
72
* @param[in,out] ass The association on that the packet came in
73
73
* @param[in] packet the incoming packet
74
74
* @param[in] packet_len the size of the packet (including the header)
76
76
void association_relay_handle_s1_alpha_n(struct association_relay *const ass, struct alpha_packet const *const packet, unsigned int const packet_len) {
78
assert(ass->mode == ALPHA_N);
79
assert(packet != NULL);
80
assert(packet->type == PACKET_S1);
81
assert(packet_len > 0);
78
assert(ass->mode == ALPHA_N);
79
assert(packet != NULL);
80
assert(packet->type == PACKET_S1);
81
assert(packet_len > 0);
84
ass->alpha_n.hmac = packet->s1.common.presigs[0];
84
ass->alpha_n.hmac = packet->s1.common.presigs[0];
87
87
//! Verifies a given Alpha S2 packet
89
89
* Verifies a given Alpha S2 packet with the stored HMAC in the given Alpha N association.
91
91
* @param[in] ass the association on that the packet came in
92
92
* @param[in] packet the incoming packet
93
93
* @param[in] packet_len the size of the incoming packet (including the header)
94
94
* @param[out] valid if the packet is valid (checked with the given HMAC)
96
96
void association_relay_verify_alpha_n(struct association_relay *const ass, struct alpha_packet const *const packet, unsigned int const packet_len, bool *const valid) {
98
assert(ass->mode == ALPHA_N);
99
assert(packet != NULL);
100
assert(packet->type == PACKET_S2);
101
enum ap_relay_validation_code ret_val = 0;
105
ret_val = association_relay_verify_anchor_s2(ass, &packet->s2.common.anchor);
107
if(ret_val == AP_REL_VALID) {
108
digest_t computed_hmac;
109
create_hmac(packet->s2.common.payload, packet_len - packet_size(s2.common), &packet->s2.common.anchor, &computed_hmac);
110
*valid = (digestcmp(&ass->alpha_n.hmac, &computed_hmac) == 0);
111
} else if(ret_val == AP_REL_INVALID) {
98
assert(ass->mode == ALPHA_N);
99
assert(packet != NULL);
100
assert(packet->type == PACKET_S2);
101
enum ap_relay_validation_code ret_val = 0;
105
ret_val = association_relay_verify_anchor_s2(ass, &packet->s2.common.anchor);
107
if (ret_val == AP_REL_VALID) {
108
digest_t computed_hmac;
109
create_hmac(packet->s2.common.payload, packet_len - packet_size(s2.common), &packet->s2.common.anchor, &computed_hmac);
110
*valid = (digestcmp(&ass->alpha_n.hmac, &computed_hmac) == 0);
111
} else if (ret_val == AP_REL_INVALID) {