示例#1
0
bool unit_test_chacha20(){
  int a;
  bool pass = true;

  uint8_t ciphertext[114];
  memset(ciphertext, 0, 114 * sizeof ciphertext[0]);
  uint32_t counter = (uint32_t )1;
  chacha20(ciphertext,chacha_plaintext,114, chacha_key, chacha_nonce, counter);

    a = memcmp(ciphertext, chacha_ciphertext, 114 * sizeof (uint8_t));
    if (a != 0){
      pass = false;
      printf("Chacha20 failed on RFC test of size 114\n.");
    }
  return pass;
}
示例#2
0
// set best knock win/chan/start/stop
mote_t mote_knock(mote_t m, knock_t k, uint64_t from)
{
  uint8_t pad[8];
  uint8_t nonce[8];
  uint64_t offset, start, stop;
  uint32_t win, lwin;
  epoch_t e;
  medium_t medium;
  if(!m || !k || !k->com) return LOG("bad args");

  k->epoch = NULL;
  k->mote = m;
  medium = k->com->medium;

  // get the best one
  for(e=m->epochs;e;e=e->next)
  {
    // normalize nonce
    memset(nonce,0,8);
    switch(e->type)
    {
      case PING:
        win = 0;
        e->base = from; // is always now
        break;
      case ECHO:
        win = 1;
        break;
      case PAIR:
      case LINK:
        win = ((from - e->base) / EPOCH_WINDOW);
        break;
      default :
        continue;
    }
    if(!e->base) continue;

    lwin = util_sys_long(win);
    memset(nonce,0,8);
    memcpy(nonce,&lwin,4);
  
    // ciphertext the pad
    memset(pad,0,8);
    chacha20(e->secret,nonce,pad,8);
    
    offset = util_sys_long((unsigned long)(pad[2])) % (EPOCH_WINDOW - medium->min);
    start = e->base + (EPOCH_WINDOW*win) + offset;
    stop = start + medium->min;
    if(!k->epoch || stop < k->stop)
    {
      // rx never trumps tx
      if(k->epoch && k->epoch->dir == TX && e->dir == RX) continue;
      k->epoch = e;
      k->win = win;
      k->chan = util_sys_short((unsigned short)(pad[0])) % medium->chans;
      k->start = start;
      k->stop = stop;
    }
  }
  
  return m;
}