aboutsummaryrefslogtreecommitdiffstats
path: root/tests/check_arc.c
blob: 9d913dcd142b331d89437cb3a1fd9946b32c4cba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdlib.h>
#include <glib.h>
#include <gmodule.h>
#include <check.h>
#include <string.h>
#include <stdio.h>
#include "arc.h"

char *password = "ArcVier";

char *clear_tests[] =
{
	"Wie dit leest is gek :-)",
	"ItllBeBitlBee",
	"One more boring password",
	"Hoi hoi",
	NULL
};

static void check_codec(int l)
{
	int i;
	
	for( i = 0; clear_tests[i]; i++ )
	{
  		tcase_fn_start (clear_tests[i], __FILE__, __LINE__);
		unsigned char *crypted;
		char *decrypted;
		int len;
		
		len = arc_encode( clear_tests[i], 0, &crypted, password, 12 );
		len = arc_decode( crypted, len, &decrypted, password );
		
		fail_if( strcmp( clear_tests[i], decrypted ) != 0,
		         "%s didn't decrypt back properly", clear_tests[i] );
		
		g_free( crypted );
		g_free( decrypted );
	}
}

struct
{
	unsigned char crypted[30];
	int len;
	char *decrypted;
} decrypt_tests[] = {
	/* One block with padding. */
	{
		{
			0x3f, 0x79, 0xb0, 0xf5, 0x91, 0x56, 0xd2, 0x1b, 0xd1, 0x4b, 0x67, 0xac,
			0xb1, 0x31, 0xc9, 0xdb, 0xf9, 0xaa
		}, 18, "short pass"
	},
	
	/* Two blocks with padding. */
	{
		{
			0xf9, 0xa6, 0xec, 0x5d, 0xc7, 0x06, 0xb8, 0x6b, 0x63, 0x9f, 0x2d, 0xb5,
			0x7d, 0xaa, 0x32, 0xbb, 0xd8, 0x08, 0xfd, 0x81, 0x2e, 0xca, 0xb4, 0xd7,
			0x2f, 0x36, 0x9c, 0xac, 0xa0, 0xbc
		}, 30, "longer password"
	},

	/* This string is exactly two "blocks" long, to make sure unpadded strings also decrypt
	   properly. */
	{
		{
			0x95, 0x4d, 0xcf, 0x4d, 0x5e, 0x6c, 0xcf, 0xef, 0xb9, 0x80, 0x00, 0xef,
			0x25, 0xe9, 0x17, 0xf6, 0x29, 0x6a, 0x82, 0x79, 0x1c, 0xca, 0x68, 0xb5,
			0x4e, 0xd0, 0xc1, 0x41, 0x8e, 0xe6
		}, 30, "OSCAR is really creepy.."
	},
	{ "", 0, NULL }
};

static void check_decod(int l)
{
	int i;
	
	for( i = 0; decrypt_tests[i].len; i++ )
	{
  		tcase_fn_start (decrypt_tests[i].decrypted, __FILE__, __LINE__);
		char *decrypted;
		int len;
		
		len = arc_decode( decrypt_tests[i].crypted, decrypt_tests[i].len,
		                  &decrypted, password );
		
		fail_if( strcmp( decrypt_tests[i].decrypted, decrypted ) != 0,
		         "`%s' didn't decrypt properly", decrypt_tests[i].decrypted );
		
		g_free( decrypted );
	}
}

Suite *arc_suite (void)
{
	Suite *s = suite_create("ArcFour");
	TCase *tc_core = tcase_create("Core");
	suite_add_tcase (s, tc_core);
	tcase_add_test (tc_core, check_codec);
	tcase_add_test (tc_core, check_decod);
	return s;
}
"> == RUNMODE_INETD ) { log_link( LOGLVL_ERROR, LOGOUTPUT_IRC ); log_link( LOGLVL_WARNING, LOGOUTPUT_IRC ); i = bitlbee_inetd_init(); log_message( LOGLVL_INFO, "%s %s starting in inetd mode.", PACKAGE, BITLBEE_VERSION ); } else if( global.conf->runmode == RUNMODE_DAEMON ) { log_link( LOGLVL_ERROR, LOGOUTPUT_CONSOLE ); log_link( LOGLVL_WARNING, LOGOUTPUT_CONSOLE ); i = bitlbee_daemon_init(); log_message( LOGLVL_INFO, "%s %s starting in daemon mode.", PACKAGE, BITLBEE_VERSION ); } else if( global.conf->runmode == RUNMODE_FORKDAEMON ) { log_link( LOGLVL_ERROR, LOGOUTPUT_CONSOLE ); log_link( LOGLVL_WARNING, LOGOUTPUT_CONSOLE ); /* In case the operator requests a restart, we need this. */ old_cwd = g_malloc( 256 ); if( getcwd( old_cwd, 255 ) == NULL ) { log_message( LOGLVL_WARNING, "Could not save current directory: %s", strerror( errno ) ); g_free( old_cwd ); old_cwd = NULL; } i = bitlbee_daemon_init(); log_message( LOGLVL_INFO, "%s %s starting in forking daemon mode.", PACKAGE, BITLBEE_VERSION ); } if( i != 0 ) return( i ); if( ( global.conf->user && *global.conf->user ) && ( global.conf->runmode == RUNMODE_DAEMON || global.conf->runmode == RUNMODE_FORKDAEMON ) && ( !getuid() || !geteuid() ) ) { struct passwd *pw = NULL; pw = getpwnam( global.conf->user ); if( pw ) { initgroups( global.conf->user, pw->pw_gid ); setgid( pw->pw_gid ); setuid( pw->pw_uid ); } else { log_message( LOGLVL_WARNING, "Failed to look up user %s.", global.conf->user ); } } /* Catch some signals to tell the user what's happening before quitting */ memset( &sig, 0, sizeof( sig ) ); sig.sa_handler = sighandler; sigaction( SIGCHLD, &sig, &old ); sigaction( SIGPIPE, &sig, &old ); sig.sa_flags = SA_RESETHAND; sigaction( SIGINT, &sig, &old ); sigaction( SIGILL, &sig, &old ); sigaction( SIGBUS, &sig, &old ); sigaction( SIGFPE, &sig, &old ); sigaction( SIGSEGV, &sig, &old ); sigaction( SIGTERM, &sig, &old ); sigaction( SIGQUIT, &sig, &old ); sigaction( SIGXCPU, &sig, &old ); if( !getuid() || !geteuid() ) log_message( LOGLVL_WARNING, "BitlBee is running with root privileges. Why?" ); b_main_run(); /* Mainly good for restarting, to make sure we close the help.txt fd. */ help_free( &global.help ); if( global.restart ) { char *fn = ipc_master_save_state(); char *env; env = g_strdup_printf( "_BITLBEE_RESTART_STATE=%s", fn ); putenv( env ); g_free( fn ); /* Looks like env should *not* be freed here as putenv doesn't make a copy. Odd. */ i = chdir( old_cwd ); close( global.listen_socket ); if( execv( argv[0], argv ) == -1 ) /* Apparently the execve() failed, so let's just jump back into our own/current main(). */ /* Need more cleanup code to make this work. */ return 1; /* main( argc, argv ); */ } return( 0 ); } static int crypt_main( int argc, char *argv[] ) { int pass_len; unsigned char *pass_cr, *pass_cl; if( argc < 4 || ( strcmp( argv[2], "hash" ) != 0 && strcmp( argv[2], "unhash" ) != 0 && argc < 5 ) ) { printf( "Supported:\n" " %s -x enc <key> <cleartext password>\n" " %s -x dec <key> <encrypted password>\n" " %s -x hash <cleartext password>\n" " %s -x unhash <hashed password>\n" " %s -x chkhash <hashed password> <cleartext password>\n", argv[0], argv[0], argv[0], argv[0], argv[0] ); } else if( strcmp( argv[2], "enc" ) == 0 ) { pass_len = arc_encode( argv[4], strlen( argv[4] ), (unsigned char**) &pass_cr, argv[3], 12 ); printf( "%s\n", base64_encode( pass_cr, pass_len ) ); } else if( strcmp( argv[2], "dec" ) == 0 ) { pass_len = base64_decode( argv[4], (unsigned char**) &pass_cr ); arc_decode( pass_cr, pass_len, (char**) &pass_cl, argv[3] ); printf( "%s\n", pass_cl ); } else if( strcmp( argv[2], "hash" ) == 0 ) { md5_byte_t pass_md5[21]; md5_state_t md5_state; random_bytes( pass_md5 + 16, 5 ); md5_init( &md5_state ); md5_append( &md5_state, (md5_byte_t*) argv[3], strlen( argv[3] ) ); md5_append( &md5_state, pass_md5 + 16, 5 ); /* Add the salt. */ md5_finish( &md5_state, pass_md5 ); printf( "%s\n", base64_encode( pass_md5, 21 ) ); } else if( strcmp( argv[2], "unhash" ) == 0 ) { printf( "Hash %s submitted to a massive Beowulf cluster of\n" "overclocked 486s. Expect your answer next year somewhere around this time. :-)\n", argv[3] ); } else if( strcmp( argv[2], "chkhash" ) == 0 ) { char *hash = strncmp( argv[3], "md5:", 4 ) == 0 ? argv[3] + 4 : argv[3]; int st = md5_verify_password( argv[4], hash ); printf( "Hash %s given password.\n", st == 0 ? "matches" : "does not match" ); return st; } return 0; } static void sighandler( int signal ) { /* FIXME: Calling log_message() here is not a very good idea! */ if( signal == SIGTERM || signal == SIGQUIT || signal == SIGINT ) { static int first = 1; if( first ) { /* We don't know what we were doing when this signal came in. It's not safe to touch the user data now (not to mention writing them to disk), so add a timer. */ log_message( LOGLVL_ERROR, "SIGTERM received, cleaning up process." ); b_timeout_add( 1, (b_event_handler) bitlbee_shutdown, NULL ); first = 0; } else { /* Well, actually, for now we'll never need this part because this signal handler will never be called more than once in a session for a non-SIGPIPE signal... But just in case we decide to change that: */ log_message( LOGLVL_ERROR, "SIGTERM received twice, so long for a clean shutdown." ); raise( signal ); } } else if( signal == SIGCHLD ) { pid_t pid; int st; while( ( pid = waitpid( 0, &st, WNOHANG ) ) > 0 ) { if( WIFSIGNALED( st ) ) log_message( LOGLVL_INFO, "Client %d terminated normally. (status = %d)", (int) pid, WEXITSTATUS( st ) ); else if( WIFEXITED( st ) ) log_message( LOGLVL_INFO, "Client %d killed by signal %d.", (int) pid, WTERMSIG( st ) ); } } else if( signal != SIGPIPE ) { log_message( LOGLVL_ERROR, "Fatal signal received: %d. That's probably a bug.", signal ); raise( signal ); } } double gettime() { struct timeval time[1]; gettimeofday( time, 0 ); return( (double) time->tv_sec + (double) time->tv_usec / 1000000 ); }