summaryrefslogtreecommitdiff
path: root/cesar/cp2/msg/test/src/msg_cm.c
blob: d34a0b37538f719ba9a9647126fc96e607693802 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp2/msg/test/src/msg_cm.c
 * \brief   Test the MSG_CM Family functions.
 * \ingroup cp2_msg
 *
 */
#include "common/std.h"

#include "common/defs/ethernet.h"
#include "common/defs/homeplugAV.h"

#include "lib/test.h"
#include "lib/swap.h"
#include "lib/bitstream.h"

#include "cp2/cp.h"
#include "cp2/msg/msg.h"
#include "cp2/sta/mgr/sta_mgr.h"
#include "cp2/sta/mgr/sta_own_data.h"

#include "cp2/inc/context.h"
#include "cp2/msg/inc/msg.h"

struct mme_header_t
{
    mac_t oda;
    mac_t osa;
    uint vlan;
    uint mtype;
    uint mmv;
    cp_mmtype_t mmtype;
    uint fmi_inf;
    uint fmi_mi;
    uint fmi_ssn;
};
typedef struct mme_header_t mme_header_t;

static u8 buffer[2048] __attribute__((aligned(2048)));
static uint buffer_len;
cp_t cp;
mac_config_t mac_config;
bitstream_t stream;
mme_header_t header;
mac_t own_mac_addr = 0x123456789ABCull;

mme_header_t expected;
test_t test;

void
test_case_cm_set_key (void)
{
    cp_mme_peer_t peer;
    cp_nid_t nid;
    cp_secu_protocol_run_t prun;
    cp_msg_cm_set_key_req_t key_req;
    cp_msg_cm_set_key_cnf_t key_cnf;
    cp_mme_rx_t *msg;
    uint fmi;
    cp_sta_own_data_t *own_data;

    test_case_begin (test, "CM_SET_KEY.REQ");

    peer.mac = 0x23456789ABCDull;
    peer.vlan_tag = 0x0;
    peer.tei = 0xA;
    peer.all_sta = false;

    nid = 0x123456789ABCDEull;

    prun.pid = 2;
    prun.pmn = 2;
    prun.prn = 2;
    prun.my_nonce = 0x12;
    prun.your_nonce = 0x13;

    key_req.key_type = CP_MSG_KEY_DAK;
    key_req.cco_cap = CP_CCO_LEVEL;
    key_req.new_eks = 2;
    key_req.new_key.key[0] = 0;
    key_req.new_key.key[1] = 1;
    key_req.new_key.key[2] = 2;
    key_req.new_key.key[3] = 3;
    key_req.nid = nid;

    cp_msg_cm_set_key_req_send (&cp, &peer, 2, &prun, &key_req);

    // Read the MME.
    own_data = cp_sta_mgr_get_sta_own_data (&cp);
    cp_sta_own_data_set_mac_address (&cp, peer.mac);

    msg = cp_msg_mme_read_header (&cp, (u8*)buffer, buffer_len, 0xa, &fmi);
    cp_msg_mme_read_header_enc (&cp, msg);

    cp_msg_cm_set_key_req_receive (&cp, msg, &key_req);
    test_begin (test, "Verify")
    {
        test_fail_if (key_req.key_type != CP_MSG_KEY_DAK, "Wrong key type");
        test_fail_if (key_req.cco_cap != CP_CCO_LEVEL, "Wrong CCo level");
        test_fail_if (key_req.nid != nid, "Wrong NID");
        test_fail_if (msg->prun.pid != prun.pid, "Wrong PID");
        test_fail_if (msg->prun.prn != prun.prn, "Wrong PRN");
        test_fail_if (msg->prun.pmn != prun.pmn, "Wrong PMN");
        test_fail_if (msg->prun.your_nonce != prun.my_nonce , "Wrong nonce");
    }
    test_end;

    slab_release (msg);

    test_case_begin (test, "CM_SET_KEY.CNF");

    cp_sta_own_data_set_mac_address (&cp, own_mac_addr);
    peer.mac = 0x23456789ABCDull;
    peer.vlan_tag = 0x0;
    peer.tei = 0xA;
    peer.all_sta = false;

    nid = 0x123456789ABCDEull;

    prun.pid = 2;
    prun.pmn = 2;
    prun.prn = 2;
    prun.my_nonce = 0x12;
    prun.your_nonce = 0x13;

    key_cnf.result = CP_MSG_CM_SET_KEY_CNF_RESULT_SUCCESS;
    key_cnf.cco_cap = CP_CCO_LEVEL;

    cp_msg_cm_set_key_cnf_send (&cp, &peer, 2, &prun, &key_cnf);

    // Read the MME.
    cp_sta_own_data_set_mac_address (&cp, peer.mac);

    msg = cp_msg_mme_read_header (&cp, (u8*)buffer, buffer_len, 0xa, &fmi);

    cp_msg_cm_set_key_cnf_receive (&cp, msg, &key_cnf);
    test_begin (test, "Verify")
    {
        test_fail_if (key_cnf.result != CP_MSG_CM_SET_KEY_CNF_RESULT_SUCCESS,
                      "Wrong result type");
        test_fail_if (key_cnf.cco_cap != CP_CCO_LEVEL, "Wrong CCo level");
        test_fail_if (msg->prun.pid != prun.pid, "Wrong PID");
        test_fail_if (msg->prun.prn != prun.prn, "Wrong PRN");
        test_fail_if (msg->prun.pmn != prun.pmn, "Wrong PMN");
        test_fail_if (msg->prun.your_nonce != prun.my_nonce , "Wrong nonce");
    }
    test_end;

    slab_release (msg);
}

void
test_case_cm_get_key (void)
{
    cp_mme_peer_t peer;
    cp_nid_t nid;
    cp_secu_protocol_run_t prun;
    cp_mme_rx_t *msg;
    uint fmi;
    cp_sta_own_data_t *own_data;
    cp_msg_cm_get_key_req_t data;
    cp_msg_cm_get_key_cnf_t cnf;

    test_case_begin (test, "CM_SET_GET_KEY.REQ");

    peer.mac = 0x23456789ABCDull;
    peer.vlan_tag = 0x0;
    peer.tei = 0xA;
    peer.all_sta = false;

    nid = 0x123456789ABCDEull;

    prun.pid = 2;
    prun.pmn = 2;
    prun.prn = 2;
    prun.my_nonce = 0x12;
    prun.your_nonce = 0x13;

    data.relayed = false;
    data.key_type = CP_MSG_KEY_DAK;
    data.nid = nid;

    cp_msg_cm_get_key_req_send (&cp, &peer, 2, &prun, &data);

    // Read the MME.
    own_data = cp_sta_mgr_get_sta_own_data (&cp);
    cp_sta_own_data_set_mac_address (&cp, peer.mac);

    msg = cp_msg_mme_read_header (&cp, (u8*)buffer, buffer_len, 0xa, &fmi);
    cp_msg_mme_read_header_enc (&cp, msg);

    cp_msg_cm_get_key_req_receive (&cp, msg, &data);
    test_begin (test, "Verify")
    {
        test_fail_if (data.relayed != false, "Wrong relayed data");
        test_fail_if (data.key_type != CP_MSG_KEY_DAK, "Wrong key type");
        test_fail_if (data.nid != nid, "Wrong NID");
        test_fail_if (msg->prun.pid != prun.pid, "Wrong PID");
        test_fail_if (msg->prun.prn != prun.prn, "Wrong PRN");
        test_fail_if (msg->prun.pmn != prun.pmn, "Wrong PMN");
        test_fail_if (msg->prun.your_nonce != prun.my_nonce , "Wrong nonce");
    }
    test_end;

    slab_release (msg);

    test_case_begin (test, "CM_SET_GET_KEY.CNF");

    peer.mac = 0x23456789ABCDull;
    peer.vlan_tag = 0x0;
    peer.tei = 0xA;
    peer.all_sta = false;

    nid = 0x123456789ABCDEull;

    prun.pid = 2;
    prun.pmn = 2;
    prun.prn = 2;
    prun.my_nonce = 0x12;
    prun.your_nonce = 0x13;

    cnf.result = CP_MSG_CM_GET_KEY_CNF_RESULT_KEY_GRANTED;
    cnf.key_type = CP_MSG_KEY_DAK;
    cnf.nid = nid;
    cnf.eks = 2;
    cnf.key.key[0] = 0;
    cnf.key.key[1] = 1;
    cnf.key.key[2] = 2;
    cnf.key.key[3] = 3;

    cp_msg_cm_get_key_cnf_send (&cp, &peer, 2, &prun, &cnf);

    // Read the MME.
    own_data = cp_sta_mgr_get_sta_own_data (&cp);
    cp_sta_own_data_set_mac_address (&cp, peer.mac);

    msg = cp_msg_mme_read_header (&cp, (u8*)buffer, buffer_len, 0xa, &fmi);
    cp_msg_mme_read_header_enc (&cp, msg);

    cp_msg_cm_get_key_cnf_receive (&cp, msg, &cnf);
    test_begin (test, "Verify")
    {
        test_fail_if (cnf.result != CP_MSG_CM_GET_KEY_CNF_RESULT_KEY_GRANTED,
                      "Wrong result data");
        test_fail_if (cnf.key_type != CP_MSG_KEY_DAK, "Wrong key type");
        test_fail_if (cnf.nid != nid, "Wrong NID");
        test_fail_if (cnf.eks != 2, "Wrong EKS");
        test_fail_if (cnf.key.key[0] != 0, "Wrong Key 0");
        test_fail_if (cnf.key.key[1] != 1, "Wrong Key 1");
        test_fail_if (cnf.key.key[2] != 2, "Wrong Key 2");
        test_fail_if (cnf.key.key[3] != 3, "Wrong Key 3");
        test_fail_if (msg->prun.pid != prun.pid, "Wrong PID");
        test_fail_if (msg->prun.prn != prun.prn, "Wrong PRN");
        test_fail_if (msg->prun.pmn != prun.pmn, "Wrong PMN");
        test_fail_if (msg->prun.your_nonce != prun.my_nonce , "Wrong nonce");
    }
    test_end;

    slab_release (msg);
}

int
main (void)
{
    cp_sta_own_data_t *own_data;

    cp.mac_config = &mac_config;
    test_init (test, 0, NULL);

    own_data = cp_sta_mgr_get_sta_own_data (&cp);
    cp_sta_own_data_set_tei (&cp, 0xA);
    cp_sta_own_data_set_mac_address (&cp, own_mac_addr);
    cp_sta_mgr_update_our_avln_nid (&cp, 0x123456789ABCDEFull);
    cp_sta_mgr_update_our_avln_snid (&cp, 0x2);

    lib_rnd_init (&cp.rnd, 0x12345678);

    test_case_cm_set_key ();
    test_case_cm_get_key ();

    test_case_begin (test, "Memory allocation");
    test_begin (test, "memory leaks")
    {
        test_fail_if (blk_check_memory () != true, "Memory leaks");
    }
    test_end;

    test_result (test);
    return test_nb_failed (test) == 0 ? 0 : 1;
}

u8 *
cp_cl_interf_get_buffer_tx (cp_t *ctx)
{
    dbg_assert (ctx);
    return buffer;
}

void
cp_cl_interf_add_buffer_tx (cp_t *ctx, u8 * buffer)
{
}

/**
 * Send a MME over the PWL or the HLE.
 * \param  ctx  the module context.
 * \param  mme  The MME to send.
 *
 */
void
cp_cl_interf_mme_send (cp_t *ctx, cp_mme_tx_t * mme)
{
    buffer_len = mme->length;
    mme->p_mme = NULL;
}