📄 bclink.c
字号:
/********************************************************************* Copyright (c) 1994-2000 Jetico, Inc., Finland* All rights reserved.** File: bclink.c* Revision: $Id: bclink.c,v 1.20 2005/05/14 07:58:33 crypt Rel-1.6-3 $* Created:* Description: implementation of mount container routine********************************************************************/#include <sys/stat.h>#include <stdlib.h>#include <dirent.h>#include <errno.h>#include <stdio.h>#include <mntent.h>#include <unistd.h>#include <bc_types.h>#include <bc_ioctl.h>#include "keygen.h"#include <kg_defs.h>#include <fcntl.h>#include <kblock.h>#include <string.h>#include "alg.h"#include "header.h"#include "config.h"#include "bctool.h"#include "misc.h"#include "error.h"char bclink_c[]="$Id: bclink.c,v 1.20 2005/05/14 07:58:33 crypt Rel-1.6-3 $";int bclink_raw(int argc, char ** argv){ int fd,n; long Offset; ALG_SERV Alg; alg_info *alg_inf; char pool[POOL_SIZE_BYTES]; char *Key; char *password; if ( NULL == o_alg ) { msg(stderr,"Please specify algorithm\n"); return -1; } if ( argc < 3 ) { msg(stderr,"Please specify file name and device\n"); return -1; } else if ( argc > 3 ) { msg(stderr,"Too many parameters\n"); return -1; } if ( (Alg.bc_fd = open(argv[2], O_RDONLY)) < 0 ) { msg(stderr,"open(%s): %s\n",argv[2],strerror(errno)); return -1; } if ( 0 != bc_get_info(Alg,&n,&n) ) { msg(stderr,"device %s busy\n",argv[2]); return -1; } if ( (fd = open(argv[1], O_RDONLY|O_LARGEFILE)) < 0 ) { msg(stderr,"open(%s): %s\n",argv[1],strerror(errno)); return -1; } if ( (alg_inf = GetInfoByName(o_alg)) == NULL ) { msg(stderr,"Unknown algorithm : %s\n",o_alg); return -1; } if (0 != access(argv[1],R_OK|W_OK)) { msg(stderr,"You do not have write permissions for container.\n \ Linking read-only\n"); o_ro=1; } Alg.alg_id=alg_inf->id; Alg.alg_module=alg_inf->mod_name; Offset=0; Key = malloc(alg_inf->key_size/8); if ( NULL == Key ) { msg(stderr,"malloc(%d): %s\n",alg_inf->key_size/8,strerror(errno)); return -1; } if ( o_random_password ) { msg(stdout,"Using random password\n"); if ( ERROR_NO != GetRandomSeed(pool,sizeof(pool)) ) { msg(stderr,"Cannot get random seed\n"); return -1; } if ( RandomGenerateKey(Key, alg_inf->key_size/8, pool, sizeof(pool)) ) { msg(stderr,"Cannot generate Key\n"); return -1; } } else { if ( NULL == (password = getpass("Enter password:")) ) { msg(stderr,"Cannot read password\n"); return -1; } if ( RandomGenerateKey(Key, alg_inf->key_size/8, password, strlen(password)) ) { msg(stderr,"Cannot generate Key\n"); return -1; } memset(password,0,strlen(password)); } memset(pool,0,sizeof(pool)); Error=bc_set_fd64(Alg, Key, alg_inf->key_size, pool, fd, Offset, o_ro ? BC_FLAGS_READONLY : 0); memset(Key, 0, alg_inf->key_size/8); free(Key); if ( Error!=0 ) { msg(stderr,"bc_set_fd: %s\n",strerror(-Error)); return -1; } close(fd); close(Alg.bc_fd); return 0;}int bclink (int argc, char ** argv){ struct HiddenSector hs; int fd, fdd, n, flag; long long Offset; int _offset; ALG_SERV Alg; DWORD KeyHandle,DataSize; DATA_BLOCK db, *pdb; alg_info *alg_inf; if ( argc < 3 ) { msg(stderr,"Please specify container name and device\n"); return -1; } else if ( argc > 3 ) { msg(stderr,"Too many parameters\n"); return -1; } if ( (Alg.bc_fd = open(argv[2], O_RDONLY)) < 0 ) { msg(stderr,"open(%s): %s\n",argv[2],strerror(errno)); return -1; } if ( 0 != bc_get_info(Alg,&n,&n) ) { msg(stderr,"device %s busy\n",argv[2]); return -1; } if ( (fd = open(argv[1], O_RDONLY|O_LARGEFILE)) < 0 ) { msg(stderr,"open(%s): %s\n",argv[1],strerror(errno)); return -1; } if ( read(fd, &hs, sizeof(hs))!=sizeof(hs) ) { msg(stderr,"Can not read container header\n"); return -1; } if ( strncmp(hs.br.OEMid, "LOCOS94 ", sizeof(hs.br.OEMid))!=0 ) { msg(stderr,"%s is not valid BestCrypt container\n",argv[1]); return -1; } 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]); return -1; } if ( read(fd, &db, sizeof (db))!=sizeof(db) ) { msg(stderr,"Can not read data block\n"); return -1; } if ( (alg_inf=GetInfoById(hs.algorithmId))==NULL ) { msg(stderr,"Unknown algorithm Id: %d\n",hs.algorithmId); return -1; } Alg.alg_id=hs.algorithmId; Alg.alg_module =alg_inf->mod_name; pdb = &db; DataSize = sizeof(db); //Offset=hs.dwDataOffset; if (KGSHA256_ID == hs.keyGenId) { UseSha256(); } CreateKeyHandleEx(Alg,alg_inf->key_size,NULL,"Enter password:",CFLAG_VERIFY_AND_LOAD_KEY,(BYTE **)&pdb,&DataSize,&KeyHandle,&Error,&_offset); if ( Error!=0 ) { print_error(Error); return -1; } if (_offset) Offset = (long long)512 * _offset; else Offset = hs.dwDataOffset; if (0 != access(argv[1],R_OK|W_OK)) { msg(stderr,"You do not have write permissions for container.\n \ Linking read-only\n"); o_ro=1; } Error=bc_set_fdkh64(Alg, KeyHandle, fd, Offset, 0, 0, (o_ro ? BC_FLAGS_READONLY : 0) | (hs.version == 0 ? 0 : BC_FLAGS_IV_64BIT)); if ( Error!=0 ) { msg(stderr,"bc_set_fd_kh: %s\n",strerror(-Error)); FreeKeyHandle(Alg,KeyHandle); return -1; }//----------------------------------------------------------------------// BestCrypt for Windows 7.11+ compatibility code//---------------------------------------------------------------------- fdd = open(argv[2], O_RDONLY); // workaround for 2.4.x kernels flag = 0; _offset = check_msdos_partition(0 > fdd ? Alg.bc_fd : fdd, &flag); if (fdd >= 0) close(fdd); if (0 != (flag & 0xFF000000)) { // partition table detected msg(stderr, "Warning! %d (%d extended) partition(s) detected inside container.\n", flag >> 24, (flag >> 16) & 0xFF); if (0x0100 != (flag >> 16)) { // non-trivial case if (!ask_y_n("Do you want to continue? (y/[n]):")) { FreeKeyHandle(Alg, KeyHandle); close(Alg.bc_fd); close(fd); return 0; } }// Offset += (long long) 512 * _offset; bc_clear_fd(Alg); Error=bc_set_fdkh64(Alg, KeyHandle, fd, Offset, (long long)_offset, 0, (o_ro ? BC_FLAGS_READONLY : 0) | (hs.version == 0 ? 0 : BC_FLAGS_IV_64BIT)); if ( Error!=0 ) { msg(stderr,"bc_set_fd_kh: %s\n",strerror(-Error)); FreeKeyHandle(Alg,KeyHandle); close(Alg.bc_fd); close(fd); return -1; } }//---------------------------------------------------------------------- //---------------------------------------------------------------------- FreeKeyHandle(Alg,KeyHandle); close(fd); close(Alg.bc_fd); if (!o_ro && lock_container(argv[1])) { msg(stderr,"lock_container(\"%s\"): %s\n",argv[1],strerror(errno)); return -1; } return 0;}int bcunlink (int argc, char ** argv){ ALG_SERV Alg; if ( argc < 2 ) { msg(stderr,"Please specify device\n"); return -1; } else if ( argc > 2 ) { msg(stderr,"Too many parameters\n"); return -1; } if ( (Alg.bc_fd = open(argv[1], O_RDONLY)) < 0 ) { msg(stderr,"open(%s): %s\n",argv[1],strerror(errno)); return -1; } Error=bc_clear_fd(Alg); if ( Error!=0 ) { msg(stderr,"clear_fd(%s): ",argv[1]); print_error(Error); close(Alg.bc_fd); return -1; } close(Alg.bc_fd); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -