52 #include "net/nbr-table.h"
57 #ifdef NONCORESEC_CONF_DECORATED_FRAMER
58 #define DECORATED_FRAMER NONCORESEC_CONF_DECORATED_FRAMER
60 #define DECORATED_FRAMER framer_802154
63 extern const struct framer DECORATED_FRAMER;
65 #ifdef NONCORESEC_CONF_SEC_LVL
66 #define SEC_LVL NONCORESEC_CONF_SEC_LVL
71 #define WITH_ENCRYPTION (SEC_LVL & (1 << 2))
72 #define MIC_LEN LLSEC802154_MIC_LEN(SEC_LVL)
74 #ifdef NONCORESEC_CONF_KEY
75 #define NONCORESEC_KEY NONCORESEC_CONF_KEY
77 #define NONCORESEC_KEY { 0x00 , 0x01 , 0x02 , 0x03 , \
78 0x04 , 0x05 , 0x06 , 0x07 , \
79 0x08 , 0x09 , 0x0A , 0x0B , \
80 0x0C , 0x0D , 0x0E , 0x0F }
83 #define SECURITY_HEADER_LENGTH 5
88 #define PRINTF(...) printf(__VA_ARGS__)
93 #if LLSEC802154_USES_AUX_HEADER && SEC_LVL && LLSEC802154_USES_FRAME_COUNTER
96 static uint8_t key[16] = NONCORESEC_KEY;
97 NBR_TABLE(
struct anti_replay_info, anti_replay_table);
101 aead(uint8_t hdrlen,
int forward)
104 uint8_t nonce[CCM_STAR_NONCE_LENGTH];
110 uint8_t generated_mic[MIC_LEN];
113 ccm_star_packetbuf_set_nonce(nonce, forward);
119 m_len = totlen - hdrlen;
127 result = forward ? mic : generated_mic;
139 return (memcmp(generated_mic, mic, MIC_LEN) == 0);
144 add_security_header(
void)
146 packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, FRAME802154_DATAFRAME);
147 packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL, SEC_LVL);
151 send(mac_callback_t sent,
void *ptr)
153 add_security_header();
155 NETSTACK_MAC.send(sent, ptr);
163 result = DECORATED_FRAMER.create();
164 if(result == FRAMER_FAILED) {
177 const linkaddr_t *sender;
178 struct anti_replay_info* info;
180 result = DECORATED_FRAMER.parse();
181 if(result == FRAMER_FAILED) {
185 if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) != SEC_LVL) {
186 PRINTF(
"noncoresec: received frame with wrong security level\n");
187 return FRAMER_FAILED;
189 sender = packetbuf_addr(PACKETBUF_ADDR_SENDER);
191 PRINTF(
"noncoresec: frame from ourselves\n");
192 return FRAMER_FAILED;
197 if(!aead(result, 0)) {
198 PRINTF(
"noncoresec: received unauthentic frame %"PRIu32
"\n",
200 return FRAMER_FAILED;
203 info = nbr_table_get_from_lladdr(anti_replay_table, sender);
205 info = nbr_table_add_lladdr(anti_replay_table, sender, NBR_TABLE_REASON_LLSEC,
NULL);
207 PRINTF(
"noncoresec: could not get nbr_table_item\n");
208 return FRAMER_FAILED;
222 if(!nbr_table_lock(anti_replay_table, info)) {
223 nbr_table_remove(anti_replay_table, info);
224 PRINTF(
"noncoresec: could not lock\n");
225 return FRAMER_FAILED;
231 PRINTF(
"noncoresec: received replayed frame %"PRIu32
"\n",
233 return FRAMER_FAILED;
243 NETSTACK_NETWORK.input();
249 add_security_header();
250 return DECORATED_FRAMER.length() + MIC_LEN;
256 CCM_STAR.set_key(key);
257 nbr_table_register(anti_replay_table,
NULL);
267 const struct framer noncoresec_framer = {
static void input(void)
Process a received 6lowpan packet.
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Common functionality of 802.15.4-compliant llsec_drivers.
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Header file for the Rime buffer (packetbuf) management
uint32_t anti_replay_get_counter(void)
Gets the frame counter from packetbuf.
void(* send)(mac_callback_t sent_callback, void *ptr)
Secures outgoing frames before passing them to NETSTACK_MAC.
Interface to anti-replay mechanisms.
Header file for the Rime address representation
The structure of a link layer security driver.
#define NULL
The null pointer.
802.15.4 frame creation and parsing functions
802.15.4 security implementation, which uses a network-wide key
void(* init)(void)
Inits link layer security.
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
void anti_replay_set_counter(void)
Sets the frame counter packetbuf attributes.
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two Rime addresses.
linkaddr_t linkaddr_node_addr
The Rime address of the node.
void anti_replay_init_info(struct anti_replay_info *info)
Initializes the anti-replay information about the sender.
CCM* convenience functions for MAC security
int anti_replay_was_replayed(struct anti_replay_info *info)
Checks if received frame was replayed.
Include file for the Contiki low-layer network stack (NETSTACK)