Esempio n. 1
0
static void rijndaelIVKAT (FILE *fp, int keyLength) {
	int i;
	BYTE pt[4*4], ct[4*4];
	BYTE keyMaterial[320];
	keyInstance keyInst;
	cipherInstance cipherInst;
	char format[10];
#ifdef TRACE_KAT_MCT
	printf ("Executing Intermediate value KAT (key %d): ", keyLength);
	fflush (stdout);
#endif /* ?TRACE_KAT_MCT */

	fprintf(fp,
		"\n"
		"==========\n"
		"\n"
		"KEYSIZE=%d\n",
		keyLength);
	fflush(fp);
	memset(keyMaterial, 0, sizeof (keyMaterial));
	for (i = 0; i < keyLength/8; i++) {
		sprintf(&keyMaterial[2*i], "%02X", i);
	}
	fprintf(fp, "KEY=%s\n", keyMaterial);
	
	for (i = 0; i < 16; i++) {
		pt[i] = i;
	}
	
	fprintf(fp, "\nIntermediate Ciphertext Values (Encryption)\n\n");
	makeKey(&keyInst, DIR_ENCRYPT, keyLength, keyMaterial);
	blockPrint(fp, pt, "PT");
	cipherInit(&cipherInst, MODE_ECB, NULL);
	for(i = 1; i < keyInst.Nr; i++) {
		cipherUpdateRounds(&cipherInst, &keyInst, pt, 16, ct, i);
		sprintf(format, "CT%d", i);
		blockPrint(fp, ct, format);
	}
	cipherUpdateRounds(&cipherInst, &keyInst, pt, 16, ct, keyInst.Nr);
	blockPrint(fp, ct, "CT");
	
	fprintf(fp, "\nIntermediate Ciphertext Values (Decryption)\n\n");
	makeKey(&keyInst, DIR_DECRYPT, keyLength, keyMaterial);
	blockPrint(fp, ct, "CT");
	cipherInit(&cipherInst, MODE_ECB, NULL);
	for(i = 1; i < keyInst.Nr; i++) {
		cipherUpdateRounds(&cipherInst, &keyInst, ct, 16, pt, i);
		sprintf(format, "PT%d", i);
		blockPrint(fp, pt, format);
	}
	cipherUpdateRounds(&cipherInst, &keyInst, ct, 16, pt, keyInst.Nr);
	blockPrint(fp, pt, "PT");
	
#ifdef TRACE_KAT_MCT
	printf(" done.\n");
#endif 
}
Esempio n. 2
0
static void makeFIPSTestVectors(const char *fipsFile) {
	int i, keyLength, r;
	keyInstance keyInst;
	cipherInstance cipherInst;
	BYTE keyMaterial[320];
	u8 pt[16], ct[16];
	char format[64];
	FILE *fp;

#ifdef TRACE_KAT_MCT
	printf("Generating FIPS test vectors...");
#endif /* ?TRACE_KAT_MCT */
	
	fp = fopen(fipsFile, "w");
	fprintf(fp,
		"\n"
		"================================\n\n"
		"FILENAME:  \"%s\"\n\n"
		"FIPS Test Vectors\n",
		fipsFile);

	/* 128-bit key: 00010103...0e0f: */
	keyLength = 128;
	memset(keyMaterial, 0, sizeof (keyMaterial));
	for (i = 0; i < keyLength/8; i++) {
		sprintf(&keyMaterial[2*i], "%02X", i);
	}
	
	fprintf(fp, "\n================================\n\n");
	fprintf(fp, "KEYSIZE=128\n\n");
    fprintf(fp, "KEY=%s\n\n", keyMaterial);

	/* plaintext is always 00112233...eeff: */
	for (i = 0; i < 16; i++) {
		pt[i] = (i << 4) | i;
	}

    /* encryption: */	
	makeKey(&keyInst, DIR_ENCRYPT, keyLength, keyMaterial);
	cipherInit(&cipherInst, MODE_ECB, NULL);
	fprintf(fp, "Round Subkey Values (Encryption)\n\n");
    for (r = 0; r <= keyInst.Nr; r++) {
        fprintf(fp, "RK%d=", r);
        for (i = 0; i < 4; i++) {
            u32 w = keyInst.rk[4*r + i];
            fprintf(fp, "%02X%02X%02X%02X", w >> 24, (w >> 16) & 0xff, (w >> 8) & 0xff, w & 0xff);
        }
        fprintf(fp, "\n");
    }
	fprintf(fp, "\nIntermediate Ciphertext Values (Encryption)\n\n");
	blockPrint(fp, pt, "PT");
	for (i = 1; i < keyInst.Nr; i++) {
		cipherUpdateRounds(&cipherInst, &keyInst, pt, 16, ct, i);
		sprintf(format, "CT%d", i);
		blockPrint(fp, ct, format);
	}
	cipherUpdateRounds(&cipherInst, &keyInst, pt, 16, ct, keyInst.Nr);
	blockPrint(fp, ct, "CT");
	
    /* decryption: */	
	makeKey(&keyInst, DIR_DECRYPT, keyLength, keyMaterial);
	cipherInit(&cipherInst, MODE_ECB, NULL);
	fprintf(fp, "\nRound Subkey Values (Decryption)\n\n");
    for (r = 0; r <= keyInst.Nr; r++) {
        fprintf(fp, "RK%d=", r);
        for (i = 0; i < 4; i++) {
            u32 w = keyInst.rk[4*r + i];
            fprintf(fp, "%02X%02X%02X%02X", w >> 24, (w >> 16) & 0xff, (w >> 8) & 0xff, w & 0xff);
        }
        fprintf(fp, "\n");
    }
	fprintf(fp, "\nIntermediate Ciphertext Values (Decryption)\n\n");
	blockPrint(fp, ct, "CT");
	for (i = 1; i < keyInst.Nr; i++) {
		cipherUpdateRounds(&cipherInst, &keyInst, ct, 16, pt, i);
		sprintf(format, "PT%d", i);
		blockPrint(fp, pt, format);
	}
	cipherUpdateRounds(&cipherInst, &keyInst, ct, 16, pt, keyInst.Nr);
	blockPrint(fp, pt, "PT");

	/* 192-bit key: 00010103...1617: */
	keyLength = 192;
	memset(keyMaterial, 0, sizeof (keyMaterial));
	for (i = 0; i < keyLength/8; i++) {
		sprintf(&keyMaterial[2*i], "%02X", i);
	}
	
	fprintf(fp, "\n================================\n\n");
	fprintf(fp, "KEYSIZE=192\n\n");
    fprintf(fp, "KEY=%s\n\n", keyMaterial);

	/* plaintext is always 00112233...eeff: */
	for (i = 0; i < 16; i++) {
		pt[i] = (i << 4) | i;
	}

    /* encryption: */	
	makeKey(&keyInst, DIR_ENCRYPT, keyLength, keyMaterial);
	cipherInit(&cipherInst, MODE_ECB, NULL);
	fprintf(fp, "\nRound Subkey Values (Encryption)\n\n");
    for (r = 0; r <= keyInst.Nr; r++) {
        fprintf(fp, "RK%d=", r);
        for (i = 0; i < 4; i++) {
            u32 w = keyInst.rk[4*r + i];
            fprintf(fp, "%02X%02X%02X%02X", w >> 24, (w >> 16) & 0xff, (w >> 8) & 0xff, w & 0xff);
        }
        fprintf(fp, "\n");
    }
	fprintf(fp, "\nIntermediate Ciphertext Values (Encryption)\n\n");
	blockPrint(fp, pt, "PT");
	for (i = 1; i < keyInst.Nr; i++) {
		cipherUpdateRounds(&cipherInst, &keyInst, pt, 16, ct, i);
		sprintf(format, "CT%d", i);
		blockPrint(fp, ct, format);
	}
	cipherUpdateRounds(&cipherInst, &keyInst, pt, 16, ct, keyInst.Nr);
	blockPrint(fp, ct, "CT");
	
    /* decryption: */	
	makeKey(&keyInst, DIR_DECRYPT, keyLength, keyMaterial);
	cipherInit(&cipherInst, MODE_ECB, NULL);
	fprintf(fp, "\nRound Subkey Values (Decryption)\n\n");
    for (r = 0; r <= keyInst.Nr; r++) {
        fprintf(fp, "RK%d=", r);
        for (i = 0; i < 4; i++) {
            u32 w = keyInst.rk[4*r + i];
            fprintf(fp, "%02X%02X%02X%02X", w >> 24, (w >> 16) & 0xff, (w >> 8) & 0xff, w & 0xff);
        }
        fprintf(fp, "\n");
    }
	fprintf(fp, "\nIntermediate Ciphertext Values (Decryption)\n\n");
	blockPrint(fp, ct, "CT");
	for(i = 1; i < keyInst.Nr; i++) {
		cipherUpdateRounds(&cipherInst, &keyInst, ct, 16, pt, i);
		sprintf(format, "PT%d", i);
		blockPrint(fp, pt, format);
	}
	cipherUpdateRounds(&cipherInst, &keyInst, ct, 16, pt, keyInst.Nr);
	blockPrint(fp, pt, "PT");

	/* 256-bit key: 00010103...1e1f: */
	keyLength = 256;
	memset(keyMaterial, 0, sizeof (keyMaterial));
	for (i = 0; i < keyLength/8; i++) {
		sprintf(&keyMaterial[2*i], "%02X", i);
	}
	
	fprintf(fp, "\n================================\n\n");
	fprintf(fp, "KEYSIZE=256\n\n");
    fprintf(fp, "KEY=%s\n\n", keyMaterial);

	/* plaintext is always 00112233...eeff: */
	for (i = 0; i < 16; i++) {
		pt[i] = (i << 4) | i;
	}

    /* encryption: */	
	makeKey(&keyInst, DIR_ENCRYPT, keyLength, keyMaterial);
	cipherInit(&cipherInst, MODE_ECB, NULL);
	fprintf(fp, "\nRound Subkey Values (Encryption)\n\n");
    for (r = 0; r <= keyInst.Nr; r++) {
        fprintf(fp, "RK%d=", r);
        for (i = 0; i < 4; i++) {
            u32 w = keyInst.rk[4*r + i];
            fprintf(fp, "%02X%02X%02X%02X", w >> 24, (w >> 16) & 0xff, (w >> 8) & 0xff, w & 0xff);
        }
        fprintf(fp, "\n");
    }
	fprintf(fp, "\nIntermediate Ciphertext Values (Encryption)\n\n");
	blockPrint(fp, pt, "PT");
	for(i = 1; i < keyInst.Nr; i++) {
		cipherUpdateRounds(&cipherInst, &keyInst, pt, 16, ct, i);
		sprintf(format, "CT%d", i);
		blockPrint(fp, ct, format);
	}
	cipherUpdateRounds(&cipherInst, &keyInst, pt, 16, ct, keyInst.Nr);
	blockPrint(fp, ct, "CT");
	
    /* decryption: */	
	makeKey(&keyInst, DIR_DECRYPT, keyLength, keyMaterial);
	cipherInit(&cipherInst, MODE_ECB, NULL);
	fprintf(fp, "\nRound Subkey Values (Decryption)\n\n");
    for (r = 0; r <= keyInst.Nr; r++) {
        fprintf(fp, "RK%d=", r);
        for (i = 0; i < 4; i++) {
            u32 w = keyInst.rk[4*r + i];
            fprintf(fp, "%02X%02X%02X%02X", w >> 24, (w >> 16) & 0xff, (w >> 8) & 0xff, w & 0xff);
        }
        fprintf(fp, "\n");
    }
	fprintf(fp, "\nIntermediate Ciphertext Values (Decryption)\n\n");
	blockPrint(fp, ct, "CT");
	for(i = 1; i < keyInst.Nr; i++) {
		cipherUpdateRounds(&cipherInst, &keyInst, ct, 16, pt, i);
		sprintf(format, "PT%d", i);
		blockPrint(fp, pt, format);
	}
	cipherUpdateRounds(&cipherInst, &keyInst, ct, 16, pt, keyInst.Nr);
	blockPrint(fp, pt, "PT");

    fprintf(fp, "\n");
	fclose(fp);
#ifdef TRACE_KAT_MCT
	printf(" done.\n");
#endif /* ?TRACE_KAT_MCT */
}
Esempio n. 3
0
static void rijndaelIVKAT (FILE *fp, int keyLength, int blockLength)
{
	int i, ROUNDS;
	BYTE block[4*MAXBC], block2[4*MAXBC];
	BYTE keyMaterial[320];
	keyInstance keyInst;
	cipherInstance cipherInst;
	char format[10];
#ifdef TRACE_KAT_MCT
	printf ("Executing Intermediate value KAT (key %d): ", keyLength);
	fflush (stdout);
#endif /* ?TRACE_KAT_MCT */

	switch (keyLength >= blockLength ? keyLength : blockLength) {
	case 128: ROUNDS = 10; break;
	case 192: ROUNDS = 12; break;
	case 256: ROUNDS = 14; break;
	default : return; /* this cannot happen */
	}
	
	fprintf (fp,
		"\n"
		"==========\n"
		"\n"
		"KEYSIZE=%d\n",
		keyLength);
	fflush (fp);
	memset (keyMaterial, 0, sizeof (keyMaterial));
	for (i = 0; i < keyLength/8; i++) {
		sprintf (&keyMaterial[2*i], "%02X", i);
	}
	keyInst.blockLen = blockLength;
	makeKey(&keyInst, DIR_ENCRYPT, keyLength, keyMaterial);
	fprintf (fp, "KEY=%s\n", keyMaterial);
	fprintf (fp, "\nIntermediate Ciphertext Values (Encryption)\n\n");
	for (i = 0; i < blockLength/8; i++) {
		block[i] = i;
	}
	blockPrint (fp, block, blockLength, "PT");
	cipherInst.blockLen = blockLength;
	cipherInit (&cipherInst, MODE_ECB, NULL);
	for(i = 1; i < ROUNDS; i++) {
		cipherUpdateRounds (&cipherInst, &keyInst, block, blockLength/8, block2, i);
		sprintf(format,"CT%d",i);
		blockPrint (fp, block2, blockLength, format);
	}
	cipherUpdateRounds (&cipherInst, &keyInst, block, blockLength, block2, ROUNDS);
	blockPrint (fp, block2, blockLength, "CT");
	
	keyInst.blockLen = blockLength;
	makeKey(&keyInst, DIR_DECRYPT, keyLength, keyMaterial);
	fprintf (fp, "\nIntermediate Ciphertext Values (Decryption)\n\n");
	blockPrint (fp, block2, blockLength, "CT");
	cipherInst.blockLen = blockLength;
	cipherInit (&cipherInst, MODE_ECB, NULL);
	for(i = 1; i < ROUNDS; i++) {
		cipherUpdateRounds (&cipherInst, &keyInst, block2, blockLength,block, ROUNDS-i);
		sprintf(format,"PT%d",i);
		blockPrint (fp, block, blockLength, format);
	}
	cipherUpdateRounds (&cipherInst, &keyInst, block2, blockLength, block, 0);
	blockPrint (fp, block, blockLength, "PT");
	
#ifdef TRACE_KAT_MCT
	printf (" done.\n");
#endif 
}