Functions

gazell/common/gzp.c File Reference

Implementation of Gazell pairing library common Device and Host functions. More...

#include "gzp.h"
#include "gzll.h"
#include "hal_aes.h"
#include <string.h>
#include <stdint.h>
#include "memdefs.h"

Go to the source code of this file.

Functions

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)
_Bool gzp_validate_id (const uint8_t *id)
void gzp_add_validation_id (uint8_t *dst)
void gzp_crypt_set_session_token (const uint8_t *token)
void gzp_crypt_set_dyn_key (const uint8_t *key)
void gzp_crypt_get_session_token (uint8_t *dst_token)
void gzp_crypt_get_dyn_key (uint8_t *dst_key)
void gzp_crypt_select_key (gzp_key_select_t key_select)
void gzp_crypt (uint8_t *dst, const uint8_t *src, uint8_t length)

Detailed Description

Implementation of Gazell pairing library common Device and Host functions.

Definition in file gzp.c.


Function Documentation

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++;   
  }
}
_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_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); 
}
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);
}