risto-des.shar
资源名称:des.zip [点击查看]
上传用户:zhh515
上传日期:2007-01-06
资源大小:966k
文件大小:25k
源码类别:
加密解密
开发平台:
C/C++
- #! /bin/sh
- # This is a shell archive. Remove anything before this line, then unpack
- # it by saving it into a file and typing "sh file". To overwrite existing
- # files, type "sh file -c". You can also feed this as standard input via
- # unshar, or by typing "sh <file", e.g.. If this archive is complete, you
- # will see the following message at the end:
- # "End of shell archive."
- # Contents: README.mdes des_test_data destest.c mdes.c mdes.h
- # Wrapped by paasivir@tukki on Thu Aug 11 09:33:21 1994
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f README.mdes -a "${1}" != "-c" ; then
- echo shar: Will not over-write existing file "README.mdes"
- else
- echo shar: Extracting "README.mdes" (3025 characters)
- sed "s/^X//" >README.mdes <<'END_OF_README.mdes'
- X
- Xmdes.c, mdes.h, destest, des_test_data
- X
- X Public Domain, NO WARRANRY.
- X
- X This is a trivial DES implementation I wrote to figure out
- X the algorithm. It's very slow. There are many faster free
- X implementations around.
- X
- X In USA this code may or may not be subject of export regulations,
- X etc.
- X
- XVersion
- X
- X 1.0, first and probably last if des_cbc_encrypt (untested) works.
- X
- XAuthor
- X Risto Paasivirta, paasivir@jyu.fi
- X
- X=============================================================================
- X
- Xmdes.h
- X
- X #define DES_ENCRYPT 1
- X #define DES_DECRYPT 0
- X
- X Des encryption flag. Encrypt if DES_ENCRYPT, decrypt if DES_DECRYPT.
- X (Doesn't sound too complex, does it.)
- X
- X typedef struct des_key_schedule {
- X unsigned char b[16][48];
- X } des_key_schedule;
- X
- X Key schedule type. Key bits are left unpacked for 'speed'.
- X
- X
- X typedef struct des_cblock {
- X unsigned char _[8];
- X } des_cblock;
- X
- X Des character block type. Any 8 char block should work fine.
- X
- Xmdes.c
- X
- X Des functions
- X
- X void des_set_odd_parity(des_cblock *key);
- X
- X Set parity of 56-bit (7bits x 8 bytes) key block. Parity bit is
- X the least significant bit in each byte. This implementation works
- X with incorrect parity key blocks.
- X
- X int des_set_key(des_cblock *k, des_key_schedule *key);
- X
- X Initialize key schedule from key block k. If key checking would be
- X implemented this probably should return some value.
- X
- X int des_ecb_encrypt(des_cblock *input, des_cblock *output,
- X des_key_schedule *schedule, int encrypt);
- X
- X Des encryption in des electronic code book mode. En/decrypt one
- X cblock from input to output using key shedule. Returns number
- X of bytes written to output (8.)
- X
- X int des_cbc_encrypt(des_cblock *input, des_cblock *output,
- X int len, des_key_schedule *key,
- X des_cblock *ivec, int mode);
- X
- X Des encryption in cipher block chaining mode. En/decrypt len bytes
- X from input to output using scheduled key and initial ivec. If len is
- X not multiple of 8, last block is padded with NULL bytes. Output is
- X written always multiple of 8 bytes. Returns number of bytes written
- X to output and final ivec in ivec.
- X
- Xdestest.c
- Xdes_test_data
- X
- X Destest reads file des_test_data, converts hex key/plain/cipher
- X cblock to binary and checks that en/decryption works in elecrtoric
- X code book mode. File des_test_data has been posted few times to
- X sci.crypt, only thing I have done to it was that I corrected one
- X key parity error from line 143:
- X original
- X 3107911598090140 0000000000000000 406A9A6AB43399AE
- X changed to:
- X 3107911598080140 0000000000000000 406A9A6AB43399AE
- X
- X(eof)
- END_OF_README.mdes
- if test 3025 -ne `wc -c <README.mdes`; then
- echo shar: "README.mdes" unpacked with wrong size!
- fi
- # end of overwriting check
- fi
- if test -f des_test_data -a "${1}" != "-c" ; then
- echo shar: Will not over-write existing file "des_test_data"
- else
- echo shar: Extracting "des_test_data" (8854 characters)
- sed "s/^X//" >des_test_data <<'END_OF_des_test_data'
- X
- XKey Plain Cipher
- X
- X/* IP and E Test */
- X
- X0101010101010101 95F8A5E5DD31D900 8000000000000000
- X0101010101010101 DD7F121CA5015619 4000000000000000
- X0101010101010101 2E8653104F3834EA 2000000000000000
- X0101010101010101 4BD388FF6CD81D4F 1000000000000000
- X0101010101010101 20B9E767B2FB1456 0800000000000000
- X0101010101010101 55579380D77138EF 0400000000000000
- X0101010101010101 6CC5DEFAAF04512F 0200000000000000
- X0101010101010101 0D9F279BA5D87260 0100000000000000
- X0101010101010101 D9031B0271BD5A0A 0080000000000000
- X0101010101010101 424250B37C3DD951 0040000000000000
- X0101010101010101 B8061B7ECD9A21E5 0020000000000000
- X0101010101010101 F15D0F286B65BD28 0010000000000000
- X0101010101010101 ADD0CC8D6E5DEBA1 0008000000000000
- X0101010101010101 E6D5F82752AD63D1 0004000000000000
- X0101010101010101 ECBFE3BD3F591A5E 0002000000000000
- X0101010101010101 F356834379D165CD 0001000000000000
- X0101010101010101 2B9F982F20037FA9 0000800000000000
- X0101010101010101 889DE068A16F0BE6 0000400000000000
- X0101010101010101 E19E275D846A1298 0000200000000000
- X0101010101010101 329A8ED523D71AEC 0000100000000000
- X0101010101010101 E7FCE22557D23C97 0000080000000000
- X0101010101010101 12A9F5817FF2D65D 0000040000000000
- X0101010101010101 A484C3AD38DC9C19 0000020000000000
- X0101010101010101 FBE00A8A1EF8AD72 0000010000000000
- X0101010101010101 750D079407521363 0000008000000000
- X0101010101010101 64FEED9C724C2FAF 0000004000000000
- X0101010101010101 F02B263B328E2B60 0000002000000000
- X0101010101010101 9D64555A9A10B852 0000001000000000
- X0101010101010101 D106FF0BED5255D7 0000000800000000
- X0101010101010101 E1652C6B138C64A5 0000000400000000
- X0101010101010101 E428581186EC8F46 0000000200000000
- X0101010101010101 AEB5F5EDE22D1A36 0000000100000000
- X0101010101010101 E943D7568AEC0C5C 0000000080000000
- X0101010101010101 DF98C8276F54B04B 0000000040000000
- X0101010101010101 B160E4680F6C696F 0000000020000000
- X0101010101010101 FA0752B07D9C4AB8 0000000010000000
- X0101010101010101 CA3A2B036DBC8502 0000000008000000
- X0101010101010101 5E0905517BB59BCF 0000000004000000
- X0101010101010101 814EEB3B91D90726 0000000002000000
- X0101010101010101 4D49DB1532919C9F 0000000001000000
- X0101010101010101 25EB5FC3F8CF0621 0000000000800000
- X0101010101010101 AB6A20C0620D1C6F 0000000000400000
- X0101010101010101 79E90DBC98F92CCA 0000000000200000
- X0101010101010101 866ECEDD8072BB0E 0000000000100000
- X0101010101010101 8B54536F2F3E64A8 0000000000080000
- X0101010101010101 EA51D3975595B86B 0000000000040000
- X0101010101010101 CAFFC6AC4542DE31 0000000000020000
- X0101010101010101 8DD45A2DDF90796C 0000000000010000
- X0101010101010101 1029D55E880EC2D0 0000000000008000
- X0101010101010101 5D86CB23639DBEA9 0000000000004000
- X0101010101010101 1D1CA853AE7C0C5F 0000000000002000
- X0101010101010101 CE332329248F3228 0000000000001000
- X0101010101010101 8405D1ABE24FB942 0000000000000800
- X0101010101010101 E643D78090CA4207 0000000000000400
- X0101010101010101 48221B9937748A23 0000000000000200
- X0101010101010101 DD7C0BBD61FAFD54 0000000000000100
- X0101010101010101 2FBC291A570DB5C4 0000000000000080
- X0101010101010101 E07C30D7E4E26E12 0000000000000040
- X0101010101010101 0953E2258E8E90A1 0000000000000020
- X0101010101010101 5B711BC4CEEBF2EE 0000000000000010
- X0101010101010101 CC083F1E6D9E85F6 0000000000000008
- X0101010101010101 D2FD8867D50D2DFE 0000000000000004
- X0101010101010101 06E7EA22CE92708F 0000000000000002
- X0101010101010101 166B40B44ABA4BD6 0000000000000001
- X
- X/* PC1 and PC2 test */
- X
- X8001010101010101 0000000000000000 95A8D72813DAA94D
- X4001010101010101 0000000000000000 0EEC1487DD8C26D5
- X2001010101010101 0000000000000000 7AD16FFB79C45926
- X1001010101010101 0000000000000000 D3746294CA6A6CF3
- X0801010101010101 0000000000000000 809F5F873C1FD761
- X0401010101010101 0000000000000000 C02FAFFEC989D1FC
- X0201010101010101 0000000000000000 4615AA1D33E72F10
- X0180010101010101 0000000000000000 2055123350C00858
- X0140010101010101 0000000000000000 DF3B99D6577397C8
- X0120010101010101 0000000000000000 31FE17369B5288C9
- X0110010101010101 0000000000000000 DFDD3CC64DAE1642
- X0108010101010101 0000000000000000 178C83CE2B399D94
- X0104010101010101 0000000000000000 50F636324A9B7F80
- X0102010101010101 0000000000000000 A8468EE3BC18F06D
- X0101800101010101 0000000000000000 A2DC9E92FD3CDE92
- X0101400101010101 0000000000000000 CAC09F797D031287
- X0101200101010101 0000000000000000 90BA680B22AEB525
- X0101100101010101 0000000000000000 CE7A24F350E280B6
- X0101080101010101 0000000000000000 882BFF0AA01A0B87
- X0101040101010101 0000000000000000 25610288924511C2
- X0101020101010101 0000000000000000 C71516C29C75D170
- X0101018001010101 0000000000000000 5199C29A52C9F059
- X0101014001010101 0000000000000000 C22F0A294A71F29F
- X0101012001010101 0000000000000000 EE371483714C02EA
- X0101011001010101 0000000000000000 A81FBD448F9E522F
- X0101010801010101 0000000000000000 4F644C92E192DFED
- X0101010401010101 0000000000000000 1AFA9A66A6DF92AE
- X0101010201010101 0000000000000000 B3C1CC715CB879D8
- X0101010180010101 0000000000000000 19D032E64AB0BD8B
- X0101010140010101 0000000000000000 3CFAA7A7DC8720DC
- X0101010120010101 0000000000000000 B7265F7F447AC6F3
- X0101010110010101 0000000000000000 9DB73B3C0D163F54
- X0101010108010101 0000000000000000 8181B65BABF4A975
- X0101010104010101 0000000000000000 93C9B64042EAA240
- X0101010102010101 0000000000000000 5570530829705592
- X0101010101800101 0000000000000000 8638809E878787A0
- X0101010101400101 0000000000000000 41B9A79AF79AC208
- X0101010101200101 0000000000000000 7A9BE42F2009A892
- X0101010101100101 0000000000000000 29038D56BA6D2745
- X0101010101080101 0000000000000000 5495C6ABF1E5DF51
- X0101010101040101 0000000000000000 AE13DBD561488933
- X0101010101020101 0000000000000000 024D1FFA8904E389
- X0101010101018001 0000000000000000 D1399712F99BF02E
- X0101010101014001 0000000000000000 14C1D7C1CFFEC79E
- X0101010101012001 0000000000000000 1DE5279DAE3BED6F
- X0101010101011001 0000000000000000 E941A33F85501303
- X0101010101010801 0000000000000000 DA99DBBC9A03F379
- X0101010101010401 0000000000000000 B7FC92F91D8E92E9
- X0101010101010201 0000000000000000 AE8E5CAA3CA04E85
- X0101010101010180 0000000000000000 9CC62DF43B6EED74
- X0101010101010140 0000000000000000 D863DBB5C59A91A0
- X0101010101010120 0000000000000000 A1AB2190545B91D7
- X0101010101010110 0000000000000000 0875041E64C570F7
- X0101010101010108 0000000000000000 5A594528BEBEF1CC
- X0101010101010104 0000000000000000 FCDB3291DE21F0C0
- X0101010101010102 0000000000000000 869EFD7F9F265A09
- X
- X/* P Test */
- X
- X1046913489980131 0000000000000000 88D55E54F54C97B4
- X1007103489988020 0000000000000000 0C0CC00C83EA48FD
- X10071034C8980120 0000000000000000 83BC8EF3A6570183
- X1046103489988020 0000000000000000 DF725DCAD94EA2E9
- X1086911519190101 0000000000000000 E652B53B550BE8B0
- X1086911519580101 0000000000000000 AF527120C485CBB0
- X5107B01519580101 0000000000000000 0F04CE393DB926D5
- X1007B01519190101 0000000000000000 C9F00FFC74079067
- X3107915498080101 0000000000000000 7CFD82A593252B4E
- X3107919498080101 0000000000000000 CB49A2F9E91363E3
- X10079115B9080140 0000000000000000 00B588BE70D23F56
- X3107911598090140 0000000000000000 406A9A6AB43399AE
- X1007D01589980101 0000000000000000 6CB773611DCA9ADA
- X9107911589980101 0000000000000000 67FD21C17DBB5D70
- X9107D01589190101 0000000000000000 9592CB4110430787
- X1007D01598980120 0000000000000000 A6B7FF68A318DDD3
- X1007940498190101 0000000000000000 4D102196C914CA16
- X0107910491190401 0000000000000000 2DFA9F4573594965
- X0107910491190101 0000000000000000 B46604816C0E0774
- X0107940491190401 0000000000000000 6E7E6221A4F34E87
- X19079210981A0101 0000000000000000 AA85E74643233199
- X1007911998190801 0000000000000000 2E5A19DB4D1962D6
- X10079119981A0801 0000000000000000 23A866A809D30894
- X1007921098190101 0000000000000000 D812D961F017D320
- X100791159819010B 0000000000000000 055605816E58608F
- X1004801598190101 0000000000000000 ABD88E8B1B7716F1
- X1004801598190102 0000000000000000 537AC95BE69DA1E1
- X1004801598190108 0000000000000000 AED0F6AE3C25CDD8
- X1002911598100104 0000000000000000 B3E35A5EE53E7B8D
- X1002911598190104 0000000000000000 61C79C71921A2EF8
- X1002911598100201 0000000000000000 E2F5728F0995013C
- X1002911698100101 0000000000000000 1AEAC39A61F0A464
- X
- X/* S-box test */
- X
- X7CA110454A1A6E57 01A1D6D039776742 690F5B0D9A26939B
- X0131D9619DC1376E 5CD54CA83DEF57DA 7A389D10354BD271
- X07A1133E4A0B2686 0248D43806F67172 868EBB51CAB4599A
- X3849674C2602319E 51454B582DDF440A 7178876E01F19B2A
- X04B915BA43FEB5B6 42FD443059577FA2 AF37FB421F8C4095
- X0113B970FD34F2CE 059B5E0851CF143A 86A560F10EC6D85B
- X0170F175468FB5E6 0756D8E0774761D2 0CD3DA020021DC09
- X43297FAD38E373FE 762514B829BF486A EA676B2CB7DB2B7A
- X07A7137045DA2A16 3BDD119049372802 DFD64A815CAF1A0F
- X04689104C2FD3B2F 26955F6835AF609A 5C513C9C4886C088
- X37D06BB516CB7546 164D5E404F275232 0A2AEEAE3FF4AB77
- X1F08260D1AC2465E 6B056E18759F5CCA EF1BF03E5DFA575A
- X584023641ABA6176 004BD6EF09176062 88BF0DB6D70DEE56
- X025816164629B007 480D39006EE762F2 A1F9915541020B56
- X49793EBC79B3258F 437540C8698F3CFA 6FBF1CAFCFFD0556
- X4FB05E1515AB73A7 072D43A077075292 2F22E49BAB7CA1AC
- X49E95D6D4CA229BF 02FE55778117F12A 5A6B612CC26CCE4A
- X018310DC409B26D6 1D9D5C5018F728C2 5F4C038ED12B2E41
- X1C587F1C13924FEF 305532286D6F295A 63FAC0D034D9F793
- X
- END_OF_des_test_data
- if test 8854 -ne `wc -c <des_test_data`; then
- echo shar: "des_test_data" unpacked with wrong size!
- fi
- # end of overwriting check
- fi
- if test -f destest.c -a "${1}" != "-c" ; then
- echo shar: Will not over-write existing file "destest.c"
- else
- echo shar: Extracting "destest.c" (2103 characters)
- sed "s/^X//" >destest.c <<'END_OF_destest.c'
- X/*---------------------------------------------------------------------------
- X * destest.c
- X *
- X *
- X * 27-Feb-92 RP
- X * 06-Jan-93 RP (for libdes)
- X */
- X
- X#include <stdio.h>
- X#include <stdlib.h>
- X#include <string.h>
- X
- X#include "mdes.h"
- X
- X#define ctod(d) ((d) <= '9' ? (d) - '0' : (d) <= 'Z' ? (d) - ('A'-10):
- X (d) - ('a' - 10))
- X
- Xint des_hex_to_cblock(des_cblock *b, char *c)
- X{
- X int i,n,m;
- X for(i = 0; i < 8; i++) {
- X if((n=ctod(c[i*2])) < 0 || n > 15) return 0;
- X if((m=ctod(c[i*2+1])) < 0 || m > 15) return 0;
- X ((char *)b)[i] = (n<<4)|m;
- X }
- X return 1;
- X}
- X
- Xvoid des_print_cblock(des_cblock *b)
- X{
- X int i;
- X for(i = 0; i < 8; i++) printf("%02X",((unsigned char *)b)[i]);
- X}
- X
- Xchar linebuf[512];
- X
- Xstruct Library *ACryptBase=0;
- X
- X
- Xvoid main(int argc,char **argv)
- X{
- X FILE *testdata;
- X des_cblock k,p,c,er,dr;
- X des_key_schedule ks;
- X int line=0,test=0,ok=1;
- X char *fname;
- X
- X if(argc<2) fname = "des_test_data";
- X else fname = argv[1];
- X
- X if(!(testdata = fopen(fname,"r"))) {
- X printf("destest: can't open file %sn",fname);
- X exit(5);
- X }
- X
- X while(fgets(linebuf,512,testdata))
- X {
- X line++;
- X if(des_hex_to_cblock(&k,linebuf) &&
- X des_hex_to_cblock(&p,linebuf+17) &&
- X des_hex_to_cblock(&c,linebuf+34)) {
- X test++;
- X des_set_key(&k,&ks);
- X des_ecb_encrypt(&p,&er,&ks,DES_ENCRYPT);
- X des_ecb_encrypt(&c,&dr,&ks,DES_DECRYPT);
- X if(memcmp(&er,&c,sizeof(des_cblock))||
- X memcmp(&dr,&p,sizeof(des_cblock))) {
- X printf("destest failed '%s' line %d, test %dn",
- X fname,line,test);
- X printf("key= ");
- X des_print_cblock(&k);
- X printf("n plain= ");
- X des_print_cblock(&p);
- X printf("n chipher= ");
- X des_print_cblock(&c);
- X printf("nenc= ");
- X des_print_cblock(&er);
- X printf("ndec= ");
- X des_print_cblock(&er);
- X printf("n");
- X ok = 0;
- X break;
- X }
- X } else {
- X fputs(linebuf, stdout);
- X fflush(stdout);
- X }
- X }
- X printf("%s test with file %sn", test?(ok?"successful":"failed"):
- X "nothing to",fname);
- X exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);
- X}
- X
- X
- END_OF_destest.c
- if test 2103 -ne `wc -c <destest.c`; then
- echo shar: "destest.c" unpacked with wrong size!
- fi
- # end of overwriting check
- fi
- if test -f mdes.c -a "${1}" != "-c" ; then
- echo shar: Will not over-write existing file "mdes.c"
- else
- echo shar: Extracting "mdes.c" (7722 characters)
- sed "s/^X//" >mdes.c <<'END_OF_mdes.c'
- X/* mdes.c -- minimal des implementation (and very slow, too)
- X * Public Domain, 1993 by Risto Paasivirta, paasivir@jyu.fi
- X * No warranty, try with your own risk.
- X */
- X
- X#include <string.h>
- X#include "mdes.h"
- X
- X/*
- X * left shift table for key schedule LS
- X */
- X
- Xstatic const unsigned char LS[16] = {
- X 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
- X};
- X
- X/*
- X * permuted-choice tables for key schedule PC1, PC2
- X */
- X
- Xstatic const unsigned char PC1[56] = {
- X 57,49,41,33,25,17, 9, 1,58,50,42,34,26,18,10, 2,
- X 59,51,43,35,27,19,11, 3,60,52,44,36,63,55,47,39,
- X 31,23,15, 7,62,54,46,38,30,22,14, 6,61,53,45,37,
- X 29,21,13, 5,28,20,12, 4
- X};
- X
- Xstatic const unsigned char PC2[48] = {
- X 14,17,11,24, 1, 5, 3,28,15, 6,21,10,23,19,12, 4,
- X 26, 8,16, 7,27,20,13, 2,41,52,31,37,47,55,30,40,
- X 51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32
- X};
- X
- X/*
- X * initial permutation IP
- X */
- X
- Xstatic const unsigned char IP[64] = {
- X 58,50,42,34,26,18,10, 2,60,52,44,36,28,20,12, 4,
- X 62,54,46,38,30,22,14, 6,64,56,48,40,32,24,16, 8,
- X 57,49,41,33,25,17, 9, 1,59,51,43,35,27,19,11, 3,
- X 61,53,45,37,29,21,13, 5,63,55,47,39,31,23,15, 7
- X};
- X
- X/*
- X * bit-selection table E
- X */
- X
- Xstatic const unsigned char E[48] = {
- X 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9,10,11,
- X 12,13,12,13,14,15,16,17,16,17,18,19,20,21,20,21,
- X 22,23,24,25,24,25,26,27,28,29,28,29,30,31,32, 1
- X};
- X
- X/*
- X * substitution tables ST
- X */
- X
- Xstatic const unsigned char ST[8][64] = {
- X 14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,
- X 0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,
- X 4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,
- X 15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13,
- X
- X 15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,
- X 3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,
- X 0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,
- X 13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9,
- X
- X 10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,
- X 13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,
- X 13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,
- X 1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12,
- X
- X 7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,
- X 13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,
- X 10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,
- X 3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14,
- X
- X 2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,
- X 14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,
- X 4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,
- X 11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3,
- X
- X 12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,
- X 10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8,
- X 9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,
- X 4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13,
- X
- X 4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,
- X 13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6,
- X 1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,
- X 6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12,
- X
- X 13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,
- X 1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,
- X 7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,
- X 2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11
- X};
- X
- X/*
- X * permutation table P
- X */
- X
- Xstatic const unsigned char P[32] = {
- X 16, 7,20,21,29,12,28,17, 1,15,23,26, 5,18,31,10,
- X 2, 8,24,14,32,27, 3, 9,19,13,30, 6,22,11, 4,25
- X};
- X
- X/*
- X * final permutation FP
- X */
- X
- Xstatic const unsigned char FP[64] = {
- X 40, 8,48,16,56,24,64,32,39, 7,47,15,55,23,63,31,
- X 38, 6,46,14,54,22,62,30,37, 5,45,13,53,21,61,29,
- X 36, 4,44,12,52,20,60,28,35, 3,43,11,51,19,59,27,
- X 34, 2,42,10,50,18,58,26,33, 1,41, 9,49,17,57,25
- X};
- X
- X/*
- X * unbit(to, from, len) -- unpack len bytes to bits
- X */
- X
- Xstatic void
- Xunbit(unsigned char *to,unsigned char *from,int len)
- X{
- X int j, f;
- X
- X while (len--) {
- X for (j = 8, f = *from++; j--;)
- X *to++ = (f >> j) & 1;
- X }
- X}
- X
- X/*
- X * pkbit(to from, len) -- pack bits to len bytes
- X */
- X
- Xstatic void
- Xpkbit(unsigned char *to, unsigned char *from, int len)
- X{
- X int j,t;
- X
- X while (len--) {
- X for (j = 8, t = 0; j--;)
- X t |= *from++ << j;
- X *to++ = t;
- X }
- X}
- X
- X/*
- X * perm(to, OP, from, bits) -- do permutation
- X */
- X
- Xstatic void
- Xperm(unsigned char *to, const unsigned char *op,
- X unsigned char *from, int bits)
- X{
- X int i;
- X
- X for (i = 0; i < bits; i++)
- X to[i] = from[op[i]-1];
- X}
- X
- X/*
- X * rot(to, from, len, bits) -- rotate left
- X */
- X
- Xstatic void
- Xrot(unsigned char *r, int len, int bits)
- X{
- X int i,t;
- X
- X if (len > 1) {
- X for (i = 0; i<bits; i++) {
- X t = r[0];
- X memmove(&r[0], &r[1], len - 1);
- X r[len-1] = t;
- X }
- X }
- X}
- X
- X/*
- X * add2(to, from, bits) -- add bits modulo 2 (that is, eor)
- X */
- X
- Xstatic void
- Xadd2(unsigned char *to, unsigned char *from, int bits)
- X{
- X int i;
- X
- X for (i=0; i<bits; i++)
- X to[i] ^= from[i];
- X}
- X
- X/*
- X * subs(o,i,s) -- substitute 6 bits to 4 bits using ST[s]
- X */
- X
- Xstatic void
- Xsubs(unsigned char *o, unsigned char *i, int s)
- X{
- X int t;
- X
- X t = ST[s][
- X (i[0] << 5) | (i[5] << 4) | /* 1st and 6th bit, row index */
- X (i[1] << 3) | (i[2] << 2) | /* 2nd to 5th bit, col index */
- X (i[3] << 1) | i[4]];
- X o[0] = (t >> 3) & 1;
- X o[1] = (t >> 2) & 1;
- X o[2] = (t >> 1) & 1;
- X o[3] = t & 1;
- X}
- X
- X/*
- X * des_set_key(key,ks) -- generate a keyschedule
- X */
- X
- Xint
- Xdes_set_key(des_cblock *key, des_key_schedule *ks)
- X{
- X int i;
- X unsigned char k[64],cd[56];
- X
- X unbit(k,(char *)key,8);
- X perm(cd, PC1, k, 56);
- X for (i = 0; i < 16; i++) {
- X rot(&cd[0], 28, LS[i]);
- X rot(&cd[28], 28, LS[i]);
- X perm(ks->b[i], PC2, cd, 48);
- X }
- X return 1;
- X}
- X
- X/*
- X * des_func(in,out,ks) -- des core function
- X */
- X
- Xstatic void
- Xdes_func(unsigned char in[64], unsigned char out[64],
- X unsigned char b[16][48], int enc)
- X{
- X int i,j;
- X unsigned char lr[64], rsto[32], si[48], so[32];
- X#define LEFT lr
- X#define RIGHT &lr[32]
- X
- X perm(lr, IP, in, 64); /* initial permutation */
- X for (i = 0; i < 16; i++) { /* 16 rounds */
- X memcpy(rsto, RIGHT,32); /* store right part */
- X perm(si, E, RIGHT, 48); /* F-box: permute to subst. input */
- X add2(si, b[enc ? i : 15-i], 48); /* add key (enc/dec order) */
- X for (j = 0; j < 8; j++) /* 48 to 32 bit substitute */
- X subs(&so[j*4],&si[j*6],j);
- X perm(RIGHT, P, so, 32); /* permute substitution output: end F*/
- X add2(RIGHT, LEFT, 32); /* add left part */
- X memcpy(LEFT, rsto, 32); /* old right part will be new left */
- X }
- X memcpy(LEFT, RIGHT, 32); /* swap left and right part */
- X memcpy(RIGHT,rsto, 32);
- X perm(out,FP,lr,64); /* final permutation */
- X}
- X
- X/*
- X * des_ecb_encrypt(in, out, ks, enc) -- des electronic code book mode
- X */
- X
- Xint
- Xdes_ecb_encrypt(des_cblock *in, des_cblock *out,
- X des_key_schedule *ks, int enc)
- X{
- X unsigned char o[64];
- X
- X unbit(o, (char *)in, 8);
- X des_func(o, o, ks->b, enc);
- X pkbit((char *)out, o, 8);
- X return 8;
- X}
- X
- X/*
- X * des_cbc_encrypt(in, out, len, ks, iv, enc) -- cipher block chaining mode
- X */
- X
- Xint
- Xdes_cbc_encrypt(des_cblock *in, des_cblock *out,
- X int len, des_key_schedule *ks, des_cblock *iv, int enc)
- X{
- X int i;
- X unsigned char ivb[64], ob[64], ib[64];
- X
- X unbit(ivb,(char *)iv,8);
- X for (i = 0; i < len-8; i+=8) {
- X unbit(ib,(char *)in++,8);
- X if (enc)
- X add2(ib, ivb, 64);
- X des_func(ib, ob, ks->b, enc);
- X if (enc) {
- X memcpy(ivb, ob, 64);
- X } else {
- X add2(ob, ivb, 64);
- X memcpy(ivb, ib, 64);
- X }
- X pkbit((char *)out++, ob, 8);
- X }
- X memset(ib,0,64); /* pad with zeroes */
- X unbit(ib,(char *)in,len-i);
- X if (enc)
- X add2(ib, ivb, 64);
- X des_func(ib, ob, ks->b, enc);
- X if (enc) {
- X pkbit((char *)iv, ob, 64);
- X } else {
- X add2(ob, ivb, 64);
- X pkbit((char *)iv, ib, 64);
- X }
- X pkbit((char *)out, ob, 8);
- X return (i+8);
- X}
- X
- Xvoid
- Xdes_set_odd_parity(des_cblock *key)
- X{
- X int i;
- X unsigned char c;
- X
- X for (i = 0; i < 8; i++) {
- X c = key->_[i] & 0xfe;
- X c = (c >> 4) ^ (c & 0x0f);
- X c = (c >> 2) ^ (c & 3);
- X key->_[i] = (key->_[i] & 0xfe) |
- X ((c >> 1) ^ (c & 1)) ^ 1 ;
- X }
- X}
- X
- END_OF_mdes.c
- if test 7722 -ne `wc -c <mdes.c`; then
- echo shar: "mdes.c" unpacked with wrong size!
- fi
- # end of overwriting check
- fi
- if test -f mdes.h -a "${1}" != "-c" ; then
- echo shar: Will not over-write existing file "mdes.h"
- else
- echo shar: Extracting "mdes.h" (747 characters)
- sed "s/^X//" >mdes.h <<'END_OF_mdes.h'
- X/*
- X * mdes.h -- header for minimal des
- X * 1993 Risto Paasivirta, paasivir@jyu.fi
- X * Public Domain, no warranty.
- X */
- X
- X#ifndef MDES_H
- X#define MDES_H
- X
- X#define DES_ENCRYPT 1
- X#define DES_DECRYPT 0
- X
- Xtypedef struct des_key_schedule {
- X unsigned char b[16][48];
- X} des_key_schedule;
- X
- Xtypedef struct des_cblock {
- X unsigned char _[8];
- X} des_cblock;
- X
- Xvoid des_string_to_key(char *str, des_cblock *key);
- Xvoid des_set_odd_parity(des_cblock *key);
- X
- Xint des_set_key(des_cblock *k, des_key_schedule *key);
- Xint des_ecb_encrypt(des_cblock *input, des_cblock *output,
- X des_key_schedule *schedule, int mode);
- Xint des_cbc_encrypt(des_cblock *input, des_cblock *output,
- X int length, des_key_schedule *schedule,
- X des_cblock *ivec, int mode);
- X
- X#endif /* MDES_H */
- X
- END_OF_mdes.h
- if test 747 -ne `wc -c <mdes.h`; then
- echo shar: "mdes.h" unpacked with wrong size!
- fi
- # end of overwriting check
- fi
- echo shar: End of shell archive.
- exit 0
- --
- /*paasivir@jyu.fi*/int a[3302],b=3301,*c=a,d,e,f;main(){for(e=b;--e;*c++=1);*c
- =2;for(d=2001;d--;printf("%05d",f))for(c=a,e=b;e;f/=e--){f+=*c*1e5;*c++=f%e;}}