?? des.c
字號:
/*****************************************************************************
* *
* ********** *
* ************ *
* *** *** *
* *** +++ *** *
* *** + + *** *
* *** + CHIPCON CC1010 Wireless audio project *
* *** + + *** *
* *** +++ *** DES - extended version of HAL - DES *
* *** *** *
* *********** *
* ********* *
* *
*****************************************************************************
* This source file is part of a software project for Full Duplex, *
* single-chip, wireless intercom, written for the CC1010 chip *
* (RF-transceiver chip with integrated 8051 micro-controller). *
*****************************************************************************
* Author: ROH, (OAE) *
*****************************************************************************
* Revision history: *
* *
* $Log: DES.c,v $
* Revision 1.1 2003/08/04 12:35:06 tos
* Initial version in CVS.
*
* *
* *
****************************************************************************/
#include <chipcon/hal.h>
//----------------------------------------------------------------------------
// byte* DES(...)
//
// Description:
// This function performs DES encryption/decryption on a block of data.
// The encryption/decryption operations are performed in place (i.e.
// the plaintext is overwritten by the ciphertext or vice versa) on
// suitably aligned data [ address(_buffer_) mod 8 = 0 ]. _key_ should
// point to the key used for single-DES operations or keys in the case
// of triple-DES.
// Two modes of the DES standard are supported: 8-bit Cipher Feedback
// (CFB) and Output Feedback (OFB.) CFB is self-synchronizing (the end of
// a ciphertext can be decrypted even though the beginning is unavailable)
// and can be used to calculate error check codes. OFB is not self-
// synchronizing and is not suitable for error check code calculation,
// but does have the favourable property that a single bit error in the
// received ciphertext produces only a single bit error in the decrypted
// plaintext.
// In CFB/OFB mode an initialization vector of 8 bytes is part of the
// algorithm. This vector must be identical for the encryption and
// decryption process. The choice of initialization data does not affect
// the security of the encryption in any way, and this function thus uses
// the value 0.
// _option_ is used to chose between these modes of operation, between
// single-DES and triple-DES, and between decryption and decryption.
// This function does not return until all data has been encrypted/
// decrypted, since the DES hardware is so fast. The DES hardware can
// run at the same time as the 8051 and generate an intterrupt when
// finished. If this is desired the hardware must be programmed directly.
//
// Arguments:
// byte options
// One or more of the below defined constants define the desired
// operational mode:
// DES_SINGLE_DES
// DES_TRIPLE_DES
// DES_ENCRYPT
// DES_DECRYPT
// DES_OFB_MODE
// DES_CFB_MODE
// byte xdata* key
// A pointer to a key (or three keys for triple-DES) stored in
// XDATA memory space. This address must be divisible by eight, i.e.
// address(_key_) mod 8 = 0. The 56 active bits of a DES-key are
// expected to be in a compressed 7-byte format, in which all parity
// bits are removed. In the case of a single key, the 56 bits of the key
// must lie on _key_[0] - _key_[6], in big-endian order. In the case of
// three keys (triple-des), the three keys must lie on _key_[0] -
// _key_[6], _key_[8] - _key_[14] and _key_[16] - _key_[22], all in big-
// endian order. The macro DES_NORMAL_2_COMPACT_KEY(...) can be used
// to convert a regular DES key to the compact form. A key can be
// generated by simply using 7 random bytes.
//
// byte xdata* buffer
// Pointer to the data to encrypt/decrypt in XDATA memory space.
// Encryption/decryption is performed in-place, i.e. the original
// plaintext/ciphertext is overwritten. The address of _buffer_ must
// be divisible by eight, i.e. address(_buffer_) mod 8 = 0.
// word length
// The number of bytes to perform the encryption/decryption on.
// bit wait
// If this bit is set, the function will wait until encryption/decryption
// is completed, otherwise the function will return a
//
// Return value:
// byte*
// A pointer to the start of _buffer_ is returned.
//----------------------------------------------------------------------------
byte* DES(byte options, byte xdata* buffer, byte xdata* key, word length, bit wait) {
CRPCON=0x10|(options&0x0E); // Init control register
CRPKEY=((word)key)>>3; // Put address of key(s) into register
CRPDAT=((word)buffer)>>3; // Put address of data into register
CRPINI7=CRPINI6=CRPINI5=CRPINI4=0; // Clear initialization vector
CRPINI3=CRPINI2=CRPINI1=CRPINI0=0;
while (length) {
// Encryption/decryption performed in blocks of maximum 256 bytes.
if (length&0xFF00){ // >=256
CRPCNT=0;
length-=256;
} else {
CRPCNT=length;
length=0;
}
CRPCON|=0x01; // Start encryption/decryption
// Wait until finished if wait bit is set
if (wait)
while (CRPCON&0x01);
}
return buffer;
}
//----------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -