📄 testvec.c
字号:
/***************************************************************************** * testvec.c * * DES ASIC Simlator, Test Vector Generation Program * * * * Written 1998 by Cryptography Research (http://www.cryptography.com) * * and Paul Kocher for the Electronic Frontier Foundation (EFF). * * Placed in the public domain by Cryptography Research and EFF. * * THIS IS UNSUPPORTED FREE SOFTWARE. USE AND DISTRIBUTE AT YOUR OWN RISK. * * * * IMPORTANT: U.S. LAW MAY REGULATE THE USE AND/OR EXPORT OF THIS PROGRAM. * * * ***************************************************************************** * * * IMPLEMENTATION NOTES: * * * * This program automatically determines the configuration of a search * * array. Additional diagnostic code should be added to detect common * * chip failures (once these are known). * * * ***************************************************************************** * * * REVISION HISTORY: * * * * Version 1.0: Initial release by Cryptography Research to EFF. * * * *****************************************************************************/#include <stdio.h>#include <stdlib.h>#include <memory.h>#include <string.h>#include <time.h>#include "sim.h"int USE_RAW_IO = 0;FILE *FILE_TOCHIP, *FILE_FROMCHIP; /* TOCHIP can be input *or* output */int CREATING_VECTOR; /* reading vs writing TOCHIP file */unsigned char HARDWIRED_CHIP_ID = 0x3A;int ALLACTIVE_IN = 1; /* gets toggled randomly */int BOARD_EN_IN = 1; /* input value for run_set/check */int ADRSEL1_IN = 1;void GetUserInfo(unsigned char plaintextVector[32], unsigned char plaintextXorMask[8], unsigned char ciphertext0[8], unsigned char ciphertext[8], unsigned char *plaintextByteMask, int *useCBC, int *extraXor, int *randomVector, unsigned char startKey[7], long *totalClocks);void LoadState(unsigned char plaintextVector[32], unsigned char plaintextXorMask[8], unsigned char ciphertext0[8], unsigned char ciphertext1[8], unsigned char plaintextByteMask, int useCBC, int extraXor, unsigned char startKey[7]);void RunSimulator_SetRegister(int addr, int data);unsigned char RunSimulator_CheckRegister(int addr);void RunSimulator_DummyIO(void);static void EXIT_ERR(char *s) { fprintf(stderr, s); exit(1); }void desDecrypt(unsigned char m[8], unsigned char c[8], unsigned char k[7]);void increment32(unsigned char *num);void decrement32(unsigned char *num);int hex2bin(char *hex, unsigned char *bin);void printHexString(char *tag, unsigned char *data, int len);void OpenFiles(char *toChipFilename, char *fromChipFilename, int useRaw);void printKeyInfo(FILE *outDev, char *preamble, int searchUnit);long getClockCounter(void);void proceedNormal(long totalClocks);void proceedRandom(void);/* * * * THESE FUNCTIONS CREATE AND MANAGE THE TEST VECTORS. * * * */void main(int argc, char **argv) { unsigned char startKey[7], plaintextVector[32]; unsigned char plaintextXorMask[8]; unsigned char ciphertext0[8]; unsigned char ciphertext1[8]; unsigned char plaintextByteMask; int useCBC, extraXor, randomVector; long totalClocks; char buffer[512]; if (argc != 3 && argc != 4) { fprintf(stderr,"Command line: TO_CHIP.OUT FROM_CHIP.OUT [RAW]\n"); fprintf(stderr," TO_CHIP.OUT File for data going to chip\n"); fprintf(stderr," (If this file exists, it will be simulated.\n"); fprintf(stderr," Otherwise, a new file will be created.)\n"); fprintf(stderr," FROM_CHIP.OUT File for chip's output\n"); fprintf(stderr," RAW Gives unix CRLFs & no header.\n"); exit(1); } /* * Open files and set CREATING_VECTOR to: * 0=reading TOCHIP file, * 1=create TOCHIP from user input, * 2=create random vector */ OpenFiles(argv[1], argv[2], (argc == 4) ? 1 : 0); if (CREATING_VECTOR == 0) { fprintf(stderr, "Using input vector from file.\n"); while (1) { if (fgets(buffer, 500, FILE_TOCHIP) == NULL) break; if (strlen(buffer) < 10) break; RunChip(buffer, FILE_FROMCHIP, USE_RAW_IO); } } else { GetUserInfo(plaintextVector, plaintextXorMask, ciphertext0, ciphertext1, &plaintextByteMask, &useCBC, &extraXor, &randomVector, startKey, &totalClocks); if (randomVector == 0) { fprintf(stderr, "Seed=random (time-based)\n"); srand((unsigned) time(NULL)); HARDWIRED_CHIP_ID = (unsigned char)(rand() & 255); } else if (randomVector == 1) { fprintf(stderr, "Using user params.\n"); } else { fprintf(stderr, "Seed=%d\n", randomVector); srand(randomVector); HARDWIRED_CHIP_ID = (unsigned char)(rand() & 255); } /* Reset chip and set the chip ID */ sprintf(buffer, "01011111 00 %02X 00\n", HARDWIRED_CHIP_ID); RunChip(buffer, FILE_FROMCHIP, USE_RAW_IO); fputs(buffer, FILE_TOCHIP); RunChip(buffer, FILE_FROMCHIP, USE_RAW_IO); fputs(buffer, FILE_TOCHIP); RunChip(buffer, FILE_FROMCHIP, USE_RAW_IO); fputs(buffer, FILE_TOCHIP); RunChip(buffer, FILE_FROMCHIP, USE_RAW_IO); fputs(buffer, FILE_TOCHIP); sprintf(buffer, "11011111 %02X %02X 00\n", HARDWIRED_CHIP_ID, HARDWIRED_CHIP_ID); RunChip(buffer, FILE_FROMCHIP, USE_RAW_IO); fputs(buffer, FILE_TOCHIP); buffer[2] = '1'; RunChip(buffer, FILE_FROMCHIP, USE_RAW_IO); fputs(buffer, FILE_TOCHIP); buffer[2] = '0'; RunChip(buffer, FILE_FROMCHIP, USE_RAW_IO); fputs(buffer, FILE_TOCHIP); if (randomVector == 1) { LoadState(plaintextVector, plaintextXorMask, ciphertext0, ciphertext1, plaintextByteMask, useCBC, extraXor, startKey); proceedNormal(totalClocks); } else { proceedRandom(); } } /* Clean up a bit (doesn't really matter -- this is test code :-) */ fclose(FILE_FROMCHIP); fclose(FILE_TOCHIP);}void proceedNormal(long totalClocks) { long numClocks = getClockCounter(); unsigned char goodKey[8]; int i,j,r; while (++numClocks < totalClocks) { r = RunSimulator_CheckRegister(REG_SEARCHINFO); if (r & 4) { fprintf(stderr, "------- Idle --------\n"); RunSimulator_DummyIO(); continue; } for (i = 0; i < 24; i++) { /* If we're going to see a stall, give some settling time */ if ((peekState(REG_SEARCH_STATUS(i)) & 1) == 0) { /* stalled? */ RunSimulator_DummyIO(); /* wait before read */ RunSimulator_DummyIO(); RunSimulator_DummyIO(); } r = RunSimulator_CheckRegister(REG_SEARCH_STATUS(i)); if ((r & 1) == 0) { /* stalled? */ goodKey[6] = RunSimulator_CheckRegister(REG_SEARCH_KEY(i)+0); goodKey[5] = RunSimulator_CheckRegister(REG_SEARCH_KEY(i)+1); goodKey[4] = RunSimulator_CheckRegister(REG_SEARCH_KEY(i)+2); goodKey[3] = RunSimulator_CheckRegister(REG_SEARCH_KEY(i)+3); goodKey[2] = RunSimulator_CheckRegister(REG_SEARCH_KEY(i)+4); goodKey[1] = RunSimulator_CheckRegister(REG_SEARCH_KEY(i)+5); goodKey[0] = RunSimulator_CheckRegister(REG_SEARCH_KEY(i)+6); fprintf(stderr, "ALERT: Full match in unit %d; extracted k = ", i); printf("ALERT: Full match in unit %d; extracted k = ", i); for (j = 0; j < 7; j++) { fprintf(stderr, "%02X", goodKey[j]); printf("%02X", goodKey[j]); } fprintf(stderr, "\n"); printf("\n"); RunSimulator_DummyIO(); /* Settling time */ RunSimulator_DummyIO(); RunSimulator_SetRegister(REG_SEARCH_STATUS(i), 1); /* restart */ } } }}void proceedRandom(void) { unsigned char readout[256]; unsigned char goodKey[7]; int i,j; unsigned char plaintextVector[32]; char buffer[256]; /* chip has already been set and the chip ID has been loaded */ /* Create plaintext vector with 181 bits set */ memset(plaintextVector, 0, sizeof(plaintextVector)); i = 0; while (i < 181) { j = rand() & 255; if ((plaintextVector[j/8] & (1 << (j % 8))) == 0) { plaintextVector[j/8] |= (1 << (j % 8)); i++; } } /* Load state */ for (i = 0; i < 32; i++) RunSimulator_SetRegister(REG_PTXT_VECTOR + i, plaintextVector[i]); for (i = 0; i < 8; i++) RunSimulator_SetRegister(REG_PTXT_XOR_MASK + i, rand() & 255); for (i = 0; i < 8; i++) RunSimulator_SetRegister(REG_CIPHERTEXT0 + i, rand() & 255); for (i = 0; i < 8; i++) RunSimulator_SetRegister(REG_CIPHERTEXT1 + i, rand() & 255); RunSimulator_SetRegister(REG_PTXT_BYTE_MASK, 1 << (rand() & 7)); i = (rand() % 3) + (rand() & 16); /* 0/1/2 for CBC & extraXor. 16=activOn */ fprintf(stderr, "Using mode %d with ActiveOn=%d.\n", (i&3), i/16); RunSimulator_SetRegister(REG_SEARCHINFO, i); for (i = 0; i < 24; i++) { /* for each engine */ for (j = 0; j < 7; j++) /* set random start key */ RunSimulator_SetRegister(REG_SEARCH_KEY(i)+j, rand() & 255); RunSimulator_SetRegister(REG_SEARCH_STATUS(i), 1); } /* Read out all registers (real and not) except for ptxt vector */ for (i = 255; i >= 32; i--) readout[i] = RunSimulator_CheckRegister(i); /* Change the key in any stopped units */ for (i = 0; i < 24; i++) { if ((readout[REG_SEARCH_STATUS(i)] & 1) == 0) /* stalled? */ RunSimulator_SetRegister(REG_SEARCH_KEY(i), readout[REG_SEARCH_KEY(i)] ^ 0x08); /* fix key */ } /* Read out ptxt vector */ for (i = 31; i >= 0; i--) readout[i] = RunSimulator_CheckRegister(i); /* scan stopped units */ for (i = 0; i < 24; i++) { if ((readout[REG_SEARCH_STATUS(i)] & 1) == 0) { /* stalled? */ goodKey[6] = RunSimulator_CheckRegister(REG_SEARCH_KEY(i)+0); goodKey[5] = RunSimulator_CheckRegister(REG_SEARCH_KEY(i)+1); goodKey[4] = RunSimulator_CheckRegister(REG_SEARCH_KEY(i)+2); goodKey[3] = RunSimulator_CheckRegister(REG_SEARCH_KEY(i)+3); goodKey[2] = RunSimulator_CheckRegister(REG_SEARCH_KEY(i)+4); goodKey[1] = RunSimulator_CheckRegister(REG_SEARCH_KEY(i)+5); goodKey[0] = RunSimulator_CheckRegister(REG_SEARCH_KEY(i)+6); if (rand() % 8) RunSimulator_SetRegister(REG_SEARCH_STATUS(i), 1); /* restart */ fprintf(stderr, "****** Full match in unit %d; extracted k = ", i); for (j = 0; j < 7; j++) { fprintf(stderr, "%02X", goodKey[j]); printf("%02X", goodKey[j]); } fprintf(stderr, "\n"); } } /* pick a different chip, read/write some registers, and reset chip id */ do { i = rand() & 255; } while (i == HARDWIRED_CHIP_ID); sprintf(buffer, "11011111 %02X %02X 00\n", i, HARDWIRED_CHIP_ID); RunChip(buffer, FILE_FROMCHIP, USE_RAW_IO); fputs(buffer, FILE_TOCHIP); buffer[2] = '1'; RunChip(buffer, FILE_FROMCHIP, USE_RAW_IO); fputs(buffer, FILE_TOCHIP); buffer[2] = '0'; RunChip(buffer, FILE_FROMCHIP, USE_RAW_IO); fputs(buffer, FILE_TOCHIP); for (i = 0; i < 8; i++) RunSimulator_SetRegister(rand() & 255, rand() & 255); for (i = 0; i < 8; i++) RunSimulator_CheckRegister(rand() & 255); sprintf(buffer, "11011111 %02X %02X 00\n", HARDWIRED_CHIP_ID, HARDWIRED_CHIP_ID); RunChip(buffer, FILE_FROMCHIP, USE_RAW_IO); fputs(buffer, FILE_TOCHIP); buffer[2] = '1'; RunChip(buffer, FILE_FROMCHIP, USE_RAW_IO); fputs(buffer, FILE_TOCHIP); buffer[2] = '0'; RunChip(buffer, FILE_FROMCHIP, USE_RAW_IO); fputs(buffer, FILE_TOCHIP); /* Test board enable and ADRSEL1 */ BOARD_EN_IN = 0; ADRSEL1_IN = 0; for (i = 0; i < 4; i++) RunSimulator_SetRegister(rand() & 255, rand() & 255); for (i = 0; i < 4; i++) RunSimulator_CheckRegister(rand() & 255); BOARD_EN_IN = 0; ADRSEL1_IN = 1; for (i = 0; i < 8; i++) RunSimulator_SetRegister(rand() & 255, rand() & 255); for (i = 0; i < 8; i++) RunSimulator_CheckRegister(rand() & 255); BOARD_EN_IN = 1; ADRSEL1_IN = 0; for (i = 0; i < 8; i++) RunSimulator_SetRegister(rand() & 255, rand() & 255); for (i = 0; i < 8; i++) RunSimulator_CheckRegister(rand() & 255); BOARD_EN_IN = 1; ADRSEL1_IN = 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -