示例#1
0
status_t g2_write_bin(g2_t g, uint8_t *data, int data_len)
{
	if(g == NULL) return ELEMENT_UNINITIALIZED;
//	int out_len = (FP_BYTES * 4) + 4;
	if(data_len < G2_LEN) return ELEMENT_INVALID_ARG_LEN;
	uint8_t d[G2_LEN+1];
	memset(d, 0, G2_LEN);

	fp_write_bin(d, FP_BYTES, g->x[0]);
	uint8_t *d1 = &(d[FP_BYTES + 1]);
	fp_write_bin(d1, FP_BYTES, g->x[1]);
	d1 = &(d1[FP_BYTES + 1]);
	fp_write_bin(d1, FP_BYTES, g->y[0]);
	d1 = &(d1[FP_BYTES + 1]);
	fp_write_bin(d1, FP_BYTES, g->y[1]);

	memcpy(data, d, data_len);

#ifdef DEBUG
	printf("%s: size for x & y :=> '%d'\n", __FUNCTION__, FP_BYTES);

	uint8_t *d2 = data;
	int i;
	for(i = 0; i < 4; i++) {
		print_as_hex(d2, FP_BYTES+1);
		d2 = &(d2[FP_BYTES + 1]);
	}
#endif
	memset(d, 0, G2_LEN);
	return ELEMENT_OK;
}
示例#2
0
void fp2_write_bin(uint8_t *bin, int len, fp2_t a, int pack) {
	fp2_t t;

	fp2_null(t);

	TRY {
		fp2_new(t);

		if (pack && fp2_test_uni(a)) {
			if (len != FP_BYTES + 1) {
				THROW(ERR_NO_BUFFER);	
			} else {
				fp2_pck(t, t);
				fp_write_bin(bin, FP_BYTES, a[0]);
				bin[FP_BYTES] = fp_get_bit(a[1], 0);
			}
		} else {
			if (len != 2 * FP_BYTES) {
				THROW(ERR_NO_BUFFER);
			} else {
				fp_write_bin(bin, FP_BYTES, a[0]);
				fp_write_bin(bin + FP_BYTES, FP_BYTES, a[1]);
			}
		}
	} CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp2_free(t);
	}
}
示例#3
0
void fp3_write_bin(uint8_t *bin, int len, fp3_t a) {
	if (len != 3 * FP_BYTES) {
		THROW(ERR_NO_BUFFER);
	}
	fp_write_bin(bin, FP_BYTES, a[0]);
	fp_write_bin(bin + FP_BYTES, FP_BYTES, a[1]);
	fp_write_bin(bin + 2 * FP_BYTES, FP_BYTES, a[2]);
}
示例#4
0
cbor_item_t* relic_fp2cbor(const fp_t n) {
	int size = FP_BYTES;
	uint8_t* data = (uint8_t*)malloc(size);
	cbor_item_t* ret = NULL;
	fp_write_bin(data, size, n);
	ret = cbor_build_bytestring(data, size);
	free(data);
	return ret;
}
示例#5
0
void ep_write_bin(uint8_t *bin, int len, const ep_t a, int pack) {
	ep_t t;

	ep_null(t);

	if (ep_is_infty(a)) {
		if (len != 1) {
			THROW(ERR_NO_BUFFER);
		} else {
			bin[0] = 0;
			return;
		}
	}

	TRY {
		ep_new(t);

		ep_norm(t, a);

		if (pack) {
			if (len != FP_BYTES + 1) {
				THROW(ERR_NO_BUFFER);	
			} else {
				ep_pck(t, t);
				bin[0] = 2 | fp_get_bit(t->y, 0);
				fp_write_bin(bin + 1, FP_BYTES, t->x);
			}
		} else {
			if (len != 2 * FP_BYTES + 1) {
				THROW(ERR_NO_BUFFER);
			} else {
				bin[0] = 4;
				fp_write_bin(bin + 1, FP_BYTES, t->x);
				fp_write_bin(bin + FP_BYTES + 1, FP_BYTES, t->y);
			}
		}
	} CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		ep_free(t);
	}
}
示例#6
0
status_t g1_write_bin(g1_t g, uint8_t *data, int data_len)
{
	if(g == NULL) return ELEMENT_UNINITIALIZED;
	if(data_len < G1_LEN) return ELEMENT_INVALID_ARG_LEN;
	uint8_t *d = data;
	memset(d, 0, G1_LEN);

	fp_write_bin(d, FP_BYTES, g->x);
	fp_write_bin(&(d[FP_BYTES + 1]), FP_BYTES, g->y);

#ifdef DEBUG
	printf("%s: size for x & y :=> '%d'\n", __FUNCTION__, FP_BYTES);

	uint8_t *d2 = data;
	int i;
	for(i = 0; i < 2; i++) {
		print_as_hex(d2, FP_BYTES+1);
		d2 = &(d2[FP_BYTES + 1]);
	}
#endif

	return ELEMENT_OK;
}
示例#7
0
status_t gt_write_bin(gt_t g, uint8_t *data, int data_len)
{
	if(g == NULL) return ELEMENT_UNINITIALIZED;
	if(data_len < GT_LEN) return ELEMENT_INVALID_ARG_LEN;
	uint8_t d[GT_LEN+1];
	uint8_t *d1 = NULL;
	memset(d, 0, GT_LEN);

#ifdef DEBUG
	printf("%s: size for x & y :=> '%d'\n", __FUNCTION__, FP_BYTES);
#endif
	// write the x-coordinate
	fp_write_bin(d, FP_BYTES, g[0][0][0]);
	d1 = &(d[FP_BYTES + 1]);
	fp_write_bin(d1, FP_BYTES, g[0][0][1]);
	d1 = &(d1[FP_BYTES + 1]);
	fp_write_bin(d1, FP_BYTES, g[0][1][0]);
	d1 = &(d1[FP_BYTES + 1]);
	fp_write_bin(d1, FP_BYTES, g[0][1][1]);
	d1 = &(d1[FP_BYTES + 1]);
	fp_write_bin(d1, FP_BYTES, g[0][2][0]);
	d1 = &(d1[FP_BYTES + 1]);
	fp_write_bin(d1, FP_BYTES, g[0][2][1]);
	d1 = &(d1[FP_BYTES + 1]);
	fp_write_bin(d1, FP_BYTES, g[1][0][0]);
	d1 = &(d1[FP_BYTES + 1]);
	fp_write_bin(d1, FP_BYTES, g[1][0][1]);
	d1 = &(d1[FP_BYTES + 1]);
	fp_write_bin(d1, FP_BYTES, g[1][1][0]);
	d1 = &(d1[FP_BYTES + 1]);
	fp_write_bin(d1, FP_BYTES, g[1][1][1]);
	d1 = &(d1[FP_BYTES + 1]);
	fp_write_bin(d1, FP_BYTES, g[1][2][0]);
	d1 = &(d1[FP_BYTES + 1]);
	fp_write_bin(d1, FP_BYTES, g[1][2][1]);

	memcpy(data, d, data_len);

	uint8_t *d2 = data;
	int i;
	for(i = 0; i < 12; i++) {
		print_as_hex(d2, FP_BYTES+1);
		d2 = &(d2[FP_BYTES + 1]);
	}

	memset(d, 0, GT_LEN);
	return ELEMENT_OK;
}