sksatest.c 4.69 KB
#include "testlib.c"

char pname[256];
char *myname;
char *dname = DEVICE_NAME;

#include "fillcard.c"

static int
write_sksa(BBCHandle h, const char *fname, int option)
{
	unsigned char *buf;
	struct stat sb;
	int len;
	int fd;
	int rv1, rv2;

#ifndef WIN32
	if ((fd = open(fname, O_RDONLY )) < 0) {
#else
	if ((fd = open(fname, O_RDONLY | O_BINARY)) < 0) {
#endif
		printf("%s: open %s fails (errno %d)\n", pname, fname, errno);
		return fd;
	}

	if (fstat(fd, &sb) < 0) {
		printf("%s: stat %s fails (errno %d)\n", pname, fname, errno);
		return -1;
	}

	len = sb.st_size;
	if ((buf = malloc(len)) == NULL) {
		perror("malloc");
		exit(1);
	}

	if ((rv1 = read(fd, buf, len)) < len) {
		printf("%s: read %s fails (ret %d, len %d)\n", pname, fname, rv1, len);
		rv1 = -1;
		goto out;
	}

	rv1 = BBCVerifySKSA2(h, buf, len, option);
	printf("%s: BBCVerifySKSA %d %s returns %d\n", pname, option, fname, rv1);

    if (option == BBC_SKSA_FORCE_SA2) {
        int tmplen = len - BLK_SIZE;

        /* Only SA2 is to be written - should get BBC_SA1DIFFERS */
        if (rv1 != BBC_SA1DIFFERS)
            goto out;
        
        /* Now force part of SKSA to be written and try again */
        rv1 = BBCVerifySKSA2(h, buf, tmplen, BBC_SKSA_FORCE_ALL);
        if (rv1 != 0 && rv1 != tmplen) 
            goto out;

        /* Try again */
        rv1 = BBCVerifySKSA2(h, buf, len, option);
        printf("%s: BBCVerifySKSA %d %s returns %d\n", pname, option, fname, rv1);
    }

	if (rv1 != 0 && rv1 != len)
		goto out;

    /* Write SKSA2 again */
	rv2 = BBCVerifySKSA2(h, buf, len, option);
	printf("%s: BBCVerifySKSA-2 %d %s returns %d\n", pname, option, fname, rv2);
    if (option == BBC_SKSA_FORCE_ALL) {
        /* Forcing the SKSA to be replaced - rv2 should be same as len */
        if (rv2 != len) {
            printf("%s: Failure: BBCVerifySKSA-2 %d %s return not length\n",
                   pname, option, fname);
            rv1 = -1;
        }
    }
    else {
        /* All other cases - rv2 should be 0 (SKSA not replaced) */
        if (rv2 != 0) {
            printf("%s: Failure: BBCVerifySKSA-2 %d %s return not zero\n",
                   pname, option, fname);
            rv1 = -1;
        }
    }

out:
	close(fd);
	free(buf);
	return rv1;
}

static void
do_sksa_test(BBCHandle h)
{
	int rv;
	int blks;
	char fname[32];
	int len;

    int sa1len = 1;
	for (blks = 1; blks < 66 ; blks++) {
		len = blks * BLK_SIZE;
		sprintf(fname, "sksafile.%d", blks);
        
        if (blks-2 < 4) {
            printf("%s: create sksa file %s, len %d (sk=%d, sa1=%d, sa2=%d)\n", 
                   pname, fname, len, blks, 0, 0);
            mksksa(fname, blks-2, 0, 0);
        }
        else {
            printf("%s: create sksa file %s, len %d (sk=%d, sa1=%d, sa2=%d)\n", 
                   pname, fname, len, 4, sa1len, blks - 4 - sa1len);
            mksksa(fname, 4, sa1len, blks - 2 - 4 - sa1len);
            if (blks % 4 == 0) {
                sa1len++;
            }
        }
	}

	/*
	 * Write the biggest file that will fit first
	 */
	for (blks = 65; blks > 0; blks--) {
		sprintf(fname, "sksafile.%d", blks);
		if ((rv = write_sksa(h, fname, blks % 4)) >= 0)
			break;
	}

	for (blks = 1; blks < 66 ; blks++) {
		len = blks * BLK_SIZE;
		sprintf(fname, "sksafile.%d", blks);
		if ((rv = write_sksa(h, fname, blks % 4)) != len) {
			printf("%s: write_sksa file %s, len %d ret %d ERROR\n", 
                   pname, fname, len, rv);
		}
	}
}

int
main(int argc, char **argv)
{
	BBCHandle h;
	int loops = 0;
	int cardsize, freeblks, reservedblks, badblks;
	int rv;

	myname = argv[0];
	if (argc > 1)
		dname = argv[argc - 1];
	setbuf(stdout, NULL);
	setbuf(stderr, NULL);

	while (1) {
		sprintf(pname, "%s(%d)", myname, ++loops);
		printf("%s: Doing BBCInit\n", pname);
#ifndef WIN32
		h = BBCInit(dname, BBC_SYNC);
#else
		h = BBCInit(BBC_SYNC);
#endif
		printf("%s: BBCInit(%s) returns %d\n", pname, dname, h);
		if (h < 0) continue;
		while (!BBCCardPresent(h))  {
			printf("%s: BBCCardPresent returns false\n", pname);
		}
		if ((rv = BBCStats(h, &cardsize, &reservedblks, &freeblks, &badblks)) < 0 && rv != BBC_NOFORMAT) {
			printf("%s: BBCStats returns %d\n", pname, rv);
			BBCClose(h);
			continue;
		}
		printf("%s: BBCStats cardsize %d, freeblks %d\n", pname, cardsize, freeblks);
		printf("%s: format card ...\n", pname);
		rv = BBCSetLed(h, BBC_LED_ORANGE);
		rv = BBCFormatCard(h);
		printf("%s: BBCFormatCard returns %d\n", pname, rv);
		if (h < 0) continue;
  		do_fill_card(h);
		//do_check_files(h);
		//do_sksa_test(h);
		//write_sksa(h, "sksa.aes", 3);
		rv = BBCSetLed(h, BBC_LED_OFF);
		rv = BBCClose(h);
		printf("%s: BBCClose returns %d\n", pname, rv);
#if 0
		printf("%s: exit to check status!\n", pname);
		exit(1);
#endif
	}

	exit(0);
}