Exemplo n.º 1
0
WIN32DLL_DEFINE int _mcrypt_self_test()
{
	char *keyword;
	char plaintext[20];
	char ciphertext[20];
	int blocksize = 20, j;
	void *key;
	unsigned char cipher_tmp[200];

	keyword = calloc(1, _mcrypt_get_key_size());
	if (keyword == NULL)
		return -1;

	strcpy(keyword, "enadyotr");

	for (j = 0; j < blocksize; j++) {
		plaintext[j] = j % 256;
	}
	key = malloc(_mcrypt_get_size());
	if (key == NULL) {
		free(keyword);
		return -1;
	}
	memmove(ciphertext, plaintext, blocksize);

	_mcrypt_set_key(key, (void *) keyword, _mcrypt_get_key_size(),
			NULL, 0);
	_mcrypt_encrypt(key, (void *) ciphertext, blocksize);

	for (j = 0; j < blocksize; j++) {
		sprintf(&((char *) cipher_tmp)[2 * j], "%.2x",
			ciphertext[j]);
	}

	if (strcmp((char *) cipher_tmp, CIPHER) != 0) {
		printf("failed compatibility\n");
		printf("Expected: %s\nGot: %s\n", CIPHER,
		       (char *) cipher_tmp);
		free(keyword);
		free(key);
		return -1;
	}

	_mcrypt_set_key(key, (void *) keyword, _mcrypt_get_key_size(),
			NULL, 0);
	free(keyword);
	
	_mcrypt_decrypt(key, (void *) ciphertext, blocksize);
	free(key);

	if (strcmp(ciphertext, plaintext) != 0) {
		printf("failed internally\n");
		return -1;
	}

	return 0;
}
Exemplo n.º 2
0
WIN32DLL_DEFINE int _mcrypt_self_test()
{
	unsigned char *keyword;
	unsigned char plaintext[43];
	unsigned char ciphertext[43];
	int blocksize = 43, j;
	void *key, *key2;
	unsigned char cipher_tmp[200];

	keyword = calloc(1, _mcrypt_get_key_size());
	for (j = 0; j < _mcrypt_get_key_size(); j++) {
		keyword[j] = (j * 5 + 10) & 0xff;
	}

	for (j = 0; j < blocksize; j++) {
		plaintext[j] = (j + 5) % 0xff;
	}
	key = malloc(_mcrypt_get_size());
	key2 = malloc(_mcrypt_get_size());

	memcpy(ciphertext, plaintext, blocksize);

	_mcrypt_set_key(key, (void *)keyword, _mcrypt_get_key_size(),
		NULL, 0);
	_mcrypt_encrypt(key, (void *)ciphertext, blocksize);
	free(key);

	for (j = 0; j < blocksize; j++) {
		sprintf(&((char *)cipher_tmp)[2 * j], "%.2x",
			ciphertext[j]);
	}

	if (strcmp((char *)cipher_tmp, CIPHER) != 0) {
		printf("failed compatibility\n");
		printf("Expected: %s\nGot: %s\n", CIPHER,
			(char *)cipher_tmp);
		free(key);
		free(key2);
		return -1;
	}
	_mcrypt_set_key(key2, (void *)keyword, _mcrypt_get_key_size(),
		NULL, 0);
	free(keyword);

	_mcrypt_decrypt(key2, (void *)ciphertext, blocksize);
	free(key2);

	if (memcmp(ciphertext, plaintext, blocksize) != 0) {
		printf("failed internally\n");
		return -1;
	}

	return 0;
}
Exemplo n.º 3
0
WIN32DLL_DEFINE int _mcrypt_self_test()
{
	char *keyword;
	unsigned char plaintext[16];
	unsigned char ciphertext[16];
	int blocksize = _mcrypt_get_block_size(), j;
	void *key;
	unsigned char cipher_tmp[200];

	keyword = calloc(1, _mcrypt_get_key_size());
	if (keyword == NULL)
		return -1;

	for (j = 0; j < _mcrypt_get_key_size(); j++) {
		keyword[j] = ((j * 2 + 10) % 256);
	}

	for (j = 0; j < blocksize; j++) {
		plaintext[j] = j % 256;
	}
	key = malloc(_mcrypt_get_size());
	if (key == NULL)
		return -1;

	memcpy(ciphertext, plaintext, blocksize);

	_mcrypt_set_key(key, (void *) keyword, _mcrypt_get_key_size());
	free(keyword);

	_mcrypt_encrypt(key, (void *) ciphertext);

	for (j = 0; j < blocksize; j++) {
		sprintf(&((char *) cipher_tmp)[2 * j], "%.2x",
			ciphertext[j]);
	}

	if (strcmp((char *) cipher_tmp, CIPHER) != 0) {
		printf("failed compatibility\n");
		printf("Expected: %s\nGot: %s\n", CIPHER,
		       (char *) cipher_tmp);
		free(key);
		return -1;
	}
	_mcrypt_decrypt(key, (void *) ciphertext);
	free(key);

	if (strcmp((const char *)ciphertext, (const char *)plaintext) != 0) {
		printf("failed internally\n");
		return -1;
	}

	return 0;
}
Exemplo n.º 4
0
WIN32DLL_DEFINE
void _mcrypt_decrypt(WAKE_KEY * wake_key, byte * input, int len)
{
	register word32 r3, r4, r5;
	word32 r6;
	int i;

	if (len == 0)
		return;

	r3 = wake_key->r[0];
	r4 = wake_key->r[1];
	r5 = wake_key->r[2];
	r6 = wake_key->r[3];

#ifdef USE_IV
	if (wake_key->started == 0) {
		wake_key->started = 1;
		_mcrypt_encrypt(wake_key, (byte *)& wake_key->iv,
			wake_key->ivsize);
		wake_key->r[0] = r3;
		wake_key->r[1] = r4;
		wake_key->r[2] = r5;
		wake_key->r[3] = r6;
		_mcrypt_decrypt(wake_key, (byte *)& wake_key->iv,
			wake_key->ivsize);
	}
#endif

	for (i = 0; i < len; i++) {
		/* R1 = V[n] */
		((byte *)& r1)[counter] = input[i];
		/* R2 = V[n] = V[n] ^ R6 */
		/* R2 is ignored */
		input[i] ^= ((byte *)& r6)[counter];
		counter++;

		if (counter == 4) {
			counter = 0;

#ifdef WORDS_BIGENDIAN
			r1 = byteswap32(r1);
			r6 = byteswap32(r6);
#endif
			r3 = M(r3, r1);
			r4 = M(r4, r3);
			r5 = M(r5, r4);
			r6 = M(r6, r5);

#ifdef WORDS_BIGENDIAN
			r6 = byteswap32(r6);
#endif
		}
	}

	wake_key->r[0] = r3;
	wake_key->r[1] = r4;
	wake_key->r[2] = r5;
	wake_key->r[3] = r6;

}