コード例 #1
0
ファイル: verify.c プロジェクト: detomastah/lamport_signature
int main(int argc, char *argv[]) {
    char md5[MD5_DIGEST_LENGTH];
    int file_descript;
    unsigned long file_size;
    char *file_buffer;
    FILE *pkr_file;
    int signature_size = BITS*sizeof(DES_cblock);
    
    file_descript = open(argv[1], O_RDONLY);
    if(file_descript < 0) { 
        puts("Signed message unreadable");
        exit(-1);
    }
    
    //read signed message
    file_size = get_size_by_fd(file_descript);    
    file_buffer = mmap(0, file_size, PROT_READ, MAP_SHARED, file_descript, 0);
    //copy signature
    memcpy(SG, file_buffer, signature_size);
    
    MD5((unsigned char*) (file_buffer+signature_size), file_size-signature_size, md5);
    //read PKR

    pkr_file = fopen (argv[2],"r");
    if (pkr_file!=NULL)
    {
        fread(S, sizeof(DES_cblock), 2*BITS, pkr_file);
        fread(R, sizeof(DES_cblock), 2*BITS, pkr_file);
        fclose (pkr_file);
    } else { puts("PKR unreadable"); return -1; }
    
    if (verifyMsg(md5)) puts("FAIL");
    else puts ("OK");
    
    return (0);
}
コード例 #2
0
ファイル: main.c プロジェクト: kkoo/cs426
int createLog(char *fn) {	
	logID = createRandomNum();
	stepNum = 0;
	////////////////STARTUP from U////////////////
	//create first message
	
	//INIT values
	char *x = "aaaaaaaaaaaaa";
	char *hashX = hash(x);
	
	_hashChain = (char *)malloc(20+1); // the initial hash chain
	memset(_hashChain, 'a', 20+1); 

	_logAuthKey = intToStr(createRandomNum());		//A
	A0 = _logAuthKey;

	char *msgAuthCode; 								//Z

	//_sessionKey = createFirstKey();				//K
	_sessionKey = createKey(LOG_INIT, _logAuthKey);

	//create msg for T
	struct Msg *msg = createMsg(stepNum, ID_UNTRUSTED, PUB_KEY_T, PRIV_KEY_U, _sessionKey, x);
	
	//create first log entry
	char *data = logToStr(createLogEntry(LOG_INIT, logID, msg));
	//char *data = logToStr2(createLogEntry(LOG_INIT, logID, msg));
	char *encData = encryptData(data, _sessionKey, strlen(data)); 

	_hashChain = createY(_hashChain, encData, LOG_INIT);
	msgAuthCode = genMAC(_logAuthKey, _hashChain);

	struct ALogEntry *firstLog = createALogEntry(LOG_INIT, encData, _hashChain, msgAuthCode);
	writeAEntry(firstLog, fn);
	//////////////END STARTUP from U////////////////


	/////////////RECIEVE  T//////////////

	//verify the message
	int result = verifyMsg(msg, PRIV_KEY_T, PUB_KEY_U);
	//printf("Result from T:%d\n", result);
	//TODO: check valid certificate

	//increment protocol step ID;
	int p = msg->p + 1;

	//create X1
	char *x0 = getX(msg, PRIV_KEY_T, PUB_KEY_U);
	char *x1 = "ZZZ";

	//create session key
	char *sessionKeyT = createKey(RESP_MSG, _logAuthKey);
	
	//create msg
	struct Msg *msg1 = createMsg(p, ID_TRUSTED, PUB_KEY_U, PRIV_KEY_T, sessionKeyT, x1);
	/////////////END  RECIEVE  T//////////////



	/////////////FINALIZE INIT U///////////////////
	//verify the msg
	result = verifyMsg(msg1, PRIV_KEY_U, PUB_KEY_T);
	//printf("Result from U:%d\n", result);

	//get the data
	data = logToStr(createLogEntry(RESP_MSG, logID, msg1));

	//update hash chains and keys
	_logAuthKey = hash(_logAuthKey);						//A+1 = H(A)			
	_sessionKey = createKey(NORMAL_MSG, _logAuthKey);		//K
	encData = encryptData(data, _sessionKey, strlen(data));

	//MSG Authentication
	_hashChain = createY(_hashChain, encData, RESP_MSG);	//Y+1 = H(y, encData, logtype)
	msgAuthCode = genMAC(_logAuthKey, _hashChain);		//Z = MAC(Y)
	struct ALogEntry *secondLog = createALogEntry(RESP_MSG, encData, _hashChain, msgAuthCode);

	writeAEntry(secondLog, fn);
}