~christoph-rackwitz/alpha/alpha

« back to all changes in this revision

Viewing changes to src/filter/association_relay_alpha_c.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:
37
37
 *
38
38
 * Allocates and initializes an Alpha C association with default values
39
39
 * and creates necessary default structures.
40
 
 *      
 
40
 *
41
41
 * @param[in] id the identifier of this association (should be unique as this is the position in the relay array)
42
42
 *
43
43
 * @return the new association
44
44
 */
45
45
struct association_relay *association_relay_new_alpha_c_ass(const unsigned int id) {
46
 
        struct association_relay *ass = xmalloc(sizeof(struct association_relay));
47
 
        ass->id = id;
48
 
        ass->mode = ALPHA_C;
49
 
 
50
 
        ap_presig_store_init(&ass->alpha_c.pstore);
51
 
 
52
 
        return ass;
 
46
    struct association_relay *ass = xmalloc(sizeof(struct association_relay));
 
47
    ass->id   = id;
 
48
    ass->mode = ALPHA_C;
 
49
 
 
50
    ap_presig_store_init(&ass->alpha_c.pstore);
 
51
 
 
52
    return ass;
53
53
}
54
54
 
55
55
//! Frees a given ALpha C Association
56
56
/*!
57
 
 * The association is freed if a not null pointer is passed. 
58
 
 * If the pointer is a null pointer no operation is performed. 
59
 
 *      
 
57
 * The association is freed if a not null pointer is passed.
 
58
 * If the pointer is a null pointer no operation is performed.
 
59
 *
60
60
 * @note This function should be called by association_relay_free() only
61
61
 * @param[in] ass the association to be freed
62
62
 */
63
63
void association_relay_free_alpha_c_ass(struct association_relay *ass) {
64
 
        if (ass == NULL) {
65
 
                print_error("can't free NULL association_relay");
66
 
                exit(EXIT_FAILURE);
67
 
        }
68
 
 
69
 
        assert(ass->mode == ALPHA_C);
70
 
 
71
 
        xfree(ass);
 
64
    if (ass == NULL) {
 
65
        print_error("can't free NULL association_relay");
 
66
        exit(EXIT_FAILURE);
 
67
    }
 
68
 
 
69
    assert(ass->mode == ALPHA_C);
 
70
 
 
71
    xfree(ass);
72
72
}
73
73
 
74
74
//! Handles an incoming Alpha C packet
75
75
/*!
76
 
 * Handles a given S1 packet. Hash chain checks are performed in 
77
 
 * association_relay_handle_s1(). This function handles only the 
 
76
 * Handles a given S1 packet. Hash chain checks are performed in
 
77
 * association_relay_handle_s1(). This function handles only the
78
78
 * special functions for Alpha C, i.e. buffering received HMACs.
79
 
 *      
 
79
 *
80
80
 * @param[in,out] ass The association on that the packet came in
81
81
 * @param[in] packet the incoming packet
82
82
 * @param[in] packet_len the size of the packet (including the header)
83
83
 */
84
84
void association_relay_handle_s1_alpha_c(struct association_relay *const ass, struct alpha_packet const *const packet, unsigned int const packet_len) {
85
 
        assert(ass != NULL);
86
 
        assert(ass->mode == ALPHA_C);
87
 
        assert(packet != NULL);
88
 
        assert(packet->type == PACKET_S1);
89
 
        assert(packet_len > 0);
90
 
        const unsigned int sig_count = ntohs(packet->s1.signatures_count);
91
 
 
92
 
        rel_stats.s1_packets_alpha_C++;
93
 
 
94
 
        if(packet_len != packet_size(s1.common) + sig_count * sizeof(digest_t)) {
95
 
                AP_MSG_F(AP_MSG_LVL_ERROR, AP_MSG_CTX,"Actual packet size does not match size computed from signature count: %d != %d\n",
96
 
                                packet_len,
97
 
                                packet_size(s1.common) + sig_count * sizeof(digest_t));
98
 
                exit(EXIT_FAILURE);
99
 
        }
100
 
 
101
 
        // Keep statistics of how many pre-signatures were wasted
102
 
        rel_stats.s1_wasted_presigs_alpha_C += (MAX_PRESIG_COUNT - sig_count);
103
 
 
104
 
        // buffer the hmacs
105
 
        ap_presig_store_put(&ass->alpha_c.pstore, &packet->s1.anchor, packet->s1.common.presigs, sig_count);
106
 
 
107
 
        ass->alpha_c.packets_to_receive = sig_count;
108
 
        ass->alpha_c.packets_received = 0;
109
 
 
 
85
    assert(ass != NULL);
 
86
    assert(ass->mode == ALPHA_C);
 
87
    assert(packet != NULL);
 
88
    assert(packet->type == PACKET_S1);
 
89
    assert(packet_len > 0);
 
90
    const unsigned int sig_count = ntohs(packet->s1.signatures_count);
 
91
 
 
92
    rel_stats.s1_packets_alpha_C++;
 
93
 
 
94
    if (packet_len != packet_size(s1.common) + sig_count * sizeof(digest_t)) {
 
95
        AP_MSG_F(AP_MSG_LVL_ERROR, AP_MSG_CTX, "Actual packet size does not match size computed from signature count: %d != %d\n",
 
96
                 packet_len,
 
97
                 packet_size(s1.common) + sig_count * sizeof(digest_t));
 
98
        exit(EXIT_FAILURE);
 
99
    }
 
100
 
 
101
    // Keep statistics of how many pre-signatures were wasted
 
102
    rel_stats.s1_wasted_presigs_alpha_C += (MAX_PRESIG_COUNT - sig_count);
 
103
 
 
104
    // buffer the hmacs
 
105
    ap_presig_store_put(&ass->alpha_c.pstore, &packet->s1.anchor, packet->s1.common.presigs, sig_count);
 
106
 
 
107
    ass->alpha_c.packets_to_receive = sig_count;
 
108
    ass->alpha_c.packets_received   = 0;
110
109
}
111
110
 
112
111
//! Verifies a given Alpha S2 packet
113
112
/*!
114
113
 * Verifies a given Alpha S2 packet with the content of the buffer in the given Alpha C association.
115
 
 *      
 
114
 *
116
115
 * @param[in] ass the association on that the packet came in
117
116
 * @param[in] packet the incoming packet
118
117
 * @param[in] packet_len the size of the incoming packet (including the header)
119
118
 * @param[out] valid if the packet is valid (checked with the given HMAC)
120
119
 */
121
120
void association_relay_verify_alpha_c(struct association_relay *const ass, struct alpha_packet const *const packet, unsigned int const packet_len, bool *const valid) {
122
 
        assert(ass != NULL);
123
 
        assert(ass->mode == ALPHA_C);
124
 
        assert(packet != NULL);
125
 
        assert(packet->type == PACKET_S2);
126
 
        *valid = false;
127
 
        
128
 
        digest_t computed_hmac;
129
 
        digest_t computed_anchor;
130
 
 
131
 
        const int payload_len = packet_len - packet_size(s2.common);
132
 
 
133
 
        create_digest(&packet->s2.common.anchor, sizeof(digest_t), &computed_anchor);
134
 
        create_hmac(packet->s2.common.payload, payload_len, &packet->s2.common.anchor, &computed_hmac);
135
 
 
136
 
 
137
 
        *valid = ap_presig_check_store(&ass->alpha_c.pstore,
138
 
                                                                   &computed_anchor,
139
 
                                                                   &computed_hmac,
140
 
                                                                   ntohs(packet->s2.common.data_index));
141
 
 
142
 
        if(*valid) {
143
 
                ass->alpha_c.packets_received++;
144
 
        }
 
121
    assert(ass != NULL);
 
122
    assert(ass->mode == ALPHA_C);
 
123
    assert(packet != NULL);
 
124
    assert(packet->type == PACKET_S2);
 
125
    *valid = false;
 
126
 
 
127
    digest_t computed_hmac;
 
128
    digest_t computed_anchor;
 
129
 
 
130
    const int payload_len = packet_len - packet_size(s2.common);
 
131
 
 
132
    create_digest(&packet->s2.common.anchor, sizeof(digest_t), &computed_anchor);
 
133
    create_hmac(packet->s2.common.payload, payload_len, &packet->s2.common.anchor, &computed_hmac);
 
134
 
 
135
 
 
136
    *valid = ap_presig_check_store(&ass->alpha_c.pstore,
 
137
                                   &computed_anchor,
 
138
                                   &computed_hmac,
 
139
                                   ntohs(packet->s2.common.data_index));
 
140
 
 
141
    if (*valid) {
 
142
        ass->alpha_c.packets_received++;
 
143
    }
145
144
}