Beispiel #1
0
ssize_t curve25519_encode(struct curve25519_struct *curve, struct curve25519_proto *proto,
			  unsigned char *plaintext, size_t size, unsigned char **chipertext)
{
	int ret, i;
	ssize_t done = size;
	struct taia packet_taia;

	spinlock_lock(&curve->enc_lock);

	if (unlikely(size > curve->enc_buf_size)) {
		done = -ENOMEM;
		goto out;
	}

	taia_now(&packet_taia);
	taia_pack(proto->enonce + NONCE_OFFSET, &packet_taia);

	memset(curve->enc_buf, 0, curve->enc_buf_size);
	ret = crypto_box_afternm(curve->enc_buf, plaintext, size, proto->enonce, proto->key);
	if (unlikely(ret)) {
		done = -EIO;
		goto out;
	}

	fmemcpy(curve->enc_buf + crypto_box_boxzerobytes - NONCE_LENGTH,
	       proto->enonce + NONCE_OFFSET, NONCE_LENGTH);

	for (i = 0; i < crypto_box_boxzerobytes - NONCE_LENGTH; ++i)
		curve->enc_buf[i] = (uint8_t) secrand();

	(*chipertext) = curve->enc_buf;
out:
	spinlock_unlock(&curve->enc_lock);
	return done;
}
Beispiel #2
0
static int proto_encode(sigma_proto *instance, uint8_t* input, uint8_t* output, size_t len)
{
    sigma_proto_nacl* inst = (sigma_proto_nacl*) instance;

    uint8_t tempbufferinput[len + crypto_box_ZEROBYTES];

    bzero(tempbufferinput, crypto_box_ZEROBYTES);
    memcpy(tempbufferinput + crypto_box_ZEROBYTES, input, len);

    len += crypto_box_ZEROBYTES;

    taia_now(&inst->cdtaie);
    taia_pack(inst->encnonce + nonceoffset, &(inst->cdtaie));

    int result = crypto_box_afternm(
        output,
        tempbufferinput,
        len,
        inst->encnonce,
        ((sigma_proto_nacl*) instance)->precomp
    );

    if (result)
    {
        fprintf(stderr, "Encryption failed (length %u, given result %i)\n", (unsigned) len, result);
        errno = EINVAL;
        return -1;
    }

    memcpy(output, inst->encnonce + nonceoffset, noncelength);

    return len;
}
Beispiel #3
0
unsigned fmt_taia25(char *s, struct taia *t) {
	static char pack[TAIA_PACK];

	taia_pack(pack, t);
	*s++ = '@';
	bin2hex(s, pack, 12);
	return 25;
}
Beispiel #4
0
 std::string Taia::encode() {
   char pack[TAIA_PACK_SIZE];
   char buffer[TAI64N_ENCODE_SIZE + 1];
   taia_pack(pack, &data);
   tai64n_encode(buffer, pack);
   buffer[TAI64N_ENCODE_SIZE] = 0;
   return std::string(buffer);
 }
Beispiel #5
0
void modify_entry(struct cdbb *a, struct taia *k, char *v, size_t vs)
{
	char pk[TAIA_PACK];

	taia_pack(pk, k);
	cdbb_mod(a, pk, TAIA_PACK, v, vs);
	blog_modified(a);
}
int main() {
  char buf[TAI64NA_ENCODE_SIZE];
  taia_t t = TAIA_INIT;
  taia_t u = TAIA_INIT;
  char pt[TAIA_PACK_SIZE];
  char pu[TAIA_PACK_SIZE];
  
  t.sec = UINT64_MAX;
  t.nano = UINT32_MAX;
  t.atto = UINT32_MAX;
  
  int i;
  for (i = 0; i <= 32; i++) {
    taia_pack(pt, &t);
    tai64na_encode(buf, pt);
    tai64na_decode(pu, buf);
    taia_unpack(&u, pu);
    
    if (taia_cmp(&t, &u) != 0)
      return 1;
      
    t.sec >>= 2;
    t.nano >>= 1;
    t.atto >>= 1;
  }
  
  t.sec = UINT64_MAX;
  t.nano = UINT32_MAX;
  t.atto = UINT32_MAX;
  
  for (i = 0; i <= 32; i++) {
    taia_pack(pt, &t);
    tai64na_encode(buf, pt);
    tai64na_decode(pu, buf);
    taia_unpack(&u, pu);
    
    if (taia_cmp(&t, &u) != 0)
      return 1;
    t.sec <<= 2;
    t.nano <<= 1;
    t.atto <<= 1;
  }
  
  return 0;
}
Beispiel #7
0
static void blog_modified(struct cdbb *a)
{
	char pk[TAIA_PACK];
	struct taia t;

	taia_now(&t);
	taia_pack(pk, &t);
	cdbb_rep(a, DB_LAST_MODIFIED, 13, pk, TAIA_PACK);
}
Beispiel #8
0
int show_entry(struct cdbb *a, struct taia *k, struct nentry *n)
{
	int err;
	char pac[TAIA_PACK];

	taia_pack(pac, k);
	err = cdbb_read_nentry(a, pac, TAIA_PACK, n);

	return err;
}
Beispiel #9
0
void
profile(const char *s)
{
#ifdef ENABLE_PROFILE
	char buf[TAIA_PACK];
	struct taia t;

	taia_now(&t);
	taia_pack(buf,&t);
	logit(LOG_PROFILE, "PROFILE: %s @%s\n", s, buf); 
#endif
}
Beispiel #10
0
void pidchange(void)
{
  struct taia now;
  unsigned long u;

  taia_now(&now);
  taia_pack(status,&now);

  u = (unsigned long) pid;
  status[12] = u; u >>= 8;
  status[13] = u; u >>= 8;
  status[14] = u; u >>= 8;
  status[15] = u;
}
Beispiel #11
0
PyObject *pytaia_now_pack(PyObject *self){
  PyObject *ret;
  unsigned char *tpad;
  tpad = PyMem_Malloc(16);

  if (!tpad)
    return PyErr_NoMemory();

  taia_now(tpad);
  taia_pack(tpad,tpad);

  ret = PyBytes_FromStringAndSize((char *)tpad,16);
  PyMem_Free(tpad);
  return ret;}
Beispiel #12
0
static void pidchange()
{
	struct taia now;
	unsigned long u;

	taia_now(&now);
	taia_pack(status, &now);

	u = (unsigned long) pid;
	status[16] = u; u >>= 8;
	status[17] = u; u >>= 8;
	status[18] = u; u >>= 8;
	status[19] = u;
}
Beispiel #13
0
void delete_entry(struct cdbb *a, struct taia *k)
{
	char pk[TAIA_PACK];
	array dayidx;

	memset(&dayidx, 0, sizeof(array));
	fmt_day_idx(&dayidx, k);

	taia_pack(pk, k);

	cdbb_del(a, pk, TAIA_PACK);
	cdbb_del_val(a, dayidx.p, array_bytes(&dayidx), pk, TAIA_PACK);
	blog_modified(a);
}
Beispiel #14
0
int sv_stat_save(char *path, sv_stat_t *svst) {
  int fd;
  array_t new_path = ARRAY_INIT(1);
  int r;
  char status[SUPERVISE_STATUS_SIZE];
  uint32_t tmp;
  
  /* Pack the svstat data into the status byte array. */
  taia_pack(status, &svst->timestamp);
  tmp = svst->pid;
  ((uint32_t *)status)[3] = htobe32(tmp);
  status[16] = svst->paused;
  status[17] = svst->mode;
  
  /* Construct the path for the .new file into the array which *MUST* be freed
   * before returning. */
  array_append(&new_path, path, strlen(path));
  array_append(&new_path, SUPERVISE_STATUS_NEW_EXT, strlen(SUPERVISE_STATUS_NEW_EXT));
  array_append_null(&new_path);
  if (array_failed(&new_path)) {
    array_reset(&new_path);
    errno = ENOMEM;
    return -1;
  }
  
  /* Open the tmp file, write the status byte array to it, verify the entire
   * array was written, and finally rename it to status. */
  fd = open_trunc(array_start(&new_path));
  if (fd == -1) {
    array_reset(&new_path);
    return ESVOPENSTATNEW;
  }
  
  r = write(fd, status, SUPERVISE_STATUS_SIZE);
  if ((r == -1) || (r != SUPERVISE_STATUS_SIZE)) {
    close(fd);
    array_reset(&new_path);
    return ESVWRITESTATNEW;
  }
  close(fd);
  
  if (rename(array_start(&new_path), path) == -1) {
    array_reset(&new_path);
    return ESVWRITESTAT;
  }
  
  array_reset(&new_path);
  return 0;
}
Beispiel #15
0
void timestamp(char s[TIMESTAMP])
{
  struct taia now;
  char nowpack[TAIA_PACK];
  int i;

  taia_now(&now);
  taia_pack(nowpack,&now);

  s[0] = '@';
  for (i = 0;i < 12;++i) {
    s[i * 2 + 1] = hex[(nowpack[i] >> 4) & 15];
    s[i * 2 + 2] = hex[nowpack[i] & 15];
  }
}
Beispiel #16
0
void add_entry(struct cdbb *a, struct taia *k, char *v, size_t vs)
{
	char pk[TAIA_PACK];
	array dayidx;

	memset(&dayidx, 0, sizeof(array));
	fmt_day_idx(&dayidx, k);
	taia_pack(pk, k);

	/* entry + idx */
	cdbb_add(a, pk, TAIA_PACK, v, vs);
	cdbb_add(a, dayidx.p, array_bytes(&dayidx), pk, TAIA_PACK);

	blog_modified(a);
	array_reset(&dayidx);
}