home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR4 / SECDRV12.ZIP / CRYPTDSK.C < prev    next >
C/C++ Source or Header  |  1994-01-06  |  8KB  |  296 lines

  1. /* Secure Drive CRYPTDSK V1.2 */
  2. /* Encrypts/decrypts disks */
  3.  
  4. #include "secdrv.h"
  5.  
  6. extern char pass1[MAXPASS];
  7. extern char pass2[MAXPASS];
  8.  
  9. void readseca(unsigned drive,unsigned head,unsigned cyl,unsigned sector,
  10.      unsigned nsects,unsigned secsize,unsigned char *buffer);
  11. void writeseca(unsigned drive,unsigned head,unsigned cyl,unsigned sector,
  12.      unsigned nsects,unsigned secsize,unsigned char *buffer);
  13. void rstfsetup(void);
  14. void freebuf(void);
  15. void  clrbufs(void);
  16.  
  17. unsigned char *buf;
  18. char *pgpv;
  19.  
  20. main()
  21. {
  22. unsigned drive,firstcyl,firsthead,encrypt;
  23. unsigned cyl,head,sector,maxcyl,maxhead,maxsector,secsize,t;
  24. unsigned serial[2];
  25. unsigned char key[16],check[4],iv[8];
  26. unsigned char *bufptr,*dummy;
  27. unsigned long i;
  28. char drvltr;
  29. word16 expkey[52];
  30.  
  31. clrscr();
  32.  
  33. cryptdata=gettsradr();
  34.  
  35. if(cryptdata) {
  36.     cryptdata->fda.firstcyl=0x0ffff;
  37.     cryptdata->fdb.firstcyl=0x0ffff;
  38.     atexit(rstfsetup); }
  39.  
  40. if(!(buf=malloc(512))) {
  41.     printf("Error: unable to allocate memory for track buffer.\n");
  42.     exit(1); }
  43.  
  44. atexit(freebuf);
  45.  
  46. printf("\
  47. Secure Drive CryptDisk Version 1.2\n\
  48. \n\
  49. This program will encrypt or decrypt a floppy disk or hard drive\
  50.  partition.\n\
  51. \n");
  52.  
  53. pgpv=getenv("PGPPASS");
  54.  
  55.  
  56. askdrive:
  57. printf("Enter the letter of the drive to process, or X to enter the\n\
  58. drive, cylinder, and head manually, or Z to cancel: ");
  59. while(!isalpha(drvltr=toupper(getch())));
  60. printf("%c\n",drvltr);
  61.  
  62. if((drvltr>'B')&&cryptdata) {
  63.     printf("\nYou cannot encrypt or decrypt a hard drive partition \
  64. while\nthe TSR is resident in memory.\n");
  65.     exit(1); }
  66.  
  67. if(drvltr=='A') { firstcyl=0; firsthead=0; drive=0; }
  68. else if(drvltr=='B') { firstcyl=0; firsthead=0; drive=1; }
  69. else if(drvltr=='X') {
  70.     printf("\nEnter physical drive (0-1), cylinder, and head for the\
  71.  beginning\n\(boot sector) of this partition: ");
  72.     scanf("%u,%u,%u",&drive,&firstcyl,&firsthead);
  73.     drive+=0x80;
  74.     }
  75. else if(drvltr=='Z') { printf("\n"); exit(0); }
  76. else {
  77.     drive=255;
  78.     readptbl(0,0,0,drvltr,&drive,&firsthead,&firstcyl);
  79.     if(drive==255) {
  80.         printf("\nDrive not found.\n\n");
  81.         goto askdrive; }
  82.     printf("\nDrive %c is physical hard drive %u, head %u,\
  83.  cylinder %u\n\n",drvltr,drive,firsthead,firstcyl);
  84.     drive+=0x80;
  85.     }
  86.  
  87. if(drive<0x80) {
  88.    printf("\nInsert disk in drive %c and press any key to\
  89.  continue ",drvltr);
  90.    getch();
  91.    printf("\n\n"); }
  92.  
  93. readsec(drive,firsthead,firstcyl,1,1,buf);
  94. if((buf[510]!=0x55)||(buf[511]!=0xaa)) {
  95.     printf("This is not a boot sector.\n\n");
  96.     exit(1); }
  97.  
  98. encrypt=memcmp(buf+3,"CRYP",4);
  99.  
  100. calcdiskparams(buf,&maxcyl,&maxhead,&maxsector,
  101.                &secsize,serial);
  102.  
  103. printf("This disk has \
  104. %u cyls, %u sectors, %u heads, sector size %u bytes\n\n",
  105.        maxcyl+1,maxsector,maxhead,secsize);
  106.  
  107. if((buf=realloc(buf,maxsector*secsize)) != NULL)
  108.     printf("Allocated %u bytes for track buffer\n\n",
  109.             maxsector*secsize);
  110. else {
  111.     printf("Error: unable to allocate %u bytes for track buffer\n",
  112.             maxsector*secsize);
  113.     exit(1); }
  114.  
  115. if(encrypt)
  116.     {
  117.     printf("This disk is not encrypted. Do you want to encrypt it? ");
  118.     if(!getyn())
  119.         {
  120.         printf("\n");
  121.         exit(0);
  122.         }
  123.     if (pgpv != NULL)
  124.      {
  125.       printf("\nUse PGPPASS as passphrase? ");
  126.       if (getyn())
  127.        {
  128.         printf("\nPGPPASS entered as floppy disk passphrase\n");
  129.         strcpy(pass1,pgpv);
  130.         setkey(key,check);
  131.        }
  132.       else
  133.        {
  134.         getkey(key,check,TRUE);
  135.         clrbufs();
  136.        }
  137.      }
  138.     else
  139.      {
  140.       getkey(key,check,TRUE);
  141.       clrbufs();
  142.      }
  143.     }
  144. else
  145.     {
  146.     printf("This disk is encrypted. Do you want to decrypt it? ");
  147.     if(!getyn())
  148.         {
  149.         printf("\n");
  150.         exit(0);
  151.         }
  152.     if (pgpv != NULL)
  153.      {
  154.       strcpy(pass1,pgpv);
  155.       setkey(key,check);
  156.       if(memcmp(check,buf+7,4))
  157.        {
  158.         printf("\nPGPPASS is wrong passphrase.\n");
  159.         for(t=0;t<3;t++)
  160.          {
  161.           printf("\nEnter passphrase: ");
  162.           getkey(key,check,FALSE);
  163.           clrbufs();
  164.           if(!memcmp(check,buf+7,4)) break;
  165.           printf("Wrong passphrase.\n");
  166.           if(t==2) exit(0);
  167.          }
  168.        }
  169.       else
  170.        printf("\nPGPPASS entered as passphrase.\n");
  171.      }
  172.     else
  173.      {
  174.       for(t=0;t<3;t++)
  175.        {
  176.         printf("\nEnter passphrase: ");
  177.         getkey(key,check,FALSE);
  178.         clrbufs();
  179.         if(!memcmp(check,buf+7,4)) break;
  180.         printf("Wrong passphrase.\n");
  181.         if(t==2) exit(0);
  182.        }
  183.      }
  184.     }
  185.  
  186. printf("\nLast chance to abort. Continue? ");
  187. if(!getyn()) exit(1);
  188. bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  189. clrscr();
  190. gotoxy(1,8);
  191. if (encrypt)
  192.  printf("Encrypting ");
  193. else
  194.  printf("Decrypting ");
  195. printf("disk %c:  %u cyls, %u sectors, %u heads, sector size %u bytes",
  196.        drvltr,maxcyl+1,maxsector,maxhead,secsize);
  197. en_key_idea((word16 *)key,expkey);
  198.  
  199. printf("\n");
  200. for(cyl=0;cyl<=maxcyl;cyl++)
  201.     for(head=0;head<maxhead;head++) {
  202.         if(cyl==0&&head<firsthead) head=firsthead;
  203.         gotoxy(1,10);
  204.         printf("Cyl %u, Head %u ",cyl,head);
  205.         readseca(drive,head,cyl+firstcyl,1,maxsector,secsize,buf);
  206.         bufptr=buf;
  207.         for(sector=1;sector<=maxsector;sector++) {
  208.             if(cyl==0&&head==firsthead&§or==1)
  209.                 if(encrypt) {
  210.                     memcpy(&buf[0x03],"CRYP",4);
  211.                     memcpy(&buf[0x07],check,4); }
  212.                 else
  213.                     memcpy(&buf[0x03],"MSDOS   ",8);
  214.             else {
  215.                 t=cyl+firstcyl;
  216.                 iv[0]=t%256;
  217.                 iv[1]=t/256;
  218.                 iv[2]=head;
  219.                 iv[3]=sector;
  220.                 iv[4]=serial[0]%256;
  221.                 iv[5]=serial[0]/256;
  222.                 iv[6]=serial[1]%256;
  223.                 iv[7]=serial[1]/256;
  224.                 IdeaCFB(iv,expkey,dummy,dummy,1);
  225.                 if(encrypt)
  226.                     IdeaCFB(iv,expkey,bufptr,bufptr,secsize/8+1);
  227.                 else
  228.                     IdeaCFBx(iv,expkey,bufptr,bufptr,secsize/8+1);
  229.                 }
  230.             bufptr+=secsize;
  231.             }
  232.         writeseca(drive,head,cyl+firstcyl,1,maxsector,secsize,buf);
  233.         }
  234.  
  235.  
  236. for(t=0;t<16;t++) key[t]='\0';
  237. for(t=0;t<52;t++) expkey[t]=0;
  238.  
  239. gotoxy(1,12);
  240. printf("\n\nDone.\n");
  241. bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  242. return(0);
  243. }
  244.  
  245. void rstfsetup(void)
  246. {
  247. if(cryptdata) {
  248.     cryptdata->fda.firstcyl=0;
  249.     cryptdata->fdb.firstcyl=0; }
  250. }
  251.  
  252. void freebuf(void)
  253. {
  254. free(buf);
  255. }
  256.  
  257. void readseca(unsigned drive,unsigned head,unsigned cyl,unsigned sector,
  258.      unsigned nsects,unsigned secsize,unsigned char *buffer)
  259. {
  260. unsigned i,j;
  261. char c;
  262. for(i=0;i<3;i++)
  263.   if(!biosdisk(2,drive,head,cyl,sector,nsects,buffer)) return;
  264. printf("\nRead error: drive %02x, head %u, cyl %u\n",
  265.        drive,head,cyl);
  266. printf("Reading one sector at a time.\n");
  267. for(j=0;j<nsects;j++) {
  268.   for(i=0;i<3;i++)
  269.     if(!biosdisk(2,drive,head,cyl,sector+j,1,buffer)) goto goodsec;
  270.   printf("Bad sector: drive %02x, head %u, cyl %u, sector %u\n",
  271.          drive,head,cyl,sector+j);
  272.   goodsec:
  273.   buffer+=secsize;
  274.   }
  275. }
  276.  
  277. void writeseca(unsigned drive,unsigned head,unsigned cyl,unsigned sector,
  278.      unsigned nsects,unsigned secsize,unsigned char *buffer)
  279. {
  280. unsigned i,j;
  281. char c;
  282. for(i=0;i<3;i++)
  283.   if(!biosdisk(3,drive,head,cyl,sector,nsects,buffer)) return;
  284. printf("\nWrite error: drive %02x, head %u, cyl %u\n",
  285.        drive,head,cyl);
  286. printf("Writing one sector at a time.\n");
  287. for(j=0;j<nsects;j++) {
  288.   for(i=0;i<3;i++)
  289.     if(!biosdisk(3,drive,head,cyl,sector+j,1,buffer)) goto goodsec;
  290.   printf("Bad sector: drive %02x, head %u, cyl %u, sector %u\n",
  291.          drive,head,cyl,sector+j);
  292.   goodsec:
  293.   buffer+=secsize;
  294.   }
  295. }
  296.