Ejemplo n.º 1
0
int main(int argc, char *argv[])
	{
	FILE *in=NULL,*out=NULL;
	char *infile=NULL,*outfile=NULL,*keystr=NULL;
	RC4_KEY key;
	char buf[BUFSIZ];
	int badops=0,i;
	char **pp;
	unsigned char md[MD5_DIGEST_LENGTH];

	argc--;
	argv++;
	while (argc >= 1)
		{
		if 	(strcmp(*argv,"-in") == 0)
			{
			if (--argc < 1) goto bad;
			infile= *(++argv);
			}
		else if (strcmp(*argv,"-out") == 0)
			{
			if (--argc < 1) goto bad;
			outfile= *(++argv);
			}
		else if (strcmp(*argv,"-key") == 0)
			{
			if (--argc < 1) goto bad;
			keystr= *(++argv);
			}
		else
			{
			fprintf(stderr,"unknown option %s\n",*argv);
			badops=1;
			break;
			}
		argc--;
		argv++;
		}

	if (badops)
		{
bad:
		for (pp=usage; (*pp != NULL); pp++)
			fprintf(stderr,*pp);
		exit(1);
		}

	if (infile == NULL)
		in=stdin;
	else
		{
		in=fopen(infile,"r");
		if (in == NULL)
			{
			perror("open");
			exit(1);
			}

		}
	if (outfile == NULL)
		out=stdout;
	else
		{
		out=fopen(outfile,"w");
		if (out == NULL)
			{
			perror("open");
			exit(1);
			}
		}
		
#ifdef MSDOS
	/* This should set the file to binary mode. */
	{
#include <fcntl.h>
	setmode(fileno(in),O_BINARY);
	setmode(fileno(out),O_BINARY);
	}
#endif

	if (keystr == NULL)
		{ /* get key */
		i=EVP_read_pw_string(buf,BUFSIZ,"Enter RC4 password:"******"bad password read\n");
			exit(1);
			}
		keystr=buf;
		}

	MD5((unsigned char *)keystr,(unsigned long)strlen(keystr),md);
	OPENSSL_cleanse(keystr,strlen(keystr));
	RC4_set_key(&key,MD5_DIGEST_LENGTH,md);
	
	for(;;)
		{
		i=fread(buf,1,BUFSIZ,in);
		if (i == 0) break;
		if (i < 0)
			{
			perror("read");
			exit(1);
			}
		RC4(&key,(unsigned int)i,(unsigned char *)buf,
			(unsigned char *)buf);
		i=fwrite(buf,(unsigned int)i,1,out);
		if (i != 1)
			{
			perror("write");
			exit(1);
			}
		}
	fclose(out);
	fclose(in);
	exit(0);
	return(1);
	}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
    int i, err = 0;
    int j;
    unsigned char *p;
    RC4_KEY key;
    unsigned char obuf[512];

# if !defined(OPENSSL_PIC)
    void OPENSSL_cpuid_setup(void);

    OPENSSL_cpuid_setup();
# endif

    for (i = 0; i < 6; i++) {
        RC4_set_key(&key, keys[i][0], &(keys[i][1]));
        sgx_memset(obuf, 0x00, sizeof(obuf));
        RC4(&key, data_len[i], &(data[i][0]), obuf);
        if (sgx_memcmp(obuf, output[i], data_len[i] + 1) != 0) {
            printf("error calculating RC4\n");
            printf("output:");
            for (j = 0; j < data_len[i] + 1; j++)
                printf(" %02x", obuf[j]);
            printf("\n");
            printf("expect:");
            p = &(output[i][0]);
            for (j = 0; j < data_len[i] + 1; j++)
                printf(" %02x", *(p++));
            printf("\n");
            err++;
        } else
            printf("test %d ok\n", i);
    }
    printf("test end processing ");
    for (i = 0; i < data_len[3]; i++) {
        RC4_set_key(&key, keys[3][0], &(keys[3][1]));
        sgx_memset(obuf, 0x00, sizeof(obuf));
        RC4(&key, i, &(data[3][0]), obuf);
        if ((sgx_memcmp(obuf, output[3], i) != 0) || (obuf[i] != 0)) {
            printf("error in RC4 length processing\n");
            printf("output:");
            for (j = 0; j < i + 1; j++)
                printf(" %02x", obuf[j]);
            printf("\n");
            printf("expect:");
            p = &(output[3][0]);
            for (j = 0; j < i; j++)
                printf(" %02x", *(p++));
            printf(" 00\n");
            err++;
        } else {
            printf(".");
            fflush(stdout);
        }
    }
    printf("done\n");
    printf("test multi-call ");
    for (i = 0; i < data_len[3]; i++) {
        RC4_set_key(&key, keys[3][0], &(keys[3][1]));
        sgx_memset(obuf, 0x00, sizeof(obuf));
        RC4(&key, i, &(data[3][0]), obuf);
        RC4(&key, data_len[3] - i, &(data[3][i]), &(obuf[i]));
        if (sgx_memcmp(obuf, output[3], data_len[3] + 1) != 0) {
            printf("error in RC4 multi-call processing\n");
            printf("output:");
            for (j = 0; j < data_len[3] + 1; j++)
                printf(" %02x", obuf[j]);
            printf("\n");
            printf("expect:");
            p = &(output[3][0]);
            for (j = 0; j < data_len[3] + 1; j++)
                printf(" %02x", *(p++));
            err++;
        } else {
            printf(".");
            fflush(stdout);
        }
    }
    printf("done\n");
    printf("bulk test ");
    {
        unsigned char buf[513];
        SHA_CTX c;
        unsigned char md[SHA_DIGEST_LENGTH];
        static unsigned char expected[] = {
            0xa4, 0x7b, 0xcc, 0x00, 0x3d, 0xd0, 0xbd, 0xe1, 0xac, 0x5f,
            0x12, 0x1e, 0x45, 0xbc, 0xfb, 0x1a, 0xa1, 0xf2, 0x7f, 0xc5
        };

        RC4_set_key(&key, keys[0][0], &(keys[3][1]));
        sgx_memset(buf, '\0', sizeof(buf));
        SHA1_Init(&c);
        for (i = 0; i < 2571; i++) {
            RC4(&key, sizeof(buf), buf, buf);
            SHA1_Update(&c, buf, sizeof(buf));
        }
        SHA1_Final(md, &c);

        if (sgx_memcmp(md, expected, sizeof(md))) {
            printf("error in RC4 bulk test\n");
            printf("output:");
            for (j = 0; j < (int)sizeof(md); j++)
                printf(" %02x", md[j]);
            printf("\n");
            printf("expect:");
            for (j = 0; j < (int)sizeof(md); j++)
                printf(" %02x", expected[j]);
            printf("\n");
            err++;
        } else
            printf("ok\n");
    }
# ifdef OPENSSL_SYS_NETWARE
    if (err)
        printf("ERROR: %d\n", err);
# endif
    EXIT(err);
    return (0);
}
Ejemplo n.º 3
0
CryptoRc4 crypto_rc4_init(const uint8* key, uint32 length)
{
	CryptoRc4 rc4 = xmalloc(sizeof(*rc4));
	RC4_set_key(&rc4->rc4_key, length, key);
	return rc4;
}
Ejemplo n.º 4
0
void send_mcast(struct params *p, unsigned char x)
{
	struct ieee80211_frame *wh;
	short *seq;
	struct queue *q = p->q;
	char *data, *ptr;
	int len;
	uLong crc = crc32(0L, Z_NULL, 0);
	uLong *pcrc;
	int i;
	int need_frag = 0;
	char payload[10] = "\xAA\xAA\x03\x00\x00\x00\x08\x06\x00\x00";

	assert(q);

	/* 802.11 */
	memset(p->packet, 0, sizeof(p->packet));
	wh = (struct ieee80211_frame*) p->packet;
	wh->i_fc[0] |= IEEE80211_FC0_TYPE_DATA;
	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_DATA;
	wh->i_fc[1] |= IEEE80211_FC1_DIR_TODS;
	wh->i_fc[1] |= IEEE80211_FC1_WEP;
	
	wh->i_dur[0] = 0x69;
	
	memcpy(wh->i_addr1, p->ap, 6);
	memcpy(wh->i_addr2, p->mac, 6);
	memcpy(wh->i_addr3, p->mcast, 5);
	wh->i_addr3[5] = x;

	seq = (short*) wh->i_seq;
	*seq = seqfn(p->seq++, 0);

	/* IV */
	data = (char*) (wh+1);
	ptr = (char*) (q->wh+1);
	memcpy(data, ptr, 3);

	if (p->prga_len == 0) {
	
		RC4_KEY k;
		unsigned char key[8];

		memset(&key[3], 0x61, 5);
		memcpy(key, (q->wh+1), 3);
		p->prga_len = 128;

		RC4_set_key(&k, 8, key);
		memset(p->prga, 0, sizeof(p->prga));
		RC4(&k, p->prga_len, p->prga, p->prga);

		
#if 0	
		int ptl = q->len;
		char *pt;

		pt = known_pt(q->wh, &ptl);
		ptr += 4;
		p->prga_len = ptl;
		for (i = 0; i < p->prga_len; i++)
			p->prga[i] = ptr[i] ^ pt[i];
#endif			
	}

	/* data */
	data += 4;
	memcpy(data, payload, sizeof(payload));
	p->prga_len = 12;
	len = p->prga_len + 1 - 4;

#if 1
	if (len < sizeof(payload)) {
		need_frag = len;
		wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
	}	
#endif

	/* crc */
	pcrc = (uLong*) (data+len);
	*pcrc = crc32(crc, data, len);

	/* wepify */
	len += 4;
	for (i = 0; i < (len); i++) {
		assert( i <= p->prga_len);
		data[i] ^= p->prga[i];
	}
//	data[i] ^= x;

	len += sizeof(*wh);
	p->packet_len = len + 4;
	send_packet(p);

	/* the data we sent is too f*****g short */
	if (need_frag) {
		memset(data, 0, len);

		/* 802.11 */
		*seq = seqfn(p->seq-1, 1);
		wh->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;

		/* data */
		len = sizeof(payload) - need_frag;
		assert(len > 0 && len <= (p->prga_len - 4));
		memcpy(data, &payload[need_frag], len);
	
		/* crc */
		crc = crc32(0L, Z_NULL, 0);
		len = p->prga_len - 4;
		pcrc = (uLong*) (data+len);
		*pcrc = crc32(crc, data, len);

		/* wepify */
		len += 4;
		for (i = 0; i < len; i++) {
			assert( i < p->prga_len);
			data[i] ^= p->prga[i];
		}

		len += sizeof(*wh) + 4;
		p->packet_len = len;
		send_packet(p);
	}
}
int ssl_test_rc4(int argc, char *argv[])
	{
	int i,err=0;
	int j;
	unsigned char *p;
	RC4_KEY key;
	unsigned char obuf[512];

	for (i=0; i<6; i++)
		{
		RC4_set_key(&key,keys[i][0],&(keys[i][1]));
		TINYCLR_SSL_MEMSET(obuf,0x00,sizeof(obuf));
		RC4(&key,data_len[i],&(data[i][0]),obuf);
		if (TINYCLR_SSL_MEMCMP(obuf,output[i],data_len[i]+1) != 0)
			{
			TINYCLR_SSL_PRINTF("error calculating RC4\n");
			TINYCLR_SSL_PRINTF("output:");
			for (j=0; j<data_len[i]+1; j++)
				TINYCLR_SSL_PRINTF(" %02x",obuf[j]);
			TINYCLR_SSL_PRINTF("\n");
			TINYCLR_SSL_PRINTF("expect:");
			p= &(output[i][0]);
			for (j=0; j<data_len[i]+1; j++)
				TINYCLR_SSL_PRINTF(" %02x",*(p++));
			TINYCLR_SSL_PRINTF("\n");
			err++;
			}
		else
			TINYCLR_SSL_PRINTF("test %d ok\n",i);
		}
	TINYCLR_SSL_PRINTF("test end processing ");
	for (i=0; i<data_len[3]; i++)
		{
		RC4_set_key(&key,keys[3][0],&(keys[3][1]));
		TINYCLR_SSL_MEMSET(obuf,0x00,sizeof(obuf));
		RC4(&key,i,&(data[3][0]),obuf);
		if ((TINYCLR_SSL_MEMCMP(obuf,output[3],i) != 0) || (obuf[i] != 0))
			{
			TINYCLR_SSL_PRINTF("error in RC4 length processing\n");
			TINYCLR_SSL_PRINTF("output:");
			for (j=0; j<i+1; j++)
				TINYCLR_SSL_PRINTF(" %02x",obuf[j]);
			TINYCLR_SSL_PRINTF("\n");
			TINYCLR_SSL_PRINTF("expect:");
			p= &(output[3][0]);
			for (j=0; j<i; j++)
				TINYCLR_SSL_PRINTF(" %02x",*(p++));
			TINYCLR_SSL_PRINTF(" 00\n");
			err++;
			}
		else
			{
			TINYCLR_SSL_PRINTF(".");
			TINYCLR_SSL_FFLUSH(OPENSSL_TYPE__FILE_STDOUT);
			}
		}
	TINYCLR_SSL_PRINTF("done\n");
	TINYCLR_SSL_PRINTF("test multi-call ");
	for (i=0; i<data_len[3]; i++)
		{
		RC4_set_key(&key,keys[3][0],&(keys[3][1]));
		TINYCLR_SSL_MEMSET(obuf,0x00,sizeof(obuf));
		RC4(&key,i,&(data[3][0]),obuf);
		RC4(&key,data_len[3]-i,&(data[3][i]),&(obuf[i]));
		if (TINYCLR_SSL_MEMCMP(obuf,output[3],data_len[3]+1) != 0)
			{
			TINYCLR_SSL_PRINTF("error in RC4 multi-call processing\n");
			TINYCLR_SSL_PRINTF("output:");
			for (j=0; j<data_len[3]+1; j++)
				TINYCLR_SSL_PRINTF(" %02x",obuf[j]);
			TINYCLR_SSL_PRINTF("\n");
			TINYCLR_SSL_PRINTF("expect:");
			p= &(output[3][0]);
			for (j=0; j<data_len[3]+1; j++)
				TINYCLR_SSL_PRINTF(" %02x",*(p++));
			err++;
			}
		else
			{
			TINYCLR_SSL_PRINTF(".");
			TINYCLR_SSL_FFLUSH(OPENSSL_TYPE__FILE_STDOUT);
			}
		}
	TINYCLR_SSL_PRINTF("done\n");
	TINYCLR_SSL_PRINTF("bulk test ");
	{   unsigned char buf[513];
	    SHA_CTX c;
	    unsigned char md[SHA_DIGEST_LENGTH];
	    static unsigned char expected[]={
		0xa4,0x7b,0xcc,0x00,0x3d,0xd0,0xbd,0xe1,0xac,0x5f,
		0x12,0x1e,0x45,0xbc,0xfb,0x1a,0xa1,0xf2,0x7f,0xc5 };

		RC4_set_key(&key,keys[0][0],&(keys[3][1]));
		TINYCLR_SSL_MEMSET(buf,'\0',sizeof(buf));
		SHA1_Init(&c);
		for (i=0;i<2571;i++) {
			RC4(&key,sizeof(buf),buf,buf);
			SHA1_Update(&c,buf,sizeof(buf));
		}
		SHA1_Final(md,&c);

		if (TINYCLR_SSL_MEMCMP(md,expected,sizeof(md))) {
			TINYCLR_SSL_PRINTF("error in RC4 bulk test\n");
			TINYCLR_SSL_PRINTF("output:");
			for (j=0; j<(int)sizeof(md); j++)
				TINYCLR_SSL_PRINTF(" %02x",md[j]);
			TINYCLR_SSL_PRINTF("\n");
			TINYCLR_SSL_PRINTF("expect:");
			for (j=0; j<(int)sizeof(md); j++)
				TINYCLR_SSL_PRINTF(" %02x",expected[j]);
			TINYCLR_SSL_PRINTF("\n");
			err++;
		}
		else	TINYCLR_SSL_PRINTF("ok\n");
	}
#ifdef OPENSSL_SYS_NETWARE
    if (err) TINYCLR_SSL_PRINTF("ERROR: %d\n", err);
#endif
	return(err);
	}