⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatkeyexchange.c

📁 基于cc1010的设计实例
💻 C
字号:
/*****************************************************************************
 *                                                                           *
 *        **********                                                         *
 *       ************                                                        *
 *      ***        ***                                                       *
 *      ***   +++   ***                                                      *
 *      ***   + +   ***                                                      *
 *      ***   +                    CHIPCON CC1010 EXAMPLE PROGRAM            *
 *      ***   + +   ***                      CC1010 CHAT                     *
 *      ***   +++   ***                                                      *
 *      ***       ***                                                        *
 *       ***********                                                         *
 *        *********                                                          *
 *                                                                           *
 *****************************************************************************
 * Author:              JOL                                                  *
 *****************************************************************************
 * Revision history:                                                         *
 *                                                                           *
 * $Log: chatKeyExchange.c,v $
 * Revision 1.1  2002/10/14 10:02:28  tos
 * Initial version in CVS.
 *
 *                                                                           *
 ****************************************************************************/

/*****************************************************************************
 *****************************************************************************
 *************    CLIENT/CLIENT KEY EXCHANGE (DIFFIE-HELLMAN)    *************
 *****************************************************************************
 ****************************************************************************/
#include "chat.h"


//----------------------------------------------------------------------------
//  bool diffieHellmanUp(byte destAddress, byte xdata *pKeyMaterial)
//
//  Description:
//      Prepare and transmit the Diffie-Hellman "key material" to destAddress,
//		then wait for the returned key material from destAddress.
//		Create a 7-byte DES key for encrypted private communication with d.A.  
//
//	Arguments:
//		byte destAddress
//			The address we're creating the key for
//
//	Returned value:
//		bool
//			TRUE if OK, FALSE otherwise
//----------------------------------------------------------------------------
bool diffieHellmanUp(byte destAddress) {
	byte n;
	byte tempKey[8];
	CLIENT_INFO xdata *pDestCI;

	if (pDestCI = getClientInfo(destAddress)) {

		// Transmit and receive settings
		TXI.destination = destAddress;
		TXI.flags = SPP_ACK_REQ | MSG_DIFFIE_HELLMAN_UP;
		TXI.pDataBuffer = pAdminTXBuffer;
		TXI.dataLen = 8;
		RXI.pDataBuffer = pRXBuffer;

		// Prepare our own Diffie-Hellman data (DUMMY METHOD)
		srand(pMyCI->address * 0x0101);
		for (n = 0; n < 8; n++) {
			TXI.pDataBuffer[n] = rand();
		}

		// Transmit the key material
		sppSend(&TXI);
		while (SPP_STATUS() != SPP_IDLE_MODE);
		if (TXI.status != SPP_TX_FINISHED) {
			return FALSE;
		}

		// Receive the returned key material
restartRX:
		sppReceive(&RXI);
		sppSettings.rxTimeout = SHORT_RX_TIMEOUT;
		while (SPP_STATUS() != SPP_IDLE_MODE);
		if (RXI.status != SPP_RX_FINISHED) {
			return FALSE;
		} else {
			if (RXI.source != destAddress) {
				goto restartRX;
			}
		}

		// Generate the DES key and place it in the CI table (DUMMY METHOD)
		for (n = 0; n < 8; n++) {
			tempKey[n] = TXI.pDataBuffer[n] ^ RXI.pDataBuffer[n];
		}
		memcpy(pDestCI->pDESKey, tempKey, CI_KEY_LENGTH);
		pDestCI->isDESKeyValid = TRUE;

		return TRUE;

	} else {
		return FALSE;
	}

} // diffieHellmanUp




//----------------------------------------------------------------------------
//  bool diffieHellmanDown(CLIENT_INFO xdata *pSourceCI, byte xdata *pKeyM...)
//
//  Description:
//      We've just received key material from another client. Prepare and 
//		transmit our Diffie-Hellman "key material" to sourceAddress,
//		Create a 7-byte DES key for encrypted private communication with s.A.  
//
//	Arguments:
//		CLIENT_INFO xdata *pSourceCI
//			The address we're creating the key for
//		byte xdata *pKeyMaterial
//			Key material from pSourceCI
//----------------------------------------------------------------------------
void diffieHellmanDown(CLIENT_INFO xdata *pSourceCI, byte xdata *pKeyMaterial) {
	byte n;
	byte tempKey[8];

	// Transmit settings
	TXI.destination = pSourceCI->address;
	TXI.flags = SPP_ACK_REQ | MSG_DIFFIE_HELLMAN_UP;
	TXI.pDataBuffer = pAdminTXBuffer;
	TXI.dataLen = 8;
	
	// Prepare our own Diffie-Hellman data (DUMMY METHOD)
	srand(pMyCI->address * 0x0101);
	for (n = 0; n < 8; n++) {
		TXI.pDataBuffer[n] = rand();
	}

	// Transmit the key material
	sppSend(&TXI);
	while (SPP_STATUS() != SPP_IDLE_MODE);
	if (TXI.status != SPP_TX_FINISHED) {
		return;
	}

	// Generate DES key (DUMMY METHOD)
	for (n = 0; n < 8; n++) {
		tempKey[n] = TXI.pDataBuffer[n] ^ pKeyMaterial[n];
	}
	memcpy(pSourceCI->pDESKey, tempKey, CI_KEY_LENGTH);
	pSourceCI->isDESKeyValid = TRUE;

} // diffieHellmanDown

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -