des.c

来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 684 行 · 第 1/2 页

C
684
字号
/* *  des.c * *  core source file for DES-150 library *  Make key schedule from DES key. *  Encrypt/Decrypt one 8-byte block. * * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is the DES-150 library. * * The Initial Developer of the Original Code is Nelson B. Bolyard, * nelsonb@iname.com.  Portions created by Nelson B. Bolyard are  * Copyright (C) 1990, 2000  Nelson B. Bolyard, All Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable * instead of those above.  If you wish to allow use of your * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL.  If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the GPL. */#include "des.h"#include <stddef.h>	/* for ptrdiff_t *//* #define USE_INDEXING 1 *//* * The tables below are the 8 sbox functions, with the 6-bit input permutation  * and the 32-bit output permutation pre-computed. * They are shifted circularly to the left 3 bits, which removes 2 shifts * and an or from each round by reducing the number of sboxes whose * indices cross word broundaries from 2 to 1.   */static const HALF SP[8][64] = {/* Box S1 */ { 	0x04041000, 0x00000000, 0x00040000, 0x04041010, 	0x04040010, 0x00041010, 0x00000010, 0x00040000, 	0x00001000, 0x04041000, 0x04041010, 0x00001000, 	0x04001010, 0x04040010, 0x04000000, 0x00000010, 	0x00001010, 0x04001000, 0x04001000, 0x00041000, 	0x00041000, 0x04040000, 0x04040000, 0x04001010, 	0x00040010, 0x04000010, 0x04000010, 0x00040010, 	0x00000000, 0x00001010, 0x00041010, 0x04000000, 	0x00040000, 0x04041010, 0x00000010, 0x04040000, 	0x04041000, 0x04000000, 0x04000000, 0x00001000, 	0x04040010, 0x00040000, 0x00041000, 0x04000010, 	0x00001000, 0x00000010, 0x04001010, 0x00041010, 	0x04041010, 0x00040010, 0x04040000, 0x04001010, 	0x04000010, 0x00001010, 0x00041010, 0x04041000, 	0x00001010, 0x04001000, 0x04001000, 0x00000000, 	0x00040010, 0x00041000, 0x00000000, 0x04040010    },/* Box S2 */ { 	0x00420082, 0x00020002, 0x00020000, 0x00420080, 	0x00400000, 0x00000080, 0x00400082, 0x00020082, 	0x00000082, 0x00420082, 0x00420002, 0x00000002, 	0x00020002, 0x00400000, 0x00000080, 0x00400082, 	0x00420000, 0x00400080, 0x00020082, 0x00000000, 	0x00000002, 0x00020000, 0x00420080, 0x00400002, 	0x00400080, 0x00000082, 0x00000000, 0x00420000, 	0x00020080, 0x00420002, 0x00400002, 0x00020080, 	0x00000000, 0x00420080, 0x00400082, 0x00400000, 	0x00020082, 0x00400002, 0x00420002, 0x00020000, 	0x00400002, 0x00020002, 0x00000080, 0x00420082, 	0x00420080, 0x00000080, 0x00020000, 0x00000002, 	0x00020080, 0x00420002, 0x00400000, 0x00000082, 	0x00400080, 0x00020082, 0x00000082, 0x00400080, 	0x00420000, 0x00000000, 0x00020002, 0x00020080, 	0x00000002, 0x00400082, 0x00420082, 0x00420000     },/* Box S3 */ { 	0x00000820, 0x20080800, 0x00000000, 0x20080020, 	0x20000800, 0x00000000, 0x00080820, 0x20000800, 	0x00080020, 0x20000020, 0x20000020, 0x00080000, 	0x20080820, 0x00080020, 0x20080000, 0x00000820, 	0x20000000, 0x00000020, 0x20080800, 0x00000800, 	0x00080800, 0x20080000, 0x20080020, 0x00080820, 	0x20000820, 0x00080800, 0x00080000, 0x20000820, 	0x00000020, 0x20080820, 0x00000800, 0x20000000, 	0x20080800, 0x20000000, 0x00080020, 0x00000820, 	0x00080000, 0x20080800, 0x20000800, 0x00000000, 	0x00000800, 0x00080020, 0x20080820, 0x20000800, 	0x20000020, 0x00000800, 0x00000000, 0x20080020, 	0x20000820, 0x00080000, 0x20000000, 0x20080820, 	0x00000020, 0x00080820, 0x00080800, 0x20000020, 	0x20080000, 0x20000820, 0x00000820, 0x20080000, 	0x00080820, 0x00000020, 0x20080020, 0x00080800     },/* Box S4 */ { 	0x02008004, 0x00008204, 0x00008204, 0x00000200, 	0x02008200, 0x02000204, 0x02000004, 0x00008004, 	0x00000000, 0x02008000, 0x02008000, 0x02008204, 	0x00000204, 0x00000000, 0x02000200, 0x02000004, 	0x00000004, 0x00008000, 0x02000000, 0x02008004, 	0x00000200, 0x02000000, 0x00008004, 0x00008200, 	0x02000204, 0x00000004, 0x00008200, 0x02000200, 	0x00008000, 0x02008200, 0x02008204, 0x00000204, 	0x02000200, 0x02000004, 0x02008000, 0x02008204, 	0x00000204, 0x00000000, 0x00000000, 0x02008000, 	0x00008200, 0x02000200, 0x02000204, 0x00000004, 	0x02008004, 0x00008204, 0x00008204, 0x00000200, 	0x02008204, 0x00000204, 0x00000004, 0x00008000, 	0x02000004, 0x00008004, 0x02008200, 0x02000204, 	0x00008004, 0x00008200, 0x02000000, 0x02008004, 	0x00000200, 0x02000000, 0x00008000, 0x02008200     },/* Box S5 */ { 	0x00000400, 0x08200400, 0x08200000, 0x08000401, 	0x00200000, 0x00000400, 0x00000001, 0x08200000, 	0x00200401, 0x00200000, 0x08000400, 0x00200401, 	0x08000401, 0x08200001, 0x00200400, 0x00000001, 	0x08000000, 0x00200001, 0x00200001, 0x00000000, 	0x00000401, 0x08200401, 0x08200401, 0x08000400, 	0x08200001, 0x00000401, 0x00000000, 0x08000001, 	0x08200400, 0x08000000, 0x08000001, 0x00200400, 	0x00200000, 0x08000401, 0x00000400, 0x08000000, 	0x00000001, 0x08200000, 0x08000401, 0x00200401, 	0x08000400, 0x00000001, 0x08200001, 0x08200400, 	0x00200401, 0x00000400, 0x08000000, 0x08200001, 	0x08200401, 0x00200400, 0x08000001, 0x08200401, 	0x08200000, 0x00000000, 0x00200001, 0x08000001, 	0x00200400, 0x08000400, 0x00000401, 0x00200000, 	0x00000000, 0x00200001, 0x08200400, 0x00000401    },/* Box S6 */ { 	0x80000040, 0x81000000, 0x00010000, 0x81010040, 	0x81000000, 0x00000040, 0x81010040, 0x01000000, 	0x80010000, 0x01010040, 0x01000000, 0x80000040, 	0x01000040, 0x80010000, 0x80000000, 0x00010040, 	0x00000000, 0x01000040, 0x80010040, 0x00010000, 	0x01010000, 0x80010040, 0x00000040, 0x81000040, 	0x81000040, 0x00000000, 0x01010040, 0x81010000, 	0x00010040, 0x01010000, 0x81010000, 0x80000000, 	0x80010000, 0x00000040, 0x81000040, 0x01010000, 	0x81010040, 0x01000000, 0x00010040, 0x80000040, 	0x01000000, 0x80010000, 0x80000000, 0x00010040, 	0x80000040, 0x81010040, 0x01010000, 0x81000000, 	0x01010040, 0x81010000, 0x00000000, 0x81000040, 	0x00000040, 0x00010000, 0x81000000, 0x01010040, 	0x00010000, 0x01000040, 0x80010040, 0x00000000, 	0x81010000, 0x80000000, 0x01000040, 0x80010040     },/* Box S7 */ { 	0x00800000, 0x10800008, 0x10002008, 0x00000000, 	0x00002000, 0x10002008, 0x00802008, 0x10802000, 	0x10802008, 0x00800000, 0x00000000, 0x10000008, 	0x00000008, 0x10000000, 0x10800008, 0x00002008, 	0x10002000, 0x00802008, 0x00800008, 0x10002000, 	0x10000008, 0x10800000, 0x10802000, 0x00800008, 	0x10800000, 0x00002000, 0x00002008, 0x10802008, 	0x00802000, 0x00000008, 0x10000000, 0x00802000, 	0x10000000, 0x00802000, 0x00800000, 0x10002008, 	0x10002008, 0x10800008, 0x10800008, 0x00000008, 	0x00800008, 0x10000000, 0x10002000, 0x00800000, 	0x10802000, 0x00002008, 0x00802008, 0x10802000, 	0x00002008, 0x10000008, 0x10802008, 0x10800000, 	0x00802000, 0x00000000, 0x00000008, 0x10802008, 	0x00000000, 0x00802008, 0x10800000, 0x00002000, 	0x10000008, 0x10002000, 0x00002000, 0x00800008     },/* Box S8 */ { 	0x40004100, 0x00004000, 0x00100000, 0x40104100, 	0x40000000, 0x40004100, 0x00000100, 0x40000000, 	0x00100100, 0x40100000, 0x40104100, 0x00104000, 	0x40104000, 0x00104100, 0x00004000, 0x00000100, 	0x40100000, 0x40000100, 0x40004000, 0x00004100, 	0x00104000, 0x00100100, 0x40100100, 0x40104000, 	0x00004100, 0x00000000, 0x00000000, 0x40100100, 	0x40000100, 0x40004000, 0x00104100, 0x00100000, 	0x00104100, 0x00100000, 0x40104000, 0x00004000, 	0x00000100, 0x40100100, 0x00004000, 0x00104100, 	0x40004000, 0x00000100, 0x40000100, 0x40100000, 	0x40100100, 0x40000000, 0x00100000, 0x40004100, 	0x00000000, 0x40104100, 0x00100100, 0x40000100, 	0x40100000, 0x40004000, 0x40004100, 0x00000000, 	0x40104100, 0x00104000, 0x00104000, 0x00004100, 	0x00004100, 0x00100100, 0x40000000, 0x40104000     }};static const HALF PC2[8][64] = {/* table 0 */ {    0x00000000, 0x00001000, 0x04000000, 0x04001000,     0x00100000, 0x00101000, 0x04100000, 0x04101000,     0x00008000, 0x00009000, 0x04008000, 0x04009000,     0x00108000, 0x00109000, 0x04108000, 0x04109000,     0x00000004, 0x00001004, 0x04000004, 0x04001004,     0x00100004, 0x00101004, 0x04100004, 0x04101004,     0x00008004, 0x00009004, 0x04008004, 0x04009004,     0x00108004, 0x00109004, 0x04108004, 0x04109004,     0x08000000, 0x08001000, 0x0c000000, 0x0c001000,     0x08100000, 0x08101000, 0x0c100000, 0x0c101000,     0x08008000, 0x08009000, 0x0c008000, 0x0c009000,     0x08108000, 0x08109000, 0x0c108000, 0x0c109000,     0x08000004, 0x08001004, 0x0c000004, 0x0c001004,     0x08100004, 0x08101004, 0x0c100004, 0x0c101004,     0x08008004, 0x08009004, 0x0c008004, 0x0c009004,     0x08108004, 0x08109004, 0x0c108004, 0x0c109004  },/* table 1 */ {    0x00000000, 0x00002000, 0x80000000, 0x80002000,     0x00000008, 0x00002008, 0x80000008, 0x80002008,     0x00200000, 0x00202000, 0x80200000, 0x80202000,     0x00200008, 0x00202008, 0x80200008, 0x80202008,     0x20000000, 0x20002000, 0xa0000000, 0xa0002000,     0x20000008, 0x20002008, 0xa0000008, 0xa0002008,     0x20200000, 0x20202000, 0xa0200000, 0xa0202000,     0x20200008, 0x20202008, 0xa0200008, 0xa0202008,     0x00000400, 0x00002400, 0x80000400, 0x80002400,     0x00000408, 0x00002408, 0x80000408, 0x80002408,     0x00200400, 0x00202400, 0x80200400, 0x80202400,     0x00200408, 0x00202408, 0x80200408, 0x80202408,     0x20000400, 0x20002400, 0xa0000400, 0xa0002400,     0x20000408, 0x20002408, 0xa0000408, 0xa0002408,     0x20200400, 0x20202400, 0xa0200400, 0xa0202400,     0x20200408, 0x20202408, 0xa0200408, 0xa0202408  },/* table 2 */ {    0x00000000, 0x00004000, 0x00000020, 0x00004020,     0x00080000, 0x00084000, 0x00080020, 0x00084020,     0x00000800, 0x00004800, 0x00000820, 0x00004820,     0x00080800, 0x00084800, 0x00080820, 0x00084820,     0x00000010, 0x00004010, 0x00000030, 0x00004030,     0x00080010, 0x00084010, 0x00080030, 0x00084030,     0x00000810, 0x00004810, 0x00000830, 0x00004830,     0x00080810, 0x00084810, 0x00080830, 0x00084830,     0x00400000, 0x00404000, 0x00400020, 0x00404020,     0x00480000, 0x00484000, 0x00480020, 0x00484020,     0x00400800, 0x00404800, 0x00400820, 0x00404820,     0x00480800, 0x00484800, 0x00480820, 0x00484820,     0x00400010, 0x00404010, 0x00400030, 0x00404030,     0x00480010, 0x00484010, 0x00480030, 0x00484030,     0x00400810, 0x00404810, 0x00400830, 0x00404830,     0x00480810, 0x00484810, 0x00480830, 0x00484830   },/* table 3 */ {    0x00000000, 0x40000000, 0x00000080, 0x40000080,     0x00040000, 0x40040000, 0x00040080, 0x40040080,     0x00000040, 0x40000040, 0x000000c0, 0x400000c0,     0x00040040, 0x40040040, 0x000400c0, 0x400400c0,     0x10000000, 0x50000000, 0x10000080, 0x50000080,     0x10040000, 0x50040000, 0x10040080, 0x50040080,     0x10000040, 0x50000040, 0x100000c0, 0x500000c0,     0x10040040, 0x50040040, 0x100400c0, 0x500400c0,     0x00800000, 0x40800000, 0x00800080, 0x40800080,     0x00840000, 0x40840000, 0x00840080, 0x40840080,     0x00800040, 0x40800040, 0x008000c0, 0x408000c0,     0x00840040, 0x40840040, 0x008400c0, 0x408400c0,     0x10800000, 0x50800000, 0x10800080, 0x50800080,     0x10840000, 0x50840000, 0x10840080, 0x50840080,     0x10800040, 0x50800040, 0x108000c0, 0x508000c0,     0x10840040, 0x50840040, 0x108400c0, 0x508400c0   },/* table 4 */ {    0x00000000, 0x00000008, 0x08000000, 0x08000008,     0x00040000, 0x00040008, 0x08040000, 0x08040008,     0x00002000, 0x00002008, 0x08002000, 0x08002008,     0x00042000, 0x00042008, 0x08042000, 0x08042008,     0x80000000, 0x80000008, 0x88000000, 0x88000008,     0x80040000, 0x80040008, 0x88040000, 0x88040008,     0x80002000, 0x80002008, 0x88002000, 0x88002008,     0x80042000, 0x80042008, 0x88042000, 0x88042008,     0x00080000, 0x00080008, 0x08080000, 0x08080008,     0x000c0000, 0x000c0008, 0x080c0000, 0x080c0008,     0x00082000, 0x00082008, 0x08082000, 0x08082008,     0x000c2000, 0x000c2008, 0x080c2000, 0x080c2008,     0x80080000, 0x80080008, 0x88080000, 0x88080008,     0x800c0000, 0x800c0008, 0x880c0000, 0x880c0008,     0x80082000, 0x80082008, 0x88082000, 0x88082008,     0x800c2000, 0x800c2008, 0x880c2000, 0x880c2008   },/* table 5 */ {    0x00000000, 0x00400000, 0x00008000, 0x00408000,     0x40000000, 0x40400000, 0x40008000, 0x40408000,     0x00000020, 0x00400020, 0x00008020, 0x00408020,     0x40000020, 0x40400020, 0x40008020, 0x40408020,     0x00001000, 0x00401000, 0x00009000, 0x00409000,     0x40001000, 0x40401000, 0x40009000, 0x40409000,     0x00001020, 0x00401020, 0x00009020, 0x00409020,     0x40001020, 0x40401020, 0x40009020, 0x40409020,     0x00100000, 0x00500000, 0x00108000, 0x00508000,     0x40100000, 0x40500000, 0x40108000, 0x40508000,     0x00100020, 0x00500020, 0x00108020, 0x00508020,     0x40100020, 0x40500020, 0x40108020, 0x40508020,     0x00101000, 0x00501000, 0x00109000, 0x00509000,     0x40101000, 0x40501000, 0x40109000, 0x40509000,     0x00101020, 0x00501020, 0x00109020, 0x00509020,     0x40101020, 0x40501020, 0x40109020, 0x40509020   },/* table 6 */ {    0x00000000, 0x00000040, 0x04000000, 0x04000040,     0x00000800, 0x00000840, 0x04000800, 0x04000840,     0x00800000, 0x00800040, 0x04800000, 0x04800040,     0x00800800, 0x00800840, 0x04800800, 0x04800840,     0x10000000, 0x10000040, 0x14000000, 0x14000040,     0x10000800, 0x10000840, 0x14000800, 0x14000840,     0x10800000, 0x10800040, 0x14800000, 0x14800040,     0x10800800, 0x10800840, 0x14800800, 0x14800840,     0x00000080, 0x000000c0, 0x04000080, 0x040000c0,     0x00000880, 0x000008c0, 0x04000880, 0x040008c0,     0x00800080, 0x008000c0, 0x04800080, 0x048000c0,     0x00800880, 0x008008c0, 0x04800880, 0x048008c0,     0x10000080, 0x100000c0, 0x14000080, 0x140000c0,     0x10000880, 0x100008c0, 0x14000880, 0x140008c0,     0x10800080, 0x108000c0, 0x14800080, 0x148000c0,     0x10800880, 0x108008c0, 0x14800880, 0x148008c0   },/* table 7 */ {    0x00000000, 0x00000010, 0x00000400, 0x00000410,     0x00000004, 0x00000014, 0x00000404, 0x00000414,     0x00004000, 0x00004010, 0x00004400, 0x00004410,     0x00004004, 0x00004014, 0x00004404, 0x00004414,     0x20000000, 0x20000010, 0x20000400, 0x20000410,     0x20000004, 0x20000014, 0x20000404, 0x20000414,     0x20004000, 0x20004010, 0x20004400, 0x20004410,     0x20004004, 0x20004014, 0x20004404, 0x20004414,     0x00200000, 0x00200010, 0x00200400, 0x00200410,     0x00200004, 0x00200014, 0x00200404, 0x00200414,     0x00204000, 0x00204010, 0x00204400, 0x00204410,     0x00204004, 0x00204014, 0x00204404, 0x00204414,     0x20200000, 0x20200010, 0x20200400, 0x20200410,     0x20200004, 0x20200014, 0x20200404, 0x20200414,     0x20204000, 0x20204010, 0x20204400, 0x20204410,     0x20204004, 0x20204014, 0x20204404, 0x20204414   }};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?