Example #1
0
static BOOL bign_keyunwrap(byte *X, byte *d, byte *untoken){
	Point q;
	REV_PI(X, q);
	BigInteger Q = bign_curve256v1::getQ();
	byte s0[32];
	memcpy(s0, d, sizeof s0);
	for (size_t jj = 0; jj < 32; jj += 4) change_endian(s0 + jj);
	BigInteger S0(s0, 32);
	S0 <<= 128;
	S0 %= Q;
	byte h_belt[32];
	memcpy(h_belt, H, 32);
	for (size_t jj = 0; jj <32; jj += 4) change_endian(h_belt + jj);
	BigInteger temp2(h_belt, 32);
	temp2 %= Q;
	byte _qq[32];
	memcpy(_qq, d + 32, sizeof _qq);
	for (size_t jj = 0; jj < 32; jj += 4) change_endian(_qq + jj);
	BigInteger S1(_qq, 32);
	if (S1 >= Q) return false;

	BigInteger rr = (temp2 + S1) % Q;
	BigInteger zero = BigInteger(0);
	Point G(zero, bign_curve256v1::getY());
	Point R = shamir(G, rr, q, S0);
	if (R.x == zero && R.y == zero) return false;
	byte toHash[108];
	byte bR[64];
	PI(bR, R);
	belt_hash(toHash, sizeof toHash, h_belt);
	for (size_t jj = 0; jj < 32; ++jj) if (h_belt[jj] != bR[jj]) return false;
	return true;
}
Example #2
0
static  BOOL bign_verify(byte *H, byte *_q, byte *S, uint32 size){
	Point q;
	REV_PI(_q, q);
	BigInteger Q = bign_curve256v1::getQ();
	byte s0[32];
	memcpy(s0, S, sizeof s0);
	for (size_t jj = 0; jj < 32; jj += 4) change_endian(s0 + jj);
	BigInteger S0(s0, 32);
	S0 <<= 128;
	S0 %= Q;
	byte _qq[32];
	memcpy(_qq, S + 32, sizeof _qq);
	for (size_t jj = 0; jj < 32; jj += 4) change_endian(_qq + jj);
	BigInteger S1(_qq, 32);
	if (S1 >= Q) return false;
	byte h_belt[32];
	memcpy(h_belt, H, 32);
	for (size_t jj = 0; jj <32; jj += 4) change_endian(h_belt + jj);
	BigInteger temp2(h_belt, 32);
	temp2 %= Q;
	BigInteger rr = (temp2 + S1) % Q;
	BigInteger zero = BigInteger(0);
	Point G(zero, bign_curve256v1::getY());
	Point R = shamir(G, rr, q, S0);
	if (R.x == zero && R.y == zero) return false;
	byte toHash[108];
	byte bR[64];
	PI(bR, R);
	memcpy(toHash, OID, sizeof OID);
	memcpy(toHash + sizeof OID, bR, sizeof bR);
	memcpy(toHash + sizeof OID + sizeof bR, H, 32);
	belt_hash(toHash, sizeof toHash, h_belt);
	for (size_t jj = 0; jj < 32; ++jj) if (h_belt[jj] != S[jj]) return false;
	return true;
}
Example #3
0
void camellia128_enc(void* block, const camellia128_ctx_t* s){

	#define BL (((uint64_t*)block)[0])
	#define BR (((uint64_t*)block)[1])
	/* endian adjustment */
	 /*BL*/
	 /* 1 2 3 4 5 6 7 8
	  *	8 7 6 5 4 3 2 1
	  */
	 
	uint64_t temp64;
	
	change_endian(&BL, 64/8);	
	change_endian(&BR, 64/8);
	
	/* Prewhitening */
	BL ^= s->kll;
	BR ^= s->klr;
	
	/* the first 6 */
	camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_POSTC1 , 0x33);
	/* FL injection  */
   camellia128_keyop((camellia128_ctx_t*)s, -1);
	BL = camellia_fl(BL, s->kal);
	BR = camellia_fl_inv(BR, s->kar);
   camellia128_keyop((camellia128_ctx_t*)s, -1);
	/* middle 6 */
	camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_INC2 , 0x34);
	/* FL injection  */
   camellia128_keyop((camellia128_ctx_t*)s, 1);
   	BL = camellia_fl(BL, s->kll);
	BR = camellia_fl_inv(BR, s->klr);
   camellia128_keyop((camellia128_ctx_t*)s, 1);
   /* last 6 */
	camellia_6rounds(s, &BL, &BR, KEY_ROL17 | KEY_DIR_NORM | KEY_POSTC2 , 0x0C);
	/* Postwhitening */
	BR ^= s->kal;
	BL ^= s->kar;
	
	temp64 = BR;
	BR = BL;
	BL = temp64;

	camellia128_keyop((camellia128_ctx_t*)s,1);
	
	change_endian(&BL, 64/8);	
	change_endian(&BR, 64/8);
		
	#undef BL
	#undef BR	
}
Example #4
0
static void PI(byte *A, Point & b) {
	uint32 ARRAY_OFFSET = 0x00;
	byte PI_HELPER[1 << 2] = {0x00};
	for (size_t jj = 0; jj < 8; ++jj) {
		memcpy(PI_HELPER, b.x.data + jj, 4);
		change_endian(PI_HELPER);
		for (size_t ll = 0; ll < 4; ++ll) A[ARRAY_OFFSET + (jj << 2) + ll] = PI_HELPER[ll];
	}
	ARRAY_OFFSET += 1 << 4;
	for (size_t jj = 0; jj < 8; ++jj) {
		memcpy(PI_HELPER, b.y.data + jj, 4);
		change_endian(PI_HELPER);
		for (size_t ll = 0; ll < 4; ++ll) A[ARRAY_OFFSET + (jj << 2) + ll] = PI_HELPER[ll];
	}
}
Example #5
0
static void REV_PI(byte *A, Point & b) {
	uint32 ARRAY_OFFSET = 0x00;
	byte PI_HELPER[1 << 2] = {0x00};
	for (size_t jj = 0; jj < 8; ++jj) {
		memcpy(PI_HELPER, A + ARRAY_OFFSET + (jj << 2), 4);
		change_endian(PI_HELPER);
		memcpy(b.x.data + jj, PI_HELPER, 4);
	}
	ARRAY_OFFSET += 1 << 4;
	for (size_t jj = 0; jj < 8; ++jj) {
		memcpy(PI_HELPER, A + ARRAY_OFFSET + (jj << 2), 4);
		change_endian(PI_HELPER);
		memcpy(b.y.data + jj, PI_HELPER, 4);
	}

	b.y.length = b.x.length = 8;
}
Example #6
0
idx3_ubyte_reader::idx3_ubyte_reader(const char* filename) : matrix_reader<float>(filename, true),w(0),h(0) {
	if (this->in.fail())    throw std::runtime_error("Input stream error 0");

	uint32_t x = 0;

	this->in.read((char*)&x,sizeof(x));
	if (this->in.fail())    throw std::runtime_error("Input stream error 1");

	this->in.read((char*)&x,sizeof(x));
	h = change_endian(x);

	this->in.read((char*)&x,sizeof(x));
	w = change_endian(x);
	this->in.read((char*)&x,sizeof(x));
	w *= change_endian(x);

	std::cout << "D=" << w << "\n";
}
Example #7
0
static  void bign_dh(byte *k, uint32 kSize, byte *P, byte* to){
	byte *_k = new byte[((kSize - 1 / 4) + 1) * 4];
	memset(_k, 0x00, sizeof _k);
	memcpy(_k, k, kSize);
	for (size_t jj = 0; jj < sizeof _k; jj += 4) change_endian(_k + jj);
	BigInteger K(_k, sizeof _k);
	K %= bign_curve256v1::getQ();
	Point PP;
	REV_PI(P, PP);
	Point ret = doit(PP, K);
	PI(to, ret);
}
Example #8
0
static void bign_sign(byte *H, byte *d, byte* to) {
	byte rand[32];
	gen_rnd_data(rand, 32);
	BigInteger k(rand, 32);
	BigInteger P = bign_curve256v1::getP();
	k %= P;
	byte dd[32];
	memcpy(dd, d, 32);
	for (size_t jj = 0; jj < 32; jj += 4) change_endian(dd + jj);
	BigInteger D(dd, 32);
	BigInteger Q = bign_curve256v1::getQ();
	Point G(BigInteger(0), bign_curve256v1::getY());
	Point R = doit(G, k);
	byte toHash[108];
	byte bR[64];
	PI(bR, R);
	memcpy(toHash, OID, sizeof OID);
	memcpy(toHash + sizeof OID, bR, sizeof bR);
	memcpy(toHash + sizeof OID + sizeof bR, H, 32);
	byte h_belt[32];
	belt_hash(toHash, sizeof toHash, h_belt);
	memcpy(to, h_belt, sizeof h_belt);
	for (size_t jj = 0; jj <32; jj += 4) change_endian(h_belt + jj);
	BigInteger temp1(h_belt, 32);
	temp1 <<= 128;
	temp1 %= Q;
	temp1 *= D %= Q;
	memcpy(h_belt, H, 32);
	for (size_t jj = 0; jj <32; jj += 4) change_endian(h_belt + jj);
	BigInteger temp2(h_belt, 32);
	temp2 %= Q;
	BigInteger temp3 = (k + Q - temp1 + Q - temp2) % Q;
	memcpy(h_belt, temp3.data, 32);
	for (size_t jj = 0; jj < 32; jj += 4) change_endian(h_belt + jj);
	memcpy(to + 32, h_belt, 32);
}
Example #9
0
static void encrypt_block(uint32* X, uint32 *Y, uint32 *sigma) {
	uint32 a = *X, b=*(X + 1), c= *(X + 2), d = *(X + 3),e;
	for (uint32 i = 1; i<= 8; ++i) {
		b ^= G<5>(plus_belt(a, *(sigma + eval(7*i - 6))));
		c ^= G<21>(plus_belt(d, *(sigma + eval(7*i-5))));
		a = minus_belt(a, G<13>(plus_belt(b, *(sigma + eval(7*i - 4)))));
		uint32 t_i = i;
		change_endian((byte*)&t_i);
		e =G<21>(plus_belt(plus_belt(b,c), *(sigma + eval(7 * i - 3)))) ^ t_i;
		b=plus_belt(b,e);
		c=minus_belt(c,e);
		d = plus_belt(d, G<13>(plus_belt(c, *(sigma + eval(7*i-2)))));
		b ^= G<21>(plus_belt(a, *(sigma + eval(7 * i - 1))));
		c ^= G<5>(plus_belt(d, *(sigma + eval(7*i))));
		a ^=b, b^=a, a^=b;
		c ^=d, d ^=c, c ^=d;
		b ^=c, c ^= b, b ^= c;
	}
	*Y = b;
	*(Y + 1) = d;
	*(Y + 2) = a;
	*(Y + 3) = c;
}
Example #10
0
// also works but sleeps between transfers
static snd_pcm_sframes_t a2dp_transfer2(snd_pcm_ioplug_t *io,
			const snd_pcm_channel_area_t *areas,
			snd_pcm_uframes_t offset, snd_pcm_uframes_t size)
{
	snd_pcm_a2dp_t *a2dp = io->private_data;
	char *buf;
	int len;
	struct media_packet_header packet_header;
	struct media_payload_header payload_header;
	int codesize,datatoread;
	unsigned long sleeptime;
	int written;
	
	struct timeval dt;
	

	codesize=a2dp->sbc.subbands*a2dp->sbc.blocks*a2dp->sbc.channels*2;
	datatoread=min(codesize,size*a2dp->frame_bytes);
	buf = (char *) areas->addr + (areas->first + areas->step * offset) / 8;
    	if(lenbufe<codesize){
		memcpy(bufe+lenbufe,buf,datatoread);
		lenbufe+=datatoread;
	}  
	else{datatoread=0;}

	if(lenbufe>=codesize){ //enough data to encode
		change_endian(bufe,codesize); // changing the endianness
		len = sbc_encode(&(a2dp->sbc), bufe, codesize); //encode
		memmove(bufe, bufe + len, lenbufe - len); //shift the bufe                                 
		lenbufe-=len;
		nbytes+=len;
		sleeptime += a2dp->sbc.duration;
		if (len <= 0)
			return len;
		if(a2dp->len + a2dp->sbc.len > 678)	{ // time to prepare and send the packet
			dt.tv_sec=0;
			dt.tv_usec=1000000*a2dp->sbc.subbands*a2dp->sbc.blocks*frame_count/io->rate;
			memset(&payload_header, 0, sizeof(payload_header));
			memset(&packet_header, 0, sizeof(packet_header));
			payload_header.frame_count=frame_count;
			packet_header.v = 2;
			packet_header.pt = 1;
			packet_header.sequence_number = htons(seq_num);
			packet_header.timestamp = htonl(timestamp);
			packet_header.ssrc = htonl(1);
			timestamp += (a2dp->sbc.blocks + 1)*4 * (a2dp->sbc.subbands + 1)*4;
			memcpy(a2dp->buf, &packet_header, sizeof(packet_header));
			memcpy(a2dp->buf + sizeof(packet_header), &payload_header, sizeof(payload_header));
			sleeptill(&tsend, &dt);
			if((written = write(a2dp->sk,a2dp->buf,a2dp->len)) != a2dp->len) {
				DBG("Wrote %d not %d bytes; errno %s(%d)", written, a2dp->len,
					strerror(errno), errno);
			}
			a2dp->len = sizeof(packet_header)+sizeof(payload_header);
			frame_count=0;
			sleeptime=0;
			seq_num++;
		}
		frame_count++;
		memcpy(a2dp->buf + a2dp->len, a2dp->sbc.data, a2dp->sbc.len);
		a2dp->len+=a2dp->sbc.len;
		if (a2dp->state == BT_CONNECTED)
			a2dp->num += len / a2dp->frame_bytes;
	}
	return datatoread / a2dp->frame_bytes;
}
Example #11
0
// transfers around correct time postions
static snd_pcm_sframes_t a2dp_transfer(snd_pcm_ioplug_t *io,
			const snd_pcm_channel_area_t *areas,
			snd_pcm_uframes_t offset, snd_pcm_uframes_t size)
{
	snd_pcm_a2dp_t *a2dp = io->private_data;
	char *buf;
	int len;
	struct media_packet_header packet_header;
	struct media_payload_header payload_header;
	int codesize,datatoread;
	unsigned long sleeptime;
	struct timeval dt;
	

	codesize=a2dp->sbc.subbands*a2dp->sbc.blocks*a2dp->sbc.channels*2; // size of data encoded by sbc_encode in one call
	datatoread=min(codesize,size*a2dp->frame_bytes); // amount of data to read
	buf = (char *) areas->addr + (areas->first + areas->step * offset) / 8;
    	if(lenbufe<codesize && lenbufe+datatoread<sizeof(bufe)){ // if not enough data in bufe to encode and there is space in bufe
		memcpy(bufe+lenbufe,buf,datatoread);// we read data to bufe
		lenbufe+=datatoread;
	}
	else{datatoread=0;}//nothing has been read

	if(lenbufe>=codesize && a2dp->len + a2dp->sbc.len < 678){ // if enough data in bufe to encode and not enough frame to fill up mtu: encoding
		change_endian(bufe,codesize); // changing the endianness
		len = sbc_encode(&(a2dp->sbc), bufe, codesize); //encode
		memmove(bufe, bufe + len, lenbufe - len); //shift the bufe                                 
		lenbufe-=len;
		nbytes+=len;
		sleeptime += a2dp->sbc.duration;
		if (len <= 0)
			return len;
		frame_count++;
		memcpy(a2dp->buf + a2dp->len, a2dp->sbc.data, a2dp->sbc.len); // copy encoded frames into a2dp->buf
		a2dp->len+=a2dp->sbc.len;
		if (a2dp->state == BT_CONNECTED)
			a2dp->num += len / a2dp->frame_bytes; //update pointer
			a2dp->num %=io->buffer_size;
	}		

	if(a2dp->len + a2dp->sbc.len > 678){ // if packet is formed
		dt.tv_usec=1000000*a2dp->sbc.subbands*a2dp->sbc.blocks*frame_count/io->rate; // time interval between transmitions
		dt.tv_sec=0;
		if(time_to_wait(&tsend, &dt)==0){ // time to send data
			memset(&payload_header, 0, sizeof(payload_header)); // fill up the headers
			memset(&packet_header, 0, sizeof(packet_header)); //---
			payload_header.frame_count=frame_count;
			packet_header.v = 2;
			packet_header.pt = 1;
			packet_header.sequence_number = htons(seq_num);
			packet_header.timestamp = htonl(timestamp);
			packet_header.ssrc = htonl(1);
			timestamp += (a2dp->sbc.blocks + 1)*4 * (a2dp->sbc.subbands + 1)*4;
			memcpy(a2dp->buf, &packet_header, sizeof(packet_header)); //copy the headers to buf
			memcpy(a2dp->buf + sizeof(packet_header), &payload_header, sizeof(payload_header));//---
			write(a2dp->sk,a2dp->buf,a2dp->len); // sending the packet
			a2dp->len = sizeof(packet_header)+sizeof(payload_header); //inital position in buf, just after headers
			frame_count=0;
			sleeptime=0;
			seq_num++;
		}else{usleep(1);}
	}
	return datatoread / a2dp->frame_bytes;
}
Example #12
0
 operator INT()
 {
   return change_endian(value);
 }
Example #13
0
 other_endian(const INT i = 0)
 {
   value = change_endian(i);
 }
Example #14
0
static void brng_hmac(byte *Sigma, byte *S, byte *to, uint32 size){
	uint32* sigma = (uint32*)Sigma;
	uint32 act_sz = ((size - 1) / 16 + 1) * 4;
	uint32 *X = new uint32[act_sz];
	uint32 *Y = new uint32[act_sz];
	uint32 byteSZ = act_sz << 2;
	memset(X, 0x00, sizeof X);
	for (size_t jj = 0; jj < act_sz; ++jj) X[jj] ^= X[jj];
	memcpy(X, S, size);
	for (size_t i = 0; i < byteSZ; i += 4) {
		byte*l = ((byte*)X) + i, 
			*r = ((byte*)X) + 3 + i;

		*r ^= *l, *l ^= *r, *r ^= *l;
		l = ((byte*)X) + i + 1, 
			r = ((byte*)X) + 2 + i;

		*r ^= *l, *l ^= *r, *r ^= *l;
	}
	for (size_t i = 0; i < 32; i += 4) {
		byte*l = ((byte*)sigma) + i, 
			*r = ((byte*)sigma) + 3 + i;

		*r ^= *l, *l ^= *r, *r ^= *l;
		l = ((byte*)sigma) + i + 1, 
			r = ((byte*)sigma) + 2 + i;

		*r ^= *l, *l ^= *r, *r ^= *l;
	}
	uint32 s[4], r[4];
	memset(s, 0, sizeof s);
	encrypt_block(s, r, sigma);

	for (size_t i = 0; i < act_sz - 4; i += 4) {
		for (size_t j = 0; j < 4; ++j) X[i + j] ^= s[j];
		encrypt_block(X + i, s, sigma);
	}
	uint32 diff = byteSZ - size;
	if (!diff) {
		phi1(r);
		for (size_t i = 0; i < 4; ++i) s[i] ^= r[i] ^ X[act_sz - 4 + i];
	} else {
		psi(X + act_sz - 4,16- diff);
		phi2(r);
		for (size_t i = 0; i < 4; ++i) s[i] ^= r[i] ^ X[act_sz - 4 + i];
	}
	encrypt_block(s, r, sigma);
	for (size_t jj = 0; jj < 4; ++jj) change_endian((byte*)(r + jj));
	memcpy(to, r, 8);
	for (size_t i = 0; i < 32; i += 4) {
		byte*l = ((byte*)sigma) + i, 
			*r = ((byte*)sigma) + 3 + i;

		*r ^= *l, *l ^= *r, *r ^= *l;
		l = ((byte*)sigma) + i + 1, 
			r = ((byte*)sigma) + 2 + i;

		*r ^= *l, *l ^= *r, *r ^= *l;
	}
	delete X;
	delete Y;
}