📄 des.c
字号:
/*****************************************************************************
* *
* ********** *
* ************ *
* *** *** *
* *** +++ *** *
* *** + + *** *
* *** + CHIPCON CC1010 *
* *** + + *** HAL - DES *
* *** +++ *** *
* *** *** *
* *********** *
* ********* *
* *
*****************************************************************************
* *
*****************************************************************************
* Author: ROH *
*****************************************************************************
* Revision history: *
* *
* $Log: DES.c,v $
* Revision 1.2 2002/11/21 10:53:56 tos
* Cosmetic change: corrected inconsistent header text.
*
* Revision 1.1 2002/10/14 13:04:31 tos
* Initial version in CVS.
*
* *
****************************************************************************/
#include <chipcon/hal.h>
//----------------------------------------------------------------------------
// byte* halDES(...)
//
// 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.
//
// Return value:
// byte*
// A pointer to the start of _buffer_ is returned.
//----------------------------------------------------------------------------
byte* halDES(byte options, byte xdata* buffer, byte xdata* key, word length) {
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 decryption
while (CRPCON&0x01); // Wait until finished
CRPDAT+=256/8; // Increment data pointer to next 256-byte block
}
return buffer;
}
//----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -