static void cipher_process(xsMachine *the, kcl_symmetric_direction_t direction) { crypt_cipher_t *cipher = xsGetHostData(xsThis); size_t len; void *indata, *outdata; if (cipher->keysched != NULL) { if (cipher->direction != direction) { (*cipher->keysched)(cipher->ctx, direction); cipher->direction = direction; } } if (xsToInteger(xsArgc) > 1 && xsTest(xsArg(1))) { if (xsGetArrayBufferLength(xsArg(1)) < (xsIntegerValue)cipher->blockSize) crypt_throw_error(the, "cipher: too small buffer"); xsResult = xsArg(1); } else xsResult = xsArrayBuffer(NULL, cipher->blockSize); if (xsTypeOf(xsArg(0)) == xsStringType) { indata = xsToString(xsArg(0)); len = c_strlen(indata); } else { indata = xsToArrayBuffer(xsArg(0)); len = xsGetArrayBufferLength(xsArg(0)); } if (len < cipher->blockSize) crypt_throw_error(the, "cipher: wrong size"); outdata = xsToArrayBuffer(xsResult); (*cipher->process)(cipher->ctx, indata, outdata); }
void xs_stream_encrypt(xsMachine *the) { crypt_stream_t *stream = xsGetHostData(xsThis); int ac = xsToInteger(xsArgc); size_t len; void *indata, *outdata; if (xsTypeOf(xsArg(0)) == xsStringType) len = c_strlen(xsToString(xsArg(0))); else len = xsGetArrayBufferLength(xsArg(0)); if (ac > 2 && xsTypeOf(xsArg(2)) != xsUndefinedType) { size_t n = xsToInteger(xsArg(2)); if (n < len) len = n; } if (ac > 1 && xsTest(xsArg(1))) { if (xsGetArrayBufferLength(xsArg(1)) < (xsIntegerValue)len) crypt_throw_error(the, "too small buffer"); xsResult = xsArg(1); } else xsResult = xsArrayBuffer(NULL, len); if (xsTypeOf(xsArg(0)) == xsStringType) indata = xsToString(xsArg(0)); else indata = xsToArrayBuffer(xsArg(0)); outdata = xsToArrayBuffer(xsResult); (*stream->process)(stream->ctx, indata, outdata, len); }
void xs_stream_constructor(xsMachine *the) { crypt_stream_t *stream; if ((stream = crypt_malloc(sizeof(crypt_stream_t))) == NULL) crypt_throw_error(the, "stream: nomem"); c_memset(stream, 0, sizeof(crypt_stream_t)); xsSetHostData(xsThis, stream); }
void xs_digest_constructor(xsMachine *the) { crypt_digest_t *digest; if ((digest = crypt_malloc(sizeof(crypt_digest_t))) == NULL) crypt_throw_error(the, "digest: nomem"); memset(digest, 0, sizeof(crypt_digest_t)); xsSetHostData(xsThis, digest); }
void xs_cipher_constructor(xsMachine *the) { crypt_cipher_t *cipher; if ((cipher = crypt_malloc(sizeof(crypt_cipher_t))) == NULL) crypt_throw_error(the, "cipher: nomem"); c_memset(cipher, 0, sizeof(crypt_cipher_t)); cipher->direction = -1; xsSetHostData(xsThis, cipher); }