Defines | Enumerations | Functions

gazell/common/gzp.h File Reference

Gazell pairing library header file. More...

#include "gzll.h"
#include "gzp_params.h"
#include <stdint.h>
#include <stdbool.h>

Go to the source code of this file.

Defines

#define GZP_PAIRING_PIPE   0
#define GZP_DATA_PIPE   1
#define GZP_TX_RX_TRANS_DELAY   10
#define GZP_SYSTEM_ADDRESS_WIDTH   4
#define GZP_VALIDATION_ID   {0x32, 0x53, 0x66}
#define GZP_VALIDATION_ID_LENGTH   3
#define GZP_HOST_ID_LENGTH   5
#define GZP_SESSION_TOKEN_LENGTH   GZP_HOST_ID_LENGTH
#define GZP_DYN_KEY_LENGTH   (16 - GZP_VALIDATION_ID_LENGTH)
#define GZP_CMD_HOST_ADDRESS_REQ_PAYLOAD_LENGTH   1
#define GZP_CMD_HOST_ADDRESS_FETCH_PAYLOAD_LENGTH   1
#define GZP_CMD_HOST_ID_REQ_SESSION_TOKEN   1
#define GZP_CMD_HOST_ID_REQ_PAYLOAD_LENGTH   (GZP_CMD_HOST_ID_REQ_SESSION_TOKEN + GZP_SESSION_TOKEN_LENGTH)
#define GZP_CMD_HOST_ID_FETCH_VALIDATION_ID   1
#define GZP_CMD_HOST_ID_FETCH_PAYLOAD_LENGTH   (GZP_CMD_HOST_ID_FETCH_VALIDATION_ID + GZP_VALIDATION_ID_LENGTH)
#define GZP_CMD_KEY_UPDATE_PREPARE_PAYLOAD_LENGTH   1
#define GZP_CMD_KEY_UPDATE_VALIDATION_ID   1
#define GZP_CMD_KEY_UPDATE_NEW_KEY   (GZP_CMD_KEY_UPDATE_VALIDATION_ID + GZP_VALIDATION_ID_LENGTH)
#define GZP_CMD_KEY_UPDATE_PAYLOAD_LENGTH   (GZP_CMD_KEY_UPDATE_NEW_KEY + GZP_DYN_KEY_LENGTH)
#define GZP_CMD_ENCRYPTED_USER_DATA_VALIDATION_ID   1
#define GZP_CMD_ENCRYPTED_USER_DATA_PAYLOAD   ((GZP_CMD_ENCRYPTED_USER_DATA_VALIDATION_ID + GZP_VALIDATION_ID_LENGTH))
#define GZP_USER_DATA_PACKET_OVERHEAD   ( GZP_CMD_ENCRYPTED_USER_DATA_VALIDATION_ID + GZP_VALIDATION_ID_LENGTH)
#define GZP_ENCRYPTED_USER_DATA_MAX_LENGTH   (17 - GZP_USER_DATA_PACKET_OVERHEAD)
#define GZPAR_CMD_FETCH_RESP_PAYLOAD_LENGTH   1
#define GZP_CMD_HOST_ADDRESS_RESP_ADDRESS   1
#define GZP_CMD_HOST_ADDRESS_RESP_PAYLOAD_LENGTH   (GZP_CMD_HOST_ADDRESS_RESP_ADDRESS + GZP_SYSTEM_ADDRESS_WIDTH)
#define GZP_CMD_HOST_ID_FETCH_RESP_VALIDATION_ID   1
#define GZP_CMD_HOST_ID_FETCH_RESP_STATUS   (GZP_CMD_HOST_ID_FETCH_RESP_VALIDATION_ID + GZP_VALIDATION_ID_LENGTH)
#define GZP_CMD_HOST_ID_FETCH_RESP_HOST_ID   (GZP_CMD_HOST_ID_FETCH_RESP_STATUS + 1)
#define GZP_CMD_HOST_ID_FETCH_RESP_PAYLOAD_LENGTH   (GZP_CMD_HOST_ID_FETCH_RESP_HOST_ID + GZP_HOST_ID_LENGTH)
#define GZP_CMD_KEY_UPDATE_PREPARE_RESP_SESSION_TOKEN   1
#define GZP_CMD_KEY_UPDATE_PREPARE_RESP_PAYLOAD_LENGTH   (GZP_CMD_KEY_UPDATE_PREPARE_RESP_SESSION_TOKEN + GZP_SESSION_TOKEN_LENGTH)
#define GZP_CMD_ENCRYPTED_USER_DATA_RESP_SESSION_TOKEN   1
#define GZP_CMD_ENCRYPTED_USER_DATA_RESP_VALIDATION_ID   (GZP_CMD_ENCRYPTED_USER_DATA_RESP_SESSION_TOKEN + GZP_SESSION_TOKEN_LENGTH)
#define GZP_CMD_ENCRYPTED_USER_DATA_RESP_PAYLOAD_LENGTH   (GZP_CMD_ENCRYPTED_USER_DATA_RESP_VALIDATION_ID + GZP_VALIDATION_ID_LENGTH)

Enumerations

enum  gzp_key_select_t { GZP_ID_EXCHANGE, GZP_KEY_EXCHANGE, GZP_DATA_EXCHANGE }
enum  gzp_cmd_t {
  GZP_CMD_HOST_ADDRESS_REQ = 0, GZP_CMD_HOST_ADDRESS_FETCH, GZP_CMD_HOST_ID_REQ, GZP_CMD_HOST_ID_FETCH,
  GZP_CMD_KEY_UPDATE_PREPARE, GZP_CMD_KEY_UPDATE, GZP_CMD_ENCRYPTED_USER_DATA, GZP_CMD_FETCH_RESP,
  GZP_CMD_HOST_ADDRESS_RESP, GZP_CMD_HOST_ID_FETCH_RESP, GZP_CMD_KEY_UPDATE_PREPARE_RESP, GZP_CMD_ENCRYPTED_USER_DATA_RESP
}
enum  gzp_id_req_res_t { GZP_ID_RESP_PENDING, GZP_ID_RESP_GRANTED, GZP_ID_RESP_REJECTED, GZP_ID_RESP_FAILED }

Functions

void gzp_crypt_set_session_token (const uint8_t *token)
void gzp_crypt_set_dyn_key (const uint8_t *src_key)
void gzp_crypt_get_session_token (uint8_t *dst_token)
void gzp_crypt_get_dyn_key (uint8_t *dst_key)
_Bool gzp_get_host_id (uint8_t *dst)
void gzp_crypt_select_key (gzp_key_select_t key_select)
void gzp_crypt (uint8_t *dst, const uint8_t *src, uint8_t length)
_Bool gzp_validate_id (const uint8_t *src_id)
void gzp_add_validation_id (uint8_t *dst_id)
void gzp_random_numbers_generate (uint8_t *dst, uint8_t n)
void gzp_update_radio_params (const uint8_t *system_address)
void gzp_generate_channels (uint8_t *ch_dst, const uint8_t *system_address, uint8_t channel_tab_size)
void gzp_xor_cipher (uint8_t *dst, const uint8_t *src, const uint8_t *pad, uint8_t length)
Common Device and Host functions
void gzp_init (void)
void gzp_id_req_cancel (void)
Device functions
_Bool gzp_address_req_send (void)
gzp_id_req_res_t gzp_id_req_send (void)
_Bool gzp_crypt_data_send (const uint8_t *src, uint8_t length)
Host functions
void gzp_pairing_enable (_Bool enable)
void gzp_host_execute (void)
_Bool gzp_address_exchanged (void)
_Bool gzp_id_req_received (void)
void gzp_id_req_reject (void)
void gzp_id_req_grant (void)
_Bool gzp_crypt_user_data_received (void)
_Bool gzp_crypt_user_data_read (uint8_t *dst, uint8_t *length)

Detailed Description

Gazell pairing library header file.

Definition in file gzp.h.


Define Documentation

#define GZP_PAIRING_PIPE   0

Definition at line 30 of file gzp.h.

#define GZP_DATA_PIPE   1

Definition at line 31 of file gzp.h.

#define GZP_TX_RX_TRANS_DELAY   10

Definition at line 32 of file gzp.h.

#define GZP_SYSTEM_ADDRESS_WIDTH   4

Definition at line 33 of file gzp.h.

#define GZP_VALIDATION_ID   {0x32, 0x53, 0x66}

Definition at line 34 of file gzp.h.

#define GZP_VALIDATION_ID_LENGTH   3

Definition at line 35 of file gzp.h.

#define GZP_HOST_ID_LENGTH   5

Definition at line 36 of file gzp.h.

#define GZP_SESSION_TOKEN_LENGTH   GZP_HOST_ID_LENGTH

Definition at line 37 of file gzp.h.

#define GZP_DYN_KEY_LENGTH   (16 - GZP_VALIDATION_ID_LENGTH)

Definition at line 38 of file gzp.h.

#define GZP_CMD_HOST_ADDRESS_REQ_PAYLOAD_LENGTH   1

Definition at line 45 of file gzp.h.

#define GZP_CMD_HOST_ADDRESS_FETCH_PAYLOAD_LENGTH   1

Definition at line 48 of file gzp.h.

#define GZP_CMD_HOST_ID_REQ_SESSION_TOKEN   1

Definition at line 51 of file gzp.h.

#define GZP_CMD_HOST_ID_REQ_PAYLOAD_LENGTH   (GZP_CMD_HOST_ID_REQ_SESSION_TOKEN + GZP_SESSION_TOKEN_LENGTH)

Definition at line 52 of file gzp.h.

#define GZP_CMD_HOST_ID_FETCH_VALIDATION_ID   1

Definition at line 59 of file gzp.h.

#define GZP_CMD_HOST_ID_FETCH_PAYLOAD_LENGTH   (GZP_CMD_HOST_ID_FETCH_VALIDATION_ID + GZP_VALIDATION_ID_LENGTH)

Definition at line 60 of file gzp.h.

#define GZP_CMD_KEY_UPDATE_PREPARE_PAYLOAD_LENGTH   1

Definition at line 67 of file gzp.h.

#define GZP_CMD_KEY_UPDATE_VALIDATION_ID   1

Definition at line 70 of file gzp.h.

#define GZP_CMD_KEY_UPDATE_NEW_KEY   (GZP_CMD_KEY_UPDATE_VALIDATION_ID + GZP_VALIDATION_ID_LENGTH)

Definition at line 71 of file gzp.h.

#define GZP_CMD_KEY_UPDATE_PAYLOAD_LENGTH   (GZP_CMD_KEY_UPDATE_NEW_KEY + GZP_DYN_KEY_LENGTH)

Definition at line 72 of file gzp.h.

#define GZP_CMD_ENCRYPTED_USER_DATA_VALIDATION_ID   1

Definition at line 79 of file gzp.h.

#define GZP_CMD_ENCRYPTED_USER_DATA_PAYLOAD   ((GZP_CMD_ENCRYPTED_USER_DATA_VALIDATION_ID + GZP_VALIDATION_ID_LENGTH))

Definition at line 80 of file gzp.h.

#define GZP_USER_DATA_PACKET_OVERHEAD   ( GZP_CMD_ENCRYPTED_USER_DATA_VALIDATION_ID + GZP_VALIDATION_ID_LENGTH)

Definition at line 81 of file gzp.h.

#define GZP_ENCRYPTED_USER_DATA_MAX_LENGTH   (17 - GZP_USER_DATA_PACKET_OVERHEAD)

Definition at line 83 of file gzp.h.

#define GZPAR_CMD_FETCH_RESP_PAYLOAD_LENGTH   1

Definition at line 90 of file gzp.h.

#define GZP_CMD_HOST_ADDRESS_RESP_ADDRESS   1

Definition at line 97 of file gzp.h.

#define GZP_CMD_HOST_ADDRESS_RESP_PAYLOAD_LENGTH   (GZP_CMD_HOST_ADDRESS_RESP_ADDRESS + GZP_SYSTEM_ADDRESS_WIDTH)

Definition at line 98 of file gzp.h.

#define GZP_CMD_HOST_ID_FETCH_RESP_VALIDATION_ID   1

Definition at line 105 of file gzp.h.

#define GZP_CMD_HOST_ID_FETCH_RESP_STATUS   (GZP_CMD_HOST_ID_FETCH_RESP_VALIDATION_ID + GZP_VALIDATION_ID_LENGTH)

Definition at line 106 of file gzp.h.

#define GZP_CMD_HOST_ID_FETCH_RESP_HOST_ID   (GZP_CMD_HOST_ID_FETCH_RESP_STATUS + 1)

Definition at line 107 of file gzp.h.

#define GZP_CMD_HOST_ID_FETCH_RESP_PAYLOAD_LENGTH   (GZP_CMD_HOST_ID_FETCH_RESP_HOST_ID + GZP_HOST_ID_LENGTH)

Definition at line 108 of file gzp.h.

#define GZP_CMD_KEY_UPDATE_PREPARE_RESP_SESSION_TOKEN   1

Definition at line 115 of file gzp.h.

#define GZP_CMD_KEY_UPDATE_PREPARE_RESP_PAYLOAD_LENGTH   (GZP_CMD_KEY_UPDATE_PREPARE_RESP_SESSION_TOKEN + GZP_SESSION_TOKEN_LENGTH)

Definition at line 116 of file gzp.h.

#define GZP_CMD_ENCRYPTED_USER_DATA_RESP_SESSION_TOKEN   1

Definition at line 123 of file gzp.h.

#define GZP_CMD_ENCRYPTED_USER_DATA_RESP_VALIDATION_ID   (GZP_CMD_ENCRYPTED_USER_DATA_RESP_SESSION_TOKEN + GZP_SESSION_TOKEN_LENGTH)

Definition at line 124 of file gzp.h.

#define GZP_CMD_ENCRYPTED_USER_DATA_RESP_PAYLOAD_LENGTH   (GZP_CMD_ENCRYPTED_USER_DATA_RESP_VALIDATION_ID + GZP_VALIDATION_ID_LENGTH)

Definition at line 125 of file gzp.h.


Enumeration Type Documentation

Enumerator:
GZP_ID_EXCHANGE 
GZP_KEY_EXCHANGE 
GZP_DATA_EXCHANGE 

Definition at line 147 of file gzp.h.

{
  GZP_ID_EXCHANGE,    // "Secret key" only
  GZP_KEY_EXCHANGE,   // "Secret key" and "Host ID"
  GZP_DATA_EXCHANGE   // "Dynamic key" and "Host ID"
} gzp_key_select_t;
enum gzp_cmd_t
Enumerator:
GZP_CMD_HOST_ADDRESS_REQ 
GZP_CMD_HOST_ADDRESS_FETCH 
GZP_CMD_HOST_ID_REQ 
GZP_CMD_HOST_ID_FETCH 
GZP_CMD_KEY_UPDATE_PREPARE 
GZP_CMD_KEY_UPDATE 
GZP_CMD_ENCRYPTED_USER_DATA 
GZP_CMD_FETCH_RESP 
GZP_CMD_HOST_ADDRESS_RESP 
GZP_CMD_HOST_ID_FETCH_RESP 
GZP_CMD_KEY_UPDATE_PREPARE_RESP 
GZP_CMD_ENCRYPTED_USER_DATA_RESP 

Definition at line 154 of file gzp.h.

Enumerator:
GZP_ID_RESP_PENDING 
GZP_ID_RESP_GRANTED 
GZP_ID_RESP_REJECTED 
GZP_ID_RESP_FAILED 

Definition at line 170 of file gzp.h.


Function Documentation

void gzp_crypt_set_session_token ( const uint8_t *  token )

Function for writing global variable gzp_session_token.

Definition at line 138 of file gzp.c.

{
  memcpy(gzp_session_token, (void const*)token, GZP_SESSION_TOKEN_LENGTH);
}
void gzp_crypt_set_dyn_key ( const uint8_t *  src_key )

Function for writing global variable gzp_dyn_key.

Definition at line 143 of file gzp.c.

{
  memcpy(gzp_dyn_key, (void const*)key, GZP_DYN_KEY_LENGTH); 
}
void gzp_crypt_get_session_token ( uint8_t *  dst_token )

Function for reading global variable gzp_session_token.

Definition at line 148 of file gzp.c.

{
  memcpy(dst_token, (void const*)gzp_session_token, GZP_SESSION_TOKEN_LENGTH);
}
void gzp_crypt_get_dyn_key ( uint8_t *  dst_key )

Function for reading global variable gzp_dyn_key.

Definition at line 153 of file gzp.c.

{
  memcpy(dst_key, (void const*)gzp_dyn_key, GZP_DYN_KEY_LENGTH); 
}
_Bool gzp_get_host_id ( uint8_t *  dst )

Function reading current "Host ID". On Host, returns false if host ID not yet generated.

Definition at line 550 of file gzp_device.c.

{
  memcpy(dst_id, gzp_host_id, GZP_HOST_ID_LENGTH);
  return true;
}
void gzp_crypt_select_key ( gzp_key_select_t  key_select )

Function selecting what key-set that should be used when encrypting data using gzp_crypt().

Definition at line 158 of file gzp.c.

{
  gzp_key_select = key_select;
}
void gzp_crypt ( uint8_t *  dst,
const uint8_t *  src,
uint8_t  length 
)

Function for encypting / decrypting data.

The current "session token" will be used as initialization vector (IV). The AES key to be used is selected by gzp_crypt_select_key().

Definition at line 163 of file gzp.c.

{
  uint8_t i;
  uint8_t key[16];
  uint8_t iv[16];

  // Build AES key based on "gzp_key_select"
  switch(gzp_key_select)
  {
    case GZP_ID_EXCHANGE:
      memcpy(key, (void const*)gzp_secret_key, 16);
      break;
    case GZP_KEY_EXCHANGE:
      memcpy(key, (void const*)gzp_secret_key, 16);
      gzp_get_host_id(key);
      break;
    case GZP_DATA_EXCHANGE:
      memcpy(key, (void const*)gzp_secret_key, 16);
      memcpy(key, (void const*)gzp_dyn_key, GZP_DYN_KEY_LENGTH);
      break;
    default: 
      break;
  }  
  
  // Build init vector from "gzp_session_token"
  for(i = 0; i < 16; i++)
  {
    if(i < GZP_SESSION_TOKEN_LENGTH)
    {
      iv[i] = gzp_session_token[i];
    }
    else
    {
      iv[i] = 0;
    }
  }

  // Set up hal_aes using new key and init vector
  hal_aes_setup(false, ECB, key, NULL); // Note, here we skip the IV as we use ECB mode

  // Encrypt IV using ECB mode
  hal_aes_crypt(iv, iv);

  // Encrypt data by XOR'ing with AES output
  gzp_xor_cipher(dst, src, iv, length);
}
_Bool gzp_validate_id ( const uint8_t *  src_id )

Function for comparing *src_id with a pre-defined validation ID.

Returns true if *src_id equals the pre-defined ID.

Definition at line 128 of file gzp.c.

{
  return (memcmp(id, (void*)gzp_validation_id, GZP_VALIDATION_ID_LENGTH) == 0);
}
void gzp_add_validation_id ( uint8_t *  dst_id )

Function for adding the pre-defined validation ID to dst_id. GZP_VALIDATION_ID_LENGTH bytes will be added.

Definition at line 133 of file gzp.c.

{
  memcpy(dst, (void const*)gzp_validation_id, GZP_VALIDATION_ID_LENGTH); 
}
void gzp_random_numbers_generate ( uint8_t *  dst,
uint8_t  n 
)

Function generating and returning n random numbers to dst.

Definition at line 30 of file gzp_device_le1.c.

{
  uint8_t i;

  hal_rng_power_up(true);
  for(i = 0; i < n; i++) 
  {
    while(!hal_rng_data_ready())
    ;
    *(dst++) = hal_rng_read();
  }
  hal_rng_power_up(false);
}
void gzp_update_radio_params ( const uint8_t *  system_address )

Function updating channel subset and all pipe addresses from a single 5 byte address.

Definition at line 59 of file gzp.c.

{
  uint8_t i;
  uint8_t channels[GZLL_MAX_CHANNEL_TAB_SIZE];
  uint8_t temp_address[GZLL_ADDRESS_WIDTH];

  // Configure "global" pairing address
  gzll_set_address(HAL_NRF_PIPE0, (uint8_t const*)pairing_address);

  if(system_address != NULL)
  {
    for(i = 0; i < GZP_SYSTEM_ADDRESS_WIDTH; i++)
    {
      temp_address[i + 1] = *(system_address + i);  
    }

    // Configure address for pipe 1 - 5. Address byte set to equal pipe number.
    for(i = 1; i < 6; i++)
    {
      temp_address[0] = i; 
      gzll_set_address((hal_nrf_address_t)i, temp_address);     
    }
  }

  gzp_generate_channels(&channels[0], &temp_address[1], gzll_get_channel_tab_size());                       
  
  // Write generated channel subset to Gazell Link Layer   
  gzll_set_channels(&channels[0], gzll_get_channel_tab_size());
}
void gzp_generate_channels ( uint8_t *  ch_dst,
const uint8_t *  system_address,
uint8_t  channel_tab_size 
)

Function generating channel subset from a single 5 byte address.

Definition at line 89 of file gzp.c.

{
  uint8_t binsize, spacing, i;

  binsize = (GZP_CHANNEL_MAX - GZP_CHANNEL_MIN) / channel_tab_size;

  ch_dst[0] = GZP_CHANNEL_LOW;
  ch_dst[channel_tab_size - 1] = GZP_CHANNEL_HIGH;

  for(i = 1; i < (channel_tab_size - 1); i++)
  {
    ch_dst[i] = (binsize * i) + (system_address[i % 4] % binsize);  
  }

  for(i = 1; i < channel_tab_size; i++)
  {
    spacing = (ch_dst[i] - ch_dst[i - 1]); 
    if(spacing < GZP_CHANNEL_SPACING_MIN)
    {
      ch_dst[i] += (GZP_CHANNEL_SPACING_MIN - spacing); 
    }
  } 
}
void gzp_xor_cipher ( uint8_t *  dst,
const uint8_t *  src,
const uint8_t *  pad,
uint8_t  length 
)

Function byte-wise xor'ing pad with src and returns result in dst.

Definition at line 115 of file gzp.c.

{
  uint8_t i;

  for(i = 0; i < length; i++)
  {
    *dst = *src ^ *pad;
    dst++;
    src++;
    pad++;   
  }
}