Example #1
0
int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask)
{
    struct crypt_op cop;
    char *buffer, iv[32];
    static int val = 23;
    struct timeval start, end;
    double total = 0;
    double secs, ddata, dspeed;
    char metric[16];

    if (alignmask) {
        if (posix_memalign((void **)&buffer, MAX(alignmask + 1, sizeof(void*)), chunksize)) {
            printf("posix_memalign() failed! (mask %x, size: %d)\n", alignmask+1, chunksize);
            return 1;
        }
    } else {
        if (!(buffer = malloc(chunksize))) {
            perror("malloc()");
            return 1;
        }
    }

    memset(iv, 0x23, 32);

    printf("\tEncrypting in chunks of %d bytes: ", chunksize);
    fflush(stdout);

    memset(buffer, val++, chunksize);

    must_finish = 0;
    alarm(5);

    gettimeofday(&start, NULL);
    do {
        memset(&cop, 0, sizeof(cop));
        cop.ses = sess->ses;
        cop.len = chunksize;
        cop.iv = (unsigned char *)iv;
        cop.op = COP_ENCRYPT;
        cop.src = cop.dst = (unsigned char *)buffer;

        if (ioctl(fdc, CIOCCRYPT, &cop)) {
            perror("ioctl(CIOCCRYPT)");
            return 1;
        }
        total+=chunksize;
    } while(must_finish==0);
    gettimeofday(&end, NULL);

    secs = udifftimeval(start, end)/ 1000000.0;

    value2human(si, total, secs, &ddata, &dspeed, metric);
    printf ("done. %.2f %s in %.2f secs: ", ddata, metric, secs);
    printf ("%.2f %s/sec\n", dspeed, metric);

    free(buffer);
    return 0;
}
int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask)
{
	struct crypt_op cop;
	char *buffer[64], iv[32];
	static int val = 23;
	struct timeval start, end;
	double total = 0;
	double secs, ddata, dspeed;
	char metric[16];
	int rc, wqueue = 0, bufidx = 0;

	memset(iv, 0x23, 32);

	printf("\tEncrypting in chunks of %d bytes: ", chunksize);
	fflush(stdout);

	for (rc = 0; rc < 64; rc++) {
		if (alignmask) {
			if (posix_memalign((void **)(buffer + rc), alignmask + 1, chunksize)) {
				printf("posix_memalign() failed!\n");
				return 1;
			}
		} else {
			if (!(buffer[rc] = malloc(chunksize))) {
				perror("malloc()");
				return 1;
			}
		}
		memset(buffer[rc], val++, chunksize);
	}
	pfd.fd = fdc;
	pfd.events = POLLOUT | POLLIN;

	must_finish = 0;
	alarm(5);

	gettimeofday(&start, NULL);
	do {
		if ((rc = poll(&pfd, 1, 100)) < 0) {
			if (errno & (ERESTART | EINTR))
				continue;
			fprintf(stderr, "errno = %d ", errno);
			perror("poll()");
			return 1;
		}

		if (pfd.revents & POLLOUT) {
			memset(&cop, 0, sizeof(cop));
			cop.ses = sess->ses;
			cop.len = chunksize;
			cop.iv = (unsigned char *)iv;
			cop.op = COP_ENCRYPT;
			cop.src = cop.dst = (unsigned char *)buffer[bufidx];
			bufidx = (bufidx + 1) % 64;

			if (ioctl(fdc, CIOCASYNCCRYPT, &cop)) {
				perror("ioctl(CIOCASYNCCRYPT)");
				return 1;
			}
			wqueue++;
		}
		if (pfd.revents & POLLIN) {
			if (ioctl(fdc, CIOCASYNCFETCH, &cop)) {
				perror("ioctl(CIOCASYNCFETCH)");
				return 1;
			}
			wqueue--;
			total += cop.len;
		}
	} while(!must_finish || wqueue);
	gettimeofday(&end, NULL);

	secs = udifftimeval(start, end)/ 1000000.0;

	value2human(total, secs, &ddata, &dspeed, metric);
	printf ("done. %.2f %s in %.2f secs: ", ddata, metric, secs);
	printf ("%.2f %s/sec\n", dspeed, metric);

	for (rc = 0; rc < 64; rc++)
		free(buffer[rc]);
	return 0;
}