1 /* 2 * Copyright (C) 2001, 2002, and 2003 Roy Keene 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 2 7 * of the License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 * 18 * email: dact@rkeene.org 19 */ 20 21 /* 22 Encrypt data. 23 */ 24 #include "dact.h" 25 #ifdef HAVE_UNISTD_H 26 #include <unistd.h> 27 #endif 28 #ifdef HAVE_STDLIB_H 29 #include <stdlib.h> 30 #endif 31 #ifdef HAVE_STRING_H 32 #include <string.h> 33 #endif 34 #ifdef HAVE_SYS_TYPES_H 35 #include <sys/types.h> 36 #endif 37 #ifdef HAVE_SYS_STAT_H 38 #include <sys/stat.h> 39 #endif 40 #include <fcntl.h> 41 #include <stdio.h> 42 #include <math.h> 43 #include "cipher_psub.h" 44 #include "parse.h" 45 #include "ui.h" 46 47 #if defined(USE_MODULES) && defined(AS_MODULE) 48 #include "module.h" 49 uint32_t DC_NUM=3; 50 uint32_t DC_TYPE=DACT_MOD_TYPE_ENC; 51 void *DC_ALGO=cipher_psub; 52 char *DC_NAME="psubst (MOD)"; 53 #endif 54 55 56 int cipher_psub(const unsigned char *inblock, unsigned char *outblock, const int blksize, unsigned char *key, const int mode) { 57 switch (mode) { 58 case (DACT_MODE_CINIT+DACT_MODE_CDEC): 59 case (DACT_MODE_CINIT+DACT_MODE_CENC): 60 case DACT_MODE_CINIT: 61 return(cipher_psub_init(mode,key)); 62 break; 63 case DACT_MODE_CDEC: 64 return(cipher_psub_decrypt(inblock, outblock, blksize, key)); 65 break; 66 case DACT_MODE_CENC: 67 return(cipher_psub_encrypt(inblock, outblock, blksize, key)); 68 break; 69 } 70 return(0); 71 } 72 73 74 int cipher_psub_init(const int mode, unsigned char *key) { 75 return(cipher_psub_init_getkey(mode-DACT_MODE_CINIT,key)); 76 } 77 78 int cipher_psub_init_getkey(const int mode, unsigned char *key) { 79 unsigned char *phrase; 80 char *keyreg; 81 82 phrase=dact_ui_getuserinput("Passphrase: ",128,1); 83 84 memcpy(key,keyreg=cipher_psub_generatekey(phrase),257); 85 free(keyreg); 86 return(257); 87 } 88 89 char *cipher_psub_generatekey(const char *passphrase) { 90 char *keybuf, used[256], hbuf[4]; 91 int i,m,x,loc=0,num; 92 double d; 93 94 keybuf=malloc(1024); 95 96 for (i=0;i<256;i++) used[i]=0; 97 98 if (strlen(passphrase)<3) { num=257; } else { num=((259/((int) (strlen(passphrase)/3)))+1); } 99 100 for (m=0;m<strlen(passphrase);m+=3) { 101 memcpy(hbuf,passphrase+m,3); 102 hbuf[3]='\0'; 103 d=hash_fourbyte(hbuf, '\0'); 104 for (i=0;i<num;i++) { 105 d=(sin(tan(d))*(255*5)); 106 x=((abs((int) d)&0x3ff)-255); 107 if (x<0 || x>255 || used[x]) { i--; continue; } 108 used[x]=1; 109 if (loc==0) used[x]=0; 110 keybuf[loc++]=x; 111 if (loc==257) break; 112 } 113 if (loc==257) break; 114 } 115 return(keybuf); 116 } 117 118 int cipher_psub_encrypt(const unsigned char *inblk, unsigned char *outblk, int blksize, unsigned char *key) { 119 int i,mod; 120 static int keyoffset=0; 121 122 mod=(int) key[0]; 123 for (i=0;i<blksize;i++) { 124 if (!(i%mod)) { 125 keyoffset=((keyoffset+1)&0xff); 126 } 127 outblk[i]=key[((((int) inblk[i])+keyoffset)&0xff)+1]; 128 } 129 return(blksize); 130 } 131 132 int cipher_psub_decrypt(const unsigned char *inblk, unsigned char *outblk, int blksize, unsigned char *key) { 133 int i,mod,x; 134 char reversekey[256]; 135 static int keyoffset=0; 136 137 mod=(int) key[0]; 138 for (i=1;i<257;i++) reversekey[(int) key[i]]=(i-1); 139 for (i=0;i<blksize;i++) { 140 if (!(i%mod)) { 141 keyoffset=((keyoffset+1)&0xff); 142 for (x=0;x<256;x++) reversekey[(int) key[((x+keyoffset)&0xff)+1]]=x; 143 } 144 outblk[i]=reversekey[(int) inblk[i]]; 145 } 146 return(blksize); 147 } |