📄 idea_cmd.c
字号:
/******************************************************************************//* *//* I N T E R N A T I O N A L D A T A E N C R Y P T I O N A L G O R I T H M *//* *//* A S A U S E R C O M M A N D *//* *//******************************************************************************//* Author: Richard De Moliner (demoliner@isi.ee.ethz.ch) *//* Signal and Information Processing Laboratory *//* Swiss Federal Institute of Technology *//* CH-8092 Zuerich, Switzerland *//* Created: April 23, 1992 *//* Changes: November 16, 1993 (support of ANSI-C and C++) *//* System: SUN SPARCstation, SUN acc ANSI-C-Compiler, SUN-OS 4.1.3 *//******************************************************************************/#ifdef RASTERFILE#include <rasterfile.h>#endif#ifdef TIME#include <time.h>#ifndef CLK_TCK#define CLK_TCK 1000000#endif#endif#include <stdio.h>#include <stdlib.h>#include <string.h>#include "idea.h"#define TRUE 1 /* boolean constant for true */#define FALSE 0 /* boolean constant for false */#define nofTestData 163840 /* number of blocks encrypted in time test */#define nomode 0 /* no mode is specified */#define ecb 1 /* electronic code book mode */#define cbc 2 /* cipher block chaining mode */#define cfb 3 /* ciphertext feedback mode */#define ofb 4 /* output feedback mode */#define tan 5 /* tandem DM-scheme for hashing */#define abr 6 /* abreast DM-scheme for hashing */#define eol 0x100 /* end of line character */#define colon 0x101 /* character ':' */#define error 0x102 /* unknown character */#define maxInterleave 1024 /* maximal interleave factor + 1 */#define nofChar ('~' - '!' +1) /* number of different printable characters */#define maxBufLen (Idea_dataSize * 1024) /* size of input and output buffer */Idea_UserKey userKey; /* user selected 128 bit key */Idea_Key key; /* expanded key with 832 bits */Idea_Data state[maxInterleave];/* state informations for interleaving modes */Idea_Data hashLow; /* lower 64 bits of hash value */Idea_Data hashHigh; /* higher 64 bits of hash value */u_int32 inputLen = 0; /* current number of bytes read from 'inFile' */int interleave = 0; /* current interleave factor */int time_0 = 0; /* time for interleaving modes */int time_N = 0; /* time-interleave for interleaving modes */int mode = nomode; /* current mode */int optEncrypt = FALSE; /* encrypt option 'e' */int optDecrypt = FALSE; /* decrypt option 'd' */int optHash = FALSE; /* hash option 'h' */int optCopyHash = FALSE; /* copy and hash option 'H' */int optKeyHexString = FALSE; /* key as hex-string option 'K' */int optKeyString = FALSE; /* key as string option 'k' */int optRas = FALSE; /* raster file option 'r' */int optTime = FALSE; /* measure time option 'T' */int inBufLen = maxBufLen; /* current length of data in 'inBuf' */int inBufPos = maxBufLen; /* current read position of 'inBuf' */int outBufLen = 0; /* current write position of 'outBuf' */u_int8 inBuf[maxBufLen]; /* buffer for file read */u_int8 outBuf[maxBufLen]; /* buffer for file write */FILE *inFile; /* file with input data (plain or ciphertext) */FILE *outFile; /* file for output data (plain or ciphertext) */FILE *hashFile; /* 128 bit hash value is written to this file *//******************************************************************************//* initialize global variables */#ifdef ANSI_C void Init(void)#else Init()#endif{ int i, pos; for (i = Idea_userKeyLen - 1; i >= 0 ; i--) userKey[i] = 0; for (pos = maxInterleave - 1; pos >= 0 ; pos--) for (i = Idea_dataLen - 1; i >= 0; i--) state[pos][i] = 0;} /* Init *//******************************************************************************//* E R R O R - H A N D L I N G *//******************************************************************************//* write usage error message and terminate program */#ifdef ANSI_C void UsageError(int num)#else UsageError(num) int num;#endif{#ifdef RASTERFILEfprintf(stderr, "(%d)\n\Usage: idea [ -e | -d ] [ -r ] [ -ecb | -cbcN | -cfbN | -ofbN ]\n\ ( -k keyString | -K keyHexString ) \n\ [ inputFile [ outputFile ] ] \n\ idea [ -h | -H ] [ -tan | -abr ] \n\ [ -k keyString | -K keyHexString ] \n\ [ inputFile [ [ outputFile ] hashvalFile ] ] \n\ idea -T \n\\nExample: idea -Hk \"k e y\" infile | idea -cbc8 -K 123:9a::eF - outfile\n\\n", num);#elsefprintf(stderr, "(%d)\n\Usage: idea [ -e | -d ] [ -ecb | -cbcN | -cfbN | -ofbN ] \n\ ( -k keyString | -K keyHexString ) \n\ [ inputFile [ outputFile ] ] \n\ idea [ -h | -H ] [ -tan | -abr ] \n\ [ -k keyString | -K keyHexString ] \n\ [ inputFile [ [ outputFile ] hashvalFile ] ] \n\\nExample: idea -Hk \"k e y\" infile | idea -cbc8 -K 123:9a::eF - outfile\n\\n", num);#endif exit(-1);} /* UsageError *//******************************************************************************//* write error message and terminate program */#ifdef ANSI_C void Error(int num, char *str)#else Error(num, str) int num; char *str;#endif{ fprintf(stderr, "error %d in idea: %s\n", num, str); exit(-1); } /* Error *//******************************************************************************//* write system error message and terminate program */#ifdef ANSI_C void PError(char *str)#else PError(str) char *str;#endif{ perror(str); exit(-1); } /* PError *//******************************************************************************//* D E C R Y P T I O N / E N C R Y P T I O N *//******************************************************************************//* read one data-block from 'inFile' */#ifdef ANSI_C int GetData(Idea_Data data)#else int GetData(data) Idea_Data data;#endif{ register int i, len; register u_int16 h; register u_int8 *inPtr; if (inBufPos >= inBufLen) { if (inBufLen != maxBufLen) return 0; inBufLen = fread(inBuf, 1, maxBufLen, inFile); inBufPos = 0; if (inBufLen == 0) return 0; if (inBufLen % Idea_dataSize != 0) for (i = inBufLen; i % Idea_dataSize != 0; i++) inBuf[i] = 0; } inPtr = &inBuf[inBufPos]; for (i = 0; i < Idea_dataLen; i++) { h = ((u_int16)*inPtr++ & 0xFF) << 8; data[i] = h | (u_int16)*inPtr++ & 0xFF; } inBufPos += Idea_dataSize; if (inBufPos <= inBufLen) len = Idea_dataSize; else len = inBufLen + Idea_dataSize - inBufPos; inputLen += len; return len;} /* GetData *//******************************************************************************//* write one data-block to 'outFile' */#ifdef ANSI_C void PutData(Idea_Data data, int len)#else PutData(data, len) Idea_Data data; int len;#endif{ register int i; register u_int16 h; register u_int8 *outPtr; outPtr = &outBuf[outBufLen]; for (i = 0; i < Idea_dataLen; i++) { h = data[i]; *outPtr++ = h >> 8 & 0xFF; *outPtr++ = h & 0xFF; } outBufLen += len; if (outBufLen >= maxBufLen) { fwrite(outBuf, 1, maxBufLen, outFile); outBufLen = 0; }} /* PutData *//******************************************************************************//* write last block to 'outFile' and close 'outFile' */#ifdef ANSI_C void CloseOutput(void)#else CloseOutput()#endif{ if (outBufLen > 0) { fwrite(outBuf, 1, outBufLen, outFile); outBufLen = 0; } fclose(outFile);} /* CloseOutput *//******************************************************************************//* increment time_0 and time_N */#ifdef ANSI_C void IncTime(void)#else IncTime()#endif{ time_0 = (time_0 + 1) % maxInterleave; time_N = (time_N + 1) % maxInterleave;} /* IncTime *//******************************************************************************//* encrypt one data-block */#ifdef ANSI_C void EncryptData(Idea_Data data)#else EncryptData(data) Idea_Data data;#endif{ int i; switch (mode) { case ecb: Idea_Crypt(data, data, key); break; case cbc: for (i = Idea_dataLen - 1; i >= 0; i--) data[i] ^= state[time_N][i]; Idea_Crypt(data, data, key); for (i = Idea_dataLen - 1; i >= 0; i--) state[time_0][i] = data[i]; IncTime(); break; case cfb: Idea_Crypt(state[time_N], state[time_0], key); for (i = Idea_dataLen - 1; i >= 0; i--) data[i] = state[time_0][i] ^= data[i]; IncTime(); break; case ofb: Idea_Crypt(state[time_N], state[time_0], key); for (i = Idea_dataLen - 1; i >= 0; i--) data[i] ^= state[time_0][i]; IncTime(); break; default: break; }} /* EncryptData *//******************************************************************************//* decrypt one data-block */#ifdef ANSI_C void DecryptData(Idea_Data data)#else DecryptData(data) Idea_Data data;#endif{ int i; switch (mode) { case ecb: Idea_Crypt(data, data, key); break; case cbc: for (i = Idea_dataLen - 1; i >= 0; i--) state[time_0][i] = data[i]; Idea_Crypt(data, data, key); for (i = Idea_dataLen - 1; i >= 0; i--) data[i] ^= state[time_N][i]; IncTime(); break; case cfb: for (i = Idea_dataLen - 1; i >= 0; i--) state[time_0][i] = data[i]; Idea_Crypt(state[time_N], data, key); for (i = Idea_dataLen - 1; i >= 0; i--) data[i] ^= state[time_0][i]; IncTime(); break; case ofb: Idea_Crypt(state[time_N], state[time_0], key); for (i = Idea_dataLen - 1; i >= 0; i--) data[i] ^= state[time_0][i]; IncTime(); break; default: break; }} /* DecryptData *//******************************************************************************//* hash one data-block */#ifdef ANSI_C void HashData(Idea_Data data)#else HashData(data) Idea_Data data;#endif{ int i; Idea_UserKey userKey; Idea_Key key; Idea_Data w; for (i = Idea_dataLen - 1; i >= 0; i--) { userKey[i] = hashLow[i]; userKey[i + Idea_dataLen] = data[i]; } Idea_ExpandUserKey(userKey, key); Idea_Crypt(hashHigh, w, key); if (mode == abr) { for (i = Idea_dataLen - 1; i >= 0; i--) { userKey[i] = data[i]; userKey[i + Idea_dataLen] = hashHigh[i]; hashHigh[i] ^= w[i]; w[i] = hashLow[i] ^ 0xFFFF; } } else { /* mode == tan */ for (i = Idea_dataLen - 1; i >= 0; i--) { hashHigh[i] ^= w[i]; userKey[i] = data[i]; userKey[i + Idea_dataLen] = w[i]; w[i] = hashLow[i]; } } Idea_ExpandUserKey(userKey, key); Idea_Crypt(w, w, key); for (i = Idea_dataLen - 1; i >= 0; i--) hashLow[i] ^= w[i];} /* HashData *//******************************************************************************//* write the hash value to 'hashFile' */#ifdef ANSI_C void WriteHashValue(void)#else WriteHashValue()#endif{ int i; for (i = 0; i < Idea_dataLen; i++) fprintf(hashFile, "%04X", hashHigh[i]); for (i = 0; i < Idea_dataLen; i++) fprintf(hashFile, "%04X", hashLow[i]);} /* WriteHashValue *//******************************************************************************//* store integer 'value' in 'data' */#ifdef ANSI_C void PlainLenToData(u_int32 value, Idea_Data data)#else PlainLenToData(value, data) u_int32 value; Idea_Data data;#endif{ data[3] = (u_int16)(value << 3 & 0xFFFF); data[2] = (u_int16)(value >> 13 & 0xFFFF); data[1] = (u_int16)(value >> 29 & 0x0007); data[0] = 0;} /* PlainLenToData *//******************************************************************************//* extract integer 'value' from 'data' */#ifdef ANSI_C void DataToPlainLen(Idea_Data data, u_int32 *value)#else DataToPlainLen(data, value) Idea_Data data; u_int32 *value;#endif{ if (data[0] || data[1] > 7 || data[3] & 7) Error(0, "input is not a valid cryptogram"); *value = (u_int32)data[3] >> 3 & 0x1FFF | (u_int32)data[2] << 13 | (u_int32)data[1] << 29;} /* DataToPlainLen *//******************************************************************************//* copy head and color-map of rasterfile from 'inFile' to 'outFile' */#ifdef ANSI_C void CopyHeadOfRasFile(void)#else CopyHeadOfRasFile()#endif{#ifdef RASTERFILE struct rasterfile header; int mapLen, len; if (fread(&header, sizeof header, 1, inFile) != 1) PError("read header from rasterfile"); if (header.ras_magic != RAS_MAGIC) Error(1, "input is not a rasterfile"); if (fwrite(&header, sizeof header, 1, outFile) != 1) PError("write header to rasterfile"); len = maxBufLen; for (mapLen = header.ras_maplength; mapLen > 0; mapLen -= len){ if (mapLen < maxBufLen) len = mapLen; if (fread(inBuf, len, 1, inFile) != 1) PError("read map from rasterfile"); if (fwrite(inBuf, len, 1, outFile) != 1) PError("write map to rasterfile"); }#endif} /* CopyHeadOfRasFile *//******************************************************************************//* encrypt / decrypt complete data-stream or compute hash value of data-stream*/#ifdef ANSI_C void CryptData(void)#else CryptData()#endif{ int t, i; u_int32 len; Idea_Data dat[4]; Idea_Data data; if (optRas) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -