Recovered from microbel
This commit is contained in:
188
common/lib/crypto/crypto.C
Normal file
188
common/lib/crypto/crypto.C
Normal file
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
* PVVMUD a 3D MUD
|
||||
* Copyright (C) 1998-1999 Programvareverkstedet (pvv@pvv.org)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include "crypto.H"
|
||||
#include <errno.h>
|
||||
|
||||
#define MAXCRYPTSIZE 1024
|
||||
#define int32 int
|
||||
|
||||
CCrypto::CCrypto(void) {
|
||||
|
||||
// Common
|
||||
|
||||
cryptKey=(BF_KEY *)malloc(sizeof(BF_KEY));
|
||||
BF_set_key(cryptKey,19,(unsigned char *)"This is just a test");
|
||||
|
||||
// Reading part of the code
|
||||
|
||||
cryptBuf_r=(unsigned char *)malloc(4);
|
||||
cryptSize_r=0;
|
||||
cryptPos_r=0;
|
||||
cryptState_r=0;
|
||||
cryptN_r=0;
|
||||
|
||||
cryptIV_r[0]=0xfe;
|
||||
cryptIV_r[1]=0xdc;
|
||||
cryptIV_r[2]=0xba;
|
||||
cryptIV_r[3]=0x98;
|
||||
cryptIV_r[4]=0x76;
|
||||
cryptIV_r[5]=0x54;
|
||||
cryptIV_r[6]=0x32;
|
||||
cryptIV_r[7]=0x10;
|
||||
|
||||
// Writing part of the code
|
||||
|
||||
cryptTempBuf=(unsigned char *)malloc(4);
|
||||
cryptSize_w=0;
|
||||
cryptPos_w=0;
|
||||
cryptState_w=0;
|
||||
cryptN_w=0;
|
||||
|
||||
cryptIV_w[0]=0xfe;
|
||||
cryptIV_w[1]=0xdc;
|
||||
cryptIV_w[2]=0xba;
|
||||
cryptIV_w[3]=0x98;
|
||||
cryptIV_w[4]=0x76;
|
||||
cryptIV_w[5]=0x54;
|
||||
cryptIV_w[6]=0x32;
|
||||
cryptIV_w[7]=0x10;
|
||||
|
||||
}
|
||||
|
||||
CCrypto::~CCrypto(void) {
|
||||
if (cryptBuf_r != NULL) free(cryptBuf_r);
|
||||
if (cryptBuf_w != NULL) free(cryptTempBuf);
|
||||
if (cryptKey != NULL) free(cryptKey);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
ssize_t CCrypto::read(int fd, char **buf, size_t count) {
|
||||
ssize_t bRead;
|
||||
unsigned char *tempBuf;
|
||||
int i,j;
|
||||
|
||||
while (1)
|
||||
switch (cryptState_r) {
|
||||
case 0:
|
||||
bRead=::read(fd,(cryptBuf_r+cryptPos_r),(4-cryptPos_r));
|
||||
if (bRead > 0) cryptPos_r+=bRead;
|
||||
if (cryptPos_r < 4) return (-2);
|
||||
cryptSize_r=*(int32 *)cryptBuf_r;
|
||||
free(cryptBuf_r);
|
||||
cryptBuf_r=(unsigned char *)malloc(cryptSize_r);
|
||||
if (cryptBuf_r==NULL) printf("Couldn't allocate memory \n");
|
||||
bRead=0;
|
||||
cryptPos_r=0;
|
||||
cryptState_r=1;
|
||||
|
||||
case 1:
|
||||
bRead=::read(fd,(cryptBuf_r+cryptPos_r),(cryptSize_r-cryptPos_r));
|
||||
if (bRead > 0) {
|
||||
cryptPos_r+=bRead;
|
||||
};
|
||||
if (cryptPos_r < cryptSize_r) return (-2);
|
||||
tempBuf=cryptBuf_r;
|
||||
cryptBuf_r=(unsigned char *)malloc(cryptSize_r);
|
||||
BF_cfb64_encrypt(tempBuf,cryptBuf_r,cryptSize_r,cryptKey,
|
||||
cryptIV_r,&cryptN_r,BF_DECRYPT);
|
||||
printf("Decrypting ... %i bytes ...\n",cryptSize_r);
|
||||
free(tempBuf);
|
||||
cryptPos_r=0;
|
||||
cryptState_r=2;
|
||||
|
||||
case 2:
|
||||
*buf=(char *)(cryptBuf_r+cryptPos_r);
|
||||
j=MIN(cryptSize_r-cryptPos_r,count);
|
||||
cryptPos_r+=j;
|
||||
if (cryptPos_r==cryptSize_r) cryptState_r=3;
|
||||
if (j==0) j=-2;
|
||||
return(j);
|
||||
case 3:
|
||||
free(cryptBuf_r);
|
||||
cryptBuf_r=(unsigned char *)malloc(4);
|
||||
cryptPos_r=0;
|
||||
cryptState_r=0;
|
||||
};
|
||||
}
|
||||
|
||||
ssize_t CCrypto::write(int fd, void *buf, size_t count) {
|
||||
ssize_t bWrote;
|
||||
int32 length,j;
|
||||
unsigned char *tempBuf;
|
||||
int i=0;
|
||||
|
||||
if (count == 0) return (0);
|
||||
|
||||
while(1)
|
||||
switch(cryptState_w) {
|
||||
case 0:
|
||||
cryptSize_w = count;
|
||||
cryptBuf_w = (unsigned char *)buf;
|
||||
cryptTempPos = 0;
|
||||
cryptPos_w = 0;
|
||||
*(int32 *)(cryptTempBuf) = cryptSize_w;
|
||||
cryptState_w = 1;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
bWrote=1;
|
||||
while (bWrote>0) {
|
||||
bWrote=::write(fd,(cryptTempBuf+cryptTempPos),(4-cryptTempPos));
|
||||
if (bWrote>0) cryptTempPos+=bWrote;;
|
||||
};
|
||||
if (cryptTempPos<4) return (0);
|
||||
free(cryptTempBuf);
|
||||
cryptTempBuf=(unsigned char *)malloc(cryptSize_w);
|
||||
BF_cfb64_encrypt(cryptBuf_w, cryptTempBuf, cryptSize_w,
|
||||
cryptKey, cryptIV_w, &cryptN_w, BF_ENCRYPT);
|
||||
cryptPos_w = 0;
|
||||
cryptState_w = 2;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
cryptTempSize = MIN(cryptSize_w-cryptPos_w,MAXCRYPTSIZE);
|
||||
cryptTempPos = 0;
|
||||
cryptState_w = 3;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
bWrote=1;
|
||||
while(bWrote>0) {
|
||||
bWrote=::write(fd,(void *)(cryptTempBuf+cryptPos_w+cryptTempPos),
|
||||
(cryptTempSize-cryptTempPos));
|
||||
if (bWrote>0) cryptTempPos+=bWrote;
|
||||
};
|
||||
if (cryptTempPos<cryptTempSize) return (0);
|
||||
cryptPos_w+=cryptTempPos;
|
||||
cryptState_w = 2;
|
||||
if (cryptPos_w<cryptSize_w) break;
|
||||
free(cryptTempBuf);
|
||||
cryptTempBuf = (unsigned char *)malloc(4);
|
||||
cryptState_w = 0;
|
||||
printf("Encrypting : ... %i bytes ...\n",cryptSize_w);
|
||||
return(cryptPos_w);
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user