Ejemplo n.º 1
0
main()
{ 
    storage_begin(100); 
    store_block("foo",0,0,foo); 
    store_block("foo",1,1,foo); 
    store_block("foo",2,4,foo); 
    store_block("bar",0,4,foo); 
    store_block("bar",1,100,foo); 
    size_remember("foo",200); 
    size_remember("bar",300); 
    printf("size of %s is %d\n","foo",size("foo")); 
    storage_end(); 
} 
Ejemplo n.º 2
0
int main(int argc, char *argv[]) {
	struct Pieces *piece_tracker;
	struct Tor *tor = malloc(sizeof(struct Tor));
	tor->name = "testName";
	tor->length = 1024;
	tor->num_pieces = 16;
	piece_tracker = initialize_file(tor);
	//printf("piece_tracker->fp_parts%s\n", piece_tracker->fp_parts);
	printf("piece_tracker->orig_name is: %s\n", piece_tracker->orig_name);

	//storing a block now
	char *data_to_store = "abcdefg";
	int index = 5;
	int begin = 3;
	int length = 7;
	store_block(piece_tracker, tor, data_to_store, index, begin, length);
}
Ejemplo n.º 3
0
static int
nfs_read_reply (uchar *pkt, unsigned len)
{
	struct rpc_t rpc_pkt;
	int rlen;

#ifdef NFS_DEBUG_nop
	printf ("%s\n", __FUNCTION__);
#endif

	memcpy ((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));

	if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
		return -1;

	if (rpc_pkt.u.reply.rstatus  ||
	    rpc_pkt.u.reply.verifier ||
	    rpc_pkt.u.reply.astatus  ||
	    rpc_pkt.u.reply.data[0]) {
		if (rpc_pkt.u.reply.rstatus) {
			return -9999;
		}
		if (rpc_pkt.u.reply.astatus) {
			return -9999;
		}
		return -ntohl(rpc_pkt.u.reply.data[0]);;
	}

	if ((nfs_offset!=0) && !((nfs_offset) % (NFS_READ_SIZE/2*10*HASHES_PER_LINE))) {
		puts ("\n\t ");
	}
	if (!(nfs_offset % ((NFS_READ_SIZE/2)*10))) {
		putc ('#');
	}

	rlen = ntohl(rpc_pkt.u.reply.data[18]);
	if ( store_block ((uchar *)pkt+sizeof(rpc_pkt.u.reply), nfs_offset, rlen) )
		return -9999;

	return rlen;
}
Ejemplo n.º 4
0
void AES_ige_encrypt(const unsigned char *in, unsigned char *out,
                     size_t length, const AES_KEY *key,
                     unsigned char *ivec, const int enc)
{
    size_t n;
    size_t len = length;

    OPENSSL_assert(in && out && key && ivec);
    OPENSSL_assert((AES_ENCRYPT == enc) || (AES_DECRYPT == enc));
    OPENSSL_assert((length % AES_BLOCK_SIZE) == 0);

    len = length / AES_BLOCK_SIZE;

    if (AES_ENCRYPT == enc) {
        if (in != out &&
            (UNALIGNED_MEMOPS_ARE_FAST
             || ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(long) ==
             0)) {
            aes_block_t *ivp = (aes_block_t *) ivec;
            aes_block_t *iv2p = (aes_block_t *) (ivec + AES_BLOCK_SIZE);

            while (len) {
                aes_block_t *inp = (aes_block_t *) in;
                aes_block_t *outp = (aes_block_t *) out;

                for (n = 0; n < N_WORDS; ++n)
                    outp->data[n] = inp->data[n] ^ ivp->data[n];
                AES_encrypt((unsigned char *)outp->data,
                            (unsigned char *)outp->data, key);
                for (n = 0; n < N_WORDS; ++n)
                    outp->data[n] ^= iv2p->data[n];
                ivp = outp;
                iv2p = inp;
                --len;
                in += AES_BLOCK_SIZE;
                out += AES_BLOCK_SIZE;
            }
            sgx_memcpy(ivec, ivp->data, AES_BLOCK_SIZE);
            sgx_memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE);
        } else {
            aes_block_t tmp, tmp2;
            aes_block_t iv;
            aes_block_t iv2;

            load_block(iv, ivec);
            load_block(iv2, ivec + AES_BLOCK_SIZE);

            while (len) {
                load_block(tmp, in);
                for (n = 0; n < N_WORDS; ++n)
                    tmp2.data[n] = tmp.data[n] ^ iv.data[n];
                AES_encrypt((unsigned char *)tmp2.data,
                            (unsigned char *)tmp2.data, key);
                for (n = 0; n < N_WORDS; ++n)
                    tmp2.data[n] ^= iv2.data[n];
                store_block(out, tmp2);
                iv = tmp2;
                iv2 = tmp;
                --len;
                in += AES_BLOCK_SIZE;
                out += AES_BLOCK_SIZE;
            }
            sgx_memcpy(ivec, iv.data, AES_BLOCK_SIZE);
            sgx_memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE);
        }
    } else {
        if (in != out &&
            (UNALIGNED_MEMOPS_ARE_FAST
             || ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(long) ==
             0)) {
            aes_block_t *ivp = (aes_block_t *) ivec;
            aes_block_t *iv2p = (aes_block_t *) (ivec + AES_BLOCK_SIZE);

            while (len) {
                aes_block_t tmp;
                aes_block_t *inp = (aes_block_t *) in;
                aes_block_t *outp = (aes_block_t *) out;

                for (n = 0; n < N_WORDS; ++n)
                    tmp.data[n] = inp->data[n] ^ iv2p->data[n];
                AES_decrypt((unsigned char *)tmp.data,
                            (unsigned char *)outp->data, key);
                for (n = 0; n < N_WORDS; ++n)
                    outp->data[n] ^= ivp->data[n];
                ivp = inp;
                iv2p = outp;
                --len;
                in += AES_BLOCK_SIZE;
                out += AES_BLOCK_SIZE;
            }
            sgx_memcpy(ivec, ivp->data, AES_BLOCK_SIZE);
            sgx_memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE);
        } else {
            aes_block_t tmp, tmp2;
            aes_block_t iv;
            aes_block_t iv2;

            load_block(iv, ivec);
            load_block(iv2, ivec + AES_BLOCK_SIZE);

            while (len) {
                load_block(tmp, in);
                tmp2 = tmp;
                for (n = 0; n < N_WORDS; ++n)
                    tmp.data[n] ^= iv2.data[n];
                AES_decrypt((unsigned char *)tmp.data,
                            (unsigned char *)tmp.data, key);
                for (n = 0; n < N_WORDS; ++n)
                    tmp.data[n] ^= iv.data[n];
                store_block(out, tmp);
                iv = tmp2;
                iv2 = tmp;
                --len;
                in += AES_BLOCK_SIZE;
                out += AES_BLOCK_SIZE;
            }
            sgx_memcpy(ivec, iv.data, AES_BLOCK_SIZE);
            sgx_memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE);
        }
    }
}
Ejemplo n.º 5
0
static void
TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
	    unsigned len)
{
	__be16 proto;
	__be16 *s;
	int i;

	if (dest != TftpOurPort) {
#ifdef CONFIG_MCAST_TFTP
		if (Multicast
		 && (!Mcast_port || (dest != Mcast_port)))
#endif
			return;
	}
	if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
	    TftpState != STATE_RECV_WRQ && TftpState != STATE_SEND_WRQ)
		return;

	if (len < 2)
		return;
	len -= 2;
	/* warning: don't use increment (++) in ntohs() macros!! */
	s = (__be16 *)pkt;
	proto = *s++;
	pkt = (uchar *)s;
	switch (ntohs(proto)) {

	case TFTP_RRQ:
		break;

	case TFTP_ACK:
#ifdef CONFIG_CMD_TFTPPUT
		if (TftpWriting) {
			if (TftpFinalBlock) {
				tftp_complete();
			} else {
				/*
				 * Move to the next block. We want our block
				 * count to wrap just like the other end!
				 */
				int block = ntohs(*s);
				int ack_ok = (TftpBlock == block);

				TftpBlock = (unsigned short)(block + 1);
				update_block_number();
				if (ack_ok)
					TftpSend(); /* Send next data block */
			}
		}
#endif
		break;

	default:
		break;

#ifdef CONFIG_CMD_TFTPSRV
	case TFTP_WRQ:
		debug("Got WRQ\n");
		TftpRemoteIP = sip;
		TftpRemotePort = src;
		TftpOurPort = 1024 + (get_timer(0) % 3072);
		new_transfer();
		TftpSend(); /* Send ACK(0) */
		break;
#endif

	case TFTP_OACK:
		debug("Got OACK: %s %s\n",
			pkt,
			pkt + strlen((char *)pkt) + 1);
		TftpState = STATE_OACK;
		TftpRemotePort = src;
		/*
		 * Check for 'blksize' option.
		 * Careful: "i" is signed, "len" is unsigned, thus
		 * something like "len-8" may give a *huge* number
		 */
		for (i = 0; i+8 < len; i++) {
			if (strcmp((char *)pkt+i, "blksize") == 0) {
				TftpBlkSize = (unsigned short)
					simple_strtoul((char *)pkt+i+8, NULL,
						       10);
				debug("Blocksize ack: %s, %d\n",
					(char *)pkt+i+8, TftpBlkSize);
			}
#ifdef CONFIG_TFTP_TSIZE
			if (strcmp((char *)pkt+i, "tsize") == 0) {
				TftpTsize = simple_strtoul((char *)pkt+i+6,
							   NULL, 10);
				debug("size = %s, %d\n",
					 (char *)pkt+i+6, TftpTsize);
			}
#endif
		}
#ifdef CONFIG_MCAST_TFTP
		parse_multicast_oack((char *)pkt, len-1);
		if ((Multicast) && (!MasterClient))
			TftpState = STATE_DATA;	/* passive.. */
		else
#endif
#ifdef CONFIG_CMD_TFTPPUT
		if (TftpWriting) {
			/* Get ready to send the first block */
			TftpState = STATE_DATA;
			TftpBlock++;
		}
#endif
		TftpSend(); /* Send ACK or first data block */
		break;
	case TFTP_DATA:
		if (len < 2)
			return;
		len -= 2;
		TftpBlock = ntohs(*(__be16 *)pkt);

		update_block_number();

		if (TftpState == STATE_SEND_RRQ)
			debug("Server did not acknowledge timeout option!\n");

		if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
		    TftpState == STATE_RECV_WRQ) {
			/* first block received */
			TftpState = STATE_DATA;
			TftpRemotePort = src;
			new_transfer();

#ifdef CONFIG_MCAST_TFTP
			if (Multicast) { /* start!=1 common if mcast */
				TftpLastBlock = TftpBlock - 1;
			} else
#endif
			if (TftpBlock != 1) {	/* Assertion */
				printf("\nTFTP error: "
				       "First block is not block 1 (%ld)\n"
				       "Starting again\n\n",
					TftpBlock);
				NetStartAgain();
				break;
			}
		}

		if (TftpBlock == TftpLastBlock) {
			/*
			 *	Same block again; ignore it.
			 */
			break;
		}

		TftpLastBlock = TftpBlock;
		TftpTimeoutCountMax = TIMEOUT_COUNT;
		NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);

		store_block(TftpBlock - 1, pkt + 2, len);

		/*
		 *	Acknowledge the block just received, which will prompt
		 *	the remote for the next one.
		 */
#ifdef CONFIG_MCAST_TFTP
		/* if I am the MasterClient, actively calculate what my next
		 * needed block is; else I'm passive; not ACKING
		 */
		if (Multicast) {
			if (len < TftpBlkSize)  {
				TftpEndingBlock = TftpBlock;
			} else if (MasterClient) {
				TftpBlock = PrevBitmapHole =
					ext2_find_next_zero_bit(
						Bitmap,
						(Mapsize*8),
						PrevBitmapHole);
				if (TftpBlock > ((Mapsize*8) - 1)) {
					printf("tftpfile too big\n");
					/* try to double it and retry */
					Mapsize <<= 1;
					mcast_cleanup();
					NetStartAgain();
					return;
				}
				TftpLastBlock = TftpBlock;
			}
		}
#endif
		TftpSend();

#ifdef CONFIG_MCAST_TFTP
		if (Multicast) {
			if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
				puts("\nMulticast tftp done\n");
				mcast_cleanup();
				net_set_state(NETLOOP_SUCCESS);
			}
		} else
#endif
		if (len < TftpBlkSize)
			tftp_complete();
		break;

	case TFTP_ERROR:
		printf("\nTFTP error: '%s' (%d)\n",
		       pkt + 2, ntohs(*(__be16 *)pkt));

		switch (ntohs(*(__be16 *)pkt)) {
		case TFTP_ERR_FILE_NOT_FOUND:
		case TFTP_ERR_ACCESS_DENIED:
			puts("Not retrying...\n");
			eth_halt();
			net_set_state(NETLOOP_FAIL);
			break;
		case TFTP_ERR_UNDEFINED:
		case TFTP_ERR_DISK_FULL:
		case TFTP_ERR_UNEXPECTED_OPCODE:
		case TFTP_ERR_UNKNOWN_TRANSFER_ID:
		case TFTP_ERR_FILE_ALREADY_EXISTS:
		default:
			puts("Starting again\n\n");
#ifdef CONFIG_MCAST_TFTP
			mcast_cleanup();
#endif
			NetStartAgain();
			break;
		}
		break;
	}
}
Ejemplo n.º 6
0
void AES192_Decrypt_Impl::process_chunk()
{
    const ubyte32 *key_expanded_ptr = key_expanded;

    ubyte32 chunk1 = get_word(chunk);
    ubyte32 chunk2 = get_word(chunk + 4);
    ubyte32 chunk3 = get_word(chunk + 8);
    ubyte32 chunk4 = get_word(chunk + 12);

    ubyte32 s0 = chunk1 ^ key_expanded_ptr[0];
    ubyte32 s1 = chunk2 ^ key_expanded_ptr[1];
    ubyte32 s2 = chunk3 ^ key_expanded_ptr[2];
    ubyte32 s3 = chunk4 ^ key_expanded_ptr[3];

    ubyte32 t0;
    ubyte32 t1;
    ubyte32 t2;
    ubyte32 t3;

    // Round 1
    t0 = table_d0[s0 >> 24] ^ table_d1[(s3 >> 16) & 0xff] ^ table_d2[(s2 >> 8) & 0xff] ^ table_d3[s1 & 0xff] ^ key_expanded_ptr[ 4];
    t1 = table_d0[s1 >> 24] ^ table_d1[(s0 >> 16) & 0xff] ^ table_d2[(s3 >> 8) & 0xff] ^ table_d3[s2 & 0xff] ^ key_expanded_ptr[ 5];
    t2 = table_d0[s2 >> 24] ^ table_d1[(s1 >> 16) & 0xff] ^ table_d2[(s0 >> 8) & 0xff] ^ table_d3[s3 & 0xff] ^ key_expanded_ptr[ 6];
    t3 = table_d0[s3 >> 24] ^ table_d1[(s2 >> 16) & 0xff] ^ table_d2[(s1 >> 8) & 0xff] ^ table_d3[s0 & 0xff] ^ key_expanded_ptr[ 7];
    // Round 2
    s0 = table_d0[t0 >> 24] ^ table_d1[(t3 >> 16) & 0xff] ^ table_d2[(t2 >> 8) & 0xff] ^ table_d3[t1 & 0xff] ^ key_expanded_ptr[ 8];
    s1 = table_d0[t1 >> 24] ^ table_d1[(t0 >> 16) & 0xff] ^ table_d2[(t3 >> 8) & 0xff] ^ table_d3[t2 & 0xff] ^ key_expanded_ptr[ 9];
    s2 = table_d0[t2 >> 24] ^ table_d1[(t1 >> 16) & 0xff] ^ table_d2[(t0 >> 8) & 0xff] ^ table_d3[t3 & 0xff] ^ key_expanded_ptr[10];
    s3 = table_d0[t3 >> 24] ^ table_d1[(t2 >> 16) & 0xff] ^ table_d2[(t1 >> 8) & 0xff] ^ table_d3[t0 & 0xff] ^ key_expanded_ptr[11];
    // Round 3
    t0 = table_d0[s0 >> 24] ^ table_d1[(s3 >> 16) & 0xff] ^ table_d2[(s2 >> 8) & 0xff] ^ table_d3[s1 & 0xff] ^ key_expanded_ptr[12];
    t1 = table_d0[s1 >> 24] ^ table_d1[(s0 >> 16) & 0xff] ^ table_d2[(s3 >> 8) & 0xff] ^ table_d3[s2 & 0xff] ^ key_expanded_ptr[13];
    t2 = table_d0[s2 >> 24] ^ table_d1[(s1 >> 16) & 0xff] ^ table_d2[(s0 >> 8) & 0xff] ^ table_d3[s3 & 0xff] ^ key_expanded_ptr[14];
    t3 = table_d0[s3 >> 24] ^ table_d1[(s2 >> 16) & 0xff] ^ table_d2[(s1 >> 8) & 0xff] ^ table_d3[s0 & 0xff] ^ key_expanded_ptr[15];
    // Round 4
    s0 = table_d0[t0 >> 24] ^ table_d1[(t3 >> 16) & 0xff] ^ table_d2[(t2 >> 8) & 0xff] ^ table_d3[t1 & 0xff] ^ key_expanded_ptr[16];
    s1 = table_d0[t1 >> 24] ^ table_d1[(t0 >> 16) & 0xff] ^ table_d2[(t3 >> 8) & 0xff] ^ table_d3[t2 & 0xff] ^ key_expanded_ptr[17];
    s2 = table_d0[t2 >> 24] ^ table_d1[(t1 >> 16) & 0xff] ^ table_d2[(t0 >> 8) & 0xff] ^ table_d3[t3 & 0xff] ^ key_expanded_ptr[18];
    s3 = table_d0[t3 >> 24] ^ table_d1[(t2 >> 16) & 0xff] ^ table_d2[(t1 >> 8) & 0xff] ^ table_d3[t0 & 0xff] ^ key_expanded_ptr[19];
    // Round 5
    t0 = table_d0[s0 >> 24] ^ table_d1[(s3 >> 16) & 0xff] ^ table_d2[(s2 >> 8) & 0xff] ^ table_d3[s1 & 0xff] ^ key_expanded_ptr[20];
    t1 = table_d0[s1 >> 24] ^ table_d1[(s0 >> 16) & 0xff] ^ table_d2[(s3 >> 8) & 0xff] ^ table_d3[s2 & 0xff] ^ key_expanded_ptr[21];
    t2 = table_d0[s2 >> 24] ^ table_d1[(s1 >> 16) & 0xff] ^ table_d2[(s0 >> 8) & 0xff] ^ table_d3[s3 & 0xff] ^ key_expanded_ptr[22];
    t3 = table_d0[s3 >> 24] ^ table_d1[(s2 >> 16) & 0xff] ^ table_d2[(s1 >> 8) & 0xff] ^ table_d3[s0 & 0xff] ^ key_expanded_ptr[23];
    // Round 6
    s0 = table_d0[t0 >> 24] ^ table_d1[(t3 >> 16) & 0xff] ^ table_d2[(t2 >> 8) & 0xff] ^ table_d3[t1 & 0xff] ^ key_expanded_ptr[24];
    s1 = table_d0[t1 >> 24] ^ table_d1[(t0 >> 16) & 0xff] ^ table_d2[(t3 >> 8) & 0xff] ^ table_d3[t2 & 0xff] ^ key_expanded_ptr[25];
    s2 = table_d0[t2 >> 24] ^ table_d1[(t1 >> 16) & 0xff] ^ table_d2[(t0 >> 8) & 0xff] ^ table_d3[t3 & 0xff] ^ key_expanded_ptr[26];
    s3 = table_d0[t3 >> 24] ^ table_d1[(t2 >> 16) & 0xff] ^ table_d2[(t1 >> 8) & 0xff] ^ table_d3[t0 & 0xff] ^ key_expanded_ptr[27];
    // Round 7
    t0 = table_d0[s0 >> 24] ^ table_d1[(s3 >> 16) & 0xff] ^ table_d2[(s2 >> 8) & 0xff] ^ table_d3[s1 & 0xff] ^ key_expanded_ptr[28];
    t1 = table_d0[s1 >> 24] ^ table_d1[(s0 >> 16) & 0xff] ^ table_d2[(s3 >> 8) & 0xff] ^ table_d3[s2 & 0xff] ^ key_expanded_ptr[29];
    t2 = table_d0[s2 >> 24] ^ table_d1[(s1 >> 16) & 0xff] ^ table_d2[(s0 >> 8) & 0xff] ^ table_d3[s3 & 0xff] ^ key_expanded_ptr[30];
    t3 = table_d0[s3 >> 24] ^ table_d1[(s2 >> 16) & 0xff] ^ table_d2[(s1 >> 8) & 0xff] ^ table_d3[s0 & 0xff] ^ key_expanded_ptr[31];
    // Round 8
    s0 = table_d0[t0 >> 24] ^ table_d1[(t3 >> 16) & 0xff] ^ table_d2[(t2 >> 8) & 0xff] ^ table_d3[t1 & 0xff] ^ key_expanded_ptr[32];
    s1 = table_d0[t1 >> 24] ^ table_d1[(t0 >> 16) & 0xff] ^ table_d2[(t3 >> 8) & 0xff] ^ table_d3[t2 & 0xff] ^ key_expanded_ptr[33];
    s2 = table_d0[t2 >> 24] ^ table_d1[(t1 >> 16) & 0xff] ^ table_d2[(t0 >> 8) & 0xff] ^ table_d3[t3 & 0xff] ^ key_expanded_ptr[34];
    s3 = table_d0[t3 >> 24] ^ table_d1[(t2 >> 16) & 0xff] ^ table_d2[(t1 >> 8) & 0xff] ^ table_d3[t0 & 0xff] ^ key_expanded_ptr[35];
    // Round 9
    t0 = table_d0[s0 >> 24] ^ table_d1[(s3 >> 16) & 0xff] ^ table_d2[(s2 >> 8) & 0xff] ^ table_d3[s1 & 0xff] ^ key_expanded_ptr[36];
    t1 = table_d0[s1 >> 24] ^ table_d1[(s0 >> 16) & 0xff] ^ table_d2[(s3 >> 8) & 0xff] ^ table_d3[s2 & 0xff] ^ key_expanded_ptr[37];
    t2 = table_d0[s2 >> 24] ^ table_d1[(s1 >> 16) & 0xff] ^ table_d2[(s0 >> 8) & 0xff] ^ table_d3[s3 & 0xff] ^ key_expanded_ptr[38];
    t3 = table_d0[s3 >> 24] ^ table_d1[(s2 >> 16) & 0xff] ^ table_d2[(s1 >> 8) & 0xff] ^ table_d3[s0 & 0xff] ^ key_expanded_ptr[39];
    // Round 10
    s0 = table_d0[t0 >> 24] ^ table_d1[(t3 >> 16) & 0xff] ^ table_d2[(t2 >> 8) & 0xff] ^ table_d3[t1 & 0xff] ^ key_expanded_ptr[40];
    s1 = table_d0[t1 >> 24] ^ table_d1[(t0 >> 16) & 0xff] ^ table_d2[(t3 >> 8) & 0xff] ^ table_d3[t2 & 0xff] ^ key_expanded_ptr[41];
    s2 = table_d0[t2 >> 24] ^ table_d1[(t1 >> 16) & 0xff] ^ table_d2[(t0 >> 8) & 0xff] ^ table_d3[t3 & 0xff] ^ key_expanded_ptr[42];
    s3 = table_d0[t3 >> 24] ^ table_d1[(t2 >> 16) & 0xff] ^ table_d2[(t1 >> 8) & 0xff] ^ table_d3[t0 & 0xff] ^ key_expanded_ptr[43];
    // Round 11
    t0 = table_d0[s0 >> 24] ^ table_d1[(s3 >> 16) & 0xff] ^ table_d2[(s2 >> 8) & 0xff] ^ table_d3[s1 & 0xff] ^ key_expanded_ptr[44];
    t1 = table_d0[s1 >> 24] ^ table_d1[(s0 >> 16) & 0xff] ^ table_d2[(s3 >> 8) & 0xff] ^ table_d3[s2 & 0xff] ^ key_expanded_ptr[45];
    t2 = table_d0[s2 >> 24] ^ table_d1[(s1 >> 16) & 0xff] ^ table_d2[(s0 >> 8) & 0xff] ^ table_d3[s3 & 0xff] ^ key_expanded_ptr[46];
    t3 = table_d0[s3 >> 24] ^ table_d1[(s2 >> 16) & 0xff] ^ table_d2[(s1 >> 8) & 0xff] ^ table_d3[s0 & 0xff] ^ key_expanded_ptr[47];

    key_expanded_ptr += aes192_num_rounds_nr * 4;

    // Apply last round
    s0 = (sbox_inverse_substitution_values[(t0 >> 24) ] & 0xff000000) ^ (sbox_inverse_substitution_values[(t3 >> 16) & 0xff] & 0x00ff0000) ^ (sbox_inverse_substitution_values[(t2 >> 8) & 0xff] & 0x0000ff00) ^ (sbox_inverse_substitution_values[(t1 ) & 0xff] & 0x000000ff) ^ key_expanded_ptr[0];
    s1 = (sbox_inverse_substitution_values[(t1 >> 24) ] & 0xff000000) ^ (sbox_inverse_substitution_values[(t0 >> 16) & 0xff] & 0x00ff0000) ^ (sbox_inverse_substitution_values[(t3 >> 8) & 0xff] & 0x0000ff00) ^ (sbox_inverse_substitution_values[(t2 ) & 0xff] & 0x000000ff) ^ key_expanded_ptr[1];
    s2 = (sbox_inverse_substitution_values[(t2 >> 24) ] & 0xff000000) ^ (sbox_inverse_substitution_values[(t1 >> 16) & 0xff] & 0x00ff0000) ^ (sbox_inverse_substitution_values[(t0 >> 8) & 0xff] & 0x0000ff00) ^ (sbox_inverse_substitution_values[(t3 ) & 0xff] & 0x000000ff) ^ key_expanded_ptr[2];
    s3 = (sbox_inverse_substitution_values[(t3 >> 24) ] & 0xff000000) ^ (sbox_inverse_substitution_values[(t2 >> 16) & 0xff] & 0x00ff0000) ^ (sbox_inverse_substitution_values[(t1 >> 8) & 0xff] & 0x0000ff00) ^ (sbox_inverse_substitution_values[(t0 ) & 0xff] & 0x000000ff) ^ key_expanded_ptr[3];

    s0 ^= initialisation_vector_1;
    s1 ^= initialisation_vector_2;
    s2 ^= initialisation_vector_3;
    s3 ^= initialisation_vector_4;
    store_block(s0, s1, s2, s3, databuffer);

    initialisation_vector_1 = chunk1;
    initialisation_vector_2 = chunk2;
    initialisation_vector_3 = chunk3;
    initialisation_vector_4 = chunk4;

}
Ejemplo n.º 7
0
void AES192_Encrypt_Impl::process_chunk()
{

	const ubyte32 *key_expanded_ptr = key_expanded;

	/* Electronic Codebook Mode
	ubyte32 s0 = get_word(chunk) ^ key_expanded_ptr[0];
	ubyte32 s1 = get_word(chunk + 4) ^ key_expanded_ptr[1];
	ubyte32 s2 = get_word(chunk + 8) ^ key_expanded_ptr[2];
	ubyte32 s3 = get_word(chunk + 12) ^ key_expanded_ptr[3];
	*/

	// Cipher Block Chaining Mode
	ubyte32 s0 = initialisation_vector_1 ^ get_word(chunk) ^ key_expanded_ptr[0];
	ubyte32 s1 = initialisation_vector_2 ^ get_word(chunk + 4) ^ key_expanded_ptr[1];
	ubyte32 s2 = initialisation_vector_3 ^ get_word(chunk + 8) ^ key_expanded_ptr[2];
	ubyte32 s3 = initialisation_vector_4 ^ get_word(chunk + 12) ^ key_expanded_ptr[3];

	ubyte32 t0;
	ubyte32 t1;
	ubyte32 t2;
	ubyte32 t3;

	// round 1
	t0 = table_e0[s0 >> 24] ^ table_e1[(s1 >> 16) & 0xff] ^ table_e2[(s2 >>  8) & 0xff] ^ table_e3[s3 & 0xff] ^ key_expanded_ptr[ 4];
	t1 = table_e0[s1 >> 24] ^ table_e1[(s2 >> 16) & 0xff] ^ table_e2[(s3 >>  8) & 0xff] ^ table_e3[s0 & 0xff] ^ key_expanded_ptr[ 5];
	t2 = table_e0[s2 >> 24] ^ table_e1[(s3 >> 16) & 0xff] ^ table_e2[(s0 >>  8) & 0xff] ^ table_e3[s1 & 0xff] ^ key_expanded_ptr[ 6];
	t3 = table_e0[s3 >> 24] ^ table_e1[(s0 >> 16) & 0xff] ^ table_e2[(s1 >>  8) & 0xff] ^ table_e3[s2 & 0xff] ^ key_expanded_ptr[ 7];
	// round 2
	s0 = table_e0[t0 >> 24] ^ table_e1[(t1 >> 16) & 0xff] ^ table_e2[(t2 >>  8) & 0xff] ^ table_e3[t3 & 0xff] ^ key_expanded_ptr[ 8];
	s1 = table_e0[t1 >> 24] ^ table_e1[(t2 >> 16) & 0xff] ^ table_e2[(t3 >>  8) & 0xff] ^ table_e3[t0 & 0xff] ^ key_expanded_ptr[ 9];
	s2 = table_e0[t2 >> 24] ^ table_e1[(t3 >> 16) & 0xff] ^ table_e2[(t0 >>  8) & 0xff] ^ table_e3[t1 & 0xff] ^ key_expanded_ptr[10];
	s3 = table_e0[t3 >> 24] ^ table_e1[(t0 >> 16) & 0xff] ^ table_e2[(t1 >>  8) & 0xff] ^ table_e3[t2 & 0xff] ^ key_expanded_ptr[11];
	// round 3
	t0 = table_e0[s0 >> 24] ^ table_e1[(s1 >> 16) & 0xff] ^ table_e2[(s2 >>  8) & 0xff] ^ table_e3[s3 & 0xff] ^ key_expanded_ptr[12];
	t1 = table_e0[s1 >> 24] ^ table_e1[(s2 >> 16) & 0xff] ^ table_e2[(s3 >>  8) & 0xff] ^ table_e3[s0 & 0xff] ^ key_expanded_ptr[13];
	t2 = table_e0[s2 >> 24] ^ table_e1[(s3 >> 16) & 0xff] ^ table_e2[(s0 >>  8) & 0xff] ^ table_e3[s1 & 0xff] ^ key_expanded_ptr[14];
	t3 = table_e0[s3 >> 24] ^ table_e1[(s0 >> 16) & 0xff] ^ table_e2[(s1 >>  8) & 0xff] ^ table_e3[s2 & 0xff] ^ key_expanded_ptr[15];
	// round 4
	s0 = table_e0[t0 >> 24] ^ table_e1[(t1 >> 16) & 0xff] ^ table_e2[(t2 >>  8) & 0xff] ^ table_e3[t3 & 0xff] ^ key_expanded_ptr[16];
	s1 = table_e0[t1 >> 24] ^ table_e1[(t2 >> 16) & 0xff] ^ table_e2[(t3 >>  8) & 0xff] ^ table_e3[t0 & 0xff] ^ key_expanded_ptr[17];
	s2 = table_e0[t2 >> 24] ^ table_e1[(t3 >> 16) & 0xff] ^ table_e2[(t0 >>  8) & 0xff] ^ table_e3[t1 & 0xff] ^ key_expanded_ptr[18];
	s3 = table_e0[t3 >> 24] ^ table_e1[(t0 >> 16) & 0xff] ^ table_e2[(t1 >>  8) & 0xff] ^ table_e3[t2 & 0xff] ^ key_expanded_ptr[19];
	// round 5
	t0 = table_e0[s0 >> 24] ^ table_e1[(s1 >> 16) & 0xff] ^ table_e2[(s2 >>  8) & 0xff] ^ table_e3[s3 & 0xff] ^ key_expanded_ptr[20];
	t1 = table_e0[s1 >> 24] ^ table_e1[(s2 >> 16) & 0xff] ^ table_e2[(s3 >>  8) & 0xff] ^ table_e3[s0 & 0xff] ^ key_expanded_ptr[21];
	t2 = table_e0[s2 >> 24] ^ table_e1[(s3 >> 16) & 0xff] ^ table_e2[(s0 >>  8) & 0xff] ^ table_e3[s1 & 0xff] ^ key_expanded_ptr[22];
	t3 = table_e0[s3 >> 24] ^ table_e1[(s0 >> 16) & 0xff] ^ table_e2[(s1 >>  8) & 0xff] ^ table_e3[s2 & 0xff] ^ key_expanded_ptr[23];
	// round 6
	s0 = table_e0[t0 >> 24] ^ table_e1[(t1 >> 16) & 0xff] ^ table_e2[(t2 >>  8) & 0xff] ^ table_e3[t3 & 0xff] ^ key_expanded_ptr[24];
	s1 = table_e0[t1 >> 24] ^ table_e1[(t2 >> 16) & 0xff] ^ table_e2[(t3 >>  8) & 0xff] ^ table_e3[t0 & 0xff] ^ key_expanded_ptr[25];
	s2 = table_e0[t2 >> 24] ^ table_e1[(t3 >> 16) & 0xff] ^ table_e2[(t0 >>  8) & 0xff] ^ table_e3[t1 & 0xff] ^ key_expanded_ptr[26];
	s3 = table_e0[t3 >> 24] ^ table_e1[(t0 >> 16) & 0xff] ^ table_e2[(t1 >>  8) & 0xff] ^ table_e3[t2 & 0xff] ^ key_expanded_ptr[27];
	// round 7
	t0 = table_e0[s0 >> 24] ^ table_e1[(s1 >> 16) & 0xff] ^ table_e2[(s2 >>  8) & 0xff] ^ table_e3[s3 & 0xff] ^ key_expanded_ptr[28];
	t1 = table_e0[s1 >> 24] ^ table_e1[(s2 >> 16) & 0xff] ^ table_e2[(s3 >>  8) & 0xff] ^ table_e3[s0 & 0xff] ^ key_expanded_ptr[29];
	t2 = table_e0[s2 >> 24] ^ table_e1[(s3 >> 16) & 0xff] ^ table_e2[(s0 >>  8) & 0xff] ^ table_e3[s1 & 0xff] ^ key_expanded_ptr[30];
	t3 = table_e0[s3 >> 24] ^ table_e1[(s0 >> 16) & 0xff] ^ table_e2[(s1 >>  8) & 0xff] ^ table_e3[s2 & 0xff] ^ key_expanded_ptr[31];
	// round 8
	s0 = table_e0[t0 >> 24] ^ table_e1[(t1 >> 16) & 0xff] ^ table_e2[(t2 >>  8) & 0xff] ^ table_e3[t3 & 0xff] ^ key_expanded_ptr[32];
	s1 = table_e0[t1 >> 24] ^ table_e1[(t2 >> 16) & 0xff] ^ table_e2[(t3 >>  8) & 0xff] ^ table_e3[t0 & 0xff] ^ key_expanded_ptr[33];
	s2 = table_e0[t2 >> 24] ^ table_e1[(t3 >> 16) & 0xff] ^ table_e2[(t0 >>  8) & 0xff] ^ table_e3[t1 & 0xff] ^ key_expanded_ptr[34];
	s3 = table_e0[t3 >> 24] ^ table_e1[(t0 >> 16) & 0xff] ^ table_e2[(t1 >>  8) & 0xff] ^ table_e3[t2 & 0xff] ^ key_expanded_ptr[35];
	// round 9
	t0 = table_e0[s0 >> 24] ^ table_e1[(s1 >> 16) & 0xff] ^ table_e2[(s2 >>  8) & 0xff] ^ table_e3[s3 & 0xff] ^ key_expanded_ptr[36];
	t1 = table_e0[s1 >> 24] ^ table_e1[(s2 >> 16) & 0xff] ^ table_e2[(s3 >>  8) & 0xff] ^ table_e3[s0 & 0xff] ^ key_expanded_ptr[37];
	t2 = table_e0[s2 >> 24] ^ table_e1[(s3 >> 16) & 0xff] ^ table_e2[(s0 >>  8) & 0xff] ^ table_e3[s1 & 0xff] ^ key_expanded_ptr[38];
	t3 = table_e0[s3 >> 24] ^ table_e1[(s0 >> 16) & 0xff] ^ table_e2[(s1 >>  8) & 0xff] ^ table_e3[s2 & 0xff] ^ key_expanded_ptr[39];
	// round 10
	s0 = table_e0[t0 >> 24] ^ table_e1[(t1 >> 16) & 0xff] ^ table_e2[(t2 >>  8) & 0xff] ^ table_e3[t3 & 0xff] ^ key_expanded_ptr[40];
	s1 = table_e0[t1 >> 24] ^ table_e1[(t2 >> 16) & 0xff] ^ table_e2[(t3 >>  8) & 0xff] ^ table_e3[t0 & 0xff] ^ key_expanded_ptr[41];
	s2 = table_e0[t2 >> 24] ^ table_e1[(t3 >> 16) & 0xff] ^ table_e2[(t0 >>  8) & 0xff] ^ table_e3[t1 & 0xff] ^ key_expanded_ptr[42];
	s3 = table_e0[t3 >> 24] ^ table_e1[(t0 >> 16) & 0xff] ^ table_e2[(t1 >>  8) & 0xff] ^ table_e3[t2 & 0xff] ^ key_expanded_ptr[43];
	// round 11
	t0 = table_e0[s0 >> 24] ^ table_e1[(s1 >> 16) & 0xff] ^ table_e2[(s2 >>  8) & 0xff] ^ table_e3[s3 & 0xff] ^ key_expanded_ptr[44];
	t1 = table_e0[s1 >> 24] ^ table_e1[(s2 >> 16) & 0xff] ^ table_e2[(s3 >>  8) & 0xff] ^ table_e3[s0 & 0xff] ^ key_expanded_ptr[45];
	t2 = table_e0[s2 >> 24] ^ table_e1[(s3 >> 16) & 0xff] ^ table_e2[(s0 >>  8) & 0xff] ^ table_e3[s1 & 0xff] ^ key_expanded_ptr[46];
	t3 = table_e0[s3 >> 24] ^ table_e1[(s0 >> 16) & 0xff] ^ table_e2[(s1 >>  8) & 0xff] ^ table_e3[s2 & 0xff] ^ key_expanded_ptr[47];

	key_expanded_ptr += aes192_num_rounds_nr * 4;
 
	// Apply last round
	s0 = (sbox_substitution_values[(t0 >> 24) ] & 0xff000000) ^ (sbox_substitution_values[(t1 >> 16) & 0xff] & 0x00ff0000) ^ (sbox_substitution_values[(t2 >> 8) & 0xff] & 0x0000ff00) ^ (sbox_substitution_values[(t3 ) & 0xff] & 0x000000ff) ^ key_expanded_ptr[0];
	s1 = (sbox_substitution_values[(t1 >> 24) ] & 0xff000000) ^ (sbox_substitution_values[(t2 >> 16) & 0xff] & 0x00ff0000) ^ (sbox_substitution_values[(t3 >> 8) & 0xff] & 0x0000ff00) ^ (sbox_substitution_values[(t0 ) & 0xff] & 0x000000ff) ^ key_expanded_ptr[1];
	s2 = (sbox_substitution_values[(t2 >> 24) ] & 0xff000000) ^ (sbox_substitution_values[(t3 >> 16) & 0xff] & 0x00ff0000) ^ (sbox_substitution_values[(t0 >> 8) & 0xff] & 0x0000ff00) ^ (sbox_substitution_values[(t1 ) & 0xff] & 0x000000ff) ^ key_expanded_ptr[2];
	s3 = (sbox_substitution_values[(t3 >> 24) ] & 0xff000000) ^ (sbox_substitution_values[(t0 >> 16) & 0xff] & 0x00ff0000) ^ (sbox_substitution_values[(t1 >> 8) & 0xff] & 0x0000ff00) ^ (sbox_substitution_values[(t2 ) & 0xff] & 0x000000ff) ^ key_expanded_ptr[3];

	store_block(s0, s1, s2, s3, databuffer);

	initialisation_vector_1 = s0;
	initialisation_vector_2 = s1;
	initialisation_vector_3 = s2;
	initialisation_vector_4 = s3;
	
}
Ejemplo n.º 8
0
static void TftpHandler (unsigned char * pkt, unsigned dest, unsigned src, unsigned len) {


	if (dest != TftpOurPort) {
		return;
	}
	if (TftpState != STATE_RRQ && src != TftpServerPort) {
		return;
	}

	if (len < 2) {
		return;
	}
	len -= 2;

	unsigned short val = *((unsigned short *) pkt);
	pkt += sizeof(unsigned short);

	switch (val) {

	case TFTP_RRQ:
	case TFTP_WRQ:
	case TFTP_ACK:
		break;
	default:
		break;

	case TFTP_DATA:
		if (len < 2)
			return;
		len -= 2;
		TftpBlock = *(unsigned short *)pkt;
		if (((TftpBlock - 1) % 10) == 0) {
			printf("#");
			TftpTimeoutCount = 0;
		} else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
			printf ("\n\t ");
		}

		if (TftpState == STATE_RRQ) {
			TftpState = STATE_DATA;
			TftpServerPort = src;
			TftpLastBlock = 0;

			if (TftpBlock != 1) {	/* Assertion */
				printf ("\nTFTP error: "
					"First block is not block 1 (%d)\n"
					"Starting again\n\n",
					TftpBlock);
				NetStartAgain ();
				break;
			}
		}

		if (TftpBlock == TftpLastBlock) {
			/*
			 *	Same block again; ignore it.
			 */
			break;
		}

		TftpLastBlock = TftpBlock;
		NetSetTimeout (TIMEOUT * 1000, TftpTimeout);

		store_block (TftpBlock - 1, pkt + 2, len);

		/*
		 *	Acknoledge the block just received, which will prompt
		 *	the server for the next one.
		 */
		TftpSend ();

		if (len < 512) {
			/*
			 *	We received the whole thing.  Try to
			 *	run it.
			 */
			printf ("\ndone\n");
			NetState = NETLOOP_SUCCESS;
		}
		break;

	case TFTP_ERROR:
		printf ("\nTFTP error: '%s' (%d)\n", pkt + 2, *(unsigned short *)pkt);
		printf ("Starting again\n\n");
		NetStartAgain ();
		break;
	}
}
Ejemplo n.º 9
0
}
/* Read the .block file, get components and colcand */
void read_and_solve_blocks(FILE *fb, const char *fn)
{
	init_expand();
	size_t n;
	int col;
	char *line = NULL;
	int bnumber = 0;
	struct dyStack *ge, *co;
	int i, components, m_cnt;
	bool *colcand;
	bool *candidates;
	Block *b;
	AllocVar(b);
	AllocArray(colcand, another_cols);
	AllocArray(candidates, another_rows);
	ge = dsNew(another_rows);
	co = dsNew(another_cols);
	FILE *fo = mustOpen(fn, "w");

	/* main course starts here */
	while (getline(&line, &n, fb) != -1)
	{
	        /* fast forward to a line that contains BC*/
		/* strncmp compares up to num characters of the C string str1 to those of the C string str2
		 * strncmp ( const char * str1, const char * str2, size_t num )*/
		while (strncmp(line, "BC", 2)!=0) 
		{
			if (getline(&line, &n, fb)==-1) 
				exit(0);
		}
		components = 0;
		col = 0;
		dsClear(ge);
		dsClear(co);
		for (i=0; i< another_cols; i++)
			colcand[i] = FALSE;
		for (i=0; i< another_rows; i++)
			candidates[i] = TRUE;
		/* read genes from block */		
		getline(&line, &n, fb);
		atom = strtok(line, delims);
		atom = strtok(NULL, delims);
		while((atom = strtok(NULL, delims)) != NULL)
		{
			/* look up for genes number */
			if (strlen(atom) == 0) continue;			
			for(i=0; i<another_rows; i++)
			{
				if (strcmp(atom ,another_genes[i]) == 0) break;
			}
			candidates[i] = FALSE;			
			dsPush(ge, i);
			components++;
		}
		/* read conditions from block */
		getline(&line, &n, fb);
		atom = strtok(line, delims);
		atom = strtok(NULL, delims);
		while((atom = strtok(NULL, delims)) != NULL)
		{
			/*if (strlen(atom) < 5) break;*/			
			if (strlen(atom) == 0) continue;			
			for(i=0; i<another_cols; i++)
				if (strcmp(atom, another_conds[i]) == 0) break;
			colcand[i] = TRUE;
			dsPush(co, i);
			col++;
		}
		
		b->block_rows_pre = components;
		/* add some possible genes */
		for( i = 0; i < another_rows; i++)
		{
			m_cnt = intersect_row(colcand, another_arr_c[dsItem(ge,0)], another_arr_c[i], another_cols);
			printf ("%d\n",m_cnt);
			if( candidates[i] && (m_cnt >= (int)floor( (double)col * po->TOLERANCE)) )
			{
				dsPush(ge,i);
				components++;
				candidates[i] = FALSE;
			}
		}
		/* add genes that negative regulated to the consensus */
		for( i = 0; i < another_rows; i++)
		{
			m_cnt = reverse_row(colcand, another_arr_c[dsItem(ge,0)], another_arr_c[i], another_cols);
			if( candidates[i] && (m_cnt >= (int)floor( (double)col * po->TOLERANCE)) )
			{
				dsPush(ge,i);
				components++;
				candidates[i] = FALSE;
			}
		}
		if(dsSize(ge) > 1)
		{		
			store_block(b, ge, co);
			/*another_print_bc(fo, b, bnumber);*/
			print_bc(fo, b, bnumber++);
		}