Ejemplo n.º 1
0
int encrypt(char *password, char *plaintext, char *ciphertext)
{

	MCRYPT td;
	int i;
	char *key;
	char block_buffer;
	char *IV;
	char *salt;
	
	td = mcrypt_module_open(algorithm, NULL, mode, NULL);
	if (td == MCRYPT_FAILED)
	{
		printf("failed to open module\n");		
		return 1;
	}

	salt = crypt_malloc(saltsize);
	crypt_random(salt, saltsize);
//	printf("salt:%s\n",salt);
        IV = crypt_malloc(ivsize);
	crypt_random(IV, ivsize);
//	printf("IV:%s\n",IV);

	putin_meg(ciphertext, salt, IV);
	
	key = crypt_malloc(keysize);
	data.hash_algorithm[0] = hash_algo;
	data.count = 0;
	data.salt = salt;
	data.salt_size = saltsize;
	mhash_keygen_ext(KEYGEN_MCRYPT, data, key, keysize, password, strlen(password));
	printf("key:%s\n",key);

	i = mcrypt_generic_init(td, key, keysize, IV);
        if (i<0) 
	{
        	mcrypt_perror(i);
        	return 1;
        }
//	printf("%d",strlen(plaintext));
// Here to encrypt in CFB performed in bytes 
        for(i=36; i<strlen(plaintext); i++)
	{
//		printf("%c",plaintext[i]);
		block_buffer = plaintext[i];
        	mcrypt_generic (td, &block_buffer, 1);
        	ciphertext[i] = block_buffer; 

//		printf("%c",ciphertext[i]);
	}

// Deinit the encryption thread, and unload the module 
         mcrypt_generic_end(td);

         return 0;

}
Ejemplo n.º 2
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);
}
Ejemplo n.º 3
0
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);
}
Ejemplo n.º 4
0
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);
}
Ejemplo n.º 5
0
int dencrypt(char *password, char *ciphertext, char *plaintext)
{

	MCRYPT td;
	int i;
	char *key;
	char block_buffer;
	char *IV;
	char *salt;	
	
	td = mcrypt_module_open(algorithm, NULL, mode, NULL);
	if (td==MCRYPT_FAILED)
	{
		return 1;
	}

	salt = crypt_malloc(saltsize);
        IV = crypt_malloc(ivsize);

	read_meg(ciphertext, salt, IV);

        i=mcrypt_generic_init( td, key, keysize, IV);
        if (i<0) 
	{
        	mcrypt_perror(i);
        	return 1;
        }
//	printf("%d",strlen(plaintext));
        for(i=saltsize + ivsize; i<=strlen(ciphertext); i++)
	{
//		printf("%c",plaintext[i]);
		block_buffer=ciphertext[i];
//Here begin to decrypt
       		mdecrypt_generic (td, &block_buffer, 1); 
        	plaintext[i]=block_buffer; 
//		printf("%c",ciphertext[i]);
	}

         mcrypt_generic_end(td);

         return 0;

       }
Ejemplo n.º 6
0
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);
}
Ejemplo n.º 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);
}
Ejemplo n.º 8
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);
}