4580030 [rkeene@sledge /home/rkeene/devel/dact]$ cat -n cipher_serpent.c
  1 /*
  2     Serpent Cipher from libmcrypt
  3 */
  4 #include "dact.h"
  5 #include "cipher_serpent.h"
  6 #ifdef HAVE_MCRYPT
  7 #ifdef HAVE_UNISTD_H
  8 #include <unistd.h>
  9 #endif
 10 #ifdef HAVE_STDLIB_H
 11 #include <stdlib.h>
 12 #endif
 13 #ifdef HAVE_STRING_H
 14 #include <string.h>
 15 #endif
 16 #ifdef HAVE_SYS_TYPES_H
 17 #include <sys/types.h>
 18 #endif
 19 #ifdef HAVE_SYS_STAT_H
 20 #include <sys/stat.h>
 21 #endif
 22 #include <fcntl.h>
 23 #include <stdio.h>
 24 #ifdef TIME_WITH_SYS_TIME
 25 #include <sys/time.h>
 26 #include <time.h>
 27 #else
 28 #ifdef HAVE_SYS_TIME_H
 29 #include <sys/time.h>
 30 #else
 31 #include <time.h>
 32 #endif
 33 #endif
 34 #include "parse.h"
 35 #include "ui.h"
 36 
 37 #if defined(USE_MODULES) && defined(AS_MODULE)
 38 #include "module.h"
 39 uint32_t DC_NUM=2;
 40 uint32_t DC_TYPE=DACT_MOD_TYPE_ENC;
 41 void *DC_ALGO=cipher_serpent;
 42 char *DC_NAME="serpent (MOD)";
 43 #endif
 44 
 45 MCRYPT mcrypt_tdid;
 46 
 47 int cipher_serpent(const char *inblock, char *outblock, const int blksize, char *key, const int mode) {
 48     static unsigned char *IV;
 49 
 50     switch (mode) {
 51         case (DACT_MODE_CINIT+DACT_MODE_CDEC):
 52         case (DACT_MODE_CINIT+DACT_MODE_CENC):
 53         case DACT_MODE_CINIT:
 54             return(cipher_serpent_init(mode, key, IV));
 55             break;
 56         case DACT_MODE_CDEC:
 57             return(cipher_serpent_decrypt(inblock, outblock, blksize, key, IV));
 58             break;
 59         case DACT_MODE_CENC:
 60             return(cipher_serpent_encrypt(inblock, outblock, blksize, key, IV));
 61             break;
 62     }
 63     return(0);
 64 }
 65 
 66 
 67 int cipher_serpent_init(const int mode, char *key, unsigned char *IV) {
 68     char *password;
 69     int plen, i;
 70     unsigned char buff;
 71     char *mime_buf;
 72 #ifdef RANDOM_DEV
 73     int fd;
 74     fd=open(RANDOM_DEV, O_RDONLY);
 75     if (fd<0) {
 76 #endif
 77         srand(time(NULL)+rand());
 78 #ifdef RANDOM_DEV
 79     }
 80 #endif
 81 
 82     mcrypt_tdid=mcrypt_module_open("serpent", NULL, "cfb", NULL);
 83     if (mcrypt_tdid==MCRYPT_FAILED) {
 84 #ifdef RANDOM_DEV
 85         if (fd>=0) close(fd);
 86 #endif
 87         return(-1);
 88     }
 89 
 90     password=dact_ui_getuserinput("Enter your passphrase: ", 128, 1);
 91     memset(key, 1, 16);
 92     plen=strlen(password);
 93     if (plen<16) plen=16;
 94     memcpy(key, password, plen);
 95 
 96     if ((mode-DACT_MODE_CINIT)==DACT_MODE_CENC) {
 97 /*  mhash_keygen(KEYGEN_MCRYPT, MHASH_MD5, key, 16, NULL, 0, password, strlen(password)); */
 98         plen=mcrypt_enc_get_iv_size(mcrypt_tdid);
 99         IV=malloc(plen);
100 
101         for (i=0; i<plen; i++) {
102 #ifdef RANDOM_DEV
103             if (fd>=0) {
104                 read(fd, &buff, 1);
105             } else {
106 #endif
107                 srand(time(NULL)+rand());
108                 buff=(int) ((256.0*rand())/(RAND_MAX+1.0));
109 #ifdef RANDOM_DEV
110             }
111 #endif
112             IV[i]=buff;
113         }
114         mime_buf=mimes64(IV, &plen);
115         fprintf(stderr, "Magic [needed for decryption]:  %s\n", mime_buf);
116         free(mime_buf);
117     } else {
118         plen=mcrypt_enc_get_iv_size(mcrypt_tdid);
119         IV=dact_ui_getuserinput("Enter your magic key: ", plen*3, 0);
120         mime_buf=demime64(IV);
121         memcpy(IV, mime_buf, plen);
122         free(mime_buf);
123     }
124 
125 
126 #ifdef RANDOM_DEV
127     if (fd>=0) close(fd);
128 #endif
129 
130     i=mcrypt_generic_init(mcrypt_tdid, key, 16, IV);
131     if (i<0) {
132         mcrypt_perror(i);
133         return(-1);
134     }
135 
136     return(16);
137 }
138 
139 int cipher_serpent_encrypt(const char *inblk, char *outblk, int blksize, char *key, unsigned char *IV) {
140     int i;
141 
142     memcpy(outblk, inblk, blksize);
143     for (i=0; i<blksize; i++) {
144         mcrypt_generic(mcrypt_tdid, &outblk[i], 1);
145     }
146     return(blksize);
147 }
148 
149 int cipher_serpent_decrypt(const char *inblk, char *outblk, int blksize, char *key, unsigned char *IV) {
150     int i;
151 
152     memcpy(outblk, inblk, blksize);
153     for (i=0; i<blksize; i++) {
154         mdecrypt_generic(mcrypt_tdid, outblk+i, 1);
155     }
156     return(blksize);
157 }
158 #else
159 int cipher_serpent(const char *inblock, char *outblock, const int blksize, char *key, const int mode) {
160     return(0);
161 }
162 #endif
4580031 [rkeene@sledge /home/rkeene/devel/dact]$

Click here to go back to the directory listing.
Click here to download this file.
last modified: 2004-04-04 07:01:49