Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/vncdll/winvnc/d3des.c
Views: 11779
/*1* This is D3DES (V5.09) by Richard Outerbridge with the double and2* triple-length support removed for use in VNC. Also the bytebit[] array3* has been reversed so that the most significant bit in each byte of the4* key is ignored, not the least significant.5*6* These changes are7* Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.8*9* This software is distributed in the hope that it will be useful,10* but WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.12*/1314/* D3DES (V5.09) -15*16* A portable, public domain, version of the Data Encryption Standard.17*18* Written with Symantec's THINK (Lightspeed) C by Richard Outerbridge.19* Thanks to: Dan Hoey for his excellent Initial and Inverse permutation20* code; Jim Gillogly & Phil Karn for the DES key schedule code; Dennis21* Ferguson, Eric Young and Dana How for comparing notes; and Ray Lau,22* for humouring me on.23*24* Copyright (c) 1988,1989,1990,1991,1992 by Richard Outerbridge.25* (GEnie : OUTER; CIS : [71755,204]) Graven Imagery, 1992.26*/2728#include "d3des.h"2930static void scrunch(unsigned char *, unsigned long *);31static void unscrun(unsigned long *, unsigned char *);32static void desfunc(unsigned long *, unsigned long *);33static void cookey(unsigned long *);3435static unsigned long KnL[32] = { 0L };36static unsigned long KnR[32] = { 0L };37static unsigned long Kn3[32] = { 0L };38static unsigned char Df_Key[24] = {390x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,400xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10,410x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67 };4243static unsigned short bytebit[8] = {4401, 02, 04, 010, 020, 040, 0100, 0200 };4546static unsigned long bigbyte[24] = {470x800000L, 0x400000L, 0x200000L, 0x100000L,480x80000L, 0x40000L, 0x20000L, 0x10000L,490x8000L, 0x4000L, 0x2000L, 0x1000L,500x800L, 0x400L, 0x200L, 0x100L,510x80L, 0x40L, 0x20L, 0x10L,520x8L, 0x4L, 0x2L, 0x1L };5354/* Use the key schedule specified in the Standard (ANSI X3.92-1981). */5556static unsigned char pc1[56] = {5756, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17,589, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35,5962, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21,6013, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19, 11, 3 };6162static unsigned char totrot[16] = {631,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 };6465static unsigned char pc2[48] = {6613, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9,6722, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1,6840, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,6943, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 };7071void deskey(key, edf) /* Thanks to James Gillogly & Phil Karn! */72unsigned char *key;73int edf;74{75register int i, j, l, m, n;76unsigned char pc1m[56], pcr[56];77unsigned long kn[32];7879for ( j = 0; j < 56; j++ ) {80l = pc1[j];81m = l & 07;82pc1m[j] = (key[l >> 3] & bytebit[m]) ? 1 : 0;83}84for( i = 0; i < 16; i++ ) {85if( edf == DE1 ) m = (15 - i) << 1;86else m = i << 1;87n = m + 1;88kn[m] = kn[n] = 0L;89for( j = 0; j < 28; j++ ) {90l = j + totrot[i];91if( l < 28 ) pcr[j] = pc1m[l];92else pcr[j] = pc1m[l - 28];93}94for( j = 28; j < 56; j++ ) {95l = j + totrot[i];96if( l < 56 ) pcr[j] = pc1m[l];97else pcr[j] = pc1m[l - 28];98}99for( j = 0; j < 24; j++ ) {100if( pcr[pc2[j]] ) kn[m] |= bigbyte[j];101if( pcr[pc2[j+24]] ) kn[n] |= bigbyte[j];102}103}104cookey(kn);105return;106}107108static void cookey(raw1)109register unsigned long *raw1;110{111register unsigned long *cook, *raw0;112unsigned long dough[32];113register int i;114115cook = dough;116for( i = 0; i < 16; i++, raw1++ ) {117raw0 = raw1++;118*cook = (*raw0 & 0x00fc0000L) << 6;119*cook |= (*raw0 & 0x00000fc0L) << 10;120*cook |= (*raw1 & 0x00fc0000L) >> 10;121*cook++ |= (*raw1 & 0x00000fc0L) >> 6;122*cook = (*raw0 & 0x0003f000L) << 12;123*cook |= (*raw0 & 0x0000003fL) << 16;124*cook |= (*raw1 & 0x0003f000L) >> 4;125*cook++ |= (*raw1 & 0x0000003fL);126}127usekey(dough);128return;129}130131void cpkey(into)132register unsigned long *into;133{134register unsigned long *from, *endp;135136from = KnL, endp = &KnL[32];137while( from < endp ) *into++ = *from++;138return;139}140141void usekey(from)142register unsigned long *from;143{144register unsigned long *to, *endp;145146to = KnL, endp = &KnL[32];147while( to < endp ) *to++ = *from++;148return;149}150151void des(inblock, outblock)152unsigned char *inblock, *outblock;153{154unsigned long work[2];155156scrunch(inblock, work);157desfunc(work, KnL);158unscrun(work, outblock);159return;160}161162static void scrunch(outof, into)163register unsigned char *outof;164register unsigned long *into;165{166*into = (*outof++ & 0xffL) << 24;167*into |= (*outof++ & 0xffL) << 16;168*into |= (*outof++ & 0xffL) << 8;169*into++ |= (*outof++ & 0xffL);170*into = (*outof++ & 0xffL) << 24;171*into |= (*outof++ & 0xffL) << 16;172*into |= (*outof++ & 0xffL) << 8;173*into |= (*outof & 0xffL);174return;175}176177static void unscrun(outof, into)178register unsigned long *outof;179register unsigned char *into;180{181*into++ = (unsigned char)((*outof >> 24) & 0xffL);182*into++ = (unsigned char)((*outof >> 16) & 0xffL);183*into++ = (unsigned char)((*outof >> 8) & 0xffL);184*into++ = (unsigned char)( *outof++ & 0xffL);185*into++ = (unsigned char)((*outof >> 24) & 0xffL);186*into++ = (unsigned char)((*outof >> 16) & 0xffL);187*into++ = (unsigned char)((*outof >> 8) & 0xffL);188*into = (unsigned char)( *outof & 0xffL);189return;190}191192static unsigned long SP1[64] = {1930x01010400L, 0x00000000L, 0x00010000L, 0x01010404L,1940x01010004L, 0x00010404L, 0x00000004L, 0x00010000L,1950x00000400L, 0x01010400L, 0x01010404L, 0x00000400L,1960x01000404L, 0x01010004L, 0x01000000L, 0x00000004L,1970x00000404L, 0x01000400L, 0x01000400L, 0x00010400L,1980x00010400L, 0x01010000L, 0x01010000L, 0x01000404L,1990x00010004L, 0x01000004L, 0x01000004L, 0x00010004L,2000x00000000L, 0x00000404L, 0x00010404L, 0x01000000L,2010x00010000L, 0x01010404L, 0x00000004L, 0x01010000L,2020x01010400L, 0x01000000L, 0x01000000L, 0x00000400L,2030x01010004L, 0x00010000L, 0x00010400L, 0x01000004L,2040x00000400L, 0x00000004L, 0x01000404L, 0x00010404L,2050x01010404L, 0x00010004L, 0x01010000L, 0x01000404L,2060x01000004L, 0x00000404L, 0x00010404L, 0x01010400L,2070x00000404L, 0x01000400L, 0x01000400L, 0x00000000L,2080x00010004L, 0x00010400L, 0x00000000L, 0x01010004L };209210static unsigned long SP2[64] = {2110x80108020L, 0x80008000L, 0x00008000L, 0x00108020L,2120x00100000L, 0x00000020L, 0x80100020L, 0x80008020L,2130x80000020L, 0x80108020L, 0x80108000L, 0x80000000L,2140x80008000L, 0x00100000L, 0x00000020L, 0x80100020L,2150x00108000L, 0x00100020L, 0x80008020L, 0x00000000L,2160x80000000L, 0x00008000L, 0x00108020L, 0x80100000L,2170x00100020L, 0x80000020L, 0x00000000L, 0x00108000L,2180x00008020L, 0x80108000L, 0x80100000L, 0x00008020L,2190x00000000L, 0x00108020L, 0x80100020L, 0x00100000L,2200x80008020L, 0x80100000L, 0x80108000L, 0x00008000L,2210x80100000L, 0x80008000L, 0x00000020L, 0x80108020L,2220x00108020L, 0x00000020L, 0x00008000L, 0x80000000L,2230x00008020L, 0x80108000L, 0x00100000L, 0x80000020L,2240x00100020L, 0x80008020L, 0x80000020L, 0x00100020L,2250x00108000L, 0x00000000L, 0x80008000L, 0x00008020L,2260x80000000L, 0x80100020L, 0x80108020L, 0x00108000L };227228static unsigned long SP3[64] = {2290x00000208L, 0x08020200L, 0x00000000L, 0x08020008L,2300x08000200L, 0x00000000L, 0x00020208L, 0x08000200L,2310x00020008L, 0x08000008L, 0x08000008L, 0x00020000L,2320x08020208L, 0x00020008L, 0x08020000L, 0x00000208L,2330x08000000L, 0x00000008L, 0x08020200L, 0x00000200L,2340x00020200L, 0x08020000L, 0x08020008L, 0x00020208L,2350x08000208L, 0x00020200L, 0x00020000L, 0x08000208L,2360x00000008L, 0x08020208L, 0x00000200L, 0x08000000L,2370x08020200L, 0x08000000L, 0x00020008L, 0x00000208L,2380x00020000L, 0x08020200L, 0x08000200L, 0x00000000L,2390x00000200L, 0x00020008L, 0x08020208L, 0x08000200L,2400x08000008L, 0x00000200L, 0x00000000L, 0x08020008L,2410x08000208L, 0x00020000L, 0x08000000L, 0x08020208L,2420x00000008L, 0x00020208L, 0x00020200L, 0x08000008L,2430x08020000L, 0x08000208L, 0x00000208L, 0x08020000L,2440x00020208L, 0x00000008L, 0x08020008L, 0x00020200L };245246static unsigned long SP4[64] = {2470x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,2480x00802080L, 0x00800081L, 0x00800001L, 0x00002001L,2490x00000000L, 0x00802000L, 0x00802000L, 0x00802081L,2500x00000081L, 0x00000000L, 0x00800080L, 0x00800001L,2510x00000001L, 0x00002000L, 0x00800000L, 0x00802001L,2520x00000080L, 0x00800000L, 0x00002001L, 0x00002080L,2530x00800081L, 0x00000001L, 0x00002080L, 0x00800080L,2540x00002000L, 0x00802080L, 0x00802081L, 0x00000081L,2550x00800080L, 0x00800001L, 0x00802000L, 0x00802081L,2560x00000081L, 0x00000000L, 0x00000000L, 0x00802000L,2570x00002080L, 0x00800080L, 0x00800081L, 0x00000001L,2580x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,2590x00802081L, 0x00000081L, 0x00000001L, 0x00002000L,2600x00800001L, 0x00002001L, 0x00802080L, 0x00800081L,2610x00002001L, 0x00002080L, 0x00800000L, 0x00802001L,2620x00000080L, 0x00800000L, 0x00002000L, 0x00802080L };263264static unsigned long SP5[64] = {2650x00000100L, 0x02080100L, 0x02080000L, 0x42000100L,2660x00080000L, 0x00000100L, 0x40000000L, 0x02080000L,2670x40080100L, 0x00080000L, 0x02000100L, 0x40080100L,2680x42000100L, 0x42080000L, 0x00080100L, 0x40000000L,2690x02000000L, 0x40080000L, 0x40080000L, 0x00000000L,2700x40000100L, 0x42080100L, 0x42080100L, 0x02000100L,2710x42080000L, 0x40000100L, 0x00000000L, 0x42000000L,2720x02080100L, 0x02000000L, 0x42000000L, 0x00080100L,2730x00080000L, 0x42000100L, 0x00000100L, 0x02000000L,2740x40000000L, 0x02080000L, 0x42000100L, 0x40080100L,2750x02000100L, 0x40000000L, 0x42080000L, 0x02080100L,2760x40080100L, 0x00000100L, 0x02000000L, 0x42080000L,2770x42080100L, 0x00080100L, 0x42000000L, 0x42080100L,2780x02080000L, 0x00000000L, 0x40080000L, 0x42000000L,2790x00080100L, 0x02000100L, 0x40000100L, 0x00080000L,2800x00000000L, 0x40080000L, 0x02080100L, 0x40000100L };281282static unsigned long SP6[64] = {2830x20000010L, 0x20400000L, 0x00004000L, 0x20404010L,2840x20400000L, 0x00000010L, 0x20404010L, 0x00400000L,2850x20004000L, 0x00404010L, 0x00400000L, 0x20000010L,2860x00400010L, 0x20004000L, 0x20000000L, 0x00004010L,2870x00000000L, 0x00400010L, 0x20004010L, 0x00004000L,2880x00404000L, 0x20004010L, 0x00000010L, 0x20400010L,2890x20400010L, 0x00000000L, 0x00404010L, 0x20404000L,2900x00004010L, 0x00404000L, 0x20404000L, 0x20000000L,2910x20004000L, 0x00000010L, 0x20400010L, 0x00404000L,2920x20404010L, 0x00400000L, 0x00004010L, 0x20000010L,2930x00400000L, 0x20004000L, 0x20000000L, 0x00004010L,2940x20000010L, 0x20404010L, 0x00404000L, 0x20400000L,2950x00404010L, 0x20404000L, 0x00000000L, 0x20400010L,2960x00000010L, 0x00004000L, 0x20400000L, 0x00404010L,2970x00004000L, 0x00400010L, 0x20004010L, 0x00000000L,2980x20404000L, 0x20000000L, 0x00400010L, 0x20004010L };299300static unsigned long SP7[64] = {3010x00200000L, 0x04200002L, 0x04000802L, 0x00000000L,3020x00000800L, 0x04000802L, 0x00200802L, 0x04200800L,3030x04200802L, 0x00200000L, 0x00000000L, 0x04000002L,3040x00000002L, 0x04000000L, 0x04200002L, 0x00000802L,3050x04000800L, 0x00200802L, 0x00200002L, 0x04000800L,3060x04000002L, 0x04200000L, 0x04200800L, 0x00200002L,3070x04200000L, 0x00000800L, 0x00000802L, 0x04200802L,3080x00200800L, 0x00000002L, 0x04000000L, 0x00200800L,3090x04000000L, 0x00200800L, 0x00200000L, 0x04000802L,3100x04000802L, 0x04200002L, 0x04200002L, 0x00000002L,3110x00200002L, 0x04000000L, 0x04000800L, 0x00200000L,3120x04200800L, 0x00000802L, 0x00200802L, 0x04200800L,3130x00000802L, 0x04000002L, 0x04200802L, 0x04200000L,3140x00200800L, 0x00000000L, 0x00000002L, 0x04200802L,3150x00000000L, 0x00200802L, 0x04200000L, 0x00000800L,3160x04000002L, 0x04000800L, 0x00000800L, 0x00200002L };317318static unsigned long SP8[64] = {3190x10001040L, 0x00001000L, 0x00040000L, 0x10041040L,3200x10000000L, 0x10001040L, 0x00000040L, 0x10000000L,3210x00040040L, 0x10040000L, 0x10041040L, 0x00041000L,3220x10041000L, 0x00041040L, 0x00001000L, 0x00000040L,3230x10040000L, 0x10000040L, 0x10001000L, 0x00001040L,3240x00041000L, 0x00040040L, 0x10040040L, 0x10041000L,3250x00001040L, 0x00000000L, 0x00000000L, 0x10040040L,3260x10000040L, 0x10001000L, 0x00041040L, 0x00040000L,3270x00041040L, 0x00040000L, 0x10041000L, 0x00001000L,3280x00000040L, 0x10040040L, 0x00001000L, 0x00041040L,3290x10001000L, 0x00000040L, 0x10000040L, 0x10040000L,3300x10040040L, 0x10000000L, 0x00040000L, 0x10001040L,3310x00000000L, 0x10041040L, 0x00040040L, 0x10000040L,3320x10040000L, 0x10001000L, 0x10001040L, 0x00000000L,3330x10041040L, 0x00041000L, 0x00041000L, 0x00001040L,3340x00001040L, 0x00040040L, 0x10000000L, 0x10041000L };335336static void desfunc(block, keys)337register unsigned long *block, *keys;338{339register unsigned long fval, work, right, leftt;340register int round;341342leftt = block[0];343right = block[1];344work = ((leftt >> 4) ^ right) & 0x0f0f0f0fL;345right ^= work;346leftt ^= (work << 4);347work = ((leftt >> 16) ^ right) & 0x0000ffffL;348right ^= work;349leftt ^= (work << 16);350work = ((right >> 2) ^ leftt) & 0x33333333L;351leftt ^= work;352right ^= (work << 2);353work = ((right >> 8) ^ leftt) & 0x00ff00ffL;354leftt ^= work;355right ^= (work << 8);356right = ((right << 1) | ((right >> 31) & 1L)) & 0xffffffffL;357work = (leftt ^ right) & 0xaaaaaaaaL;358leftt ^= work;359right ^= work;360leftt = ((leftt << 1) | ((leftt >> 31) & 1L)) & 0xffffffffL;361362for( round = 0; round < 8; round++ ) {363work = (right << 28) | (right >> 4);364work ^= *keys++;365fval = SP7[ work & 0x3fL];366fval |= SP5[(work >> 8) & 0x3fL];367fval |= SP3[(work >> 16) & 0x3fL];368fval |= SP1[(work >> 24) & 0x3fL];369work = right ^ *keys++;370fval |= SP8[ work & 0x3fL];371fval |= SP6[(work >> 8) & 0x3fL];372fval |= SP4[(work >> 16) & 0x3fL];373fval |= SP2[(work >> 24) & 0x3fL];374leftt ^= fval;375work = (leftt << 28) | (leftt >> 4);376work ^= *keys++;377fval = SP7[ work & 0x3fL];378fval |= SP5[(work >> 8) & 0x3fL];379fval |= SP3[(work >> 16) & 0x3fL];380fval |= SP1[(work >> 24) & 0x3fL];381work = leftt ^ *keys++;382fval |= SP8[ work & 0x3fL];383fval |= SP6[(work >> 8) & 0x3fL];384fval |= SP4[(work >> 16) & 0x3fL];385fval |= SP2[(work >> 24) & 0x3fL];386right ^= fval;387}388389right = (right << 31) | (right >> 1);390work = (leftt ^ right) & 0xaaaaaaaaL;391leftt ^= work;392right ^= work;393leftt = (leftt << 31) | (leftt >> 1);394work = ((leftt >> 8) ^ right) & 0x00ff00ffL;395right ^= work;396leftt ^= (work << 8);397work = ((leftt >> 2) ^ right) & 0x33333333L;398right ^= work;399leftt ^= (work << 2);400work = ((right >> 16) ^ leftt) & 0x0000ffffL;401leftt ^= work;402right ^= (work << 16);403work = ((right >> 4) ^ leftt) & 0x0f0f0f0fL;404leftt ^= work;405right ^= (work << 4);406*block++ = right;407*block = leftt;408return;409}410411/* Validation sets:412*413* Single-length key, single-length plaintext -414* Key : 0123 4567 89ab cdef415* Plain : 0123 4567 89ab cde7416* Cipher : c957 4425 6a5e d31d417*418* Double-length key, single-length plaintext -419* Key : 0123 4567 89ab cdef fedc ba98 7654 3210420* Plain : 0123 4567 89ab cde7421* Cipher : 7f1d 0a77 826b 8aff422*423* Double-length key, double-length plaintext -424* Key : 0123 4567 89ab cdef fedc ba98 7654 3210425* Plain : 0123 4567 89ab cdef 0123 4567 89ab cdff426* Cipher : 27a0 8440 406a df60 278f 47cf 42d6 15d7427*428* Triple-length key, single-length plaintext -429* Key : 0123 4567 89ab cdef fedc ba98 7654 3210 89ab cdef 0123 4567430* Plain : 0123 4567 89ab cde7431* Cipher : de0b 7c06 ae5e 0ed5432*433* Triple-length key, double-length plaintext -434* Key : 0123 4567 89ab cdef fedc ba98 7654 3210 89ab cdef 0123 4567435* Plain : 0123 4567 89ab cdef 0123 4567 89ab cdff436* Cipher : ad0d 1b30 ac17 cf07 0ed1 1c63 81e4 4de5437*438* d3des V5.0a rwo 9208.07 18:44 Graven Imagery439**********************************************************************/440441442