Example #1
0
/*
 * Convert a uuid to a "double" value for estimating sizes of ranges.
 */
static double
uuid_2_double(const pg_uuid_t *u)
{
	uint64		uu[2];
	const double two64 = 18446744073709551616.0;		/* 2^64 */

	/* Source data may not be suitably aligned, so copy */
	memcpy(uu, u->data, UUID_LEN);

	/*
	 * uuid values should be considered as big-endian numbers, since that
	 * corresponds to how memcmp will compare them.  On a little-endian
	 * machine, byte-swap each half so we can use native uint64 arithmetic.
	 */
#ifndef WORDS_BIGENDIAN
	uu[0] = BSWAP64(uu[0]);
	uu[1] = BSWAP64(uu[1]);
#endif

	/*
	 * 2^128 is about 3.4e38, which in theory could exceed the range of
	 * "double" (POSIX only requires 1e37).  To avoid any risk of overflow,
	 * put the decimal point between the two halves rather than treating the
	 * uuid value as a 128-bit integer.
	 */
	return (double) uu[0] + (double) uu[1] / two64;
}
Example #2
0
static void check_dtls_window_epoch_higher(void **glob_state)
{
	struct record_parameters_st state;
	uint64 t;

	RESET_WINDOW;
	SET_WINDOW_NEXT(LARGE_INT-1);
	SET_WINDOW_LAST_RECV(LARGE_INT);

	uint64_set(&t, BSWAP64(LARGE_INT));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64((LARGE_INT+8)|0x1000000000000LL));
	assert_int_equal(_dtls_record_check(&state, &t), -1);
}
Example #3
0
void StreamPeer::put_64(int64_t p_val) {

	if (big_endian) {
		p_val = BSWAP64(p_val);
	}
	uint8_t buf[8];
	encode_uint64(p_val, buf);
	put_data(buf, 8);
}
Example #4
0
void StreamPeer::put_double(double p_val) {

	uint8_t buf[8];
	encode_double(p_val, buf);
	if (big_endian) {
		uint64_t *p64 = (uint64_t *)buf;
		*p64 = BSWAP64(*p64);
	}
	put_data(buf, 8);
}
Example #5
0
int64_t StreamPeer::get_64() {

	uint8_t buf[8];
	get_data(buf, 8);
	uint64_t r = decode_uint64(buf);
	if (big_endian) {
		r = BSWAP64(r);
	}
	return r;
}
Example #6
0
static void check_dtls_window_uninit_large(void **glob_state)
{
	struct record_parameters_st state;
	uint64 t;

	RESET_WINDOW;

	uint64_set(&t, BSWAP64(LARGE_INT+1+64));

	assert_int_equal(_dtls_record_check(&state, &t), 0);
}
Example #7
0
static void check_dtls_window_uninit_very_large(void **glob_state)
{
	struct record_parameters_st state;
	uint64 t;

	RESET_WINDOW;

	uint64_set(&t, BSWAP64(INT_OVER_32_BITS));

	assert_int_equal(_dtls_record_check(&state, &t), 0);
}
Example #8
0
float StreamPeer::get_double() {

	uint8_t buf[8];
	get_data(buf, 8);

	if (big_endian) {
		uint64_t *p64 = (uint64_t *)buf;
		*p64 = BSWAP64(*p64);
	}

	return decode_double(buf);
}
Example #9
0
static void check_dtls_window_epoch_lower(void **glob_state)
{
	struct record_parameters_st state;
	uint64 t;

	RESET_WINDOW;
	uint64_set(&t, BSWAP64(0x1000000000000LL));

	state.epoch = 1;
	SET_WINDOW_NEXT(0x1000000000000LL);
	SET_WINDOW_LAST_RECV((0x1000000000000LL) + 1);

	uint64_set(&t, BSWAP64(2 | 0x1000000000000LL));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64(3 | 0x1000000000000LL));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64(5));
	assert_int_equal(_dtls_record_check(&state, &t), -1);
}
Example #10
0
static void check_dtls_window_dup3(void **glob_state)
{
	struct record_parameters_st state;
	uint64 t;

	RESET_WINDOW;
	SET_WINDOW_NEXT(LARGE_INT-1);
	SET_WINDOW_LAST_RECV(LARGE_INT);

	uint64_set(&t, BSWAP64(LARGE_INT));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64(LARGE_INT+16));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64(LARGE_INT+15));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64(LARGE_INT+14));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64(LARGE_INT+5));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64(LARGE_INT+5));
	assert_int_equal(_dtls_record_check(&state, &t), -3);
}
Example #11
0
static void check_dtls_window_91(void **glob_state)
{
	struct record_parameters_st state;
	uint64 t;

	RESET_WINDOW;
	SET_WINDOW_NEXT(0);
	SET_WINDOW_LAST_RECV(9);

	uint64_set(&t, BSWAP64(1));

	assert_int_equal(_dtls_record_check(&state, &t), 0);
}
Example #12
0
static void check_dtls_window_very_large_outside(void **glob_state)
{
	struct record_parameters_st state;
	uint64 t;

	RESET_WINDOW;
	SET_WINDOW_NEXT(INT_OVER_32_BITS);
	SET_WINDOW_LAST_RECV(INT_OVER_32_BITS+1);

	uint64_set(&t, BSWAP64(INT_OVER_32_BITS+1+64));

	assert_int_equal(_dtls_record_check(&state, &t), 0);
}
Example #13
0
static void check_dtls_window_out_of_order(void **glob_state)
{
	struct record_parameters_st state;
	uint64 t;

	RESET_WINDOW;
	SET_WINDOW_NEXT(LARGE_INT-1);
	SET_WINDOW_LAST_RECV(LARGE_INT);

	uint64_set(&t, BSWAP64(LARGE_INT));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64(LARGE_INT+8));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64(LARGE_INT+7));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64(LARGE_INT+6));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64(LARGE_INT+5));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64(LARGE_INT+4));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64(LARGE_INT+3));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64(LARGE_INT+2));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64(LARGE_INT+1));
	assert_int_equal(_dtls_record_check(&state, &t), 0);

	uint64_set(&t, BSWAP64(LARGE_INT+9));
	assert_int_equal(_dtls_record_check(&state, &t), 0);
}
Example #14
0
static void check_dtls_window_skip3(void **glob_state)
{
	struct record_parameters_st state;
	uint64 t;
	unsigned i;

	RESET_WINDOW;
	SET_WINDOW_NEXT(0);
	SET_WINDOW_LAST_RECV(1);

	for (i=5;i<256;i+=2) {
		uint64_set(&t, BSWAP64(i));
		assert_int_equal(_dtls_record_check(&state, &t), 0);
	}
}
Example #15
0
static inline int blake512_compress( state * state, const u8 * datablock ) 
{

  __m128i row1l;
  __m128i row2l;
  __m128i row3l;
  __m128i row4l;
  u64 row1hl, row1hh;
  u64 row2hl, row2hh;
  u64 row3hl, row3hh;
  u64 row4hl, row4hh;

  const __m128i r16 = _mm_setr_epi8(2,3,4,5,6,7,0,1,10,11,12,13,14,15,8,9);
  const __m128i u8to64 = _mm_set_epi8(8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7);

  union
  {
    __m128i u128[8];
    u64     u64[16];
  } m;

  __m128i t0, t1, t2, t3, t4, t5, t6, t7;
  u64     u0, u1, u2, u3;
  __m128i b0;
  u64 b1l, b1h;

  m.u128[0] = _mm_loadu_si128((__m128i*)(datablock +   0));
  m.u128[1] = _mm_loadu_si128((__m128i*)(datablock +  16));
  m.u128[2] = _mm_loadu_si128((__m128i*)(datablock +  32));
  m.u128[3] = _mm_loadu_si128((__m128i*)(datablock +  48));
  m.u128[4] = _mm_loadu_si128((__m128i*)(datablock +  64));
  m.u128[5] = _mm_loadu_si128((__m128i*)(datablock +  80));
  m.u128[6] = _mm_loadu_si128((__m128i*)(datablock +  96));
  m.u128[7] = _mm_loadu_si128((__m128i*)(datablock + 112));

  m.u128[0] = BSWAP64(m.u128[0]);
  m.u128[1] = BSWAP64(m.u128[1]);
  m.u128[2] = BSWAP64(m.u128[2]);
  m.u128[3] = BSWAP64(m.u128[3]);
  m.u128[4] = BSWAP64(m.u128[4]);
  m.u128[5] = BSWAP64(m.u128[5]);
  m.u128[6] = BSWAP64(m.u128[6]);
  m.u128[7] = BSWAP64(m.u128[7]);

  row1l = _mm_load_si128((__m128i*)&state->h[0]);
  row1hl = state->h[2];
  row1hh = state->h[3];

  row2l = _mm_load_si128((__m128i*)&state->h[4]);
  row2hl = state->h[6];
  row2hh = state->h[7];

  row3l = _mm_set_epi64x(0x13198A2E03707344ULL, 0x243F6A8885A308D3ULL);
  row3hl = 0xA4093822299F31D0ULL;
  row3hh = 0x082EFA98EC4E6C89ULL;

  row4l = _mm_set_epi64x(0xBE5466CF34E90C6CULL, 0x452821E638D01377ULL);
  row4hl = 0xC0AC29B7C97C50DDULL;
  row4hh = 0x3F84D5B5B5470917ULL;

  if(!state->nullt)
  {
  	row4l = _mm_xor_si128(row4l, _mm_set1_epi64x(state->t[0]));
    row4hl ^= state->t[1];
    row4hh ^= state->t[1];
  }

  ROUND( 0);
  ROUND( 1);
  ROUND( 2);
  ROUND( 3);
  ROUND( 4);
  ROUND( 5);
  ROUND( 6);
  ROUND( 7);
  ROUND( 8);
  ROUND( 9);
  ROUND(10);
  ROUND(11);
  ROUND(12);
  ROUND(13);
  ROUND(14);
  ROUND(15);

  row1l = _mm_xor_si128(row3l,row1l);
  row1hl ^= row3hl;
  row1hh ^= row3hh;

  _mm_store_si128((__m128i*)&state->h[0], _mm_xor_si128(row1l, _mm_load_si128((__m128i*)&state->h[0])));
  state->h[2] ^= row1hl;
  state->h[3] ^= row1hh;

  row2l = _mm_xor_si128(row4l,row2l);
  row2hl ^= row4hl;
  row2hh ^= row4hh;

  _mm_store_si128((__m128i*)&state->h[4], _mm_xor_si128(row2l, _mm_load_si128((__m128i*)&state->h[4])));
  state->h[6] ^= row2hl;
  state->h[7] ^= row2hh;
  
  return 0;
}
Example #16
0
void PRESENT128table_core(const u8* plaintext, const u8* roundKeys128, u8* ciphertext)
{
	u64 * state, * roundKeys;

	/* cast variables */
	*((u64*)ciphertext) = BSWAP64(*((u64*)plaintext));
	state     = (u64 *)ciphertext;
	roundKeys = (u64 *)roundKeys128;

	/* round 1 */
	state[0] ^= roundKeys[0];
	PRESENTROUND(state[0]);

	/* round 2 */
	state[0] ^= roundKeys[1];
	PRESENTROUND(state[0]);

	/* round 3 */
	state[0] ^= roundKeys[2];
	PRESENTROUND(state[0]);

	/* round 4 */
	state[0] ^= roundKeys[3];
	PRESENTROUND(state[0]);

	/* round 5 */
	state[0] ^= roundKeys[4];
	PRESENTROUND(state[0]);

	/* round 6 */
	state[0] ^= roundKeys[5];
	PRESENTROUND(state[0]);

	/* round 7 */
	state[0] ^= roundKeys[6];
	PRESENTROUND(state[0]);

	/* round 8 */
	state[0] ^= roundKeys[7];
	PRESENTROUND(state[0]);

	/* round 9 */
	state[0] ^= roundKeys[8];
	PRESENTROUND(state[0]);

	/* round 10 */
	state[0] ^= roundKeys[9];
	PRESENTROUND(state[0]);

	/* round 11 */
	state[0] ^= roundKeys[10];
	PRESENTROUND(state[0]);

	/* round 12 */
	state[0] ^= roundKeys[11];
	PRESENTROUND(state[0]);

	/* round 13 */
	state[0] ^= roundKeys[12];
	PRESENTROUND(state[0]);

	/* round 14 */
	state[0] ^= roundKeys[13];
	PRESENTROUND(state[0]);

	/* round 15 */
	state[0] ^= roundKeys[14];
	PRESENTROUND(state[0]);

	/* round 16 */
	state[0] ^= roundKeys[15];
	PRESENTROUND(state[0]);

	/* round 17 */
	state[0] ^= roundKeys[16];
	PRESENTROUND(state[0]);

	/* round 18 */
	state[0] ^= roundKeys[17];
	PRESENTROUND(state[0]);

	/* round 19 */
	state[0] ^= roundKeys[18];
	PRESENTROUND(state[0]);

	/* round 20 */
	state[0] ^= roundKeys[19];
	PRESENTROUND(state[0]);

	/* round 21 */
	state[0] ^= roundKeys[20];
	PRESENTROUND(state[0]);

	/* round 22 */
	state[0] ^= roundKeys[21];
	PRESENTROUND(state[0]);

	/* round 23 */
	state[0] ^= roundKeys[22];
	PRESENTROUND(state[0]);

	/* round 24 */
	state[0] ^= roundKeys[23];
	PRESENTROUND(state[0]);

	/* round 25 */
	state[0] ^= roundKeys[24];
	PRESENTROUND(state[0]);

	/* round 26 */
	state[0] ^= roundKeys[25];
	PRESENTROUND(state[0]);

	/* round 27 */
	state[0] ^= roundKeys[26];
	PRESENTROUND(state[0]);

	/* round 28 */
	state[0] ^= roundKeys[27];
	PRESENTROUND(state[0]);

	/* round 29 */
	state[0] ^= roundKeys[28];
	PRESENTROUND(state[0]);

	/* round 30 */
	state[0] ^= roundKeys[29];
	PRESENTROUND(state[0]);

	/* round 31 */
	state[0] ^= roundKeys[30];
	PRESENTROUND(state[0]);

	/* last addRoundKey */
	state[0] ^= roundKeys[31];

	/* endianness handling */
	state[0] = BSWAP64(state[0]);

	return;
}
Example #17
0
void PRESENT128table_key_schedule(const u8* masterKey128, u8* roundKeys128)
{
	u64 currentKeyLow, currentKeyHigh;

	/* get low and high parts of master key */
	currentKeyHigh = BSWAP64(((u64 *)masterKey128)[0]);
	currentKeyLow = BSWAP64(((u64 *)masterKey128)[1]);

	/* get round key 0 and compute round key 1 */
	((u64 *)roundKeys128)[0] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 0);

	/* get round key 1 and compute round key 2 */
	((u64 *)roundKeys128)[1] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 1);

	/* get round key 2 and compute round key 3 */
	((u64 *)roundKeys128)[2] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 2);

	/* get round key 3 and compute round key 4 */
	((u64 *)roundKeys128)[3] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 3);

	/* get round key 4 and compute round key 5 */
	((u64 *)roundKeys128)[4] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 4);

	/* get round key 5 and compute round key 6 */
	((u64 *)roundKeys128)[5] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 5);

	/* get round key 6 and compute round key 7 */
	((u64 *)roundKeys128)[6] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 6);

	/* get round key 7 and compute round key 8 */
	((u64 *)roundKeys128)[7] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 7);

	/* get round key 8 and compute round key 9 */
	((u64 *)roundKeys128)[8] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 8);

	/* get round key 9 and compute round key 10 */
	((u64 *)roundKeys128)[9] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 9);

	/* get round key 10 and compute round key 11 */
	((u64 *)roundKeys128)[10] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 10);

	/* get round key 11 and compute round key 12 */
	((u64 *)roundKeys128)[11] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 11);

	/* get round key 12 and compute round key 13 */
	((u64 *)roundKeys128)[12] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 12);

	/* get round key 13 and compute round key 14 */
	((u64 *)roundKeys128)[13] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 13);

	/* get round key 14 and compute round key 15 */
	((u64 *)roundKeys128)[14] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 14);

	/* get round key 15 and compute round key 16 */
	((u64 *)roundKeys128)[15] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 15);

	/* get round key 16 and compute round key 17 */
	((u64 *)roundKeys128)[16] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 16);

	/* get round key 17 and compute round key 18 */
	((u64 *)roundKeys128)[17] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 17);

	/* get round key 18 and compute round key 19 */
	((u64 *)roundKeys128)[18] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 18);

	/* get round key 19 and compute round key 20 */
	((u64 *)roundKeys128)[19] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 19);

	/* get round key 20 and compute round key 21 */
	((u64 *)roundKeys128)[20] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 20);

	/* get round key 21 and compute round key 22 */
	((u64 *)roundKeys128)[21] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 21);

	/* get round key 22 and compute round key 23 */
	((u64 *)roundKeys128)[22] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 22);

	/* get round key 23 and compute round key 24 */
	((u64 *)roundKeys128)[23] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 23);

	/* get round key 24 and compute round key 25 */
	((u64 *)roundKeys128)[24] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 24);

	/* get round key 25 and compute round key 26 */
	((u64 *)roundKeys128)[25] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 25);

	/* get round key 26 and compute round key 27 */
	((u64 *)roundKeys128)[26] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 26);

	/* get round key 27 and compute round key 28 */
	((u64 *)roundKeys128)[27] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 27);

	/* get round key 28 and compute round key 29 */
	((u64 *)roundKeys128)[28] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 28);

	/* get round key 29 and compute round key 30 */
	((u64 *)roundKeys128)[29] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 29);

	/* get round key 30 and compute round key 31 */
	((u64 *)roundKeys128)[30] = currentKeyHigh;
	PRESENTKS128(currentKeyLow, currentKeyHigh, 30);

	/* get round key 31 */
	((u64 *)roundKeys128)[31] = currentKeyHigh;

	return;
}