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

  1. /* Secure Drive LOGIN V1.2 */
  2. /* Logs into encrypted disks */
  3.  
  4. #include "secdrv.h"
  5.  
  6. extern char pass1[MAXPASS];
  7. extern char pass2[MAXPASS];
  8. int   setenv(char*,char *);
  9. void  clrbufs(void);
  10. char *pgpv;
  11.  
  12. void main(int argc,char *argv[])
  13. {
  14. unsigned drive,firstcyl,firsthead,maxcyl;
  15. unsigned maxhead,maxsector,secsize,i;
  16. unsigned safemode=FALSE;
  17. unsigned pgpsw=FALSE;
  18. unsigned char buf[512],key[16],check[4];
  19. word16 expkey[52];
  20. unsigned serial[2];
  21. unsigned char *p;
  22.  
  23. if(argc==1)
  24.  {
  25.     printf("\n\
  26. Secure Drive Login Version 1.2\n\
  27. This program sets parameters and loads passphrases for encrypted\
  28.  drives.\n\n\
  29. LOGIN /F  [/PGP]                 to enter floppy disk passphrase\n\
  30.                                  [use/set PGPPASS]\n\
  31. \n\
  32. LOGIN /C  [/PGP]                 to erase keys and disable encryption\n\
  33.                                  [clear PGPPASS]\n\
  34. \n\
  35. LOGIN drive letter  [/PGP]       to activate an encrypted hard drive\n\
  36.                                  [use/set PGPPASS]\n\
  37. LOGIN D:    (example)\n\
  38. LOGIN D: /S                      to prevent accidental access to an\n\
  39.                                  encrypted drive without logging in\n\
  40. \n\
  41. LOGIN /PGP                       to set PGPPASS environment variable\n\
  42. \n\
  43. LOGIN drive cylinder head        to manually enter parameters for\n\
  44.                                  a hard drive partition\n\
  45. LOGIN drive cylinder head /S     to prevent accidental access\n\
  46. LOGIN 0 100 1 (example)\n\
  47.       drives are numbered from zero\n\n");
  48.     exit(1);
  49.   }
  50.  
  51. if(!(cryptdata=gettsradr()))
  52.  {
  53.   printf("\nError: Secure Drive TSR not loaded.\n");
  54.   exit(1);
  55.  }
  56.  
  57. for (i=1;i<argc;i++)
  58.  {
  59.    if (stricmp(argv[i],"/PGP") == 0)
  60.     {
  61.      pgpsw=TRUE;
  62.      pgpv=getenv("PGPPASS");
  63.      break;
  64.     }
  65.  }
  66.  
  67. if (argc == 2 && pgpsw)
  68.  {
  69.   printf("\nEnter PGPPASS passphrase for PGP Secret Key: ");
  70.   invgets(pass1);
  71.   if (setenv("PGPPASS",pass1) == 0)
  72.    {
  73.     printf("PGPPASS set. \n");
  74.     clrbufs();
  75.     exit(0);
  76.    }
  77.   else
  78.    {
  79.     printf("PGPPASS not set. \n");
  80.     clrbufs();
  81.     exit(1);
  82.    }
  83.  }
  84.  
  85. if((*argv[1]=='/')&&(toupper(*(argv[1]+1))=='C'))
  86.  {
  87.   cryptdata->hd.active=0;
  88.   for(i=0;i<104;i++)
  89.    {
  90.     cryptdata->fkey[i]=0xaa;
  91.     cryptdata->hkey[i]=0xbb;
  92.    }
  93.    for(i=0;i<4;i++)
  94.     cryptdata->fkeychk[i]=0x0ff;
  95.    bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  96.    printf("\nClearing free memory...");
  97.    while ((p=malloc(1024)) != NULL)
  98.     {
  99.      memset(p,0,1024);
  100.     }
  101.    while ((p=malloc(8)) != NULL)
  102.     {
  103.      memset(p,0,8);
  104.     }
  105.  
  106.    printf("\nAll keys erased. System secured.\n");
  107.    if (pgpsw && pgpv != NULL)
  108.     {
  109.      for (i=0;i<strlen(pgpv);i++)
  110.       pass1[i]='x';
  111.      pass1[i]=0;
  112.      setenv("PGPPASS",pass1);
  113.      setenv("PGPPASS","");
  114.      printf("PGPPASS removed from environment\n");
  115.     }
  116.   clrbufs();
  117.   exit(0);
  118.  }
  119.  
  120. else if((*argv[1]=='/')&&(toupper(*(argv[1]+1))=='F'))
  121.  {
  122.    if (pgpsw && pgpv != NULL)
  123.     {
  124.      printf("\nPGPPASS entered as floppy disk passphrase\n");
  125.      strcpy(pass1,pgpv);
  126.      setkey(key,check);
  127.     }
  128.    else
  129.     {
  130.      printf("\nEnter floppy disk passphrase: ");
  131.      getkey(key,check,FALSE);
  132.     }
  133.    en_key_idea((word16 *) key,expkey);
  134.    memcpy(cryptdata->fkey,expkey,104);
  135.    memcpy(cryptdata->fkeychk,check,4);
  136.    cryptdata->fda.firstcyl=0;
  137.    cryptdata->fdb.firstcyl=0;
  138.    printf("\nFloppy disk encryption enabled.\n");
  139.    if (pgpsw && pgpv == NULL && setenv("PGPPASS",pass1) == 0)
  140.     printf("PGPPASS set. \n");
  141.    bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  142.    clrbufs();
  143.    exit(0);
  144.  }
  145.  
  146. else if(isalpha(*argv[1])) {
  147.    char drvltr=toupper(*argv[1]);
  148.    drive=255;
  149.    readptbl(0,0,0,drvltr,&drive,&firsthead,&firstcyl);
  150.    if(drive==255) {
  151.        printf("\nDrive not found.\n");
  152.        exit(1); }
  153.    if((*argv[2]=='/')&&(toupper(*(argv[2]+1))=='S'))
  154.        safemode=TRUE;
  155.    printf("\nDrive %c is physical hard drive %u, head %u,\
  156.  cylinder %u\n",drvltr,drive,firsthead,firstcyl); }
  157.  
  158. else {
  159.     i=sscanf(argv[1],"%u",&drive);
  160.     i=i&&sscanf(argv[2],"%u",&firstcyl);
  161.     i=i&&sscanf(argv[3],"%u",&firsthead);
  162.     if((*argv[4]=='/')&&(toupper(*(argv[4]+1))=='S'))
  163.         safemode=TRUE;
  164.     if(!i) {
  165.         printf("\nIncorrect drive, cyl, or head input.\n\
  166. Run without a command line for help.\n");
  167.         exit(1); }
  168.     }
  169.  
  170. drive+=0x80;
  171.  
  172. readsec(drive,firsthead,firstcyl,1,1,buf);
  173.  
  174. if((buf[510]!=0x55)||(buf[511]!=0xaa)) {
  175.     printf("\nThis is not a boot sector.\n");
  176.     exit(1); }
  177.  
  178. calcdiskparams(buf,&maxcyl,&maxhead,&maxsector,
  179.                &secsize,serial);
  180.  
  181. printf("\nThis disk has: %i cyls, %i sectors, %i heads, sector \
  182. size %i bytes\n",maxcyl+1,maxsector,maxhead,secsize);
  183.  
  184. if(memcmp("CRYP",buf+3,4)) {
  185.     printf("\nThis disk is not encrypted.\n");
  186.     exit(1); }
  187.  
  188. if(!safemode)
  189. {
  190.  if (pgpsw && pgpv != NULL)
  191.   {
  192.    strcpy(pass1,pgpv);
  193.    setkey(key,check);
  194.    if(memcmp(check,buf+7,4))
  195.     {
  196.      printf("\nPGPPASS is wrong passphrase.\n");
  197.      printf("\nEnter hard disk passphrase: ");
  198.      getkey(key,check,FALSE);
  199.     }
  200.    else
  201.     printf("\nPGPPASS entered as hard disk passphrase.\n");
  202.   }
  203.  else
  204.   {
  205.    printf("\nEnter hard disk passphrase: ");
  206.    getkey(key,check,FALSE);
  207.   }
  208.  
  209.  if(memcmp(check,buf+7,4))
  210.   {
  211.    printf("\nWrong passphrase.\n");
  212.    exit(1);
  213.   }
  214.  
  215.  en_key_idea((word16 *)key,expkey);
  216.  
  217.  memcpy(cryptdata->hkey,expkey,104);
  218.  }
  219.  
  220. cryptdata->hdnum=drive;
  221. cryptdata->hd.firstcyl=firstcyl;
  222. cryptdata->hd.firsthd=firsthead;
  223. cryptdata->hd.firstsec=1;
  224. cryptdata->hd.lastcyl=firstcyl+maxcyl;
  225. cryptdata->hd.maxsec=maxsector;
  226. cryptdata->hd.maxhd=maxhead;
  227. cryptdata->hd.secsize=secsize;
  228. cryptdata->hdnum=drive;
  229. if(!safemode)
  230.  {
  231.   cryptdata->hd.serial[0]=serial[0];
  232.   cryptdata->hd.serial[1]=serial[1];
  233.   cryptdata->hd.active=1;
  234.   printf("\nHard disk encryption enabled.\n");
  235.   if (pgpsw && pgpv == NULL && setenv("PGPPASS",pass1) == 0)
  236.    printf("PGPPASS set. \n");
  237.  }
  238. else
  239.  printf("\nHard disk set to safe mode.\n");
  240.  
  241. for(i=0;i<16;i++) key[i]='\0';
  242. for(i=0;i<52;i++) expkey[i]=0;
  243.  
  244. bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  245. clrbufs();
  246. exit(0);
  247. }
  248.  
  249.