summaryrefslogtreecommitdiff
path: root/cesar/cp2/msg
diff options
context:
space:
mode:
Diffstat (limited to 'cesar/cp2/msg')
-rw-r--r--cesar/cp2/msg/src/msg.c33
-rw-r--r--cesar/cp2/msg/test/src/cp_cl_interface_stub.c2
-rw-r--r--cesar/cp2/msg/test/src/test-msg.c377
3 files changed, 401 insertions, 11 deletions
diff --git a/cesar/cp2/msg/src/msg.c b/cesar/cp2/msg/src/msg.c
index 08ebfe5242..d94bf8ae42 100644
--- a/cesar/cp2/msg/src/msg.c
+++ b/cesar/cp2/msg/src/msg.c
@@ -331,7 +331,7 @@ cp_msg_mme_send (cp_t *ctx, cp_mme_tx_t *mme)
dbg_assert (mme);
/* Ends the MME. See HPAV table 11-253. */
- if ((mme->peks == CP_MME_PEKS_SPC_NOT_EMBEDDED)
+ if ((mme->peks != CP_MME_PEKS_SPC_NOT_EMBEDDED)
&& (mme->prun.pid != 0x4))
{
crc_t crc;
@@ -339,6 +339,9 @@ cp_msg_mme_send (cp_t *ctx, cp_mme_tx_t *mme)
uint crc_value;
u32 enc_tab[256];
uint word;
+ uint padding_len;
+ uint i;
+ uint enc_length;
crc.width = 32;
crc.generator = HPAV_CRC32_GENERATOR;
@@ -356,16 +359,16 @@ cp_msg_mme_send (cp_t *ctx, cp_mme_tx_t *mme)
* + The MME header.
* - 1 for the alignment.
*/
- word = *(uint* )(mme->p_mme + (mme->peer.vlan_tag ?
+ word = *((uint *) (mme->p_mme + (mme->peer.vlan_tag ?
HPAV_MME_HEADER_LEN_WITH_VLAN:
HPAV_MME_HEADER)
- + 21);
+ + 21));
word &= 0xFF0000FF;
word |= mme->length << 8;
- *(uint*)(mme->p_mme + (mme->peer.vlan_tag ?
+ *((uint*)(mme->p_mme + (mme->peer.vlan_tag ?
HPAV_MME_HEADER_LEN_WITH_VLAN:
HPAV_MME_HEADER)
- + 21) = word;
+ + 21)) = word;
@@ -386,10 +389,13 @@ cp_msg_mme_send (cp_t *ctx, cp_mme_tx_t *mme)
bitstream_access (&mme->bitstream, &mme->prun.pmn, 8);
/* Padding. crc_value variable used as Padding. */
- crc_value = 0;
- bitstream_access (&mme->bitstream, &crc_value, 32);
- bitstream_access (&mme->bitstream, &crc_value, 32);
- bitstream_access (&mme->bitstream, &crc_value, 24);
+ padding_len = (mme->length + mme->rf_len) / 16;
+
+ word = 0;
+ for ( i = 0; i < padding_len; i++)
+ {
+ bitstream_access (&mme->bitstream, &word, 8);
+ }
/* RF Len. */
bitstream_access (&mme->bitstream, &mme->rf_len, 8);
@@ -398,10 +404,14 @@ cp_msg_mme_send (cp_t *ctx, cp_mme_tx_t *mme)
bitstream_finalise (&mme->bitstream);
/* AES encryption. */
+ enc_length = (mme->peer.vlan_tag ? HPAV_MME_HEADER_LEN_WITH_VLAN :
+ HPAV_MME_HEADER) + mme->length + mme->rf_len + 9 + padding_len;
+
+ aes_set_key (&aes, (u8 *) &mme->iv_uuid);
aes_cbc_encrypt (&aes, (u8 *) &mme->iv_uuid,
mme->p_mme + mme->payload_offset - mme->rf_len,
mme->p_mme + mme->payload_offset - mme->rf_len,
- mme->length);
+ enc_length);
}
else
/* Finalise the bitstream. */
@@ -409,6 +419,9 @@ cp_msg_mme_send (cp_t *ctx, cp_mme_tx_t *mme)
/* Send the MME. */
cp_cl_interf_mme_send (ctx, mme);
+
+ /* Release the mme. */
+ slab_release (mme);
}
diff --git a/cesar/cp2/msg/test/src/cp_cl_interface_stub.c b/cesar/cp2/msg/test/src/cp_cl_interface_stub.c
index bd0625cb21..bbb364f7fb 100644
--- a/cesar/cp2/msg/test/src/cp_cl_interface_stub.c
+++ b/cesar/cp2/msg/test/src/cp_cl_interface_stub.c
@@ -40,5 +40,5 @@ cp_cl_interf_add_buffer_tx (cp_t *ctx, u8 * buffer)
void
cp_cl_interf_mme_send (cp_t *ctx, cp_mme_tx_t * mme)
{
-
+ mme->p_mme = NULL;
}
diff --git a/cesar/cp2/msg/test/src/test-msg.c b/cesar/cp2/msg/test/src/test-msg.c
index db6595dc04..8903c859d0 100644
--- a/cesar/cp2/msg/test/src/test-msg.c
+++ b/cesar/cp2/msg/test/src/test-msg.c
@@ -232,6 +232,380 @@ test_case_msg_mme_tx_init_enc (test_t test)
cp_msg_uninit (&cp);
}
+void
+test_case_msg_mme_tx_without_vlan_tag (test_t test)
+{
+ cp_t cp;
+ cp_mme_peer_t peer;
+ cp_sta_own_data_t *sta;
+ cp_secu_protocol_run_t prun;
+ cp_mme_tx_t *mme;
+ u8 *buffer;
+ mac_t mac;
+ bitstream_t bitstream;
+ cp_key_t key;
+
+ lib_rnd_init (&cp.rand, 0x12345678);
+ peer.mac = 0x23456789ABCDull;
+ peer.vlan_tag = 0;
+ peer.tei = 0x2;
+ peer.all_sta = false;
+ uint i;
+ uint data;
+ aes_context aes;
+ uint enc_length;
+
+ cp_msg_init (&cp);
+
+ sta = cp_sta_mgr_get_sta_own_data (&cp);
+ sta->is_cco = false;
+ sta->pco_status = false;
+
+ prun.pid = 2;
+ prun.pmn = 2;
+ prun.my_nonce = 0x3;
+ prun.your_nonce = 0x4;
+
+ mme = cp_msg_mme_init_encrypted (&cp, &peer, 0xb0, 2, &prun);
+ mme->length = 200-19;
+ key = mme->iv_uuid;
+ slab_addref (mme);
+
+ // Store the buffer to the buffer variable.
+ buffer = mme->p_mme;
+
+ // Fill the MME payload.
+ for ( i = 0; i < mme->length - 19; i++)
+ {
+ data = 0xAA;
+ bitstream_access (&mme->bitstream, &data, 8);
+ }
+
+ cp_msg_mme_send (&cp, mme);
+
+ cp_msg_uninit (&cp);
+
+ // Verify the data.
+ test_case_begin (test, "Encrypted Payload without Vlan");
+
+ bitstream_init (&bitstream, buffer, ETH_PACKET_MAX_SIZE, BITSTREAM_READ);
+ test_begin (test, "verify")
+ {
+ // ODA.
+ bitstream_access (&bitstream, &mac, 48);
+ test_fail_if (mac != peer.mac, "Wrong mac address");
+
+ // OSA.
+ bitstream_access (&bitstream, &mac, 48);
+ test_fail_if (mac != 0x123456789ABCull, "Wrong mac address");
+
+ // MType
+ bitstream_access (&bitstream, &data, 16);
+ test_fail_if (data != swap16(HPAV_MTYPE_MME), "Wrong MType");
+
+ // MMV
+ bitstream_access (&bitstream, &data, 8);
+ test_fail_if (data != HPAV_MMV, "Wrong MMV");
+
+ // MMTYPE.
+ bitstream_access (&bitstream, &data, 16);
+ test_fail_if (data != CM_ENCRYPTED_PAYLOAD_IND, "Wrong MMType");
+
+ // FMI.
+ bitstream_access (&bitstream, &data, 16);
+ test_fail_if (data != 0, "Wrong FMI");
+
+ // PEKS.
+ bitstream_access (&bitstream, &data, 8);
+ test_fail_if (data != 2, "Wrong PEKS");
+
+ // AVLN STATUS.
+ bitstream_access (&bitstream, &data, 8);
+ test_fail_if (data != CP_MSG_AVLN_STATUS_ASSOC_NOT_PCO,
+ "Wrong AVLN Status");
+
+ // PID.
+ bitstream_access (&bitstream, &data, 8);
+ test_fail_if (data != prun.pid, "Wrong PID");
+
+ // PRN.
+ bitstream_access (&bitstream, &data, 16);
+ test_fail_if (data != prun.prn, "Wrong PRN");
+
+ // PMN.
+ bitstream_access (&bitstream, &data, 8);
+ test_fail_if (data != prun.pmn, "Wrong PMN");
+
+ // IV.
+ for ( i = 0; i < 4; i++)
+ {
+ bitstream_access (&bitstream, &data, 32);
+ test_fail_if (data != key.key[i], "Wrong Key");
+ }
+
+ // Len.
+ bitstream_access (&bitstream, &data, 16);
+ test_fail_if (data != mme->length, "Wrong MME payload length");
+
+ // Decrypt the buffer.
+ enc_length = 19 + 9 + mme->rf_len + mme->length
+ + ((mme->length + mme->rf_len) / 16);
+ aes_set_key (&aes, (u8 *) &key);
+ aes_cbc_decrypt (&aes, (u8*) &key,
+ buffer + mme->payload_offset - mme->rf_len,
+ buffer + mme->payload_offset - mme->rf_len,
+ enc_length);
+
+ // RF.
+ for (i = 0; i < mme->rf_len; i++)
+ bitstream_access (&bitstream, &data, 8);
+
+ /* Payload. */
+ // ODA.
+ bitstream_access (&bitstream, &mac, 48);
+ test_fail_if (mac != peer.mac, "Wrong mac address");
+
+ // OSA.
+ bitstream_access (&bitstream, &mac, 48);
+ test_fail_if (mac != 0x123456789ABCull, "Wrong mac address");
+
+ // MType
+ bitstream_access (&bitstream, &data, 16);
+ test_fail_if (data != swap16(HPAV_MTYPE_MME), "Wrong MType");
+
+ // MMV
+ bitstream_access (&bitstream, &data, 8);
+ test_fail_if (data != HPAV_MMV, "Wrong MMV");
+
+ // MMTYPE.
+ bitstream_access (&bitstream, &data, 16);
+ test_fail_if (data != 0xb0, "Wrong MMType");
+ }
+ test_end;
+
+ slab_release (mme);
+}
+
+void
+test_case_msg_mme_tx_with_vlan_tag (test_t test)
+{
+ cp_t cp;
+ cp_mme_peer_t peer;
+ cp_sta_own_data_t *sta;
+ cp_secu_protocol_run_t prun;
+ cp_mme_tx_t *mme;
+ u8 *buffer;
+ mac_t mac;
+ bitstream_t bitstream;
+ cp_key_t key;
+
+ lib_rnd_init (&cp.rand, 0x12345678);
+ peer.mac = 0x23456789ABCDull;
+ peer.vlan_tag = 0x21;
+ peer.tei = 0x2;
+ peer.all_sta = false;
+ uint i;
+ uint data;
+ aes_context aes;
+ uint enc_length;
+
+ cp_msg_init (&cp);
+
+ sta = cp_sta_mgr_get_sta_own_data (&cp);
+ sta->is_cco = false;
+ sta->pco_status = false;
+
+ prun.pid = 2;
+ prun.pmn = 2;
+ prun.my_nonce = 0x3;
+ prun.your_nonce = 0x4;
+
+ mme = cp_msg_mme_init_encrypted (&cp, &peer, 0xb0, 2, &prun);
+ mme->length = 200-23;
+ key = mme->iv_uuid;
+ slab_addref (mme);
+
+ // Store the buffer to the buffer variable.
+ buffer = mme->p_mme;
+
+ // Fill the MME payload.
+ for ( i = 0; i < mme->length - 19; i++)
+ {
+ data = 0xAA;
+ bitstream_access (&mme->bitstream, &data, 8);
+ }
+
+ cp_msg_mme_send (&cp, mme);
+
+ cp_msg_uninit (&cp);
+
+ // Verify the data.
+ test_case_begin (test, "Encrypted Payload without Vlan");
+
+ bitstream_init (&bitstream, buffer, ETH_PACKET_MAX_SIZE, BITSTREAM_READ);
+ test_begin (test, "verify")
+ {
+ // ODA.
+ bitstream_access (&bitstream, &mac, 48);
+ test_fail_if (mac != peer.mac, "Wrong mac address");
+
+ // OSA.
+ bitstream_access (&bitstream, &mac, 48);
+ test_fail_if (mac != 0x123456789ABCull, "Wrong mac address");
+
+ // VLAN Tag.
+ bitstream_access (&bitstream, &data, 32);
+ test_fail_if (data != peer.vlan_tag, "Wrong VLAN tag");
+
+ // MType
+ bitstream_access (&bitstream, &data, 16);
+ test_fail_if (data != swap16(HPAV_MTYPE_MME), "Wrong MType");
+
+ // MMV
+ bitstream_access (&bitstream, &data, 8);
+ test_fail_if (data != HPAV_MMV, "Wrong MMV");
+
+ // MMTYPE.
+ bitstream_access (&bitstream, &data, 16);
+ test_fail_if (data != CM_ENCRYPTED_PAYLOAD_IND, "Wrong MMType");
+
+ // FMI.
+ bitstream_access (&bitstream, &data, 16);
+ test_fail_if (data != 0, "Wrong FMI");
+
+ // PEKS.
+ bitstream_access (&bitstream, &data, 8);
+ test_fail_if (data != 2, "Wrong PEKS");
+
+ // AVLN STATUS.
+ bitstream_access (&bitstream, &data, 8);
+ test_fail_if (data != CP_MSG_AVLN_STATUS_ASSOC_NOT_PCO,
+ "Wrong AVLN Status");
+
+ // PID.
+ bitstream_access (&bitstream, &data, 8);
+ test_fail_if (data != prun.pid, "Wrong PID");
+
+ // PRN.
+ bitstream_access (&bitstream, &data, 16);
+ test_fail_if (data != prun.prn, "Wrong PRN");
+
+ // PMN.
+ bitstream_access (&bitstream, &data, 8);
+ test_fail_if (data != prun.pmn, "Wrong PMN");
+
+ // IV.
+ for ( i = 0; i < 4; i++)
+ {
+ bitstream_access (&bitstream, &data, 32);
+ test_fail_if (data != key.key[i], "Wrong Key");
+ }
+
+ // Len.
+ bitstream_access (&bitstream, &data, 16);
+ test_fail_if (data != mme->length, "Wrong MME payload length");
+
+ // Decrypt the buffer.
+ enc_length = 23 + 9 + mme->rf_len + mme->length
+ + ((mme->length + mme->rf_len) / 16);
+ aes_set_key (&aes, (u8 *) &key);
+ aes_cbc_decrypt (&aes, (u8*) &key,
+ buffer + mme->payload_offset - mme->rf_len,
+ buffer + mme->payload_offset - mme->rf_len,
+ enc_length);
+
+ // RF.
+ for (i = 0; i < mme->rf_len; i++)
+ bitstream_access (&bitstream, &data, 8);
+
+ /* Payload. */
+ // ODA.
+ bitstream_access (&bitstream, &mac, 48);
+ test_fail_if (mac != peer.mac, "Wrong mac address");
+
+ // OSA.
+ bitstream_access (&bitstream, &mac, 48);
+ test_fail_if (mac != 0x123456789ABCull, "Wrong mac address");
+
+ // VLAN Tag.
+ bitstream_access (&bitstream, &data, 32);
+ test_fail_if (data != peer.vlan_tag, "Wrong VLAN tag");
+
+ // MType
+ bitstream_access (&bitstream, &data, 16);
+ test_fail_if (data != swap16(HPAV_MTYPE_MME), "Wrong MType");
+
+ // MMV
+ bitstream_access (&bitstream, &data, 8);
+ test_fail_if (data != HPAV_MMV, "Wrong MMV");
+
+ // MMTYPE.
+ bitstream_access (&bitstream, &data, 16);
+ test_fail_if (data != 0xb0, "Wrong MMType");
+ }
+ test_end;
+
+ slab_release (mme);
+}
+
+void
+test_case_msg_not_enc (test_t test)
+{
+ cp_t cp;
+ cp_mme_tx_t *mme;
+ cp_mme_peer_t peer;
+ bitstream_t bitstream;
+ u64 data;
+ u8 *buffer;
+
+ cp_msg_init (&cp);
+
+ test_case_begin (test, "Send a MME not encrypted");
+
+ peer.mac = 0x123456789ABCull;
+ peer.vlan_tag = 0xAB;
+ peer.tei = 0xA;
+ peer.all_sta = false;
+
+ mme = cp_msg_mme_init (&cp, &peer, 0xb0);
+ slab_addref (mme);
+ buffer = mme->p_mme;
+ cp_msg_mme_send (&cp, mme);
+
+ bitstream_init (&bitstream, buffer, HPAV_MME_HEADER_LEN_WITH_VLAN,
+ BITSTREAM_READ);
+ test_begin (test, "Verify")
+ {
+ test_fail_if (mme->peer.mac != peer.mac, "Wrong MAC @");
+ test_fail_if (mme->peer.vlan_tag != peer.vlan_tag, "Wrong VLAN tag");
+ test_fail_if (mme->peer.all_sta != peer.all_sta, "Wrong STA Flag");
+
+ bitstream_access (&bitstream, &data, 48);
+ test_fail_if (data != peer.mac, "Wrong destination mac address");
+
+ bitstream_access (&bitstream, &data, 48);
+ test_fail_if (data != 0x123456789ABCull,
+ "Wrong destination mac address");
+
+ bitstream_access (&bitstream, &data, 32);
+ test_fail_if (data != peer.vlan_tag, "Wrong VLAN TAG");
+
+ bitstream_access (&bitstream, &data, 16);
+ test_fail_if (swap16(data) != HPAV_MTYPE_MME, "Wrong Mtype");
+
+ bitstream_access (&bitstream, &data, 8);
+ test_fail_if (data != HPAV_MMV, "Wrong MMV");
+
+ bitstream_access (&bitstream, &data, 16);
+ test_fail_if (data != 0xb0, "Wrong MMTYPE");
+ }
+ test_end;
+
+ bitstream_finalise (&bitstream);
+ slab_release (mme);
+
+ cp_msg_uninit (&cp);
+}
int
main (void)
@@ -242,6 +616,9 @@ main (void)
test_case_msg_messages_init (test);
test_case_msg_mme_tx_init (test);
test_case_msg_mme_tx_init_enc (test);
+ test_case_msg_mme_tx_without_vlan_tag (test);
+ test_case_msg_mme_tx_with_vlan_tag (test);
+ test_case_msg_not_enc (test);
test_case_begin (test, "Memory allocation");
test_begin (test, "memory leaks")