Skip to content

Commit

Permalink
drivers/xbee: encryption support review
Browse files Browse the repository at this point in the history
add encryption functions prototipes and modify _set() to include NETCONF_OPT_ENCRYPTION

add NETCONF_OPT_ENCRYPTION to ng_netconf_opt_t

add  optional encryption funtionality to the xbee  test

fix trailng white space
  • Loading branch information
FrancescoErmini committed Apr 26, 2015
1 parent 2f758a6 commit 0e12d75
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 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
47 changes: 47 additions & 0 deletions 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
6 changes: 6 additions & 0 deletions sys/include/net/ng_netconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ typedef enum {
NETCONF_OPT_RAWMODE, /**< en/disable the pre-processing of data
in a network device driver as type
ng_nettype_t */
/**
* @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
9 changes: 9 additions & 0 deletions tests/driver_xbee/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ int main(void)
puts("Error initializing MAC layer");
return -1;
}
/* optionally en/disable and set Xbee encryption key */
#ifdef OPT_ENCRYPTION
ng_netconf_enable_t encrypt = NETCONF_ENABLE; /* warning: use NETCONF_DISABLE before
* set OPT_ENCRYPTION to undefined */
static uint8_t key_buf[XBEE_ENCRYPTION_KEY_LEN]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,}; // change those value to match your key
dev.driver->set((ng_netdev_t *)&dev,NETCONF_OPT_ENCRYPTION, &encrypt,1);
dev.driver->set((ng_netdev_t *)&dev,NETCONF_OPT_ENCRYPTION_KEY,key_buf,sizeof(key_buf));
#endif

/* start the shell */
puts("Initialization OK, starting shell now");
Expand Down

0 comments on commit 0e12d75

Please sign in to comment.