- UID
- 1029342
- 性别
- 男
|
const uint8_t rots[16] =
{
1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
};
const uint8_t bit_msk[8] =
{
0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
};
uint8_t DES_Encrypt_key[8];
uint8_t DES_Decrypt_key[8];
uint8_t sub_keys[16][8]; //sub_keys[16][8]
uint8_t main_key[8];
void des(uint8_t*, uint8_t*, uint8_t, uint8_t*);
void FLASH_Read_KEYS(uint8_t key_index);
static void transpose (uint8_t*, uint8_t*, const uint8_t*, uint8_t);
static void rotate_l (uint8_t*);
static void compute_subkeys (uint8_t*);
static void f (uint8_t*, uint8_t*, uint8_t*);
/************************************************************************/
/* */
/* Module title: des */
/* Module type: des mainrutine */
/* */
/* Author: YXH */
/* Date: 2012-07-13 */
/* */
/* Last changed by: YXH */
/* Date: 2012-07-13 */
/* */
/* Functional Description: Encipher and decipher 64 bits string */
/* according to a 64 bits key string */
/* The string format is shown below */
/* */
/* input parameter 1: pointer to 64 bits input string */
/* 2: pointer to 64 bits key string */
/* 3: boolean if false indicating enciphering */
/* if true dechiphering */
/* 4: pointer to a 64 bits output string */
/************************************************************************/
/* */
/* msb lsb */
/* bit bit */
/* +-- -- -- -- -- -- -- --+ */
/* addr !1st 8th! */
/* +-- -- -- -- -- -- -- --+ */
/* addr+1 !9th 16th! */
/* +-- -- -- -- -- -- -- --+ */
/* : : */
/* : : */
/* +-- -- -- -- -- -- -- --+ */
/* addr+7 !57th 64th! */
/* +-- -- -- -- -- -- -- --+ */
/* */
/************************************************************************/
void des(uint8_t *plain_strng, uint8_t *key, uint8_t d, uint8_t *ciph_strng)
{
uint8_t a_str[8], b_str[8], x_str[8];
uint8_t i, j, *pkey, temp;
for (i = 0; i < 8 ; ++i)
{
if (key[i] != main_key[i])
{
compute_subkeys(key);
i = 7;
}
}
transpose(plain_strng, a_str, initial_tr, 64);
for (i=1; i < 17; ++i)
{
for (j=0; j < 8; ++j){b_str[j] = a_str[j];}
if (!d) /*enchipher*/
pkey = &sub_keys[i-1][0];
else /*dechipher*/
pkey = &sub_keys[16-i][0];
for (j=0; j < 4; ++j){a_str[j] = b_str[j+4];}
f(pkey, a_str, x_str);
for (j=0; j < 4; ++j) {a_str[j+4] = b_str[j] ^ x_str[j];}
}
temp = a_str[0]; a_str[0] = a_str[4]; a_str[4] = temp;
temp = a_str[1]; a_str[1] = a_str[5]; a_str[5] = temp;
temp = a_str[2]; a_str[2] = a_str[6]; a_str[6] = temp;
temp = a_str[3]; a_str[3] = a_str[7]; a_str[7] = temp;
transpose(a_str, ciph_strng, final_tr, 64);
}
/************************************************************************/
/* */
/* Module title: transpose */
/* Module type: des subrutine */
/* */
/* Author: YXH */
/* Date: 2012-07-13 */
/* */
/* Last changed by: YXH */
/* Date: 2012-07-13 */
/* */
/* Functional Description: Permute n bits in a string, according */
/* to a table describing the new order. */
/* 0 < n <= 64 */
/* */
/* input parameter 1: pointer to first byte in input string */
/* 2: pointer to first byte in output string */
/* 3: pointer to table describing new order */
/* 4: number of bits to be permuted */
/************************************************************************/
void transpose(uint8_t *idata, uint8_t *odata, const uint8_t *tbl, uint8_t n)
{
const uint8_t *tab_adr;
int i, bi_idx;
tab_adr = &bit_msk[0];
i = 0;
do
{odata[i++] = 0;}
while (i < 8);
i = 0;
do
{
bi_idx = *tbl++;
if (idata[bi_idx>>3] & tab_adr[bi_idx & 7])
{
odata[i>>3] |= tab_adr[i & 7];
}
}
while (++i < n);
}
/************************************************************************/
/* */
/* Module title: rotate_l */
/* Module type: des subrutine */
/* */
/* Author: YXH */
/* Date: 2012-07-13 */
/* */
/* Last changed by: YXH */
/* Date: 2012-07-13 */
/* */
/* Functional Description: rotate 2 concatenated strings of 28 */
/* bits one position to the left. */
/* */
/* input parameter 1: pointer to first byte in key string */
/* */
/************************************************************************/
void rotate_l(uint8_t *key)
{
uint8_t str_x[8];
uint8_t i;
for (i=0; i < 8; ++i) str_x[i] = key[i];
for (i=0; i < 7; ++i)
{
key[i] = (key[i] << 1);
if ((i < 6) && ((str_x[i+1] & 128) == 128))
key[i] |= 1;
}
if (str_x[0] & 0x80 )
key[3] |= 0x10;
else
key[3] &= ~0x10;
if (str_x[3] & 0x08 )
key[6] |= 0x01;
else
key[6] &= ~0x01;
}
/************************************************************************/
/* */
/* Module title: compute_subkeys */
/* Module type: des subrutine */
/* */
/* Author: YXH */
/* Date: 2012-07-13 */
/* */
/* Last changed by: YXH */
/* Date: 2012-07-13 */
/* */
/* Functional Description: Computes the 16 sub keys for use in the */
/* DES algorithm */
/* */
/* input parameter 1: pointer to first byte in key string */
/* output : fills the array sub_keys[16][8] with */
/* sub keys and stores the input key in */
/* main_key[8] */
/************************************************************************/
void compute_subkeys(uint8_t *key)
{
uint8_t i, j, ikey[8], okey[8];
for (i=0; i < 8; ++i)
{
main_key[i] = key[i];
}
transpose(key, ikey, key_tr1, 56);
for (i=0; i < 16; ++i)
{
for (j=0; j < rots[i]; ++j) {rotate_l(ikey);}
transpose(ikey, okey, key_tr2, 64);
for (j=0; j < 8; ++j)
{ sub_keys[i][j] = okey[j];}
}
} |
|