crypt.c - 9base - revived minimalist port of Plan 9 userland to Unix
 (HTM) git clone git://git.suckless.org/9base
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       crypt.c (1045B)
       ---
            1 /*
            2  *        Data Encryption Standard
            3  *        D.P.Mitchell  83/06/08.
            4  *
            5  *        block_cipher(key, block, decrypting)
            6  *
            7  *        these routines use the non-standard 7 byte format
            8  *        for DES keys.
            9  */
           10 #include <u.h>
           11 #include <libc.h>
           12 #include <auth.h>
           13 #include <libsec.h>
           14 
           15 /*
           16  * destructively encrypt the buffer, which
           17  * must be at least 8 characters long.
           18  */
           19 int
           20 encrypt(void *key, void *vbuf, int n)
           21 {
           22         ulong ekey[32];
           23         uchar *buf;
           24         int i, r;
           25 
           26         if(n < 8)
           27                 return 0;
           28         key_setup(key, ekey);
           29         buf = vbuf;
           30         n--;
           31         r = n % 7;
           32         n /= 7;
           33         for(i = 0; i < n; i++){
           34                 block_cipher(ekey, buf, 0);
           35                 buf += 7;
           36         }
           37         if(r)
           38                 block_cipher(ekey, buf - 7 + r, 0);
           39         return 1;
           40 }
           41 
           42 /*
           43  * destructively decrypt the buffer, which
           44  * must be at least 8 characters long.
           45  */
           46 int
           47 decrypt(void *key, void *vbuf, int n)
           48 {
           49         ulong ekey[128];
           50         uchar *buf;
           51         int i, r;
           52 
           53         if(n < 8)
           54                 return 0;
           55         key_setup(key, ekey);
           56         buf = vbuf;
           57         n--;
           58         r = n % 7;
           59         n /= 7;
           60         buf += n * 7;
           61         if(r)
           62                 block_cipher(ekey, buf - 7 + r, 1);
           63         for(i = 0; i < n; i++){
           64                 buf -= 7;
           65                 block_cipher(ekey, buf, 1);
           66         }
           67         return 1;
           68 }