/**
* @file des.c
* @brief DES (Data Encryption Standard)
*
* @section License
*
* Copyright (C) 2010-2016 Oryx Embedded SARL. All rights reserved.
*
* This file is part of CycloneCrypto Open.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @section Description
*
* DES is an encryption algorithm designed to encipher and decipher blocks of
* 64 bits under control of a 64-bit key. Refer to FIPS 46-3 for more details
*
* @author Oryx Embedded SARL (www.oryx-embedded.com)
* @version 1.7.2
**/
//! Switch to the appropriate trace level
#define TRACE_LEVEL CRYPTO_TRACE_LEVEL
//! Dependencies
#include <string.h>
#include <stdint.h>
//! #include "crypto.h"
#include "des.h"
#include "endian.h"
//! Rotate left operation
#define ROL28(a, n) ((((a) << (n)) | ((a) >> (28 - (n)))) & 0x0FFFFFFF)
//! Initial permutation
#define DES_IP(left, right) \
{ \
temp = ((left >> 4) ^ right) & 0x0F0F0F0F; \
right ^= temp; \
left ^= temp << 4; \
temp = ((left >> 16) ^ right) & 0x0000FFFF; \
right ^= temp; \
left ^= temp << 16; \
temp = ((right >> 2) ^ left) & 0x33333333; \
left ^= temp; \
right ^= temp << 2; \
temp = ((right >> 8) ^ left) & 0x00FF00FF; \
left ^= temp; \
right ^= temp << 8; \
temp = ((left >> 1) ^ right) & 0x55555555; \
right ^= temp; \
left ^= temp << 1; \
left = ROL32(left, 1); \
right = ROL32(right, 1); \
}
//! Final permutation
#define DES_FP(left, right) \
{ \
left = ROR32(left, 1); \
right = ROR32(right, 1); \
temp = ((left >> 1) ^ right) & 0x55555555; \
right ^= temp; \
left ^= temp << 1; \
temp = ((right >> 8) ^ left) & 0x00FF00FF; \
left ^= temp; \
right ^= temp << 8; \
temp = ((right >> 2) ^ left) & 0x33333333; \
left ^= temp; \
right ^= temp << 2; \
temp = ((left >> 16) ^ right) & 0x0000FFFF; \
right ^= temp; \
left ^= temp << 16; \
temp = ((left >> 4) ^ right) & 0x0F0F0F0F; \
right ^= temp; \
left ^= temp << 4; \
}
//! DES round
#define DES_ROUND(left, right, ks) \
{ \
temp = right ^ *(ks); \
left ^= sp2[(temp >> 24) & 0x3F]; \
left ^= sp4[(temp >> 16) & 0x3F]; \
left ^= sp6[(temp >> 8) & 0x3F]; \
left ^= sp8[temp & 0x3F]; \
temp = ROR32(right, 4) ^ *(ks + 1); \
left ^= sp1[(temp >> 24) & 0x3F]; \
left ^= sp3[(temp >> 16) & 0x3F]; \
left ^= sp5[(temp >> 8) & 0x3F]; \
left ^= sp7[temp & 0x3F]; \
temp = right; \
right = left; \
left = temp; \
}
//! Permuted choice 1
#define DES_PC1(left, right) \
{ \
uint temp; \
temp = ((left >> 4) ^ right) & 0x0F0F0F0F; \
right ^= temp; \
left ^= (temp << 4); \
temp = ((right >> 16) ^ left) & 0x0000FFFF; \
left ^= temp; \
right ^= (temp << 16); \
temp = ((left >> 2) ^ right) & 0x33333333; \
right ^= temp; \
left ^= (temp << 2); \
temp = ((right >> 16) ^ left) & 0x0000FFFF; \
left ^= temp; \
right ^= (temp << 16); \
temp = ((left >> 1) ^ right) & 0x55555555; \
right ^= temp; \
left ^= (temp << 1); \
temp = ((right >> 8) ^ left) & 0x00FF00FF; \
left ^= temp; \
right ^= (temp << 8); \
temp = ((left >> 1) ^ right) & 0x55555555; \
right ^= temp; \
left ^= (temp << 1); \
temp = (left << 8) | ((right >> 20) & 0x000000F0); \
left = ((right << 20) & 0x0FF00000); \
left |= ((right << 4) & 0x000FF000); \
left |= ((right >> 12) & 0x00000FF0); \
left |= ((right >> 28) & 0x0000000F); \
right = temp >> 4; \
}
//! Selection function 1
static const uint sp1[64] =
{
0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004, 0x00010000,
0x00000400, 0x01010400, 0x01010404, 0x00000400, 0x01000404, 0x01010004, 0x01000000, 0x00000004,
0x00000404, 0x01000400, 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404,
0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404, 0x00010404, 0x01000000,
0x00010000, 0x01010404, 0x00000004, 0x01010000, 0x01010400, 0x01000000, 0x01000000, 0x00000400,
0x01010004, 0x00010000, 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404,
0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404, 0x00010404, 0x01010400,
0x00000404, 0x01000400, 0x01000400, 0x00000000, 0x00010004, 0x00010400, 0x00000000, 0x01010004
};
//! Selection function 2
static const uint sp2[64] =
{
0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020, 0x80100020, 0x80008020,
0x80000020, 0x80108020, 0x80108000, 0x80000000, 0x80008000, 0x00100000, 0x00000020, 0x80100020,
0x00108000, 0x00100020, 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000,
0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000, 0x80100000, 0x00008020,
0x00000000, 0x00108020, 0x80100020, 0x00100000, 0x80008020, 0x80100000, 0x80108000, 0x00008000,
0x80100000, 0x80008000, 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000,
0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020, 0x80000020, 0x00100020,
0x00108000, 0x00000000, 0x80008000, 0x00008020, 0x80000000, 0x80100020, 0x80108020, 0x00108000
};
//! Selection function 3
static const uint sp3[64] =
{
0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000, 0x00020208, 0x08000200,
0x00020008, 0x08000008, 0x08000008, 0x00020000, 0x08020208, 0x00020008, 0x08020000, 0x00000208,
0x08000000, 0x00000008, 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208,
0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208, 0x00000200, 0x08000000,
0x08020200, 0x08000000, 0x00020008, 0x00000208, 0x00020000, 0x08020200, 0x08000200, 0x00000000,
0x00000200, 0x00020008, 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008,
0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208, 0x00020200, 0x08000008,
0x08020000, 0x08000208, 0x00000208, 0x08020000, 0x00020208, 0x00000008, 0x08020008, 0x00020200
};
//! Selection function 4
static const uint sp4[64] =
{
0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081, 0x00800001, 0x00002001,
0x00000000, 0x00802000, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00800080, 0x00800001,
0x00000001, 0x00002000, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080,
0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080, 0x00802081, 0x00000081,
0x00800080, 0x00800001, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00000000, 0x00802000,
0x00002080, 0x00800080, 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080,
0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001, 0x00802080, 0x00800081,
0x00002001, 0x00002080, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002000, 0x00802080
};
//! Selection function 5
static const uint sp5[64] =
{
0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100, 0x40000000, 0x02080000,
0x40080100, 0x00080000, 0x02000100, 0x40080100, 0x42000100, 0x42080000, 0x00080100, 0x40000000,
0x02000000, 0x40080000, 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100,
0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000, 0x42000000, 0x00080100,
0x00080000, 0x42000100, 0x000001
![avatar](https://profile-avatar.csdnimg.cn/c7605ebd585249f1b630f560f4d9ba6f_weixin_42650811.jpg!1)
我虽横行却不霸道
- 粉丝: 97
- 资源: 1万+
最新资源
- 【JCR一区级】秃鹰算法BES-Transformer-GRU负荷数据回归预测【含Matlab源码 6347期】.zip
- 【独家首发】开普勒算法KOA优化Transformer-BiLSTM负荷数据回归预测【含Matlab源码 6560期】.zip
- 【JCR一区级】雾凇算法RIME-Transformer-GRU负荷数据回归预测【含Matlab源码 6348期】.zip
- 【JCR1区】雪融算法SAO-CNN-SVM故障诊断分类预测【含Matlab源码 5823期】.zip
- 【JCR1区】蚁狮算法ALO-CNN-SVM故障诊断分类预测【含Matlab源码 5825期】.zip
- 【JCR一区级】鹈鹕算法POA-Transformer-GRU负荷数据回归预测【含Matlab源码 6345期】.zip
- 【JCR一区级】金豺算法GJO-Transformer-GRU负荷数据回归预测【含Matlab源码 6326期】.zip
- 【JCR一区级】天鹰算法AO-Transformer-GRU负荷数据回归预测【含Matlab源码 6346期】.zip
- 【LSTM时序预测】鲸鱼算法优化卷积长短期记忆神经网络WOA-CNN-LSTM股价序列预测【含Matlab源码 3008期】.zip
- 【独家首发】粒子群算法PSO优化Transformer-LSTM负荷数据回归预测【含Matlab源码 6388期】.zip
- 【JCR1区】遗传算法GA-CNN-SVM故障诊断分类预测【含Matlab源码 5824期】.zip
- 【JCR1区】飞蛾扑火算法MFO-CNN-SVM故障诊断分类预测【含Matlab源码 5784期】.zip
- 【JCR1区】引力搜索算法GSA-CNN-SVM故障诊断分类预测【含Matlab源码 5826期】.zip
- 【JCR一区级】金枪鱼算法TSO-Transformer-GRU负荷数据回归预测【含Matlab源码 6327期】.zip
- 【JCR一区级】鲸鱼算法WOA-Transformer-GRU负荷数据回归预测【含Matlab源码 6328期】.zip
- 【JCR一区级】淘金算法GRO-Transformer-GRU负荷数据回归预测【含Matlab源码 6344期】.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
评论0