/**
* @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
我虽横行却不霸道
- 粉丝: 95
- 资源: 1万+
最新资源
- awewq1132323
- 手写流程图检测31-YOLO(v5至v8)、COCO、CreateML、Darknet、Paligemma、TFRecord数据集合集.rar
- frida拦截微信小程序云托管API
- 肝脏及其肿瘤分割的 CT 数据集,已经切片成jpg数据,约2w张数据和mask
- 基于Java的网上教务评教管理系统的设计与实现.doc
- 2024圣诞节海外消费市场趋势及营销策略分析报告
- JWaaaaaaaaaaaaaaaaaaaa
- Python实现常见排序算法详解
- 等发达地区的无穷大无穷大无穷大请问
- 微藻检测19-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0