
mdes.c, mdes.h, destest, des_test_data

        Public Domain, NO WARRANRY.

        This is a trivial DES implementation I wrote to figure out
        the algorithm. It's very slow. There are many faster free
        implementations around.

        In USA this code may or may not be subject of export regulations,
        etc.

Version

        1.0, first and probably last if des_cbc_encrypt (untested) works.

Author
        Risto Paasivirta, paasivir@jyu.fi

=============================================================================

mdes.h

                #define DES_ENCRYPT        1
                #define DES_DECRYPT        0

        Des encryption flag. Encrypt if DES_ENCRYPT, decrypt if DES_DECRYPT.
        (Doesn't sound too complex, does it.)

                typedef struct des_key_schedule {
                        unsigned char b[16][48];
                } des_key_schedule;

        Key schedule type. Key bits are left unpacked for 'speed'.


                typedef struct des_cblock {
                        unsigned char _[8];
                } des_cblock;

        Des character block type. Any 8 char block should work fine.

mdes.c

        Des functions

                void des_set_odd_parity(des_cblock *key);

        Set parity of 56-bit (7bits x 8 bytes) key block. Parity bit is
        the least significant bit in each byte. This implementation works
        with incorrect parity key blocks.

                int des_set_key(des_cblock *k, des_key_schedule *key);

        Initialize key schedule from key block k. If key checking would be
        implemented this probably should return some value.

                int des_ecb_encrypt(des_cblock *input, des_cblock *output,
                        des_key_schedule *schedule, int encrypt);

        Des encryption in des electronic code book mode. En/decrypt one
        cblock from input to output using key shedule. Returns number
        of bytes written to output (8.)

                int des_cbc_encrypt(des_cblock *input, des_cblock *output,
                        int len, des_key_schedule *key,
                        des_cblock *ivec, int mode);

        Des encryption in cipher block chaining mode. En/decrypt len bytes
        from input to output using scheduled key and initial ivec. If len is
        not multiple of 8, last block is padded with NULL bytes. Output is
        written always multiple of 8 bytes. Returns number of bytes written
        to output and final ivec in ivec.

destest.c
des_test_data

        Destest reads file des_test_data, converts hex key/plain/cipher
        cblock to binary and checks that en/decryption works in elecrtoric
        code book mode. File des_test_data has been posted few times to
        sci.crypt, only thing I have done to it was that I corrected one 
        key parity error from line 143:
        original
        3107911598090140 0000000000000000 406A9A6AB43399AE
        changed to:
        3107911598080140 0000000000000000 406A9A6AB43399AE

(eof)
