Пример #1
0
/*---------------------------------------------------------------------------*/
void
adaptivesec_aead(uint8_t *key, int shall_encrypt, uint8_t *result, int forward)
{
  uint8_t nonce[CCM_STAR_NONCE_LENGTH];
  uint8_t *m;
  uint8_t m_len;
  uint8_t *a;
  uint8_t a_len;

  ccm_star_packetbuf_set_nonce(nonce, forward);
  a = packetbuf_hdrptr();
  if(shall_encrypt) {
#if AKES_NBR_WITH_GROUP_KEYS && PACKETBUF_WITH_UNENCRYPTED_BYTES
    a_len = packetbuf_hdrlen() + packetbuf_attr(PACKETBUF_ATTR_UNENCRYPTED_BYTES);
#else /* AKES_NBR_WITH_GROUP_KEYS && PACKETBUF_WITH_UNENCRYPTED_BYTES */
    a_len = packetbuf_hdrlen();
#endif /* AKES_NBR_WITH_GROUP_KEYS && PACKETBUF_WITH_UNENCRYPTED_BYTES */
    m = a + a_len;
    m_len = packetbuf_totlen() - a_len;
  } else {
    a_len = packetbuf_totlen();
    m = NULL;
    m_len = 0;
  }

  AES_128_GET_LOCK();
  ADAPTIVESEC_SET_KEY(key);
  CCM_STAR.aead(nonce,
      m, m_len,
      a, a_len,
      result, adaptivesec_mic_len(),
      forward);
  AES_128_RELEASE_LOCK();
}
Пример #2
0
/*---------------------------------------------------------------------------*/
static int
aead(uint8_t hdrlen, int forward)
{
  uint8_t totlen;
  uint8_t nonce[CCM_STAR_NONCE_LENGTH];
  uint8_t *m;
  uint8_t m_len;
  uint8_t *a;
  uint8_t a_len;
  uint8_t *result;
  uint8_t generated_mic[MIC_LEN];
  uint8_t *mic;
  
  ccm_star_packetbuf_set_nonce(nonce, forward);
  totlen = packetbuf_totlen();
  a = packetbuf_hdrptr();
#if WITH_ENCRYPTION
  a_len = hdrlen;
  m = a + a_len;
  m_len = totlen - hdrlen;
#else /* WITH_ENCRYPTION */
  a_len = totlen;
  m = NULL;
  m_len = 0;
#endif /* WITH_ENCRYPTION */
  
  mic = a + totlen;
  result = forward ? mic : generated_mic;
  
  CCM_STAR.aead(nonce,
      m, m_len,
      a, a_len,
      result, MIC_LEN,
      forward);
  
  if(forward) {
    packetbuf_set_datalen(packetbuf_datalen() + MIC_LEN);
    return 1;
  } else {
    return (memcmp(generated_mic, mic, MIC_LEN) == 0);
  }
}