Beispiel #1
0
void
xs_ed_init(xsMachine *the)
{
	ed_t *ed;
	mod_t *mod;	/* mod_t* */
	void *d;	/* kcl_int_t* */
	kcl_err_t err;

	if ((ed = crypt_malloc(sizeof(ed_t))) == NULL)
		kcl_throw_error(the, KCL_ERR_NOMEM);
	if ((err = kcl_ed_alloc(&ed->ctx)) != KCL_ERR_NONE) {
		crypt_free(ed);
		kcl_throw_error(the, err);
	}
	xsResult = xsGet(xsThis, xsID("mod"));
	mod = xsGetHostData(xsResult);
	xsResult = xsGet(xsThis, xsID("d"));
	d = xsGetHostData(xsResult);
	kcl_ed_init(ed->ctx, mod->ctx, d);

	ed->id_x = xsID("x");
	ed->id_y = xsID("y");
	ed->proto_int = xsGet(xsThis, xsID("_proto_int"));
	ed->proto_ecp = xsGet(xsThis, xsID("_proto_ecp"));
	xsSetHostData(xsThis, ed);
}
Beispiel #2
0
static kcl_ecp_t *
__arith_get_ecp(xsMachine *the, xsSlot *slot, xsSlot *proto)
{
	if (!xsIsInstanceOf(*slot, *proto))
		kcl_throw_error(the, KCL_ERR_NAN);
	return xsGetHostData(*slot);
}
Beispiel #3
0
void
xs_chacha_init(xsMachine *the)
{
	crypt_stream_t *stream = xsGetHostData(xsThis);
	int ac = xsToInteger(xsArgc);
	void *key, *iv;
	size_t keysize, ivsize;
	uint64_t counter = 0;
	kcl_err_t err;

	key = xsToArrayBuffer(xsArg(0));
	keysize = xsGetArrayBufferLength(xsArg(0));
	if (ac > 1 && xsTest(xsArg(1))) {
		iv = xsToArrayBuffer(xsArg(1));
		ivsize = xsGetArrayBufferLength(xsArg(1));
	}
	else {
		iv = NULL;
		ivsize = 0;
	}
	if (ac > 2) {
		switch (xsTypeOf(xsArg(2))) {
		case xsIntegerType:
		case xsNumberType:
			counter = xsToInteger(xsArg(2));	/* @@ take only 32bit */
			break;
		}
	}
	if ((err = kcl_chacha_init(&stream->ctx, key, keysize, iv, ivsize, counter)) != KCL_ERR_NONE)
		kcl_throw_error(the, err);
	stream->process = kcl_chacha_process;
	stream->finish = kcl_chacha_finish;
}
Beispiel #4
0
static void
z_call1(xsMachine *the, z_t *z, kcl_z_f1 f)
{
	kcl_int_t *a = arith_get_integer(z, xsArg(0));
	kcl_int_t *r = NULL;
	kcl_err_t err;

	err = (*f)(z->ctx, a, &r);
	if (err != KCL_ERR_NONE)
		kcl_throw_error(the, err);
	arith_set_integer(z, xsResult, r);
}
Beispiel #5
0
static void
z_call2d(xsMachine *the, z_t *z, kcl_z_f2d f)
{
	kcl_int_t *a = arith_get_integer(z, xsArg(0));
	xsIntegerValue b = xsToInteger(xsArg(1));
	kcl_int_t *r = NULL;
	kcl_err_t err;

	err = (*f)(z->ctx, a, (int)b, &r);
	if (err != KCL_ERR_NONE)
		kcl_throw_error(the, err);
	arith_set_integer(z, xsResult, r);
}
Beispiel #6
0
void
xs_z_toInteger(xsMachine *the)
{
	z_t *z = xsGetHostData(xsThis);
	char *digits = xsToString(xsArg(0));
	unsigned int radix = xsToInteger(xsArg(1));
	kcl_int_t *ai = NULL;
	kcl_err_t err;

	if ((err = kcl_z_str2i(z->ctx, &ai, digits, radix)) != KCL_ERR_NONE)
		kcl_throw_error(the, err);
	arith_set_integer(z, xsResult, ai);
}
Beispiel #7
0
void
xs_z_init(xsMachine *the)
{
	z_t *z;
	kcl_err_t err;
	static kcl_error_callback_t cb;

	if ((z = crypt_malloc(sizeof(z_t))) == NULL)
		kcl_throw_error(the, KCL_ERR_NOMEM);
	if ((err = kcl_z_alloc(&z->ctx)) != KCL_ERR_NONE) {
		crypt_free(z);
		kcl_throw_error(the, err);
	}
	z->proto_int = xsGet(xsThis, xsID("_proto_int"));
	cb.f = kcl_z_error_callback;
	cb.closure = the;
	if ((err = kcl_z_init(z->ctx, &cb)) != KCL_ERR_NONE) {
		kcl_z_dispose(z->ctx);
		crypt_free(z);
		kcl_throw_error(the, err);
	}
	xsSetHostData(xsThis, z);
}
Beispiel #8
0
void
xs_ed_add(xsMachine *the)
{
	ed_t *ed = xsGetHostData(xsThis);
	kcl_ecp_t *a, *b, *r;
	kcl_err_t err;

	xsVars(1);
	a = arith_get_ecp(ed, xsArg(0));
	b = arith_get_ecp(ed, xsArg(1));
	(void)((err = kcl_ecp_alloc(&r)) || (err = kcl_ed_add(ed->ctx, a, b, r)));
	if (err != KCL_ERR_NONE)
		kcl_throw_error(the, err);
	arith_set_ecp(ed, xsResult, r);
}
Beispiel #9
0
void
xs_sha256_init(xsMachine *the)
{
	crypt_digest_t *digest = xsGetHostData(xsThis);
	kcl_err_t err;

	if ((err = kcl_sha256_create(&digest->ctx)) != KCL_ERR_NONE)
		kcl_throw_error(the, err);
	digest->update = kcl_sha256_update;
	digest->init = kcl_sha256_init;
	digest->result = kcl_sha256_result;
	digest->finish = kcl_sha256_finish;
	kcl_sha256_size(digest->ctx, &digest->blockSize, &digest->outputSize);
	kcl_sha256_init(digest->ctx);
}
Beispiel #10
0
void
xs_z_toString(xsMachine *the)
{
	z_t *z = xsGetHostData(xsThis);
	kcl_int_t *ai = arith_get_integer(z, xsArg(0));
	unsigned int radix = xsToInteger(xsArg(1));
	size_t usize, n;
	char *str;
	kcl_err_t err;
#define NBITS(n)	(n < 4 ? 1: n < 8 ? 2: n < 16 ? 3: n < 32 ? 4: 5)

	usize = kcl_int_sizeof(ai);
	n = (usize * 8) / NBITS(radix);	/* quite inaccurate, but better than shortage */
	n += 2;	/* for "+-" sign + '\0' */
	if ((str = crypt_malloc(n)) == NULL)
		kcl_throw_error(the, KCL_ERR_NOMEM);
	if ((err = kcl_z_i2str(z->ctx, ai, str, n, radix)) != KCL_ERR_NONE)
		goto bail;
	xsResult = xsString(str);
bail:
	crypt_free(str);
	if (err != KCL_ERR_NONE)
		kcl_throw_error(the, err);
}
Beispiel #11
0
void
xs_ed_mul(xsMachine *the)
{
	ed_t *ed = xsGetHostData(xsThis);
	kcl_ecp_t *p, *r;
	kcl_int_t *k;
	kcl_err_t err;

	xsVars(1);
	p = arith_get_ecp(ed, xsArg(0));
	k = arith_get_integer(ed, xsArg(1));
	(void)((err = kcl_ecp_alloc(&r)) || (err = kcl_ed_mul(ed->ctx, p, k, r)));
	if (err != KCL_ERR_NONE)
		kcl_throw_error(the, err);
	arith_set_ecp(ed, xsResult, r);
}
Beispiel #12
0
static void
z_call3(xsMachine *the, z_t *z, kcl_z_f3 f, xsSlot *rem)
{
	kcl_int_t *a = arith_get_integer(z, xsArg(0));
	kcl_int_t *b = arith_get_integer(z, xsArg(1));
	kcl_int_t *r = NULL, *m = NULL;
	kcl_err_t err;

	err = (*f)(z->ctx, a, b, &r, rem != NULL ? &m : NULL);
	if (err != KCL_ERR_NONE)
		kcl_throw_error(the, err);
	arith_set_integer(z, xsResult, r);
	if (rem != NULL) {
		arith_set_integer(z, *rem, m);
	}
}
Beispiel #13
0
void
xs_tdes_init(xsMachine *the)
{
	crypt_cipher_t *cipher = xsGetHostData(xsThis);
	void *key;
	size_t keysize;
	kcl_err_t err;

	key = xsToArrayBuffer(xsArg(0));
	keysize = xsGetArrayBufferLength(xsArg(0));
	if ((err = kcl_tdes_init(&cipher->ctx, key, keysize)) != KCL_ERR_NONE)
		kcl_throw_error(the, err);
	cipher->keysched = kcl_tdes_keysched;
	cipher->process = kcl_tdes_process;
	cipher->finish = kcl_tdes_finish;
	kcl_tdes_size(cipher->ctx, &cipher->blockSize, &cipher->keySize);
}
Beispiel #14
0
static void
kcl_z_error_callback(kcl_err_t err, void *closure)
{
	xsMachine *the = closure;
	kcl_throw_error(the, err);
}