~christoph-rackwitz/alpha/alpha

« back to all changes in this revision

Viewing changes to src/filter/association_relay_alpha_n.c

  • Committer: rackwitz
  • Date: 2012-04-05 13:25:20 UTC
  • Revision ID: svn-v4:e53e26d6-f796-8e42-a7b4-c8c1a20d4d3d::1538
uncrustify src/filter/*.c

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#include "../tools.h"
32
32
 
33
33
/** Creates a new Alpha N association
34
 
 * 
 
34
 *
35
35
 * @param[in] id the id of the association (should be unique ranging from 0-255).
36
36
 *
37
37
 * @return the new association
38
38
 */
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));
41
 
        ass->id = id;
42
 
        ass->mode = ALPHA_N;
 
40
    struct association_relay *ass = xmalloc(sizeof(struct association_relay));
 
41
    ass->id   = id;
 
42
    ass->mode = ALPHA_N;
43
43
 
44
 
        return ass;
 
44
    return ass;
45
45
}
46
46
 
47
47
//! Frees a given ALpha N Association
48
48
/*!
49
 
 * The association is freed if a not null pointer is passed. 
50
 
 * If the pointer is a null pointer no operation is performed. 
51
 
 *      
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.
 
51
 *
 
52
 * @note This function should be called by association_relay_free() only
53
53
 * @param[in] ass the association to be freed
54
 
*/
 
54
 */
55
55
void association_relay_free_alpha_n_ass(struct association_relay *ass) {
56
 
        if (ass == NULL) {
57
 
                print_error("can't free NULL association_relay");
58
 
                exit(EXIT_FAILURE);
59
 
        }
60
 
 
61
 
        assert(ass->mode == ALPHA_N);
62
 
 
63
 
        xfree(ass);
 
56
    if (ass == NULL) {
 
57
        print_error("can't free NULL association_relay");
 
58
        exit(EXIT_FAILURE);
 
59
    }
 
60
 
 
61
    assert(ass->mode == ALPHA_N);
 
62
 
 
63
    xfree(ass);
64
64
}
65
65
 
66
66
//! Handles an incoming Alpha N packet
67
67
/*!
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.
71
 
 *      
 
71
 *
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)
75
75
 */
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) {
77
 
        assert(ass != NULL);
78
 
        assert(ass->mode == ALPHA_N);
79
 
        assert(packet != NULL);
80
 
        assert(packet->type == PACKET_S1);
81
 
        assert(packet_len > 0);
 
77
    assert(ass != NULL);
 
78
    assert(ass->mode == ALPHA_N);
 
79
    assert(packet != NULL);
 
80
    assert(packet->type == PACKET_S1);
 
81
    assert(packet_len > 0);
82
82
 
83
 
        //just store the hmac
84
 
        ass->alpha_n.hmac = packet->s1.common.presigs[0];
 
83
    //just store the hmac
 
84
    ass->alpha_n.hmac = packet->s1.common.presigs[0];
85
85
}
86
86
 
87
87
//! Verifies a given Alpha S2 packet
88
88
/*!
89
89
 * Verifies a given Alpha S2 packet with the stored HMAC in the given Alpha N association.
90
 
 *      
 
90
 *
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)
95
95
 */
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) {
97
 
        assert(ass != NULL);
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;
102
 
 
103
 
        *valid = false;
104
 
 
105
 
        ret_val = association_relay_verify_anchor_s2(ass, &packet->s2.common.anchor);
106
 
 
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) {
112
 
                *valid = false;
113
 
        }
114
 
 
 
97
    assert(ass != NULL);
 
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;
 
102
 
 
103
    *valid = false;
 
104
 
 
105
    ret_val = association_relay_verify_anchor_s2(ass, &packet->s2.common.anchor);
 
106
 
 
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) {
 
112
        *valid = false;
 
113
    }
115
114
}