📄 bccreate.c
字号:
/********************************************************************* Copyright (c) 1994-1999 Jetico, Inc., Finland* All rights reserved.** File: bccreate.c* Revision: $Id: bccreate.c,v 1.20 2005/05/14 07:50:50 crypt Rel-1.6-3 $* Created:* Description: implementation of create container routine********************************************************************///#define __USE_LARGEFILE64#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <strings.h>#include <string.h>#include <errno.h>#include <bc_types.h>#include <bc_ioctl.h>#include "keygen.h"#include <kg_defs.h>#include <sys/types.h>#include <time.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <sys/mount.h>#include <kblock.h>#include "alg.h"#include "header.h"#include "config.h"#include "bctool.h"#include "misc.h"#include "error.h"#include "version.h"#ifndef S_BLKSIZE#define S_BLKSIZE 512 /* Block size for `st_blocks'. */#endif#ifndef BLKGETSIZE#include <linux/fs.h>#endif//#ifndef O_LARGEFILE//#define O_LARGEFILE 0100000//#endifchar bccreate_c[]="$Id: bccreate.c,v 1.20 2005/05/14 07:50:50 crypt Rel-1.6-3 $";ALG_SERV Alg;DWORD KeyHandle;char *ContName=NULL;DATA_BLOCK *pdb=NULL;void CreateSignalHandler(int sig){ if ( ask_y_n("Do you really want to terminate process? (y/n) [n]:")==FALSE ) return; FreeKeyHandle(Alg,KeyHandle); if ( pdb!=NULL )FreeDataBlock((BYTE **)&pdb);// if ( ContName!=NULL ) unlink(ContName); //Not really needed exit(-1);}int bccreate (int argc, char ** argv){ struct HiddenSector hs; int fd; DWORD DataSize; alg_info *alg_inf; unsigned long long i; time_t t,ot; int pr=0; int opr=0; struct stat st; long l; int is_dev=0; char device[MAX_DEV_PATH]; if ( argc < 2 ) { msg(stderr,"Please specify container name\n"); return -1; } else if ( argc > 2 ) { msg(stderr,"Too many parameters\n"); return -1; } UseSha256(); if ( o_alg==NULL ) { msg(stderr,"Please specify algorithm name\n"); return -1; } if (0 == access(argv[1],F_OK) && -1 != stat(argv[1],&st) && S_ISBLK(st.st_mode)) { is_dev=1; } if ( o_cont_size==0 && !is_dev) { msg(stderr,"Please specify container size\n"); return -1; } if ( 0 != o_cont_size && is_dev) { msg(stderr,"Ignoring \"-s\" parameter\n"); } if (is_dev) { if (0 != access(argv[1],W_OK|R_OK)) { msg(stderr,"access(\"%s\",W_OK|R_OK): %s\n",argv[1],strerror(errno)); return -1; } if (!ask_y_n("Warning: All data on specified block device will be lost! Continue? (y/[n]):")) { return -1; } } if ( (alg_inf=GetInfoByName(o_alg))==NULL ) { msg(stderr,"Unknown algorithm: %s\n",o_alg); return -1; } SetSignalHandlers(CreateSignalHandler); if ( is_dev ) { if ( (fd = open(argv[1], O_RDWR)) < 0 ) { msg(stderr,"open(\"%s\",O_RDWR): %s\n",argv[1],strerror(errno)); return -1; } } else { if ( (fd = open(argv[1], O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE)) < 0 ) { msg(stderr,"open(\"%s\",O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE): %s\n",argv[1],strerror(errno)); return -1; } } ContName=argv[1]; if ( is_dev && !ioctl(fd,BLKGETSIZE,&l) ) o_cont_size=(long long)l*S_BLKSIZE; /* setting defaults for hidden sector */ memset(&hs,0,sizeof(hs)); strncpy(hs.br.OEMid,"LOCOS94 ",8); strncpy(hs.br.volumeLabel,"CRYPTED_DSK",11); hs.br.jmpCode[0] = 0xEB; hs.br.jmpCode[1] = 0x3C; hs.br.jmpCode[2] = 0x90; hs.dwKeySize = sizeof(DATA_BLOCK); hs.dwDataOffset = ( 512 + sizeof(DATA_BLOCK) + 511 ) & ~511;// hs.br.bpb.totalSectorsLong = (DWORD)((o_cont_size - hs.dwDataOffset) / 512) ; hs.br.bpb.totalSectorsLong = (DWORD)((o_cont_size) / 512) ; hs.description[0]='\0'; if ( o_description!=NULL ) { strncpy(hs.description,o_description,65); hs.description[65]=0; } hs.algorithmId=alg_inf->id; hs.keyGenId=KGSHA256_ID; hs.version = CONTAINER_VERSION; /* end of setting defaults */ if ( (Alg.bc_fd = open_bc_dev(device,0)) < 0 ) { msg(stderr,"open(\"%s\", O_RDONLY): %s\n",device,strerror(errno)); return -1; } Alg.alg_id=alg_inf->id; Alg.alg_module =alg_inf->mod_name; DataSize = sizeof(DATA_BLOCK); CreateKeyHandle(Alg,alg_inf->key_size,NULL,"Enter password:",CFLAG_CREATE_WITH_SINGLE_PASSWORD,(BYTE **)&pdb,&DataSize,&KeyHandle,&Error); if ( Error!=0 ) { print_error(Error); close(fd); close(Alg.bc_fd); if (!is_dev) unlink(ContName); return -1; } if ( write(fd,&hs,sizeof(hs)) < 0 ) { msg(stderr,"write(\"%s\"...): %s\n",argv[1],strerror(errno)); FreeKeyHandle(Alg,KeyHandle); FreeDataBlock((BYTE **)&pdb); close(fd); close(Alg.bc_fd); if (!is_dev) unlink(ContName); return -1; } if ( write(fd,pdb,sizeof(DATA_BLOCK)) < 0 ) { msg(stderr,"write(\"%s\"...): %s\n",argv[1],strerror(errno)); FreeKeyHandle(Alg,KeyHandle); FreeDataBlock((BYTE **)&pdb); close(fd); close(Alg.bc_fd); if (!is_dev) unlink(ContName); return -1; } if (is_dev) { FreeKeyHandle(Alg,KeyHandle); FreeDataBlock((BYTE **)&pdb); close(fd); close(Alg.bc_fd); return 0; } // memset (pdb,0,sizeof(DATA_BLOCK)); //o_cont_size-=sizeof(hs)+sizeof(DATA_BLOCK); ot=t=time(NULL); RandomGenerate((unsigned char*)pdb, sizeof(DATA_BLOCK), (unsigned char*)&t, sizeof(t)); if ( o_verbose ) msg(stdout,"Creating container %s\n",argv[1]); for ( i = o_cont_size+(hs.dwDataOffset-sizeof(hs)-sizeof(DATA_BLOCK)); i >= sizeof(DATA_BLOCK); i -= sizeof(DATA_BLOCK) ) { if ( write(fd,pdb,sizeof(DATA_BLOCK)) < 0 ) { msg(stderr,"write(\"%s\"...): %s\n",argv[1],strerror(errno)); FreeKeyHandle(Alg,KeyHandle); FreeDataBlock((BYTE **)&pdb); close(fd); close(Alg.bc_fd); unlink(ContName); return -1; } RandomGenerate((unsigned char*)pdb, sizeof(DATA_BLOCK),(unsigned char*)pdb, sizeof(DATA_BLOCK)); if ( o_verbose ) { t=time(NULL); pr=100 - (int)((long long)i*100/o_cont_size); if ( t != ot ) { msg(stdout,"%10ld/%ld Kb (%3d%%) \r",(unsigned long int)((o_cont_size-i)>>10),(unsigned long int)(o_cont_size>>10),pr); ot=t; opr=pr; } else if ( pr != opr ) { msg(stdout,"%10ld/%ld Kb (%3d%%) \r",(unsigned long int)((o_cont_size-i)>>10),(unsigned long int)(o_cont_size>>10),pr); opr=pr; } } } if ( write(fd,pdb,i) < 0 ) { msg(stderr,"write(\"%s\"...): %s\n",argv[1],strerror(errno)); FreeKeyHandle(Alg,KeyHandle); FreeDataBlock((BYTE **)&pdb); close(fd); close(Alg.bc_fd); unlink(ContName); return -1; } if ( o_verbose ) { msg(stdout,"%10ld/%ld Kb (100%%)\n",(unsigned long int)(o_cont_size>>10),(unsigned long int)(o_cont_size>>10)); } FreeKeyHandle(Alg,KeyHandle); FreeDataBlock((BYTE **)&pdb); fchmod(fd,S_IRUSR|S_IWUSR); close(fd); close(Alg.bc_fd); msg(stdout,"\nPlease do not forget to format the container\n"); return 0;}//return size of hidden part in 512 bytes unitslong long get_size_of_hidden_part(char *string, long long int cont_size /*in 512 units*/ ) { char *endptr; long long result; result=strtoul(string,&endptr,0); if ( string==endptr ) return 0; if ( (string[0]!='\0')&&(endptr[0]=='\0') ) return result/512; if ( (endptr[0]=='K' || endptr[0]=='k' )&&(endptr[1]=='\0') ) { result *=1024; return result/512; }; if ( (endptr[0]=='M' || endptr[0]=='m')&&(endptr[1]=='\0') ) { result *=1024*1024; return result/512; }; if ( (endptr[0]=='%')&&(endptr[1]=='\0') ) { result = (result * cont_size / 100 ); return result; }; return 0L;}int bccreate_hidden(int argc, char ** argv){ struct HiddenSector hs; int fd; unsigned int offset = 2048; //1Mb ALG_SERV Alg; DWORD KeyHandle,DataSize; DATA_BLOCK db, *pdb; alg_info *alg_inf; char device[MAX_DEV_PATH]; if ( argc < 3 ) { msg(stderr,"Please specify container name and size of hidden part\n"); return -1; } else if ( argc > 3 ) { msg(stderr,"Too many parameters\n"); return -1; } if (!is_valid_container(argv[1])) { msg(stderr,"Invalid container\n"); return -1; } if ( (Alg.bc_fd = open_bc_dev(device,0)) < 0 ) { msg(stderr,"open(\"%s\",O_RDONLY): %s\n",device,strerror(errno)); close(Alg.bc_fd); return -1; } if ( (fd = open(argv[1], O_RDWR|O_LARGEFILE)) < 0 ) { msg(stderr,"open(\"%s\",O_RDWR|O_LARGEFILE): %s\n", argv[1], strerror(errno)); return -1; } if ( read(fd, &hs, sizeof(hs))!=sizeof(hs) ) { msg(stderr,"Can not read container header\n"); close(fd); close(Alg.bc_fd); return -1; } offset = get_size_of_hidden_part(argv[2],hs.br.bpb.totalSectorsLong); // size, not offset offset = hs.br.bpb.totalSectorsLong - offset; if (offset < 20 /*10Kb*/ || offset >= hs.br.bpb.totalSectorsLong) { msg(stderr,"Wrong size for hidden part\n"); close(fd); close(Alg.bc_fd); return -1; } if (o_verbose) { msg(stdout,"Creating hidden part with size = %dMb\n",(hs.br.bpb.totalSectorsLong - offset)*512/(1024*1024)); } if ( 'B' == hs.br.jmpCode[0] ) { msg(stderr, "WARNING! Container \"%s\" is probably in use!\n\n" "Two reasons for the report are possible:\n" " 1. The container is already mounted. In this case do not use the container\n" " until someone dismounts it.\n" " 2. The container was not unmounted properly last time, so a lock mask \n" " for the container was not reset. If so, unlock the container by running the command:\n" " \"bctool unlock %s\" \n\n", argv[1],argv[1]); close(fd); close(Alg.bc_fd); return -1; } if ( read(fd, &db, sizeof (db))!=sizeof(db) ) { msg(stderr,"Can not read data block\n"); close(fd); close(Alg.bc_fd); return -1; } if ( (alg_inf=GetInfoById(hs.algorithmId))==NULL ) { msg(stderr,"Unknown algorithm Id: %d\n",hs.algorithmId); close(fd); close(Alg.bc_fd); return -1; } Alg.alg_id=hs.algorithmId; Alg.alg_module =alg_inf->mod_name; pdb = &db; DataSize = sizeof(db); if (KGSHA256_ID == hs.keyGenId) { UseSha256(); } CreateKeyHandleEx(Alg,alg_inf->key_size,NULL,"Enter password for hidden part:",CFLAG_CREATE_HIDDEN_PART,(BYTE **)&pdb,&DataSize,&KeyHandle,&Error,&offset); if ( Error!=0 ) { print_error(Error); close(fd); close(Alg.bc_fd); return -1; } if ( lseek(fd,sizeof(hs),SEEK_SET) < 0 ) { msg(stderr,"lseek(%d,%d,SEEK_SET): %s\n", fd, sizeof(hs), strerror(errno)); FreeKeyHandle(Alg,KeyHandle); close(fd); close(Alg.bc_fd); return -1; } if ( write(fd,&db,sizeof(db)) < 0 ) { msg(stderr,"write(%d,&db,%d): %s\n", fd, sizeof(db), strerror(errno)); FreeKeyHandle(Alg,KeyHandle); close(fd); close(Alg.bc_fd); return -1; } FreeKeyHandle(Alg,KeyHandle); close(fd); close(Alg.bc_fd); msg(stdout,"\nPlease do not forget to format the hidden part of container\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -