Skip to content

Commit

Permalink
drivers/xbee: encryption support
Browse files Browse the repository at this point in the history
add encryption to drivers
  • Loading branch information
FrancescoErmini committed May 14, 2015
1 parent 03bd65d commit 0d8ddf4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
11 changes: 10 additions & 1 deletion drivers/include/xbee.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
*/
#define XBEE_DEFAULT_CHANNEL (17U)

/**
* @brief Encryption key length in bytes (128 bit AES encryption)
*/
#define XBEE_ENCRYPTION_KEY_LEN (16U)

/**
* @brief States of the internal FSM for handling incoming UART frames
*
Expand Down Expand Up @@ -130,6 +135,10 @@ typedef struct {
uint8_t rx_buf[XBEE_MAX_PKT_LENGTH];/**< receiving data buffer */
uint16_t rx_count; /**< counter for ongoing transmission */
uint16_t rx_limit; /**< size RX frame transferred */

/* optional encryption status */
unsigned int encrypt; /**< Current state of encryption */

} xbee_t;

/**
Expand Down Expand Up @@ -160,4 +169,4 @@ int xbee_init(xbee_t *dev, uart_t uart, uint32_t baudrate,
#endif

#endif /* XBEE_H_ */
/** @} */
/** @} */
49 changes: 48 additions & 1 deletion drivers/xbee/xbee.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,49 @@ static int _set_proto(xbee_t *dev, uint8_t *val, size_t len)
return sizeof(ng_nettype_t);
}

static int _set_encryption(xbee_t *dev, uint8_t *val, size_t len)
{
dev->encrypt = *val; //store the current encryption status
uint8_t cmd[3];
resp_t resp;
/* get the current state of Encryption */
cmd[0] = 'E';
cmd[1] = 'E';
_api_at_cmd(dev, cmd, 2, &resp);

/* Prevent writing the same value in EE. */
if (val[0] != resp.data[0] ){
cmd[0] = 'E';
cmd[1] = 'E';
cmd[2] = val[0];
_api_at_cmd(dev, cmd, 3, &resp);
}
if (resp.status == 0) {
return 2;
}
return -ECANCELED;
}

static int _set_encryption_key(xbee_t *dev, uint8_t *val, size_t len)
{
uint8_t cmd[18];
resp_t resp;
if (len != 16) { //the AES key is 128bit, 16 byte
return -EINVAL;
}
cmd[0] = 'K';
cmd[1] = 'Y';

for(int i=0;i < 16;i++){ /* Append the key to the KY API AT command */
cmd[i+2]=val[i];
}
_api_at_cmd(dev, cmd, 18, &resp);
if (resp.status == 0) {
return 2;
}
return -ECANCELED;
}

/*
* Driver's "public" functions
*/
Expand Down Expand Up @@ -646,6 +689,10 @@ static int _set(ng_netdev_t *netdev, ng_netconf_opt_t opt,
return _set_panid(dev, (uint8_t *)value, value_len);
case NETCONF_OPT_PROTO:
return _set_proto(dev, (uint8_t *)value, value_len);
case NETCONF_OPT_ENCRYPTION:
return _set_encryption(dev, (uint8_t *) value, value_len);
case NETCONF_OPT_ENCRYPTION_KEY:
return _set_encryption_key(dev, (uint8_t *)value, value_len);
default:
return -ENOTSUP;
}
Expand Down Expand Up @@ -738,4 +785,4 @@ const ng_netdev_driver_t xbee_driver = {
.get = _get,
.set = _set,
.isr_event = _isr_event,
};
};
5 changes: 5 additions & 0 deletions sys/include/net/ng_netconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ typedef enum {
NETCONF_OPT_TX_END_IRQ,
NETCONF_OPT_AUTOCCA, /**< en/disable to check automatically
before sending the channel is clear. */
/**
* @brief en/disable encryption.
*/
NETCONF_OPT_ENCRYPTION, /**< en/disable encryption */
NETCONF_OPT_ENCRYPTION_KEY, /**< set encryption key */
/* add more options if needed */
} ng_netconf_opt_t;

Expand Down

0 comments on commit 0d8ddf4

Please sign in to comment.